blob: 77b8cf93ec81b1c6f3d2aba4d279a68c9391963e [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 Gregor57016dd2012-10-16 23:40:58 +000038#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Sebastian Redlade50002010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000060}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000095 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000096}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000189 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000190 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000191 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000192 Record.push_back(T->getExceptionSpecType());
193 if (T->getExceptionSpecType() == EST_Dynamic) {
194 Record.push_back(T->getNumExceptions());
195 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
196 Writer.AddTypeRef(T->getExceptionType(I), Record);
197 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
198 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000199 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
200 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
201 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000202 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000204 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000205 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206}
207
Sebastian Redl3397c552010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000209 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000211}
John McCalled976492009-12-04 22:46:56 +0000212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000215 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
216 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000217 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000218}
219
Sebastian Redl3397c552010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000221 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000222 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223}
224
Sebastian Redl3397c552010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000227 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228}
229
Sebastian Redl3397c552010-08-18 23:56:27 +0000230void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000232 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000233 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000234}
235
Sean Huntca63c202011-05-24 22:41:36 +0000236void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
237 Writer.AddTypeRef(T->getBaseType(), Record);
238 Writer.AddTypeRef(T->getUnderlyingType(), Record);
239 Record.push_back(T->getUTTKind());
240 Code = TYPE_UNARY_TRANSFORM;
241}
242
Richard Smith34b41d92011-02-20 03:19:35 +0000243void ASTTypeWriter::VisitAutoType(const AutoType *T) {
244 Writer.AddTypeRef(T->getDeducedType(), Record);
245 Code = TYPE_AUTO;
246}
247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000249 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000250 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000251 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000252 "Cannot serialize in the middle of a type definition");
253}
254
Sebastian Redl3397c552010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000257 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258}
259
Sebastian Redl3397c552010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000262 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263}
264
John McCall9d156a72011-01-06 01:58:22 +0000265void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
266 Writer.AddTypeRef(T->getModifiedType(), Record);
267 Writer.AddTypeRef(T->getEquivalentType(), Record);
268 Record.push_back(T->getAttrKind());
269 Code = TYPE_ATTRIBUTED;
270}
271
Mike Stump1eb44332009-09-09 15:08:12 +0000272void
Sebastian Redl3397c552010-08-18 23:56:27 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000274 const SubstTemplateTypeParmType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000278}
279
280void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000281ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
282 const SubstTemplateTypeParmPackType *T) {
283 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
284 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
285 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
286}
287
288void
Sebastian Redl3397c552010-08-18 23:56:27 +0000289ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000290 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000291 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000292 Writer.AddTemplateName(T->getTemplateName(), Record);
293 Record.push_back(T->getNumArgs());
294 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
295 ArgI != ArgE; ++ArgI)
296 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000297 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
298 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000299 : T->getCanonicalTypeInternal(),
300 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000306 VisitArrayType(T);
307 Writer.AddStmt(T->getSizeExpr());
308 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000309 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314 const DependentSizedExtVectorType *T) {
315 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000316 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317}
318
319void
Sebastian Redl3397c552010-08-18 23:56:27 +0000320ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000321 Record.push_back(T->getDepth());
322 Record.push_back(T->getIndex());
323 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000324 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000325 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000326}
327
328void
Sebastian Redl3397c552010-08-18 23:56:27 +0000329ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000330 Record.push_back(T->getKeyword());
331 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
332 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000333 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
334 : T->getCanonicalTypeInternal(),
335 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000336 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000337}
338
339void
Sebastian Redl3397c552010-08-18 23:56:27 +0000340ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000341 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000342 Record.push_back(T->getKeyword());
343 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
344 Writer.AddIdentifierRef(T->getIdentifier(), Record);
345 Record.push_back(T->getNumArgs());
346 for (DependentTemplateSpecializationType::iterator
347 I = T->begin(), E = T->end(); I != E; ++I)
348 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000349 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000350}
351
Douglas Gregor7536dd52010-12-20 02:24:11 +0000352void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
353 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000354 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
355 Record.push_back(*NumExpansions + 1);
356 else
357 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000358 Code = TYPE_PACK_EXPANSION;
359}
360
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000361void ASTTypeWriter::VisitParenType(const ParenType *T) {
362 Writer.AddTypeRef(T->getInnerType(), Record);
363 Code = TYPE_PAREN;
364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000367 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000370 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000371}
372
Sebastian Redl3397c552010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000375 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000376 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000377}
378
Sebastian Redl3397c552010-08-18 23:56:27 +0000379void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000380 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000381 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000382}
383
Sebastian Redl3397c552010-08-18 23:56:27 +0000384void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000385 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000386 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000387 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000388 E = T->qual_end(); I != E; ++I)
389 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000390 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000391}
392
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000393void
Sebastian Redl3397c552010-08-18 23:56:27 +0000394ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000395 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000396 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000397}
398
Eli Friedmanb001de72011-10-06 23:00:33 +0000399void
400ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
401 Writer.AddTypeRef(T->getValueType(), Record);
402 Code = TYPE_ATOMIC;
403}
404
John McCalla1ee0c52009-10-16 21:56:05 +0000405namespace {
406
407class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000408 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000409 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000410
411public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000412 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000413 : Writer(Writer), Record(Record) { }
414
John McCall51bd8032009-10-18 01:05:36 +0000415#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000416#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000417 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000418#include "clang/AST/TypeLocNodes.def"
419
John McCall51bd8032009-10-18 01:05:36 +0000420 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
421 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000422};
423
424}
425
John McCall51bd8032009-10-18 01:05:36 +0000426void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
427 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000428}
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000430 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
431 if (TL.needsExtraLocalData()) {
432 Record.push_back(TL.getWrittenTypeSpec());
433 Record.push_back(TL.getWrittenSignSpec());
434 Record.push_back(TL.getWrittenWidthSpec());
435 Record.push_back(TL.hasModeAttr());
436 }
John McCalla1ee0c52009-10-16 21:56:05 +0000437}
John McCall51bd8032009-10-18 01:05:36 +0000438void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000455 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000456}
John McCall51bd8032009-10-18 01:05:36 +0000457void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
459 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
460 Record.push_back(TL.getSizeExpr() ? 1 : 0);
461 if (TL.getSizeExpr())
462 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000463}
John McCall51bd8032009-10-18 01:05:36 +0000464void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
465 VisitArrayTypeLoc(TL);
466}
467void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
474 DependentSizedArrayTypeLoc TL) {
475 VisitArrayTypeLoc(TL);
476}
477void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
478 DependentSizedExtVectorTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000488 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000489 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
490 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000492 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
493 Writer.AddDeclRef(TL.getArg(i), Record);
494}
495void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
496 VisitFunctionTypeLoc(TL);
497}
498void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
John McCalled976492009-12-04 22:46:56 +0000501void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCall51bd8032009-10-18 01:05:36 +0000504void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000508 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000511}
512void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000513 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
Sean Huntca63c202011-05-24 22:41:36 +0000521void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getKWLoc(), Record);
523 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
524 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
525 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
526}
Richard Smith34b41d92011-02-20 03:19:35 +0000527void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
John McCall51bd8032009-10-18 01:05:36 +0000530void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
533void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
John McCall9d156a72011-01-06 01:58:22 +0000536void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
538 if (TL.hasAttrOperand()) {
539 SourceRange range = TL.getAttrOperandParensRange();
540 Writer.AddSourceLocation(range.getBegin(), Record);
541 Writer.AddSourceLocation(range.getEnd(), Record);
542 }
543 if (TL.hasAttrExprOperand()) {
544 Expr *operand = TL.getAttrExprOperand();
545 Record.push_back(operand ? 1 : 0);
546 if (operand) Writer.AddStmt(operand);
547 } else if (TL.hasAttrEnumOperand()) {
548 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
549 }
550}
John McCall51bd8032009-10-18 01:05:36 +0000551void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
John McCall49a832b2009-10-18 09:09:24 +0000554void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
555 SubstTemplateTypeParmTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000558void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
559 SubstTemplateTypeParmPackTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
John McCall51bd8032009-10-18 01:05:36 +0000562void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
563 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000564 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000565 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
566 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
567 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
568 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000569 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
570 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000571}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000572void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
573 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
574 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
575}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000576void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000579}
John McCall3cb0ebd2010-03-10 03:28:59 +0000580void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
Douglas Gregor4714c122010-03-31 17:34:00 +0000583void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000584 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000585 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
587}
John McCall33500952010-06-11 00:33:02 +0000588void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
589 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000590 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000591 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000592 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000594 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
595 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
596 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000597 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
598 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000599}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000600void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
602}
John McCall51bd8032009-10-18 01:05:36 +0000603void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000605}
606void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
607 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000608 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
609 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
610 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
611 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000612}
John McCall54e14c42009-10-22 22:37:11 +0000613void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
614 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000615}
Eli Friedmanb001de72011-10-06 23:00:33 +0000616void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getKWLoc(), Record);
618 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
619 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
620}
John McCalla1ee0c52009-10-16 21:56:05 +0000621
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000622//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000623// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000624//===----------------------------------------------------------------------===//
625
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000626static void EmitBlockID(unsigned ID, const char *Name,
627 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000628 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000629 Record.clear();
630 Record.push_back(ID);
631 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
632
633 // Emit the block name if present.
634 if (Name == 0 || Name[0] == 0) return;
635 Record.clear();
636 while (*Name)
637 Record.push_back(*Name++);
638 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
639}
640
641static void EmitRecordID(unsigned ID, const char *Name,
642 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000643 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000644 Record.clear();
645 Record.push_back(ID);
646 while (*Name)
647 Record.push_back(*Name++);
648 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000649}
650
651static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000652 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000653#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000654 RECORD(STMT_STOP);
655 RECORD(STMT_NULL_PTR);
656 RECORD(STMT_NULL);
657 RECORD(STMT_COMPOUND);
658 RECORD(STMT_CASE);
659 RECORD(STMT_DEFAULT);
660 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000661 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000662 RECORD(STMT_IF);
663 RECORD(STMT_SWITCH);
664 RECORD(STMT_WHILE);
665 RECORD(STMT_DO);
666 RECORD(STMT_FOR);
667 RECORD(STMT_GOTO);
668 RECORD(STMT_INDIRECT_GOTO);
669 RECORD(STMT_CONTINUE);
670 RECORD(STMT_BREAK);
671 RECORD(STMT_RETURN);
672 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000673 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000674 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000675 RECORD(EXPR_PREDEFINED);
676 RECORD(EXPR_DECL_REF);
677 RECORD(EXPR_INTEGER_LITERAL);
678 RECORD(EXPR_FLOATING_LITERAL);
679 RECORD(EXPR_IMAGINARY_LITERAL);
680 RECORD(EXPR_STRING_LITERAL);
681 RECORD(EXPR_CHARACTER_LITERAL);
682 RECORD(EXPR_PAREN);
683 RECORD(EXPR_UNARY_OPERATOR);
684 RECORD(EXPR_SIZEOF_ALIGN_OF);
685 RECORD(EXPR_ARRAY_SUBSCRIPT);
686 RECORD(EXPR_CALL);
687 RECORD(EXPR_MEMBER);
688 RECORD(EXPR_BINARY_OPERATOR);
689 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
690 RECORD(EXPR_CONDITIONAL_OPERATOR);
691 RECORD(EXPR_IMPLICIT_CAST);
692 RECORD(EXPR_CSTYLE_CAST);
693 RECORD(EXPR_COMPOUND_LITERAL);
694 RECORD(EXPR_EXT_VECTOR_ELEMENT);
695 RECORD(EXPR_INIT_LIST);
696 RECORD(EXPR_DESIGNATED_INIT);
697 RECORD(EXPR_IMPLICIT_VALUE_INIT);
698 RECORD(EXPR_VA_ARG);
699 RECORD(EXPR_ADDR_LABEL);
700 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000701 RECORD(EXPR_CHOOSE);
702 RECORD(EXPR_GNU_NULL);
703 RECORD(EXPR_SHUFFLE_VECTOR);
704 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000705 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000706 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000707 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000708 RECORD(EXPR_OBJC_ARRAY_LITERAL);
709 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000710 RECORD(EXPR_OBJC_ENCODE);
711 RECORD(EXPR_OBJC_SELECTOR_EXPR);
712 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
713 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
714 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
715 RECORD(EXPR_OBJC_KVC_REF_EXPR);
716 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000717 RECORD(STMT_OBJC_FOR_COLLECTION);
718 RECORD(STMT_OBJC_CATCH);
719 RECORD(STMT_OBJC_FINALLY);
720 RECORD(STMT_OBJC_AT_TRY);
721 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
722 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000723 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000724 RECORD(EXPR_CXX_OPERATOR_CALL);
725 RECORD(EXPR_CXX_CONSTRUCT);
726 RECORD(EXPR_CXX_STATIC_CAST);
727 RECORD(EXPR_CXX_DYNAMIC_CAST);
728 RECORD(EXPR_CXX_REINTERPRET_CAST);
729 RECORD(EXPR_CXX_CONST_CAST);
730 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000731 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000732 RECORD(EXPR_CXX_BOOL_LITERAL);
733 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000734 RECORD(EXPR_CXX_TYPEID_EXPR);
735 RECORD(EXPR_CXX_TYPEID_TYPE);
736 RECORD(EXPR_CXX_UUIDOF_EXPR);
737 RECORD(EXPR_CXX_UUIDOF_TYPE);
738 RECORD(EXPR_CXX_THIS);
739 RECORD(EXPR_CXX_THROW);
740 RECORD(EXPR_CXX_DEFAULT_ARG);
741 RECORD(EXPR_CXX_BIND_TEMPORARY);
742 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
743 RECORD(EXPR_CXX_NEW);
744 RECORD(EXPR_CXX_DELETE);
745 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
746 RECORD(EXPR_EXPR_WITH_CLEANUPS);
747 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
748 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
749 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
750 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
751 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
752 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
753 RECORD(EXPR_CXX_NOEXCEPT);
754 RECORD(EXPR_OPAQUE_VALUE);
755 RECORD(EXPR_BINARY_TYPE_TRAIT);
756 RECORD(EXPR_PACK_EXPANSION);
757 RECORD(EXPR_SIZEOF_PACK);
758 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000759 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000760#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761}
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Sebastian Redla4232eb2010-08-18 23:56:21 +0000763void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764 RecordData Record;
765 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000767#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
768#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000770 // Control Block.
771 BLOCK(CONTROL_BLOCK);
772 RECORD(METADATA);
773 RECORD(IMPORTS);
774 RECORD(LANGUAGE_OPTIONS);
775 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000776 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000777 RECORD(ORIGINAL_PCH_DIR);
778
Douglas Gregorc337fef2012-10-19 00:45:00 +0000779 BLOCK(INPUT_FILES_BLOCK);
780 RECORD(INPUT_FILE);
781
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000782 // AST Top-Level Block.
783 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000784 RECORD(TYPE_OFFSET);
785 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000786 RECORD(IDENTIFIER_OFFSET);
787 RECORD(IDENTIFIER_TABLE);
788 RECORD(EXTERNAL_DEFINITIONS);
789 RECORD(SPECIAL_TYPES);
790 RECORD(STATISTICS);
791 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000792 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
794 RECORD(SELECTOR_OFFSETS);
795 RECORD(METHOD_POOL);
796 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000797 RECORD(SOURCE_LOCATION_OFFSETS);
798 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000799 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000800 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000801 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000802 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000803 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000804 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000805 RECORD(SEMA_DECL_REFS);
806 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
807 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
808 RECORD(DECL_REPLACEMENTS);
809 RECORD(UPDATE_VISIBLE);
810 RECORD(DECL_UPDATE_OFFSETS);
811 RECORD(DECL_UPDATES);
812 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
813 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000814 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000815 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000816 RECORD(FP_PRAGMA_OPTIONS);
817 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000818 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000819 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000820 RECORD(MODULE_OFFSET_MAP);
821 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000822 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000823 RECORD(FILE_SORTED_DECLS);
824 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000825 RECORD(MERGED_DECLARATIONS);
826 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000827 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000828 RECORD(MACRO_OFFSET);
829 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000830
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000831 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000832 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000833 RECORD(SM_SLOC_FILE_ENTRY);
834 RECORD(SM_SLOC_BUFFER_ENTRY);
835 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000836 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000838 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000839 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 RECORD(PP_MACRO_OBJECT_LIKE);
841 RECORD(PP_MACRO_FUNCTION_LIKE);
842 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000843
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000844 // Decls and Types block.
845 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000846 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 RECORD(TYPE_COMPLEX);
848 RECORD(TYPE_POINTER);
849 RECORD(TYPE_BLOCK_POINTER);
850 RECORD(TYPE_LVALUE_REFERENCE);
851 RECORD(TYPE_RVALUE_REFERENCE);
852 RECORD(TYPE_MEMBER_POINTER);
853 RECORD(TYPE_CONSTANT_ARRAY);
854 RECORD(TYPE_INCOMPLETE_ARRAY);
855 RECORD(TYPE_VARIABLE_ARRAY);
856 RECORD(TYPE_VECTOR);
857 RECORD(TYPE_EXT_VECTOR);
858 RECORD(TYPE_FUNCTION_PROTO);
859 RECORD(TYPE_FUNCTION_NO_PROTO);
860 RECORD(TYPE_TYPEDEF);
861 RECORD(TYPE_TYPEOF_EXPR);
862 RECORD(TYPE_TYPEOF);
863 RECORD(TYPE_RECORD);
864 RECORD(TYPE_ENUM);
865 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000866 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000867 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000868 RECORD(TYPE_DECLTYPE);
869 RECORD(TYPE_ELABORATED);
870 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
871 RECORD(TYPE_UNRESOLVED_USING);
872 RECORD(TYPE_INJECTED_CLASS_NAME);
873 RECORD(TYPE_OBJC_OBJECT);
874 RECORD(TYPE_TEMPLATE_TYPE_PARM);
875 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
876 RECORD(TYPE_DEPENDENT_NAME);
877 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
878 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
879 RECORD(TYPE_PAREN);
880 RECORD(TYPE_PACK_EXPANSION);
881 RECORD(TYPE_ATTRIBUTED);
882 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000883 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000884 RECORD(DECL_TYPEDEF);
885 RECORD(DECL_ENUM);
886 RECORD(DECL_RECORD);
887 RECORD(DECL_ENUM_CONSTANT);
888 RECORD(DECL_FUNCTION);
889 RECORD(DECL_OBJC_METHOD);
890 RECORD(DECL_OBJC_INTERFACE);
891 RECORD(DECL_OBJC_PROTOCOL);
892 RECORD(DECL_OBJC_IVAR);
893 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000894 RECORD(DECL_OBJC_CATEGORY);
895 RECORD(DECL_OBJC_CATEGORY_IMPL);
896 RECORD(DECL_OBJC_IMPLEMENTATION);
897 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
898 RECORD(DECL_OBJC_PROPERTY);
899 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000900 RECORD(DECL_FIELD);
901 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000902 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000903 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000904 RECORD(DECL_FILE_SCOPE_ASM);
905 RECORD(DECL_BLOCK);
906 RECORD(DECL_CONTEXT_LEXICAL);
907 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000908 RECORD(DECL_NAMESPACE);
909 RECORD(DECL_NAMESPACE_ALIAS);
910 RECORD(DECL_USING);
911 RECORD(DECL_USING_SHADOW);
912 RECORD(DECL_USING_DIRECTIVE);
913 RECORD(DECL_UNRESOLVED_USING_VALUE);
914 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
915 RECORD(DECL_LINKAGE_SPEC);
916 RECORD(DECL_CXX_RECORD);
917 RECORD(DECL_CXX_METHOD);
918 RECORD(DECL_CXX_CONSTRUCTOR);
919 RECORD(DECL_CXX_DESTRUCTOR);
920 RECORD(DECL_CXX_CONVERSION);
921 RECORD(DECL_ACCESS_SPEC);
922 RECORD(DECL_FRIEND);
923 RECORD(DECL_FRIEND_TEMPLATE);
924 RECORD(DECL_CLASS_TEMPLATE);
925 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
926 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
927 RECORD(DECL_FUNCTION_TEMPLATE);
928 RECORD(DECL_TEMPLATE_TYPE_PARM);
929 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
930 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
931 RECORD(DECL_STATIC_ASSERT);
932 RECORD(DECL_CXX_BASE_SPECIFIERS);
933 RECORD(DECL_INDIRECTFIELD);
934 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
935
Douglas Gregora72d8c42011-06-03 02:27:19 +0000936 // Statements and Exprs can occur in the Decls and Types block.
937 AddStmtsExprs(Stream, Record);
938
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000939 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000940 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000941 RECORD(PPD_MACRO_DEFINITION);
942 RECORD(PPD_INCLUSION_DIRECTIVE);
943
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000944#undef RECORD
945#undef BLOCK
946 Stream.ExitBlock();
947}
948
Douglas Gregore650c8c2009-07-07 00:12:59 +0000949/// \brief Adjusts the given filename to only write out the portion of the
950/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000951///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952/// \param Filename the file name to adjust.
953///
954/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
955/// the returned filename will be adjusted by this system root.
956///
957/// \returns either the original filename (if it needs no adjustment) or the
958/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000959static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000960adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregor832d6202011-07-22 16:35:34 +0000963 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Douglas Gregore650c8c2009-07-07 00:12:59 +0000966 // Verify that the filename and the system root have the same prefix.
967 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000968 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 if (Filename[Pos] != isysroot[Pos])
970 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 // We hit the end of the filename before we hit the end of the system root.
973 if (!Filename[Pos])
974 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 // If the file name has a '/' at the current position, skip over the '/'.
977 // We distinguish sysroot-based includes from absolute includes by the
978 // absence of '/' at the beginning of sysroot-based includes.
979 if (Filename[Pos] == '/')
980 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Douglas Gregore650c8c2009-07-07 00:12:59 +0000982 return Filename + Pos;
983}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000984
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000985/// \brief Write the control block.
986void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
987 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000988 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000989 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
990 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000991
Douglas Gregore650c8c2009-07-07 00:12:59 +0000992 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000993 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
994 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
995 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
996 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
997 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
998 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
999 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1000 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1002 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1003 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001004 Record.push_back(VERSION_MAJOR);
1005 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001006 Record.push_back(CLANG_VERSION_MAJOR);
1007 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001008 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001009 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001010 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1011 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001012
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001013 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001014 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001015 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1016 llvm::SmallVector<char, 128> ModulePaths;
1017 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001018
1019 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1020 M != MEnd; ++M) {
1021 // Skip modules that weren't directly imported.
1022 if (!(*M)->isDirectlyImported())
1023 continue;
1024
1025 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1026 // FIXME: Write import location, once it matters.
1027 // FIXME: This writes the absolute path for AST files we depend on.
1028 const std::string &FileName = (*M)->FileName;
1029 Record.push_back(FileName.size());
1030 Record.append(FileName.begin(), FileName.end());
1031 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001032 Stream.EmitRecord(IMPORTS, Record);
1033 }
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001035 // Language options.
1036 Record.clear();
1037 const LangOptions &LangOpts = Context.getLangOpts();
1038#define LANGOPT(Name, Bits, Default, Description) \
1039 Record.push_back(LangOpts.Name);
1040#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1041 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1042#include "clang/Basic/LangOptions.def"
1043
1044 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1045 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1046
1047 Record.push_back(LangOpts.CurrentModule.size());
1048 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1049 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1050
Douglas Gregoree097c12012-10-18 17:58:09 +00001051 // Target options.
1052 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001053 const TargetInfo &Target = Context.getTargetInfo();
1054 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001055 AddString(TargetOpts.Triple, Record);
1056 AddString(TargetOpts.CPU, Record);
1057 AddString(TargetOpts.ABI, Record);
1058 AddString(TargetOpts.CXXABI, Record);
1059 AddString(TargetOpts.LinkerVersion, Record);
1060 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1061 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1062 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1063 }
1064 Record.push_back(TargetOpts.Features.size());
1065 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1066 AddString(TargetOpts.Features[I], Record);
1067 }
1068 Stream.EmitRecord(TARGET_OPTIONS, Record);
1069
Douglas Gregor31d375f2011-05-06 21:43:30 +00001070 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001071 SourceManager &SM = Context.getSourceManager();
1072 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1073 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001074 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1075 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001076 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1077 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1078
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001079 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001081 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001082
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001083 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001084 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001085 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001086 RecordData Record;
Douglas Gregor39c497b2012-10-18 18:36:53 +00001087 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001088 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001089 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1090 Record.clear();
Douglas Gregorb64c1932009-05-12 01:31:05 +00001091 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001092
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001093 // Original PCH directory
1094 if (!OutputFile.empty() && OutputFile != "-") {
1095 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1096 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1098 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1099
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001100 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001101
1102 llvm::sys::fs::make_absolute(OutputPath);
1103 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1104
1105 RecordData Record;
1106 Record.push_back(ORIGINAL_PCH_DIR);
1107 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1108 }
1109
Douglas Gregor745e6f12012-10-19 00:38:02 +00001110 WriteInputFiles(Context.SourceMgr, isysroot);
1111 Stream.ExitBlock();
1112}
1113
1114void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1115 using namespace llvm;
1116 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1117 RecordData Record;
1118
1119 // Create input-file abbreviation.
1120 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1121 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1122 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1123 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1124 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1125 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1126
1127 // Write out all of the input files.
1128 std::vector<uint32_t> InputFileOffsets;
1129 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1130 // Get this source location entry.
1131 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001132 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001133
1134 // We only care about file entries that were not overridden.
1135 if (!SLoc->isFile())
1136 continue;
1137 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1138 if (!Cache->OrigEntry || Cache->BufferOverridden)
1139 continue;
1140
1141 Record.clear();
1142 Record.push_back(INPUT_FILE);
1143
1144 // Emit size/modification time for this file.
1145 Record.push_back(Cache->OrigEntry->getSize());
1146 Record.push_back(Cache->OrigEntry->getModificationTime());
1147
1148 // Turn the file name into an absolute path, if it isn't already.
1149 const char *Filename = Cache->OrigEntry->getName();
1150 SmallString<128> FilePath(Filename);
1151
1152 // Ask the file manager to fixup the relative path for us. This will
1153 // honor the working directory.
1154 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1155
1156 // FIXME: This call to make_absolute shouldn't be necessary, the
1157 // call to FixupRelativePath should always return an absolute path.
1158 llvm::sys::fs::make_absolute(FilePath);
1159 Filename = FilePath.c_str();
1160
1161 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1162
1163 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1164 }
1165
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001166 Stream.ExitBlock();
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001167}
1168
Douglas Gregor14f79002009-04-10 03:52:48 +00001169//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001170// stat cache Serialization
1171//===----------------------------------------------------------------------===//
1172
1173namespace {
1174// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001175class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001176public:
1177 typedef const char * key_type;
1178 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattner74e976b2010-11-23 19:28:12 +00001180 typedef struct stat data_type;
1181 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001182
1183 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001184 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001185 }
Mike Stump1eb44332009-09-09 15:08:12 +00001186
1187 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001188 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001189 data_type_ref Data) {
1190 unsigned StrLen = strlen(path);
1191 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001192 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001193 clang::io::Emit8(Out, DataLen);
1194 return std::make_pair(StrLen + 1, DataLen);
1195 }
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Chris Lattner5f9e2722011-07-23 10:55:15 +00001197 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001198 Out.write(path, KeyLen);
1199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Chris Lattner5f9e2722011-07-23 10:55:15 +00001201 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001202 data_type_ref Data, unsigned DataLen) {
1203 using namespace clang::io;
1204 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Chris Lattner74e976b2010-11-23 19:28:12 +00001206 Emit32(Out, (uint32_t) Data.st_ino);
1207 Emit32(Out, (uint32_t) Data.st_dev);
1208 Emit16(Out, (uint16_t) Data.st_mode);
1209 Emit64(Out, (uint64_t) Data.st_mtime);
1210 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001211
1212 assert(Out.tell() - Start == DataLen && "Wrong data length");
1213 }
1214};
1215} // end anonymous namespace
1216
Sebastian Redl3397c552010-08-18 23:56:27 +00001217/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001218void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001219 // Build the on-disk hash table containing information about every
1220 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001221 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001222 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001223 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001224 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001225 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001226 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001227 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001228 }
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001230 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001231 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001232 uint32_t BucketOffset;
1233 {
1234 llvm::raw_svector_ostream Out(StatCacheData);
1235 // Make sure that no bucket is at offset 0
1236 clang::io::Emit32(Out, 0);
1237 BucketOffset = Generator.Emit(Out);
1238 }
1239
1240 // Create a blob abbreviation
1241 using namespace llvm;
1242 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001243 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1247 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1248
1249 // Write the stat cache
1250 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001251 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001252 Record.push_back(BucketOffset);
1253 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001254 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001255}
1256
1257//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001258// Source Manager Serialization
1259//===----------------------------------------------------------------------===//
1260
1261/// \brief Create an abbreviation for the SLocEntry that refers to a
1262/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001263static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001264 using namespace llvm;
1265 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001266 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001271 // FileEntry fields.
1272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001279 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001280}
1281
1282/// \brief Create an abbreviation for the SLocEntry that refers to a
1283/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001284static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001285 using namespace llvm;
1286 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001287 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001293 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001294}
1295
1296/// \brief Create an abbreviation for the SLocEntry that refers to a
1297/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001298static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001299 using namespace llvm;
1300 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001301 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001303 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001304}
1305
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001306/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1307/// expansion.
1308static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001309 using namespace llvm;
1310 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001311 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001317 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001318}
1319
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001320namespace {
1321 // Trait used for the on-disk hash table of header search information.
1322 class HeaderFileInfoTrait {
1323 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001324
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001325 // Keep track of the framework names we've used during serialization.
1326 SmallVector<char, 128> FrameworkStringData;
1327 llvm::StringMap<unsigned> FrameworkNameOffset;
1328
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001329 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001330 HeaderFileInfoTrait(ASTWriter &Writer)
1331 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001332
1333 typedef const char *key_type;
1334 typedef key_type key_type_ref;
1335
1336 typedef HeaderFileInfo data_type;
1337 typedef const data_type &data_type_ref;
1338
1339 static unsigned ComputeHash(const char *path) {
1340 // The hash is based only on the filename portion of the key, so that the
1341 // reader can match based on filenames when symlinking or excess path
1342 // elements ("foo/../", "../") change the form of the name. However,
1343 // complete path is still the key.
1344 return llvm::HashString(llvm::sys::path::filename(path));
1345 }
1346
1347 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001348 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001349 data_type_ref Data) {
1350 unsigned StrLen = strlen(path);
1351 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001352 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001353 clang::io::Emit8(Out, DataLen);
1354 return std::make_pair(StrLen + 1, DataLen);
1355 }
1356
Chris Lattner5f9e2722011-07-23 10:55:15 +00001357 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001358 Out.write(path, KeyLen);
1359 }
1360
Chris Lattner5f9e2722011-07-23 10:55:15 +00001361 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001362 data_type_ref Data, unsigned DataLen) {
1363 using namespace clang::io;
1364 uint64_t Start = Out.tell(); (void)Start;
1365
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001366 unsigned char Flags = (Data.isImport << 5)
1367 | (Data.isPragmaOnce << 4)
1368 | (Data.DirInfo << 2)
1369 | (Data.Resolved << 1)
1370 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001371 Emit8(Out, (uint8_t)Flags);
1372 Emit16(Out, (uint16_t) Data.NumIncludes);
1373
1374 if (!Data.ControllingMacro)
1375 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1376 else
1377 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001378
1379 unsigned Offset = 0;
1380 if (!Data.Framework.empty()) {
1381 // If this header refers into a framework, save the framework name.
1382 llvm::StringMap<unsigned>::iterator Pos
1383 = FrameworkNameOffset.find(Data.Framework);
1384 if (Pos == FrameworkNameOffset.end()) {
1385 Offset = FrameworkStringData.size() + 1;
1386 FrameworkStringData.append(Data.Framework.begin(),
1387 Data.Framework.end());
1388 FrameworkStringData.push_back(0);
1389
1390 FrameworkNameOffset[Data.Framework] = Offset;
1391 } else
1392 Offset = Pos->second;
1393 }
1394 Emit32(Out, Offset);
1395
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001396 assert(Out.tell() - Start == DataLen && "Wrong data length");
1397 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001398
1399 const char *strings_begin() const { return FrameworkStringData.begin(); }
1400 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001401 };
1402} // end anonymous namespace
1403
1404/// \brief Write the header search block for the list of files that
1405///
1406/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001407void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001408 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001409 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1410
1411 if (FilesByUID.size() > HS.header_file_size())
1412 FilesByUID.resize(HS.header_file_size());
1413
Benjamin Kramerfacde172012-06-06 17:32:50 +00001414 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001415 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001416 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001417 unsigned NumHeaderSearchEntries = 0;
1418 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1419 const FileEntry *File = FilesByUID[UID];
1420 if (!File)
1421 continue;
1422
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001423 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1424 // from the external source if it was not provided already.
1425 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001426 if (HFI.External && Chain)
1427 continue;
1428
1429 // Turn the file name into an absolute path, if it isn't already.
1430 const char *Filename = File->getName();
1431 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1432
1433 // If we performed any translation on the file name at all, we need to
1434 // save this string, since the generator will refer to it later.
1435 if (Filename != File->getName()) {
1436 Filename = strdup(Filename);
1437 SavedStrings.push_back(Filename);
1438 }
1439
1440 Generator.insert(Filename, HFI, GeneratorTrait);
1441 ++NumHeaderSearchEntries;
1442 }
1443
1444 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001445 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001446 uint32_t BucketOffset;
1447 {
1448 llvm::raw_svector_ostream Out(TableData);
1449 // Make sure that no bucket is at offset 0
1450 clang::io::Emit32(Out, 0);
1451 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1452 }
1453
1454 // Create a blob abbreviation
1455 using namespace llvm;
1456 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1457 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1462 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1463
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001464 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465 RecordData Record;
1466 Record.push_back(HEADER_SEARCH_TABLE);
1467 Record.push_back(BucketOffset);
1468 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001469 Record.push_back(TableData.size());
1470 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001471 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1472
1473 // Free all of the strings we had to duplicate.
1474 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1475 free((void*)SavedStrings[I]);
1476}
1477
Douglas Gregor14f79002009-04-10 03:52:48 +00001478/// \brief Writes the block containing the serialized form of the
1479/// source manager.
1480///
1481/// TODO: We should probably use an on-disk hash table (stored in a
1482/// blob), indexed based on the file name, so that we only create
1483/// entries for files that we actually need. In the common case (no
1484/// errors), we probably won't have to create file entries for any of
1485/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001486void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001487 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001488 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001489 RecordData Record;
1490
Chris Lattnerf04ad692009-04-10 17:16:57 +00001491 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001492 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001493
1494 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001495 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1496 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1497 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001498 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001499
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001500 // Write out the source location entry table. We skip the first
1501 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001502 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001503 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001504 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1505 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001506 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001507 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001508 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001509 FileID FID = FileID::get(I);
1510 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001511
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001512 // Record the offset of this source-location entry.
1513 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1514
1515 // Figure out which record code to use.
1516 unsigned Code;
1517 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001518 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1519 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001520 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001521 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001522 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001524 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001525 Record.clear();
1526 Record.push_back(Code);
1527
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001528 // Starting offset of this entry within this module, so skip the dummy.
1529 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001530 if (SLoc->isFile()) {
1531 const SrcMgr::FileInfo &File = SLoc->getFile();
1532 Record.push_back(File.getIncludeLoc().getRawEncoding());
1533 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1534 Record.push_back(File.hasLineDirectives());
1535
1536 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001537 if (Content->OrigEntry) {
1538 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001539 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001540
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 // The source location entry is a file. The blob associated
1542 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Douglas Gregor2d52be52010-03-21 22:49:54 +00001544 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001545 Record.push_back(Content->OrigEntry->getSize());
1546 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001547 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001548 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001549
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001550 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001551 if (FDI != FileDeclIDs.end()) {
1552 Record.push_back(FDI->second->FirstDeclIndex);
1553 Record.push_back(FDI->second->DeclIDs.size());
1554 } else {
1555 Record.push_back(0);
1556 Record.push_back(0);
1557 }
Douglas Gregora081da52011-11-16 20:05:18 +00001558
Douglas Gregore650c8c2009-07-07 00:12:59 +00001559 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001560 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001561 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001562
1563 // Ask the file manager to fixup the relative path for us. This will
1564 // honor the working directory.
1565 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1566
1567 // FIXME: This call to make_absolute shouldn't be necessary, the
1568 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001569 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001570 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregore650c8c2009-07-07 00:12:59 +00001572 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001573 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001574
1575 if (Content->BufferOverridden) {
1576 Record.clear();
1577 Record.push_back(SM_SLOC_BUFFER_BLOB);
1578 const llvm::MemoryBuffer *Buffer
1579 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1580 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1581 StringRef(Buffer->getBufferStart(),
1582 Buffer->getBufferSize() + 1));
1583 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001584 } else {
1585 // The source location entry is a buffer. The blob associated
1586 // with this entry contains the contents of the buffer.
1587
1588 // We add one to the size so that we capture the trailing NULL
1589 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1590 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001591 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001592 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001593 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001594 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001595 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001596 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001597 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001598 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001599 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001600 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001601
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001602 if (strcmp(Name, "<built-in>") == 0) {
1603 PreloadSLocs.push_back(SLocEntryOffsets.size());
1604 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001605 }
1606 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001607 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001608 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001609 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1610 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001611 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1612 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001613
1614 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001615 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001616 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001617 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001618 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001619 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001620 }
1621 }
1622
Douglas Gregorc9490c02009-04-16 22:23:12 +00001623 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001624
1625 if (SLocEntryOffsets.empty())
1626 return;
1627
Sebastian Redl3397c552010-08-18 23:56:27 +00001628 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001629 // table is used for lazily loading source-location information.
1630 using namespace llvm;
1631 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001632 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1636 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001638 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001639 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001640 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001641 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001642 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001643
Sebastian Redl3397c552010-08-18 23:56:27 +00001644 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001645 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001646 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001647
1648 // Write the line table. It depends on remapping working, so it must come
1649 // after the source location offsets.
1650 if (SourceMgr.hasLineTable()) {
1651 LineTableInfo &LineTable = SourceMgr.getLineTable();
1652
1653 Record.clear();
1654 // Emit the file names
1655 Record.push_back(LineTable.getNumFilenames());
1656 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1657 // Emit the file name
1658 const char *Filename = LineTable.getFilename(I);
1659 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1660 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1661 Record.push_back(FilenameLen);
1662 if (FilenameLen)
1663 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1664 }
1665
1666 // Emit the line entries
1667 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1668 L != LEnd; ++L) {
1669 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001670 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001671 continue;
1672
1673 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001674 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001675
1676 // Emit the line entries
1677 Record.push_back(L->second.size());
1678 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1679 LEEnd = L->second.end();
1680 LE != LEEnd; ++LE) {
1681 Record.push_back(LE->FileOffset);
1682 Record.push_back(LE->LineNo);
1683 Record.push_back(LE->FilenameID);
1684 Record.push_back((unsigned)LE->FileKind);
1685 Record.push_back(LE->IncludeOffset);
1686 }
1687 }
1688 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1689 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001690}
1691
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001692//===----------------------------------------------------------------------===//
1693// Preprocessor Serialization
1694//===----------------------------------------------------------------------===//
1695
Douglas Gregor9c736102011-02-10 18:20:09 +00001696static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1697 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1698 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1699 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1700 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1701 return X.first->getName().compare(Y.first->getName());
1702}
1703
Chris Lattner0b1fb982009-04-10 17:15:23 +00001704/// \brief Writes the block containing the serialized form of the
1705/// preprocessor.
1706///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001707void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001708 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1709 if (PPRec)
1710 WritePreprocessorDetail(*PPRec);
1711
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001712 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001713
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001714 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1715 if (PP.getCounterValue() != 0) {
1716 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001717 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001718 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001719 }
1720
1721 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001722 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Sebastian Redl3397c552010-08-18 23:56:27 +00001724 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001725 // FIXME: use diagnostics subsystem for localization etc.
1726 if (PP.SawDateOrTime())
1727 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Douglas Gregorecdcb882010-10-20 22:00:55 +00001729
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001730 // Loop over all the macro definitions that are live at the end of the file,
1731 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001732
Douglas Gregor9c736102011-02-10 18:20:09 +00001733 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001734 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001735 MacrosToEmit;
1736 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001737 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001738 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001739 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001740 if (!IsModule || I->second->isPublic()) {
1741 MacroDefinitionsSeen.insert(I->first);
1742 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001743 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001744 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001745
Douglas Gregor9c736102011-02-10 18:20:09 +00001746 // Sort the set of macro definitions that need to be serialized by the
1747 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001748 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001749 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001750
Douglas Gregora8235d62012-10-09 23:05:51 +00001751 /// \brief Offsets of each of the macros into the bitstream, indexed by
1752 /// the local macro ID
1753 ///
1754 /// For each identifier that is associated with a macro, this map
1755 /// provides the offset into the bitstream where that macro is
1756 /// defined.
1757 std::vector<uint32_t> MacroOffsets;
1758
Douglas Gregor9c736102011-02-10 18:20:09 +00001759 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1760 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001761
Douglas Gregora8235d62012-10-09 23:05:51 +00001762 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1763 MI = MI->getPreviousDefinition()) {
1764 MacroID ID = getMacroRef(MI);
1765 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001766 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Douglas Gregora8235d62012-10-09 23:05:51 +00001768 // Skip macros from a AST file if we're chaining.
1769 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1770 continue;
1771
1772 if (ID < FirstMacroID) {
1773 // This will have been dealt with via an update record.
1774 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1775 continue;
1776 }
1777
1778 // Record the local offset of this macro.
1779 unsigned Index = ID - FirstMacroID;
1780 if (Index == MacroOffsets.size())
1781 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1782 else {
1783 if (Index > MacroOffsets.size())
1784 MacroOffsets.resize(Index + 1);
1785
1786 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1787 }
1788
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001789 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001790 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001791 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001792 AddSourceLocation(MI->getDefinitionLoc(), Record);
1793 AddSourceLocation(MI->getUndefLoc(), Record);
1794 Record.push_back(MI->isUsed());
1795 Record.push_back(MI->isPublic());
1796 AddSourceLocation(MI->getVisibilityLocation(), Record);
1797 unsigned Code;
1798 if (MI->isObjectLike()) {
1799 Code = PP_MACRO_OBJECT_LIKE;
1800 } else {
1801 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001802
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001803 Record.push_back(MI->isC99Varargs());
1804 Record.push_back(MI->isGNUVarargs());
1805 Record.push_back(MI->getNumArgs());
1806 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1807 I != E; ++I)
1808 AddIdentifierRef(*I, Record);
1809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001811 // If we have a detailed preprocessing record, record the macro definition
1812 // ID that corresponds to this macro.
1813 if (PPRec)
1814 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1815
1816 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001817 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001818
1819 // Emit the tokens array.
1820 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1821 // Note that we know that the preprocessor does not have any annotation
1822 // tokens in it because they are created by the parser, and thus can't
1823 // be in a macro definition.
1824 const Token &Tok = MI->getReplacementToken(TokNo);
1825
1826 Record.push_back(Tok.getLocation().getRawEncoding());
1827 Record.push_back(Tok.getLength());
1828
1829 // FIXME: When reading literal tokens, reconstruct the literal pointer
1830 // if it is needed.
1831 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1832 // FIXME: Should translate token kind to a stable encoding.
1833 Record.push_back(Tok.getKind());
1834 // FIXME: Should translate token flags to a stable encoding.
1835 Record.push_back(Tok.getFlags());
1836
1837 Stream.EmitRecord(PP_TOKEN, Record);
1838 Record.clear();
1839 }
1840 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001841 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001842 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001843 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001844
1845 // Write the offsets table for macro IDs.
1846 using namespace llvm;
1847 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1848 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1852
1853 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1854 Record.clear();
1855 Record.push_back(MACRO_OFFSET);
1856 Record.push_back(MacroOffsets.size());
1857 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1858 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1859 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001860}
1861
1862void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001863 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001864 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001865
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001866 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001867
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001868 // Enter the preprocessor block.
1869 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001870
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001871 // If the preprocessor has a preprocessing record, emit it.
1872 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001873 using namespace llvm;
1874
1875 // Set up the abbreviation for
1876 unsigned InclusionAbbrev = 0;
1877 {
1878 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1879 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1885 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1886 }
1887
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001888 unsigned FirstPreprocessorEntityID
1889 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1890 + NUM_PREDEF_PP_ENTITY_IDS;
1891 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001892 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001893 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1894 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001895 E != EEnd;
1896 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001897 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001898
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001899 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1900 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001901
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001902 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001903 // Record this macro definition's ID.
1904 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001905
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001906 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001907 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1908 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001909 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001910
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001911 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001912 Record.push_back(ME->isBuiltinMacro());
1913 if (ME->isBuiltinMacro())
1914 AddIdentifierRef(ME->getName(), Record);
1915 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001916 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001917 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001918 continue;
1919 }
1920
1921 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1922 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001923 Record.push_back(ID->getFileName().size());
1924 Record.push_back(ID->wasInQuotes());
1925 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001926 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001927 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001928 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001929 // Check that the FileEntry is not null because it was not resolved and
1930 // we create a PCH even with compiler errors.
1931 if (ID->getFile())
1932 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001933 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1934 continue;
1935 }
1936
1937 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1938 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001939 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001940
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001941 // Write the offsets table for the preprocessing record.
1942 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001943 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1944
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001945 // Write the offsets table for identifier IDs.
1946 using namespace llvm;
1947 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001948 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001951 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001952
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001953 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001954 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001955 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001956 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1957 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001958 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001959}
1960
Douglas Gregore209e502011-12-06 01:10:29 +00001961unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1962 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1963 if (Known != SubmoduleIDs.end())
1964 return Known->second;
1965
1966 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1967}
1968
Douglas Gregor26ced122011-12-01 00:59:36 +00001969/// \brief Compute the number of modules within the given tree (including the
1970/// given module).
1971static unsigned getNumberOfModules(Module *Mod) {
1972 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001973 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1974 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001975 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001976 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001977
1978 return ChildModules + 1;
1979}
1980
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001981void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001982 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001983 // FIXME: This feels like it belongs somewhere else, but there are no
1984 // other consumers of this information.
1985 SourceManager &SrcMgr = PP->getSourceManager();
1986 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1987 for (ASTContext::import_iterator I = Context->local_import_begin(),
1988 IEnd = Context->local_import_end();
1989 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001990 if (Module *ImportedFrom
1991 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1992 SrcMgr))) {
1993 ImportedFrom->Imports.push_back(I->getImportedModule());
1994 }
1995 }
1996
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001997 // Enter the submodule description block.
1998 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1999
2000 // Write the abbreviations needed for the submodules block.
2001 using namespace llvm;
2002 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2003 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002011 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2013 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2014
2015 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002016 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2018 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2019
2020 Abbrev = new BitCodeAbbrev();
2021 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2023 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002024
2025 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002026 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2028 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2029
2030 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002031 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2033 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2034
Douglas Gregor51f564f2011-12-31 04:05:44 +00002035 Abbrev = new BitCodeAbbrev();
2036 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2038 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2039
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002040 Abbrev = new BitCodeAbbrev();
2041 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2043 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2044
Douglas Gregor26ced122011-12-01 00:59:36 +00002045 // Write the submodule metadata block.
2046 RecordData Record;
2047 Record.push_back(getNumberOfModules(WritingModule));
2048 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2049 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2050
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002051 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002052 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002053 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002054 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002055 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002056 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002057 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002058
2059 // Emit the definition of the block.
2060 Record.clear();
2061 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002062 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002063 if (Mod->Parent) {
2064 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2065 Record.push_back(SubmoduleIDs[Mod->Parent]);
2066 } else {
2067 Record.push_back(0);
2068 }
2069 Record.push_back(Mod->IsFramework);
2070 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002071 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002072 Record.push_back(Mod->InferSubmodules);
2073 Record.push_back(Mod->InferExplicitSubmodules);
2074 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002075 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2076
Douglas Gregor51f564f2011-12-31 04:05:44 +00002077 // Emit the requirements.
2078 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2079 Record.clear();
2080 Record.push_back(SUBMODULE_REQUIRES);
2081 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2082 Mod->Requires[I].data(),
2083 Mod->Requires[I].size());
2084 }
2085
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002086 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002087 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002088 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002089 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002090 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002091 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002092 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2093 Record.clear();
2094 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2095 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2096 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002097 }
2098
2099 // Emit the headers.
2100 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2101 Record.clear();
2102 Record.push_back(SUBMODULE_HEADER);
2103 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2104 Mod->Headers[I]->getName());
2105 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002106 // Emit the excluded headers.
2107 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2108 Record.clear();
2109 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2110 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2111 Mod->ExcludedHeaders[I]->getName());
2112 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002113 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2114 Record.clear();
2115 Record.push_back(SUBMODULE_TOPHEADER);
2116 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2117 Mod->TopHeaders[I]->getName());
2118 }
Douglas Gregor55988682011-12-05 16:33:54 +00002119
2120 // Emit the imports.
2121 if (!Mod->Imports.empty()) {
2122 Record.clear();
2123 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002124 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002125 assert(ImportedID && "Unknown submodule!");
2126 Record.push_back(ImportedID);
2127 }
2128 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2129 }
2130
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002131 // Emit the exports.
2132 if (!Mod->Exports.empty()) {
2133 Record.clear();
2134 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002135 if (Module *Exported = Mod->Exports[I].getPointer()) {
2136 unsigned ExportedID = SubmoduleIDs[Exported];
2137 assert(ExportedID > 0 && "Unknown submodule ID?");
2138 Record.push_back(ExportedID);
2139 } else {
2140 Record.push_back(0);
2141 }
2142
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002143 Record.push_back(Mod->Exports[I].getInt());
2144 }
2145 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2146 }
2147
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002148 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002149 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2150 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002151 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002152 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002153 }
2154
2155 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002156
2157 assert((NextSubmoduleID - FirstSubmoduleID
2158 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002159}
2160
Douglas Gregor185dbd72011-12-01 02:07:58 +00002161serialization::SubmoduleID
2162ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002163 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002164 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002165
2166 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002167 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002168 Module *OwningMod
2169 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002170 if (!OwningMod)
2171 return 0;
2172
Douglas Gregore209e502011-12-06 01:10:29 +00002173 // Check whether this submodule is part of our own module.
2174 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002175 return 0;
2176
Douglas Gregore209e502011-12-06 01:10:29 +00002177 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002178}
2179
David Blaikied6471f72011-09-25 23:23:43 +00002180void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002181 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002182 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002183 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2184 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002185 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002186 if (point.Loc.isInvalid())
2187 continue;
2188
2189 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002190 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002191 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002192 if (I->second.isPragma()) {
2193 Record.push_back(I->first);
2194 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002195 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002196 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002197 Record.push_back(-1); // mark the end of the diag/map pairs for this
2198 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002199 }
2200
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002201 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002202 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002203}
2204
Anders Carlssonc8505782011-03-06 18:41:18 +00002205void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2206 if (CXXBaseSpecifiersOffsets.empty())
2207 return;
2208
2209 RecordData Record;
2210
2211 // Create a blob abbreviation for the C++ base specifiers offsets.
2212 using namespace llvm;
2213
2214 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2215 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2218 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2219
Douglas Gregore92b8a12011-08-04 00:01:48 +00002220 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002221 Record.clear();
2222 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2223 Record.push_back(CXXBaseSpecifiersOffsets.size());
2224 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002225 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002226}
2227
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002228//===----------------------------------------------------------------------===//
2229// Type Serialization
2230//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002231
Sebastian Redl3397c552010-08-18 23:56:27 +00002232/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002233void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002234 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002235 if (Idx.getIndex() == 0) // we haven't seen this type before.
2236 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Douglas Gregor97475832010-10-05 18:37:06 +00002238 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002239
Douglas Gregor2cf26342009-04-09 22:27:44 +00002240 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002241 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002242 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002243 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002244 else if (TypeOffsets.size() < Index) {
2245 TypeOffsets.resize(Index + 1);
2246 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247 }
2248
2249 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Douglas Gregor2cf26342009-04-09 22:27:44 +00002251 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002252 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002253
Douglas Gregora4923eb2009-11-16 21:35:15 +00002254 if (T.hasLocalNonFastQualifiers()) {
2255 Qualifiers Qs = T.getLocalQualifiers();
2256 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002257 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002258 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002259 } else {
2260 switch (T->getTypeClass()) {
2261 // For all of the concrete, non-dependent types, call the
2262 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002263#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002264 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002265#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002266#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002267 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002268 }
2269
2270 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002271 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002272
2273 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002274 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002275}
2276
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002277//===----------------------------------------------------------------------===//
2278// Declaration Serialization
2279//===----------------------------------------------------------------------===//
2280
Douglas Gregor2cf26342009-04-09 22:27:44 +00002281/// \brief Write the block containing all of the declaration IDs
2282/// lexically declared within the given DeclContext.
2283///
2284/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2285/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002286uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002288 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002289 return 0;
2290
Douglas Gregorc9490c02009-04-16 22:23:12 +00002291 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002292 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002293 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002294 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002295 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2296 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002297 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002298
Douglas Gregor25123082009-04-22 22:34:57 +00002299 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002300 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002301 return Offset;
2302}
2303
Sebastian Redla4232eb2010-08-18 23:56:21 +00002304void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002305 using namespace llvm;
2306 RecordData Record;
2307
2308 // Write the type offsets array
2309 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002310 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2314 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2315 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002316 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002317 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002318 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002319 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002320
2321 // Write the declaration offsets array
2322 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002323 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2327 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2328 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002329 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002330 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002331 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002332 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002333}
2334
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002335void ASTWriter::WriteFileDeclIDsMap() {
2336 using namespace llvm;
2337 RecordData Record;
2338
2339 // Join the vectors of DeclIDs from all files.
2340 SmallVector<DeclID, 256> FileSortedIDs;
2341 for (FileDeclIDsTy::iterator
2342 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2343 DeclIDInFileInfo &Info = *FI->second;
2344 Info.FirstDeclIndex = FileSortedIDs.size();
2345 for (LocDeclIDsTy::iterator
2346 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2347 FileSortedIDs.push_back(DI->second);
2348 }
2349
2350 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2351 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2354 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2355 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002356 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002357 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2358}
2359
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002360void ASTWriter::WriteComments() {
2361 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002362 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002363 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002364 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2365 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002366 I != E; ++I) {
2367 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002368 AddSourceRange((*I)->getSourceRange(), Record);
2369 Record.push_back((*I)->getKind());
2370 Record.push_back((*I)->isTrailingComment());
2371 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002372 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2373 }
2374 Stream.ExitBlock();
2375}
2376
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002377//===----------------------------------------------------------------------===//
2378// Global Method Pool and Selector Serialization
2379//===----------------------------------------------------------------------===//
2380
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002381namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002382// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002383class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002384 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385
2386public:
2387 typedef Selector key_type;
2388 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Sebastian Redl5d050072010-08-04 17:20:04 +00002390 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002391 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002392 ObjCMethodList Instance, Factory;
2393 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002394 typedef const data_type& data_type_ref;
2395
Sebastian Redl3397c552010-08-18 23:56:27 +00002396 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002397
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002398 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002399 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002400 }
Mike Stump1eb44332009-09-09 15:08:12 +00002401
2402 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002403 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002404 data_type_ref Methods) {
2405 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2406 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002407 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2408 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002409 Method = Method->Next)
2410 if (Method->Method)
2411 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002412 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002413 Method = Method->Next)
2414 if (Method->Method)
2415 DataLen += 4;
2416 clang::io::Emit16(Out, DataLen);
2417 return std::make_pair(KeyLen, DataLen);
2418 }
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Chris Lattner5f9e2722011-07-23 10:55:15 +00002420 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002421 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002422 assert((Start >> 32) == 0 && "Selector key offset too large");
2423 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002424 unsigned N = Sel.getNumArgs();
2425 clang::io::Emit16(Out, N);
2426 if (N == 0)
2427 N = 1;
2428 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002429 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002430 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2431 }
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Chris Lattner5f9e2722011-07-23 10:55:15 +00002433 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002434 data_type_ref Methods, unsigned DataLen) {
2435 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002436 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002437 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002438 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002439 Method = Method->Next)
2440 if (Method->Method)
2441 ++NumInstanceMethods;
2442
2443 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002444 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002445 Method = Method->Next)
2446 if (Method->Method)
2447 ++NumFactoryMethods;
2448
2449 clang::io::Emit16(Out, NumInstanceMethods);
2450 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002451 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002452 Method = Method->Next)
2453 if (Method->Method)
2454 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002455 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002456 Method = Method->Next)
2457 if (Method->Method)
2458 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002459
2460 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002461 }
2462};
2463} // end anonymous namespace
2464
Sebastian Redl059612d2010-08-03 21:58:15 +00002465/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002466///
2467/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002468/// in an on-disk hash table indexed by the selector. The hash table also
2469/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002470void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002471 using namespace llvm;
2472
Sebastian Redl059612d2010-08-03 21:58:15 +00002473 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002474 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002475 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002476 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002477 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002478 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002479 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002480 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Sebastian Redl059612d2010-08-03 21:58:15 +00002482 // Create the on-disk hash table representation. We walk through every
2483 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002484 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002485 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002486 I = SelectorIDs.begin(), E = SelectorIDs.end();
2487 I != E; ++I) {
2488 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002489 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002490 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002491 I->second,
2492 ObjCMethodList(),
2493 ObjCMethodList()
2494 };
2495 if (F != SemaRef.MethodPool.end()) {
2496 Data.Instance = F->second.first;
2497 Data.Factory = F->second.second;
2498 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002499 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002500 // changed.
2501 if (Chain && I->second < FirstSelectorID) {
2502 // Selector already exists. Did it change?
2503 bool changed = false;
2504 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2505 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002506 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002507 changed = true;
2508 }
2509 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2510 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002511 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002512 changed = true;
2513 }
2514 if (!changed)
2515 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002516 } else if (Data.Instance.Method || Data.Factory.Method) {
2517 // A new method pool entry.
2518 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002519 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002520 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002521 }
2522
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002523 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002524 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002525 uint32_t BucketOffset;
2526 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002527 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002528 llvm::raw_svector_ostream Out(MethodPool);
2529 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002530 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002531 BucketOffset = Generator.Emit(Out, Trait);
2532 }
2533
2534 // Create a blob abbreviation
2535 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002536 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2540 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2541
Douglas Gregor83941df2009-04-25 17:48:32 +00002542 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002543 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002544 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002545 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002546 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002547 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002548
2549 // Create a blob abbreviation for the selector table offsets.
2550 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002551 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2555 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2556
2557 // Write the selector offsets table.
2558 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002559 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002560 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002561 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002562 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002563 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002564 }
2565}
2566
Sebastian Redl3397c552010-08-18 23:56:27 +00002567/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002568void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002569 using namespace llvm;
2570 if (SemaRef.ReferencedSelectors.empty())
2571 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002572
Fariborz Jahanian32019832010-07-23 19:11:11 +00002573 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002574
Sebastian Redl3397c552010-08-18 23:56:27 +00002575 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002576 // very tricky to fix, and given that @selector shouldn't really appear in
2577 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002578 for (DenseMap<Selector, SourceLocation>::iterator S =
2579 SemaRef.ReferencedSelectors.begin(),
2580 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2581 Selector Sel = (*S).first;
2582 SourceLocation Loc = (*S).second;
2583 AddSelectorRef(Sel, Record);
2584 AddSourceLocation(Loc, Record);
2585 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002586 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002587}
2588
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002589//===----------------------------------------------------------------------===//
2590// Identifier Table Serialization
2591//===----------------------------------------------------------------------===//
2592
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002593namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002594class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002595 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002596 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002597 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002598 bool IsModule;
2599
Douglas Gregora92193e2009-04-28 21:18:29 +00002600 /// \brief Determines whether this is an "interesting" identifier
2601 /// that needs a full IdentifierInfo structure written into the hash
2602 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002603 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002604 if (II->isPoisoned() ||
2605 II->isExtensionToken() ||
2606 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002607 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002608 II->getFETokenInfo<void>())
2609 return true;
2610
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002611 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002612 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002613
2614 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2615 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002616 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002617
2618 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002619 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002620
2621 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002622 }
2623
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002624public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002625 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002626 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002628 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002629 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Douglas Gregoreee242f2011-10-27 09:33:13 +00002631 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2632 IdentifierResolver &IdResolver, bool IsModule)
2633 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002634
2635 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002636 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002637 }
Mike Stump1eb44332009-09-09 15:08:12 +00002638
2639 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002640 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002641 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002642 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002643 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002644 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002645 DataLen += 2; // 2 bytes for builtin ID
2646 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002647 if (hadMacroDefinition(II, Macro)) {
2648 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2649 if (Writer.getMacroRef(M) != 0)
2650 DataLen += 4;
2651 }
2652
2653 DataLen += 4;
2654 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002655
Douglas Gregoreee242f2011-10-27 09:33:13 +00002656 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2657 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002658 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002659 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002660 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002661 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002662 // We emit the key length after the data length so that every
2663 // string is preceded by a 16-bit length. This matches the PTH
2664 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002665 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002666 return std::make_pair(KeyLen, DataLen);
2667 }
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Chris Lattner5f9e2722011-07-23 10:55:15 +00002669 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002670 unsigned KeyLen) {
2671 // Record the location of the key data. This is used when generating
2672 // the mapping from persistent IDs to strings.
2673 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002674 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002675 }
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Douglas Gregor7143aab2011-09-01 17:04:32 +00002677 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002678 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002679 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002680 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002681 clang::io::Emit32(Out, ID << 1);
2682 return;
2683 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002684
Douglas Gregora92193e2009-04-28 21:18:29 +00002685 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002686 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2687 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2688 clang::io::Emit16(Out, Bits);
2689 Bits = 0;
2690 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002691 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002692 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2693 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002694 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002695 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002696 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002697
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002698 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002699 // Write all of the macro IDs associated with this identifier.
2700 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2701 if (MacroID ID = Writer.getMacroRef(M))
2702 clang::io::Emit32(Out, ID);
2703 }
2704
2705 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002706 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002707
Douglas Gregor668c1a42009-04-21 22:25:48 +00002708 // Emit the declaration IDs in reverse order, because the
2709 // IdentifierResolver provides the declarations as they would be
2710 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002711 // "stat"), but the ASTReader adds declarations to the end of the list
2712 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002713 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002714 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2715 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002717 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002718 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002719 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002720 }
2721};
2722} // end anonymous namespace
2723
Sebastian Redl3397c552010-08-18 23:56:27 +00002724/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002725///
2726/// The identifier table consists of a blob containing string data
2727/// (the actual identifiers themselves) and a separate "offsets" index
2728/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002729void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2730 IdentifierResolver &IdResolver,
2731 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002732 using namespace llvm;
2733
2734 // Create and write out the blob that contains the identifier
2735 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002736 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002737 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002738 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Douglas Gregor92b059e2009-04-28 20:33:11 +00002740 // Look for any identifiers that were named while processing the
2741 // headers, but are otherwise not needed. We add these to the hash
2742 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002743 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002744 // file.
2745 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2746 IDEnd = PP.getIdentifierTable().end();
2747 ID != IDEnd; ++ID)
2748 getIdentifierRef(ID->second);
2749
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002750 // Create the on-disk hash table representation. We only store offsets
2751 // for identifiers that appear here for the first time.
2752 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002753 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002754 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2755 ID != IDEnd; ++ID) {
2756 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002757 if (!Chain || !ID->first->isFromAST() ||
2758 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002759 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2760 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002761 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002762
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002763 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002764 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002765 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002766 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002767 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002768 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002769 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002770 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002771 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002772 }
2773
2774 // Create a blob abbreviation
2775 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002776 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002779 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002780
2781 // Write the identifier table
2782 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002783 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002784 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002785 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002786 }
2787
2788 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002789 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002790 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2794 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2795
2796 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002797 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002798 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002799 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002800 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002801 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002802}
2803
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002804//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002805// DeclContext's Name Lookup Table Serialization
2806//===----------------------------------------------------------------------===//
2807
2808namespace {
2809// Trait used for the on-disk hash table used in the method pool.
2810class ASTDeclContextNameLookupTrait {
2811 ASTWriter &Writer;
2812
2813public:
2814 typedef DeclarationName key_type;
2815 typedef key_type key_type_ref;
2816
2817 typedef DeclContext::lookup_result data_type;
2818 typedef const data_type& data_type_ref;
2819
2820 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2821
2822 unsigned ComputeHash(DeclarationName Name) {
2823 llvm::FoldingSetNodeID ID;
2824 ID.AddInteger(Name.getNameKind());
2825
2826 switch (Name.getNameKind()) {
2827 case DeclarationName::Identifier:
2828 ID.AddString(Name.getAsIdentifierInfo()->getName());
2829 break;
2830 case DeclarationName::ObjCZeroArgSelector:
2831 case DeclarationName::ObjCOneArgSelector:
2832 case DeclarationName::ObjCMultiArgSelector:
2833 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2834 break;
2835 case DeclarationName::CXXConstructorName:
2836 case DeclarationName::CXXDestructorName:
2837 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002838 break;
2839 case DeclarationName::CXXOperatorName:
2840 ID.AddInteger(Name.getCXXOverloadedOperator());
2841 break;
2842 case DeclarationName::CXXLiteralOperatorName:
2843 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2844 case DeclarationName::CXXUsingDirective:
2845 break;
2846 }
2847
2848 return ID.ComputeHash();
2849 }
2850
2851 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002852 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002853 data_type_ref Lookup) {
2854 unsigned KeyLen = 1;
2855 switch (Name.getNameKind()) {
2856 case DeclarationName::Identifier:
2857 case DeclarationName::ObjCZeroArgSelector:
2858 case DeclarationName::ObjCOneArgSelector:
2859 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002860 case DeclarationName::CXXLiteralOperatorName:
2861 KeyLen += 4;
2862 break;
2863 case DeclarationName::CXXOperatorName:
2864 KeyLen += 1;
2865 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002866 case DeclarationName::CXXConstructorName:
2867 case DeclarationName::CXXDestructorName:
2868 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002869 case DeclarationName::CXXUsingDirective:
2870 break;
2871 }
2872 clang::io::Emit16(Out, KeyLen);
2873
2874 // 2 bytes for num of decls and 4 for each DeclID.
2875 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2876 clang::io::Emit16(Out, DataLen);
2877
2878 return std::make_pair(KeyLen, DataLen);
2879 }
2880
Chris Lattner5f9e2722011-07-23 10:55:15 +00002881 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002882 using namespace clang::io;
2883
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002884 Emit8(Out, Name.getNameKind());
2885 switch (Name.getNameKind()) {
2886 case DeclarationName::Identifier:
2887 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002888 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002889 case DeclarationName::ObjCZeroArgSelector:
2890 case DeclarationName::ObjCOneArgSelector:
2891 case DeclarationName::ObjCMultiArgSelector:
2892 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002893 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002894 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002895 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2896 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002897 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002898 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002899 case DeclarationName::CXXLiteralOperatorName:
2900 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002901 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002902 case DeclarationName::CXXConstructorName:
2903 case DeclarationName::CXXDestructorName:
2904 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002905 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002906 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002907 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002908
2909 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002910 }
2911
Chris Lattner5f9e2722011-07-23 10:55:15 +00002912 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002913 data_type Lookup, unsigned DataLen) {
2914 uint64_t Start = Out.tell(); (void)Start;
2915 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2916 for (; Lookup.first != Lookup.second; ++Lookup.first)
2917 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2918
2919 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2920 }
2921};
2922} // end anonymous namespace
2923
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002924/// \brief Write the block containing all of the declaration IDs
2925/// visible from the given DeclContext.
2926///
2927/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002928/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002929uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2930 DeclContext *DC) {
2931 if (DC->getPrimaryContext() != DC)
2932 return 0;
2933
2934 // Since there is no name lookup into functions or methods, don't bother to
2935 // build a visible-declarations table for these entities.
2936 if (DC->isFunctionOrMethod())
2937 return 0;
2938
2939 // If not in C++, we perform name lookup for the translation unit via the
2940 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2941 // FIXME: In C++ we need the visible declarations in order to "see" the
2942 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002943 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002944 return 0;
2945
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002946 // Serialize the contents of the mapping used for lookup. Note that,
2947 // although we have two very different code paths, the serialized
2948 // representation is the same for both cases: a declaration name,
2949 // followed by a size, followed by references to the visible
2950 // declarations that have that name.
2951 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002952 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002953 if (!Map || Map->empty())
2954 return 0;
2955
2956 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2957 ASTDeclContextNameLookupTrait Trait(*this);
2958
2959 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002960 DeclarationName ConversionName;
2961 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002962 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2963 D != DEnd; ++D) {
2964 DeclarationName Name = D->first;
2965 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002966 if (Result.first != Result.second) {
2967 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2968 // Hash all conversion function names to the same name. The actual
2969 // type information in conversion function name is not used in the
2970 // key (since such type information is not stable across different
2971 // modules), so the intended effect is to coalesce all of the conversion
2972 // functions under a single key.
2973 if (!ConversionName)
2974 ConversionName = Name;
2975 ConversionDecls.append(Result.first, Result.second);
2976 continue;
2977 }
2978
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002979 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002980 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002981 }
2982
Douglas Gregore5a54b62011-08-30 20:49:19 +00002983 // Add the conversion functions
2984 if (!ConversionDecls.empty()) {
2985 Generator.insert(ConversionName,
2986 DeclContext::lookup_result(ConversionDecls.begin(),
2987 ConversionDecls.end()),
2988 Trait);
2989 }
2990
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002991 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002992 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002993 uint32_t BucketOffset;
2994 {
2995 llvm::raw_svector_ostream Out(LookupTable);
2996 // Make sure that no bucket is at offset 0
2997 clang::io::Emit32(Out, 0);
2998 BucketOffset = Generator.Emit(Out, Trait);
2999 }
3000
3001 // Write the lookup table
3002 RecordData Record;
3003 Record.push_back(DECL_CONTEXT_VISIBLE);
3004 Record.push_back(BucketOffset);
3005 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3006 LookupTable.str());
3007
3008 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3009 ++NumVisibleDeclContexts;
3010 return Offset;
3011}
3012
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003013/// \brief Write an UPDATE_VISIBLE block for the given context.
3014///
3015/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3016/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003017/// (in C++), for namespaces, and for classes with forward-declared unscoped
3018/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003019void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003020 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3021 if (!Map || Map->empty())
3022 return;
3023
3024 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3025 ASTDeclContextNameLookupTrait Trait(*this);
3026
3027 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003028 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3029 D != DEnd; ++D) {
3030 DeclarationName Name = D->first;
3031 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003032 // For any name that appears in this table, the results are complete, i.e.
3033 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003034 if (Result.first != Result.second)
3035 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003036 }
3037
3038 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003039 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003040 uint32_t BucketOffset;
3041 {
3042 llvm::raw_svector_ostream Out(LookupTable);
3043 // Make sure that no bucket is at offset 0
3044 clang::io::Emit32(Out, 0);
3045 BucketOffset = Generator.Emit(Out, Trait);
3046 }
3047
3048 // Write the lookup table
3049 RecordData Record;
3050 Record.push_back(UPDATE_VISIBLE);
3051 Record.push_back(getDeclID(cast<Decl>(DC)));
3052 Record.push_back(BucketOffset);
3053 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3054}
3055
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003056/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3057void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3058 RecordData Record;
3059 Record.push_back(Opts.fp_contract);
3060 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3061}
3062
3063/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3064void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003065 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003066 return;
3067
3068 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3069 RecordData Record;
3070#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3071#include "clang/Basic/OpenCLExtensions.def"
3072 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3073}
3074
Douglas Gregor2171bf12012-01-15 16:58:34 +00003075void ASTWriter::WriteRedeclarations() {
3076 RecordData LocalRedeclChains;
3077 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3078
3079 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3080 Decl *First = Redeclarations[I];
3081 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3082
3083 Decl *MostRecent = First->getMostRecentDecl();
3084
3085 // If we only have a single declaration, there is no point in storing
3086 // a redeclaration chain.
3087 if (First == MostRecent)
3088 continue;
3089
3090 unsigned Offset = LocalRedeclChains.size();
3091 unsigned Size = 0;
3092 LocalRedeclChains.push_back(0); // Placeholder for the size.
3093
3094 // Collect the set of local redeclarations of this declaration.
3095 for (Decl *Prev = MostRecent; Prev != First;
3096 Prev = Prev->getPreviousDecl()) {
3097 if (!Prev->isFromASTFile()) {
3098 AddDeclRef(Prev, LocalRedeclChains);
3099 ++Size;
3100 }
3101 }
3102 LocalRedeclChains[Offset] = Size;
3103
3104 // Reverse the set of local redeclarations, so that we store them in
3105 // order (since we found them in reverse order).
3106 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3107
3108 // Add the mapping from the first ID to the set of local declarations.
3109 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3110 LocalRedeclsMap.push_back(Info);
3111
3112 assert(N == Redeclarations.size() &&
3113 "Deserialized a declaration we shouldn't have");
3114 }
3115
3116 if (LocalRedeclChains.empty())
3117 return;
3118
3119 // Sort the local redeclarations map by the first declaration ID,
3120 // since the reader will be performing binary searches on this information.
3121 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3122
3123 // Emit the local redeclarations map.
3124 using namespace llvm;
3125 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3126 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3129 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3130
3131 RecordData Record;
3132 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3133 Record.push_back(LocalRedeclsMap.size());
3134 Stream.EmitRecordWithBlob(AbbrevID, Record,
3135 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3136 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3137
3138 // Emit the redeclaration chains.
3139 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3140}
3141
Douglas Gregorcff9f262012-01-27 01:47:08 +00003142void ASTWriter::WriteObjCCategories() {
3143 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3144 RecordData Categories;
3145
3146 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3147 unsigned Size = 0;
3148 unsigned StartIndex = Categories.size();
3149
3150 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3151
3152 // Allocate space for the size.
3153 Categories.push_back(0);
3154
3155 // Add the categories.
3156 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3157 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3158 assert(getDeclID(Cat) != 0 && "Bogus category");
3159 AddDeclRef(Cat, Categories);
3160 }
3161
3162 // Update the size.
3163 Categories[StartIndex] = Size;
3164
3165 // Record this interface -> category map.
3166 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3167 CategoriesMap.push_back(CatInfo);
3168 }
3169
3170 // Sort the categories map by the definition ID, since the reader will be
3171 // performing binary searches on this information.
3172 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3173
3174 // Emit the categories map.
3175 using namespace llvm;
3176 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3177 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3180 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3181
3182 RecordData Record;
3183 Record.push_back(OBJC_CATEGORIES_MAP);
3184 Record.push_back(CategoriesMap.size());
3185 Stream.EmitRecordWithBlob(AbbrevID, Record,
3186 reinterpret_cast<char*>(CategoriesMap.data()),
3187 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3188
3189 // Emit the category lists.
3190 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3191}
3192
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003193void ASTWriter::WriteMergedDecls() {
3194 if (!Chain || Chain->MergedDecls.empty())
3195 return;
3196
3197 RecordData Record;
3198 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3199 IEnd = Chain->MergedDecls.end();
3200 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003201 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003202 : getDeclID(I->first);
3203 assert(CanonID && "Merged declaration not known?");
3204
3205 Record.push_back(CanonID);
3206 Record.push_back(I->second.size());
3207 Record.append(I->second.begin(), I->second.end());
3208 }
3209 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3210}
3211
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003212//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003213// General Serialization Routines
3214//===----------------------------------------------------------------------===//
3215
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003216/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003217void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3218 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003219 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003220 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3221 e = Attrs.end(); i != e; ++i){
3222 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003223 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003224 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003225
Sean Huntcf807c42010-08-18 23:23:40 +00003226#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003227
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003228 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003229}
3230
Chris Lattner5f9e2722011-07-23 10:55:15 +00003231void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003232 Record.push_back(Str.size());
3233 Record.insert(Record.end(), Str.begin(), Str.end());
3234}
3235
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003236void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3237 RecordDataImpl &Record) {
3238 Record.push_back(Version.getMajor());
3239 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3240 Record.push_back(*Minor + 1);
3241 else
3242 Record.push_back(0);
3243 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3244 Record.push_back(*Subminor + 1);
3245 else
3246 Record.push_back(0);
3247}
3248
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003249/// \brief Note that the identifier II occurs at the given offset
3250/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003251void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003252 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003253 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003254 // up earlier in the chain and thus don't need an offset.
3255 if (ID >= FirstIdentID)
3256 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003257}
3258
Douglas Gregor83941df2009-04-25 17:48:32 +00003259/// \brief Note that the selector Sel occurs at the given offset
3260/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003261void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003262 unsigned ID = SelectorIDs[Sel];
3263 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003264 // Don't record offsets for selectors that are also available in a different
3265 // file.
3266 if (ID < FirstSelectorID)
3267 return;
3268 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003269}
3270
Sebastian Redla4232eb2010-08-18 23:56:21 +00003271ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003272 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003273 WritingAST(false), DoneWritingDeclsAndTypes(false),
3274 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003275 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003276 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003277 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3278 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003279 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3280 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003281 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003282 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003283 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003284 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003285 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003286 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003287 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3288 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3289 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003290 DeclTypedefAbbrev(0),
3291 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3292 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003293{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003294}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003295
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003296ASTWriter::~ASTWriter() {
3297 for (FileDeclIDsTy::iterator
3298 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3299 delete I->second;
3300}
3301
Sebastian Redla4232eb2010-08-18 23:56:21 +00003302void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003303 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003304 Module *WritingModule, StringRef isysroot,
3305 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003306 WritingAST = true;
3307
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003308 ASTHasCompilerErrors = hasErrors;
3309
Douglas Gregor2cf26342009-04-09 22:27:44 +00003310 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003311 Stream.Emit((unsigned)'C', 8);
3312 Stream.Emit((unsigned)'P', 8);
3313 Stream.Emit((unsigned)'C', 8);
3314 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003315
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003316 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003317
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003318 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003319 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003320 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003321 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003322 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003323 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003324 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003325
3326 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003327}
3328
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003329template<typename Vector>
3330static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3331 ASTWriter::RecordData &Record) {
3332 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3333 I != E; ++I) {
3334 Writer.AddDeclRef(*I, Record);
3335 }
3336}
3337
Sebastian Redla4232eb2010-08-18 23:56:21 +00003338void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003339 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003340 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003341 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003342 using namespace llvm;
3343
Douglas Gregorecc2c092011-12-01 22:20:10 +00003344 // Make sure that the AST reader knows to finalize itself.
3345 if (Chain)
3346 Chain->finalizeForWriting();
3347
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003348 ASTContext &Context = SemaRef.Context;
3349 Preprocessor &PP = SemaRef.PP;
3350
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003351 // Set up predefined declaration IDs.
3352 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003353 if (Context.ObjCIdDecl)
3354 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003355 if (Context.ObjCSelDecl)
3356 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003357 if (Context.ObjCClassDecl)
3358 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003359 if (Context.ObjCProtocolClassDecl)
3360 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003361 if (Context.Int128Decl)
3362 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3363 if (Context.UInt128Decl)
3364 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003365 if (Context.ObjCInstanceTypeDecl)
3366 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003367 if (Context.BuiltinVaListDecl)
3368 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3369
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003370 if (!Chain) {
3371 // Make sure that we emit IdentifierInfos (and any attached
3372 // declarations) for builtins. We don't need to do this when we're
3373 // emitting chained PCH files, because all of the builtins will be
3374 // in the original PCH file.
3375 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003376 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003377 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003378 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003379 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003380 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3381 getIdentifierRef(&Table.get(BuiltinNames[I]));
3382 }
3383
Douglas Gregoreee242f2011-10-27 09:33:13 +00003384 // If there are any out-of-date identifiers, bring them up to date.
3385 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3386 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3387 IDEnd = PP.getIdentifierTable().end();
3388 ID != IDEnd; ++ID)
3389 if (ID->second->isOutOfDate())
3390 ExtSource->updateOutOfDateIdentifier(*ID->second);
3391 }
3392
Chris Lattner63d65f82009-09-08 18:19:27 +00003393 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003394 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003395 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003396 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003397 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003398
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003399 // Build a record containing all of the file scoped decls in this file.
3400 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003401 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3402 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003403
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003404 // Build a record containing all of the delegating constructors we still need
3405 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003406 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003407 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003408
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003409 // Write the set of weak, undeclared identifiers. We always write the
3410 // entire table, since later PCH files in a PCH chain are only interested in
3411 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003412 RecordData WeakUndeclaredIdentifiers;
3413 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003414 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003415 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3416 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3417 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3418 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3419 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3420 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3421 }
3422 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003423
Douglas Gregor14c22f22009-04-22 22:18:58 +00003424 // Build a record containing all of the locally-scoped external
3425 // declarations in this header file. Generally, this record will be
3426 // empty.
3427 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003428 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003429 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003430 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003431 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3432 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003433 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003434 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003435 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3436 }
3437
Douglas Gregorb81c1702009-04-27 20:06:05 +00003438 // Build a record containing all of the ext_vector declarations.
3439 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003440 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003441
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003442 // Build a record containing all of the VTable uses information.
3443 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003444 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003445 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3446 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3447 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3448 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3449 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003450 }
3451
3452 // Build a record containing all of dynamic classes declarations.
3453 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003454 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003455
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003456 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003457 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003458 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003459 I = SemaRef.PendingInstantiations.begin(),
3460 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3461 AddDeclRef(I->first, PendingInstantiations);
3462 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003463 }
3464 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3465 "There are local ones at end of translation unit!");
3466
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003467 // Build a record containing some declaration references.
3468 RecordData SemaDeclRefs;
3469 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3470 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3471 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3472 }
3473
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003474 RecordData CUDASpecialDeclRefs;
3475 if (Context.getcudaConfigureCallDecl()) {
3476 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3477 }
3478
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003479 // Build a record containing all of the known namespaces.
3480 RecordData KnownNamespaces;
3481 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3482 I = SemaRef.KnownNamespaces.begin(),
3483 IEnd = SemaRef.KnownNamespaces.end();
3484 I != IEnd; ++I) {
3485 if (!I->second)
3486 AddDeclRef(I->first, KnownNamespaces);
3487 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003488
3489 // Write the control block
3490 WriteControlBlock(Context, isysroot, OutputFile);
3491
Sebastian Redl3397c552010-08-18 23:56:27 +00003492 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003493 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003494 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003495 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003496 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003497
3498 // Create a lexical update block containing all of the declarations in the
3499 // translation unit that do not come from other AST files.
3500 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3501 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3502 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3503 E = TU->noload_decls_end();
3504 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003505 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003506 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003507 }
3508
3509 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3510 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3511 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3512 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3513 Record.clear();
3514 Record.push_back(TU_UPDATE_LEXICAL);
3515 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3516 data(NewGlobalDecls));
3517
3518 // And a visible updates block for the translation unit.
3519 Abv = new llvm::BitCodeAbbrev();
3520 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3521 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3522 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3523 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3524 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3525 WriteDeclContextVisibleUpdate(TU);
3526
3527 // If the translation unit has an anonymous namespace, and we don't already
3528 // have an update block for it, write it as an update block.
3529 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3530 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3531 if (Record.empty()) {
3532 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003533 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003534 }
3535 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003536
3537 // Make sure visible decls, added to DeclContexts previously loaded from
3538 // an AST file, are registered for serialization.
3539 for (SmallVector<const Decl *, 16>::iterator
3540 I = UpdatingVisibleDecls.begin(),
3541 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3542 GetDeclRef(*I);
3543 }
3544
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003545 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003546 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003547
Douglas Gregora119da02011-08-02 16:26:37 +00003548 // Form the record of special types.
3549 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003550 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003551 AddTypeRef(Context.getFILEType(), SpecialTypes);
3552 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3553 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3554 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3555 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003556 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003557 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003558
Douglas Gregor366809a2009-04-26 03:49:13 +00003559 // Keep writing types and declarations until all types and
3560 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003561 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003562 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003563 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3564 E = DeclsToRewrite.end();
3565 I != E; ++I)
3566 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003567 while (!DeclTypesToEmit.empty()) {
3568 DeclOrType DOT = DeclTypesToEmit.front();
3569 DeclTypesToEmit.pop();
3570 if (DOT.isType())
3571 WriteType(DOT.getType());
3572 else
3573 WriteDecl(Context, DOT.getDecl());
3574 }
3575 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003576
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003577 DoneWritingDeclsAndTypes = true;
3578
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003579 WriteFileDeclIDsMap();
3580 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003581 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003582
3583 if (Chain) {
3584 // Write the mapping information describing our module dependencies and how
3585 // each of those modules were mapped into our own offset/ID space, so that
3586 // the reader can build the appropriate mapping to its own offset/ID space.
3587 // The map consists solely of a blob with the following format:
3588 // *(module-name-len:i16 module-name:len*i8
3589 // source-location-offset:i32
3590 // identifier-id:i32
3591 // preprocessed-entity-id:i32
3592 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003593 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003594 // selector-id:i32
3595 // declaration-id:i32
3596 // c++-base-specifiers-id:i32
3597 // type-id:i32)
3598 //
3599 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3600 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3602 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003603 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003604 {
3605 llvm::raw_svector_ostream Out(Buffer);
3606 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003607 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003608 M != MEnd; ++M) {
3609 StringRef FileName = (*M)->FileName;
3610 io::Emit16(Out, FileName.size());
3611 Out.write(FileName.data(), FileName.size());
3612 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3613 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003614 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003615 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003616 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003617 io::Emit32(Out, (*M)->BaseSelectorID);
3618 io::Emit32(Out, (*M)->BaseDeclID);
3619 io::Emit32(Out, (*M)->BaseTypeIndex);
3620 }
3621 }
3622 Record.clear();
3623 Record.push_back(MODULE_OFFSET_MAP);
3624 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3625 Buffer.data(), Buffer.size());
3626 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003627 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003628 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003629 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003630 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003631 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003632 WriteFPPragmaOptions(SemaRef.getFPOptions());
3633 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003634
Sebastian Redl1476ed42010-07-16 16:36:56 +00003635 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003636 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003637
Anders Carlssonc8505782011-03-06 18:41:18 +00003638 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003639
Douglas Gregore209e502011-12-06 01:10:29 +00003640 // If we're emitting a module, write out the submodule information.
3641 if (WritingModule)
3642 WriteSubmodules(WritingModule);
3643
Douglas Gregora119da02011-08-02 16:26:37 +00003644 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3645
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003646 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003647 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003648 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003649
3650 // Write the record containing tentative definitions.
3651 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003652 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003653
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003654 // Write the record containing unused file scoped decls.
3655 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003656 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003657
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003658 // Write the record containing weak undeclared identifiers.
3659 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003660 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003661 WeakUndeclaredIdentifiers);
3662
Douglas Gregor14c22f22009-04-22 22:18:58 +00003663 // Write the record containing locally-scoped external definitions.
3664 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003665 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003666 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003667
3668 // Write the record containing ext_vector type names.
3669 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003670 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003671
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003672 // Write the record containing VTable uses information.
3673 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003674 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003675
3676 // Write the record containing dynamic classes declarations.
3677 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003678 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003679
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003680 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003681 if (!PendingInstantiations.empty())
3682 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003683
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003684 // Write the record containing declaration references of Sema.
3685 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003686 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003687
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003688 // Write the record containing CUDA-specific declaration references.
3689 if (!CUDASpecialDeclRefs.empty())
3690 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003691
3692 // Write the delegating constructors.
3693 if (!DelegatingCtorDecls.empty())
3694 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003695
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003696 // Write the known namespaces.
3697 if (!KnownNamespaces.empty())
3698 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3699
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003700 // Write the visible updates to DeclContexts.
3701 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3702 I = UpdatedDeclContexts.begin(),
3703 E = UpdatedDeclContexts.end();
3704 I != E; ++I)
3705 WriteDeclContextVisibleUpdate(*I);
3706
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003707 if (!WritingModule) {
3708 // Write the submodules that were imported, if any.
3709 RecordData ImportedModules;
3710 for (ASTContext::import_iterator I = Context.local_import_begin(),
3711 IEnd = Context.local_import_end();
3712 I != IEnd; ++I) {
3713 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3714 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3715 }
3716 if (!ImportedModules.empty()) {
3717 // Sort module IDs.
3718 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3719
3720 // Unique module IDs.
3721 ImportedModules.erase(std::unique(ImportedModules.begin(),
3722 ImportedModules.end()),
3723 ImportedModules.end());
3724
3725 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3726 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003727 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003728
3729 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003730 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003731 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003732 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003733 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003734 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003735
Douglas Gregor3e1af842009-04-17 22:13:46 +00003736 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003737 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003738 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003739 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003740 Record.push_back(NumLexicalDeclContexts);
3741 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003742 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003743 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003744}
3745
Douglas Gregora8235d62012-10-09 23:05:51 +00003746void ASTWriter::WriteMacroUpdates() {
3747 if (MacroUpdates.empty())
3748 return;
3749
3750 RecordData Record;
3751 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3752 E = MacroUpdates.end();
3753 I != E; ++I) {
3754 addMacroRef(I->first, Record);
3755 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003756 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003757 }
3758 Stream.EmitRecord(MACRO_UPDATES, Record);
3759}
3760
Douglas Gregor61c5e342011-09-17 00:05:03 +00003761/// \brief Go through the declaration update blocks and resolve declaration
3762/// pointers into declaration IDs.
3763void ASTWriter::ResolveDeclUpdatesBlocks() {
3764 for (DeclUpdateMap::iterator
3765 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3766 const Decl *D = I->first;
3767 UpdateRecord &URec = I->second;
3768
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003769 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003770 continue; // The decl will be written completely
3771
3772 unsigned Idx = 0, N = URec.size();
3773 while (Idx < N) {
3774 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003775 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3776 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3777 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3778 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3779 ++Idx;
3780 break;
3781
3782 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3783 ++Idx;
3784 break;
3785 }
3786 }
3787 }
3788}
3789
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003790void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003791 if (DeclUpdates.empty())
3792 return;
3793
3794 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003795 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003796 for (DeclUpdateMap::iterator
3797 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3798 const Decl *D = I->first;
3799 UpdateRecord &URec = I->second;
3800
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003801 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003802 continue; // The decl will be written completely,no need to store updates.
3803
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003804 uint64_t Offset = Stream.GetCurrentBitNo();
3805 Stream.EmitRecord(DECL_UPDATES, URec);
3806
3807 OffsetsRecord.push_back(GetDeclRef(D));
3808 OffsetsRecord.push_back(Offset);
3809 }
3810 Stream.ExitBlock();
3811 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3812}
3813
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003814void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003815 if (ReplacedDecls.empty())
3816 return;
3817
3818 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003819 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003820 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003821 Record.push_back(I->ID);
3822 Record.push_back(I->Offset);
3823 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003824 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003825 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003826}
3827
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003828void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003829 Record.push_back(Loc.getRawEncoding());
3830}
3831
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003832void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003833 AddSourceLocation(Range.getBegin(), Record);
3834 AddSourceLocation(Range.getEnd(), Record);
3835}
3836
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003837void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003838 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003839 const uint64_t *Words = Value.getRawData();
3840 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003841}
3842
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003843void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003844 Record.push_back(Value.isUnsigned());
3845 AddAPInt(Value, Record);
3846}
3847
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003848void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003849 AddAPInt(Value.bitcastToAPInt(), Record);
3850}
3851
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003852void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003853 Record.push_back(getIdentifierRef(II));
3854}
3855
Douglas Gregora8235d62012-10-09 23:05:51 +00003856void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3857 Record.push_back(getMacroRef(MI));
3858}
3859
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003860IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003861 if (II == 0)
3862 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003863
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003864 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003865 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003866 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003867 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003868}
3869
Douglas Gregora8235d62012-10-09 23:05:51 +00003870MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3871 // Don't emit builtin macros like __LINE__ to the AST file unless they
3872 // have been redefined by the header (in which case they are not
3873 // isBuiltinMacro).
3874 if (MI == 0 || MI->isBuiltinMacro())
3875 return 0;
3876
3877 MacroID &ID = MacroIDs[MI];
3878 if (ID == 0)
3879 ID = NextMacroID++;
3880 return ID;
3881}
3882
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003883void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003884 Record.push_back(getSelectorRef(SelRef));
3885}
3886
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003887SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003888 if (Sel.getAsOpaquePtr() == 0) {
3889 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003890 }
3891
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003892 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003893 if (SID == 0 && Chain) {
3894 // This might trigger a ReadSelector callback, which will set the ID for
3895 // this selector.
3896 Chain->LoadSelector(Sel);
3897 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003898 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003899 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003900 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003901 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003902}
3903
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003904void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003905 AddDeclRef(Temp->getDestructor(), Record);
3906}
3907
Douglas Gregor7c789c12010-10-29 22:39:52 +00003908void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3909 CXXBaseSpecifier const *BasesEnd,
3910 RecordDataImpl &Record) {
3911 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3912 CXXBaseSpecifiersToWrite.push_back(
3913 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3914 Bases, BasesEnd));
3915 Record.push_back(NextCXXBaseSpecifiersID++);
3916}
3917
Sebastian Redla4232eb2010-08-18 23:56:21 +00003918void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003919 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003920 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003921 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003922 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003923 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003924 break;
3925 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003926 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003927 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003928 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003929 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003930 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003931 break;
3932 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003933 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003934 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003935 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003936 break;
John McCall833ca992009-10-29 08:12:44 +00003937 case TemplateArgument::Null:
3938 case TemplateArgument::Integral:
3939 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003940 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003941 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003942 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003943 break;
3944 }
3945}
3946
Sebastian Redla4232eb2010-08-18 23:56:21 +00003947void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003948 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003949 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003950
3951 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3952 bool InfoHasSameExpr
3953 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3954 Record.push_back(InfoHasSameExpr);
3955 if (InfoHasSameExpr)
3956 return; // Avoid storing the same expr twice.
3957 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003958 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3959 Record);
3960}
3961
Douglas Gregordc355712011-02-25 00:36:19 +00003962void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3963 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003964 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003965 AddTypeRef(QualType(), Record);
3966 return;
3967 }
3968
Douglas Gregordc355712011-02-25 00:36:19 +00003969 AddTypeLoc(TInfo->getTypeLoc(), Record);
3970}
3971
3972void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3973 AddTypeRef(TL.getType(), Record);
3974
John McCalla1ee0c52009-10-16 21:56:05 +00003975 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003976 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003977 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003978}
3979
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003980void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003981 Record.push_back(GetOrCreateTypeID(T));
3982}
3983
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003984TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3985 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003986 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3987}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003988
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003989TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003990 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003991 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003992}
3993
3994TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3995 if (T.isNull())
3996 return TypeIdx();
3997 assert(!T.getLocalFastQualifiers());
3998
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003999 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004000 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004001 if (DoneWritingDeclsAndTypes) {
4002 assert(0 && "New type seen after serializing all the types to emit!");
4003 return TypeIdx();
4004 }
4005
Douglas Gregor366809a2009-04-26 03:49:13 +00004006 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004007 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004008 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004009 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004010 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004011 return Idx;
4012}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004013
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004014TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004015 if (T.isNull())
4016 return TypeIdx();
4017 assert(!T.getLocalFastQualifiers());
4018
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004019 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4020 assert(I != TypeIdxs.end() && "Type not emitted!");
4021 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004022}
4023
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004024void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004025 Record.push_back(GetDeclRef(D));
4026}
4027
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004028DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004029 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4030
Douglas Gregor2cf26342009-04-09 22:27:44 +00004031 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004032 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004033 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004034
4035 // If D comes from an AST file, its declaration ID is already known and
4036 // fixed.
4037 if (D->isFromASTFile())
4038 return D->getGlobalID();
4039
Douglas Gregor97475832010-10-05 18:37:06 +00004040 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004041 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004042 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004043 if (DoneWritingDeclsAndTypes) {
4044 assert(0 && "New decl seen after serializing all the decls to emit!");
4045 return 0;
4046 }
4047
Douglas Gregor2cf26342009-04-09 22:27:44 +00004048 // We haven't seen this declaration before. Give it a new ID and
4049 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004050 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004051 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004052 }
4053
Sebastian Redl681d7232010-07-27 00:17:23 +00004054 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004055}
4056
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004057DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004058 if (D == 0)
4059 return 0;
4060
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004061 // If D comes from an AST file, its declaration ID is already known and
4062 // fixed.
4063 if (D->isFromASTFile())
4064 return D->getGlobalID();
4065
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004066 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4067 return DeclIDs[D];
4068}
4069
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004070static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4071 std::pair<unsigned, serialization::DeclID> R) {
4072 return L.first < R.first;
4073}
4074
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004075void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004076 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004077 assert(D);
4078
4079 SourceLocation Loc = D->getLocation();
4080 if (Loc.isInvalid())
4081 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004082
4083 // We only keep track of the file-level declarations of each file.
4084 if (!D->getLexicalDeclContext()->isFileContext())
4085 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004086 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4087 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004088 if (isa<ParmVarDecl>(D))
4089 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004090
4091 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004092 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004093 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004094 FileID FID;
4095 unsigned Offset;
4096 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004097 if (FID.isInvalid())
4098 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004099 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004100
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004101 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004102 if (!Info)
4103 Info = new DeclIDInFileInfo();
4104
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004105 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004106 LocDeclIDsTy &Decls = Info->DeclIDs;
4107
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004108 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004109 Decls.push_back(LocDecl);
4110 return;
4111 }
4112
4113 LocDeclIDsTy::iterator
4114 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4115
4116 Decls.insert(I, LocDecl);
4117}
4118
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004119void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004120 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004121 Record.push_back(Name.getNameKind());
4122 switch (Name.getNameKind()) {
4123 case DeclarationName::Identifier:
4124 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4125 break;
4126
4127 case DeclarationName::ObjCZeroArgSelector:
4128 case DeclarationName::ObjCOneArgSelector:
4129 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004130 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004131 break;
4132
4133 case DeclarationName::CXXConstructorName:
4134 case DeclarationName::CXXDestructorName:
4135 case DeclarationName::CXXConversionFunctionName:
4136 AddTypeRef(Name.getCXXNameType(), Record);
4137 break;
4138
4139 case DeclarationName::CXXOperatorName:
4140 Record.push_back(Name.getCXXOverloadedOperator());
4141 break;
4142
Sean Hunt3e518bd2009-11-29 07:34:05 +00004143 case DeclarationName::CXXLiteralOperatorName:
4144 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4145 break;
4146
Douglas Gregor2cf26342009-04-09 22:27:44 +00004147 case DeclarationName::CXXUsingDirective:
4148 // No extra data to emit
4149 break;
4150 }
4151}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004152
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004153void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004154 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004155 switch (Name.getNameKind()) {
4156 case DeclarationName::CXXConstructorName:
4157 case DeclarationName::CXXDestructorName:
4158 case DeclarationName::CXXConversionFunctionName:
4159 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4160 break;
4161
4162 case DeclarationName::CXXOperatorName:
4163 AddSourceLocation(
4164 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4165 Record);
4166 AddSourceLocation(
4167 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4168 Record);
4169 break;
4170
4171 case DeclarationName::CXXLiteralOperatorName:
4172 AddSourceLocation(
4173 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4174 Record);
4175 break;
4176
4177 case DeclarationName::Identifier:
4178 case DeclarationName::ObjCZeroArgSelector:
4179 case DeclarationName::ObjCOneArgSelector:
4180 case DeclarationName::ObjCMultiArgSelector:
4181 case DeclarationName::CXXUsingDirective:
4182 break;
4183 }
4184}
4185
4186void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004187 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004188 AddDeclarationName(NameInfo.getName(), Record);
4189 AddSourceLocation(NameInfo.getLoc(), Record);
4190 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4191}
4192
4193void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004194 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004195 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004196 Record.push_back(Info.NumTemplParamLists);
4197 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4198 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4199}
4200
Sebastian Redla4232eb2010-08-18 23:56:21 +00004201void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004202 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004203 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004204 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004205 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004206
4207 // Push each of the NNS's onto a stack for serialization in reverse order.
4208 while (NNS) {
4209 NestedNames.push_back(NNS);
4210 NNS = NNS->getPrefix();
4211 }
4212
4213 Record.push_back(NestedNames.size());
4214 while(!NestedNames.empty()) {
4215 NNS = NestedNames.pop_back_val();
4216 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4217 Record.push_back(Kind);
4218 switch (Kind) {
4219 case NestedNameSpecifier::Identifier:
4220 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4221 break;
4222
4223 case NestedNameSpecifier::Namespace:
4224 AddDeclRef(NNS->getAsNamespace(), Record);
4225 break;
4226
Douglas Gregor14aba762011-02-24 02:36:08 +00004227 case NestedNameSpecifier::NamespaceAlias:
4228 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4229 break;
4230
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004231 case NestedNameSpecifier::TypeSpec:
4232 case NestedNameSpecifier::TypeSpecWithTemplate:
4233 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4234 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4235 break;
4236
4237 case NestedNameSpecifier::Global:
4238 // Don't need to write an associated value.
4239 break;
4240 }
4241 }
4242}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004243
Douglas Gregordc355712011-02-25 00:36:19 +00004244void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4245 RecordDataImpl &Record) {
4246 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004247 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004248 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004249
4250 // Push each of the nested-name-specifiers's onto a stack for
4251 // serialization in reverse order.
4252 while (NNS) {
4253 NestedNames.push_back(NNS);
4254 NNS = NNS.getPrefix();
4255 }
4256
4257 Record.push_back(NestedNames.size());
4258 while(!NestedNames.empty()) {
4259 NNS = NestedNames.pop_back_val();
4260 NestedNameSpecifier::SpecifierKind Kind
4261 = NNS.getNestedNameSpecifier()->getKind();
4262 Record.push_back(Kind);
4263 switch (Kind) {
4264 case NestedNameSpecifier::Identifier:
4265 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4266 AddSourceRange(NNS.getLocalSourceRange(), Record);
4267 break;
4268
4269 case NestedNameSpecifier::Namespace:
4270 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4271 AddSourceRange(NNS.getLocalSourceRange(), Record);
4272 break;
4273
4274 case NestedNameSpecifier::NamespaceAlias:
4275 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4276 AddSourceRange(NNS.getLocalSourceRange(), Record);
4277 break;
4278
4279 case NestedNameSpecifier::TypeSpec:
4280 case NestedNameSpecifier::TypeSpecWithTemplate:
4281 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4282 AddTypeLoc(NNS.getTypeLoc(), Record);
4283 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4284 break;
4285
4286 case NestedNameSpecifier::Global:
4287 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4288 break;
4289 }
4290 }
4291}
4292
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004293void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004294 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004295 Record.push_back(Kind);
4296 switch (Kind) {
4297 case TemplateName::Template:
4298 AddDeclRef(Name.getAsTemplateDecl(), Record);
4299 break;
4300
4301 case TemplateName::OverloadedTemplate: {
4302 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4303 Record.push_back(OvT->size());
4304 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4305 I != E; ++I)
4306 AddDeclRef(*I, Record);
4307 break;
4308 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004309
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004310 case TemplateName::QualifiedTemplate: {
4311 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4312 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4313 Record.push_back(QualT->hasTemplateKeyword());
4314 AddDeclRef(QualT->getTemplateDecl(), Record);
4315 break;
4316 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004317
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004318 case TemplateName::DependentTemplate: {
4319 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4320 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4321 Record.push_back(DepT->isIdentifier());
4322 if (DepT->isIdentifier())
4323 AddIdentifierRef(DepT->getIdentifier(), Record);
4324 else
4325 Record.push_back(DepT->getOperator());
4326 break;
4327 }
John McCall14606042011-06-30 08:33:18 +00004328
4329 case TemplateName::SubstTemplateTemplateParm: {
4330 SubstTemplateTemplateParmStorage *subst
4331 = Name.getAsSubstTemplateTemplateParm();
4332 AddDeclRef(subst->getParameter(), Record);
4333 AddTemplateName(subst->getReplacement(), Record);
4334 break;
4335 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004336
4337 case TemplateName::SubstTemplateTemplateParmPack: {
4338 SubstTemplateTemplateParmPackStorage *SubstPack
4339 = Name.getAsSubstTemplateTemplateParmPack();
4340 AddDeclRef(SubstPack->getParameterPack(), Record);
4341 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4342 break;
4343 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004344 }
4345}
4346
Michael J. Spencer20249a12010-10-21 03:16:25 +00004347void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004348 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004349 Record.push_back(Arg.getKind());
4350 switch (Arg.getKind()) {
4351 case TemplateArgument::Null:
4352 break;
4353 case TemplateArgument::Type:
4354 AddTypeRef(Arg.getAsType(), Record);
4355 break;
4356 case TemplateArgument::Declaration:
4357 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004358 Record.push_back(Arg.isDeclForReferenceParam());
4359 break;
4360 case TemplateArgument::NullPtr:
4361 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004362 break;
4363 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004364 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004365 AddTypeRef(Arg.getIntegralType(), Record);
4366 break;
4367 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004368 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4369 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004370 case TemplateArgument::TemplateExpansion:
4371 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004372 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4373 Record.push_back(*NumExpansions + 1);
4374 else
4375 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004376 break;
4377 case TemplateArgument::Expression:
4378 AddStmt(Arg.getAsExpr());
4379 break;
4380 case TemplateArgument::Pack:
4381 Record.push_back(Arg.pack_size());
4382 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4383 I != E; ++I)
4384 AddTemplateArgument(*I, Record);
4385 break;
4386 }
4387}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004388
4389void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004390ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004391 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004392 assert(TemplateParams && "No TemplateParams!");
4393 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4394 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4395 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4396 Record.push_back(TemplateParams->size());
4397 for (TemplateParameterList::const_iterator
4398 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4399 P != PEnd; ++P)
4400 AddDeclRef(*P, Record);
4401}
4402
4403/// \brief Emit a template argument list.
4404void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004405ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004406 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004407 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004408 Record.push_back(TemplateArgs->size());
4409 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004410 AddTemplateArgument(TemplateArgs->get(i), Record);
4411}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004412
4413
4414void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004415ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004416 Record.push_back(Set.size());
4417 for (UnresolvedSetImpl::const_iterator
4418 I = Set.begin(), E = Set.end(); I != E; ++I) {
4419 AddDeclRef(I.getDecl(), Record);
4420 Record.push_back(I.getAccess());
4421 }
4422}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004423
Sebastian Redla4232eb2010-08-18 23:56:21 +00004424void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004425 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004426 Record.push_back(Base.isVirtual());
4427 Record.push_back(Base.isBaseOfClass());
4428 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004429 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004430 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004431 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004432 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4433 : SourceLocation(),
4434 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004435}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004436
Douglas Gregor7c789c12010-10-29 22:39:52 +00004437void ASTWriter::FlushCXXBaseSpecifiers() {
4438 RecordData Record;
4439 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4440 Record.clear();
4441
4442 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004443 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004444 if (Index == CXXBaseSpecifiersOffsets.size())
4445 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4446 else {
4447 if (Index > CXXBaseSpecifiersOffsets.size())
4448 CXXBaseSpecifiersOffsets.resize(Index + 1);
4449 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4450 }
4451
4452 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4453 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4454 Record.push_back(BEnd - B);
4455 for (; B != BEnd; ++B)
4456 AddCXXBaseSpecifier(*B, Record);
4457 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004458
4459 // Flush any expressions that were written as part of the base specifiers.
4460 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004461 }
4462
4463 CXXBaseSpecifiersToWrite.clear();
4464}
4465
Sean Huntcbb67482011-01-08 20:30:50 +00004466void ASTWriter::AddCXXCtorInitializers(
4467 const CXXCtorInitializer * const *CtorInitializers,
4468 unsigned NumCtorInitializers,
4469 RecordDataImpl &Record) {
4470 Record.push_back(NumCtorInitializers);
4471 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4472 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004473
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004474 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004475 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004476 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004477 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004478 } else if (Init->isDelegatingInitializer()) {
4479 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004480 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004481 } else if (Init->isMemberInitializer()){
4482 Record.push_back(CTOR_INITIALIZER_MEMBER);
4483 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004484 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004485 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4486 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004487 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004488
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004489 AddSourceLocation(Init->getMemberLocation(), Record);
4490 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004491 AddSourceLocation(Init->getLParenLoc(), Record);
4492 AddSourceLocation(Init->getRParenLoc(), Record);
4493 Record.push_back(Init->isWritten());
4494 if (Init->isWritten()) {
4495 Record.push_back(Init->getSourceOrder());
4496 } else {
4497 Record.push_back(Init->getNumArrayIndices());
4498 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4499 AddDeclRef(Init->getArrayIndex(i), Record);
4500 }
4501 }
4502}
4503
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004504void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4505 assert(D->DefinitionData);
4506 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004507 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004508 Record.push_back(Data.UserDeclaredConstructor);
4509 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004510 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004511 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004512 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004513 Record.push_back(Data.UserDeclaredDestructor);
4514 Record.push_back(Data.Aggregate);
4515 Record.push_back(Data.PlainOldData);
4516 Record.push_back(Data.Empty);
4517 Record.push_back(Data.Polymorphic);
4518 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004519 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004520 Record.push_back(Data.HasNoNonEmptyBases);
4521 Record.push_back(Data.HasPrivateFields);
4522 Record.push_back(Data.HasProtectedFields);
4523 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004524 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004525 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004526 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004527 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004528 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004529 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004530 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004531 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004532 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004533 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004534 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004535 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004536 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004537 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004538 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004539 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004540 Record.push_back(Data.DeclaredDefaultConstructor);
4541 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004542 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004543 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004544 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004545 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004546 Record.push_back(Data.FailedImplicitMoveConstructor);
4547 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004548 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004549
4550 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004551 if (Data.NumBases > 0)
4552 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4553 Record);
4554
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004555 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4556 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004557 if (Data.NumVBases > 0)
4558 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4559 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004560
4561 AddUnresolvedSet(Data.Conversions, Record);
4562 AddUnresolvedSet(Data.VisibleConversions, Record);
4563 // Data.Definition is the owning decl, no need to write it.
4564 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004565
4566 // Add lambda-specific data.
4567 if (Data.IsLambda) {
4568 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004569 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004570 Record.push_back(Lambda.NumCaptures);
4571 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004572 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004573 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004574 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004575 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4576 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4577 AddSourceLocation(Capture.getLocation(), Record);
4578 Record.push_back(Capture.isImplicit());
4579 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4580 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4581 AddDeclRef(Var, Record);
4582 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4583 : SourceLocation(),
4584 Record);
4585 }
4586 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004587}
4588
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004589void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004590 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004591 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004592 assert(FirstDeclID == NextDeclID &&
4593 FirstTypeID == NextTypeID &&
4594 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004595 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004596 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004597 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004598 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004599
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004600 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004601
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004602 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4603 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4604 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004605 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004606 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004607 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004608 NextDeclID = FirstDeclID;
4609 NextTypeID = FirstTypeID;
4610 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004611 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004612 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004613 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004614}
4615
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004616void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004617 IdentifierIDs[II] = ID;
4618}
4619
Douglas Gregora8235d62012-10-09 23:05:51 +00004620void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4621 MacroIDs[MI] = ID;
4622}
4623
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004624void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004625 // Always take the highest-numbered type index. This copes with an interesting
4626 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004627 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004628 // keep the higher-numbered entry so that we can properly write it out to
4629 // the AST file.
4630 TypeIdx &StoredIdx = TypeIdxs[T];
4631 if (Idx.getIndex() >= StoredIdx.getIndex())
4632 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004633}
4634
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004635void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004636 SelectorIDs[S] = ID;
4637}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004638
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004639void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004640 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004641 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004642 MacroDefinitions[MD] = ID;
4643}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004644
Douglas Gregora015cab2011-12-02 17:30:13 +00004645void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4646 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4647 SubmoduleIDs[Mod] = ID;
4648}
4649
Douglas Gregora8235d62012-10-09 23:05:51 +00004650void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4651 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4652}
4653
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004654void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004655 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004656 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004657 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4658 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004659 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004660 // A forward reference was mutated into a definition. Rewrite it.
4661 // FIXME: This happens during template instantiation, should we
4662 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004663 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004664 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004665 }
4666}
Douglas Gregora8235d62012-10-09 23:05:51 +00004667
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004668void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004669 assert(!WritingAST && "Already writing the AST!");
4670
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004671 // TU and namespaces are handled elsewhere.
4672 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4673 return;
4674
Douglas Gregor919814d2011-09-09 23:01:35 +00004675 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004676 return; // Not a source decl added to a DeclContext from PCH.
4677
4678 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004679 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004680}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004681
4682void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004683 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004684 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004685 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004686 return; // Not a source member added to a class from PCH.
4687 if (!isa<CXXMethodDecl>(D))
4688 return; // We are interested in lazily declared implicit methods.
4689
4690 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004691 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004692 UpdateRecord &Record = DeclUpdates[RD];
4693 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004694 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004695}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004696
4697void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4698 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004699 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004700 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004701 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004702 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004703 return; // Not a source specialization added to a template from PCH.
4704
4705 UpdateRecord &Record = DeclUpdates[TD];
4706 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004707 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004708}
Douglas Gregor89d99802010-11-30 06:16:57 +00004709
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004710void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4711 const FunctionDecl *D) {
4712 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004713 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004714 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004715 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004716 return; // Not a source specialization added to a template from PCH.
4717
4718 UpdateRecord &Record = DeclUpdates[TD];
4719 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004720 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004721}
4722
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004723void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004724 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004725 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004726 return; // Declaration not imported from PCH.
4727
4728 // Implicit decl from a PCH was defined.
4729 // FIXME: Should implicit definition be a separate FunctionDecl?
4730 RewriteDecl(D);
4731}
4732
Sebastian Redlf79a7192011-04-29 08:19:30 +00004733void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004734 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004735 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004736 return;
4737
4738 // Since the actual instantiation is delayed, this really means that we need
4739 // to update the instantiation location.
4740 UpdateRecord &Record = DeclUpdates[D];
4741 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4742 AddSourceLocation(
4743 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4744}
4745
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004746void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4747 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004748 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004749 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004750 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004751
4752 assert(IFD->getDefinition() && "Category on a class without a definition?");
4753 ObjCClassesWithCategories.insert(
4754 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004755}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004756
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004757
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004758void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4759 const ObjCPropertyDecl *OrigProp,
4760 const ObjCCategoryDecl *ClassExt) {
4761 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4762 if (!D)
4763 return;
4764
4765 assert(!WritingAST && "Already writing the AST!");
4766 if (!D->isFromASTFile())
4767 return; // Declaration not imported from PCH.
4768
4769 RewriteDecl(D);
4770}