blob: b9061ad75620c0df7ee31197fd1f4793139825ae [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 Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
Sebastian Redlade50002010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000063}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000069}
70
Douglas Gregor2cf26342009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000074
Douglas Gregor2cf26342009-04-09 22:27:44 +000075namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Reid Kleckner12df2462013-06-24 17:51:48 +0000111void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
112 Writer.AddTypeRef(T->getOriginalType(), Record);
113 Code = TYPE_DECAYED;
114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000118 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000119}
120
Sebastian Redl3397c552010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000122 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
123 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000124 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000128 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000129 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130}
131
Sebastian Redl3397c552010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 Writer.AddTypeRef(T->getPointeeType(), Record);
134 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000135 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136}
137
Sebastian Redl3397c552010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139 Writer.AddTypeRef(T->getElementType(), Record);
140 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000141 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142}
143
Sebastian Redl3397c552010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145 VisitArrayType(T);
146 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000157 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
158 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000159 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000160 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161}
162
Sebastian Redl3397c552010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164 Writer.AddTypeRef(T->getElementType(), Record);
165 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000166 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000167 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000172 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173}
174
Sebastian Redl3397c552010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 FunctionType::ExtInfo C = T->getExtInfo();
178 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000179 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000180 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000181 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000182 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000183 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000188 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000189}
190
Sebastian Redl3397c552010-08-18 23:56:27 +0000191void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192 VisitFunctionType(T);
193 Record.push_back(T->getNumArgs());
194 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
195 Writer.AddTypeRef(T->getArgType(I), Record);
196 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000197 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000199 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000200 Record.push_back(T->getExceptionSpecType());
201 if (T->getExceptionSpecType() == EST_Dynamic) {
202 Record.push_back(T->getNumExceptions());
203 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
204 Writer.AddTypeRef(T->getExceptionType(I), Record);
205 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
206 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000207 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
208 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
209 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000210 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
211 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000212 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214}
215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000218 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000219}
John McCalled976492009-12-04 22:46:56 +0000220
Sebastian Redl3397c552010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000223 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
224 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000229 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000234 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236}
237
Sebastian Redl3397c552010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000239 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000240 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000241 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000242}
243
Sean Huntca63c202011-05-24 22:41:36 +0000244void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
245 Writer.AddTypeRef(T->getBaseType(), Record);
246 Writer.AddTypeRef(T->getUnderlyingType(), Record);
247 Record.push_back(T->getUTTKind());
248 Code = TYPE_UNARY_TRANSFORM;
249}
250
Richard Smith34b41d92011-02-20 03:19:35 +0000251void ASTTypeWriter::VisitAutoType(const AutoType *T) {
252 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smitha2c36462013-04-26 16:15:35 +0000253 Record.push_back(T->isDecltypeAuto());
Richard Smithdc7a4f52013-04-30 13:56:41 +0000254 if (T->getDeducedType().isNull())
255 Record.push_back(T->isDependentType());
Richard Smith34b41d92011-02-20 03:19:35 +0000256 Code = TYPE_AUTO;
257}
258
Sebastian Redl3397c552010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000260 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000261 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000262 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263 "Cannot serialize in the middle of a type definition");
264}
265
Sebastian Redl3397c552010-08-18 23:56:27 +0000266void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000268 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000269}
270
Sebastian Redl3397c552010-08-18 23:56:27 +0000271void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000272 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000273 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000274}
275
John McCall9d156a72011-01-06 01:58:22 +0000276void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
277 Writer.AddTypeRef(T->getModifiedType(), Record);
278 Writer.AddTypeRef(T->getEquivalentType(), Record);
279 Record.push_back(T->getAttrKind());
280 Code = TYPE_ATTRIBUTED;
281}
282
Mike Stump1eb44332009-09-09 15:08:12 +0000283void
Sebastian Redl3397c552010-08-18 23:56:27 +0000284ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000285 const SubstTemplateTypeParmType *T) {
286 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
287 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000288 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000289}
290
291void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000292ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
293 const SubstTemplateTypeParmPackType *T) {
294 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
295 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
296 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
297}
298
299void
Sebastian Redl3397c552010-08-18 23:56:27 +0000300ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000301 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000302 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303 Writer.AddTemplateName(T->getTemplateName(), Record);
304 Record.push_back(T->getNumArgs());
305 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
306 ArgI != ArgE; ++ArgI)
307 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000308 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
309 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000310 : T->getCanonicalTypeInternal(),
311 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313}
314
315void
Sebastian Redl3397c552010-08-18 23:56:27 +0000316ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000317 VisitArrayType(T);
318 Writer.AddStmt(T->getSizeExpr());
319 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000320 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000321}
322
323void
Sebastian Redl3397c552010-08-18 23:56:27 +0000324ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000325 const DependentSizedExtVectorType *T) {
326 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000327 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000328}
329
330void
Sebastian Redl3397c552010-08-18 23:56:27 +0000331ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000332 Record.push_back(T->getDepth());
333 Record.push_back(T->getIndex());
334 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000335 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000336 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000337}
338
339void
Sebastian Redl3397c552010-08-18 23:56:27 +0000340ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000341 Record.push_back(T->getKeyword());
342 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
343 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000344 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
345 : T->getCanonicalTypeInternal(),
346 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000347 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000348}
349
350void
Sebastian Redl3397c552010-08-18 23:56:27 +0000351ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000352 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000353 Record.push_back(T->getKeyword());
354 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
355 Writer.AddIdentifierRef(T->getIdentifier(), Record);
356 Record.push_back(T->getNumArgs());
357 for (DependentTemplateSpecializationType::iterator
358 I = T->begin(), E = T->end(); I != E; ++I)
359 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000360 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000361}
362
Douglas Gregor7536dd52010-12-20 02:24:11 +0000363void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
364 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000365 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000366 Record.push_back(*NumExpansions + 1);
367 else
368 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000369 Code = TYPE_PACK_EXPANSION;
370}
371
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000372void ASTTypeWriter::VisitParenType(const ParenType *T) {
373 Writer.AddTypeRef(T->getInnerType(), Record);
374 Code = TYPE_PAREN;
375}
376
Sebastian Redl3397c552010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000378 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000379 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
380 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000381 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000382}
383
Sebastian Redl3397c552010-08-18 23:56:27 +0000384void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000385 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000386 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000387 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000388}
389
Sebastian Redl3397c552010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000391 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000392 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000393}
394
Sebastian Redl3397c552010-08-18 23:56:27 +0000395void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000396 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000397 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000398 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000399 E = T->qual_end(); I != E; ++I)
400 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000401 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000402}
403
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000404void
Sebastian Redl3397c552010-08-18 23:56:27 +0000405ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000406 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000407 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000408}
409
Eli Friedmanb001de72011-10-06 23:00:33 +0000410void
411ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
412 Writer.AddTypeRef(T->getValueType(), Record);
413 Code = TYPE_ATOMIC;
414}
415
John McCalla1ee0c52009-10-16 21:56:05 +0000416namespace {
417
418class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000419 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000420 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000421
422public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000423 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000424 : Writer(Writer), Record(Record) { }
425
John McCall51bd8032009-10-18 01:05:36 +0000426#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000427#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000428 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000429#include "clang/AST/TypeLocNodes.def"
430
John McCall51bd8032009-10-18 01:05:36 +0000431 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
432 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000433};
434
435}
436
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
438 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000441 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
442 if (TL.needsExtraLocalData()) {
443 Record.push_back(TL.getWrittenTypeSpec());
444 Record.push_back(TL.getWrittenSignSpec());
445 Record.push_back(TL.getWrittenWidthSpec());
446 Record.push_back(TL.hasModeAttr());
447 }
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000454}
Reid Kleckner12df2462013-06-24 17:51:48 +0000455void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
456 // nothing to do
457}
John McCall51bd8032009-10-18 01:05:36 +0000458void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000460}
John McCall51bd8032009-10-18 01:05:36 +0000461void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000463}
John McCall51bd8032009-10-18 01:05:36 +0000464void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
465 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
John McCall51bd8032009-10-18 01:05:36 +0000467void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000469 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000470}
John McCall51bd8032009-10-18 01:05:36 +0000471void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
473 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
474 Record.push_back(TL.getSizeExpr() ? 1 : 0);
475 if (TL.getSizeExpr())
476 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000477}
John McCall51bd8032009-10-18 01:05:36 +0000478void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
479 VisitArrayTypeLoc(TL);
480}
481void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
482 VisitArrayTypeLoc(TL);
483}
484void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
485 VisitArrayTypeLoc(TL);
486}
487void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
488 DependentSizedArrayTypeLoc TL) {
489 VisitArrayTypeLoc(TL);
490}
491void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
492 DependentSizedExtVectorTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
495void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getNameLoc(), Record);
497}
498void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getNameLoc(), Record);
500}
501void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000502 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000503 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
504 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000505 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000506 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
507 Writer.AddDeclRef(TL.getArg(i), Record);
508}
509void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
510 VisitFunctionTypeLoc(TL);
511}
512void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
513 VisitFunctionTypeLoc(TL);
514}
John McCalled976492009-12-04 22:46:56 +0000515void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
516 Writer.AddSourceLocation(TL.getNameLoc(), Record);
517}
John McCall51bd8032009-10-18 01:05:36 +0000518void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
521void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000522 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
523 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
524 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000525}
526void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000527 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
528 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
529 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
530 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000531}
532void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
Sean Huntca63c202011-05-24 22:41:36 +0000535void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getKWLoc(), Record);
537 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
538 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
539 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
540}
Richard Smith34b41d92011-02-20 03:19:35 +0000541void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
John McCall51bd8032009-10-18 01:05:36 +0000544void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
547void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
John McCall9d156a72011-01-06 01:58:22 +0000550void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
552 if (TL.hasAttrOperand()) {
553 SourceRange range = TL.getAttrOperandParensRange();
554 Writer.AddSourceLocation(range.getBegin(), Record);
555 Writer.AddSourceLocation(range.getEnd(), Record);
556 }
557 if (TL.hasAttrExprOperand()) {
558 Expr *operand = TL.getAttrExprOperand();
559 Record.push_back(operand ? 1 : 0);
560 if (operand) Writer.AddStmt(operand);
561 } else if (TL.hasAttrEnumOperand()) {
562 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
563 }
564}
John McCall51bd8032009-10-18 01:05:36 +0000565void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
John McCall49a832b2009-10-18 09:09:24 +0000568void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
569 SubstTemplateTypeParmTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getNameLoc(), Record);
571}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000572void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
573 SubstTemplateTypeParmPackTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
John McCall51bd8032009-10-18 01:05:36 +0000576void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
577 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000578 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000579 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
580 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
581 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
582 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000583 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
584 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000585}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000586void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
587 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
588 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
589}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000590void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000591 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000592 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000593}
John McCall3cb0ebd2010-03-10 03:28:59 +0000594void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getNameLoc(), Record);
596}
Douglas Gregor4714c122010-03-31 17:34:00 +0000597void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000598 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000599 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000600 Writer.AddSourceLocation(TL.getNameLoc(), Record);
601}
John McCall33500952010-06-11 00:33:02 +0000602void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
603 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000604 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000605 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000606 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000607 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000608 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
609 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
610 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000611 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
612 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000613}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000614void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
615 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
616}
John McCall51bd8032009-10-18 01:05:36 +0000617void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
618 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000619}
620void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
621 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000622 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
623 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
624 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
625 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000626}
John McCall54e14c42009-10-22 22:37:11 +0000627void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
628 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000629}
Eli Friedmanb001de72011-10-06 23:00:33 +0000630void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
631 Writer.AddSourceLocation(TL.getKWLoc(), Record);
632 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
633 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
634}
John McCalla1ee0c52009-10-16 21:56:05 +0000635
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000636//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000637// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000638//===----------------------------------------------------------------------===//
639
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000640static void EmitBlockID(unsigned ID, const char *Name,
641 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000642 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 Record.clear();
644 Record.push_back(ID);
645 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
646
647 // Emit the block name if present.
648 if (Name == 0 || Name[0] == 0) return;
649 Record.clear();
650 while (*Name)
651 Record.push_back(*Name++);
652 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
653}
654
655static void EmitRecordID(unsigned ID, const char *Name,
656 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000657 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000658 Record.clear();
659 Record.push_back(ID);
660 while (*Name)
661 Record.push_back(*Name++);
662 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000663}
664
665static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000666 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000667#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000668 RECORD(STMT_STOP);
669 RECORD(STMT_NULL_PTR);
670 RECORD(STMT_NULL);
671 RECORD(STMT_COMPOUND);
672 RECORD(STMT_CASE);
673 RECORD(STMT_DEFAULT);
674 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000675 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000676 RECORD(STMT_IF);
677 RECORD(STMT_SWITCH);
678 RECORD(STMT_WHILE);
679 RECORD(STMT_DO);
680 RECORD(STMT_FOR);
681 RECORD(STMT_GOTO);
682 RECORD(STMT_INDIRECT_GOTO);
683 RECORD(STMT_CONTINUE);
684 RECORD(STMT_BREAK);
685 RECORD(STMT_RETURN);
686 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000687 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000688 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000689 RECORD(EXPR_PREDEFINED);
690 RECORD(EXPR_DECL_REF);
691 RECORD(EXPR_INTEGER_LITERAL);
692 RECORD(EXPR_FLOATING_LITERAL);
693 RECORD(EXPR_IMAGINARY_LITERAL);
694 RECORD(EXPR_STRING_LITERAL);
695 RECORD(EXPR_CHARACTER_LITERAL);
696 RECORD(EXPR_PAREN);
697 RECORD(EXPR_UNARY_OPERATOR);
698 RECORD(EXPR_SIZEOF_ALIGN_OF);
699 RECORD(EXPR_ARRAY_SUBSCRIPT);
700 RECORD(EXPR_CALL);
701 RECORD(EXPR_MEMBER);
702 RECORD(EXPR_BINARY_OPERATOR);
703 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
704 RECORD(EXPR_CONDITIONAL_OPERATOR);
705 RECORD(EXPR_IMPLICIT_CAST);
706 RECORD(EXPR_CSTYLE_CAST);
707 RECORD(EXPR_COMPOUND_LITERAL);
708 RECORD(EXPR_EXT_VECTOR_ELEMENT);
709 RECORD(EXPR_INIT_LIST);
710 RECORD(EXPR_DESIGNATED_INIT);
711 RECORD(EXPR_IMPLICIT_VALUE_INIT);
712 RECORD(EXPR_VA_ARG);
713 RECORD(EXPR_ADDR_LABEL);
714 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000715 RECORD(EXPR_CHOOSE);
716 RECORD(EXPR_GNU_NULL);
717 RECORD(EXPR_SHUFFLE_VECTOR);
718 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000719 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000720 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000721 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000722 RECORD(EXPR_OBJC_ARRAY_LITERAL);
723 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000724 RECORD(EXPR_OBJC_ENCODE);
725 RECORD(EXPR_OBJC_SELECTOR_EXPR);
726 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
727 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
728 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
729 RECORD(EXPR_OBJC_KVC_REF_EXPR);
730 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000731 RECORD(STMT_OBJC_FOR_COLLECTION);
732 RECORD(STMT_OBJC_CATCH);
733 RECORD(STMT_OBJC_FINALLY);
734 RECORD(STMT_OBJC_AT_TRY);
735 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
736 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000737 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000738 RECORD(EXPR_CXX_OPERATOR_CALL);
739 RECORD(EXPR_CXX_CONSTRUCT);
740 RECORD(EXPR_CXX_STATIC_CAST);
741 RECORD(EXPR_CXX_DYNAMIC_CAST);
742 RECORD(EXPR_CXX_REINTERPRET_CAST);
743 RECORD(EXPR_CXX_CONST_CAST);
744 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000745 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smith7c3e6152013-06-12 22:31:48 +0000746 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000747 RECORD(EXPR_CXX_BOOL_LITERAL);
748 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000749 RECORD(EXPR_CXX_TYPEID_EXPR);
750 RECORD(EXPR_CXX_TYPEID_TYPE);
751 RECORD(EXPR_CXX_UUIDOF_EXPR);
752 RECORD(EXPR_CXX_UUIDOF_TYPE);
753 RECORD(EXPR_CXX_THIS);
754 RECORD(EXPR_CXX_THROW);
755 RECORD(EXPR_CXX_DEFAULT_ARG);
756 RECORD(EXPR_CXX_BIND_TEMPORARY);
757 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
758 RECORD(EXPR_CXX_NEW);
759 RECORD(EXPR_CXX_DELETE);
760 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
761 RECORD(EXPR_EXPR_WITH_CLEANUPS);
762 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
763 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
764 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
765 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
766 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
767 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
768 RECORD(EXPR_CXX_NOEXCEPT);
769 RECORD(EXPR_OPAQUE_VALUE);
770 RECORD(EXPR_BINARY_TYPE_TRAIT);
771 RECORD(EXPR_PACK_EXPANSION);
772 RECORD(EXPR_SIZEOF_PACK);
773 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000774 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000775#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000776}
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Sebastian Redla4232eb2010-08-18 23:56:21 +0000778void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000779 RecordData Record;
780 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000782#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
783#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000785 // Control Block.
786 BLOCK(CONTROL_BLOCK);
787 RECORD(METADATA);
788 RECORD(IMPORTS);
789 RECORD(LANGUAGE_OPTIONS);
790 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000791 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000792 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000793 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000794 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000795 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000796 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000797 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000798 RECORD(PREPROCESSOR_OPTIONS);
799
Douglas Gregorc337fef2012-10-19 00:45:00 +0000800 BLOCK(INPUT_FILES_BLOCK);
801 RECORD(INPUT_FILE);
802
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000803 // AST Top-Level Block.
804 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000805 RECORD(TYPE_OFFSET);
806 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 RECORD(IDENTIFIER_OFFSET);
808 RECORD(IDENTIFIER_TABLE);
809 RECORD(EXTERNAL_DEFINITIONS);
810 RECORD(SPECIAL_TYPES);
811 RECORD(STATISTICS);
812 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000813 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000814 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000815 RECORD(SELECTOR_OFFSETS);
816 RECORD(METHOD_POOL);
817 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000818 RECORD(SOURCE_LOCATION_OFFSETS);
819 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000820 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000821 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000822 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000823 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000824 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000825 RECORD(SEMA_DECL_REFS);
826 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
827 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
828 RECORD(DECL_REPLACEMENTS);
829 RECORD(UPDATE_VISIBLE);
830 RECORD(DECL_UPDATE_OFFSETS);
831 RECORD(DECL_UPDATES);
832 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
833 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000834 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000835 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000836 RECORD(FP_PRAGMA_OPTIONS);
837 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000838 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000839 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000840 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000841 RECORD(MODULE_OFFSET_MAP);
842 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000843 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000844 RECORD(FILE_SORTED_DECLS);
845 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000846 RECORD(MERGED_DECLARATIONS);
847 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000848 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000849 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000850 RECORD(MACRO_TABLE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000851
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000852 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000853 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000854 RECORD(SM_SLOC_FILE_ENTRY);
855 RECORD(SM_SLOC_BUFFER_ENTRY);
856 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000857 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000859 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000860 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000861 RECORD(PP_MACRO_OBJECT_LIKE);
862 RECORD(PP_MACRO_FUNCTION_LIKE);
863 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000864
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000865 // Decls and Types block.
866 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000867 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000868 RECORD(TYPE_COMPLEX);
869 RECORD(TYPE_POINTER);
870 RECORD(TYPE_BLOCK_POINTER);
871 RECORD(TYPE_LVALUE_REFERENCE);
872 RECORD(TYPE_RVALUE_REFERENCE);
873 RECORD(TYPE_MEMBER_POINTER);
874 RECORD(TYPE_CONSTANT_ARRAY);
875 RECORD(TYPE_INCOMPLETE_ARRAY);
876 RECORD(TYPE_VARIABLE_ARRAY);
877 RECORD(TYPE_VECTOR);
878 RECORD(TYPE_EXT_VECTOR);
879 RECORD(TYPE_FUNCTION_PROTO);
880 RECORD(TYPE_FUNCTION_NO_PROTO);
881 RECORD(TYPE_TYPEDEF);
882 RECORD(TYPE_TYPEOF_EXPR);
883 RECORD(TYPE_TYPEOF);
884 RECORD(TYPE_RECORD);
885 RECORD(TYPE_ENUM);
886 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000887 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000888 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000889 RECORD(TYPE_DECLTYPE);
890 RECORD(TYPE_ELABORATED);
891 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
892 RECORD(TYPE_UNRESOLVED_USING);
893 RECORD(TYPE_INJECTED_CLASS_NAME);
894 RECORD(TYPE_OBJC_OBJECT);
895 RECORD(TYPE_TEMPLATE_TYPE_PARM);
896 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
897 RECORD(TYPE_DEPENDENT_NAME);
898 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
899 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
900 RECORD(TYPE_PAREN);
901 RECORD(TYPE_PACK_EXPANSION);
902 RECORD(TYPE_ATTRIBUTED);
903 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000904 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000905 RECORD(DECL_TYPEDEF);
906 RECORD(DECL_ENUM);
907 RECORD(DECL_RECORD);
908 RECORD(DECL_ENUM_CONSTANT);
909 RECORD(DECL_FUNCTION);
910 RECORD(DECL_OBJC_METHOD);
911 RECORD(DECL_OBJC_INTERFACE);
912 RECORD(DECL_OBJC_PROTOCOL);
913 RECORD(DECL_OBJC_IVAR);
914 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000915 RECORD(DECL_OBJC_CATEGORY);
916 RECORD(DECL_OBJC_CATEGORY_IMPL);
917 RECORD(DECL_OBJC_IMPLEMENTATION);
918 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
919 RECORD(DECL_OBJC_PROPERTY);
920 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000921 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000922 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000923 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000924 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000925 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000926 RECORD(DECL_FILE_SCOPE_ASM);
927 RECORD(DECL_BLOCK);
928 RECORD(DECL_CONTEXT_LEXICAL);
929 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000930 RECORD(DECL_NAMESPACE);
931 RECORD(DECL_NAMESPACE_ALIAS);
932 RECORD(DECL_USING);
933 RECORD(DECL_USING_SHADOW);
934 RECORD(DECL_USING_DIRECTIVE);
935 RECORD(DECL_UNRESOLVED_USING_VALUE);
936 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
937 RECORD(DECL_LINKAGE_SPEC);
938 RECORD(DECL_CXX_RECORD);
939 RECORD(DECL_CXX_METHOD);
940 RECORD(DECL_CXX_CONSTRUCTOR);
941 RECORD(DECL_CXX_DESTRUCTOR);
942 RECORD(DECL_CXX_CONVERSION);
943 RECORD(DECL_ACCESS_SPEC);
944 RECORD(DECL_FRIEND);
945 RECORD(DECL_FRIEND_TEMPLATE);
946 RECORD(DECL_CLASS_TEMPLATE);
947 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
948 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
949 RECORD(DECL_FUNCTION_TEMPLATE);
950 RECORD(DECL_TEMPLATE_TYPE_PARM);
951 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
952 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
953 RECORD(DECL_STATIC_ASSERT);
954 RECORD(DECL_CXX_BASE_SPECIFIERS);
955 RECORD(DECL_INDIRECTFIELD);
956 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
957
Douglas Gregora72d8c42011-06-03 02:27:19 +0000958 // Statements and Exprs can occur in the Decls and Types block.
959 AddStmtsExprs(Stream, Record);
960
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000961 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000962 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000963 RECORD(PPD_MACRO_DEFINITION);
964 RECORD(PPD_INCLUSION_DIRECTIVE);
965
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000966#undef RECORD
967#undef BLOCK
968 Stream.ExitBlock();
969}
970
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971/// \brief Adjusts the given filename to only write out the portion of the
972/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000973///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974/// \param Filename the file name to adjust.
975///
976/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
977/// the returned filename will be adjusted by this system root.
978///
979/// \returns either the original filename (if it needs no adjustment) or the
980/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000981static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000982adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregor832d6202011-07-22 16:35:34 +0000985 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 // Verify that the filename and the system root have the same prefix.
989 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000990 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 if (Filename[Pos] != isysroot[Pos])
992 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregore650c8c2009-07-07 00:12:59 +0000994 // We hit the end of the filename before we hit the end of the system root.
995 if (!Filename[Pos])
996 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Douglas Gregore650c8c2009-07-07 00:12:59 +0000998 // If the file name has a '/' at the current position, skip over the '/'.
999 // We distinguish sysroot-based includes from absolute includes by the
1000 // absence of '/' at the beginning of sysroot-based includes.
1001 if (Filename[Pos] == '/')
1002 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Douglas Gregore650c8c2009-07-07 00:12:59 +00001004 return Filename + Pos;
1005}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001006
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001007/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001008void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1009 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001010 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001011 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001012 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1013 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001014
Douglas Gregore650c8c2009-07-07 00:12:59 +00001015 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001016 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1017 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1018 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1019 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1020 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1021 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1022 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1023 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1024 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1025 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1026 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001027 Record.push_back(VERSION_MAJOR);
1028 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001029 Record.push_back(CLANG_VERSION_MAJOR);
1030 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001031 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001032 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001033 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1034 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001035
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001036 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001037 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001038 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001039 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001040 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001041
1042 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1043 M != MEnd; ++M) {
1044 // Skip modules that weren't directly imported.
1045 if (!(*M)->isDirectlyImported())
1046 continue;
1047
1048 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001049 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001050 Record.push_back((*M)->File->getSize());
1051 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001052 // FIXME: This writes the absolute path for AST files we depend on.
1053 const std::string &FileName = (*M)->FileName;
1054 Record.push_back(FileName.size());
1055 Record.append(FileName.begin(), FileName.end());
1056 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001057 Stream.EmitRecord(IMPORTS, Record);
1058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001060 // Language options.
1061 Record.clear();
1062 const LangOptions &LangOpts = Context.getLangOpts();
1063#define LANGOPT(Name, Bits, Default, Description) \
1064 Record.push_back(LangOpts.Name);
1065#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1066 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1067#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001068#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1069#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001070
1071 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1072 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1073
1074 Record.push_back(LangOpts.CurrentModule.size());
1075 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001076
1077 // Comment options.
1078 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1079 for (CommentOptions::BlockCommandNamesTy::const_iterator
1080 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1081 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1082 I != IEnd; ++I) {
1083 AddString(*I, Record);
1084 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001085 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001086
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001087 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1088
Douglas Gregoree097c12012-10-18 17:58:09 +00001089 // Target options.
1090 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001091 const TargetInfo &Target = Context.getTargetInfo();
1092 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001093 AddString(TargetOpts.Triple, Record);
1094 AddString(TargetOpts.CPU, Record);
1095 AddString(TargetOpts.ABI, Record);
1096 AddString(TargetOpts.CXXABI, Record);
1097 AddString(TargetOpts.LinkerVersion, Record);
1098 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1099 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1100 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1101 }
1102 Record.push_back(TargetOpts.Features.size());
1103 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1104 AddString(TargetOpts.Features[I], Record);
1105 }
1106 Stream.EmitRecord(TARGET_OPTIONS, Record);
1107
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001108 // Diagnostic options.
1109 Record.clear();
1110 const DiagnosticOptions &DiagOpts
1111 = Context.getDiagnostics().getDiagnosticOptions();
1112#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1113#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1114 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1115#include "clang/Basic/DiagnosticOptions.def"
1116 Record.push_back(DiagOpts.Warnings.size());
1117 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1118 AddString(DiagOpts.Warnings[I], Record);
1119 // Note: we don't serialize the log or serialization file names, because they
1120 // are generally transient files and will almost always be overridden.
1121 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1122
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001123 // File system options.
1124 Record.clear();
1125 const FileSystemOptions &FSOpts
1126 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1127 AddString(FSOpts.WorkingDir, Record);
1128 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1129
Douglas Gregorbbf38312012-10-24 16:50:34 +00001130 // Header search options.
1131 Record.clear();
1132 const HeaderSearchOptions &HSOpts
1133 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1134 AddString(HSOpts.Sysroot, Record);
1135
1136 // Include entries.
1137 Record.push_back(HSOpts.UserEntries.size());
1138 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1139 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1140 AddString(Entry.Path, Record);
1141 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001142 Record.push_back(Entry.IsFramework);
1143 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001144 }
1145
1146 // System header prefixes.
1147 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1148 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1149 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1150 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1151 }
1152
1153 AddString(HSOpts.ResourceDir, Record);
1154 AddString(HSOpts.ModuleCachePath, Record);
1155 Record.push_back(HSOpts.DisableModuleHash);
1156 Record.push_back(HSOpts.UseBuiltinIncludes);
1157 Record.push_back(HSOpts.UseStandardSystemIncludes);
1158 Record.push_back(HSOpts.UseStandardCXXIncludes);
1159 Record.push_back(HSOpts.UseLibcxx);
1160 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1161
Douglas Gregora71a7d82012-10-24 20:05:57 +00001162 // Preprocessor options.
1163 Record.clear();
1164 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1165
1166 // Macro definitions.
1167 Record.push_back(PPOpts.Macros.size());
1168 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1169 AddString(PPOpts.Macros[I].first, Record);
1170 Record.push_back(PPOpts.Macros[I].second);
1171 }
1172
1173 // Includes
1174 Record.push_back(PPOpts.Includes.size());
1175 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1176 AddString(PPOpts.Includes[I], Record);
1177
1178 // Macro includes
1179 Record.push_back(PPOpts.MacroIncludes.size());
1180 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1181 AddString(PPOpts.MacroIncludes[I], Record);
1182
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001183 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001184 // Detailed record is important since it is used for the module cache hash.
1185 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001186 AddString(PPOpts.ImplicitPCHInclude, Record);
1187 AddString(PPOpts.ImplicitPTHInclude, Record);
1188 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1189 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1190
Douglas Gregor31d375f2011-05-06 21:43:30 +00001191 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001192 SourceManager &SM = Context.getSourceManager();
1193 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1194 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001195 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1196 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001197 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1198 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1199
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001200 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001202 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001203
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001204 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001205 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001206 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001207 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001208 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001209 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001210 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001211 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001212
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001213 Record.clear();
1214 Record.push_back(SM.getMainFileID().getOpaqueValue());
1215 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1216
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001217 // Original PCH directory
1218 if (!OutputFile.empty() && OutputFile != "-") {
1219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1220 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1222 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1223
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001224 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001225
1226 llvm::sys::fs::make_absolute(OutputPath);
1227 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1228
1229 RecordData Record;
1230 Record.push_back(ORIGINAL_PCH_DIR);
1231 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1232 }
1233
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001234 WriteInputFiles(Context.SourceMgr,
1235 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1236 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001237 Stream.ExitBlock();
1238}
1239
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001240namespace {
1241 /// \brief An input file.
1242 struct InputFileEntry {
1243 const FileEntry *File;
1244 bool IsSystemFile;
1245 bool BufferOverridden;
1246 };
1247}
1248
1249void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1250 HeaderSearchOptions &HSOpts,
1251 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001252 using namespace llvm;
1253 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1254 RecordData Record;
1255
1256 // Create input-file abbreviation.
1257 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1258 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001259 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001260 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1261 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001262 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001263 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1264 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1265
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001266 // Get all ContentCache objects for files, sorted by whether the file is a
1267 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001268 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001269 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1270 // Get this source location entry.
1271 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001272 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001273
1274 // We only care about file entries that were not overridden.
1275 if (!SLoc->isFile())
1276 continue;
1277 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001278 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001279 continue;
1280
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001281 InputFileEntry Entry;
1282 Entry.File = Cache->OrigEntry;
1283 Entry.IsSystemFile = Cache->IsSystemFile;
1284 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001285 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001286 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001287 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001288 SortedFiles.push_front(Entry);
1289 }
1290
1291 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1292 // the set of (non-system) input files. This is simple heuristic for
1293 // detecting whether the system headers may have changed, because it is too
1294 // expensive to stat() all of the system headers.
Richard Smithcc8e22b2013-05-20 23:40:27 +00001295 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001296 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001297 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1298 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1299 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1300 InputFileEntry Entry = { SDKSettingsFile, false, false };
1301 SortedFiles.push_front(Entry);
1302 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001303 }
1304
1305 unsigned UserFilesNum = 0;
1306 // Write out all of the input files.
1307 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001308 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001309 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001310 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001311
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001312 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001313 if (InputFileID != 0)
1314 continue; // already recorded this file.
1315
Douglas Gregora930dc92012-10-22 18:42:04 +00001316 // Record this entry's offset.
1317 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001318
1319 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001320
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001321 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001322 ++UserFilesNum;
1323
Douglas Gregor745e6f12012-10-19 00:38:02 +00001324 Record.clear();
1325 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001326 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001327
1328 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001329 Record.push_back(Entry.File->getSize());
1330 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001331
Douglas Gregora930dc92012-10-22 18:42:04 +00001332 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001333 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001334
Douglas Gregor745e6f12012-10-19 00:38:02 +00001335 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001336 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001337 SmallString<128> FilePath(Filename);
1338
1339 // Ask the file manager to fixup the relative path for us. This will
1340 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001341 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001342
1343 // FIXME: This call to make_absolute shouldn't be necessary, the
1344 // call to FixupRelativePath should always return an absolute path.
1345 llvm::sys::fs::make_absolute(FilePath);
1346 Filename = FilePath.c_str();
1347
1348 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1349
1350 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1351 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001352
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001353 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001354
1355 // Create input file offsets abbreviation.
1356 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1357 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1358 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001359 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1360 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001361 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1362 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1363
1364 // Write input file offsets.
1365 Record.clear();
1366 Record.push_back(INPUT_FILE_OFFSETS);
1367 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001368 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001369 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001370}
1371
Douglas Gregor14f79002009-04-10 03:52:48 +00001372//===----------------------------------------------------------------------===//
1373// Source Manager Serialization
1374//===----------------------------------------------------------------------===//
1375
1376/// \brief Create an abbreviation for the SLocEntry that refers to a
1377/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001378static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001379 using namespace llvm;
1380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001381 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001386 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001391 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001392}
1393
1394/// \brief Create an abbreviation for the SLocEntry that refers to a
1395/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001396static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001397 using namespace llvm;
1398 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001399 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001405 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001406}
1407
1408/// \brief Create an abbreviation for the SLocEntry that refers to a
1409/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001410static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001411 using namespace llvm;
1412 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001413 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001415 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001416}
1417
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001418/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1419/// expansion.
1420static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001421 using namespace llvm;
1422 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001423 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001429 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001430}
1431
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001432namespace {
1433 // Trait used for the on-disk hash table of header search information.
1434 class HeaderFileInfoTrait {
1435 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001436 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001437
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001438 // Keep track of the framework names we've used during serialization.
1439 SmallVector<char, 128> FrameworkStringData;
1440 llvm::StringMap<unsigned> FrameworkNameOffset;
1441
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001442 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001443 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1444 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001445
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001446 struct key_type {
1447 const FileEntry *FE;
1448 const char *Filename;
1449 };
1450 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001451
1452 typedef HeaderFileInfo data_type;
1453 typedef const data_type &data_type_ref;
1454
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001455 static unsigned ComputeHash(key_type_ref key) {
1456 // The hash is based only on size/time of the file, so that the reader can
1457 // match even when symlinking or excess path elements ("foo/../", "../")
1458 // change the form of the name. However, complete path is still the key.
1459 return llvm::hash_combine(key.FE->getSize(),
1460 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001461 }
1462
1463 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001464 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1465 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1466 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001467 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001468 if (Data.isModuleHeader)
1469 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001470 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001471 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001472 }
1473
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001474 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1475 clang::io::Emit64(Out, key.FE->getSize());
1476 KeyLen -= 8;
1477 clang::io::Emit64(Out, key.FE->getModificationTime());
1478 KeyLen -= 8;
1479 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001480 }
1481
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001482 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001483 data_type_ref Data, unsigned DataLen) {
1484 using namespace clang::io;
1485 uint64_t Start = Out.tell(); (void)Start;
1486
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001487 unsigned char Flags = (Data.HeaderRole << 6)
1488 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001489 | (Data.isPragmaOnce << 4)
1490 | (Data.DirInfo << 2)
1491 | (Data.Resolved << 1)
1492 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001493 Emit8(Out, (uint8_t)Flags);
1494 Emit16(Out, (uint16_t) Data.NumIncludes);
1495
1496 if (!Data.ControllingMacro)
1497 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1498 else
1499 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001500
1501 unsigned Offset = 0;
1502 if (!Data.Framework.empty()) {
1503 // If this header refers into a framework, save the framework name.
1504 llvm::StringMap<unsigned>::iterator Pos
1505 = FrameworkNameOffset.find(Data.Framework);
1506 if (Pos == FrameworkNameOffset.end()) {
1507 Offset = FrameworkStringData.size() + 1;
1508 FrameworkStringData.append(Data.Framework.begin(),
1509 Data.Framework.end());
1510 FrameworkStringData.push_back(0);
1511
1512 FrameworkNameOffset[Data.Framework] = Offset;
1513 } else
1514 Offset = Pos->second;
1515 }
1516 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001517
1518 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001519 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001520 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1521 }
1522
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001523 assert(Out.tell() - Start == DataLen && "Wrong data length");
1524 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001525
1526 const char *strings_begin() const { return FrameworkStringData.begin(); }
1527 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001528 };
1529} // end anonymous namespace
1530
1531/// \brief Write the header search block for the list of files that
1532///
1533/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001534void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001535 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001536 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1537
1538 if (FilesByUID.size() > HS.header_file_size())
1539 FilesByUID.resize(HS.header_file_size());
1540
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001541 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001542 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001543 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001544 unsigned NumHeaderSearchEntries = 0;
1545 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1546 const FileEntry *File = FilesByUID[UID];
1547 if (!File)
1548 continue;
1549
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001550 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1551 // from the external source if it was not provided already.
1552 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001553 if (HFI.External && Chain)
1554 continue;
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001555 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1556 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001557
1558 // Turn the file name into an absolute path, if it isn't already.
1559 const char *Filename = File->getName();
1560 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1561
1562 // If we performed any translation on the file name at all, we need to
1563 // save this string, since the generator will refer to it later.
1564 if (Filename != File->getName()) {
1565 Filename = strdup(Filename);
1566 SavedStrings.push_back(Filename);
1567 }
1568
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001569 HeaderFileInfoTrait::key_type key = { File, Filename };
1570 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001571 ++NumHeaderSearchEntries;
1572 }
1573
1574 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001575 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001576 uint32_t BucketOffset;
1577 {
1578 llvm::raw_svector_ostream Out(TableData);
1579 // Make sure that no bucket is at offset 0
1580 clang::io::Emit32(Out, 0);
1581 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1582 }
1583
1584 // Create a blob abbreviation
1585 using namespace llvm;
1586 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1587 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1592 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1593
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001594 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001595 RecordData Record;
1596 Record.push_back(HEADER_SEARCH_TABLE);
1597 Record.push_back(BucketOffset);
1598 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001599 Record.push_back(TableData.size());
1600 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001601 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1602
1603 // Free all of the strings we had to duplicate.
1604 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001605 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001606}
1607
Douglas Gregor14f79002009-04-10 03:52:48 +00001608/// \brief Writes the block containing the serialized form of the
1609/// source manager.
1610///
1611/// TODO: We should probably use an on-disk hash table (stored in a
1612/// blob), indexed based on the file name, so that we only create
1613/// entries for files that we actually need. In the common case (no
1614/// errors), we probably won't have to create file entries for any of
1615/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001616void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001617 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001618 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001619 RecordData Record;
1620
Chris Lattnerf04ad692009-04-10 17:16:57 +00001621 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001622 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001623
1624 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001625 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1626 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1627 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001628 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001629
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001630 // Write out the source location entry table. We skip the first
1631 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001632 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001633 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001634 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1635 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001636 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001637 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001638 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001639 FileID FID = FileID::get(I);
1640 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001641
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001642 // Record the offset of this source-location entry.
1643 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1644
1645 // Figure out which record code to use.
1646 unsigned Code;
1647 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001648 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1649 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001650 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001651 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001652 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001653 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001654 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001655 Record.clear();
1656 Record.push_back(Code);
1657
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001658 // Starting offset of this entry within this module, so skip the dummy.
1659 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001660 if (SLoc->isFile()) {
1661 const SrcMgr::FileInfo &File = SLoc->getFile();
1662 Record.push_back(File.getIncludeLoc().getRawEncoding());
1663 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1664 Record.push_back(File.hasLineDirectives());
1665
1666 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001667 if (Content->OrigEntry) {
1668 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001669 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001670
Douglas Gregora930dc92012-10-22 18:42:04 +00001671 // The source location entry is a file. Emit input file ID.
1672 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1673 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001675 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001676
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001677 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001678 if (FDI != FileDeclIDs.end()) {
1679 Record.push_back(FDI->second->FirstDeclIndex);
1680 Record.push_back(FDI->second->DeclIDs.size());
1681 } else {
1682 Record.push_back(0);
1683 Record.push_back(0);
1684 }
Douglas Gregora081da52011-11-16 20:05:18 +00001685
Douglas Gregora930dc92012-10-22 18:42:04 +00001686 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001687
1688 if (Content->BufferOverridden) {
1689 Record.clear();
1690 Record.push_back(SM_SLOC_BUFFER_BLOB);
1691 const llvm::MemoryBuffer *Buffer
1692 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1693 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1694 StringRef(Buffer->getBufferStart(),
1695 Buffer->getBufferSize() + 1));
1696 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697 } else {
1698 // The source location entry is a buffer. The blob associated
1699 // with this entry contains the contents of the buffer.
1700
1701 // We add one to the size so that we capture the trailing NULL
1702 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1703 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001704 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001705 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001706 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001707 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001708 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001709 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001710 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001711 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001712 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001713 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001714
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001715 if (strcmp(Name, "<built-in>") == 0) {
1716 PreloadSLocs.push_back(SLocEntryOffsets.size());
1717 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001718 }
1719 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001720 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001721 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001722 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1723 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001724 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1725 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001726
1727 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001728 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001729 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001730 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001731 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001732 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001733 }
1734 }
1735
Douglas Gregorc9490c02009-04-16 22:23:12 +00001736 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737
1738 if (SLocEntryOffsets.empty())
1739 return;
1740
Sebastian Redl3397c552010-08-18 23:56:27 +00001741 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001742 // table is used for lazily loading source-location information.
1743 using namespace llvm;
1744 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001745 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1749 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001751 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001752 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001753 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001754 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001755 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001756
Sebastian Redl3397c552010-08-18 23:56:27 +00001757 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001758 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001759 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001760
1761 // Write the line table. It depends on remapping working, so it must come
1762 // after the source location offsets.
1763 if (SourceMgr.hasLineTable()) {
1764 LineTableInfo &LineTable = SourceMgr.getLineTable();
1765
1766 Record.clear();
1767 // Emit the file names
1768 Record.push_back(LineTable.getNumFilenames());
1769 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1770 // Emit the file name
1771 const char *Filename = LineTable.getFilename(I);
1772 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1773 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1774 Record.push_back(FilenameLen);
1775 if (FilenameLen)
1776 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1777 }
1778
1779 // Emit the line entries
1780 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1781 L != LEnd; ++L) {
1782 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001783 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001784 continue;
1785
1786 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001787 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001788
1789 // Emit the line entries
1790 Record.push_back(L->second.size());
1791 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1792 LEEnd = L->second.end();
1793 LE != LEEnd; ++LE) {
1794 Record.push_back(LE->FileOffset);
1795 Record.push_back(LE->LineNo);
1796 Record.push_back(LE->FilenameID);
1797 Record.push_back((unsigned)LE->FileKind);
1798 Record.push_back(LE->IncludeOffset);
1799 }
1800 }
1801 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1802 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001803}
1804
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001805//===----------------------------------------------------------------------===//
1806// Preprocessor Serialization
1807//===----------------------------------------------------------------------===//
1808
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001809namespace {
1810class ASTMacroTableTrait {
1811public:
1812 typedef IdentID key_type;
1813 typedef key_type key_type_ref;
1814
1815 struct Data {
1816 uint32_t MacroDirectivesOffset;
1817 };
1818
1819 typedef Data data_type;
1820 typedef const data_type &data_type_ref;
1821
1822 static unsigned ComputeHash(IdentID IdID) {
1823 return llvm::hash_value(IdID);
1824 }
1825
1826 std::pair<unsigned,unsigned>
1827 static EmitKeyDataLength(raw_ostream& Out,
1828 key_type_ref Key, data_type_ref Data) {
1829 unsigned KeyLen = 4; // IdentID.
1830 unsigned DataLen = 4; // MacroDirectivesOffset.
1831 return std::make_pair(KeyLen, DataLen);
1832 }
1833
1834 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1835 clang::io::Emit32(Out, Key);
1836 }
1837
1838 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1839 unsigned) {
1840 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1841 }
1842};
1843} // end anonymous namespace
1844
1845static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1846 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1847 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1848 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1849 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001850 return X.first->getName().compare(Y.first->getName());
1851}
1852
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001853static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1854 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001855 if (MacroInfo *MI = MD->getMacroInfo())
1856 if (MI->isBuiltinMacro())
1857 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001858
1859 if (IsModule) {
1860 SourceLocation Loc = MD->getLocation();
1861 if (Loc.isInvalid())
1862 return true;
1863 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1864 return true;
1865 }
1866
1867 return false;
1868}
1869
Chris Lattner0b1fb982009-04-10 17:15:23 +00001870/// \brief Writes the block containing the serialized form of the
1871/// preprocessor.
1872///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001873void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001874 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1875 if (PPRec)
1876 WritePreprocessorDetail(*PPRec);
1877
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001878 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001879
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001880 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1881 if (PP.getCounterValue() != 0) {
1882 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001883 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001884 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001885 }
1886
1887 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001888 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Sebastian Redl3397c552010-08-18 23:56:27 +00001890 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001891 // FIXME: use diagnostics subsystem for localization etc.
1892 if (PP.SawDateOrTime())
1893 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Douglas Gregorecdcb882010-10-20 22:00:55 +00001895
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001896 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001897 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001898
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001899 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001900 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001901 MacroDirectives;
1902 for (Preprocessor::macro_iterator
1903 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1904 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001905 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001906 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001907 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001908
Douglas Gregor9c736102011-02-10 18:20:09 +00001909 // Sort the set of macro definitions that need to be serialized by the
1910 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001911 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1912 &compareMacroDirectives);
1913
1914 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1915
1916 // Emit the macro directives as a list and associate the offset with the
1917 // identifier they belong to.
1918 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1919 const IdentifierInfo *Name = MacroDirectives[I].first;
1920 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1921 MacroDirective *MD = MacroDirectives[I].second;
1922
1923 // If the macro or identifier need no updates, don't write the macro history
1924 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001925 // FIXME: Chain the macro history instead of re-writing it.
1926 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001927 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1928 continue;
1929
1930 // Emit the macro directives in reverse source order.
1931 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001932 if (MD->isHidden())
1933 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001934 if (shouldIgnoreMacro(MD, IsModule, PP))
1935 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001936
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001937 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001938 Record.push_back(MD->getKind());
1939 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1940 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1941 Record.push_back(InfoID);
1942 Record.push_back(DefMD->isImported());
1943 Record.push_back(DefMD->isAmbiguous());
1944
1945 } else if (VisibilityMacroDirective *
1946 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1947 Record.push_back(VisMD->isPublic());
1948 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001949 }
1950 if (Record.empty())
1951 continue;
1952
1953 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1954 Record.clear();
1955
1956 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1957
1958 IdentID NameID = getIdentifierRef(Name);
1959 ASTMacroTableTrait::Data data;
1960 data.MacroDirectivesOffset = MacroDirectiveOffset;
1961 Generator.insert(NameID, data);
1962 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001963
Douglas Gregora8235d62012-10-09 23:05:51 +00001964 /// \brief Offsets of each of the macros into the bitstream, indexed by
1965 /// the local macro ID
1966 ///
1967 /// For each identifier that is associated with a macro, this map
1968 /// provides the offset into the bitstream where that macro is
1969 /// defined.
1970 std::vector<uint32_t> MacroOffsets;
1971
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001972 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1973 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1974 MacroInfo *MI = MacroInfosToEmit[I].MI;
1975 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001976
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001977 if (ID < FirstMacroID) {
1978 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1979 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001980 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001981
1982 // Record the local offset of this macro.
1983 unsigned Index = ID - FirstMacroID;
1984 if (Index == MacroOffsets.size())
1985 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1986 else {
1987 if (Index > MacroOffsets.size())
1988 MacroOffsets.resize(Index + 1);
1989
1990 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1991 }
1992
1993 AddIdentifierRef(Name, Record);
1994 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1995 AddSourceLocation(MI->getDefinitionLoc(), Record);
1996 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1997 Record.push_back(MI->isUsed());
1998 unsigned Code;
1999 if (MI->isObjectLike()) {
2000 Code = PP_MACRO_OBJECT_LIKE;
2001 } else {
2002 Code = PP_MACRO_FUNCTION_LIKE;
2003
2004 Record.push_back(MI->isC99Varargs());
2005 Record.push_back(MI->isGNUVarargs());
2006 Record.push_back(MI->hasCommaPasting());
2007 Record.push_back(MI->getNumArgs());
2008 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2009 I != E; ++I)
2010 AddIdentifierRef(*I, Record);
2011 }
2012
2013 // If we have a detailed preprocessing record, record the macro definition
2014 // ID that corresponds to this macro.
2015 if (PPRec)
2016 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2017
2018 Stream.EmitRecord(Code, Record);
2019 Record.clear();
2020
2021 // Emit the tokens array.
2022 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2023 // Note that we know that the preprocessor does not have any annotation
2024 // tokens in it because they are created by the parser, and thus can't
2025 // be in a macro definition.
2026 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002027 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002028 Stream.EmitRecord(PP_TOKEN, Record);
2029 Record.clear();
2030 }
2031 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002032 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002033
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002034 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002035
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002036 // Create the on-disk hash table in a buffer.
2037 SmallString<4096> MacroTable;
2038 uint32_t BucketOffset;
2039 {
2040 llvm::raw_svector_ostream Out(MacroTable);
2041 // Make sure that no bucket is at offset 0
2042 clang::io::Emit32(Out, 0);
2043 BucketOffset = Generator.Emit(Out);
2044 }
2045
2046 // Write the macro table
2047 using namespace llvm;
2048 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2049 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2052 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2053
2054 Record.push_back(MACRO_TABLE);
2055 Record.push_back(BucketOffset);
2056 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2057 Record.clear();
2058
Douglas Gregora8235d62012-10-09 23:05:51 +00002059 // Write the offsets table for macro IDs.
2060 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002061 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002062 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2066
2067 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2068 Record.clear();
2069 Record.push_back(MACRO_OFFSET);
2070 Record.push_back(MacroOffsets.size());
2071 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2072 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2073 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002074}
2075
2076void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002077 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002078 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002079
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002080 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002081
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002082 // Enter the preprocessor block.
2083 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002084
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002085 // If the preprocessor has a preprocessing record, emit it.
2086 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002087 using namespace llvm;
2088
2089 // Set up the abbreviation for
2090 unsigned InclusionAbbrev = 0;
2091 {
2092 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2093 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2099 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2100 }
2101
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002102 unsigned FirstPreprocessorEntityID
2103 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2104 + NUM_PREDEF_PP_ENTITY_IDS;
2105 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002106 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002107 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2108 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002109 E != EEnd;
2110 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002111 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002112
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002113 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2114 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002115
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002116 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002117 // Record this macro definition's ID.
2118 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002119
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002120 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002121 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2122 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002123 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002124
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002125 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002126 Record.push_back(ME->isBuiltinMacro());
2127 if (ME->isBuiltinMacro())
2128 AddIdentifierRef(ME->getName(), Record);
2129 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002130 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002131 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002132 continue;
2133 }
2134
2135 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2136 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002137 Record.push_back(ID->getFileName().size());
2138 Record.push_back(ID->wasInQuotes());
2139 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002140 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002141 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002142 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002143 // Check that the FileEntry is not null because it was not resolved and
2144 // we create a PCH even with compiler errors.
2145 if (ID->getFile())
2146 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002147 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2148 continue;
2149 }
2150
2151 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2152 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002153 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002154
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002155 // Write the offsets table for the preprocessing record.
2156 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002157 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2158
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002159 // Write the offsets table for identifier IDs.
2160 using namespace llvm;
2161 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002162 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002165 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002166
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002167 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002168 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002169 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002170 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2171 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002172 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002173}
2174
Douglas Gregore209e502011-12-06 01:10:29 +00002175unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2176 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2177 if (Known != SubmoduleIDs.end())
2178 return Known->second;
2179
2180 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2181}
2182
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002183unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2184 if (!Mod)
2185 return 0;
2186
2187 llvm::DenseMap<Module *, unsigned>::const_iterator
2188 Known = SubmoduleIDs.find(Mod);
2189 if (Known != SubmoduleIDs.end())
2190 return Known->second;
2191
2192 return 0;
2193}
2194
Douglas Gregor26ced122011-12-01 00:59:36 +00002195/// \brief Compute the number of modules within the given tree (including the
2196/// given module).
2197static unsigned getNumberOfModules(Module *Mod) {
2198 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002199 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2200 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002201 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002202 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002203
2204 return ChildModules + 1;
2205}
2206
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002207void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002208 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002209 // FIXME: This feels like it belongs somewhere else, but there are no
2210 // other consumers of this information.
2211 SourceManager &SrcMgr = PP->getSourceManager();
2212 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2213 for (ASTContext::import_iterator I = Context->local_import_begin(),
2214 IEnd = Context->local_import_end();
2215 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002216 if (Module *ImportedFrom
2217 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2218 SrcMgr))) {
2219 ImportedFrom->Imports.push_back(I->getImportedModule());
2220 }
2221 }
2222
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002223 // Enter the submodule description block.
2224 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2225
2226 // Write the abbreviations needed for the submodules block.
2227 using namespace llvm;
2228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2229 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2240 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2241
2242 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002243 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2245 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2246
2247 Abbrev = new BitCodeAbbrev();
2248 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2250 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002251
2252 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002253 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2255 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2256
2257 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002258 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2260 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2261
Douglas Gregor51f564f2011-12-31 04:05:44 +00002262 Abbrev = new BitCodeAbbrev();
2263 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2265 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2266
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002267 Abbrev = new BitCodeAbbrev();
2268 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2270 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2271
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002272 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002273 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2275 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2276
2277 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002278 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2281 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2282
Douglas Gregor63a72682013-03-20 00:22:05 +00002283 Abbrev = new BitCodeAbbrev();
2284 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2286 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2287
Douglas Gregor906d66a2013-03-20 21:10:35 +00002288 Abbrev = new BitCodeAbbrev();
2289 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2292 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2293
Douglas Gregor26ced122011-12-01 00:59:36 +00002294 // Write the submodule metadata block.
2295 RecordData Record;
2296 Record.push_back(getNumberOfModules(WritingModule));
2297 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2298 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2299
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002300 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002301 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002302 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002303 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002304 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002305 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002306 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002307
2308 // Emit the definition of the block.
2309 Record.clear();
2310 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002311 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002312 if (Mod->Parent) {
2313 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2314 Record.push_back(SubmoduleIDs[Mod->Parent]);
2315 } else {
2316 Record.push_back(0);
2317 }
2318 Record.push_back(Mod->IsFramework);
2319 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002320 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002321 Record.push_back(Mod->InferSubmodules);
2322 Record.push_back(Mod->InferExplicitSubmodules);
2323 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002324 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002325 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2326
Douglas Gregor51f564f2011-12-31 04:05:44 +00002327 // Emit the requirements.
2328 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2329 Record.clear();
2330 Record.push_back(SUBMODULE_REQUIRES);
2331 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2332 Mod->Requires[I].data(),
2333 Mod->Requires[I].size());
2334 }
2335
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002336 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002337 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002338 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002339 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002340 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002341 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002342 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2343 Record.clear();
2344 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2345 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2346 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002347 }
2348
2349 // Emit the headers.
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002350 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002351 Record.clear();
2352 Record.push_back(SUBMODULE_HEADER);
2353 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002354 Mod->NormalHeaders[I]->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002355 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002356 // Emit the excluded headers.
2357 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2358 Record.clear();
2359 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2360 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2361 Mod->ExcludedHeaders[I]->getName());
2362 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002363 // Emit the private headers.
2364 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2365 Record.clear();
2366 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2367 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2368 Mod->PrivateHeaders[I]->getName());
2369 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002370 ArrayRef<const FileEntry *>
2371 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2372 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002373 Record.clear();
2374 Record.push_back(SUBMODULE_TOPHEADER);
2375 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002376 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002377 }
Douglas Gregor55988682011-12-05 16:33:54 +00002378
2379 // Emit the imports.
2380 if (!Mod->Imports.empty()) {
2381 Record.clear();
2382 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002383 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002384 assert(ImportedID && "Unknown submodule!");
2385 Record.push_back(ImportedID);
2386 }
2387 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2388 }
2389
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002390 // Emit the exports.
2391 if (!Mod->Exports.empty()) {
2392 Record.clear();
2393 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002394 if (Module *Exported = Mod->Exports[I].getPointer()) {
2395 unsigned ExportedID = SubmoduleIDs[Exported];
2396 assert(ExportedID > 0 && "Unknown submodule ID?");
2397 Record.push_back(ExportedID);
2398 } else {
2399 Record.push_back(0);
2400 }
2401
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002402 Record.push_back(Mod->Exports[I].getInt());
2403 }
2404 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2405 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002406
2407 // Emit the link libraries.
2408 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2409 Record.clear();
2410 Record.push_back(SUBMODULE_LINK_LIBRARY);
2411 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2412 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2413 Mod->LinkLibraries[I].Library);
2414 }
2415
Douglas Gregor906d66a2013-03-20 21:10:35 +00002416 // Emit the conflicts.
2417 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2418 Record.clear();
2419 Record.push_back(SUBMODULE_CONFLICT);
2420 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2421 assert(OtherID && "Unknown submodule!");
2422 Record.push_back(OtherID);
2423 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2424 Mod->Conflicts[I].Message);
2425 }
2426
Douglas Gregor63a72682013-03-20 00:22:05 +00002427 // Emit the configuration macros.
2428 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2429 Record.clear();
2430 Record.push_back(SUBMODULE_CONFIG_MACRO);
2431 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2432 Mod->ConfigMacros[I]);
2433 }
2434
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002435 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002436 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2437 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002438 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002439 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002440 }
2441
2442 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002443
2444 assert((NextSubmoduleID - FirstSubmoduleID
2445 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002446}
2447
Douglas Gregor185dbd72011-12-01 02:07:58 +00002448serialization::SubmoduleID
2449ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002450 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002451 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002452
2453 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002454 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002455 Module *OwningMod
2456 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002457 if (!OwningMod)
2458 return 0;
2459
Douglas Gregore209e502011-12-06 01:10:29 +00002460 // Check whether this submodule is part of our own module.
2461 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002462 return 0;
2463
Douglas Gregore209e502011-12-06 01:10:29 +00002464 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002465}
2466
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002467void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2468 bool isModule) {
2469 // Make sure set diagnostic pragmas don't affect the translation unit that
2470 // imports the module.
2471 // FIXME: Make diagnostic pragma sections work properly with modules.
2472 if (isModule)
2473 return;
2474
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002475 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2476 DiagStateIDMap;
2477 unsigned CurrID = 0;
2478 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002479 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002480 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002481 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2482 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002483 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002484 if (point.Loc.isInvalid())
2485 continue;
2486
2487 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002488 unsigned &DiagStateID = DiagStateIDMap[point.State];
2489 Record.push_back(DiagStateID);
2490
2491 if (DiagStateID == 0) {
2492 DiagStateID = ++CurrID;
2493 for (DiagnosticsEngine::DiagState::const_iterator
2494 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2495 if (I->second.isPragma()) {
2496 Record.push_back(I->first);
2497 Record.push_back(I->second.getMapping());
2498 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002499 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002500 Record.push_back(-1); // mark the end of the diag/map pairs for this
2501 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002502 }
2503 }
2504
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002505 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002506 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002507}
2508
Anders Carlssonc8505782011-03-06 18:41:18 +00002509void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2510 if (CXXBaseSpecifiersOffsets.empty())
2511 return;
2512
2513 RecordData Record;
2514
2515 // Create a blob abbreviation for the C++ base specifiers offsets.
2516 using namespace llvm;
2517
2518 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2519 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2521 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2522 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2523
Douglas Gregore92b8a12011-08-04 00:01:48 +00002524 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002525 Record.clear();
2526 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2527 Record.push_back(CXXBaseSpecifiersOffsets.size());
2528 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002529 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002530}
2531
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002532//===----------------------------------------------------------------------===//
2533// Type Serialization
2534//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002535
Sebastian Redl3397c552010-08-18 23:56:27 +00002536/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002537void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002538 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002539 if (Idx.getIndex() == 0) // we haven't seen this type before.
2540 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002541
Douglas Gregor97475832010-10-05 18:37:06 +00002542 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002543
Douglas Gregor2cf26342009-04-09 22:27:44 +00002544 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002545 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002546 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002547 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002548 else if (TypeOffsets.size() < Index) {
2549 TypeOffsets.resize(Index + 1);
2550 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551 }
2552
2553 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Douglas Gregor2cf26342009-04-09 22:27:44 +00002555 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002556 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002557
Douglas Gregora4923eb2009-11-16 21:35:15 +00002558 if (T.hasLocalNonFastQualifiers()) {
2559 Qualifiers Qs = T.getLocalQualifiers();
2560 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002561 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002562 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002563 } else {
2564 switch (T->getTypeClass()) {
2565 // For all of the concrete, non-dependent types, call the
2566 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002567#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002568 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002569#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002570#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002571 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002572 }
2573
2574 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002575 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002576
2577 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002578 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002579}
2580
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002581//===----------------------------------------------------------------------===//
2582// Declaration Serialization
2583//===----------------------------------------------------------------------===//
2584
Douglas Gregor2cf26342009-04-09 22:27:44 +00002585/// \brief Write the block containing all of the declaration IDs
2586/// lexically declared within the given DeclContext.
2587///
2588/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2589/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002590uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002591 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002592 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002593 return 0;
2594
Douglas Gregorc9490c02009-04-16 22:23:12 +00002595 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002596 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002597 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002598 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002599 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2600 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002601 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002602
Douglas Gregor25123082009-04-22 22:34:57 +00002603 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002604 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002605 return Offset;
2606}
2607
Sebastian Redla4232eb2010-08-18 23:56:21 +00002608void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002609 using namespace llvm;
2610 RecordData Record;
2611
2612 // Write the type offsets array
2613 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002614 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2618 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2619 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002620 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002621 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002622 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002623 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002624
2625 // Write the declaration offsets array
2626 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002627 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2631 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2632 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002633 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002634 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002635 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002636 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002637}
2638
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002639void ASTWriter::WriteFileDeclIDsMap() {
2640 using namespace llvm;
2641 RecordData Record;
2642
2643 // Join the vectors of DeclIDs from all files.
2644 SmallVector<DeclID, 256> FileSortedIDs;
2645 for (FileDeclIDsTy::iterator
2646 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2647 DeclIDInFileInfo &Info = *FI->second;
2648 Info.FirstDeclIndex = FileSortedIDs.size();
2649 for (LocDeclIDsTy::iterator
2650 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2651 FileSortedIDs.push_back(DI->second);
2652 }
2653
2654 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2655 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002657 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2658 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2659 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002660 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002661 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2662}
2663
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002664void ASTWriter::WriteComments() {
2665 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002666 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002667 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002668 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2669 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002670 I != E; ++I) {
2671 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002672 AddSourceRange((*I)->getSourceRange(), Record);
2673 Record.push_back((*I)->getKind());
2674 Record.push_back((*I)->isTrailingComment());
2675 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002676 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2677 }
2678 Stream.ExitBlock();
2679}
2680
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002681//===----------------------------------------------------------------------===//
2682// Global Method Pool and Selector Serialization
2683//===----------------------------------------------------------------------===//
2684
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002685namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002686// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002687class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002688 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002689
2690public:
2691 typedef Selector key_type;
2692 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Sebastian Redl5d050072010-08-04 17:20:04 +00002694 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002695 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002696 ObjCMethodList Instance, Factory;
2697 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002698 typedef const data_type& data_type_ref;
2699
Sebastian Redl3397c552010-08-18 23:56:27 +00002700 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002702 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002703 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002704 }
Mike Stump1eb44332009-09-09 15:08:12 +00002705
2706 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002707 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002708 data_type_ref Methods) {
2709 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2710 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002711 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2712 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002713 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002714 if (Method->Method)
2715 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002716 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002717 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002718 if (Method->Method)
2719 DataLen += 4;
2720 clang::io::Emit16(Out, DataLen);
2721 return std::make_pair(KeyLen, DataLen);
2722 }
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Chris Lattner5f9e2722011-07-23 10:55:15 +00002724 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002725 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002726 assert((Start >> 32) == 0 && "Selector key offset too large");
2727 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002728 unsigned N = Sel.getNumArgs();
2729 clang::io::Emit16(Out, N);
2730 if (N == 0)
2731 N = 1;
2732 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002733 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002734 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2735 }
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Chris Lattner5f9e2722011-07-23 10:55:15 +00002737 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002738 data_type_ref Methods, unsigned DataLen) {
2739 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002740 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002741 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002742 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002743 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002744 if (Method->Method)
2745 ++NumInstanceMethods;
2746
2747 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002748 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002749 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002750 if (Method->Method)
2751 ++NumFactoryMethods;
2752
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002753 unsigned InstanceBits = Methods.Instance.getBits();
2754 assert(InstanceBits < 4);
2755 unsigned NumInstanceMethodsAndBits =
2756 (NumInstanceMethods << 2) | InstanceBits;
2757 unsigned FactoryBits = Methods.Factory.getBits();
2758 assert(FactoryBits < 4);
2759 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2760 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2761 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002762 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002763 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002764 if (Method->Method)
2765 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002766 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002767 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002768 if (Method->Method)
2769 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002770
2771 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002772 }
2773};
2774} // end anonymous namespace
2775
Sebastian Redl059612d2010-08-03 21:58:15 +00002776/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002777///
2778/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002779/// in an on-disk hash table indexed by the selector. The hash table also
2780/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002781void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002782 using namespace llvm;
2783
Sebastian Redl059612d2010-08-03 21:58:15 +00002784 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002785 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002786 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002787 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002788 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002789 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002790 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002791 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002792
Sebastian Redl059612d2010-08-03 21:58:15 +00002793 // Create the on-disk hash table representation. We walk through every
2794 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002795 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002796 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002797 I = SelectorIDs.begin(), E = SelectorIDs.end();
2798 I != E; ++I) {
2799 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002800 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002801 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002802 I->second,
2803 ObjCMethodList(),
2804 ObjCMethodList()
2805 };
2806 if (F != SemaRef.MethodPool.end()) {
2807 Data.Instance = F->second.first;
2808 Data.Factory = F->second.second;
2809 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002810 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002811 // changed.
2812 if (Chain && I->second < FirstSelectorID) {
2813 // Selector already exists. Did it change?
2814 bool changed = false;
2815 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002816 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002817 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002818 changed = true;
2819 }
2820 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002821 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002822 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002823 changed = true;
2824 }
2825 if (!changed)
2826 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002827 } else if (Data.Instance.Method || Data.Factory.Method) {
2828 // A new method pool entry.
2829 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002830 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002831 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002832 }
2833
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002834 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002835 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002836 uint32_t BucketOffset;
2837 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002838 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002839 llvm::raw_svector_ostream Out(MethodPool);
2840 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002841 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002842 BucketOffset = Generator.Emit(Out, Trait);
2843 }
2844
2845 // Create a blob abbreviation
2846 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002847 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2851 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2852
Douglas Gregor83941df2009-04-25 17:48:32 +00002853 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002854 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002855 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002856 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002857 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002858 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002859
2860 // Create a blob abbreviation for the selector table offsets.
2861 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002862 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2866 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2867
2868 // Write the selector offsets table.
2869 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002870 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002871 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002872 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002873 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002874 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002875 }
2876}
2877
Sebastian Redl3397c552010-08-18 23:56:27 +00002878/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002879void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002880 using namespace llvm;
2881 if (SemaRef.ReferencedSelectors.empty())
2882 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002883
Fariborz Jahanian32019832010-07-23 19:11:11 +00002884 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002885
Sebastian Redl3397c552010-08-18 23:56:27 +00002886 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002887 // very tricky to fix, and given that @selector shouldn't really appear in
2888 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002889 for (DenseMap<Selector, SourceLocation>::iterator S =
2890 SemaRef.ReferencedSelectors.begin(),
2891 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2892 Selector Sel = (*S).first;
2893 SourceLocation Loc = (*S).second;
2894 AddSelectorRef(Sel, Record);
2895 AddSourceLocation(Loc, Record);
2896 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002897 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002898}
2899
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002900//===----------------------------------------------------------------------===//
2901// Identifier Table Serialization
2902//===----------------------------------------------------------------------===//
2903
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002904namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002905class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002906 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002907 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002908 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002909 bool IsModule;
2910
Douglas Gregora92193e2009-04-28 21:18:29 +00002911 /// \brief Determines whether this is an "interesting" identifier
2912 /// that needs a full IdentifierInfo structure written into the hash
2913 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002914 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002915 if (II->isPoisoned() ||
2916 II->isExtensionToken() ||
2917 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002918 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002919 II->getFETokenInfo<void>())
2920 return true;
2921
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002922 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002923 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002924
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002925 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002926 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002927 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002928
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002929 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2930 if (!IsModule)
2931 return !shouldIgnoreMacro(Macro, IsModule, PP);
2932 SubmoduleID ModID;
2933 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2934 return true;
2935 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002936
2937 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002938 }
2939
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002940 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2941 SubmoduleID &ModID) {
2942 ModID = 0;
2943 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2944 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2945 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002946 return 0;
2947 }
2948
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002949 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2950 SubmoduleID &ModID) {
2951 if (DefMacroDirective *
2952 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2953 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2954 return DefMD;
2955 return 0;
2956 }
2957
2958 /// \brief Traverses the macro directives history and returns the latest
2959 /// macro that is public and not undefined in the same submodule.
2960 /// A macro that is defined in submodule A and undefined in submodule B,
2961 /// will still be considered as defined/exported from submodule A.
2962 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2963 SubmoduleID &ModID) {
2964 if (!MD)
2965 return 0;
2966
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002967 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002968 bool isUndefined = false;
2969 Optional<bool> isPublic;
2970 for (; MD; MD = MD->getPrevious()) {
2971 if (MD->isHidden())
2972 continue;
2973
2974 SubmoduleID ThisModID = getSubmoduleID(MD);
2975 if (ThisModID == 0) {
2976 isUndefined = false;
2977 isPublic = Optional<bool>();
2978 continue;
2979 }
2980 if (ThisModID != ModID){
2981 ModID = ThisModID;
2982 isUndefined = false;
2983 isPublic = Optional<bool>();
2984 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002985 // We are looking for a definition in a different submodule than the one
2986 // that we started with. If a submodule has re-definitions of the same
2987 // macro, only the last definition will be used as the "exported" one.
2988 if (ModID == OrigModID)
2989 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002990
2991 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2992 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2993 return DefMD;
2994 continue;
2995 }
2996
2997 if (isa<UndefMacroDirective>(MD)) {
2998 isUndefined = true;
2999 continue;
3000 }
3001
3002 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
3003 if (!isPublic.hasValue())
3004 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003005 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003006
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003007 return 0;
3008 }
3009
3010 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003011 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3012 MacroInfo *MI = DefMD->getInfo();
3013 if (unsigned ID = MI->getOwningModuleID())
3014 return ID;
3015 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3016 }
3017 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003018 }
3019
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003020public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003021 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003022 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003024 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003025 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Douglas Gregoreee242f2011-10-27 09:33:13 +00003027 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3028 IdentifierResolver &IdResolver, bool IsModule)
3029 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003030
3031 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003032 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003033 }
Mike Stump1eb44332009-09-09 15:08:12 +00003034
3035 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003036 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003037 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003038 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003039 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003040 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003041 DataLen += 2; // 2 bytes for builtin ID
3042 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003043 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003044 DataLen += 4; // MacroDirectives offset.
3045 if (IsModule) {
3046 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003047 for (DefMacroDirective *
3048 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3049 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003050 DataLen += 4; // MacroInfo ID.
3051 }
3052 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003053 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003054 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003055
Douglas Gregoreee242f2011-10-27 09:33:13 +00003056 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3057 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003058 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003059 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003060 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003061 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003062 // We emit the key length after the data length so that every
3063 // string is preceded by a 16-bit length. This matches the PTH
3064 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003065 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003066 return std::make_pair(KeyLen, DataLen);
3067 }
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Chris Lattner5f9e2722011-07-23 10:55:15 +00003069 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003070 unsigned KeyLen) {
3071 // Record the location of the key data. This is used when generating
3072 // the mapping from persistent IDs to strings.
3073 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003074 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003075 }
Mike Stump1eb44332009-09-09 15:08:12 +00003076
Douglas Gregor7143aab2011-09-01 17:04:32 +00003077 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003078 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003079 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003080 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003081 clang::io::Emit32(Out, ID << 1);
3082 return;
3083 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003084
Douglas Gregora92193e2009-04-28 21:18:29 +00003085 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003086 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3087 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3088 clang::io::Emit16(Out, Bits);
3089 Bits = 0;
3090 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003091 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003092 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003093 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3094 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003095 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003096 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003097 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003098
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003099 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003100 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3101 if (IsModule) {
3102 // Write the IDs of macros coming from different submodules.
3103 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003104 for (DefMacroDirective *
3105 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3106 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3107 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003108 assert(InfoID);
3109 clang::io::Emit32(Out, InfoID);
3110 }
3111 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003112 }
Douglas Gregor13292642011-12-02 15:45:10 +00003113 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003114
Douglas Gregor668c1a42009-04-21 22:25:48 +00003115 // Emit the declaration IDs in reverse order, because the
3116 // IdentifierResolver provides the declarations as they would be
3117 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003118 // "stat"), but the ASTReader adds declarations to the end of the list
3119 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003120 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003121 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3122 IdResolver.end());
Craig Topper09d19ef2013-07-04 03:08:24 +00003123 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003124 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003125 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003126 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3127 }
3128
3129 /// \brief Returns the most recent local decl or the given decl if there are
3130 /// no local ones. The given decl is assumed to be the most recent one.
3131 Decl *getMostRecentLocalDecl(Decl *Orig) {
3132 // The only way a "from AST file" decl would be more recent from a local one
3133 // is if it came from a module.
3134 if (!PP.getLangOpts().Modules)
3135 return Orig;
3136
3137 // Look for a local in the decl chain.
3138 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3139 if (!D->isFromASTFile())
3140 return D;
3141 // If we come up a decl from a (chained-)PCH stop since we won't find a
3142 // local one.
3143 if (D->getOwningModuleID() == 0)
3144 break;
3145 }
3146
3147 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003148 }
3149};
3150} // end anonymous namespace
3151
Sebastian Redl3397c552010-08-18 23:56:27 +00003152/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003153///
3154/// The identifier table consists of a blob containing string data
3155/// (the actual identifiers themselves) and a separate "offsets" index
3156/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003157void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3158 IdentifierResolver &IdResolver,
3159 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003160 using namespace llvm;
3161
3162 // Create and write out the blob that contains the identifier
3163 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003164 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003165 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003166 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003167
Douglas Gregor92b059e2009-04-28 20:33:11 +00003168 // Look for any identifiers that were named while processing the
3169 // headers, but are otherwise not needed. We add these to the hash
3170 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003171 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003172 // file.
3173 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3174 IDEnd = PP.getIdentifierTable().end();
3175 ID != IDEnd; ++ID)
3176 getIdentifierRef(ID->second);
3177
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003178 // Create the on-disk hash table representation. We only store offsets
3179 // for identifiers that appear here for the first time.
3180 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003181 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003182 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3183 ID != IDEnd; ++ID) {
3184 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003185 if (!Chain || !ID->first->isFromAST() ||
3186 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003187 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003188 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003189 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003190
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003191 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003192 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003193 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003194 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003195 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003196 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003197 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003198 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003199 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003200 }
3201
3202 // Create a blob abbreviation
3203 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003204 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003207 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003208
3209 // Write the identifier table
3210 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003211 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003212 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003213 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003214 }
3215
3216 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003218 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3222 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3223
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003224#ifndef NDEBUG
3225 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3226 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3227#endif
3228
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003229 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003230 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003231 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003232 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003233 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003234 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003235}
3236
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003237//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003238// DeclContext's Name Lookup Table Serialization
3239//===----------------------------------------------------------------------===//
3240
3241namespace {
3242// Trait used for the on-disk hash table used in the method pool.
3243class ASTDeclContextNameLookupTrait {
3244 ASTWriter &Writer;
3245
3246public:
3247 typedef DeclarationName key_type;
3248 typedef key_type key_type_ref;
3249
3250 typedef DeclContext::lookup_result data_type;
3251 typedef const data_type& data_type_ref;
3252
3253 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3254
3255 unsigned ComputeHash(DeclarationName Name) {
3256 llvm::FoldingSetNodeID ID;
3257 ID.AddInteger(Name.getNameKind());
3258
3259 switch (Name.getNameKind()) {
3260 case DeclarationName::Identifier:
3261 ID.AddString(Name.getAsIdentifierInfo()->getName());
3262 break;
3263 case DeclarationName::ObjCZeroArgSelector:
3264 case DeclarationName::ObjCOneArgSelector:
3265 case DeclarationName::ObjCMultiArgSelector:
3266 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3267 break;
3268 case DeclarationName::CXXConstructorName:
3269 case DeclarationName::CXXDestructorName:
3270 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003271 break;
3272 case DeclarationName::CXXOperatorName:
3273 ID.AddInteger(Name.getCXXOverloadedOperator());
3274 break;
3275 case DeclarationName::CXXLiteralOperatorName:
3276 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3277 case DeclarationName::CXXUsingDirective:
3278 break;
3279 }
3280
3281 return ID.ComputeHash();
3282 }
3283
3284 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003285 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003286 data_type_ref Lookup) {
3287 unsigned KeyLen = 1;
3288 switch (Name.getNameKind()) {
3289 case DeclarationName::Identifier:
3290 case DeclarationName::ObjCZeroArgSelector:
3291 case DeclarationName::ObjCOneArgSelector:
3292 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003293 case DeclarationName::CXXLiteralOperatorName:
3294 KeyLen += 4;
3295 break;
3296 case DeclarationName::CXXOperatorName:
3297 KeyLen += 1;
3298 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003299 case DeclarationName::CXXConstructorName:
3300 case DeclarationName::CXXDestructorName:
3301 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003302 case DeclarationName::CXXUsingDirective:
3303 break;
3304 }
3305 clang::io::Emit16(Out, KeyLen);
3306
3307 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003308 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003309 clang::io::Emit16(Out, DataLen);
3310
3311 return std::make_pair(KeyLen, DataLen);
3312 }
3313
Chris Lattner5f9e2722011-07-23 10:55:15 +00003314 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003315 using namespace clang::io;
3316
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003317 Emit8(Out, Name.getNameKind());
3318 switch (Name.getNameKind()) {
3319 case DeclarationName::Identifier:
3320 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003321 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003322 case DeclarationName::ObjCZeroArgSelector:
3323 case DeclarationName::ObjCOneArgSelector:
3324 case DeclarationName::ObjCMultiArgSelector:
3325 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003326 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003327 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003328 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3329 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003330 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003331 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003332 case DeclarationName::CXXLiteralOperatorName:
3333 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003334 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003335 case DeclarationName::CXXConstructorName:
3336 case DeclarationName::CXXDestructorName:
3337 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003338 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003339 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003340 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003341
3342 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003343 }
3344
Chris Lattner5f9e2722011-07-23 10:55:15 +00003345 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003346 data_type Lookup, unsigned DataLen) {
3347 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003348 clang::io::Emit16(Out, Lookup.size());
3349 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3350 I != E; ++I)
3351 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003352
3353 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3354 }
3355};
3356} // end anonymous namespace
3357
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003358/// \brief Write the block containing all of the declaration IDs
3359/// visible from the given DeclContext.
3360///
3361/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003362/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003363uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3364 DeclContext *DC) {
3365 if (DC->getPrimaryContext() != DC)
3366 return 0;
3367
3368 // Since there is no name lookup into functions or methods, don't bother to
3369 // build a visible-declarations table for these entities.
3370 if (DC->isFunctionOrMethod())
3371 return 0;
3372
3373 // If not in C++, we perform name lookup for the translation unit via the
3374 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003375 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003376 return 0;
3377
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003378 // Serialize the contents of the mapping used for lookup. Note that,
3379 // although we have two very different code paths, the serialized
3380 // representation is the same for both cases: a declaration name,
3381 // followed by a size, followed by references to the visible
3382 // declarations that have that name.
3383 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003384 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003385 if (!Map || Map->empty())
3386 return 0;
3387
3388 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3389 ASTDeclContextNameLookupTrait Trait(*this);
3390
3391 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003392 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003393 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003394 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3395 D != DEnd; ++D) {
3396 DeclarationName Name = D->first;
3397 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003398 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003399 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3400 // Hash all conversion function names to the same name. The actual
3401 // type information in conversion function name is not used in the
3402 // key (since such type information is not stable across different
3403 // modules), so the intended effect is to coalesce all of the conversion
3404 // functions under a single key.
3405 if (!ConversionName)
3406 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003407 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003408 continue;
3409 }
3410
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003411 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003412 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003413 }
3414
Douglas Gregore5a54b62011-08-30 20:49:19 +00003415 // Add the conversion functions
3416 if (!ConversionDecls.empty()) {
3417 Generator.insert(ConversionName,
3418 DeclContext::lookup_result(ConversionDecls.begin(),
3419 ConversionDecls.end()),
3420 Trait);
3421 }
3422
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003423 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003424 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003425 uint32_t BucketOffset;
3426 {
3427 llvm::raw_svector_ostream Out(LookupTable);
3428 // Make sure that no bucket is at offset 0
3429 clang::io::Emit32(Out, 0);
3430 BucketOffset = Generator.Emit(Out, Trait);
3431 }
3432
3433 // Write the lookup table
3434 RecordData Record;
3435 Record.push_back(DECL_CONTEXT_VISIBLE);
3436 Record.push_back(BucketOffset);
3437 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3438 LookupTable.str());
3439
3440 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3441 ++NumVisibleDeclContexts;
3442 return Offset;
3443}
3444
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003445/// \brief Write an UPDATE_VISIBLE block for the given context.
3446///
3447/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3448/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003449/// (in C++), for namespaces, and for classes with forward-declared unscoped
3450/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003451void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003452 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3453 if (!Map || Map->empty())
3454 return;
3455
3456 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3457 ASTDeclContextNameLookupTrait Trait(*this);
3458
3459 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003460 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3461 D != DEnd; ++D) {
3462 DeclarationName Name = D->first;
3463 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003464 // For any name that appears in this table, the results are complete, i.e.
3465 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003466 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003467 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003468 }
3469
3470 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003471 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003472 uint32_t BucketOffset;
3473 {
3474 llvm::raw_svector_ostream Out(LookupTable);
3475 // Make sure that no bucket is at offset 0
3476 clang::io::Emit32(Out, 0);
3477 BucketOffset = Generator.Emit(Out, Trait);
3478 }
3479
3480 // Write the lookup table
3481 RecordData Record;
3482 Record.push_back(UPDATE_VISIBLE);
3483 Record.push_back(getDeclID(cast<Decl>(DC)));
3484 Record.push_back(BucketOffset);
3485 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3486}
3487
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003488/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3489void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3490 RecordData Record;
3491 Record.push_back(Opts.fp_contract);
3492 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3493}
3494
3495/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3496void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003497 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003498 return;
3499
3500 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3501 RecordData Record;
3502#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3503#include "clang/Basic/OpenCLExtensions.def"
3504 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3505}
3506
Douglas Gregor2171bf12012-01-15 16:58:34 +00003507void ASTWriter::WriteRedeclarations() {
3508 RecordData LocalRedeclChains;
3509 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3510
3511 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3512 Decl *First = Redeclarations[I];
3513 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3514
3515 Decl *MostRecent = First->getMostRecentDecl();
3516
3517 // If we only have a single declaration, there is no point in storing
3518 // a redeclaration chain.
3519 if (First == MostRecent)
3520 continue;
3521
3522 unsigned Offset = LocalRedeclChains.size();
3523 unsigned Size = 0;
3524 LocalRedeclChains.push_back(0); // Placeholder for the size.
3525
3526 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003527 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003528 Prev = Prev->getPreviousDecl()) {
3529 if (!Prev->isFromASTFile()) {
3530 AddDeclRef(Prev, LocalRedeclChains);
3531 ++Size;
3532 }
3533 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003534
3535 if (!First->isFromASTFile() && Chain) {
3536 Decl *FirstFromAST = MostRecent;
3537 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3538 if (Prev->isFromASTFile())
3539 FirstFromAST = Prev;
3540 }
3541
3542 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3543 }
3544
Douglas Gregor2171bf12012-01-15 16:58:34 +00003545 LocalRedeclChains[Offset] = Size;
3546
3547 // Reverse the set of local redeclarations, so that we store them in
3548 // order (since we found them in reverse order).
3549 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3550
Douglas Gregoraa945902013-02-18 15:53:43 +00003551 // Add the mapping from the first ID from the AST to the set of local
3552 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003553 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3554 LocalRedeclsMap.push_back(Info);
3555
3556 assert(N == Redeclarations.size() &&
3557 "Deserialized a declaration we shouldn't have");
3558 }
3559
3560 if (LocalRedeclChains.empty())
3561 return;
3562
3563 // Sort the local redeclarations map by the first declaration ID,
3564 // since the reader will be performing binary searches on this information.
3565 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3566
3567 // Emit the local redeclarations map.
3568 using namespace llvm;
3569 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3570 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3573 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3574
3575 RecordData Record;
3576 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3577 Record.push_back(LocalRedeclsMap.size());
3578 Stream.EmitRecordWithBlob(AbbrevID, Record,
3579 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3580 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3581
3582 // Emit the redeclaration chains.
3583 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3584}
3585
Douglas Gregorcff9f262012-01-27 01:47:08 +00003586void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003587 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003588 RecordData Categories;
3589
3590 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3591 unsigned Size = 0;
3592 unsigned StartIndex = Categories.size();
3593
3594 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3595
3596 // Allocate space for the size.
3597 Categories.push_back(0);
3598
3599 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003600 for (ObjCInterfaceDecl::known_categories_iterator
3601 Cat = Class->known_categories_begin(),
3602 CatEnd = Class->known_categories_end();
3603 Cat != CatEnd; ++Cat, ++Size) {
3604 assert(getDeclID(*Cat) != 0 && "Bogus category");
3605 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003606 }
3607
3608 // Update the size.
3609 Categories[StartIndex] = Size;
3610
3611 // Record this interface -> category map.
3612 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3613 CategoriesMap.push_back(CatInfo);
3614 }
3615
3616 // Sort the categories map by the definition ID, since the reader will be
3617 // performing binary searches on this information.
3618 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3619
3620 // Emit the categories map.
3621 using namespace llvm;
3622 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3623 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3626 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3627
3628 RecordData Record;
3629 Record.push_back(OBJC_CATEGORIES_MAP);
3630 Record.push_back(CategoriesMap.size());
3631 Stream.EmitRecordWithBlob(AbbrevID, Record,
3632 reinterpret_cast<char*>(CategoriesMap.data()),
3633 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3634
3635 // Emit the category lists.
3636 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3637}
3638
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003639void ASTWriter::WriteMergedDecls() {
3640 if (!Chain || Chain->MergedDecls.empty())
3641 return;
3642
3643 RecordData Record;
3644 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3645 IEnd = Chain->MergedDecls.end();
3646 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003647 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003648 : getDeclID(I->first);
3649 assert(CanonID && "Merged declaration not known?");
3650
3651 Record.push_back(CanonID);
3652 Record.push_back(I->second.size());
3653 Record.append(I->second.begin(), I->second.end());
3654 }
3655 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3656}
3657
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003658//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003659// General Serialization Routines
3660//===----------------------------------------------------------------------===//
3661
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003662/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003663void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3664 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003665 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003666 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3667 e = Attrs.end(); i != e; ++i){
3668 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003669 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003670 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003671
Sean Huntcf807c42010-08-18 23:23:40 +00003672#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003673
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003674 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003675}
3676
John McCallaeeacf72013-05-03 00:10:13 +00003677void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3678 AddSourceLocation(Tok.getLocation(), Record);
3679 Record.push_back(Tok.getLength());
3680
3681 // FIXME: When reading literal tokens, reconstruct the literal pointer
3682 // if it is needed.
3683 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3684 // FIXME: Should translate token kind to a stable encoding.
3685 Record.push_back(Tok.getKind());
3686 // FIXME: Should translate token flags to a stable encoding.
3687 Record.push_back(Tok.getFlags());
3688}
3689
Chris Lattner5f9e2722011-07-23 10:55:15 +00003690void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003691 Record.push_back(Str.size());
3692 Record.insert(Record.end(), Str.begin(), Str.end());
3693}
3694
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003695void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3696 RecordDataImpl &Record) {
3697 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003698 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003699 Record.push_back(*Minor + 1);
3700 else
3701 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003702 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003703 Record.push_back(*Subminor + 1);
3704 else
3705 Record.push_back(0);
3706}
3707
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003708/// \brief Note that the identifier II occurs at the given offset
3709/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003710void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003711 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003712 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003713 // up earlier in the chain and thus don't need an offset.
3714 if (ID >= FirstIdentID)
3715 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003716}
3717
Douglas Gregor83941df2009-04-25 17:48:32 +00003718/// \brief Note that the selector Sel occurs at the given offset
3719/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003720void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003721 unsigned ID = SelectorIDs[Sel];
3722 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003723 // Don't record offsets for selectors that are also available in a different
3724 // file.
3725 if (ID < FirstSelectorID)
3726 return;
3727 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003728}
3729
Sebastian Redla4232eb2010-08-18 23:56:21 +00003730ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003731 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003732 WritingAST(false), DoneWritingDeclsAndTypes(false),
3733 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003734 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003735 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003736 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3737 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003738 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3739 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003740 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003741 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003742 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003743 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003744 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003745 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003746 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3747 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3748 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003749 DeclTypedefAbbrev(0),
3750 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3751 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003752{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003753}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003754
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003755ASTWriter::~ASTWriter() {
3756 for (FileDeclIDsTy::iterator
3757 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3758 delete I->second;
3759}
3760
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003761void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003762 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003763 Module *WritingModule, StringRef isysroot,
3764 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003765 WritingAST = true;
3766
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003767 ASTHasCompilerErrors = hasErrors;
3768
Douglas Gregor2cf26342009-04-09 22:27:44 +00003769 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003770 Stream.Emit((unsigned)'C', 8);
3771 Stream.Emit((unsigned)'P', 8);
3772 Stream.Emit((unsigned)'C', 8);
3773 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003774
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003775 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003776
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003777 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003778 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003779 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003780 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003781 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003782 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003783 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003784
3785 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003786}
3787
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003788template<typename Vector>
3789static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3790 ASTWriter::RecordData &Record) {
3791 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3792 I != E; ++I) {
3793 Writer.AddDeclRef(*I, Record);
3794 }
3795}
3796
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003797void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003798 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003799 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003800 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003801 using namespace llvm;
3802
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003803 bool isModule = WritingModule != 0;
3804
Douglas Gregorecc2c092011-12-01 22:20:10 +00003805 // Make sure that the AST reader knows to finalize itself.
3806 if (Chain)
3807 Chain->finalizeForWriting();
3808
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003809 ASTContext &Context = SemaRef.Context;
3810 Preprocessor &PP = SemaRef.PP;
3811
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003812 // Set up predefined declaration IDs.
3813 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003814 if (Context.ObjCIdDecl)
3815 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003816 if (Context.ObjCSelDecl)
3817 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003818 if (Context.ObjCClassDecl)
3819 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003820 if (Context.ObjCProtocolClassDecl)
3821 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003822 if (Context.Int128Decl)
3823 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3824 if (Context.UInt128Decl)
3825 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003826 if (Context.ObjCInstanceTypeDecl)
3827 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003828 if (Context.BuiltinVaListDecl)
3829 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3830
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003831 if (!Chain) {
3832 // Make sure that we emit IdentifierInfos (and any attached
3833 // declarations) for builtins. We don't need to do this when we're
3834 // emitting chained PCH files, because all of the builtins will be
3835 // in the original PCH file.
3836 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003837 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003838 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003839 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003840 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003841 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3842 getIdentifierRef(&Table.get(BuiltinNames[I]));
3843 }
3844
Douglas Gregoreee242f2011-10-27 09:33:13 +00003845 // If there are any out-of-date identifiers, bring them up to date.
3846 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003847 // Find out-of-date identifiers.
3848 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003849 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3850 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003851 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003852 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003853 OutOfDate.push_back(ID->second);
3854 }
3855
3856 // Update the out-of-date identifiers.
3857 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3858 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3859 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003860 }
3861
Chris Lattner63d65f82009-09-08 18:19:27 +00003862 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003863 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003864 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003865 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003866 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003867
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003868 // Build a record containing all of the file scoped decls in this file.
3869 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003870 if (!isModule)
3871 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3872 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003873
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003874 // Build a record containing all of the delegating constructors we still need
3875 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003876 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003877 if (!isModule)
3878 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003879
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003880 // Write the set of weak, undeclared identifiers. We always write the
3881 // entire table, since later PCH files in a PCH chain are only interested in
3882 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003883 RecordData WeakUndeclaredIdentifiers;
3884 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003885 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003886 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3887 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3888 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3889 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3890 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3891 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3892 }
3893 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003894
Richard Smith5ea6ef42013-01-10 23:43:47 +00003895 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003896 // declarations in this header file. Generally, this record will be
3897 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003898 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003899 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003900 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003901 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003902 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3903 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003904 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003905 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003906 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003907 }
3908
Douglas Gregorb81c1702009-04-27 20:06:05 +00003909 // Build a record containing all of the ext_vector declarations.
3910 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003911 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003912
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003913 // Build a record containing all of the VTable uses information.
3914 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003915 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003916 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3917 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3918 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3919 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3920 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003921 }
3922
3923 // Build a record containing all of dynamic classes declarations.
3924 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003925 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003926
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003927 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003928 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003929 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003930 I = SemaRef.PendingInstantiations.begin(),
3931 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3932 AddDeclRef(I->first, PendingInstantiations);
3933 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003934 }
3935 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3936 "There are local ones at end of translation unit!");
3937
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003938 // Build a record containing some declaration references.
3939 RecordData SemaDeclRefs;
3940 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3941 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3942 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3943 }
3944
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003945 RecordData CUDASpecialDeclRefs;
3946 if (Context.getcudaConfigureCallDecl()) {
3947 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3948 }
3949
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003950 // Build a record containing all of the known namespaces.
3951 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003952 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003953 I = SemaRef.KnownNamespaces.begin(),
3954 IEnd = SemaRef.KnownNamespaces.end();
3955 I != IEnd; ++I) {
3956 if (!I->second)
3957 AddDeclRef(I->first, KnownNamespaces);
3958 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003959
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003960 // Build a record of all used, undefined objects that require definitions.
3961 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003962
3963 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003964 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003965 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3966 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003967 AddDeclRef(I->first, UndefinedButUsed);
3968 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003969 }
3970
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003971 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003972 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003973
Sebastian Redl3397c552010-08-18 23:56:27 +00003974 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003975 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003976 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003977
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003978 // This is so that older clang versions, before the introduction
3979 // of the control block, can read and reject the newer PCH format.
3980 Record.clear();
3981 Record.push_back(VERSION_MAJOR);
3982 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3983
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003984 // Create a lexical update block containing all of the declarations in the
3985 // translation unit that do not come from other AST files.
3986 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3987 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3988 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3989 E = TU->noload_decls_end();
3990 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003991 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003992 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003993 }
3994
3995 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3996 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3997 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3998 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3999 Record.clear();
4000 Record.push_back(TU_UPDATE_LEXICAL);
4001 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4002 data(NewGlobalDecls));
4003
4004 // And a visible updates block for the translation unit.
4005 Abv = new llvm::BitCodeAbbrev();
4006 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4007 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4008 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4009 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4010 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4011 WriteDeclContextVisibleUpdate(TU);
4012
4013 // If the translation unit has an anonymous namespace, and we don't already
4014 // have an update block for it, write it as an update block.
4015 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4016 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4017 if (Record.empty()) {
4018 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004019 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004020 }
4021 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004022
4023 // Make sure visible decls, added to DeclContexts previously loaded from
4024 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004025 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004026 I = UpdatingVisibleDecls.begin(),
4027 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4028 GetDeclRef(*I);
4029 }
4030
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004031 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004032 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004033
Douglas Gregora119da02011-08-02 16:26:37 +00004034 // Form the record of special types.
4035 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004036 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004037 AddTypeRef(Context.getFILEType(), SpecialTypes);
4038 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4039 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4040 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4041 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004042 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004043 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004044
Douglas Gregor366809a2009-04-26 03:49:13 +00004045 // Keep writing types and declarations until all types and
4046 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004047 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004048 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004049 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4050 E = DeclsToRewrite.end();
4051 I != E; ++I)
4052 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004053 while (!DeclTypesToEmit.empty()) {
4054 DeclOrType DOT = DeclTypesToEmit.front();
4055 DeclTypesToEmit.pop();
4056 if (DOT.isType())
4057 WriteType(DOT.getType());
4058 else
4059 WriteDecl(Context, DOT.getDecl());
4060 }
4061 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004062
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004063 DoneWritingDeclsAndTypes = true;
4064
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004065 WriteFileDeclIDsMap();
4066 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004067 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004068
4069 if (Chain) {
4070 // Write the mapping information describing our module dependencies and how
4071 // each of those modules were mapped into our own offset/ID space, so that
4072 // the reader can build the appropriate mapping to its own offset/ID space.
4073 // The map consists solely of a blob with the following format:
4074 // *(module-name-len:i16 module-name:len*i8
4075 // source-location-offset:i32
4076 // identifier-id:i32
4077 // preprocessed-entity-id:i32
4078 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004079 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004080 // selector-id:i32
4081 // declaration-id:i32
4082 // c++-base-specifiers-id:i32
4083 // type-id:i32)
4084 //
4085 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4086 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4088 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004089 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004090 {
4091 llvm::raw_svector_ostream Out(Buffer);
4092 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004093 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004094 M != MEnd; ++M) {
4095 StringRef FileName = (*M)->FileName;
4096 io::Emit16(Out, FileName.size());
4097 Out.write(FileName.data(), FileName.size());
4098 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4099 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004100 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004101 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004102 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004103 io::Emit32(Out, (*M)->BaseSelectorID);
4104 io::Emit32(Out, (*M)->BaseDeclID);
4105 io::Emit32(Out, (*M)->BaseTypeIndex);
4106 }
4107 }
4108 Record.clear();
4109 Record.push_back(MODULE_OFFSET_MAP);
4110 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4111 Buffer.data(), Buffer.size());
4112 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004113 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004114 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004115 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004116 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004117 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004118 WriteFPPragmaOptions(SemaRef.getFPOptions());
4119 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004120
Sebastian Redl1476ed42010-07-16 16:36:56 +00004121 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004122 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004123
Anders Carlssonc8505782011-03-06 18:41:18 +00004124 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004125
Douglas Gregore209e502011-12-06 01:10:29 +00004126 // If we're emitting a module, write out the submodule information.
4127 if (WritingModule)
4128 WriteSubmodules(WritingModule);
4129
Douglas Gregora119da02011-08-02 16:26:37 +00004130 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4131
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004132 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004133 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004134 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004135
4136 // Write the record containing tentative definitions.
4137 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004138 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004139
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004140 // Write the record containing unused file scoped decls.
4141 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004142 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004143
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004144 // Write the record containing weak undeclared identifiers.
4145 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004146 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004147 WeakUndeclaredIdentifiers);
4148
Richard Smith5ea6ef42013-01-10 23:43:47 +00004149 // Write the record containing locally-scoped extern "C" definitions.
4150 if (!LocallyScopedExternCDecls.empty())
4151 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4152 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004153
4154 // Write the record containing ext_vector type names.
4155 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004156 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004157
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004158 // Write the record containing VTable uses information.
4159 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004160 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004161
4162 // Write the record containing dynamic classes declarations.
4163 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004164 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004165
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004166 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004167 if (!PendingInstantiations.empty())
4168 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004169
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004170 // Write the record containing declaration references of Sema.
4171 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004172 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004173
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004174 // Write the record containing CUDA-specific declaration references.
4175 if (!CUDASpecialDeclRefs.empty())
4176 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004177
4178 // Write the delegating constructors.
4179 if (!DelegatingCtorDecls.empty())
4180 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004181
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004182 // Write the known namespaces.
4183 if (!KnownNamespaces.empty())
4184 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004185
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004186 // Write the undefined internal functions and variables, and inline functions.
4187 if (!UndefinedButUsed.empty())
4188 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004189
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004190 // Write the visible updates to DeclContexts.
4191 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4192 I = UpdatedDeclContexts.begin(),
4193 E = UpdatedDeclContexts.end();
4194 I != E; ++I)
4195 WriteDeclContextVisibleUpdate(*I);
4196
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004197 if (!WritingModule) {
4198 // Write the submodules that were imported, if any.
4199 RecordData ImportedModules;
4200 for (ASTContext::import_iterator I = Context.local_import_begin(),
4201 IEnd = Context.local_import_end();
4202 I != IEnd; ++I) {
4203 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4204 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4205 }
4206 if (!ImportedModules.empty()) {
4207 // Sort module IDs.
4208 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4209
4210 // Unique module IDs.
4211 ImportedModules.erase(std::unique(ImportedModules.begin(),
4212 ImportedModules.end()),
4213 ImportedModules.end());
4214
4215 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4216 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004217 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004218
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004219 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004220 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004221 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004222 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004223 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004224
Douglas Gregor3e1af842009-04-17 22:13:46 +00004225 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004226 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004227 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004228 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004229 Record.push_back(NumLexicalDeclContexts);
4230 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004231 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004232 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004233}
4234
Douglas Gregor61c5e342011-09-17 00:05:03 +00004235/// \brief Go through the declaration update blocks and resolve declaration
4236/// pointers into declaration IDs.
4237void ASTWriter::ResolveDeclUpdatesBlocks() {
4238 for (DeclUpdateMap::iterator
4239 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4240 const Decl *D = I->first;
4241 UpdateRecord &URec = I->second;
4242
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004243 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004244 continue; // The decl will be written completely
4245
4246 unsigned Idx = 0, N = URec.size();
4247 while (Idx < N) {
4248 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004249 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4250 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4251 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4252 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4253 ++Idx;
4254 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004255
Douglas Gregor61c5e342011-09-17 00:05:03 +00004256 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4257 ++Idx;
4258 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004259
4260 case UPD_CXX_DEDUCED_RETURN_TYPE:
4261 URec[Idx] = GetOrCreateTypeID(
4262 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4263 ++Idx;
4264 break;
Douglas Gregor61c5e342011-09-17 00:05:03 +00004265 }
4266 }
4267 }
4268}
4269
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004270void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004271 if (DeclUpdates.empty())
4272 return;
4273
4274 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004275 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004276 for (DeclUpdateMap::iterator
4277 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4278 const Decl *D = I->first;
4279 UpdateRecord &URec = I->second;
4280
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004281 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004282 continue; // The decl will be written completely,no need to store updates.
4283
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004284 uint64_t Offset = Stream.GetCurrentBitNo();
4285 Stream.EmitRecord(DECL_UPDATES, URec);
4286
4287 OffsetsRecord.push_back(GetDeclRef(D));
4288 OffsetsRecord.push_back(Offset);
4289 }
4290 Stream.ExitBlock();
4291 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4292}
4293
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004294void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004295 if (ReplacedDecls.empty())
4296 return;
4297
4298 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004299 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4300 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004301 Record.push_back(I->ID);
4302 Record.push_back(I->Offset);
4303 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004304 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004305 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004306}
4307
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004308void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004309 Record.push_back(Loc.getRawEncoding());
4310}
4311
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004312void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004313 AddSourceLocation(Range.getBegin(), Record);
4314 AddSourceLocation(Range.getEnd(), Record);
4315}
4316
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004317void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004318 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004319 const uint64_t *Words = Value.getRawData();
4320 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004321}
4322
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004323void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004324 Record.push_back(Value.isUnsigned());
4325 AddAPInt(Value, Record);
4326}
4327
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004328void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004329 AddAPInt(Value.bitcastToAPInt(), Record);
4330}
4331
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004332void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004333 Record.push_back(getIdentifierRef(II));
4334}
4335
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004336IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004337 if (II == 0)
4338 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004339
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004340 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004341 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004342 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004343 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004344}
4345
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004346MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004347 // Don't emit builtin macros like __LINE__ to the AST file unless they
4348 // have been redefined by the header (in which case they are not
4349 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004350 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004351 return 0;
4352
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004353 MacroID &ID = MacroIDs[MI];
4354 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004355 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004356 MacroInfoToEmitData Info = { Name, MI, ID };
4357 MacroInfosToEmit.push_back(Info);
4358 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004359 return ID;
4360}
4361
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004362MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4363 if (MI == 0 || MI->isBuiltinMacro())
4364 return 0;
4365
4366 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4367 return MacroIDs[MI];
4368}
4369
4370uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4371 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4372 return IdentMacroDirectivesOffsetMap[Name];
4373}
4374
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004375void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004376 Record.push_back(getSelectorRef(SelRef));
4377}
4378
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004379SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004380 if (Sel.getAsOpaquePtr() == 0) {
4381 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004382 }
4383
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004384 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004385 if (SID == 0 && Chain) {
4386 // This might trigger a ReadSelector callback, which will set the ID for
4387 // this selector.
4388 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004389 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004390 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004391 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004392 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004393 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004394 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004395 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004396}
4397
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004398void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004399 AddDeclRef(Temp->getDestructor(), Record);
4400}
4401
Douglas Gregor7c789c12010-10-29 22:39:52 +00004402void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4403 CXXBaseSpecifier const *BasesEnd,
4404 RecordDataImpl &Record) {
4405 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4406 CXXBaseSpecifiersToWrite.push_back(
4407 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4408 Bases, BasesEnd));
4409 Record.push_back(NextCXXBaseSpecifiersID++);
4410}
4411
Sebastian Redla4232eb2010-08-18 23:56:21 +00004412void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004413 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004414 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004415 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004416 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004417 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004418 break;
4419 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004420 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004421 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004422 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004423 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004424 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004425 break;
4426 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004427 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004428 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004429 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004430 break;
John McCall833ca992009-10-29 08:12:44 +00004431 case TemplateArgument::Null:
4432 case TemplateArgument::Integral:
4433 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004434 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004435 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004436 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004437 break;
4438 }
4439}
4440
Sebastian Redla4232eb2010-08-18 23:56:21 +00004441void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004442 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004443 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004444
4445 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4446 bool InfoHasSameExpr
4447 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4448 Record.push_back(InfoHasSameExpr);
4449 if (InfoHasSameExpr)
4450 return; // Avoid storing the same expr twice.
4451 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004452 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4453 Record);
4454}
4455
Douglas Gregordc355712011-02-25 00:36:19 +00004456void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4457 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004458 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004459 AddTypeRef(QualType(), Record);
4460 return;
4461 }
4462
Douglas Gregordc355712011-02-25 00:36:19 +00004463 AddTypeLoc(TInfo->getTypeLoc(), Record);
4464}
4465
4466void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4467 AddTypeRef(TL.getType(), Record);
4468
John McCalla1ee0c52009-10-16 21:56:05 +00004469 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004470 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004471 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004472}
4473
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004474void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004475 Record.push_back(GetOrCreateTypeID(T));
4476}
4477
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004478TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004479 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004480 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004481 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4482}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004483
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004484TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004485 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004486 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004487 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004488}
4489
4490TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4491 if (T.isNull())
4492 return TypeIdx();
4493 assert(!T.getLocalFastQualifiers());
4494
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004495 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004496 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004497 if (DoneWritingDeclsAndTypes) {
4498 assert(0 && "New type seen after serializing all the types to emit!");
4499 return TypeIdx();
4500 }
4501
Douglas Gregor366809a2009-04-26 03:49:13 +00004502 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004503 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004504 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004505 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004506 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004507 return Idx;
4508}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004509
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004510TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004511 if (T.isNull())
4512 return TypeIdx();
4513 assert(!T.getLocalFastQualifiers());
4514
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004515 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4516 assert(I != TypeIdxs.end() && "Type not emitted!");
4517 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004518}
4519
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004520void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004521 Record.push_back(GetDeclRef(D));
4522}
4523
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004524DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004525 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4526
Douglas Gregor2cf26342009-04-09 22:27:44 +00004527 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004528 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004529 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004530
4531 // If D comes from an AST file, its declaration ID is already known and
4532 // fixed.
4533 if (D->isFromASTFile())
4534 return D->getGlobalID();
4535
Douglas Gregor97475832010-10-05 18:37:06 +00004536 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004537 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004538 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004539 if (DoneWritingDeclsAndTypes) {
4540 assert(0 && "New decl seen after serializing all the decls to emit!");
4541 return 0;
4542 }
4543
Douglas Gregor2cf26342009-04-09 22:27:44 +00004544 // We haven't seen this declaration before. Give it a new ID and
4545 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004546 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004547 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004548 }
4549
Sebastian Redl681d7232010-07-27 00:17:23 +00004550 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004551}
4552
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004553DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004554 if (D == 0)
4555 return 0;
4556
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004557 // If D comes from an AST file, its declaration ID is already known and
4558 // fixed.
4559 if (D->isFromASTFile())
4560 return D->getGlobalID();
4561
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004562 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4563 return DeclIDs[D];
4564}
4565
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004566static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4567 std::pair<unsigned, serialization::DeclID> R) {
4568 return L.first < R.first;
4569}
4570
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004571void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004572 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004573 assert(D);
4574
4575 SourceLocation Loc = D->getLocation();
4576 if (Loc.isInvalid())
4577 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004578
4579 // We only keep track of the file-level declarations of each file.
4580 if (!D->getLexicalDeclContext()->isFileContext())
4581 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004582 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4583 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004584 if (isa<ParmVarDecl>(D))
4585 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004586
4587 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004588 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004589 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004590 FileID FID;
4591 unsigned Offset;
4592 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004593 if (FID.isInvalid())
4594 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004595 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004596
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004597 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004598 if (!Info)
4599 Info = new DeclIDInFileInfo();
4600
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004601 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004602 LocDeclIDsTy &Decls = Info->DeclIDs;
4603
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004604 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004605 Decls.push_back(LocDecl);
4606 return;
4607 }
4608
4609 LocDeclIDsTy::iterator
4610 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4611
4612 Decls.insert(I, LocDecl);
4613}
4614
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004615void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004616 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004617 Record.push_back(Name.getNameKind());
4618 switch (Name.getNameKind()) {
4619 case DeclarationName::Identifier:
4620 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4621 break;
4622
4623 case DeclarationName::ObjCZeroArgSelector:
4624 case DeclarationName::ObjCOneArgSelector:
4625 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004626 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004627 break;
4628
4629 case DeclarationName::CXXConstructorName:
4630 case DeclarationName::CXXDestructorName:
4631 case DeclarationName::CXXConversionFunctionName:
4632 AddTypeRef(Name.getCXXNameType(), Record);
4633 break;
4634
4635 case DeclarationName::CXXOperatorName:
4636 Record.push_back(Name.getCXXOverloadedOperator());
4637 break;
4638
Sean Hunt3e518bd2009-11-29 07:34:05 +00004639 case DeclarationName::CXXLiteralOperatorName:
4640 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4641 break;
4642
Douglas Gregor2cf26342009-04-09 22:27:44 +00004643 case DeclarationName::CXXUsingDirective:
4644 // No extra data to emit
4645 break;
4646 }
4647}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004648
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004649void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004650 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004651 switch (Name.getNameKind()) {
4652 case DeclarationName::CXXConstructorName:
4653 case DeclarationName::CXXDestructorName:
4654 case DeclarationName::CXXConversionFunctionName:
4655 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4656 break;
4657
4658 case DeclarationName::CXXOperatorName:
4659 AddSourceLocation(
4660 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4661 Record);
4662 AddSourceLocation(
4663 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4664 Record);
4665 break;
4666
4667 case DeclarationName::CXXLiteralOperatorName:
4668 AddSourceLocation(
4669 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4670 Record);
4671 break;
4672
4673 case DeclarationName::Identifier:
4674 case DeclarationName::ObjCZeroArgSelector:
4675 case DeclarationName::ObjCOneArgSelector:
4676 case DeclarationName::ObjCMultiArgSelector:
4677 case DeclarationName::CXXUsingDirective:
4678 break;
4679 }
4680}
4681
4682void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004683 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004684 AddDeclarationName(NameInfo.getName(), Record);
4685 AddSourceLocation(NameInfo.getLoc(), Record);
4686 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4687}
4688
4689void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004690 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004691 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004692 Record.push_back(Info.NumTemplParamLists);
4693 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4694 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4695}
4696
Sebastian Redla4232eb2010-08-18 23:56:21 +00004697void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004698 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004699 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004700 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004701 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004702
4703 // Push each of the NNS's onto a stack for serialization in reverse order.
4704 while (NNS) {
4705 NestedNames.push_back(NNS);
4706 NNS = NNS->getPrefix();
4707 }
4708
4709 Record.push_back(NestedNames.size());
4710 while(!NestedNames.empty()) {
4711 NNS = NestedNames.pop_back_val();
4712 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4713 Record.push_back(Kind);
4714 switch (Kind) {
4715 case NestedNameSpecifier::Identifier:
4716 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4717 break;
4718
4719 case NestedNameSpecifier::Namespace:
4720 AddDeclRef(NNS->getAsNamespace(), Record);
4721 break;
4722
Douglas Gregor14aba762011-02-24 02:36:08 +00004723 case NestedNameSpecifier::NamespaceAlias:
4724 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4725 break;
4726
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004727 case NestedNameSpecifier::TypeSpec:
4728 case NestedNameSpecifier::TypeSpecWithTemplate:
4729 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4730 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4731 break;
4732
4733 case NestedNameSpecifier::Global:
4734 // Don't need to write an associated value.
4735 break;
4736 }
4737 }
4738}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004739
Douglas Gregordc355712011-02-25 00:36:19 +00004740void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4741 RecordDataImpl &Record) {
4742 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004743 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004744 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004745
4746 // Push each of the nested-name-specifiers's onto a stack for
4747 // serialization in reverse order.
4748 while (NNS) {
4749 NestedNames.push_back(NNS);
4750 NNS = NNS.getPrefix();
4751 }
4752
4753 Record.push_back(NestedNames.size());
4754 while(!NestedNames.empty()) {
4755 NNS = NestedNames.pop_back_val();
4756 NestedNameSpecifier::SpecifierKind Kind
4757 = NNS.getNestedNameSpecifier()->getKind();
4758 Record.push_back(Kind);
4759 switch (Kind) {
4760 case NestedNameSpecifier::Identifier:
4761 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4762 AddSourceRange(NNS.getLocalSourceRange(), Record);
4763 break;
4764
4765 case NestedNameSpecifier::Namespace:
4766 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4767 AddSourceRange(NNS.getLocalSourceRange(), Record);
4768 break;
4769
4770 case NestedNameSpecifier::NamespaceAlias:
4771 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4772 AddSourceRange(NNS.getLocalSourceRange(), Record);
4773 break;
4774
4775 case NestedNameSpecifier::TypeSpec:
4776 case NestedNameSpecifier::TypeSpecWithTemplate:
4777 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4778 AddTypeLoc(NNS.getTypeLoc(), Record);
4779 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4780 break;
4781
4782 case NestedNameSpecifier::Global:
4783 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4784 break;
4785 }
4786 }
4787}
4788
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004789void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004790 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004791 Record.push_back(Kind);
4792 switch (Kind) {
4793 case TemplateName::Template:
4794 AddDeclRef(Name.getAsTemplateDecl(), Record);
4795 break;
4796
4797 case TemplateName::OverloadedTemplate: {
4798 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4799 Record.push_back(OvT->size());
4800 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4801 I != E; ++I)
4802 AddDeclRef(*I, Record);
4803 break;
4804 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004805
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004806 case TemplateName::QualifiedTemplate: {
4807 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4808 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4809 Record.push_back(QualT->hasTemplateKeyword());
4810 AddDeclRef(QualT->getTemplateDecl(), Record);
4811 break;
4812 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004813
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004814 case TemplateName::DependentTemplate: {
4815 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4816 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4817 Record.push_back(DepT->isIdentifier());
4818 if (DepT->isIdentifier())
4819 AddIdentifierRef(DepT->getIdentifier(), Record);
4820 else
4821 Record.push_back(DepT->getOperator());
4822 break;
4823 }
John McCall14606042011-06-30 08:33:18 +00004824
4825 case TemplateName::SubstTemplateTemplateParm: {
4826 SubstTemplateTemplateParmStorage *subst
4827 = Name.getAsSubstTemplateTemplateParm();
4828 AddDeclRef(subst->getParameter(), Record);
4829 AddTemplateName(subst->getReplacement(), Record);
4830 break;
4831 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004832
4833 case TemplateName::SubstTemplateTemplateParmPack: {
4834 SubstTemplateTemplateParmPackStorage *SubstPack
4835 = Name.getAsSubstTemplateTemplateParmPack();
4836 AddDeclRef(SubstPack->getParameterPack(), Record);
4837 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4838 break;
4839 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004840 }
4841}
4842
Michael J. Spencer20249a12010-10-21 03:16:25 +00004843void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004844 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004845 Record.push_back(Arg.getKind());
4846 switch (Arg.getKind()) {
4847 case TemplateArgument::Null:
4848 break;
4849 case TemplateArgument::Type:
4850 AddTypeRef(Arg.getAsType(), Record);
4851 break;
4852 case TemplateArgument::Declaration:
4853 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004854 Record.push_back(Arg.isDeclForReferenceParam());
4855 break;
4856 case TemplateArgument::NullPtr:
4857 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004858 break;
4859 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004860 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004861 AddTypeRef(Arg.getIntegralType(), Record);
4862 break;
4863 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004864 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4865 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004866 case TemplateArgument::TemplateExpansion:
4867 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004868 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004869 Record.push_back(*NumExpansions + 1);
4870 else
4871 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004872 break;
4873 case TemplateArgument::Expression:
4874 AddStmt(Arg.getAsExpr());
4875 break;
4876 case TemplateArgument::Pack:
4877 Record.push_back(Arg.pack_size());
4878 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4879 I != E; ++I)
4880 AddTemplateArgument(*I, Record);
4881 break;
4882 }
4883}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004884
4885void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004886ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004887 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004888 assert(TemplateParams && "No TemplateParams!");
4889 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4890 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4891 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4892 Record.push_back(TemplateParams->size());
4893 for (TemplateParameterList::const_iterator
4894 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4895 P != PEnd; ++P)
4896 AddDeclRef(*P, Record);
4897}
4898
4899/// \brief Emit a template argument list.
4900void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004901ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004902 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004903 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004904 Record.push_back(TemplateArgs->size());
4905 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004906 AddTemplateArgument(TemplateArgs->get(i), Record);
4907}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004908
4909
4910void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004911ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004912 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004913 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004914 I = Set.begin(), E = Set.end(); I != E; ++I) {
4915 AddDeclRef(I.getDecl(), Record);
4916 Record.push_back(I.getAccess());
4917 }
4918}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004919
Sebastian Redla4232eb2010-08-18 23:56:21 +00004920void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004921 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004922 Record.push_back(Base.isVirtual());
4923 Record.push_back(Base.isBaseOfClass());
4924 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004925 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004926 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004927 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004928 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4929 : SourceLocation(),
4930 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004931}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004932
Douglas Gregor7c789c12010-10-29 22:39:52 +00004933void ASTWriter::FlushCXXBaseSpecifiers() {
4934 RecordData Record;
4935 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4936 Record.clear();
4937
4938 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004939 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004940 if (Index == CXXBaseSpecifiersOffsets.size())
4941 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4942 else {
4943 if (Index > CXXBaseSpecifiersOffsets.size())
4944 CXXBaseSpecifiersOffsets.resize(Index + 1);
4945 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4946 }
4947
4948 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4949 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4950 Record.push_back(BEnd - B);
4951 for (; B != BEnd; ++B)
4952 AddCXXBaseSpecifier(*B, Record);
4953 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004954
4955 // Flush any expressions that were written as part of the base specifiers.
4956 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004957 }
4958
4959 CXXBaseSpecifiersToWrite.clear();
4960}
4961
Sean Huntcbb67482011-01-08 20:30:50 +00004962void ASTWriter::AddCXXCtorInitializers(
4963 const CXXCtorInitializer * const *CtorInitializers,
4964 unsigned NumCtorInitializers,
4965 RecordDataImpl &Record) {
4966 Record.push_back(NumCtorInitializers);
4967 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4968 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004969
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004970 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004971 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004972 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004973 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004974 } else if (Init->isDelegatingInitializer()) {
4975 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004976 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004977 } else if (Init->isMemberInitializer()){
4978 Record.push_back(CTOR_INITIALIZER_MEMBER);
4979 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004980 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004981 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4982 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004983 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004984
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004985 AddSourceLocation(Init->getMemberLocation(), Record);
4986 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004987 AddSourceLocation(Init->getLParenLoc(), Record);
4988 AddSourceLocation(Init->getRParenLoc(), Record);
4989 Record.push_back(Init->isWritten());
4990 if (Init->isWritten()) {
4991 Record.push_back(Init->getSourceOrder());
4992 } else {
4993 Record.push_back(Init->getNumArrayIndices());
4994 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4995 AddDeclRef(Init->getArrayIndex(i), Record);
4996 }
4997 }
4998}
4999
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005000void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5001 assert(D->DefinitionData);
5002 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005003 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005004 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005005 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005006 Record.push_back(Data.Aggregate);
5007 Record.push_back(Data.PlainOldData);
5008 Record.push_back(Data.Empty);
5009 Record.push_back(Data.Polymorphic);
5010 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005011 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005012 Record.push_back(Data.HasNoNonEmptyBases);
5013 Record.push_back(Data.HasPrivateFields);
5014 Record.push_back(Data.HasProtectedFields);
5015 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005016 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00005017 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005018 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005019 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005020 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5021 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5022 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5023 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5024 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5025 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005026 Record.push_back(Data.HasTrivialSpecialMembers);
5027 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005028 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005029 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005030 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005031 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005032 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005033 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005034 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005035 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5036 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5037 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5038 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005039 Record.push_back(Data.FailedImplicitMoveConstructor);
5040 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005041 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005042
5043 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005044 if (Data.NumBases > 0)
5045 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5046 Record);
5047
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005048 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5049 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005050 if (Data.NumVBases > 0)
5051 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5052 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005053
5054 AddUnresolvedSet(Data.Conversions, Record);
5055 AddUnresolvedSet(Data.VisibleConversions, Record);
5056 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005057 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005058
5059 // Add lambda-specific data.
5060 if (Data.IsLambda) {
5061 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005062 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005063 Record.push_back(Lambda.NumCaptures);
5064 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005065 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005066 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005067 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005068 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5069 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5070 AddSourceLocation(Capture.getLocation(), Record);
5071 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005072 Record.push_back(Capture.getCaptureKind());
5073 switch (Capture.getCaptureKind()) {
5074 case LCK_This:
5075 break;
5076 case LCK_ByCopy:
5077 case LCK_ByRef: {
5078 VarDecl *Var =
5079 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5080 AddDeclRef(Var, Record);
5081 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5082 : SourceLocation(),
5083 Record);
5084 break;
5085 }
5086 case LCK_Init:
5087 FieldDecl *Field = Capture.getInitCaptureField();
5088 AddDeclRef(Field, Record);
5089 break;
5090 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005091 }
5092 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005093}
5094
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005095void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005096 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005097 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005098 assert(FirstDeclID == NextDeclID &&
5099 FirstTypeID == NextTypeID &&
5100 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005101 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005102 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005103 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005104 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005105
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005106 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005107
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005108 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5109 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5110 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005111 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005112 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005113 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005114 NextDeclID = FirstDeclID;
5115 NextTypeID = FirstTypeID;
5116 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005117 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005118 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005119 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005120}
5121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005122void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005123 // Always keep the highest ID. See \p TypeRead() for more information.
5124 IdentID &StoredID = IdentifierIDs[II];
5125 if (ID > StoredID)
5126 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005127}
5128
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005129void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005130 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005131 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005132 if (ID > StoredID)
5133 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005134}
5135
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005136void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005137 // Always take the highest-numbered type index. This copes with an interesting
5138 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005139 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005140 // keep the higher-numbered entry so that we can properly write it out to
5141 // the AST file.
5142 TypeIdx &StoredIdx = TypeIdxs[T];
5143 if (Idx.getIndex() >= StoredIdx.getIndex())
5144 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005145}
5146
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005147void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005148 // Always keep the highest ID. See \p TypeRead() for more information.
5149 SelectorID &StoredID = SelectorIDs[S];
5150 if (ID > StoredID)
5151 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005152}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005153
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005154void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005155 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005156 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005157 MacroDefinitions[MD] = ID;
5158}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005159
Douglas Gregora015cab2011-12-02 17:30:13 +00005160void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5161 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5162 SubmoduleIDs[Mod] = ID;
5163}
5164
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005165void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005166 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005167 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005168 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5169 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005170 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005171 // A forward reference was mutated into a definition. Rewrite it.
5172 // FIXME: This happens during template instantiation, should we
5173 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005174 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005175 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005176 }
5177}
Douglas Gregora8235d62012-10-09 23:05:51 +00005178
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005179void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005180 assert(!WritingAST && "Already writing the AST!");
5181
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005182 // TU and namespaces are handled elsewhere.
5183 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5184 return;
5185
Douglas Gregor919814d2011-09-09 23:01:35 +00005186 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005187 return; // Not a source decl added to a DeclContext from PCH.
5188
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005189 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005190 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005191 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005192}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005193
5194void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005195 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005196 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005197 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005198 return; // Not a source member added to a class from PCH.
5199 if (!isa<CXXMethodDecl>(D))
5200 return; // We are interested in lazily declared implicit methods.
5201
5202 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005203 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005204 UpdateRecord &Record = DeclUpdates[RD];
5205 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005206 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005207}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005208
5209void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5210 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005211 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005212 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005213 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005214 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005215 return; // Not a source specialization added to a template from PCH.
5216
5217 UpdateRecord &Record = DeclUpdates[TD];
5218 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005219 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005220}
Douglas Gregor89d99802010-11-30 06:16:57 +00005221
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005222void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5223 const FunctionDecl *D) {
5224 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005225 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005226 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005227 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005228 return; // Not a source specialization added to a template from PCH.
5229
5230 UpdateRecord &Record = DeclUpdates[TD];
5231 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005232 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005233}
5234
Richard Smith9dadfab2013-05-11 05:45:24 +00005235void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5236 assert(!WritingAST && "Already writing the AST!");
5237 FD = FD->getCanonicalDecl();
5238 if (!FD->isFromASTFile())
5239 return; // Not a function declared in PCH and defined outside.
5240
5241 UpdateRecord &Record = DeclUpdates[FD];
5242 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5243 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5244}
5245
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005246void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005247 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005248 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005249 return; // Declaration not imported from PCH.
5250
5251 // Implicit decl from a PCH was defined.
5252 // FIXME: Should implicit definition be a separate FunctionDecl?
5253 RewriteDecl(D);
5254}
5255
Sebastian Redlf79a7192011-04-29 08:19:30 +00005256void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005257 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005258 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005259 return;
5260
5261 // Since the actual instantiation is delayed, this really means that we need
5262 // to update the instantiation location.
5263 UpdateRecord &Record = DeclUpdates[D];
5264 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5265 AddSourceLocation(
5266 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5267}
5268
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005269void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5270 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005271 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005272 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005273 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005274
5275 assert(IFD->getDefinition() && "Category on a class without a definition?");
5276 ObjCClassesWithCategories.insert(
5277 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005278}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005279
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005280
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005281void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5282 const ObjCPropertyDecl *OrigProp,
5283 const ObjCCategoryDecl *ClassExt) {
5284 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5285 if (!D)
5286 return;
5287
5288 assert(!WritingAST && "Already writing the AST!");
5289 if (!D->isFromASTFile())
5290 return; // Declaration not imported from PCH.
5291
5292 RewriteDecl(D);
5293}