blob: 6e19977a94a04d65db814be634ed9b522f117b64 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000198 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
199 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
200 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000201 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000203 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205}
206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000209 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000210}
John McCalled976492009-12-04 22:46:56 +0000211
Sebastian Redl3397c552010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000213 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000214 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
215 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000220 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227}
228
Sebastian Redl3397c552010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000231 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000232 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000233}
234
Sean Huntca63c202011-05-24 22:41:36 +0000235void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
236 Writer.AddTypeRef(T->getBaseType(), Record);
237 Writer.AddTypeRef(T->getUnderlyingType(), Record);
238 Record.push_back(T->getUTTKind());
239 Code = TYPE_UNARY_TRANSFORM;
240}
241
Richard Smith34b41d92011-02-20 03:19:35 +0000242void ASTTypeWriter::VisitAutoType(const AutoType *T) {
243 Writer.AddTypeRef(T->getDeducedType(), Record);
244 Code = TYPE_AUTO;
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000248 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000249 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000250 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251 "Cannot serialize in the middle of a type definition");
252}
253
Sebastian Redl3397c552010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000256 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257}
258
Sebastian Redl3397c552010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000261 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262}
263
John McCall9d156a72011-01-06 01:58:22 +0000264void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
265 Writer.AddTypeRef(T->getModifiedType(), Record);
266 Writer.AddTypeRef(T->getEquivalentType(), Record);
267 Record.push_back(T->getAttrKind());
268 Code = TYPE_ATTRIBUTED;
269}
270
Mike Stump1eb44332009-09-09 15:08:12 +0000271void
Sebastian Redl3397c552010-08-18 23:56:27 +0000272ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000273 const SubstTemplateTypeParmType *T) {
274 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
275 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000276 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000277}
278
279void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000280ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
281 const SubstTemplateTypeParmPackType *T) {
282 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
283 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
284 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
285}
286
287void
Sebastian Redl3397c552010-08-18 23:56:27 +0000288ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000289 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000290 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000291 Writer.AddTemplateName(T->getTemplateName(), Record);
292 Record.push_back(T->getNumArgs());
293 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
294 ArgI != ArgE; ++ArgI)
295 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000296 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
297 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000298 : T->getCanonicalTypeInternal(),
299 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000300 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000301}
302
303void
Sebastian Redl3397c552010-08-18 23:56:27 +0000304ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000305 VisitArrayType(T);
306 Writer.AddStmt(T->getSizeExpr());
307 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 const DependentSizedExtVectorType *T) {
314 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000315 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl3397c552010-08-18 23:56:27 +0000319ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320 Record.push_back(T->getDepth());
321 Record.push_back(T->getIndex());
322 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000323 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000324 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000325}
326
327void
Sebastian Redl3397c552010-08-18 23:56:27 +0000328ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000329 Record.push_back(T->getKeyword());
330 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
331 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000332 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
333 : T->getCanonicalTypeInternal(),
334 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000335 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000336}
337
338void
Sebastian Redl3397c552010-08-18 23:56:27 +0000339ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000341 Record.push_back(T->getKeyword());
342 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
343 Writer.AddIdentifierRef(T->getIdentifier(), Record);
344 Record.push_back(T->getNumArgs());
345 for (DependentTemplateSpecializationType::iterator
346 I = T->begin(), E = T->end(); I != E; ++I)
347 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000349}
350
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
352 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000353 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
354 Record.push_back(*NumExpansions + 1);
355 else
356 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000357 Code = TYPE_PACK_EXPANSION;
358}
359
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000360void ASTTypeWriter::VisitParenType(const ParenType *T) {
361 Writer.AddTypeRef(T->getInnerType(), Record);
362 Code = TYPE_PAREN;
363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000366 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000373 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000374 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000375 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000376}
377
Sebastian Redl3397c552010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000379 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000381}
382
Sebastian Redl3397c552010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000384 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000385 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000386 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000387 E = T->qual_end(); I != E; ++I)
388 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000392void
Sebastian Redl3397c552010-08-18 23:56:27 +0000393ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000394 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000395 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000396}
397
Eli Friedmanb001de72011-10-06 23:00:33 +0000398void
399ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
400 Writer.AddTypeRef(T->getValueType(), Record);
401 Code = TYPE_ATOMIC;
402}
403
John McCalla1ee0c52009-10-16 21:56:05 +0000404namespace {
405
406class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000407 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000408 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000409
410public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000411 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000412 : Writer(Writer), Record(Record) { }
413
John McCall51bd8032009-10-18 01:05:36 +0000414#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000415#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000417#include "clang/AST/TypeLocNodes.def"
418
John McCall51bd8032009-10-18 01:05:36 +0000419 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
420 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421};
422
423}
424
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
426 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000429 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
430 if (TL.needsExtraLocalData()) {
431 Record.push_back(TL.getWrittenTypeSpec());
432 Record.push_back(TL.getWrittenSignSpec());
433 Record.push_back(TL.getWrittenWidthSpec());
434 Record.push_back(TL.hasModeAttr());
435 }
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000454 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
458 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
459 Record.push_back(TL.getSizeExpr() ? 1 : 0);
460 if (TL.getSizeExpr())
461 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000462}
John McCall51bd8032009-10-18 01:05:36 +0000463void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
464 VisitArrayTypeLoc(TL);
465}
466void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
473 DependentSizedArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
477 DependentSizedExtVectorTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000487 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
488 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000489 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
490 Writer.AddDeclRef(TL.getArg(i), Record);
491}
492void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
493 VisitFunctionTypeLoc(TL);
494}
495void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
496 VisitFunctionTypeLoc(TL);
497}
John McCalled976492009-12-04 22:46:56 +0000498void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getNameLoc(), Record);
500}
John McCall51bd8032009-10-18 01:05:36 +0000501void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
504void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000505 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
506 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
507 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000510 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
513 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000514}
515void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
516 Writer.AddSourceLocation(TL.getNameLoc(), Record);
517}
Sean Huntca63c202011-05-24 22:41:36 +0000518void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getKWLoc(), Record);
520 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
521 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
522 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
523}
Richard Smith34b41d92011-02-20 03:19:35 +0000524void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall51bd8032009-10-18 01:05:36 +0000527void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall9d156a72011-01-06 01:58:22 +0000533void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
535 if (TL.hasAttrOperand()) {
536 SourceRange range = TL.getAttrOperandParensRange();
537 Writer.AddSourceLocation(range.getBegin(), Record);
538 Writer.AddSourceLocation(range.getEnd(), Record);
539 }
540 if (TL.hasAttrExprOperand()) {
541 Expr *operand = TL.getAttrExprOperand();
542 Record.push_back(operand ? 1 : 0);
543 if (operand) Writer.AddStmt(operand);
544 } else if (TL.hasAttrEnumOperand()) {
545 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
546 }
547}
John McCall51bd8032009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
John McCall49a832b2009-10-18 09:09:24 +0000551void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
552 SubstTemplateTypeParmTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000555void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
556 SubstTemplateTypeParmPackTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall51bd8032009-10-18 01:05:36 +0000559void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
560 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000561 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000562 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
563 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
564 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
565 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000566 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
567 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000568}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000569void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
571 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
572}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000573void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000574 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000576}
John McCall3cb0ebd2010-03-10 03:28:59 +0000577void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
578 Writer.AddSourceLocation(TL.getNameLoc(), Record);
579}
Douglas Gregor4714c122010-03-31 17:34:00 +0000580void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000581 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000582 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
John McCall33500952010-06-11 00:33:02 +0000585void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
586 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000587 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000588 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000589 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000590 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000591 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
592 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
593 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000594 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
595 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000596}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000597void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
599}
John McCall51bd8032009-10-18 01:05:36 +0000600void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000602}
603void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
604 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000605 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
606 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
607 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
608 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000609}
John McCall54e14c42009-10-22 22:37:11 +0000610void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
611 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000612}
Eli Friedmanb001de72011-10-06 23:00:33 +0000613void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
614 Writer.AddSourceLocation(TL.getKWLoc(), Record);
615 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
616 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
617}
John McCalla1ee0c52009-10-16 21:56:05 +0000618
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000619//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000620// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000621//===----------------------------------------------------------------------===//
622
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623static void EmitBlockID(unsigned ID, const char *Name,
624 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000625 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000626 Record.clear();
627 Record.push_back(ID);
628 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
629
630 // Emit the block name if present.
631 if (Name == 0 || Name[0] == 0) return;
632 Record.clear();
633 while (*Name)
634 Record.push_back(*Name++);
635 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
636}
637
638static void EmitRecordID(unsigned ID, const char *Name,
639 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000640 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000641 Record.clear();
642 Record.push_back(ID);
643 while (*Name)
644 Record.push_back(*Name++);
645 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000646}
647
648static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000649 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000650#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000651 RECORD(STMT_STOP);
652 RECORD(STMT_NULL_PTR);
653 RECORD(STMT_NULL);
654 RECORD(STMT_COMPOUND);
655 RECORD(STMT_CASE);
656 RECORD(STMT_DEFAULT);
657 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000658 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000659 RECORD(STMT_IF);
660 RECORD(STMT_SWITCH);
661 RECORD(STMT_WHILE);
662 RECORD(STMT_DO);
663 RECORD(STMT_FOR);
664 RECORD(STMT_GOTO);
665 RECORD(STMT_INDIRECT_GOTO);
666 RECORD(STMT_CONTINUE);
667 RECORD(STMT_BREAK);
668 RECORD(STMT_RETURN);
669 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000670 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000671 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000672 RECORD(EXPR_PREDEFINED);
673 RECORD(EXPR_DECL_REF);
674 RECORD(EXPR_INTEGER_LITERAL);
675 RECORD(EXPR_FLOATING_LITERAL);
676 RECORD(EXPR_IMAGINARY_LITERAL);
677 RECORD(EXPR_STRING_LITERAL);
678 RECORD(EXPR_CHARACTER_LITERAL);
679 RECORD(EXPR_PAREN);
680 RECORD(EXPR_UNARY_OPERATOR);
681 RECORD(EXPR_SIZEOF_ALIGN_OF);
682 RECORD(EXPR_ARRAY_SUBSCRIPT);
683 RECORD(EXPR_CALL);
684 RECORD(EXPR_MEMBER);
685 RECORD(EXPR_BINARY_OPERATOR);
686 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
687 RECORD(EXPR_CONDITIONAL_OPERATOR);
688 RECORD(EXPR_IMPLICIT_CAST);
689 RECORD(EXPR_CSTYLE_CAST);
690 RECORD(EXPR_COMPOUND_LITERAL);
691 RECORD(EXPR_EXT_VECTOR_ELEMENT);
692 RECORD(EXPR_INIT_LIST);
693 RECORD(EXPR_DESIGNATED_INIT);
694 RECORD(EXPR_IMPLICIT_VALUE_INIT);
695 RECORD(EXPR_VA_ARG);
696 RECORD(EXPR_ADDR_LABEL);
697 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000698 RECORD(EXPR_CHOOSE);
699 RECORD(EXPR_GNU_NULL);
700 RECORD(EXPR_SHUFFLE_VECTOR);
701 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000702 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000703 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000704 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000705 RECORD(EXPR_OBJC_ARRAY_LITERAL);
706 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000707 RECORD(EXPR_OBJC_ENCODE);
708 RECORD(EXPR_OBJC_SELECTOR_EXPR);
709 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
710 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
711 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
712 RECORD(EXPR_OBJC_KVC_REF_EXPR);
713 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000714 RECORD(STMT_OBJC_FOR_COLLECTION);
715 RECORD(STMT_OBJC_CATCH);
716 RECORD(STMT_OBJC_FINALLY);
717 RECORD(STMT_OBJC_AT_TRY);
718 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
719 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000720 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000721 RECORD(EXPR_CXX_OPERATOR_CALL);
722 RECORD(EXPR_CXX_CONSTRUCT);
723 RECORD(EXPR_CXX_STATIC_CAST);
724 RECORD(EXPR_CXX_DYNAMIC_CAST);
725 RECORD(EXPR_CXX_REINTERPRET_CAST);
726 RECORD(EXPR_CXX_CONST_CAST);
727 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000728 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000729 RECORD(EXPR_CXX_BOOL_LITERAL);
730 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000731 RECORD(EXPR_CXX_TYPEID_EXPR);
732 RECORD(EXPR_CXX_TYPEID_TYPE);
733 RECORD(EXPR_CXX_UUIDOF_EXPR);
734 RECORD(EXPR_CXX_UUIDOF_TYPE);
735 RECORD(EXPR_CXX_THIS);
736 RECORD(EXPR_CXX_THROW);
737 RECORD(EXPR_CXX_DEFAULT_ARG);
738 RECORD(EXPR_CXX_BIND_TEMPORARY);
739 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
740 RECORD(EXPR_CXX_NEW);
741 RECORD(EXPR_CXX_DELETE);
742 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
743 RECORD(EXPR_EXPR_WITH_CLEANUPS);
744 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
745 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
746 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
747 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
748 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
749 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
750 RECORD(EXPR_CXX_NOEXCEPT);
751 RECORD(EXPR_OPAQUE_VALUE);
752 RECORD(EXPR_BINARY_TYPE_TRAIT);
753 RECORD(EXPR_PACK_EXPANSION);
754 RECORD(EXPR_SIZEOF_PACK);
755 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000756 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000757#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758}
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Sebastian Redla4232eb2010-08-18 23:56:21 +0000760void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000761 RecordData Record;
762 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000764#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
765#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Sebastian Redl3397c552010-08-18 23:56:27 +0000767 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000768 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000769 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000770 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000771 RECORD(TYPE_OFFSET);
772 RECORD(DECL_OFFSET);
773 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000774 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000775 RECORD(IDENTIFIER_OFFSET);
776 RECORD(IDENTIFIER_TABLE);
777 RECORD(EXTERNAL_DEFINITIONS);
778 RECORD(SPECIAL_TYPES);
779 RECORD(STATISTICS);
780 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000781 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000782 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
783 RECORD(SELECTOR_OFFSETS);
784 RECORD(METHOD_POOL);
785 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000786 RECORD(SOURCE_LOCATION_OFFSETS);
787 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000788 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000789 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000790 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000791 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000792 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000793 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000794 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000795 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000796 RECORD(SEMA_DECL_REFS);
797 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
798 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
799 RECORD(DECL_REPLACEMENTS);
800 RECORD(UPDATE_VISIBLE);
801 RECORD(DECL_UPDATE_OFFSETS);
802 RECORD(DECL_UPDATES);
803 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
804 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000805 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000806 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000807 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000808 RECORD(FP_PRAGMA_OPTIONS);
809 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000810 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000811 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
812 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000813 RECORD(MODULE_OFFSET_MAP);
814 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000815 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000816 RECORD(FILE_SORTED_DECLS);
817 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000818 RECORD(MERGED_DECLARATIONS);
819 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000820 RECORD(OBJC_CATEGORIES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000821
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000822 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000823 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000824 RECORD(SM_SLOC_FILE_ENTRY);
825 RECORD(SM_SLOC_BUFFER_ENTRY);
826 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000827 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000829 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000830 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000831 RECORD(PP_MACRO_OBJECT_LIKE);
832 RECORD(PP_MACRO_FUNCTION_LIKE);
833 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000834
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000835 // Decls and Types block.
836 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000837 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000838 RECORD(TYPE_COMPLEX);
839 RECORD(TYPE_POINTER);
840 RECORD(TYPE_BLOCK_POINTER);
841 RECORD(TYPE_LVALUE_REFERENCE);
842 RECORD(TYPE_RVALUE_REFERENCE);
843 RECORD(TYPE_MEMBER_POINTER);
844 RECORD(TYPE_CONSTANT_ARRAY);
845 RECORD(TYPE_INCOMPLETE_ARRAY);
846 RECORD(TYPE_VARIABLE_ARRAY);
847 RECORD(TYPE_VECTOR);
848 RECORD(TYPE_EXT_VECTOR);
849 RECORD(TYPE_FUNCTION_PROTO);
850 RECORD(TYPE_FUNCTION_NO_PROTO);
851 RECORD(TYPE_TYPEDEF);
852 RECORD(TYPE_TYPEOF_EXPR);
853 RECORD(TYPE_TYPEOF);
854 RECORD(TYPE_RECORD);
855 RECORD(TYPE_ENUM);
856 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000857 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000858 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000859 RECORD(TYPE_DECLTYPE);
860 RECORD(TYPE_ELABORATED);
861 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
862 RECORD(TYPE_UNRESOLVED_USING);
863 RECORD(TYPE_INJECTED_CLASS_NAME);
864 RECORD(TYPE_OBJC_OBJECT);
865 RECORD(TYPE_TEMPLATE_TYPE_PARM);
866 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
867 RECORD(TYPE_DEPENDENT_NAME);
868 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
869 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
870 RECORD(TYPE_PAREN);
871 RECORD(TYPE_PACK_EXPANSION);
872 RECORD(TYPE_ATTRIBUTED);
873 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000874 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000875 RECORD(DECL_TYPEDEF);
876 RECORD(DECL_ENUM);
877 RECORD(DECL_RECORD);
878 RECORD(DECL_ENUM_CONSTANT);
879 RECORD(DECL_FUNCTION);
880 RECORD(DECL_OBJC_METHOD);
881 RECORD(DECL_OBJC_INTERFACE);
882 RECORD(DECL_OBJC_PROTOCOL);
883 RECORD(DECL_OBJC_IVAR);
884 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000885 RECORD(DECL_OBJC_CATEGORY);
886 RECORD(DECL_OBJC_CATEGORY_IMPL);
887 RECORD(DECL_OBJC_IMPLEMENTATION);
888 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
889 RECORD(DECL_OBJC_PROPERTY);
890 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000891 RECORD(DECL_FIELD);
892 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000893 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000894 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000895 RECORD(DECL_FILE_SCOPE_ASM);
896 RECORD(DECL_BLOCK);
897 RECORD(DECL_CONTEXT_LEXICAL);
898 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000899 RECORD(DECL_NAMESPACE);
900 RECORD(DECL_NAMESPACE_ALIAS);
901 RECORD(DECL_USING);
902 RECORD(DECL_USING_SHADOW);
903 RECORD(DECL_USING_DIRECTIVE);
904 RECORD(DECL_UNRESOLVED_USING_VALUE);
905 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
906 RECORD(DECL_LINKAGE_SPEC);
907 RECORD(DECL_CXX_RECORD);
908 RECORD(DECL_CXX_METHOD);
909 RECORD(DECL_CXX_CONSTRUCTOR);
910 RECORD(DECL_CXX_DESTRUCTOR);
911 RECORD(DECL_CXX_CONVERSION);
912 RECORD(DECL_ACCESS_SPEC);
913 RECORD(DECL_FRIEND);
914 RECORD(DECL_FRIEND_TEMPLATE);
915 RECORD(DECL_CLASS_TEMPLATE);
916 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
917 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
918 RECORD(DECL_FUNCTION_TEMPLATE);
919 RECORD(DECL_TEMPLATE_TYPE_PARM);
920 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
921 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
922 RECORD(DECL_STATIC_ASSERT);
923 RECORD(DECL_CXX_BASE_SPECIFIERS);
924 RECORD(DECL_INDIRECTFIELD);
925 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
926
Douglas Gregora72d8c42011-06-03 02:27:19 +0000927 // Statements and Exprs can occur in the Decls and Types block.
928 AddStmtsExprs(Stream, Record);
929
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000930 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000931 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000932 RECORD(PPD_MACRO_DEFINITION);
933 RECORD(PPD_INCLUSION_DIRECTIVE);
934
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000935#undef RECORD
936#undef BLOCK
937 Stream.ExitBlock();
938}
939
Douglas Gregore650c8c2009-07-07 00:12:59 +0000940/// \brief Adjusts the given filename to only write out the portion of the
941/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000942///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943/// \param Filename the file name to adjust.
944///
945/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
946/// the returned filename will be adjusted by this system root.
947///
948/// \returns either the original filename (if it needs no adjustment) or the
949/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000950static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000951adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregor832d6202011-07-22 16:35:34 +0000954 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 // Verify that the filename and the system root have the same prefix.
958 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000959 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000960 if (Filename[Pos] != isysroot[Pos])
961 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963 // We hit the end of the filename before we hit the end of the system root.
964 if (!Filename[Pos])
965 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 // If the file name has a '/' at the current position, skip over the '/'.
968 // We distinguish sysroot-based includes from absolute includes by the
969 // absence of '/' at the beginning of sysroot-based includes.
970 if (Filename[Pos] == '/')
971 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 return Filename + Pos;
974}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000975
Sebastian Redl3397c552010-08-18 23:56:27 +0000976/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000977void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000978 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000979 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000980
Douglas Gregore650c8c2009-07-07 00:12:59 +0000981 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000982 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000984 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000985 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
986 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
988 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000990 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregore95b9192011-08-17 21:07:30 +0000991 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000992 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregore650c8c2009-07-07 00:12:59 +0000994 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000995 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000996 Record.push_back(VERSION_MAJOR);
997 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000998 Record.push_back(CLANG_VERSION_MAJOR);
999 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001000 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001001 Record.push_back(ASTHasCompilerErrors);
Douglas Gregore95b9192011-08-17 21:07:30 +00001002 const std::string &Triple = Target.getTriple().getTriple();
1003 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1004
1005 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001006 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1007 llvm::SmallVector<char, 128> ModulePaths;
1008 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001009
1010 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1011 M != MEnd; ++M) {
1012 // Skip modules that weren't directly imported.
1013 if (!(*M)->isDirectlyImported())
1014 continue;
1015
1016 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1017 // FIXME: Write import location, once it matters.
1018 // FIXME: This writes the absolute path for AST files we depend on.
1019 const std::string &FileName = (*M)->FileName;
1020 Record.push_back(FileName.size());
1021 Record.append(FileName.begin(), FileName.end());
1022 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001023 Stream.EmitRecord(IMPORTS, Record);
1024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregor31d375f2011-05-06 21:43:30 +00001026 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001027 SourceManager &SM = Context.getSourceManager();
1028 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1029 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001030 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001031 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1032 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1033
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001034 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001036 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001037
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001038 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001039 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001040 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001041 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001042 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001043 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001044
1045 Record.clear();
1046 Record.push_back(SM.getMainFileID().getOpaqueValue());
1047 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001048 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001049
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001050 // Original PCH directory
1051 if (!OutputFile.empty() && OutputFile != "-") {
1052 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1053 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1055 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1056
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001057 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001058
1059 llvm::sys::fs::make_absolute(OutputPath);
1060 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1061
1062 RecordData Record;
1063 Record.push_back(ORIGINAL_PCH_DIR);
1064 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1065 }
1066
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001067 // Repository branch/version information.
1068 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001069 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001070 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1071 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001072 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001073 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001074 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1075 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001076}
1077
1078/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001079void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001080 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001081#define LANGOPT(Name, Bits, Default, Description) \
1082 Record.push_back(LangOpts.Name);
1083#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1084 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1085#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00001086
1087 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1088 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001089
1090 Record.push_back(LangOpts.CurrentModule.size());
1091 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001092 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001093}
1094
Douglas Gregor14f79002009-04-10 03:52:48 +00001095//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096// stat cache Serialization
1097//===----------------------------------------------------------------------===//
1098
1099namespace {
1100// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001101class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102public:
1103 typedef const char * key_type;
1104 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner74e976b2010-11-23 19:28:12 +00001106 typedef struct stat data_type;
1107 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001108
1109 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001110 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001111 }
Mike Stump1eb44332009-09-09 15:08:12 +00001112
1113 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001114 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001115 data_type_ref Data) {
1116 unsigned StrLen = strlen(path);
1117 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001118 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001119 clang::io::Emit8(Out, DataLen);
1120 return std::make_pair(StrLen + 1, DataLen);
1121 }
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner5f9e2722011-07-23 10:55:15 +00001123 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001124 Out.write(path, KeyLen);
1125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Chris Lattner5f9e2722011-07-23 10:55:15 +00001127 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 data_type_ref Data, unsigned DataLen) {
1129 using namespace clang::io;
1130 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattner74e976b2010-11-23 19:28:12 +00001132 Emit32(Out, (uint32_t) Data.st_ino);
1133 Emit32(Out, (uint32_t) Data.st_dev);
1134 Emit16(Out, (uint16_t) Data.st_mode);
1135 Emit64(Out, (uint64_t) Data.st_mtime);
1136 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001137
1138 assert(Out.tell() - Start == DataLen && "Wrong data length");
1139 }
1140};
1141} // end anonymous namespace
1142
Sebastian Redl3397c552010-08-18 23:56:27 +00001143/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001144void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001145 // Build the on-disk hash table containing information about every
1146 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001147 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001148 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001149 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001150 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001151 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001152 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001153 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001156 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001157 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001158 uint32_t BucketOffset;
1159 {
1160 llvm::raw_svector_ostream Out(StatCacheData);
1161 // Make sure that no bucket is at offset 0
1162 clang::io::Emit32(Out, 0);
1163 BucketOffset = Generator.Emit(Out);
1164 }
1165
1166 // Create a blob abbreviation
1167 using namespace llvm;
1168 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001169 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1173 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1174
1175 // Write the stat cache
1176 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001177 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001178 Record.push_back(BucketOffset);
1179 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001180 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001181}
1182
1183//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001184// Source Manager Serialization
1185//===----------------------------------------------------------------------===//
1186
1187/// \brief Create an abbreviation for the SLocEntry that refers to a
1188/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001189static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001190 using namespace llvm;
1191 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001192 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001197 // FileEntry fields.
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001205 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001206}
1207
1208/// \brief Create an abbreviation for the SLocEntry that refers to a
1209/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001211 using namespace llvm;
1212 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001213 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001220}
1221
1222/// \brief Create an abbreviation for the SLocEntry that refers to a
1223/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001224static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001225 using namespace llvm;
1226 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001227 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001229 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001230}
1231
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001232/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1233/// expansion.
1234static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001235 using namespace llvm;
1236 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001237 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001243 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001244}
1245
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001246namespace {
1247 // Trait used for the on-disk hash table of header search information.
1248 class HeaderFileInfoTrait {
1249 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001250
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001251 // Keep track of the framework names we've used during serialization.
1252 SmallVector<char, 128> FrameworkStringData;
1253 llvm::StringMap<unsigned> FrameworkNameOffset;
1254
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001255 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001256 HeaderFileInfoTrait(ASTWriter &Writer)
1257 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001258
1259 typedef const char *key_type;
1260 typedef key_type key_type_ref;
1261
1262 typedef HeaderFileInfo data_type;
1263 typedef const data_type &data_type_ref;
1264
1265 static unsigned ComputeHash(const char *path) {
1266 // The hash is based only on the filename portion of the key, so that the
1267 // reader can match based on filenames when symlinking or excess path
1268 // elements ("foo/../", "../") change the form of the name. However,
1269 // complete path is still the key.
1270 return llvm::HashString(llvm::sys::path::filename(path));
1271 }
1272
1273 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001274 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001275 data_type_ref Data) {
1276 unsigned StrLen = strlen(path);
1277 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001278 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001279 clang::io::Emit8(Out, DataLen);
1280 return std::make_pair(StrLen + 1, DataLen);
1281 }
1282
Chris Lattner5f9e2722011-07-23 10:55:15 +00001283 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001284 Out.write(path, KeyLen);
1285 }
1286
Chris Lattner5f9e2722011-07-23 10:55:15 +00001287 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001288 data_type_ref Data, unsigned DataLen) {
1289 using namespace clang::io;
1290 uint64_t Start = Out.tell(); (void)Start;
1291
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001292 unsigned char Flags = (Data.isImport << 5)
1293 | (Data.isPragmaOnce << 4)
1294 | (Data.DirInfo << 2)
1295 | (Data.Resolved << 1)
1296 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001297 Emit8(Out, (uint8_t)Flags);
1298 Emit16(Out, (uint16_t) Data.NumIncludes);
1299
1300 if (!Data.ControllingMacro)
1301 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1302 else
1303 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001304
1305 unsigned Offset = 0;
1306 if (!Data.Framework.empty()) {
1307 // If this header refers into a framework, save the framework name.
1308 llvm::StringMap<unsigned>::iterator Pos
1309 = FrameworkNameOffset.find(Data.Framework);
1310 if (Pos == FrameworkNameOffset.end()) {
1311 Offset = FrameworkStringData.size() + 1;
1312 FrameworkStringData.append(Data.Framework.begin(),
1313 Data.Framework.end());
1314 FrameworkStringData.push_back(0);
1315
1316 FrameworkNameOffset[Data.Framework] = Offset;
1317 } else
1318 Offset = Pos->second;
1319 }
1320 Emit32(Out, Offset);
1321
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001322 assert(Out.tell() - Start == DataLen && "Wrong data length");
1323 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001324
1325 const char *strings_begin() const { return FrameworkStringData.begin(); }
1326 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001327 };
1328} // end anonymous namespace
1329
1330/// \brief Write the header search block for the list of files that
1331///
1332/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001333void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001334 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001335 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1336
1337 if (FilesByUID.size() > HS.header_file_size())
1338 FilesByUID.resize(HS.header_file_size());
1339
Benjamin Kramerfacde172012-06-06 17:32:50 +00001340 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001341 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001342 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001343 unsigned NumHeaderSearchEntries = 0;
1344 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1345 const FileEntry *File = FilesByUID[UID];
1346 if (!File)
1347 continue;
1348
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001349 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1350 // from the external source if it was not provided already.
1351 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001352 if (HFI.External && Chain)
1353 continue;
1354
1355 // Turn the file name into an absolute path, if it isn't already.
1356 const char *Filename = File->getName();
1357 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1358
1359 // If we performed any translation on the file name at all, we need to
1360 // save this string, since the generator will refer to it later.
1361 if (Filename != File->getName()) {
1362 Filename = strdup(Filename);
1363 SavedStrings.push_back(Filename);
1364 }
1365
1366 Generator.insert(Filename, HFI, GeneratorTrait);
1367 ++NumHeaderSearchEntries;
1368 }
1369
1370 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001371 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001372 uint32_t BucketOffset;
1373 {
1374 llvm::raw_svector_ostream Out(TableData);
1375 // Make sure that no bucket is at offset 0
1376 clang::io::Emit32(Out, 0);
1377 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1378 }
1379
1380 // Create a blob abbreviation
1381 using namespace llvm;
1382 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1383 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1388 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1389
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001390 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001391 RecordData Record;
1392 Record.push_back(HEADER_SEARCH_TABLE);
1393 Record.push_back(BucketOffset);
1394 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001395 Record.push_back(TableData.size());
1396 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001397 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1398
1399 // Free all of the strings we had to duplicate.
1400 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1401 free((void*)SavedStrings[I]);
1402}
1403
Douglas Gregor14f79002009-04-10 03:52:48 +00001404/// \brief Writes the block containing the serialized form of the
1405/// source manager.
1406///
1407/// TODO: We should probably use an on-disk hash table (stored in a
1408/// blob), indexed based on the file name, so that we only create
1409/// entries for files that we actually need. In the common case (no
1410/// errors), we probably won't have to create file entries for any of
1411/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001412void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001413 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001414 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001415 RecordData Record;
1416
Chris Lattnerf04ad692009-04-10 17:16:57 +00001417 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001418 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001419
1420 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001421 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1422 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1423 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001424 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001425
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001426 // Write out the source location entry table. We skip the first
1427 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001428 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001429 // Write out the offsets of only source location file entries.
1430 // We will go through them in ASTReader::validateFileEntries().
1431 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001432 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001433 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1434 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001435 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001436 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001437 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001438 FileID FID = FileID::get(I);
1439 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001440
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001441 // Record the offset of this source-location entry.
1442 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1443
1444 // Figure out which record code to use.
1445 unsigned Code;
1446 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001447 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1448 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001449 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001450 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1451 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001452 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001453 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001454 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001455 Record.clear();
1456 Record.push_back(Code);
1457
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001458 // Starting offset of this entry within this module, so skip the dummy.
1459 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001460 if (SLoc->isFile()) {
1461 const SrcMgr::FileInfo &File = SLoc->getFile();
1462 Record.push_back(File.getIncludeLoc().getRawEncoding());
1463 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1464 Record.push_back(File.hasLineDirectives());
1465
1466 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001467 if (Content->OrigEntry) {
1468 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001469 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001470
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001471 // The source location entry is a file. The blob associated
1472 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregor2d52be52010-03-21 22:49:54 +00001474 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001475 Record.push_back(Content->OrigEntry->getSize());
1476 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001477 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001478 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001479
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001480 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001481 if (FDI != FileDeclIDs.end()) {
1482 Record.push_back(FDI->second->FirstDeclIndex);
1483 Record.push_back(FDI->second->DeclIDs.size());
1484 } else {
1485 Record.push_back(0);
1486 Record.push_back(0);
1487 }
Douglas Gregora081da52011-11-16 20:05:18 +00001488
Douglas Gregore650c8c2009-07-07 00:12:59 +00001489 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001490 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001491 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001492
1493 // Ask the file manager to fixup the relative path for us. This will
1494 // honor the working directory.
1495 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1496
1497 // FIXME: This call to make_absolute shouldn't be necessary, the
1498 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001499 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001500 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregore650c8c2009-07-07 00:12:59 +00001502 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001503 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001504
1505 if (Content->BufferOverridden) {
1506 Record.clear();
1507 Record.push_back(SM_SLOC_BUFFER_BLOB);
1508 const llvm::MemoryBuffer *Buffer
1509 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1510 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1511 StringRef(Buffer->getBufferStart(),
1512 Buffer->getBufferSize() + 1));
1513 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001514 } else {
1515 // The source location entry is a buffer. The blob associated
1516 // with this entry contains the contents of the buffer.
1517
1518 // We add one to the size so that we capture the trailing NULL
1519 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1520 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001521 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001522 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001524 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001525 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001526 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001527 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001528 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001529 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001530 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001531
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001532 if (strcmp(Name, "<built-in>") == 0) {
1533 PreloadSLocs.push_back(SLocEntryOffsets.size());
1534 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535 }
1536 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001537 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001538 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001539 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1540 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001541 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1542 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001543
1544 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001545 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001546 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001547 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001548 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001549 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001550 }
1551 }
1552
Douglas Gregorc9490c02009-04-16 22:23:12 +00001553 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554
1555 if (SLocEntryOffsets.empty())
1556 return;
1557
Sebastian Redl3397c552010-08-18 23:56:27 +00001558 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001559 // table is used for lazily loading source-location information.
1560 using namespace llvm;
1561 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001562 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1566 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001568 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001569 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001571 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001572 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001573
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001574 Abbrev = new BitCodeAbbrev();
1575 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1578 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1579
1580 Record.clear();
1581 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1582 Record.push_back(SLocFileEntryOffsets.size());
1583 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1584 data(SLocFileEntryOffsets));
1585
Sebastian Redl3397c552010-08-18 23:56:27 +00001586 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001587 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001588 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001589
1590 // Write the line table. It depends on remapping working, so it must come
1591 // after the source location offsets.
1592 if (SourceMgr.hasLineTable()) {
1593 LineTableInfo &LineTable = SourceMgr.getLineTable();
1594
1595 Record.clear();
1596 // Emit the file names
1597 Record.push_back(LineTable.getNumFilenames());
1598 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1599 // Emit the file name
1600 const char *Filename = LineTable.getFilename(I);
1601 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1602 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1603 Record.push_back(FilenameLen);
1604 if (FilenameLen)
1605 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1606 }
1607
1608 // Emit the line entries
1609 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1610 L != LEnd; ++L) {
1611 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001612 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001613 continue;
1614
1615 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001616 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001617
1618 // Emit the line entries
1619 Record.push_back(L->second.size());
1620 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1621 LEEnd = L->second.end();
1622 LE != LEEnd; ++LE) {
1623 Record.push_back(LE->FileOffset);
1624 Record.push_back(LE->LineNo);
1625 Record.push_back(LE->FilenameID);
1626 Record.push_back((unsigned)LE->FileKind);
1627 Record.push_back(LE->IncludeOffset);
1628 }
1629 }
1630 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1631 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001632}
1633
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001634//===----------------------------------------------------------------------===//
1635// Preprocessor Serialization
1636//===----------------------------------------------------------------------===//
1637
Douglas Gregor9c736102011-02-10 18:20:09 +00001638static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1639 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1640 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1641 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1642 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1643 return X.first->getName().compare(Y.first->getName());
1644}
1645
Chris Lattner0b1fb982009-04-10 17:15:23 +00001646/// \brief Writes the block containing the serialized form of the
1647/// preprocessor.
1648///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001649void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001650 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1651 if (PPRec)
1652 WritePreprocessorDetail(*PPRec);
1653
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001654 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001655
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001656 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1657 if (PP.getCounterValue() != 0) {
1658 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001659 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001660 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001661 }
1662
1663 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001664 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Sebastian Redl3397c552010-08-18 23:56:27 +00001666 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001667 // FIXME: use diagnostics subsystem for localization etc.
1668 if (PP.SawDateOrTime())
1669 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregorecdcb882010-10-20 22:00:55 +00001671
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001672 // Loop over all the macro definitions that are live at the end of the file,
1673 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001674
Douglas Gregor9c736102011-02-10 18:20:09 +00001675 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001676 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001677 MacrosToEmit;
1678 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001679 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001680 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001681 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001682 if (!IsModule || I->second->isPublic()) {
1683 MacroDefinitionsSeen.insert(I->first);
1684 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001685 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001686 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001687
Douglas Gregor9c736102011-02-10 18:20:09 +00001688 // Sort the set of macro definitions that need to be serialized by the
1689 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001690 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001691 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001692
Douglas Gregor040a8042011-02-11 00:26:14 +00001693 // Resolve any identifiers that defined macros at the time they were
1694 // deserialized, adding them to the list of macros to emit (if appropriate).
1695 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1696 IdentifierInfo *Name
1697 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001698 if (Name->hadMacroDefinition() && MacroDefinitionsSeen.insert(Name))
Douglas Gregor040a8042011-02-11 00:26:14 +00001699 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1700 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001701
Douglas Gregor9c736102011-02-10 18:20:09 +00001702 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1703 const IdentifierInfo *Name = MacrosToEmit[I].first;
1704 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001705 if (!MI)
1706 continue;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001707
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001708 // History of macro definitions for this identifier in chronological order.
1709 SmallVector<MacroInfo*, 8> MacroHistory;
1710 while (MI) {
1711 MacroHistory.push_back(MI);
1712 MI = MI->getPreviousDefinition();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001713 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001714
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001715 while (!MacroHistory.empty()) {
1716 MI = MacroHistory.pop_back_val();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001717
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001718 // Don't emit builtin macros like __LINE__ to the AST file unless they
1719 // have been redefined by the header (in which case they are not
1720 // isBuiltinMacro).
1721 // Also skip macros from a AST file if we're chaining.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001722
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001723 // FIXME: There is a (probably minor) optimization we could do here, if
1724 // the macro comes from the original PCH but the identifier comes from a
1725 // chained PCH, by storing the offset into the original PCH rather than
1726 // writing the macro definition a second time.
1727 if (MI->isBuiltinMacro() ||
1728 (Chain &&
1729 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1730 MI->isFromAST() && !MI->hasChangedAfterLoad()))
1731 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001732
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001733 AddIdentifierRef(Name, Record);
1734 MacroOffsets[Name] = Stream.GetCurrentBitNo();
1735 AddSourceLocation(MI->getDefinitionLoc(), Record);
1736 AddSourceLocation(MI->getUndefLoc(), Record);
1737 Record.push_back(MI->isUsed());
1738 Record.push_back(MI->isPublic());
1739 AddSourceLocation(MI->getVisibilityLocation(), Record);
1740 unsigned Code;
1741 if (MI->isObjectLike()) {
1742 Code = PP_MACRO_OBJECT_LIKE;
1743 } else {
1744 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001745
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001746 Record.push_back(MI->isC99Varargs());
1747 Record.push_back(MI->isGNUVarargs());
1748 Record.push_back(MI->getNumArgs());
1749 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1750 I != E; ++I)
1751 AddIdentifierRef(*I, Record);
1752 }
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001754 // If we have a detailed preprocessing record, record the macro definition
1755 // ID that corresponds to this macro.
1756 if (PPRec)
1757 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1758
1759 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001760 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001761
1762 // Emit the tokens array.
1763 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1764 // Note that we know that the preprocessor does not have any annotation
1765 // tokens in it because they are created by the parser, and thus can't
1766 // be in a macro definition.
1767 const Token &Tok = MI->getReplacementToken(TokNo);
1768
1769 Record.push_back(Tok.getLocation().getRawEncoding());
1770 Record.push_back(Tok.getLength());
1771
1772 // FIXME: When reading literal tokens, reconstruct the literal pointer
1773 // if it is needed.
1774 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1775 // FIXME: Should translate token kind to a stable encoding.
1776 Record.push_back(Tok.getKind());
1777 // FIXME: Should translate token flags to a stable encoding.
1778 Record.push_back(Tok.getFlags());
1779
1780 Stream.EmitRecord(PP_TOKEN, Record);
1781 Record.clear();
1782 }
1783 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001784 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001785 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001786 Stream.ExitBlock();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001787}
1788
1789void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001790 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001791 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001792
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001793 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001794
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001795 // Enter the preprocessor block.
1796 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001797
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001798 // If the preprocessor has a preprocessing record, emit it.
1799 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001800 using namespace llvm;
1801
1802 // Set up the abbreviation for
1803 unsigned InclusionAbbrev = 0;
1804 {
1805 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1806 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1812 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1813 }
1814
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001815 unsigned FirstPreprocessorEntityID
1816 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1817 + NUM_PREDEF_PP_ENTITY_IDS;
1818 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001819 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001820 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1821 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001822 E != EEnd;
1823 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001824 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001825
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001826 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1827 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001828
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001829 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001830 // Record this macro definition's ID.
1831 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001832
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001833 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001834 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1835 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001836 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001837
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001838 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001839 Record.push_back(ME->isBuiltinMacro());
1840 if (ME->isBuiltinMacro())
1841 AddIdentifierRef(ME->getName(), Record);
1842 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001843 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001844 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001845 continue;
1846 }
1847
1848 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1849 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001850 Record.push_back(ID->getFileName().size());
1851 Record.push_back(ID->wasInQuotes());
1852 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001853 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001854 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001855 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001856 // Check that the FileEntry is not null because it was not resolved and
1857 // we create a PCH even with compiler errors.
1858 if (ID->getFile())
1859 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001860 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1861 continue;
1862 }
1863
1864 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1865 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001866 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001867
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001868 // Write the offsets table for the preprocessing record.
1869 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001870 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1871
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001872 // Write the offsets table for identifier IDs.
1873 using namespace llvm;
1874 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001875 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001878 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001879
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001880 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001881 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001882 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001883 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1884 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001885 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001886}
1887
Douglas Gregore209e502011-12-06 01:10:29 +00001888unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1889 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1890 if (Known != SubmoduleIDs.end())
1891 return Known->second;
1892
1893 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1894}
1895
Douglas Gregor26ced122011-12-01 00:59:36 +00001896/// \brief Compute the number of modules within the given tree (including the
1897/// given module).
1898static unsigned getNumberOfModules(Module *Mod) {
1899 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001900 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1901 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001902 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001903 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001904
1905 return ChildModules + 1;
1906}
1907
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001908void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001909 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001910 // FIXME: This feels like it belongs somewhere else, but there are no
1911 // other consumers of this information.
1912 SourceManager &SrcMgr = PP->getSourceManager();
1913 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1914 for (ASTContext::import_iterator I = Context->local_import_begin(),
1915 IEnd = Context->local_import_end();
1916 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001917 if (Module *ImportedFrom
1918 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1919 SrcMgr))) {
1920 ImportedFrom->Imports.push_back(I->getImportedModule());
1921 }
1922 }
1923
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001924 // Enter the submodule description block.
1925 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1926
1927 // Write the abbreviations needed for the submodules block.
1928 using namespace llvm;
1929 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1930 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1940 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1941
1942 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001943 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1945 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1946
1947 Abbrev = new BitCodeAbbrev();
1948 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1950 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001951
1952 Abbrev = new BitCodeAbbrev();
1953 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1955 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1956
Douglas Gregor51f564f2011-12-31 04:05:44 +00001957 Abbrev = new BitCodeAbbrev();
1958 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1960 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1961
Douglas Gregor26ced122011-12-01 00:59:36 +00001962 // Write the submodule metadata block.
1963 RecordData Record;
1964 Record.push_back(getNumberOfModules(WritingModule));
1965 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1966 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1967
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001968 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001969 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001970 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001971 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001972 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001973 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001974 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001975
1976 // Emit the definition of the block.
1977 Record.clear();
1978 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00001979 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001980 if (Mod->Parent) {
1981 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1982 Record.push_back(SubmoduleIDs[Mod->Parent]);
1983 } else {
1984 Record.push_back(0);
1985 }
1986 Record.push_back(Mod->IsFramework);
1987 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001988 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00001989 Record.push_back(Mod->InferSubmodules);
1990 Record.push_back(Mod->InferExplicitSubmodules);
1991 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001992 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1993
Douglas Gregor51f564f2011-12-31 04:05:44 +00001994 // Emit the requirements.
1995 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1996 Record.clear();
1997 Record.push_back(SUBMODULE_REQUIRES);
1998 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1999 Mod->Requires[I].data(),
2000 Mod->Requires[I].size());
2001 }
2002
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002003 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002004 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002005 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002006 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002007 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002008 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002009 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2010 Record.clear();
2011 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2012 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2013 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002014 }
2015
2016 // Emit the headers.
2017 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2018 Record.clear();
2019 Record.push_back(SUBMODULE_HEADER);
2020 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2021 Mod->Headers[I]->getName());
2022 }
Douglas Gregor55988682011-12-05 16:33:54 +00002023
2024 // Emit the imports.
2025 if (!Mod->Imports.empty()) {
2026 Record.clear();
2027 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002028 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002029 assert(ImportedID && "Unknown submodule!");
2030 Record.push_back(ImportedID);
2031 }
2032 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2033 }
2034
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002035 // Emit the exports.
2036 if (!Mod->Exports.empty()) {
2037 Record.clear();
2038 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002039 if (Module *Exported = Mod->Exports[I].getPointer()) {
2040 unsigned ExportedID = SubmoduleIDs[Exported];
2041 assert(ExportedID > 0 && "Unknown submodule ID?");
2042 Record.push_back(ExportedID);
2043 } else {
2044 Record.push_back(0);
2045 }
2046
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002047 Record.push_back(Mod->Exports[I].getInt());
2048 }
2049 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2050 }
2051
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002052 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002053 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2054 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002055 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002056 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002057 }
2058
2059 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002060
2061 assert((NextSubmoduleID - FirstSubmoduleID
2062 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002063}
2064
Douglas Gregor185dbd72011-12-01 02:07:58 +00002065serialization::SubmoduleID
2066ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002067 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002068 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002069
2070 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002071 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002072 Module *OwningMod
2073 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002074 if (!OwningMod)
2075 return 0;
2076
Douglas Gregore209e502011-12-06 01:10:29 +00002077 // Check whether this submodule is part of our own module.
2078 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002079 return 0;
2080
Douglas Gregore209e502011-12-06 01:10:29 +00002081 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002082}
2083
David Blaikied6471f72011-09-25 23:23:43 +00002084void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002085 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002086 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002087 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2088 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002089 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002090 if (point.Loc.isInvalid())
2091 continue;
2092
2093 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002094 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002095 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002096 if (I->second.isPragma()) {
2097 Record.push_back(I->first);
2098 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002099 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002100 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002101 Record.push_back(-1); // mark the end of the diag/map pairs for this
2102 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002103 }
2104
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002105 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002106 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002107}
2108
Anders Carlssonc8505782011-03-06 18:41:18 +00002109void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2110 if (CXXBaseSpecifiersOffsets.empty())
2111 return;
2112
2113 RecordData Record;
2114
2115 // Create a blob abbreviation for the C++ base specifiers offsets.
2116 using namespace llvm;
2117
2118 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2119 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2122 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2123
Douglas Gregore92b8a12011-08-04 00:01:48 +00002124 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002125 Record.clear();
2126 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2127 Record.push_back(CXXBaseSpecifiersOffsets.size());
2128 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002129 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002130}
2131
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002132//===----------------------------------------------------------------------===//
2133// Type Serialization
2134//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002135
Sebastian Redl3397c552010-08-18 23:56:27 +00002136/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002137void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002138 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002139 if (Idx.getIndex() == 0) // we haven't seen this type before.
2140 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Douglas Gregor97475832010-10-05 18:37:06 +00002142 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002143
Douglas Gregor2cf26342009-04-09 22:27:44 +00002144 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002145 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002146 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002147 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002148 else if (TypeOffsets.size() < Index) {
2149 TypeOffsets.resize(Index + 1);
2150 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002151 }
2152
2153 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Douglas Gregor2cf26342009-04-09 22:27:44 +00002155 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002156 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002157
Douglas Gregora4923eb2009-11-16 21:35:15 +00002158 if (T.hasLocalNonFastQualifiers()) {
2159 Qualifiers Qs = T.getLocalQualifiers();
2160 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002161 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002162 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002163 } else {
2164 switch (T->getTypeClass()) {
2165 // For all of the concrete, non-dependent types, call the
2166 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002167#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002168 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002169#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002170#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002171 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002172 }
2173
2174 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002175 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002176
2177 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002178 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002179}
2180
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002181//===----------------------------------------------------------------------===//
2182// Declaration Serialization
2183//===----------------------------------------------------------------------===//
2184
Douglas Gregor2cf26342009-04-09 22:27:44 +00002185/// \brief Write the block containing all of the declaration IDs
2186/// lexically declared within the given DeclContext.
2187///
2188/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2189/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002190uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002191 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002192 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002193 return 0;
2194
Douglas Gregorc9490c02009-04-16 22:23:12 +00002195 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002196 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002197 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002198 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002199 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2200 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002201 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002202
Douglas Gregor25123082009-04-22 22:34:57 +00002203 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002204 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002205 return Offset;
2206}
2207
Sebastian Redla4232eb2010-08-18 23:56:21 +00002208void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002209 using namespace llvm;
2210 RecordData Record;
2211
2212 // Write the type offsets array
2213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002214 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2218 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2219 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002220 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002221 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002222 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002223 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002224
2225 // Write the declaration offsets array
2226 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2231 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2232 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002233 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002234 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002235 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002236 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002237}
2238
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002239void ASTWriter::WriteFileDeclIDsMap() {
2240 using namespace llvm;
2241 RecordData Record;
2242
2243 // Join the vectors of DeclIDs from all files.
2244 SmallVector<DeclID, 256> FileSortedIDs;
2245 for (FileDeclIDsTy::iterator
2246 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2247 DeclIDInFileInfo &Info = *FI->second;
2248 Info.FirstDeclIndex = FileSortedIDs.size();
2249 for (LocDeclIDsTy::iterator
2250 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2251 FileSortedIDs.push_back(DI->second);
2252 }
2253
2254 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2255 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2258 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2259 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002260 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002261 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2262}
2263
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002264void ASTWriter::WriteComments() {
2265 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002266 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002267 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002268 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2269 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002270 I != E; ++I) {
2271 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002272 AddSourceRange((*I)->getSourceRange(), Record);
2273 Record.push_back((*I)->getKind());
2274 Record.push_back((*I)->isTrailingComment());
2275 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002276 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2277 }
2278 Stream.ExitBlock();
2279}
2280
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002281//===----------------------------------------------------------------------===//
2282// Global Method Pool and Selector Serialization
2283//===----------------------------------------------------------------------===//
2284
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002285namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002286// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002287class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002288 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002289
2290public:
2291 typedef Selector key_type;
2292 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Sebastian Redl5d050072010-08-04 17:20:04 +00002294 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002295 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002296 ObjCMethodList Instance, Factory;
2297 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002298 typedef const data_type& data_type_ref;
2299
Sebastian Redl3397c552010-08-18 23:56:27 +00002300 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002302 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002303 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002304 }
Mike Stump1eb44332009-09-09 15:08:12 +00002305
2306 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002307 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002308 data_type_ref Methods) {
2309 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2310 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002311 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2312 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002313 Method = Method->Next)
2314 if (Method->Method)
2315 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002316 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002317 Method = Method->Next)
2318 if (Method->Method)
2319 DataLen += 4;
2320 clang::io::Emit16(Out, DataLen);
2321 return std::make_pair(KeyLen, DataLen);
2322 }
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Chris Lattner5f9e2722011-07-23 10:55:15 +00002324 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002325 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002326 assert((Start >> 32) == 0 && "Selector key offset too large");
2327 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002328 unsigned N = Sel.getNumArgs();
2329 clang::io::Emit16(Out, N);
2330 if (N == 0)
2331 N = 1;
2332 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002333 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002334 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2335 }
Mike Stump1eb44332009-09-09 15:08:12 +00002336
Chris Lattner5f9e2722011-07-23 10:55:15 +00002337 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002338 data_type_ref Methods, unsigned DataLen) {
2339 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002340 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002341 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002342 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002343 Method = Method->Next)
2344 if (Method->Method)
2345 ++NumInstanceMethods;
2346
2347 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002348 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002349 Method = Method->Next)
2350 if (Method->Method)
2351 ++NumFactoryMethods;
2352
2353 clang::io::Emit16(Out, NumInstanceMethods);
2354 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002355 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002356 Method = Method->Next)
2357 if (Method->Method)
2358 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002359 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002360 Method = Method->Next)
2361 if (Method->Method)
2362 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002363
2364 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002365 }
2366};
2367} // end anonymous namespace
2368
Sebastian Redl059612d2010-08-03 21:58:15 +00002369/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002370///
2371/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002372/// in an on-disk hash table indexed by the selector. The hash table also
2373/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002374void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002375 using namespace llvm;
2376
Sebastian Redl059612d2010-08-03 21:58:15 +00002377 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002378 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002379 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002380 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002381 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002382 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002383 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002384 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Sebastian Redl059612d2010-08-03 21:58:15 +00002386 // Create the on-disk hash table representation. We walk through every
2387 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002388 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002389 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002390 I = SelectorIDs.begin(), E = SelectorIDs.end();
2391 I != E; ++I) {
2392 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002393 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002394 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002395 I->second,
2396 ObjCMethodList(),
2397 ObjCMethodList()
2398 };
2399 if (F != SemaRef.MethodPool.end()) {
2400 Data.Instance = F->second.first;
2401 Data.Factory = F->second.second;
2402 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002403 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002404 // changed.
2405 if (Chain && I->second < FirstSelectorID) {
2406 // Selector already exists. Did it change?
2407 bool changed = false;
2408 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2409 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002410 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002411 changed = true;
2412 }
2413 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2414 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002415 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002416 changed = true;
2417 }
2418 if (!changed)
2419 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002420 } else if (Data.Instance.Method || Data.Factory.Method) {
2421 // A new method pool entry.
2422 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002423 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002424 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002425 }
2426
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002427 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002428 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002429 uint32_t BucketOffset;
2430 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002431 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002432 llvm::raw_svector_ostream Out(MethodPool);
2433 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002434 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002435 BucketOffset = Generator.Emit(Out, Trait);
2436 }
2437
2438 // Create a blob abbreviation
2439 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002440 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2444 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2445
Douglas Gregor83941df2009-04-25 17:48:32 +00002446 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002447 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002448 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002449 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002450 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002451 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002452
2453 // Create a blob abbreviation for the selector table offsets.
2454 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002455 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2459 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2460
2461 // Write the selector offsets table.
2462 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002463 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002464 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002465 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002466 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002467 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002468 }
2469}
2470
Sebastian Redl3397c552010-08-18 23:56:27 +00002471/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002472void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002473 using namespace llvm;
2474 if (SemaRef.ReferencedSelectors.empty())
2475 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002476
Fariborz Jahanian32019832010-07-23 19:11:11 +00002477 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002478
Sebastian Redl3397c552010-08-18 23:56:27 +00002479 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002480 // very tricky to fix, and given that @selector shouldn't really appear in
2481 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002482 for (DenseMap<Selector, SourceLocation>::iterator S =
2483 SemaRef.ReferencedSelectors.begin(),
2484 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2485 Selector Sel = (*S).first;
2486 SourceLocation Loc = (*S).second;
2487 AddSelectorRef(Sel, Record);
2488 AddSourceLocation(Loc, Record);
2489 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002490 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002491}
2492
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002493//===----------------------------------------------------------------------===//
2494// Identifier Table Serialization
2495//===----------------------------------------------------------------------===//
2496
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002497namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002498class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002499 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002500 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002501 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002502 bool IsModule;
2503
Douglas Gregora92193e2009-04-28 21:18:29 +00002504 /// \brief Determines whether this is an "interesting" identifier
2505 /// that needs a full IdentifierInfo structure written into the hash
2506 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002507 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002508 if (II->isPoisoned() ||
2509 II->isExtensionToken() ||
2510 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002511 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002512 II->getFETokenInfo<void>())
2513 return true;
2514
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002515 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002516 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002517
2518 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2519 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002520 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002521
2522 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002523 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002524
2525 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002526 }
2527
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002528public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002529 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002530 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002531
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002532 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002533 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Douglas Gregoreee242f2011-10-27 09:33:13 +00002535 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2536 IdentifierResolver &IdResolver, bool IsModule)
2537 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002538
2539 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002540 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002541 }
Mike Stump1eb44332009-09-09 15:08:12 +00002542
2543 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002544 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002545 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002546 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002547 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002548 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002549 DataLen += 2; // 2 bytes for builtin ID
2550 DataLen += 2; // 2 bytes for flags
2551 if (hadMacroDefinition(II, Macro))
Douglas Gregor13292642011-12-02 15:45:10 +00002552 DataLen += 8;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002553
Douglas Gregoreee242f2011-10-27 09:33:13 +00002554 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2555 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002556 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002557 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002558 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002559 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002560 // We emit the key length after the data length so that every
2561 // string is preceded by a 16-bit length. This matches the PTH
2562 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002563 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002564 return std::make_pair(KeyLen, DataLen);
2565 }
Mike Stump1eb44332009-09-09 15:08:12 +00002566
Chris Lattner5f9e2722011-07-23 10:55:15 +00002567 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002568 unsigned KeyLen) {
2569 // Record the location of the key data. This is used when generating
2570 // the mapping from persistent IDs to strings.
2571 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002572 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002573 }
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Douglas Gregor7143aab2011-09-01 17:04:32 +00002575 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002576 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002577 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002578 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002579 clang::io::Emit32(Out, ID << 1);
2580 return;
2581 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002582
Douglas Gregora92193e2009-04-28 21:18:29 +00002583 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002584 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2585 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2586 clang::io::Emit16(Out, Bits);
2587 Bits = 0;
2588 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
2589 bool HasMacroDefinition = HadMacroDefinition && II->hasMacroDefinition();
Douglas Gregorce835df2011-09-14 22:14:14 +00002590 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002591 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002592 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2593 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002594 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002595 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002596 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002597
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002598 if (HadMacroDefinition) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002599 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002600 clang::io::Emit32(Out,
Douglas Gregor13292642011-12-02 15:45:10 +00002601 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2602 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002603
Douglas Gregor668c1a42009-04-21 22:25:48 +00002604 // Emit the declaration IDs in reverse order, because the
2605 // IdentifierResolver provides the declarations as they would be
2606 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002607 // "stat"), but the ASTReader adds declarations to the end of the list
2608 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002609 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002610 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2611 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002612 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002613 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002614 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002615 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002616 }
2617};
2618} // end anonymous namespace
2619
Sebastian Redl3397c552010-08-18 23:56:27 +00002620/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002621///
2622/// The identifier table consists of a blob containing string data
2623/// (the actual identifiers themselves) and a separate "offsets" index
2624/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002625void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2626 IdentifierResolver &IdResolver,
2627 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002628 using namespace llvm;
2629
2630 // Create and write out the blob that contains the identifier
2631 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002632 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002633 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002634 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002635
Douglas Gregor92b059e2009-04-28 20:33:11 +00002636 // Look for any identifiers that were named while processing the
2637 // headers, but are otherwise not needed. We add these to the hash
2638 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002639 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002640 // file.
2641 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2642 IDEnd = PP.getIdentifierTable().end();
2643 ID != IDEnd; ++ID)
2644 getIdentifierRef(ID->second);
2645
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002646 // Create the on-disk hash table representation. We only store offsets
2647 // for identifiers that appear here for the first time.
2648 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002649 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002650 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2651 ID != IDEnd; ++ID) {
2652 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002653 if (!Chain || !ID->first->isFromAST() ||
2654 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002655 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2656 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002657 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002658
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002659 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002660 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002661 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002662 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002663 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002664 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002665 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002666 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002667 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002668 }
2669
2670 // Create a blob abbreviation
2671 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002672 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002674 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002675 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002676
2677 // Write the identifier table
2678 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002679 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002680 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002681 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002682 }
2683
2684 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002685 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002686 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2690 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2691
2692 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002693 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002694 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002695 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002696 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002697 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002698}
2699
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002700//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002701// DeclContext's Name Lookup Table Serialization
2702//===----------------------------------------------------------------------===//
2703
2704namespace {
2705// Trait used for the on-disk hash table used in the method pool.
2706class ASTDeclContextNameLookupTrait {
2707 ASTWriter &Writer;
2708
2709public:
2710 typedef DeclarationName key_type;
2711 typedef key_type key_type_ref;
2712
2713 typedef DeclContext::lookup_result data_type;
2714 typedef const data_type& data_type_ref;
2715
2716 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2717
2718 unsigned ComputeHash(DeclarationName Name) {
2719 llvm::FoldingSetNodeID ID;
2720 ID.AddInteger(Name.getNameKind());
2721
2722 switch (Name.getNameKind()) {
2723 case DeclarationName::Identifier:
2724 ID.AddString(Name.getAsIdentifierInfo()->getName());
2725 break;
2726 case DeclarationName::ObjCZeroArgSelector:
2727 case DeclarationName::ObjCOneArgSelector:
2728 case DeclarationName::ObjCMultiArgSelector:
2729 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2730 break;
2731 case DeclarationName::CXXConstructorName:
2732 case DeclarationName::CXXDestructorName:
2733 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002734 break;
2735 case DeclarationName::CXXOperatorName:
2736 ID.AddInteger(Name.getCXXOverloadedOperator());
2737 break;
2738 case DeclarationName::CXXLiteralOperatorName:
2739 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2740 case DeclarationName::CXXUsingDirective:
2741 break;
2742 }
2743
2744 return ID.ComputeHash();
2745 }
2746
2747 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002748 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002749 data_type_ref Lookup) {
2750 unsigned KeyLen = 1;
2751 switch (Name.getNameKind()) {
2752 case DeclarationName::Identifier:
2753 case DeclarationName::ObjCZeroArgSelector:
2754 case DeclarationName::ObjCOneArgSelector:
2755 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002756 case DeclarationName::CXXLiteralOperatorName:
2757 KeyLen += 4;
2758 break;
2759 case DeclarationName::CXXOperatorName:
2760 KeyLen += 1;
2761 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002762 case DeclarationName::CXXConstructorName:
2763 case DeclarationName::CXXDestructorName:
2764 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002765 case DeclarationName::CXXUsingDirective:
2766 break;
2767 }
2768 clang::io::Emit16(Out, KeyLen);
2769
2770 // 2 bytes for num of decls and 4 for each DeclID.
2771 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2772 clang::io::Emit16(Out, DataLen);
2773
2774 return std::make_pair(KeyLen, DataLen);
2775 }
2776
Chris Lattner5f9e2722011-07-23 10:55:15 +00002777 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002778 using namespace clang::io;
2779
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002780 Emit8(Out, Name.getNameKind());
2781 switch (Name.getNameKind()) {
2782 case DeclarationName::Identifier:
2783 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002784 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002785 case DeclarationName::ObjCZeroArgSelector:
2786 case DeclarationName::ObjCOneArgSelector:
2787 case DeclarationName::ObjCMultiArgSelector:
2788 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002789 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002790 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002791 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2792 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002793 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002794 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002795 case DeclarationName::CXXLiteralOperatorName:
2796 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002797 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002798 case DeclarationName::CXXConstructorName:
2799 case DeclarationName::CXXDestructorName:
2800 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002801 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002802 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002803 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002804
2805 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002806 }
2807
Chris Lattner5f9e2722011-07-23 10:55:15 +00002808 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002809 data_type Lookup, unsigned DataLen) {
2810 uint64_t Start = Out.tell(); (void)Start;
2811 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2812 for (; Lookup.first != Lookup.second; ++Lookup.first)
2813 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2814
2815 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2816 }
2817};
2818} // end anonymous namespace
2819
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002820/// \brief Write the block containing all of the declaration IDs
2821/// visible from the given DeclContext.
2822///
2823/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002824/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002825uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2826 DeclContext *DC) {
2827 if (DC->getPrimaryContext() != DC)
2828 return 0;
2829
2830 // Since there is no name lookup into functions or methods, don't bother to
2831 // build a visible-declarations table for these entities.
2832 if (DC->isFunctionOrMethod())
2833 return 0;
2834
2835 // If not in C++, we perform name lookup for the translation unit via the
2836 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2837 // FIXME: In C++ we need the visible declarations in order to "see" the
2838 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002839 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002840 return 0;
2841
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002842 // Serialize the contents of the mapping used for lookup. Note that,
2843 // although we have two very different code paths, the serialized
2844 // representation is the same for both cases: a declaration name,
2845 // followed by a size, followed by references to the visible
2846 // declarations that have that name.
2847 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002848 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002849 if (!Map || Map->empty())
2850 return 0;
2851
2852 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2853 ASTDeclContextNameLookupTrait Trait(*this);
2854
2855 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002856 DeclarationName ConversionName;
2857 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002858 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2859 D != DEnd; ++D) {
2860 DeclarationName Name = D->first;
2861 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002862 if (Result.first != Result.second) {
2863 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2864 // Hash all conversion function names to the same name. The actual
2865 // type information in conversion function name is not used in the
2866 // key (since such type information is not stable across different
2867 // modules), so the intended effect is to coalesce all of the conversion
2868 // functions under a single key.
2869 if (!ConversionName)
2870 ConversionName = Name;
2871 ConversionDecls.append(Result.first, Result.second);
2872 continue;
2873 }
2874
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002875 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002876 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002877 }
2878
Douglas Gregore5a54b62011-08-30 20:49:19 +00002879 // Add the conversion functions
2880 if (!ConversionDecls.empty()) {
2881 Generator.insert(ConversionName,
2882 DeclContext::lookup_result(ConversionDecls.begin(),
2883 ConversionDecls.end()),
2884 Trait);
2885 }
2886
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002887 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002888 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002889 uint32_t BucketOffset;
2890 {
2891 llvm::raw_svector_ostream Out(LookupTable);
2892 // Make sure that no bucket is at offset 0
2893 clang::io::Emit32(Out, 0);
2894 BucketOffset = Generator.Emit(Out, Trait);
2895 }
2896
2897 // Write the lookup table
2898 RecordData Record;
2899 Record.push_back(DECL_CONTEXT_VISIBLE);
2900 Record.push_back(BucketOffset);
2901 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2902 LookupTable.str());
2903
2904 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2905 ++NumVisibleDeclContexts;
2906 return Offset;
2907}
2908
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002909/// \brief Write an UPDATE_VISIBLE block for the given context.
2910///
2911/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2912/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002913/// (in C++), for namespaces, and for classes with forward-declared unscoped
2914/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002915void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002916 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2917 if (!Map || Map->empty())
2918 return;
2919
2920 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2921 ASTDeclContextNameLookupTrait Trait(*this);
2922
2923 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002924 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2925 D != DEnd; ++D) {
2926 DeclarationName Name = D->first;
2927 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002928 // For any name that appears in this table, the results are complete, i.e.
2929 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002930 if (Result.first != Result.second)
2931 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002932 }
2933
2934 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002935 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002936 uint32_t BucketOffset;
2937 {
2938 llvm::raw_svector_ostream Out(LookupTable);
2939 // Make sure that no bucket is at offset 0
2940 clang::io::Emit32(Out, 0);
2941 BucketOffset = Generator.Emit(Out, Trait);
2942 }
2943
2944 // Write the lookup table
2945 RecordData Record;
2946 Record.push_back(UPDATE_VISIBLE);
2947 Record.push_back(getDeclID(cast<Decl>(DC)));
2948 Record.push_back(BucketOffset);
2949 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2950}
2951
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002952/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2953void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2954 RecordData Record;
2955 Record.push_back(Opts.fp_contract);
2956 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2957}
2958
2959/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2960void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002961 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002962 return;
2963
2964 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2965 RecordData Record;
2966#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2967#include "clang/Basic/OpenCLExtensions.def"
2968 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2969}
2970
Douglas Gregor2171bf12012-01-15 16:58:34 +00002971void ASTWriter::WriteRedeclarations() {
2972 RecordData LocalRedeclChains;
2973 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2974
2975 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2976 Decl *First = Redeclarations[I];
2977 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2978
2979 Decl *MostRecent = First->getMostRecentDecl();
2980
2981 // If we only have a single declaration, there is no point in storing
2982 // a redeclaration chain.
2983 if (First == MostRecent)
2984 continue;
2985
2986 unsigned Offset = LocalRedeclChains.size();
2987 unsigned Size = 0;
2988 LocalRedeclChains.push_back(0); // Placeholder for the size.
2989
2990 // Collect the set of local redeclarations of this declaration.
2991 for (Decl *Prev = MostRecent; Prev != First;
2992 Prev = Prev->getPreviousDecl()) {
2993 if (!Prev->isFromASTFile()) {
2994 AddDeclRef(Prev, LocalRedeclChains);
2995 ++Size;
2996 }
2997 }
2998 LocalRedeclChains[Offset] = Size;
2999
3000 // Reverse the set of local redeclarations, so that we store them in
3001 // order (since we found them in reverse order).
3002 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3003
3004 // Add the mapping from the first ID to the set of local declarations.
3005 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3006 LocalRedeclsMap.push_back(Info);
3007
3008 assert(N == Redeclarations.size() &&
3009 "Deserialized a declaration we shouldn't have");
3010 }
3011
3012 if (LocalRedeclChains.empty())
3013 return;
3014
3015 // Sort the local redeclarations map by the first declaration ID,
3016 // since the reader will be performing binary searches on this information.
3017 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3018
3019 // Emit the local redeclarations map.
3020 using namespace llvm;
3021 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3022 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3025 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3026
3027 RecordData Record;
3028 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3029 Record.push_back(LocalRedeclsMap.size());
3030 Stream.EmitRecordWithBlob(AbbrevID, Record,
3031 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3032 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3033
3034 // Emit the redeclaration chains.
3035 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3036}
3037
Douglas Gregorcff9f262012-01-27 01:47:08 +00003038void ASTWriter::WriteObjCCategories() {
3039 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3040 RecordData Categories;
3041
3042 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3043 unsigned Size = 0;
3044 unsigned StartIndex = Categories.size();
3045
3046 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3047
3048 // Allocate space for the size.
3049 Categories.push_back(0);
3050
3051 // Add the categories.
3052 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3053 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3054 assert(getDeclID(Cat) != 0 && "Bogus category");
3055 AddDeclRef(Cat, Categories);
3056 }
3057
3058 // Update the size.
3059 Categories[StartIndex] = Size;
3060
3061 // Record this interface -> category map.
3062 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3063 CategoriesMap.push_back(CatInfo);
3064 }
3065
3066 // Sort the categories map by the definition ID, since the reader will be
3067 // performing binary searches on this information.
3068 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3069
3070 // Emit the categories map.
3071 using namespace llvm;
3072 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3073 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3076 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3077
3078 RecordData Record;
3079 Record.push_back(OBJC_CATEGORIES_MAP);
3080 Record.push_back(CategoriesMap.size());
3081 Stream.EmitRecordWithBlob(AbbrevID, Record,
3082 reinterpret_cast<char*>(CategoriesMap.data()),
3083 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3084
3085 // Emit the category lists.
3086 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3087}
3088
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003089void ASTWriter::WriteMergedDecls() {
3090 if (!Chain || Chain->MergedDecls.empty())
3091 return;
3092
3093 RecordData Record;
3094 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3095 IEnd = Chain->MergedDecls.end();
3096 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003097 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003098 : getDeclID(I->first);
3099 assert(CanonID && "Merged declaration not known?");
3100
3101 Record.push_back(CanonID);
3102 Record.push_back(I->second.size());
3103 Record.append(I->second.begin(), I->second.end());
3104 }
3105 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3106}
3107
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003108//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003109// General Serialization Routines
3110//===----------------------------------------------------------------------===//
3111
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003112/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003113void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3114 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003115 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003116 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3117 e = Attrs.end(); i != e; ++i){
3118 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003119 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003120 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003121
Sean Huntcf807c42010-08-18 23:23:40 +00003122#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003123
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003124 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003125}
3126
Chris Lattner5f9e2722011-07-23 10:55:15 +00003127void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003128 Record.push_back(Str.size());
3129 Record.insert(Record.end(), Str.begin(), Str.end());
3130}
3131
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003132void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3133 RecordDataImpl &Record) {
3134 Record.push_back(Version.getMajor());
3135 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3136 Record.push_back(*Minor + 1);
3137 else
3138 Record.push_back(0);
3139 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3140 Record.push_back(*Subminor + 1);
3141 else
3142 Record.push_back(0);
3143}
3144
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003145/// \brief Note that the identifier II occurs at the given offset
3146/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003147void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003148 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003149 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003150 // up earlier in the chain and thus don't need an offset.
3151 if (ID >= FirstIdentID)
3152 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003153}
3154
Douglas Gregor83941df2009-04-25 17:48:32 +00003155/// \brief Note that the selector Sel occurs at the given offset
3156/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003157void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003158 unsigned ID = SelectorIDs[Sel];
3159 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003160 // Don't record offsets for selectors that are also available in a different
3161 // file.
3162 if (ID < FirstSelectorID)
3163 return;
3164 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003165}
3166
Sebastian Redla4232eb2010-08-18 23:56:21 +00003167ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003168 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003169 WritingAST(false), DoneWritingDeclsAndTypes(false),
3170 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003171 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003172 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003173 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003174 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3175 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003176 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003177 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003178 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003179 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003180 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003181 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003182 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3183 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3184 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003185 DeclTypedefAbbrev(0),
3186 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3187 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003188{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003189}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003190
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003191ASTWriter::~ASTWriter() {
3192 for (FileDeclIDsTy::iterator
3193 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3194 delete I->second;
3195}
3196
Sebastian Redla4232eb2010-08-18 23:56:21 +00003197void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003198 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003199 Module *WritingModule, StringRef isysroot,
3200 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003201 WritingAST = true;
3202
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003203 ASTHasCompilerErrors = hasErrors;
3204
Douglas Gregor2cf26342009-04-09 22:27:44 +00003205 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003206 Stream.Emit((unsigned)'C', 8);
3207 Stream.Emit((unsigned)'P', 8);
3208 Stream.Emit((unsigned)'C', 8);
3209 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003210
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003211 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003212
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003213 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003214 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003215 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003216 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003217 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003218 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003219 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003220
3221 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003222}
3223
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003224template<typename Vector>
3225static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3226 ASTWriter::RecordData &Record) {
3227 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3228 I != E; ++I) {
3229 Writer.AddDeclRef(*I, Record);
3230 }
3231}
3232
Sebastian Redla4232eb2010-08-18 23:56:21 +00003233void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003234 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003235 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003236 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003237 using namespace llvm;
3238
Douglas Gregorecc2c092011-12-01 22:20:10 +00003239 // Make sure that the AST reader knows to finalize itself.
3240 if (Chain)
3241 Chain->finalizeForWriting();
3242
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003243 ASTContext &Context = SemaRef.Context;
3244 Preprocessor &PP = SemaRef.PP;
3245
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003246 // Set up predefined declaration IDs.
3247 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003248 if (Context.ObjCIdDecl)
3249 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003250 if (Context.ObjCSelDecl)
3251 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003252 if (Context.ObjCClassDecl)
3253 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003254 if (Context.ObjCProtocolClassDecl)
3255 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003256 if (Context.Int128Decl)
3257 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3258 if (Context.UInt128Decl)
3259 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003260 if (Context.ObjCInstanceTypeDecl)
3261 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003262 if (Context.BuiltinVaListDecl)
3263 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3264
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003265 if (!Chain) {
3266 // Make sure that we emit IdentifierInfos (and any attached
3267 // declarations) for builtins. We don't need to do this when we're
3268 // emitting chained PCH files, because all of the builtins will be
3269 // in the original PCH file.
3270 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003271 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003272 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003273 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003274 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003275 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3276 getIdentifierRef(&Table.get(BuiltinNames[I]));
3277 }
3278
Douglas Gregoreee242f2011-10-27 09:33:13 +00003279 // If there are any out-of-date identifiers, bring them up to date.
3280 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3281 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3282 IDEnd = PP.getIdentifierTable().end();
3283 ID != IDEnd; ++ID)
3284 if (ID->second->isOutOfDate())
3285 ExtSource->updateOutOfDateIdentifier(*ID->second);
3286 }
3287
Chris Lattner63d65f82009-09-08 18:19:27 +00003288 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003289 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003290 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003291 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003292 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003293
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003294 // Build a record containing all of the file scoped decls in this file.
3295 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003296 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3297 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003298
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003299 // Build a record containing all of the delegating constructors we still need
3300 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003301 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003302 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003303
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003304 // Write the set of weak, undeclared identifiers. We always write the
3305 // entire table, since later PCH files in a PCH chain are only interested in
3306 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003307 RecordData WeakUndeclaredIdentifiers;
3308 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003309 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003310 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3311 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3312 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3313 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3314 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3315 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3316 }
3317 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003318
Douglas Gregor14c22f22009-04-22 22:18:58 +00003319 // Build a record containing all of the locally-scoped external
3320 // declarations in this header file. Generally, this record will be
3321 // empty.
3322 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003323 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003324 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003325 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003326 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3327 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003328 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003329 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003330 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3331 }
3332
Douglas Gregorb81c1702009-04-27 20:06:05 +00003333 // Build a record containing all of the ext_vector declarations.
3334 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003335 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003336
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003337 // Build a record containing all of the VTable uses information.
3338 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003339 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003340 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3341 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3342 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3343 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3344 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003345 }
3346
3347 // Build a record containing all of dynamic classes declarations.
3348 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003349 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003350
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003351 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003352 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003353 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003354 I = SemaRef.PendingInstantiations.begin(),
3355 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3356 AddDeclRef(I->first, PendingInstantiations);
3357 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003358 }
3359 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3360 "There are local ones at end of translation unit!");
3361
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003362 // Build a record containing some declaration references.
3363 RecordData SemaDeclRefs;
3364 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3365 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3366 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3367 }
3368
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003369 RecordData CUDASpecialDeclRefs;
3370 if (Context.getcudaConfigureCallDecl()) {
3371 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3372 }
3373
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003374 // Build a record containing all of the known namespaces.
3375 RecordData KnownNamespaces;
3376 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3377 I = SemaRef.KnownNamespaces.begin(),
3378 IEnd = SemaRef.KnownNamespaces.end();
3379 I != IEnd; ++I) {
3380 if (!I->second)
3381 AddDeclRef(I->first, KnownNamespaces);
3382 }
3383
Sebastian Redl3397c552010-08-18 23:56:27 +00003384 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003385 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003386 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003387 WriteMetadata(Context, isysroot, OutputFile);
David Blaikie4e4d0842012-03-11 07:00:24 +00003388 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregor832d6202011-07-22 16:35:34 +00003389 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003390 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003391
3392 // Create a lexical update block containing all of the declarations in the
3393 // translation unit that do not come from other AST files.
3394 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3395 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3396 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3397 E = TU->noload_decls_end();
3398 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003399 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003400 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003401 }
3402
3403 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3404 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3405 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3406 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3407 Record.clear();
3408 Record.push_back(TU_UPDATE_LEXICAL);
3409 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3410 data(NewGlobalDecls));
3411
3412 // And a visible updates block for the translation unit.
3413 Abv = new llvm::BitCodeAbbrev();
3414 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3415 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3416 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3417 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3418 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3419 WriteDeclContextVisibleUpdate(TU);
3420
3421 // If the translation unit has an anonymous namespace, and we don't already
3422 // have an update block for it, write it as an update block.
3423 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3424 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3425 if (Record.empty()) {
3426 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003427 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003428 }
3429 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003430
3431 // Make sure visible decls, added to DeclContexts previously loaded from
3432 // an AST file, are registered for serialization.
3433 for (SmallVector<const Decl *, 16>::iterator
3434 I = UpdatingVisibleDecls.begin(),
3435 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3436 GetDeclRef(*I);
3437 }
3438
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003439 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003440 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003441
Douglas Gregora119da02011-08-02 16:26:37 +00003442 // Form the record of special types.
3443 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003444 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003445 AddTypeRef(Context.getFILEType(), SpecialTypes);
3446 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3447 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3448 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3449 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003450 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003451 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003452
Douglas Gregor366809a2009-04-26 03:49:13 +00003453 // Keep writing types and declarations until all types and
3454 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003455 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003456 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003457 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3458 E = DeclsToRewrite.end();
3459 I != E; ++I)
3460 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003461 while (!DeclTypesToEmit.empty()) {
3462 DeclOrType DOT = DeclTypesToEmit.front();
3463 DeclTypesToEmit.pop();
3464 if (DOT.isType())
3465 WriteType(DOT.getType());
3466 else
3467 WriteDecl(Context, DOT.getDecl());
3468 }
3469 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003470
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003471 DoneWritingDeclsAndTypes = true;
3472
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003473 WriteFileDeclIDsMap();
3474 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003475 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003476
3477 if (Chain) {
3478 // Write the mapping information describing our module dependencies and how
3479 // each of those modules were mapped into our own offset/ID space, so that
3480 // the reader can build the appropriate mapping to its own offset/ID space.
3481 // The map consists solely of a blob with the following format:
3482 // *(module-name-len:i16 module-name:len*i8
3483 // source-location-offset:i32
3484 // identifier-id:i32
3485 // preprocessed-entity-id:i32
3486 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003487 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003488 // selector-id:i32
3489 // declaration-id:i32
3490 // c++-base-specifiers-id:i32
3491 // type-id:i32)
3492 //
3493 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3494 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3496 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003497 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003498 {
3499 llvm::raw_svector_ostream Out(Buffer);
3500 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003501 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003502 M != MEnd; ++M) {
3503 StringRef FileName = (*M)->FileName;
3504 io::Emit16(Out, FileName.size());
3505 Out.write(FileName.data(), FileName.size());
3506 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3507 io::Emit32(Out, (*M)->BaseIdentifierID);
3508 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003509 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003510 io::Emit32(Out, (*M)->BaseSelectorID);
3511 io::Emit32(Out, (*M)->BaseDeclID);
3512 io::Emit32(Out, (*M)->BaseTypeIndex);
3513 }
3514 }
3515 Record.clear();
3516 Record.push_back(MODULE_OFFSET_MAP);
3517 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3518 Buffer.data(), Buffer.size());
3519 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003520 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003521 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003522 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003523 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003524 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003525 WriteFPPragmaOptions(SemaRef.getFPOptions());
3526 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003527
Sebastian Redl1476ed42010-07-16 16:36:56 +00003528 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003529 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003530
Anders Carlssonc8505782011-03-06 18:41:18 +00003531 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003532
Douglas Gregore209e502011-12-06 01:10:29 +00003533 // If we're emitting a module, write out the submodule information.
3534 if (WritingModule)
3535 WriteSubmodules(WritingModule);
3536
Douglas Gregora119da02011-08-02 16:26:37 +00003537 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3538
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003539 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003540 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003541 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003542
3543 // Write the record containing tentative definitions.
3544 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003545 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003546
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003547 // Write the record containing unused file scoped decls.
3548 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003549 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003550
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003551 // Write the record containing weak undeclared identifiers.
3552 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003553 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003554 WeakUndeclaredIdentifiers);
3555
Douglas Gregor14c22f22009-04-22 22:18:58 +00003556 // Write the record containing locally-scoped external definitions.
3557 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003558 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003559 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003560
3561 // Write the record containing ext_vector type names.
3562 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003563 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003564
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003565 // Write the record containing VTable uses information.
3566 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003567 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003568
3569 // Write the record containing dynamic classes declarations.
3570 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003571 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003572
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003573 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003574 if (!PendingInstantiations.empty())
3575 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003576
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003577 // Write the record containing declaration references of Sema.
3578 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003579 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003580
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003581 // Write the record containing CUDA-specific declaration references.
3582 if (!CUDASpecialDeclRefs.empty())
3583 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003584
3585 // Write the delegating constructors.
3586 if (!DelegatingCtorDecls.empty())
3587 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003588
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003589 // Write the known namespaces.
3590 if (!KnownNamespaces.empty())
3591 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3592
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003593 // Write the visible updates to DeclContexts.
3594 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3595 I = UpdatedDeclContexts.begin(),
3596 E = UpdatedDeclContexts.end();
3597 I != E; ++I)
3598 WriteDeclContextVisibleUpdate(*I);
3599
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003600 if (!WritingModule) {
3601 // Write the submodules that were imported, if any.
3602 RecordData ImportedModules;
3603 for (ASTContext::import_iterator I = Context.local_import_begin(),
3604 IEnd = Context.local_import_end();
3605 I != IEnd; ++I) {
3606 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3607 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3608 }
3609 if (!ImportedModules.empty()) {
3610 // Sort module IDs.
3611 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3612
3613 // Unique module IDs.
3614 ImportedModules.erase(std::unique(ImportedModules.begin(),
3615 ImportedModules.end()),
3616 ImportedModules.end());
3617
3618 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3619 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003620 }
3621
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003622 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003623 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003624 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003625 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003626 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003627
Douglas Gregor3e1af842009-04-17 22:13:46 +00003628 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003629 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003630 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003631 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003632 Record.push_back(NumLexicalDeclContexts);
3633 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003634 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003635 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003636}
3637
Douglas Gregor61c5e342011-09-17 00:05:03 +00003638/// \brief Go through the declaration update blocks and resolve declaration
3639/// pointers into declaration IDs.
3640void ASTWriter::ResolveDeclUpdatesBlocks() {
3641 for (DeclUpdateMap::iterator
3642 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3643 const Decl *D = I->first;
3644 UpdateRecord &URec = I->second;
3645
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003646 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003647 continue; // The decl will be written completely
3648
3649 unsigned Idx = 0, N = URec.size();
3650 while (Idx < N) {
3651 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003652 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3653 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3654 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3655 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3656 ++Idx;
3657 break;
3658
3659 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3660 ++Idx;
3661 break;
3662 }
3663 }
3664 }
3665}
3666
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003667void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003668 if (DeclUpdates.empty())
3669 return;
3670
3671 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003672 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003673 for (DeclUpdateMap::iterator
3674 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3675 const Decl *D = I->first;
3676 UpdateRecord &URec = I->second;
3677
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003678 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003679 continue; // The decl will be written completely,no need to store updates.
3680
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003681 uint64_t Offset = Stream.GetCurrentBitNo();
3682 Stream.EmitRecord(DECL_UPDATES, URec);
3683
3684 OffsetsRecord.push_back(GetDeclRef(D));
3685 OffsetsRecord.push_back(Offset);
3686 }
3687 Stream.ExitBlock();
3688 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3689}
3690
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003691void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003692 if (ReplacedDecls.empty())
3693 return;
3694
3695 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003696 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003697 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003698 Record.push_back(I->ID);
3699 Record.push_back(I->Offset);
3700 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003701 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003702 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003703}
3704
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003705void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003706 Record.push_back(Loc.getRawEncoding());
3707}
3708
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003709void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003710 AddSourceLocation(Range.getBegin(), Record);
3711 AddSourceLocation(Range.getEnd(), Record);
3712}
3713
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003714void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003715 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003716 const uint64_t *Words = Value.getRawData();
3717 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003718}
3719
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003720void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003721 Record.push_back(Value.isUnsigned());
3722 AddAPInt(Value, Record);
3723}
3724
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003725void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003726 AddAPInt(Value.bitcastToAPInt(), Record);
3727}
3728
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003729void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003730 Record.push_back(getIdentifierRef(II));
3731}
3732
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003733IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003734 if (II == 0)
3735 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003737 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003738 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003739 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003740 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003741}
3742
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003743void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003744 Record.push_back(getSelectorRef(SelRef));
3745}
3746
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003747SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003748 if (Sel.getAsOpaquePtr() == 0) {
3749 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003750 }
3751
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003752 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003753 if (SID == 0 && Chain) {
3754 // This might trigger a ReadSelector callback, which will set the ID for
3755 // this selector.
3756 Chain->LoadSelector(Sel);
3757 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003758 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003759 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003760 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003761 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003762}
3763
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003764void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003765 AddDeclRef(Temp->getDestructor(), Record);
3766}
3767
Douglas Gregor7c789c12010-10-29 22:39:52 +00003768void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3769 CXXBaseSpecifier const *BasesEnd,
3770 RecordDataImpl &Record) {
3771 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3772 CXXBaseSpecifiersToWrite.push_back(
3773 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3774 Bases, BasesEnd));
3775 Record.push_back(NextCXXBaseSpecifiersID++);
3776}
3777
Sebastian Redla4232eb2010-08-18 23:56:21 +00003778void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003779 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003780 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003781 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003782 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003783 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003784 break;
3785 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003786 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003787 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003788 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003789 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003790 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003791 break;
3792 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003793 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003794 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003795 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003796 break;
John McCall833ca992009-10-29 08:12:44 +00003797 case TemplateArgument::Null:
3798 case TemplateArgument::Integral:
3799 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003800 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003801 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003802 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003803 break;
3804 }
3805}
3806
Sebastian Redla4232eb2010-08-18 23:56:21 +00003807void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003808 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003809 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003810
3811 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3812 bool InfoHasSameExpr
3813 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3814 Record.push_back(InfoHasSameExpr);
3815 if (InfoHasSameExpr)
3816 return; // Avoid storing the same expr twice.
3817 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003818 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3819 Record);
3820}
3821
Douglas Gregordc355712011-02-25 00:36:19 +00003822void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3823 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003824 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003825 AddTypeRef(QualType(), Record);
3826 return;
3827 }
3828
Douglas Gregordc355712011-02-25 00:36:19 +00003829 AddTypeLoc(TInfo->getTypeLoc(), Record);
3830}
3831
3832void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3833 AddTypeRef(TL.getType(), Record);
3834
John McCalla1ee0c52009-10-16 21:56:05 +00003835 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003836 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003837 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003838}
3839
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003840void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003841 Record.push_back(GetOrCreateTypeID(T));
3842}
3843
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003844TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3845 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003846 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3847}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003848
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003849TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003850 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003851 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003852}
3853
3854TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3855 if (T.isNull())
3856 return TypeIdx();
3857 assert(!T.getLocalFastQualifiers());
3858
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003859 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003860 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003861 if (DoneWritingDeclsAndTypes) {
3862 assert(0 && "New type seen after serializing all the types to emit!");
3863 return TypeIdx();
3864 }
3865
Douglas Gregor366809a2009-04-26 03:49:13 +00003866 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003867 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003868 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003869 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003870 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003871 return Idx;
3872}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003873
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003874TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003875 if (T.isNull())
3876 return TypeIdx();
3877 assert(!T.getLocalFastQualifiers());
3878
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003879 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3880 assert(I != TypeIdxs.end() && "Type not emitted!");
3881 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003882}
3883
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003884void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003885 Record.push_back(GetDeclRef(D));
3886}
3887
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003888DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003889 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3890
Douglas Gregor2cf26342009-04-09 22:27:44 +00003891 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003892 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003893 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003894
3895 // If D comes from an AST file, its declaration ID is already known and
3896 // fixed.
3897 if (D->isFromASTFile())
3898 return D->getGlobalID();
3899
Douglas Gregor97475832010-10-05 18:37:06 +00003900 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003901 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003902 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003903 if (DoneWritingDeclsAndTypes) {
3904 assert(0 && "New decl seen after serializing all the decls to emit!");
3905 return 0;
3906 }
3907
Douglas Gregor2cf26342009-04-09 22:27:44 +00003908 // We haven't seen this declaration before. Give it a new ID and
3909 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003910 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003911 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003912 }
3913
Sebastian Redl681d7232010-07-27 00:17:23 +00003914 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003915}
3916
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003917DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003918 if (D == 0)
3919 return 0;
3920
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003921 // If D comes from an AST file, its declaration ID is already known and
3922 // fixed.
3923 if (D->isFromASTFile())
3924 return D->getGlobalID();
3925
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003926 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3927 return DeclIDs[D];
3928}
3929
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003930static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3931 std::pair<unsigned, serialization::DeclID> R) {
3932 return L.first < R.first;
3933}
3934
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003935void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003936 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003937 assert(D);
3938
3939 SourceLocation Loc = D->getLocation();
3940 if (Loc.isInvalid())
3941 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003942
3943 // We only keep track of the file-level declarations of each file.
3944 if (!D->getLexicalDeclContext()->isFileContext())
3945 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00003946 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3947 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00003948 if (isa<ParmVarDecl>(D))
3949 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003950
3951 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00003952 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003953 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003954 FileID FID;
3955 unsigned Offset;
3956 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003957 if (FID.isInvalid())
3958 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00003959 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003960
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00003961 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003962 if (!Info)
3963 Info = new DeclIDInFileInfo();
3964
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003965 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003966 LocDeclIDsTy &Decls = Info->DeclIDs;
3967
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00003968 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003969 Decls.push_back(LocDecl);
3970 return;
3971 }
3972
3973 LocDeclIDsTy::iterator
3974 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3975
3976 Decls.insert(I, LocDecl);
3977}
3978
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003979void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003980 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003981 Record.push_back(Name.getNameKind());
3982 switch (Name.getNameKind()) {
3983 case DeclarationName::Identifier:
3984 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3985 break;
3986
3987 case DeclarationName::ObjCZeroArgSelector:
3988 case DeclarationName::ObjCOneArgSelector:
3989 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003990 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003991 break;
3992
3993 case DeclarationName::CXXConstructorName:
3994 case DeclarationName::CXXDestructorName:
3995 case DeclarationName::CXXConversionFunctionName:
3996 AddTypeRef(Name.getCXXNameType(), Record);
3997 break;
3998
3999 case DeclarationName::CXXOperatorName:
4000 Record.push_back(Name.getCXXOverloadedOperator());
4001 break;
4002
Sean Hunt3e518bd2009-11-29 07:34:05 +00004003 case DeclarationName::CXXLiteralOperatorName:
4004 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4005 break;
4006
Douglas Gregor2cf26342009-04-09 22:27:44 +00004007 case DeclarationName::CXXUsingDirective:
4008 // No extra data to emit
4009 break;
4010 }
4011}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004012
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004013void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004014 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004015 switch (Name.getNameKind()) {
4016 case DeclarationName::CXXConstructorName:
4017 case DeclarationName::CXXDestructorName:
4018 case DeclarationName::CXXConversionFunctionName:
4019 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4020 break;
4021
4022 case DeclarationName::CXXOperatorName:
4023 AddSourceLocation(
4024 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4025 Record);
4026 AddSourceLocation(
4027 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4028 Record);
4029 break;
4030
4031 case DeclarationName::CXXLiteralOperatorName:
4032 AddSourceLocation(
4033 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4034 Record);
4035 break;
4036
4037 case DeclarationName::Identifier:
4038 case DeclarationName::ObjCZeroArgSelector:
4039 case DeclarationName::ObjCOneArgSelector:
4040 case DeclarationName::ObjCMultiArgSelector:
4041 case DeclarationName::CXXUsingDirective:
4042 break;
4043 }
4044}
4045
4046void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004047 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004048 AddDeclarationName(NameInfo.getName(), Record);
4049 AddSourceLocation(NameInfo.getLoc(), Record);
4050 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4051}
4052
4053void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004054 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004055 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004056 Record.push_back(Info.NumTemplParamLists);
4057 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4058 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4059}
4060
Sebastian Redla4232eb2010-08-18 23:56:21 +00004061void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004062 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004063 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004064 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004065 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004066
4067 // Push each of the NNS's onto a stack for serialization in reverse order.
4068 while (NNS) {
4069 NestedNames.push_back(NNS);
4070 NNS = NNS->getPrefix();
4071 }
4072
4073 Record.push_back(NestedNames.size());
4074 while(!NestedNames.empty()) {
4075 NNS = NestedNames.pop_back_val();
4076 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4077 Record.push_back(Kind);
4078 switch (Kind) {
4079 case NestedNameSpecifier::Identifier:
4080 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4081 break;
4082
4083 case NestedNameSpecifier::Namespace:
4084 AddDeclRef(NNS->getAsNamespace(), Record);
4085 break;
4086
Douglas Gregor14aba762011-02-24 02:36:08 +00004087 case NestedNameSpecifier::NamespaceAlias:
4088 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4089 break;
4090
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004091 case NestedNameSpecifier::TypeSpec:
4092 case NestedNameSpecifier::TypeSpecWithTemplate:
4093 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4094 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4095 break;
4096
4097 case NestedNameSpecifier::Global:
4098 // Don't need to write an associated value.
4099 break;
4100 }
4101 }
4102}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004103
Douglas Gregordc355712011-02-25 00:36:19 +00004104void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4105 RecordDataImpl &Record) {
4106 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004107 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004108 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004109
4110 // Push each of the nested-name-specifiers's onto a stack for
4111 // serialization in reverse order.
4112 while (NNS) {
4113 NestedNames.push_back(NNS);
4114 NNS = NNS.getPrefix();
4115 }
4116
4117 Record.push_back(NestedNames.size());
4118 while(!NestedNames.empty()) {
4119 NNS = NestedNames.pop_back_val();
4120 NestedNameSpecifier::SpecifierKind Kind
4121 = NNS.getNestedNameSpecifier()->getKind();
4122 Record.push_back(Kind);
4123 switch (Kind) {
4124 case NestedNameSpecifier::Identifier:
4125 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4126 AddSourceRange(NNS.getLocalSourceRange(), Record);
4127 break;
4128
4129 case NestedNameSpecifier::Namespace:
4130 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4131 AddSourceRange(NNS.getLocalSourceRange(), Record);
4132 break;
4133
4134 case NestedNameSpecifier::NamespaceAlias:
4135 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4136 AddSourceRange(NNS.getLocalSourceRange(), Record);
4137 break;
4138
4139 case NestedNameSpecifier::TypeSpec:
4140 case NestedNameSpecifier::TypeSpecWithTemplate:
4141 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4142 AddTypeLoc(NNS.getTypeLoc(), Record);
4143 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4144 break;
4145
4146 case NestedNameSpecifier::Global:
4147 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4148 break;
4149 }
4150 }
4151}
4152
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004153void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004154 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004155 Record.push_back(Kind);
4156 switch (Kind) {
4157 case TemplateName::Template:
4158 AddDeclRef(Name.getAsTemplateDecl(), Record);
4159 break;
4160
4161 case TemplateName::OverloadedTemplate: {
4162 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4163 Record.push_back(OvT->size());
4164 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4165 I != E; ++I)
4166 AddDeclRef(*I, Record);
4167 break;
4168 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004169
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004170 case TemplateName::QualifiedTemplate: {
4171 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4172 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4173 Record.push_back(QualT->hasTemplateKeyword());
4174 AddDeclRef(QualT->getTemplateDecl(), Record);
4175 break;
4176 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004177
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004178 case TemplateName::DependentTemplate: {
4179 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4180 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4181 Record.push_back(DepT->isIdentifier());
4182 if (DepT->isIdentifier())
4183 AddIdentifierRef(DepT->getIdentifier(), Record);
4184 else
4185 Record.push_back(DepT->getOperator());
4186 break;
4187 }
John McCall14606042011-06-30 08:33:18 +00004188
4189 case TemplateName::SubstTemplateTemplateParm: {
4190 SubstTemplateTemplateParmStorage *subst
4191 = Name.getAsSubstTemplateTemplateParm();
4192 AddDeclRef(subst->getParameter(), Record);
4193 AddTemplateName(subst->getReplacement(), Record);
4194 break;
4195 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004196
4197 case TemplateName::SubstTemplateTemplateParmPack: {
4198 SubstTemplateTemplateParmPackStorage *SubstPack
4199 = Name.getAsSubstTemplateTemplateParmPack();
4200 AddDeclRef(SubstPack->getParameterPack(), Record);
4201 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4202 break;
4203 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004204 }
4205}
4206
Michael J. Spencer20249a12010-10-21 03:16:25 +00004207void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004208 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004209 Record.push_back(Arg.getKind());
4210 switch (Arg.getKind()) {
4211 case TemplateArgument::Null:
4212 break;
4213 case TemplateArgument::Type:
4214 AddTypeRef(Arg.getAsType(), Record);
4215 break;
4216 case TemplateArgument::Declaration:
4217 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004218 Record.push_back(Arg.isDeclForReferenceParam());
4219 break;
4220 case TemplateArgument::NullPtr:
4221 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004222 break;
4223 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004224 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004225 AddTypeRef(Arg.getIntegralType(), Record);
4226 break;
4227 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004228 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4229 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004230 case TemplateArgument::TemplateExpansion:
4231 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004232 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4233 Record.push_back(*NumExpansions + 1);
4234 else
4235 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004236 break;
4237 case TemplateArgument::Expression:
4238 AddStmt(Arg.getAsExpr());
4239 break;
4240 case TemplateArgument::Pack:
4241 Record.push_back(Arg.pack_size());
4242 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4243 I != E; ++I)
4244 AddTemplateArgument(*I, Record);
4245 break;
4246 }
4247}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004248
4249void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004250ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004251 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004252 assert(TemplateParams && "No TemplateParams!");
4253 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4254 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4255 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4256 Record.push_back(TemplateParams->size());
4257 for (TemplateParameterList::const_iterator
4258 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4259 P != PEnd; ++P)
4260 AddDeclRef(*P, Record);
4261}
4262
4263/// \brief Emit a template argument list.
4264void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004265ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004266 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004267 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004268 Record.push_back(TemplateArgs->size());
4269 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004270 AddTemplateArgument(TemplateArgs->get(i), Record);
4271}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004272
4273
4274void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004275ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004276 Record.push_back(Set.size());
4277 for (UnresolvedSetImpl::const_iterator
4278 I = Set.begin(), E = Set.end(); I != E; ++I) {
4279 AddDeclRef(I.getDecl(), Record);
4280 Record.push_back(I.getAccess());
4281 }
4282}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004283
Sebastian Redla4232eb2010-08-18 23:56:21 +00004284void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004285 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004286 Record.push_back(Base.isVirtual());
4287 Record.push_back(Base.isBaseOfClass());
4288 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004289 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004290 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004291 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004292 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4293 : SourceLocation(),
4294 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004295}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004296
Douglas Gregor7c789c12010-10-29 22:39:52 +00004297void ASTWriter::FlushCXXBaseSpecifiers() {
4298 RecordData Record;
4299 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4300 Record.clear();
4301
4302 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004303 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004304 if (Index == CXXBaseSpecifiersOffsets.size())
4305 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4306 else {
4307 if (Index > CXXBaseSpecifiersOffsets.size())
4308 CXXBaseSpecifiersOffsets.resize(Index + 1);
4309 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4310 }
4311
4312 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4313 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4314 Record.push_back(BEnd - B);
4315 for (; B != BEnd; ++B)
4316 AddCXXBaseSpecifier(*B, Record);
4317 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004318
4319 // Flush any expressions that were written as part of the base specifiers.
4320 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004321 }
4322
4323 CXXBaseSpecifiersToWrite.clear();
4324}
4325
Sean Huntcbb67482011-01-08 20:30:50 +00004326void ASTWriter::AddCXXCtorInitializers(
4327 const CXXCtorInitializer * const *CtorInitializers,
4328 unsigned NumCtorInitializers,
4329 RecordDataImpl &Record) {
4330 Record.push_back(NumCtorInitializers);
4331 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4332 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004333
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004334 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004335 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004336 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004337 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004338 } else if (Init->isDelegatingInitializer()) {
4339 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004340 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004341 } else if (Init->isMemberInitializer()){
4342 Record.push_back(CTOR_INITIALIZER_MEMBER);
4343 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004344 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004345 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4346 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004347 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004348
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004349 AddSourceLocation(Init->getMemberLocation(), Record);
4350 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004351 AddSourceLocation(Init->getLParenLoc(), Record);
4352 AddSourceLocation(Init->getRParenLoc(), Record);
4353 Record.push_back(Init->isWritten());
4354 if (Init->isWritten()) {
4355 Record.push_back(Init->getSourceOrder());
4356 } else {
4357 Record.push_back(Init->getNumArrayIndices());
4358 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4359 AddDeclRef(Init->getArrayIndex(i), Record);
4360 }
4361 }
4362}
4363
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004364void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4365 assert(D->DefinitionData);
4366 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004367 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004368 Record.push_back(Data.UserDeclaredConstructor);
4369 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004370 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004371 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004372 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004373 Record.push_back(Data.UserDeclaredDestructor);
4374 Record.push_back(Data.Aggregate);
4375 Record.push_back(Data.PlainOldData);
4376 Record.push_back(Data.Empty);
4377 Record.push_back(Data.Polymorphic);
4378 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004379 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004380 Record.push_back(Data.HasNoNonEmptyBases);
4381 Record.push_back(Data.HasPrivateFields);
4382 Record.push_back(Data.HasProtectedFields);
4383 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004384 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004385 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004386 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004387 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004388 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004389 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004390 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004391 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004392 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004393 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004394 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004395 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004396 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004397 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004398 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004399 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004400 Record.push_back(Data.DeclaredDefaultConstructor);
4401 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004402 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004403 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004404 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004405 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004406 Record.push_back(Data.FailedImplicitMoveConstructor);
4407 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004408 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004409
4410 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004411 if (Data.NumBases > 0)
4412 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4413 Record);
4414
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004415 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4416 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004417 if (Data.NumVBases > 0)
4418 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4419 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004420
4421 AddUnresolvedSet(Data.Conversions, Record);
4422 AddUnresolvedSet(Data.VisibleConversions, Record);
4423 // Data.Definition is the owning decl, no need to write it.
4424 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004425
4426 // Add lambda-specific data.
4427 if (Data.IsLambda) {
4428 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004429 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004430 Record.push_back(Lambda.NumCaptures);
4431 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004432 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004433 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004434 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004435 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4436 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4437 AddSourceLocation(Capture.getLocation(), Record);
4438 Record.push_back(Capture.isImplicit());
4439 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4440 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4441 AddDeclRef(Var, Record);
4442 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4443 : SourceLocation(),
4444 Record);
4445 }
4446 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004447}
4448
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004449void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004450 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004451 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004452 assert(FirstDeclID == NextDeclID &&
4453 FirstTypeID == NextTypeID &&
4454 FirstIdentID == NextIdentID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004455 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004456 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004457 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004458
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004459 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004460
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004461 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4462 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4463 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor26ced122011-12-01 00:59:36 +00004464 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004465 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004466 NextDeclID = FirstDeclID;
4467 NextTypeID = FirstTypeID;
4468 NextIdentID = FirstIdentID;
4469 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004470 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004471}
4472
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004473void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004474 IdentifierIDs[II] = ID;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00004475 if (II->hadMacroDefinition())
Douglas Gregor040a8042011-02-11 00:26:14 +00004476 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004477}
4478
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004479void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004480 // Always take the highest-numbered type index. This copes with an interesting
4481 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004482 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004483 // keep the higher-numbered entry so that we can properly write it out to
4484 // the AST file.
4485 TypeIdx &StoredIdx = TypeIdxs[T];
4486 if (Idx.getIndex() >= StoredIdx.getIndex())
4487 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004488}
4489
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004490void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004491 SelectorIDs[S] = ID;
4492}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004493
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004494void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004495 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004496 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004497 MacroDefinitions[MD] = ID;
4498}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004499
Douglas Gregor1d4c1132011-12-20 22:06:13 +00004500void ASTWriter::MacroVisible(IdentifierInfo *II) {
4501 DeserializedMacroNames.push_back(II);
4502}
4503
Douglas Gregora015cab2011-12-02 17:30:13 +00004504void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4505 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4506 SubmoduleIDs[Mod] = ID;
4507}
4508
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004509void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004510 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004511 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004512 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4513 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004514 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004515 // A forward reference was mutated into a definition. Rewrite it.
4516 // FIXME: This happens during template instantiation, should we
4517 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004518 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004519 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004520 }
4521}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004522void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004523 assert(!WritingAST && "Already writing the AST!");
4524
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004525 // TU and namespaces are handled elsewhere.
4526 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4527 return;
4528
Douglas Gregor919814d2011-09-09 23:01:35 +00004529 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004530 return; // Not a source decl added to a DeclContext from PCH.
4531
4532 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004533 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004534}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004535
4536void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004537 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004538 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004539 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004540 return; // Not a source member added to a class from PCH.
4541 if (!isa<CXXMethodDecl>(D))
4542 return; // We are interested in lazily declared implicit methods.
4543
4544 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004545 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004546 UpdateRecord &Record = DeclUpdates[RD];
4547 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004548 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004549}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004550
4551void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4552 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004553 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004554 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004555 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004556 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004557 return; // Not a source specialization added to a template from PCH.
4558
4559 UpdateRecord &Record = DeclUpdates[TD];
4560 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004561 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004562}
Douglas Gregor89d99802010-11-30 06:16:57 +00004563
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004564void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4565 const FunctionDecl *D) {
4566 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004567 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004568 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004569 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004570 return; // Not a source specialization added to a template from PCH.
4571
4572 UpdateRecord &Record = DeclUpdates[TD];
4573 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004574 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004575}
4576
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004577void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004578 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004579 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004580 return; // Declaration not imported from PCH.
4581
4582 // Implicit decl from a PCH was defined.
4583 // FIXME: Should implicit definition be a separate FunctionDecl?
4584 RewriteDecl(D);
4585}
4586
Sebastian Redlf79a7192011-04-29 08:19:30 +00004587void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004588 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004589 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004590 return;
4591
4592 // Since the actual instantiation is delayed, this really means that we need
4593 // to update the instantiation location.
4594 UpdateRecord &Record = DeclUpdates[D];
4595 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4596 AddSourceLocation(
4597 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4598}
4599
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004600void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4601 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004602 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004603 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004604 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004605
4606 assert(IFD->getDefinition() && "Category on a class without a definition?");
4607 ObjCClassesWithCategories.insert(
4608 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004609}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004610
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004611
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004612void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4613 const ObjCPropertyDecl *OrigProp,
4614 const ObjCCategoryDecl *ClassExt) {
4615 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4616 if (!D)
4617 return;
4618
4619 assert(!WritingAST && "Already writing the AST!");
4620 if (!D->isFromASTFile())
4621 return; // Declaration not imported from PCH.
4622
4623 RewriteDecl(D);
4624}