blob: 3ee9830e7cb036356bcd20b9ecd3244502db907d [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);
Richard Smithac32d902013-08-07 21:41:30 +0000851 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000852
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000853 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000854 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000855 RECORD(SM_SLOC_FILE_ENTRY);
856 RECORD(SM_SLOC_BUFFER_ENTRY);
857 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000858 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000860 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000861 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000862 RECORD(PP_MACRO_OBJECT_LIKE);
863 RECORD(PP_MACRO_FUNCTION_LIKE);
864 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000865
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000866 // Decls and Types block.
867 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000868 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000869 RECORD(TYPE_COMPLEX);
870 RECORD(TYPE_POINTER);
871 RECORD(TYPE_BLOCK_POINTER);
872 RECORD(TYPE_LVALUE_REFERENCE);
873 RECORD(TYPE_RVALUE_REFERENCE);
874 RECORD(TYPE_MEMBER_POINTER);
875 RECORD(TYPE_CONSTANT_ARRAY);
876 RECORD(TYPE_INCOMPLETE_ARRAY);
877 RECORD(TYPE_VARIABLE_ARRAY);
878 RECORD(TYPE_VECTOR);
879 RECORD(TYPE_EXT_VECTOR);
880 RECORD(TYPE_FUNCTION_PROTO);
881 RECORD(TYPE_FUNCTION_NO_PROTO);
882 RECORD(TYPE_TYPEDEF);
883 RECORD(TYPE_TYPEOF_EXPR);
884 RECORD(TYPE_TYPEOF);
885 RECORD(TYPE_RECORD);
886 RECORD(TYPE_ENUM);
887 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000888 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000889 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000890 RECORD(TYPE_DECLTYPE);
891 RECORD(TYPE_ELABORATED);
892 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
893 RECORD(TYPE_UNRESOLVED_USING);
894 RECORD(TYPE_INJECTED_CLASS_NAME);
895 RECORD(TYPE_OBJC_OBJECT);
896 RECORD(TYPE_TEMPLATE_TYPE_PARM);
897 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
898 RECORD(TYPE_DEPENDENT_NAME);
899 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
900 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
901 RECORD(TYPE_PAREN);
902 RECORD(TYPE_PACK_EXPANSION);
903 RECORD(TYPE_ATTRIBUTED);
904 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000905 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000906 RECORD(DECL_TYPEDEF);
907 RECORD(DECL_ENUM);
908 RECORD(DECL_RECORD);
909 RECORD(DECL_ENUM_CONSTANT);
910 RECORD(DECL_FUNCTION);
911 RECORD(DECL_OBJC_METHOD);
912 RECORD(DECL_OBJC_INTERFACE);
913 RECORD(DECL_OBJC_PROTOCOL);
914 RECORD(DECL_OBJC_IVAR);
915 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000916 RECORD(DECL_OBJC_CATEGORY);
917 RECORD(DECL_OBJC_CATEGORY_IMPL);
918 RECORD(DECL_OBJC_IMPLEMENTATION);
919 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
920 RECORD(DECL_OBJC_PROPERTY);
921 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000922 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000923 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000924 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000925 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000926 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000927 RECORD(DECL_FILE_SCOPE_ASM);
928 RECORD(DECL_BLOCK);
929 RECORD(DECL_CONTEXT_LEXICAL);
930 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000931 RECORD(DECL_NAMESPACE);
932 RECORD(DECL_NAMESPACE_ALIAS);
933 RECORD(DECL_USING);
934 RECORD(DECL_USING_SHADOW);
935 RECORD(DECL_USING_DIRECTIVE);
936 RECORD(DECL_UNRESOLVED_USING_VALUE);
937 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
938 RECORD(DECL_LINKAGE_SPEC);
939 RECORD(DECL_CXX_RECORD);
940 RECORD(DECL_CXX_METHOD);
941 RECORD(DECL_CXX_CONSTRUCTOR);
942 RECORD(DECL_CXX_DESTRUCTOR);
943 RECORD(DECL_CXX_CONVERSION);
944 RECORD(DECL_ACCESS_SPEC);
945 RECORD(DECL_FRIEND);
946 RECORD(DECL_FRIEND_TEMPLATE);
947 RECORD(DECL_CLASS_TEMPLATE);
948 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
949 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000950 RECORD(DECL_VAR_TEMPLATE);
951 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
952 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000953 RECORD(DECL_FUNCTION_TEMPLATE);
954 RECORD(DECL_TEMPLATE_TYPE_PARM);
955 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
956 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
957 RECORD(DECL_STATIC_ASSERT);
958 RECORD(DECL_CXX_BASE_SPECIFIERS);
959 RECORD(DECL_INDIRECTFIELD);
960 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
961
Douglas Gregora72d8c42011-06-03 02:27:19 +0000962 // Statements and Exprs can occur in the Decls and Types block.
963 AddStmtsExprs(Stream, Record);
964
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000965 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000966 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000967 RECORD(PPD_MACRO_DEFINITION);
968 RECORD(PPD_INCLUSION_DIRECTIVE);
969
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000970#undef RECORD
971#undef BLOCK
972 Stream.ExitBlock();
973}
974
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975/// \brief Adjusts the given filename to only write out the portion of the
976/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000977///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978/// \param Filename the file name to adjust.
979///
980/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
981/// the returned filename will be adjusted by this system root.
982///
983/// \returns either the original filename (if it needs no adjustment) or the
984/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000985static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000986adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregor832d6202011-07-22 16:35:34 +0000989 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000990 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregore650c8c2009-07-07 00:12:59 +0000992 // Verify that the filename and the system root have the same prefix.
993 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000994 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000995 if (Filename[Pos] != isysroot[Pos])
996 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Douglas Gregore650c8c2009-07-07 00:12:59 +0000998 // We hit the end of the filename before we hit the end of the system root.
999 if (!Filename[Pos])
1000 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 // If the file name has a '/' at the current position, skip over the '/'.
1003 // We distinguish sysroot-based includes from absolute includes by the
1004 // absence of '/' at the beginning of sysroot-based includes.
1005 if (Filename[Pos] == '/')
1006 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Douglas Gregore650c8c2009-07-07 00:12:59 +00001008 return Filename + Pos;
1009}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001010
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001011/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001012void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1013 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001014 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001015 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001016 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1017 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001018
Douglas Gregore650c8c2009-07-07 00:12:59 +00001019 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001020 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1021 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1022 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1023 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1024 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1025 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1026 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1027 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1028 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1029 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1030 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001031 Record.push_back(VERSION_MAJOR);
1032 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001033 Record.push_back(CLANG_VERSION_MAJOR);
1034 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001035 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001036 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001037 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1038 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001039
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001040 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001041 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001042 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001043 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001044 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001045
1046 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1047 M != MEnd; ++M) {
1048 // Skip modules that weren't directly imported.
1049 if (!(*M)->isDirectlyImported())
1050 continue;
1051
1052 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001053 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001054 Record.push_back((*M)->File->getSize());
1055 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001056 // FIXME: This writes the absolute path for AST files we depend on.
1057 const std::string &FileName = (*M)->FileName;
1058 Record.push_back(FileName.size());
1059 Record.append(FileName.begin(), FileName.end());
1060 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001061 Stream.EmitRecord(IMPORTS, Record);
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001064 // Language options.
1065 Record.clear();
1066 const LangOptions &LangOpts = Context.getLangOpts();
1067#define LANGOPT(Name, Bits, Default, Description) \
1068 Record.push_back(LangOpts.Name);
1069#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1070 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1071#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001072#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1073#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001074
1075 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1076 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1077
1078 Record.push_back(LangOpts.CurrentModule.size());
1079 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001080
1081 // Comment options.
1082 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1083 for (CommentOptions::BlockCommandNamesTy::const_iterator
1084 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1085 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1086 I != IEnd; ++I) {
1087 AddString(*I, Record);
1088 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001089 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001090
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001091 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1092
Douglas Gregoree097c12012-10-18 17:58:09 +00001093 // Target options.
1094 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001095 const TargetInfo &Target = Context.getTargetInfo();
1096 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001097 AddString(TargetOpts.Triple, Record);
1098 AddString(TargetOpts.CPU, Record);
1099 AddString(TargetOpts.ABI, Record);
1100 AddString(TargetOpts.CXXABI, Record);
1101 AddString(TargetOpts.LinkerVersion, Record);
1102 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1103 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1104 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1105 }
1106 Record.push_back(TargetOpts.Features.size());
1107 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1108 AddString(TargetOpts.Features[I], Record);
1109 }
1110 Stream.EmitRecord(TARGET_OPTIONS, Record);
1111
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001112 // Diagnostic options.
1113 Record.clear();
1114 const DiagnosticOptions &DiagOpts
1115 = Context.getDiagnostics().getDiagnosticOptions();
1116#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1117#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1118 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1119#include "clang/Basic/DiagnosticOptions.def"
1120 Record.push_back(DiagOpts.Warnings.size());
1121 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1122 AddString(DiagOpts.Warnings[I], Record);
1123 // Note: we don't serialize the log or serialization file names, because they
1124 // are generally transient files and will almost always be overridden.
1125 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1126
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001127 // File system options.
1128 Record.clear();
1129 const FileSystemOptions &FSOpts
1130 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1131 AddString(FSOpts.WorkingDir, Record);
1132 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1133
Douglas Gregorbbf38312012-10-24 16:50:34 +00001134 // Header search options.
1135 Record.clear();
1136 const HeaderSearchOptions &HSOpts
1137 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1138 AddString(HSOpts.Sysroot, Record);
1139
1140 // Include entries.
1141 Record.push_back(HSOpts.UserEntries.size());
1142 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1143 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1144 AddString(Entry.Path, Record);
1145 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001146 Record.push_back(Entry.IsFramework);
1147 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001148 }
1149
1150 // System header prefixes.
1151 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1152 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1153 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1154 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1155 }
1156
1157 AddString(HSOpts.ResourceDir, Record);
1158 AddString(HSOpts.ModuleCachePath, Record);
1159 Record.push_back(HSOpts.DisableModuleHash);
1160 Record.push_back(HSOpts.UseBuiltinIncludes);
1161 Record.push_back(HSOpts.UseStandardSystemIncludes);
1162 Record.push_back(HSOpts.UseStandardCXXIncludes);
1163 Record.push_back(HSOpts.UseLibcxx);
1164 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1165
Douglas Gregora71a7d82012-10-24 20:05:57 +00001166 // Preprocessor options.
1167 Record.clear();
1168 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1169
1170 // Macro definitions.
1171 Record.push_back(PPOpts.Macros.size());
1172 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1173 AddString(PPOpts.Macros[I].first, Record);
1174 Record.push_back(PPOpts.Macros[I].second);
1175 }
1176
1177 // Includes
1178 Record.push_back(PPOpts.Includes.size());
1179 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1180 AddString(PPOpts.Includes[I], Record);
1181
1182 // Macro includes
1183 Record.push_back(PPOpts.MacroIncludes.size());
1184 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1185 AddString(PPOpts.MacroIncludes[I], Record);
1186
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001187 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001188 // Detailed record is important since it is used for the module cache hash.
1189 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001190 AddString(PPOpts.ImplicitPCHInclude, Record);
1191 AddString(PPOpts.ImplicitPTHInclude, Record);
1192 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1193 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1194
Douglas Gregor31d375f2011-05-06 21:43:30 +00001195 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001196 SourceManager &SM = Context.getSourceManager();
1197 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1198 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001199 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1200 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001201 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1202 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1203
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001204 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001206 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001207
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001208 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001209 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001210 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001211 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001212 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001213 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001214 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001215 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001216
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001217 Record.clear();
1218 Record.push_back(SM.getMainFileID().getOpaqueValue());
1219 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1220
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001221 // Original PCH directory
1222 if (!OutputFile.empty() && OutputFile != "-") {
1223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1224 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1226 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1227
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001228 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001229
1230 llvm::sys::fs::make_absolute(OutputPath);
1231 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1232
1233 RecordData Record;
1234 Record.push_back(ORIGINAL_PCH_DIR);
1235 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1236 }
1237
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001238 WriteInputFiles(Context.SourceMgr,
1239 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregorb22d1942013-07-22 20:48:33 +00001240 isysroot,
1241 PP.getLangOpts().Modules);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001242 Stream.ExitBlock();
1243}
1244
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001245namespace {
1246 /// \brief An input file.
1247 struct InputFileEntry {
1248 const FileEntry *File;
1249 bool IsSystemFile;
1250 bool BufferOverridden;
1251 };
1252}
1253
1254void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1255 HeaderSearchOptions &HSOpts,
Douglas Gregorb22d1942013-07-22 20:48:33 +00001256 StringRef isysroot,
1257 bool Modules) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001258 using namespace llvm;
1259 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1260 RecordData Record;
1261
1262 // Create input-file abbreviation.
1263 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1264 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001265 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001266 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1267 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001268 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001269 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1270 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1271
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001272 // Get all ContentCache objects for files, sorted by whether the file is a
1273 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001274 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001275 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1276 // Get this source location entry.
1277 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001278 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001279
1280 // We only care about file entries that were not overridden.
1281 if (!SLoc->isFile())
1282 continue;
1283 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001284 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001285 continue;
1286
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001287 InputFileEntry Entry;
1288 Entry.File = Cache->OrigEntry;
1289 Entry.IsSystemFile = Cache->IsSystemFile;
1290 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001291 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001292 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001293 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001294 SortedFiles.push_front(Entry);
1295 }
1296
1297 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1298 // the set of (non-system) input files. This is simple heuristic for
1299 // detecting whether the system headers may have changed, because it is too
1300 // expensive to stat() all of the system headers.
Richard Smithcc8e22b2013-05-20 23:40:27 +00001301 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001302 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001303 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1304 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1305 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1306 InputFileEntry Entry = { SDKSettingsFile, false, false };
1307 SortedFiles.push_front(Entry);
1308 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001309 }
1310
Douglas Gregorb22d1942013-07-22 20:48:33 +00001311 // Add the compiler's own module.map in the set of (non-system) input files.
1312 // This is a simple heuristic for detecting whether the compiler's headers
1313 // have changed, because we don't want to stat() all of them.
1314 if (Modules && !Chain) {
1315 SmallString<128> P = StringRef(HSOpts.ResourceDir);
1316 llvm::sys::path::append(P, "include");
1317 llvm::sys::path::append(P, "module.map");
1318 if (const FileEntry *ModuleMapFile = FileMgr.getFile(P)) {
1319 InputFileEntry Entry = { ModuleMapFile, false, false };
1320 SortedFiles.push_front(Entry);
1321 }
1322 }
1323
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001324 unsigned UserFilesNum = 0;
1325 // Write out all of the input files.
1326 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001327 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001328 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001329 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001330
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001331 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001332 if (InputFileID != 0)
1333 continue; // already recorded this file.
1334
Douglas Gregora930dc92012-10-22 18:42:04 +00001335 // Record this entry's offset.
1336 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001337
1338 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001339
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001340 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001341 ++UserFilesNum;
1342
Douglas Gregor745e6f12012-10-19 00:38:02 +00001343 Record.clear();
1344 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001345 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001346
1347 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001348 Record.push_back(Entry.File->getSize());
1349 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001350
Douglas Gregora930dc92012-10-22 18:42:04 +00001351 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001352 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001353
Douglas Gregor745e6f12012-10-19 00:38:02 +00001354 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001355 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001356 SmallString<128> FilePath(Filename);
1357
1358 // Ask the file manager to fixup the relative path for us. This will
1359 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001360 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001361
1362 // FIXME: This call to make_absolute shouldn't be necessary, the
1363 // call to FixupRelativePath should always return an absolute path.
1364 llvm::sys::fs::make_absolute(FilePath);
1365 Filename = FilePath.c_str();
1366
1367 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1368
1369 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1370 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001371
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001372 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001373
1374 // Create input file offsets abbreviation.
1375 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1376 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1377 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001378 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1379 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001380 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1381 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1382
1383 // Write input file offsets.
1384 Record.clear();
1385 Record.push_back(INPUT_FILE_OFFSETS);
1386 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001387 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001388 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001389}
1390
Douglas Gregor14f79002009-04-10 03:52:48 +00001391//===----------------------------------------------------------------------===//
1392// Source Manager Serialization
1393//===----------------------------------------------------------------------===//
1394
1395/// \brief Create an abbreviation for the SLocEntry that refers to a
1396/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001397static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001398 using namespace llvm;
1399 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001400 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001405 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001410 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001411}
1412
1413/// \brief Create an abbreviation for the SLocEntry that refers to a
1414/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001415static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001416 using namespace llvm;
1417 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001418 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001424 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001425}
1426
1427/// \brief Create an abbreviation for the SLocEntry that refers to a
1428/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001429static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001430 using namespace llvm;
1431 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001432 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001434 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001435}
1436
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001437/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1438/// expansion.
1439static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001440 using namespace llvm;
1441 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001442 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001448 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001449}
1450
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001451namespace {
1452 // Trait used for the on-disk hash table of header search information.
1453 class HeaderFileInfoTrait {
1454 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001455 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001456
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001457 // Keep track of the framework names we've used during serialization.
1458 SmallVector<char, 128> FrameworkStringData;
1459 llvm::StringMap<unsigned> FrameworkNameOffset;
1460
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001461 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001462 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1463 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001464
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001465 struct key_type {
1466 const FileEntry *FE;
1467 const char *Filename;
1468 };
1469 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001470
1471 typedef HeaderFileInfo data_type;
1472 typedef const data_type &data_type_ref;
1473
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001474 static unsigned ComputeHash(key_type_ref key) {
1475 // The hash is based only on size/time of the file, so that the reader can
1476 // match even when symlinking or excess path elements ("foo/../", "../")
1477 // change the form of the name. However, complete path is still the key.
1478 return llvm::hash_combine(key.FE->getSize(),
1479 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001480 }
1481
1482 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001483 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1484 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1485 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001486 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001487 if (Data.isModuleHeader)
1488 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001489 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001490 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001491 }
1492
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001493 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1494 clang::io::Emit64(Out, key.FE->getSize());
1495 KeyLen -= 8;
1496 clang::io::Emit64(Out, key.FE->getModificationTime());
1497 KeyLen -= 8;
1498 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001499 }
1500
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001501 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001502 data_type_ref Data, unsigned DataLen) {
1503 using namespace clang::io;
1504 uint64_t Start = Out.tell(); (void)Start;
1505
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001506 unsigned char Flags = (Data.HeaderRole << 6)
1507 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001508 | (Data.isPragmaOnce << 4)
1509 | (Data.DirInfo << 2)
1510 | (Data.Resolved << 1)
1511 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001512 Emit8(Out, (uint8_t)Flags);
1513 Emit16(Out, (uint16_t) Data.NumIncludes);
1514
1515 if (!Data.ControllingMacro)
1516 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1517 else
1518 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001519
1520 unsigned Offset = 0;
1521 if (!Data.Framework.empty()) {
1522 // If this header refers into a framework, save the framework name.
1523 llvm::StringMap<unsigned>::iterator Pos
1524 = FrameworkNameOffset.find(Data.Framework);
1525 if (Pos == FrameworkNameOffset.end()) {
1526 Offset = FrameworkStringData.size() + 1;
1527 FrameworkStringData.append(Data.Framework.begin(),
1528 Data.Framework.end());
1529 FrameworkStringData.push_back(0);
1530
1531 FrameworkNameOffset[Data.Framework] = Offset;
1532 } else
1533 Offset = Pos->second;
1534 }
1535 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001536
1537 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001538 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001539 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1540 }
1541
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001542 assert(Out.tell() - Start == DataLen && "Wrong data length");
1543 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001544
1545 const char *strings_begin() const { return FrameworkStringData.begin(); }
1546 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001547 };
1548} // end anonymous namespace
1549
1550/// \brief Write the header search block for the list of files that
1551///
1552/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001553void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001554 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001555 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1556
1557 if (FilesByUID.size() > HS.header_file_size())
1558 FilesByUID.resize(HS.header_file_size());
1559
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001560 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001561 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001562 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001563 unsigned NumHeaderSearchEntries = 0;
1564 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1565 const FileEntry *File = FilesByUID[UID];
1566 if (!File)
1567 continue;
1568
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001569 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1570 // from the external source if it was not provided already.
1571 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001572 if (HFI.External && Chain)
1573 continue;
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001574 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1575 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001576
1577 // Turn the file name into an absolute path, if it isn't already.
1578 const char *Filename = File->getName();
1579 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1580
1581 // If we performed any translation on the file name at all, we need to
1582 // save this string, since the generator will refer to it later.
1583 if (Filename != File->getName()) {
1584 Filename = strdup(Filename);
1585 SavedStrings.push_back(Filename);
1586 }
1587
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001588 HeaderFileInfoTrait::key_type key = { File, Filename };
1589 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001590 ++NumHeaderSearchEntries;
1591 }
1592
1593 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001594 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001595 uint32_t BucketOffset;
1596 {
1597 llvm::raw_svector_ostream Out(TableData);
1598 // Make sure that no bucket is at offset 0
1599 clang::io::Emit32(Out, 0);
1600 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1601 }
1602
1603 // Create a blob abbreviation
1604 using namespace llvm;
1605 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1606 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1611 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1612
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001613 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001614 RecordData Record;
1615 Record.push_back(HEADER_SEARCH_TABLE);
1616 Record.push_back(BucketOffset);
1617 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001618 Record.push_back(TableData.size());
1619 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001620 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1621
1622 // Free all of the strings we had to duplicate.
1623 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001624 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001625}
1626
Douglas Gregor14f79002009-04-10 03:52:48 +00001627/// \brief Writes the block containing the serialized form of the
1628/// source manager.
1629///
1630/// TODO: We should probably use an on-disk hash table (stored in a
1631/// blob), indexed based on the file name, so that we only create
1632/// entries for files that we actually need. In the common case (no
1633/// errors), we probably won't have to create file entries for any of
1634/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001635void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001636 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001637 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001638 RecordData Record;
1639
Chris Lattnerf04ad692009-04-10 17:16:57 +00001640 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001641 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001642
1643 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001644 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1645 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1646 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001647 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001648
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001649 // Write out the source location entry table. We skip the first
1650 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001651 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001652 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001653 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1654 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001655 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001656 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001657 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001658 FileID FID = FileID::get(I);
1659 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001660
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001661 // Record the offset of this source-location entry.
1662 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1663
1664 // Figure out which record code to use.
1665 unsigned Code;
1666 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001667 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1668 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001669 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001670 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001671 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001672 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001673 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001674 Record.clear();
1675 Record.push_back(Code);
1676
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001677 // Starting offset of this entry within this module, so skip the dummy.
1678 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001679 if (SLoc->isFile()) {
1680 const SrcMgr::FileInfo &File = SLoc->getFile();
1681 Record.push_back(File.getIncludeLoc().getRawEncoding());
1682 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1683 Record.push_back(File.hasLineDirectives());
1684
1685 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001686 if (Content->OrigEntry) {
1687 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001688 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001689
Douglas Gregora930dc92012-10-22 18:42:04 +00001690 // The source location entry is a file. Emit input file ID.
1691 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1692 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001694 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001695
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001696 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001697 if (FDI != FileDeclIDs.end()) {
1698 Record.push_back(FDI->second->FirstDeclIndex);
1699 Record.push_back(FDI->second->DeclIDs.size());
1700 } else {
1701 Record.push_back(0);
1702 Record.push_back(0);
1703 }
Douglas Gregora081da52011-11-16 20:05:18 +00001704
Douglas Gregora930dc92012-10-22 18:42:04 +00001705 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001706
1707 if (Content->BufferOverridden) {
1708 Record.clear();
1709 Record.push_back(SM_SLOC_BUFFER_BLOB);
1710 const llvm::MemoryBuffer *Buffer
1711 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1712 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1713 StringRef(Buffer->getBufferStart(),
1714 Buffer->getBufferSize() + 1));
1715 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001716 } else {
1717 // The source location entry is a buffer. The blob associated
1718 // with this entry contains the contents of the buffer.
1719
1720 // We add one to the size so that we capture the trailing NULL
1721 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1722 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001723 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001724 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001725 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001726 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001727 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001728 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001729 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001730 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001731 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001732 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001733
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001734 if (strcmp(Name, "<built-in>") == 0) {
1735 PreloadSLocs.push_back(SLocEntryOffsets.size());
1736 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737 }
1738 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001739 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001740 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001741 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1742 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001743 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1744 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001745
1746 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001747 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001748 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001749 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001750 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001751 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001752 }
1753 }
1754
Douglas Gregorc9490c02009-04-16 22:23:12 +00001755 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001756
1757 if (SLocEntryOffsets.empty())
1758 return;
1759
Sebastian Redl3397c552010-08-18 23:56:27 +00001760 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001761 // table is used for lazily loading source-location information.
1762 using namespace llvm;
1763 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001764 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001765 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1768 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001770 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001771 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001772 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001773 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001774 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001775
Sebastian Redl3397c552010-08-18 23:56:27 +00001776 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001777 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001778 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001779
1780 // Write the line table. It depends on remapping working, so it must come
1781 // after the source location offsets.
1782 if (SourceMgr.hasLineTable()) {
1783 LineTableInfo &LineTable = SourceMgr.getLineTable();
1784
1785 Record.clear();
1786 // Emit the file names
1787 Record.push_back(LineTable.getNumFilenames());
1788 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1789 // Emit the file name
1790 const char *Filename = LineTable.getFilename(I);
1791 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1792 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1793 Record.push_back(FilenameLen);
1794 if (FilenameLen)
1795 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1796 }
1797
1798 // Emit the line entries
1799 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1800 L != LEnd; ++L) {
1801 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001802 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001803 continue;
1804
1805 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001806 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001807
1808 // Emit the line entries
1809 Record.push_back(L->second.size());
1810 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1811 LEEnd = L->second.end();
1812 LE != LEEnd; ++LE) {
1813 Record.push_back(LE->FileOffset);
1814 Record.push_back(LE->LineNo);
1815 Record.push_back(LE->FilenameID);
1816 Record.push_back((unsigned)LE->FileKind);
1817 Record.push_back(LE->IncludeOffset);
1818 }
1819 }
1820 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1821 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001822}
1823
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001824//===----------------------------------------------------------------------===//
1825// Preprocessor Serialization
1826//===----------------------------------------------------------------------===//
1827
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001828namespace {
1829class ASTMacroTableTrait {
1830public:
1831 typedef IdentID key_type;
1832 typedef key_type key_type_ref;
1833
1834 struct Data {
1835 uint32_t MacroDirectivesOffset;
1836 };
1837
1838 typedef Data data_type;
1839 typedef const data_type &data_type_ref;
1840
1841 static unsigned ComputeHash(IdentID IdID) {
1842 return llvm::hash_value(IdID);
1843 }
1844
1845 std::pair<unsigned,unsigned>
1846 static EmitKeyDataLength(raw_ostream& Out,
1847 key_type_ref Key, data_type_ref Data) {
1848 unsigned KeyLen = 4; // IdentID.
1849 unsigned DataLen = 4; // MacroDirectivesOffset.
1850 return std::make_pair(KeyLen, DataLen);
1851 }
1852
1853 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1854 clang::io::Emit32(Out, Key);
1855 }
1856
1857 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1858 unsigned) {
1859 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1860 }
1861};
1862} // end anonymous namespace
1863
Benjamin Kramer767b3d22013-09-22 14:10:29 +00001864static int compareMacroDirectives(
1865 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1866 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1867 return X->first->getName().compare(Y->first->getName());
Douglas Gregor9c736102011-02-10 18:20:09 +00001868}
1869
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001870static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1871 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001872 if (MacroInfo *MI = MD->getMacroInfo())
1873 if (MI->isBuiltinMacro())
1874 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001875
1876 if (IsModule) {
1877 SourceLocation Loc = MD->getLocation();
1878 if (Loc.isInvalid())
1879 return true;
1880 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1881 return true;
1882 }
1883
1884 return false;
1885}
1886
Chris Lattner0b1fb982009-04-10 17:15:23 +00001887/// \brief Writes the block containing the serialized form of the
1888/// preprocessor.
1889///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001890void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001891 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1892 if (PPRec)
1893 WritePreprocessorDetail(*PPRec);
1894
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001895 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001896
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001897 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1898 if (PP.getCounterValue() != 0) {
1899 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001900 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001901 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001902 }
1903
1904 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001905 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Sebastian Redl3397c552010-08-18 23:56:27 +00001907 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001908 // FIXME: use diagnostics subsystem for localization etc.
1909 if (PP.SawDateOrTime())
1910 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Douglas Gregorecdcb882010-10-20 22:00:55 +00001912
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001913 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001914 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001915
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001916 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001917 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001918 MacroDirectives;
1919 for (Preprocessor::macro_iterator
1920 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1921 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001922 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001923 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001924 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001925
Douglas Gregor9c736102011-02-10 18:20:09 +00001926 // Sort the set of macro definitions that need to be serialized by the
1927 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001928 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1929 &compareMacroDirectives);
1930
1931 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1932
1933 // Emit the macro directives as a list and associate the offset with the
1934 // identifier they belong to.
1935 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1936 const IdentifierInfo *Name = MacroDirectives[I].first;
1937 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1938 MacroDirective *MD = MacroDirectives[I].second;
1939
1940 // If the macro or identifier need no updates, don't write the macro history
1941 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001942 // FIXME: Chain the macro history instead of re-writing it.
1943 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001944 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1945 continue;
1946
1947 // Emit the macro directives in reverse source order.
1948 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001949 if (MD->isHidden())
1950 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001951 if (shouldIgnoreMacro(MD, IsModule, PP))
1952 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001953
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001954 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001955 Record.push_back(MD->getKind());
1956 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1957 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1958 Record.push_back(InfoID);
1959 Record.push_back(DefMD->isImported());
1960 Record.push_back(DefMD->isAmbiguous());
1961
1962 } else if (VisibilityMacroDirective *
1963 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1964 Record.push_back(VisMD->isPublic());
1965 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001966 }
1967 if (Record.empty())
1968 continue;
1969
1970 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1971 Record.clear();
1972
1973 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1974
1975 IdentID NameID = getIdentifierRef(Name);
1976 ASTMacroTableTrait::Data data;
1977 data.MacroDirectivesOffset = MacroDirectiveOffset;
1978 Generator.insert(NameID, data);
1979 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001980
Douglas Gregora8235d62012-10-09 23:05:51 +00001981 /// \brief Offsets of each of the macros into the bitstream, indexed by
1982 /// the local macro ID
1983 ///
1984 /// For each identifier that is associated with a macro, this map
1985 /// provides the offset into the bitstream where that macro is
1986 /// defined.
1987 std::vector<uint32_t> MacroOffsets;
1988
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001989 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1990 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1991 MacroInfo *MI = MacroInfosToEmit[I].MI;
1992 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001993
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001994 if (ID < FirstMacroID) {
1995 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1996 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001997 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001998
1999 // Record the local offset of this macro.
2000 unsigned Index = ID - FirstMacroID;
2001 if (Index == MacroOffsets.size())
2002 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2003 else {
2004 if (Index > MacroOffsets.size())
2005 MacroOffsets.resize(Index + 1);
2006
2007 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2008 }
2009
2010 AddIdentifierRef(Name, Record);
2011 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2012 AddSourceLocation(MI->getDefinitionLoc(), Record);
2013 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2014 Record.push_back(MI->isUsed());
2015 unsigned Code;
2016 if (MI->isObjectLike()) {
2017 Code = PP_MACRO_OBJECT_LIKE;
2018 } else {
2019 Code = PP_MACRO_FUNCTION_LIKE;
2020
2021 Record.push_back(MI->isC99Varargs());
2022 Record.push_back(MI->isGNUVarargs());
2023 Record.push_back(MI->hasCommaPasting());
2024 Record.push_back(MI->getNumArgs());
2025 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2026 I != E; ++I)
2027 AddIdentifierRef(*I, Record);
2028 }
2029
2030 // If we have a detailed preprocessing record, record the macro definition
2031 // ID that corresponds to this macro.
2032 if (PPRec)
2033 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2034
2035 Stream.EmitRecord(Code, Record);
2036 Record.clear();
2037
2038 // Emit the tokens array.
2039 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2040 // Note that we know that the preprocessor does not have any annotation
2041 // tokens in it because they are created by the parser, and thus can't
2042 // be in a macro definition.
2043 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002044 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002045 Stream.EmitRecord(PP_TOKEN, Record);
2046 Record.clear();
2047 }
2048 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002049 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002050
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002051 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002052
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002053 // Create the on-disk hash table in a buffer.
2054 SmallString<4096> MacroTable;
2055 uint32_t BucketOffset;
2056 {
2057 llvm::raw_svector_ostream Out(MacroTable);
2058 // Make sure that no bucket is at offset 0
2059 clang::io::Emit32(Out, 0);
2060 BucketOffset = Generator.Emit(Out);
2061 }
2062
2063 // Write the macro table
2064 using namespace llvm;
2065 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2066 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2069 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2070
2071 Record.push_back(MACRO_TABLE);
2072 Record.push_back(BucketOffset);
2073 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2074 Record.clear();
2075
Douglas Gregora8235d62012-10-09 23:05:51 +00002076 // Write the offsets table for macro IDs.
2077 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002078 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002079 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2083
2084 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2085 Record.clear();
2086 Record.push_back(MACRO_OFFSET);
2087 Record.push_back(MacroOffsets.size());
2088 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2089 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2090 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002091}
2092
2093void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002094 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002095 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002096
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002097 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002098
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002099 // Enter the preprocessor block.
2100 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002101
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002102 // If the preprocessor has a preprocessing record, emit it.
2103 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002104 using namespace llvm;
2105
2106 // Set up the abbreviation for
2107 unsigned InclusionAbbrev = 0;
2108 {
2109 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2110 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002111 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2116 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2117 }
2118
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002119 unsigned FirstPreprocessorEntityID
2120 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2121 + NUM_PREDEF_PP_ENTITY_IDS;
2122 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002123 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002124 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2125 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002126 E != EEnd;
2127 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002128 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002129
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002130 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2131 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002132
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002133 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002134 // Record this macro definition's ID.
2135 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002136
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002137 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002138 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2139 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002140 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002141
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002142 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002143 Record.push_back(ME->isBuiltinMacro());
2144 if (ME->isBuiltinMacro())
2145 AddIdentifierRef(ME->getName(), Record);
2146 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002147 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002148 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002149 continue;
2150 }
2151
2152 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2153 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002154 Record.push_back(ID->getFileName().size());
2155 Record.push_back(ID->wasInQuotes());
2156 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002157 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002158 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002159 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002160 // Check that the FileEntry is not null because it was not resolved and
2161 // we create a PCH even with compiler errors.
2162 if (ID->getFile())
2163 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002164 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2165 continue;
2166 }
2167
2168 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2169 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002170 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002171
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002172 // Write the offsets table for the preprocessing record.
2173 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002174 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2175
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002176 // Write the offsets table for identifier IDs.
2177 using namespace llvm;
2178 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002179 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002182 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002183
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002184 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002185 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002186 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002187 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2188 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002189 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002190}
2191
Douglas Gregore209e502011-12-06 01:10:29 +00002192unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2193 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2194 if (Known != SubmoduleIDs.end())
2195 return Known->second;
2196
2197 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2198}
2199
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002200unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2201 if (!Mod)
2202 return 0;
2203
2204 llvm::DenseMap<Module *, unsigned>::const_iterator
2205 Known = SubmoduleIDs.find(Mod);
2206 if (Known != SubmoduleIDs.end())
2207 return Known->second;
2208
2209 return 0;
2210}
2211
Douglas Gregor26ced122011-12-01 00:59:36 +00002212/// \brief Compute the number of modules within the given tree (including the
2213/// given module).
2214static unsigned getNumberOfModules(Module *Mod) {
2215 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002216 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2217 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002218 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002219 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002220
2221 return ChildModules + 1;
2222}
2223
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002224void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002225 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002226 // FIXME: This feels like it belongs somewhere else, but there are no
2227 // other consumers of this information.
2228 SourceManager &SrcMgr = PP->getSourceManager();
2229 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2230 for (ASTContext::import_iterator I = Context->local_import_begin(),
2231 IEnd = Context->local_import_end();
2232 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002233 if (Module *ImportedFrom
2234 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2235 SrcMgr))) {
2236 ImportedFrom->Imports.push_back(I->getImportedModule());
2237 }
2238 }
2239
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002240 // Enter the submodule description block.
2241 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2242
2243 // Write the abbreviations needed for the submodules block.
2244 using namespace llvm;
2245 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2246 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2257 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2258
2259 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002260 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2262 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2263
2264 Abbrev = new BitCodeAbbrev();
2265 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2267 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002268
2269 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002270 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2272 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2273
2274 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002275 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2277 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2278
Douglas Gregor51f564f2011-12-31 04:05:44 +00002279 Abbrev = new BitCodeAbbrev();
2280 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2282 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2283
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002284 Abbrev = new BitCodeAbbrev();
2285 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2287 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2288
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002289 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002290 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2292 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2293
2294 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002295 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2298 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2299
Douglas Gregor63a72682013-03-20 00:22:05 +00002300 Abbrev = new BitCodeAbbrev();
2301 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2303 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2304
Douglas Gregor906d66a2013-03-20 21:10:35 +00002305 Abbrev = new BitCodeAbbrev();
2306 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2309 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2310
Douglas Gregor26ced122011-12-01 00:59:36 +00002311 // Write the submodule metadata block.
2312 RecordData Record;
2313 Record.push_back(getNumberOfModules(WritingModule));
2314 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2315 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2316
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002317 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002318 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002319 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002320 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002321 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002322 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002323 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002324
2325 // Emit the definition of the block.
2326 Record.clear();
2327 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002328 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002329 if (Mod->Parent) {
2330 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2331 Record.push_back(SubmoduleIDs[Mod->Parent]);
2332 } else {
2333 Record.push_back(0);
2334 }
2335 Record.push_back(Mod->IsFramework);
2336 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002337 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002338 Record.push_back(Mod->InferSubmodules);
2339 Record.push_back(Mod->InferExplicitSubmodules);
2340 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002341 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002342 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2343
Douglas Gregor51f564f2011-12-31 04:05:44 +00002344 // Emit the requirements.
2345 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2346 Record.clear();
2347 Record.push_back(SUBMODULE_REQUIRES);
2348 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2349 Mod->Requires[I].data(),
2350 Mod->Requires[I].size());
2351 }
2352
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002353 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002354 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002355 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002356 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002357 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002358 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002359 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2360 Record.clear();
2361 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2362 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2363 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002364 }
2365
2366 // Emit the headers.
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002367 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002368 Record.clear();
2369 Record.push_back(SUBMODULE_HEADER);
2370 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002371 Mod->NormalHeaders[I]->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002372 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002373 // Emit the excluded headers.
2374 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2375 Record.clear();
2376 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2377 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2378 Mod->ExcludedHeaders[I]->getName());
2379 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002380 // Emit the private headers.
2381 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2382 Record.clear();
2383 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2384 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2385 Mod->PrivateHeaders[I]->getName());
2386 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002387 ArrayRef<const FileEntry *>
2388 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2389 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002390 Record.clear();
2391 Record.push_back(SUBMODULE_TOPHEADER);
2392 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002393 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002394 }
Douglas Gregor55988682011-12-05 16:33:54 +00002395
2396 // Emit the imports.
2397 if (!Mod->Imports.empty()) {
2398 Record.clear();
2399 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002400 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002401 assert(ImportedID && "Unknown submodule!");
2402 Record.push_back(ImportedID);
2403 }
2404 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2405 }
2406
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002407 // Emit the exports.
2408 if (!Mod->Exports.empty()) {
2409 Record.clear();
2410 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002411 if (Module *Exported = Mod->Exports[I].getPointer()) {
2412 unsigned ExportedID = SubmoduleIDs[Exported];
2413 assert(ExportedID > 0 && "Unknown submodule ID?");
2414 Record.push_back(ExportedID);
2415 } else {
2416 Record.push_back(0);
2417 }
2418
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002419 Record.push_back(Mod->Exports[I].getInt());
2420 }
2421 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2422 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002423
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00002424 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2425 // Might be unnecessary as use declarations are only used to build the
2426 // module itself.
2427
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002428 // Emit the link libraries.
2429 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2430 Record.clear();
2431 Record.push_back(SUBMODULE_LINK_LIBRARY);
2432 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2433 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2434 Mod->LinkLibraries[I].Library);
2435 }
2436
Douglas Gregor906d66a2013-03-20 21:10:35 +00002437 // Emit the conflicts.
2438 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2439 Record.clear();
2440 Record.push_back(SUBMODULE_CONFLICT);
2441 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2442 assert(OtherID && "Unknown submodule!");
2443 Record.push_back(OtherID);
2444 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2445 Mod->Conflicts[I].Message);
2446 }
2447
Douglas Gregor63a72682013-03-20 00:22:05 +00002448 // Emit the configuration macros.
2449 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2450 Record.clear();
2451 Record.push_back(SUBMODULE_CONFIG_MACRO);
2452 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2453 Mod->ConfigMacros[I]);
2454 }
2455
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002456 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002457 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2458 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002459 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002460 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002461 }
2462
2463 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002464
2465 assert((NextSubmoduleID - FirstSubmoduleID
2466 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002467}
2468
Douglas Gregor185dbd72011-12-01 02:07:58 +00002469serialization::SubmoduleID
2470ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002471 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002472 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002473
2474 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002475 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002476 Module *OwningMod
2477 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002478 if (!OwningMod)
2479 return 0;
2480
Douglas Gregore209e502011-12-06 01:10:29 +00002481 // Check whether this submodule is part of our own module.
2482 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002483 return 0;
2484
Douglas Gregore209e502011-12-06 01:10:29 +00002485 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002486}
2487
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002488void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2489 bool isModule) {
2490 // Make sure set diagnostic pragmas don't affect the translation unit that
2491 // imports the module.
2492 // FIXME: Make diagnostic pragma sections work properly with modules.
2493 if (isModule)
2494 return;
2495
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002496 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2497 DiagStateIDMap;
2498 unsigned CurrID = 0;
2499 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002500 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002501 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002502 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2503 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002504 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002505 if (point.Loc.isInvalid())
2506 continue;
2507
2508 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002509 unsigned &DiagStateID = DiagStateIDMap[point.State];
2510 Record.push_back(DiagStateID);
2511
2512 if (DiagStateID == 0) {
2513 DiagStateID = ++CurrID;
2514 for (DiagnosticsEngine::DiagState::const_iterator
2515 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2516 if (I->second.isPragma()) {
2517 Record.push_back(I->first);
2518 Record.push_back(I->second.getMapping());
2519 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002520 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002521 Record.push_back(-1); // mark the end of the diag/map pairs for this
2522 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002523 }
2524 }
2525
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002526 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002527 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002528}
2529
Anders Carlssonc8505782011-03-06 18:41:18 +00002530void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2531 if (CXXBaseSpecifiersOffsets.empty())
2532 return;
2533
2534 RecordData Record;
2535
2536 // Create a blob abbreviation for the C++ base specifiers offsets.
2537 using namespace llvm;
2538
2539 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2540 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2543 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2544
Douglas Gregore92b8a12011-08-04 00:01:48 +00002545 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002546 Record.clear();
2547 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2548 Record.push_back(CXXBaseSpecifiersOffsets.size());
2549 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002550 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002551}
2552
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002553//===----------------------------------------------------------------------===//
2554// Type Serialization
2555//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002556
Sebastian Redl3397c552010-08-18 23:56:27 +00002557/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002558void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002559 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002560 if (Idx.getIndex() == 0) // we haven't seen this type before.
2561 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002562
Douglas Gregor97475832010-10-05 18:37:06 +00002563 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002564
Douglas Gregor2cf26342009-04-09 22:27:44 +00002565 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002566 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002567 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002568 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002569 else if (TypeOffsets.size() < Index) {
2570 TypeOffsets.resize(Index + 1);
2571 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002572 }
2573
2574 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Douglas Gregor2cf26342009-04-09 22:27:44 +00002576 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002577 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002578
Douglas Gregora4923eb2009-11-16 21:35:15 +00002579 if (T.hasLocalNonFastQualifiers()) {
2580 Qualifiers Qs = T.getLocalQualifiers();
2581 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002582 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002583 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002584 } else {
2585 switch (T->getTypeClass()) {
2586 // For all of the concrete, non-dependent types, call the
2587 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002588#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002589 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002590#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002591#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002592 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002593 }
2594
2595 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002596 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002597
2598 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002599 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002600}
2601
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002602//===----------------------------------------------------------------------===//
2603// Declaration Serialization
2604//===----------------------------------------------------------------------===//
2605
Douglas Gregor2cf26342009-04-09 22:27:44 +00002606/// \brief Write the block containing all of the declaration IDs
2607/// lexically declared within the given DeclContext.
2608///
2609/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2610/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002611uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002612 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002613 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002614 return 0;
2615
Douglas Gregorc9490c02009-04-16 22:23:12 +00002616 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002617 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002618 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002619 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002620 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2621 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002622 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002623
Douglas Gregor25123082009-04-22 22:34:57 +00002624 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002625 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002626 return Offset;
2627}
2628
Sebastian Redla4232eb2010-08-18 23:56:21 +00002629void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002630 using namespace llvm;
2631 RecordData Record;
2632
2633 // Write the type offsets array
2634 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002635 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2639 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2640 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002641 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002642 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002643 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002644 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002645
2646 // Write the declaration offsets array
2647 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002648 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002649 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2652 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2653 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002654 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002655 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002656 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002657 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002658}
2659
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002660void ASTWriter::WriteFileDeclIDsMap() {
2661 using namespace llvm;
2662 RecordData Record;
2663
2664 // Join the vectors of DeclIDs from all files.
2665 SmallVector<DeclID, 256> FileSortedIDs;
2666 for (FileDeclIDsTy::iterator
2667 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2668 DeclIDInFileInfo &Info = *FI->second;
2669 Info.FirstDeclIndex = FileSortedIDs.size();
2670 for (LocDeclIDsTy::iterator
2671 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2672 FileSortedIDs.push_back(DI->second);
2673 }
2674
2675 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2676 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002678 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2679 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2680 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002681 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002682 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2683}
2684
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002685void ASTWriter::WriteComments() {
2686 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002687 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002688 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002689 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2690 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002691 I != E; ++I) {
2692 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002693 AddSourceRange((*I)->getSourceRange(), Record);
2694 Record.push_back((*I)->getKind());
2695 Record.push_back((*I)->isTrailingComment());
2696 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002697 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2698 }
2699 Stream.ExitBlock();
2700}
2701
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002702//===----------------------------------------------------------------------===//
2703// Global Method Pool and Selector Serialization
2704//===----------------------------------------------------------------------===//
2705
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002706namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002707// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002708class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002709 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002710
2711public:
2712 typedef Selector key_type;
2713 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Sebastian Redl5d050072010-08-04 17:20:04 +00002715 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002716 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002717 ObjCMethodList Instance, Factory;
2718 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002719 typedef const data_type& data_type_ref;
2720
Sebastian Redl3397c552010-08-18 23:56:27 +00002721 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002723 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002724 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002725 }
Mike Stump1eb44332009-09-09 15:08:12 +00002726
2727 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002728 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002729 data_type_ref Methods) {
2730 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2731 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002732 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2733 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002734 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002735 if (Method->Method)
2736 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002737 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002738 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002739 if (Method->Method)
2740 DataLen += 4;
2741 clang::io::Emit16(Out, DataLen);
2742 return std::make_pair(KeyLen, DataLen);
2743 }
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Chris Lattner5f9e2722011-07-23 10:55:15 +00002745 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002746 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002747 assert((Start >> 32) == 0 && "Selector key offset too large");
2748 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002749 unsigned N = Sel.getNumArgs();
2750 clang::io::Emit16(Out, N);
2751 if (N == 0)
2752 N = 1;
2753 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002754 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002755 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2756 }
Mike Stump1eb44332009-09-09 15:08:12 +00002757
Chris Lattner5f9e2722011-07-23 10:55:15 +00002758 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002759 data_type_ref Methods, unsigned DataLen) {
2760 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002761 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002762 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002763 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002764 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002765 if (Method->Method)
2766 ++NumInstanceMethods;
2767
2768 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002769 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002770 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002771 if (Method->Method)
2772 ++NumFactoryMethods;
2773
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002774 unsigned InstanceBits = Methods.Instance.getBits();
2775 assert(InstanceBits < 4);
2776 unsigned NumInstanceMethodsAndBits =
2777 (NumInstanceMethods << 2) | InstanceBits;
2778 unsigned FactoryBits = Methods.Factory.getBits();
2779 assert(FactoryBits < 4);
2780 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2781 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2782 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002783 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002784 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002785 if (Method->Method)
2786 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002787 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002788 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002789 if (Method->Method)
2790 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002791
2792 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002793 }
2794};
2795} // end anonymous namespace
2796
Sebastian Redl059612d2010-08-03 21:58:15 +00002797/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002798///
2799/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002800/// in an on-disk hash table indexed by the selector. The hash table also
2801/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002802void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002803 using namespace llvm;
2804
Sebastian Redl059612d2010-08-03 21:58:15 +00002805 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002806 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002807 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002808 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002809 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002810 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002811 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002812 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002813
Sebastian Redl059612d2010-08-03 21:58:15 +00002814 // Create the on-disk hash table representation. We walk through every
2815 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002816 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002817 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002818 I = SelectorIDs.begin(), E = SelectorIDs.end();
2819 I != E; ++I) {
2820 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002821 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002822 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002823 I->second,
2824 ObjCMethodList(),
2825 ObjCMethodList()
2826 };
2827 if (F != SemaRef.MethodPool.end()) {
2828 Data.Instance = F->second.first;
2829 Data.Factory = F->second.second;
2830 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002831 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002832 // changed.
2833 if (Chain && I->second < FirstSelectorID) {
2834 // Selector already exists. Did it change?
2835 bool changed = false;
2836 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002837 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002838 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002839 changed = true;
2840 }
2841 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002842 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002843 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002844 changed = true;
2845 }
2846 if (!changed)
2847 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002848 } else if (Data.Instance.Method || Data.Factory.Method) {
2849 // A new method pool entry.
2850 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002851 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002852 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002853 }
2854
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002855 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002856 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002857 uint32_t BucketOffset;
2858 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002859 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002860 llvm::raw_svector_ostream Out(MethodPool);
2861 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002862 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002863 BucketOffset = Generator.Emit(Out, Trait);
2864 }
2865
2866 // Create a blob abbreviation
2867 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002868 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2872 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2873
Douglas Gregor83941df2009-04-25 17:48:32 +00002874 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002875 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002876 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002877 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002878 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002879 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002880
2881 // Create a blob abbreviation for the selector table offsets.
2882 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002883 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2887 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2888
2889 // Write the selector offsets table.
2890 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002891 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002892 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002893 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002894 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002895 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002896 }
2897}
2898
Sebastian Redl3397c552010-08-18 23:56:27 +00002899/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002900void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002901 using namespace llvm;
2902 if (SemaRef.ReferencedSelectors.empty())
2903 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002904
Fariborz Jahanian32019832010-07-23 19:11:11 +00002905 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002906
Sebastian Redl3397c552010-08-18 23:56:27 +00002907 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002908 // very tricky to fix, and given that @selector shouldn't really appear in
2909 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002910 for (DenseMap<Selector, SourceLocation>::iterator S =
2911 SemaRef.ReferencedSelectors.begin(),
2912 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2913 Selector Sel = (*S).first;
2914 SourceLocation Loc = (*S).second;
2915 AddSelectorRef(Sel, Record);
2916 AddSourceLocation(Loc, Record);
2917 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002918 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002919}
2920
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002921//===----------------------------------------------------------------------===//
2922// Identifier Table Serialization
2923//===----------------------------------------------------------------------===//
2924
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002925namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002926class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002927 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002928 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002929 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002930 bool IsModule;
2931
Douglas Gregora92193e2009-04-28 21:18:29 +00002932 /// \brief Determines whether this is an "interesting" identifier
2933 /// that needs a full IdentifierInfo structure written into the hash
2934 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002935 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002936 if (II->isPoisoned() ||
2937 II->isExtensionToken() ||
2938 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002939 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002940 II->getFETokenInfo<void>())
2941 return true;
2942
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002943 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002944 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002945
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002946 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002947 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002948 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002949
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002950 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2951 if (!IsModule)
2952 return !shouldIgnoreMacro(Macro, IsModule, PP);
2953 SubmoduleID ModID;
2954 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2955 return true;
2956 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002957
2958 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002959 }
2960
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002961 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2962 SubmoduleID &ModID) {
2963 ModID = 0;
2964 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2965 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2966 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002967 return 0;
2968 }
2969
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002970 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2971 SubmoduleID &ModID) {
2972 if (DefMacroDirective *
2973 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2974 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2975 return DefMD;
2976 return 0;
2977 }
2978
2979 /// \brief Traverses the macro directives history and returns the latest
2980 /// macro that is public and not undefined in the same submodule.
2981 /// A macro that is defined in submodule A and undefined in submodule B,
2982 /// will still be considered as defined/exported from submodule A.
2983 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2984 SubmoduleID &ModID) {
2985 if (!MD)
2986 return 0;
2987
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002988 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002989 bool isUndefined = false;
2990 Optional<bool> isPublic;
2991 for (; MD; MD = MD->getPrevious()) {
2992 if (MD->isHidden())
2993 continue;
2994
2995 SubmoduleID ThisModID = getSubmoduleID(MD);
2996 if (ThisModID == 0) {
2997 isUndefined = false;
2998 isPublic = Optional<bool>();
2999 continue;
3000 }
3001 if (ThisModID != ModID){
3002 ModID = ThisModID;
3003 isUndefined = false;
3004 isPublic = Optional<bool>();
3005 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003006 // We are looking for a definition in a different submodule than the one
3007 // that we started with. If a submodule has re-definitions of the same
3008 // macro, only the last definition will be used as the "exported" one.
3009 if (ModID == OrigModID)
3010 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003011
3012 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3013 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
3014 return DefMD;
3015 continue;
3016 }
3017
3018 if (isa<UndefMacroDirective>(MD)) {
3019 isUndefined = true;
3020 continue;
3021 }
3022
3023 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
3024 if (!isPublic.hasValue())
3025 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003026 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003027
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003028 return 0;
3029 }
3030
3031 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003032 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3033 MacroInfo *MI = DefMD->getInfo();
3034 if (unsigned ID = MI->getOwningModuleID())
3035 return ID;
3036 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3037 }
3038 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003039 }
3040
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003041public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003042 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003043 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003045 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003046 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003047
Douglas Gregoreee242f2011-10-27 09:33:13 +00003048 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3049 IdentifierResolver &IdResolver, bool IsModule)
3050 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003051
3052 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003053 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003054 }
Mike Stump1eb44332009-09-09 15:08:12 +00003055
3056 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003057 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003058 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003059 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003060 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003061 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003062 DataLen += 2; // 2 bytes for builtin ID
3063 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003064 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003065 DataLen += 4; // MacroDirectives offset.
3066 if (IsModule) {
3067 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003068 for (DefMacroDirective *
3069 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3070 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003071 DataLen += 4; // MacroInfo ID.
3072 }
3073 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003074 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003075 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003076
Douglas Gregoreee242f2011-10-27 09:33:13 +00003077 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3078 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003079 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003080 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003081 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003082 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003083 // We emit the key length after the data length so that every
3084 // string is preceded by a 16-bit length. This matches the PTH
3085 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003086 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003087 return std::make_pair(KeyLen, DataLen);
3088 }
Mike Stump1eb44332009-09-09 15:08:12 +00003089
Chris Lattner5f9e2722011-07-23 10:55:15 +00003090 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003091 unsigned KeyLen) {
3092 // Record the location of the key data. This is used when generating
3093 // the mapping from persistent IDs to strings.
3094 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003095 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003096 }
Mike Stump1eb44332009-09-09 15:08:12 +00003097
Douglas Gregor7143aab2011-09-01 17:04:32 +00003098 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003099 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003100 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003101 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003102 clang::io::Emit32(Out, ID << 1);
3103 return;
3104 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003105
Douglas Gregora92193e2009-04-28 21:18:29 +00003106 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003107 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3108 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3109 clang::io::Emit16(Out, Bits);
3110 Bits = 0;
3111 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003112 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003113 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003114 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3115 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003116 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003117 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003118 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003119
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003120 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003121 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3122 if (IsModule) {
3123 // Write the IDs of macros coming from different submodules.
3124 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003125 for (DefMacroDirective *
3126 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3127 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3128 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003129 assert(InfoID);
3130 clang::io::Emit32(Out, InfoID);
3131 }
3132 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003133 }
Douglas Gregor13292642011-12-02 15:45:10 +00003134 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003135
Douglas Gregor668c1a42009-04-21 22:25:48 +00003136 // Emit the declaration IDs in reverse order, because the
3137 // IdentifierResolver provides the declarations as they would be
3138 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003139 // "stat"), but the ASTReader adds declarations to the end of the list
3140 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003141 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003142 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3143 IdResolver.end());
Craig Topper09d19ef2013-07-04 03:08:24 +00003144 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003145 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003146 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003147 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3148 }
3149
3150 /// \brief Returns the most recent local decl or the given decl if there are
3151 /// no local ones. The given decl is assumed to be the most recent one.
3152 Decl *getMostRecentLocalDecl(Decl *Orig) {
3153 // The only way a "from AST file" decl would be more recent from a local one
3154 // is if it came from a module.
3155 if (!PP.getLangOpts().Modules)
3156 return Orig;
3157
3158 // Look for a local in the decl chain.
3159 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3160 if (!D->isFromASTFile())
3161 return D;
3162 // If we come up a decl from a (chained-)PCH stop since we won't find a
3163 // local one.
3164 if (D->getOwningModuleID() == 0)
3165 break;
3166 }
3167
3168 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003169 }
3170};
3171} // end anonymous namespace
3172
Sebastian Redl3397c552010-08-18 23:56:27 +00003173/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003174///
3175/// The identifier table consists of a blob containing string data
3176/// (the actual identifiers themselves) and a separate "offsets" index
3177/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003178void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3179 IdentifierResolver &IdResolver,
3180 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003181 using namespace llvm;
3182
3183 // Create and write out the blob that contains the identifier
3184 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003185 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003186 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003187 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Douglas Gregor92b059e2009-04-28 20:33:11 +00003189 // Look for any identifiers that were named while processing the
3190 // headers, but are otherwise not needed. We add these to the hash
3191 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003192 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003193 // file.
3194 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3195 IDEnd = PP.getIdentifierTable().end();
3196 ID != IDEnd; ++ID)
3197 getIdentifierRef(ID->second);
3198
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003199 // Create the on-disk hash table representation. We only store offsets
3200 // for identifiers that appear here for the first time.
3201 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003202 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003203 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3204 ID != IDEnd; ++ID) {
3205 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003206 if (!Chain || !ID->first->isFromAST() ||
3207 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003208 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003209 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003210 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003211
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003212 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003213 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003214 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003215 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003216 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003217 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003218 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003219 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003220 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003221 }
3222
3223 // Create a blob abbreviation
3224 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003225 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003228 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003229
3230 // Write the identifier table
3231 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003232 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003233 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003234 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003235 }
3236
3237 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003239 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3243 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3244
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003245#ifndef NDEBUG
3246 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3247 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3248#endif
3249
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003250 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003251 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003252 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003253 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003254 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003255 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003256}
3257
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003258//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003259// DeclContext's Name Lookup Table Serialization
3260//===----------------------------------------------------------------------===//
3261
3262namespace {
3263// Trait used for the on-disk hash table used in the method pool.
3264class ASTDeclContextNameLookupTrait {
3265 ASTWriter &Writer;
3266
3267public:
3268 typedef DeclarationName key_type;
3269 typedef key_type key_type_ref;
3270
3271 typedef DeclContext::lookup_result data_type;
3272 typedef const data_type& data_type_ref;
3273
3274 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3275
3276 unsigned ComputeHash(DeclarationName Name) {
3277 llvm::FoldingSetNodeID ID;
3278 ID.AddInteger(Name.getNameKind());
3279
3280 switch (Name.getNameKind()) {
3281 case DeclarationName::Identifier:
3282 ID.AddString(Name.getAsIdentifierInfo()->getName());
3283 break;
3284 case DeclarationName::ObjCZeroArgSelector:
3285 case DeclarationName::ObjCOneArgSelector:
3286 case DeclarationName::ObjCMultiArgSelector:
3287 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3288 break;
3289 case DeclarationName::CXXConstructorName:
3290 case DeclarationName::CXXDestructorName:
3291 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003292 break;
3293 case DeclarationName::CXXOperatorName:
3294 ID.AddInteger(Name.getCXXOverloadedOperator());
3295 break;
3296 case DeclarationName::CXXLiteralOperatorName:
3297 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3298 case DeclarationName::CXXUsingDirective:
3299 break;
3300 }
3301
3302 return ID.ComputeHash();
3303 }
3304
3305 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003306 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003307 data_type_ref Lookup) {
3308 unsigned KeyLen = 1;
3309 switch (Name.getNameKind()) {
3310 case DeclarationName::Identifier:
3311 case DeclarationName::ObjCZeroArgSelector:
3312 case DeclarationName::ObjCOneArgSelector:
3313 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003314 case DeclarationName::CXXLiteralOperatorName:
3315 KeyLen += 4;
3316 break;
3317 case DeclarationName::CXXOperatorName:
3318 KeyLen += 1;
3319 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003320 case DeclarationName::CXXConstructorName:
3321 case DeclarationName::CXXDestructorName:
3322 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003323 case DeclarationName::CXXUsingDirective:
3324 break;
3325 }
3326 clang::io::Emit16(Out, KeyLen);
3327
3328 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003329 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003330 clang::io::Emit16(Out, DataLen);
3331
3332 return std::make_pair(KeyLen, DataLen);
3333 }
3334
Chris Lattner5f9e2722011-07-23 10:55:15 +00003335 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003336 using namespace clang::io;
3337
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003338 Emit8(Out, Name.getNameKind());
3339 switch (Name.getNameKind()) {
3340 case DeclarationName::Identifier:
3341 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003342 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003343 case DeclarationName::ObjCZeroArgSelector:
3344 case DeclarationName::ObjCOneArgSelector:
3345 case DeclarationName::ObjCMultiArgSelector:
3346 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003347 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003348 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003349 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3350 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003351 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003352 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003353 case DeclarationName::CXXLiteralOperatorName:
3354 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003355 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003356 case DeclarationName::CXXConstructorName:
3357 case DeclarationName::CXXDestructorName:
3358 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003359 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003360 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003361 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003362
3363 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003364 }
3365
Chris Lattner5f9e2722011-07-23 10:55:15 +00003366 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003367 data_type Lookup, unsigned DataLen) {
3368 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003369 clang::io::Emit16(Out, Lookup.size());
3370 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3371 I != E; ++I)
3372 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003373
3374 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3375 }
3376};
3377} // end anonymous namespace
3378
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003379/// \brief Write the block containing all of the declaration IDs
3380/// visible from the given DeclContext.
3381///
3382/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003383/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003384uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3385 DeclContext *DC) {
3386 if (DC->getPrimaryContext() != DC)
3387 return 0;
3388
3389 // Since there is no name lookup into functions or methods, don't bother to
3390 // build a visible-declarations table for these entities.
3391 if (DC->isFunctionOrMethod())
3392 return 0;
3393
3394 // If not in C++, we perform name lookup for the translation unit via the
3395 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003396 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003397 return 0;
3398
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003399 // Serialize the contents of the mapping used for lookup. Note that,
3400 // although we have two very different code paths, the serialized
3401 // representation is the same for both cases: a declaration name,
3402 // followed by a size, followed by references to the visible
3403 // declarations that have that name.
3404 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003405 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003406 if (!Map || Map->empty())
3407 return 0;
3408
3409 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3410 ASTDeclContextNameLookupTrait Trait(*this);
3411
3412 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003413 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003414 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003415 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3416 D != DEnd; ++D) {
3417 DeclarationName Name = D->first;
3418 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003419 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003420 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3421 // Hash all conversion function names to the same name. The actual
3422 // type information in conversion function name is not used in the
3423 // key (since such type information is not stable across different
3424 // modules), so the intended effect is to coalesce all of the conversion
3425 // functions under a single key.
3426 if (!ConversionName)
3427 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003428 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003429 continue;
3430 }
3431
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003432 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003433 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003434 }
3435
Douglas Gregore5a54b62011-08-30 20:49:19 +00003436 // Add the conversion functions
3437 if (!ConversionDecls.empty()) {
3438 Generator.insert(ConversionName,
3439 DeclContext::lookup_result(ConversionDecls.begin(),
3440 ConversionDecls.end()),
3441 Trait);
3442 }
3443
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003444 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003445 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003446 uint32_t BucketOffset;
3447 {
3448 llvm::raw_svector_ostream Out(LookupTable);
3449 // Make sure that no bucket is at offset 0
3450 clang::io::Emit32(Out, 0);
3451 BucketOffset = Generator.Emit(Out, Trait);
3452 }
3453
3454 // Write the lookup table
3455 RecordData Record;
3456 Record.push_back(DECL_CONTEXT_VISIBLE);
3457 Record.push_back(BucketOffset);
3458 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3459 LookupTable.str());
3460
3461 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3462 ++NumVisibleDeclContexts;
3463 return Offset;
3464}
3465
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003466/// \brief Write an UPDATE_VISIBLE block for the given context.
3467///
3468/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3469/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003470/// (in C++), for namespaces, and for classes with forward-declared unscoped
3471/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003472void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003473 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3474 if (!Map || Map->empty())
3475 return;
3476
3477 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3478 ASTDeclContextNameLookupTrait Trait(*this);
3479
3480 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003481 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3482 D != DEnd; ++D) {
3483 DeclarationName Name = D->first;
3484 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003485 // For any name that appears in this table, the results are complete, i.e.
3486 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003487 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003488 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003489 }
3490
3491 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003492 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003493 uint32_t BucketOffset;
3494 {
3495 llvm::raw_svector_ostream Out(LookupTable);
3496 // Make sure that no bucket is at offset 0
3497 clang::io::Emit32(Out, 0);
3498 BucketOffset = Generator.Emit(Out, Trait);
3499 }
3500
3501 // Write the lookup table
3502 RecordData Record;
3503 Record.push_back(UPDATE_VISIBLE);
3504 Record.push_back(getDeclID(cast<Decl>(DC)));
3505 Record.push_back(BucketOffset);
3506 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3507}
3508
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003509/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3510void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3511 RecordData Record;
3512 Record.push_back(Opts.fp_contract);
3513 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3514}
3515
3516/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3517void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003518 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003519 return;
3520
3521 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3522 RecordData Record;
3523#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3524#include "clang/Basic/OpenCLExtensions.def"
3525 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3526}
3527
Douglas Gregor2171bf12012-01-15 16:58:34 +00003528void ASTWriter::WriteRedeclarations() {
3529 RecordData LocalRedeclChains;
3530 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3531
3532 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3533 Decl *First = Redeclarations[I];
Rafael Espindola7693b322013-10-19 02:13:21 +00003534 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor2171bf12012-01-15 16:58:34 +00003535
3536 Decl *MostRecent = First->getMostRecentDecl();
3537
3538 // If we only have a single declaration, there is no point in storing
3539 // a redeclaration chain.
3540 if (First == MostRecent)
3541 continue;
3542
3543 unsigned Offset = LocalRedeclChains.size();
3544 unsigned Size = 0;
3545 LocalRedeclChains.push_back(0); // Placeholder for the size.
3546
3547 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003548 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003549 Prev = Prev->getPreviousDecl()) {
3550 if (!Prev->isFromASTFile()) {
3551 AddDeclRef(Prev, LocalRedeclChains);
3552 ++Size;
3553 }
3554 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003555
3556 if (!First->isFromASTFile() && Chain) {
3557 Decl *FirstFromAST = MostRecent;
3558 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3559 if (Prev->isFromASTFile())
3560 FirstFromAST = Prev;
3561 }
3562
3563 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3564 }
3565
Douglas Gregor2171bf12012-01-15 16:58:34 +00003566 LocalRedeclChains[Offset] = Size;
3567
3568 // Reverse the set of local redeclarations, so that we store them in
3569 // order (since we found them in reverse order).
3570 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3571
Douglas Gregoraa945902013-02-18 15:53:43 +00003572 // Add the mapping from the first ID from the AST to the set of local
3573 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003574 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3575 LocalRedeclsMap.push_back(Info);
3576
3577 assert(N == Redeclarations.size() &&
3578 "Deserialized a declaration we shouldn't have");
3579 }
3580
3581 if (LocalRedeclChains.empty())
3582 return;
3583
3584 // Sort the local redeclarations map by the first declaration ID,
3585 // since the reader will be performing binary searches on this information.
3586 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3587
3588 // Emit the local redeclarations map.
3589 using namespace llvm;
3590 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3591 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3594 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3595
3596 RecordData Record;
3597 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3598 Record.push_back(LocalRedeclsMap.size());
3599 Stream.EmitRecordWithBlob(AbbrevID, Record,
3600 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3601 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3602
3603 // Emit the redeclaration chains.
3604 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3605}
3606
Douglas Gregorcff9f262012-01-27 01:47:08 +00003607void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003608 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003609 RecordData Categories;
3610
3611 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3612 unsigned Size = 0;
3613 unsigned StartIndex = Categories.size();
3614
3615 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3616
3617 // Allocate space for the size.
3618 Categories.push_back(0);
3619
3620 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003621 for (ObjCInterfaceDecl::known_categories_iterator
3622 Cat = Class->known_categories_begin(),
3623 CatEnd = Class->known_categories_end();
3624 Cat != CatEnd; ++Cat, ++Size) {
3625 assert(getDeclID(*Cat) != 0 && "Bogus category");
3626 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003627 }
3628
3629 // Update the size.
3630 Categories[StartIndex] = Size;
3631
3632 // Record this interface -> category map.
3633 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3634 CategoriesMap.push_back(CatInfo);
3635 }
3636
3637 // Sort the categories map by the definition ID, since the reader will be
3638 // performing binary searches on this information.
3639 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3640
3641 // Emit the categories map.
3642 using namespace llvm;
3643 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3644 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3647 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3648
3649 RecordData Record;
3650 Record.push_back(OBJC_CATEGORIES_MAP);
3651 Record.push_back(CategoriesMap.size());
3652 Stream.EmitRecordWithBlob(AbbrevID, Record,
3653 reinterpret_cast<char*>(CategoriesMap.data()),
3654 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3655
3656 // Emit the category lists.
3657 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3658}
3659
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003660void ASTWriter::WriteMergedDecls() {
3661 if (!Chain || Chain->MergedDecls.empty())
3662 return;
3663
3664 RecordData Record;
3665 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3666 IEnd = Chain->MergedDecls.end();
3667 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003668 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003669 : getDeclID(I->first);
3670 assert(CanonID && "Merged declaration not known?");
3671
3672 Record.push_back(CanonID);
3673 Record.push_back(I->second.size());
3674 Record.append(I->second.begin(), I->second.end());
3675 }
3676 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3677}
3678
Richard Smithac32d902013-08-07 21:41:30 +00003679void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3680 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3681
3682 if (LPTMap.empty())
3683 return;
3684
3685 RecordData Record;
3686 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3687 ItEnd = LPTMap.end();
3688 It != ItEnd; ++It) {
3689 LateParsedTemplate *LPT = It->second;
3690 AddDeclRef(It->first, Record);
3691 AddDeclRef(LPT->D, Record);
3692 Record.push_back(LPT->Toks.size());
3693
3694 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3695 TokEnd = LPT->Toks.end();
3696 TokIt != TokEnd; ++TokIt) {
3697 AddToken(*TokIt, Record);
3698 }
3699 }
3700 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3701}
3702
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003703//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003704// General Serialization Routines
3705//===----------------------------------------------------------------------===//
3706
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003707/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003708void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3709 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003710 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003711 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3712 e = Attrs.end(); i != e; ++i){
3713 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003714 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003715 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003716
Sean Huntcf807c42010-08-18 23:23:40 +00003717#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003718
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003719 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003720}
3721
John McCallaeeacf72013-05-03 00:10:13 +00003722void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3723 AddSourceLocation(Tok.getLocation(), Record);
3724 Record.push_back(Tok.getLength());
3725
3726 // FIXME: When reading literal tokens, reconstruct the literal pointer
3727 // if it is needed.
3728 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3729 // FIXME: Should translate token kind to a stable encoding.
3730 Record.push_back(Tok.getKind());
3731 // FIXME: Should translate token flags to a stable encoding.
3732 Record.push_back(Tok.getFlags());
3733}
3734
Chris Lattner5f9e2722011-07-23 10:55:15 +00003735void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003736 Record.push_back(Str.size());
3737 Record.insert(Record.end(), Str.begin(), Str.end());
3738}
3739
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003740void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3741 RecordDataImpl &Record) {
3742 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003743 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003744 Record.push_back(*Minor + 1);
3745 else
3746 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003747 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003748 Record.push_back(*Subminor + 1);
3749 else
3750 Record.push_back(0);
3751}
3752
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003753/// \brief Note that the identifier II occurs at the given offset
3754/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003755void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003756 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003757 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003758 // up earlier in the chain and thus don't need an offset.
3759 if (ID >= FirstIdentID)
3760 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003761}
3762
Douglas Gregor83941df2009-04-25 17:48:32 +00003763/// \brief Note that the selector Sel occurs at the given offset
3764/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003765void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003766 unsigned ID = SelectorIDs[Sel];
3767 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003768 // Don't record offsets for selectors that are also available in a different
3769 // file.
3770 if (ID < FirstSelectorID)
3771 return;
3772 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003773}
3774
Sebastian Redla4232eb2010-08-18 23:56:21 +00003775ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003776 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003777 WritingAST(false), DoneWritingDeclsAndTypes(false),
3778 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003779 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003780 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003781 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3782 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003783 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3784 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003785 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003786 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003787 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003788 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003789 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003790 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003791 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3792 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3793 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003794 DeclTypedefAbbrev(0),
3795 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3796 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003797{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003798}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003799
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003800ASTWriter::~ASTWriter() {
3801 for (FileDeclIDsTy::iterator
3802 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3803 delete I->second;
3804}
3805
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003806void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003807 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003808 Module *WritingModule, StringRef isysroot,
3809 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003810 WritingAST = true;
3811
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003812 ASTHasCompilerErrors = hasErrors;
3813
Douglas Gregor2cf26342009-04-09 22:27:44 +00003814 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003815 Stream.Emit((unsigned)'C', 8);
3816 Stream.Emit((unsigned)'P', 8);
3817 Stream.Emit((unsigned)'C', 8);
3818 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003819
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003820 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003821
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003822 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003823 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003824 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003825 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003826 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003827 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003828 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003829
3830 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003831}
3832
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003833template<typename Vector>
3834static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3835 ASTWriter::RecordData &Record) {
3836 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3837 I != E; ++I) {
3838 Writer.AddDeclRef(*I, Record);
3839 }
3840}
3841
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003842void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003843 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003844 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003845 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003846 using namespace llvm;
3847
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003848 bool isModule = WritingModule != 0;
3849
Douglas Gregorecc2c092011-12-01 22:20:10 +00003850 // Make sure that the AST reader knows to finalize itself.
3851 if (Chain)
3852 Chain->finalizeForWriting();
3853
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003854 ASTContext &Context = SemaRef.Context;
3855 Preprocessor &PP = SemaRef.PP;
3856
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003857 // Set up predefined declaration IDs.
3858 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003859 if (Context.ObjCIdDecl)
3860 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003861 if (Context.ObjCSelDecl)
3862 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003863 if (Context.ObjCClassDecl)
3864 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003865 if (Context.ObjCProtocolClassDecl)
3866 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003867 if (Context.Int128Decl)
3868 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3869 if (Context.UInt128Decl)
3870 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003871 if (Context.ObjCInstanceTypeDecl)
3872 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003873 if (Context.BuiltinVaListDecl)
3874 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3875
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003876 if (!Chain) {
3877 // Make sure that we emit IdentifierInfos (and any attached
3878 // declarations) for builtins. We don't need to do this when we're
3879 // emitting chained PCH files, because all of the builtins will be
3880 // in the original PCH file.
3881 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003882 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003883 SmallVector<const char *, 32> BuiltinNames;
Eli Bendersky97a03cf2013-07-11 16:53:04 +00003884 if (!Context.getLangOpts().NoBuiltin) {
3885 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3886 }
Douglas Gregor2deaea32009-04-22 18:49:13 +00003887 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3888 getIdentifierRef(&Table.get(BuiltinNames[I]));
3889 }
3890
Douglas Gregoreee242f2011-10-27 09:33:13 +00003891 // If there are any out-of-date identifiers, bring them up to date.
3892 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003893 // Find out-of-date identifiers.
3894 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003895 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3896 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003897 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003898 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003899 OutOfDate.push_back(ID->second);
3900 }
3901
3902 // Update the out-of-date identifiers.
3903 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3904 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3905 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003906 }
3907
Chris Lattner63d65f82009-09-08 18:19:27 +00003908 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003909 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003910 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003911 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003912 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003913
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003914 // Build a record containing all of the file scoped decls in this file.
3915 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003916 if (!isModule)
3917 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3918 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003919
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003920 // Build a record containing all of the delegating constructors we still need
3921 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003922 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003923 if (!isModule)
3924 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003925
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003926 // Write the set of weak, undeclared identifiers. We always write the
3927 // entire table, since later PCH files in a PCH chain are only interested in
3928 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003929 RecordData WeakUndeclaredIdentifiers;
3930 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003931 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003932 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3933 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3934 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3935 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3936 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3937 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3938 }
3939 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003940
Richard Smith5ea6ef42013-01-10 23:43:47 +00003941 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003942 // declarations in this header file. Generally, this record will be
3943 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003944 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003945 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003946 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003947 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003948 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3949 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003950 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003951 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003952 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003953 }
3954
Douglas Gregorb81c1702009-04-27 20:06:05 +00003955 // Build a record containing all of the ext_vector declarations.
3956 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003957 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003958
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003959 // Build a record containing all of the VTable uses information.
3960 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003961 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003962 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3963 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3964 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3965 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3966 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003967 }
3968
3969 // Build a record containing all of dynamic classes declarations.
3970 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003971 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003972
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003973 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003974 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003975 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003976 I = SemaRef.PendingInstantiations.begin(),
3977 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3978 AddDeclRef(I->first, PendingInstantiations);
3979 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003980 }
3981 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3982 "There are local ones at end of translation unit!");
3983
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003984 // Build a record containing some declaration references.
3985 RecordData SemaDeclRefs;
3986 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3987 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3988 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3989 }
3990
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003991 RecordData CUDASpecialDeclRefs;
3992 if (Context.getcudaConfigureCallDecl()) {
3993 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3994 }
3995
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003996 // Build a record containing all of the known namespaces.
3997 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003998 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003999 I = SemaRef.KnownNamespaces.begin(),
4000 IEnd = SemaRef.KnownNamespaces.end();
4001 I != IEnd; ++I) {
4002 if (!I->second)
4003 AddDeclRef(I->first, KnownNamespaces);
4004 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004005
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004006 // Build a record of all used, undefined objects that require definitions.
4007 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00004008
4009 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004010 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00004011 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4012 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004013 AddDeclRef(I->first, UndefinedButUsed);
4014 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00004015 }
4016
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004017 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00004018 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004019
Sebastian Redl3397c552010-08-18 23:56:27 +00004020 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00004021 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004022 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004023
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00004024 // This is so that older clang versions, before the introduction
4025 // of the control block, can read and reject the newer PCH format.
4026 Record.clear();
4027 Record.push_back(VERSION_MAJOR);
4028 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4029
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004030 // Create a lexical update block containing all of the declarations in the
4031 // translation unit that do not come from other AST files.
4032 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4033 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4034 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
4035 E = TU->noload_decls_end();
4036 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00004037 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004038 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004039 }
4040
4041 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4042 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4043 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4044 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4045 Record.clear();
4046 Record.push_back(TU_UPDATE_LEXICAL);
4047 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4048 data(NewGlobalDecls));
4049
4050 // And a visible updates block for the translation unit.
4051 Abv = new llvm::BitCodeAbbrev();
4052 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4053 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4054 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4055 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4056 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4057 WriteDeclContextVisibleUpdate(TU);
4058
4059 // If the translation unit has an anonymous namespace, and we don't already
4060 // have an update block for it, write it as an update block.
4061 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4062 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4063 if (Record.empty()) {
4064 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004065 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004066 }
4067 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004068
4069 // Make sure visible decls, added to DeclContexts previously loaded from
4070 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004071 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004072 I = UpdatingVisibleDecls.begin(),
4073 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4074 GetDeclRef(*I);
4075 }
4076
Argyrios Kyrtzidis51e75ae2013-08-07 21:17:33 +00004077 // Make sure all decls associated with an identifier are registered for
4078 // serialization.
4079 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4080 IDEnd = PP.getIdentifierTable().end();
4081 ID != IDEnd; ++ID) {
4082 const IdentifierInfo *II = ID->second;
4083 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4084 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4085 DEnd = SemaRef.IdResolver.end();
4086 D != DEnd; ++D) {
4087 GetDeclRef(*D);
4088 }
4089 }
4090 }
4091
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004092 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004093 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004094
Douglas Gregora119da02011-08-02 16:26:37 +00004095 // Form the record of special types.
4096 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004097 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004098 AddTypeRef(Context.getFILEType(), SpecialTypes);
4099 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4100 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4101 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4102 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004103 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004104 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004105
Douglas Gregor366809a2009-04-26 03:49:13 +00004106 // Keep writing types and declarations until all types and
4107 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004108 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004109 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004110 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4111 E = DeclsToRewrite.end();
4112 I != E; ++I)
4113 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004114 while (!DeclTypesToEmit.empty()) {
4115 DeclOrType DOT = DeclTypesToEmit.front();
4116 DeclTypesToEmit.pop();
4117 if (DOT.isType())
4118 WriteType(DOT.getType());
4119 else
4120 WriteDecl(Context, DOT.getDecl());
4121 }
4122 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004123
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004124 DoneWritingDeclsAndTypes = true;
4125
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004126 WriteFileDeclIDsMap();
4127 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004128 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004129
4130 if (Chain) {
4131 // Write the mapping information describing our module dependencies and how
4132 // each of those modules were mapped into our own offset/ID space, so that
4133 // the reader can build the appropriate mapping to its own offset/ID space.
4134 // The map consists solely of a blob with the following format:
4135 // *(module-name-len:i16 module-name:len*i8
4136 // source-location-offset:i32
4137 // identifier-id:i32
4138 // preprocessed-entity-id:i32
4139 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004140 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004141 // selector-id:i32
4142 // declaration-id:i32
4143 // c++-base-specifiers-id:i32
4144 // type-id:i32)
4145 //
4146 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4147 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4148 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4149 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004150 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004151 {
4152 llvm::raw_svector_ostream Out(Buffer);
4153 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004154 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004155 M != MEnd; ++M) {
4156 StringRef FileName = (*M)->FileName;
4157 io::Emit16(Out, FileName.size());
4158 Out.write(FileName.data(), FileName.size());
4159 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4160 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004161 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004162 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004163 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004164 io::Emit32(Out, (*M)->BaseSelectorID);
4165 io::Emit32(Out, (*M)->BaseDeclID);
4166 io::Emit32(Out, (*M)->BaseTypeIndex);
4167 }
4168 }
4169 Record.clear();
4170 Record.push_back(MODULE_OFFSET_MAP);
4171 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4172 Buffer.data(), Buffer.size());
4173 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004174 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004175 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004176 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004177 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004178 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004179 WriteFPPragmaOptions(SemaRef.getFPOptions());
4180 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004181
Sebastian Redl1476ed42010-07-16 16:36:56 +00004182 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004183 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004184
Anders Carlssonc8505782011-03-06 18:41:18 +00004185 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004186
Douglas Gregore209e502011-12-06 01:10:29 +00004187 // If we're emitting a module, write out the submodule information.
4188 if (WritingModule)
4189 WriteSubmodules(WritingModule);
4190
Douglas Gregora119da02011-08-02 16:26:37 +00004191 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4192
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004193 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004194 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004195 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004196
4197 // Write the record containing tentative definitions.
4198 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004199 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004200
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004201 // Write the record containing unused file scoped decls.
4202 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004203 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004204
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004205 // Write the record containing weak undeclared identifiers.
4206 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004207 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004208 WeakUndeclaredIdentifiers);
4209
Richard Smith5ea6ef42013-01-10 23:43:47 +00004210 // Write the record containing locally-scoped extern "C" definitions.
4211 if (!LocallyScopedExternCDecls.empty())
4212 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4213 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004214
4215 // Write the record containing ext_vector type names.
4216 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004217 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004218
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004219 // Write the record containing VTable uses information.
4220 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004221 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004222
4223 // Write the record containing dynamic classes declarations.
4224 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004225 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004226
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004227 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004228 if (!PendingInstantiations.empty())
4229 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004230
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004231 // Write the record containing declaration references of Sema.
4232 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004233 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004234
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004235 // Write the record containing CUDA-specific declaration references.
4236 if (!CUDASpecialDeclRefs.empty())
4237 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004238
4239 // Write the delegating constructors.
4240 if (!DelegatingCtorDecls.empty())
4241 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004242
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004243 // Write the known namespaces.
4244 if (!KnownNamespaces.empty())
4245 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004246
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004247 // Write the undefined internal functions and variables, and inline functions.
4248 if (!UndefinedButUsed.empty())
4249 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004250
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004251 // Write the visible updates to DeclContexts.
4252 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4253 I = UpdatedDeclContexts.begin(),
4254 E = UpdatedDeclContexts.end();
4255 I != E; ++I)
4256 WriteDeclContextVisibleUpdate(*I);
4257
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004258 if (!WritingModule) {
4259 // Write the submodules that were imported, if any.
4260 RecordData ImportedModules;
4261 for (ASTContext::import_iterator I = Context.local_import_begin(),
4262 IEnd = Context.local_import_end();
4263 I != IEnd; ++I) {
4264 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4265 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4266 }
4267 if (!ImportedModules.empty()) {
4268 // Sort module IDs.
4269 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4270
4271 // Unique module IDs.
4272 ImportedModules.erase(std::unique(ImportedModules.begin(),
4273 ImportedModules.end()),
4274 ImportedModules.end());
4275
4276 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4277 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004278 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004279
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004280 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004281 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004282 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004283 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004284 WriteObjCCategories();
Richard Smithac32d902013-08-07 21:41:30 +00004285 WriteLateParsedTemplates(SemaRef);
4286
Douglas Gregor3e1af842009-04-17 22:13:46 +00004287 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004288 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004289 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004290 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004291 Record.push_back(NumLexicalDeclContexts);
4292 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004293 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004294 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004295}
4296
Douglas Gregor61c5e342011-09-17 00:05:03 +00004297/// \brief Go through the declaration update blocks and resolve declaration
4298/// pointers into declaration IDs.
4299void ASTWriter::ResolveDeclUpdatesBlocks() {
4300 for (DeclUpdateMap::iterator
4301 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4302 const Decl *D = I->first;
4303 UpdateRecord &URec = I->second;
4304
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004305 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004306 continue; // The decl will be written completely
4307
4308 unsigned Idx = 0, N = URec.size();
4309 while (Idx < N) {
4310 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004311 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4312 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4313 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4314 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4315 ++Idx;
4316 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004317
Douglas Gregor61c5e342011-09-17 00:05:03 +00004318 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman86164e82013-09-05 00:02:25 +00004319 case UPD_DECL_MARKED_USED:
Douglas Gregor61c5e342011-09-17 00:05:03 +00004320 ++Idx;
4321 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004322
4323 case UPD_CXX_DEDUCED_RETURN_TYPE:
4324 URec[Idx] = GetOrCreateTypeID(
4325 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4326 ++Idx;
4327 break;
Douglas Gregor61c5e342011-09-17 00:05:03 +00004328 }
4329 }
4330 }
4331}
4332
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004333void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004334 if (DeclUpdates.empty())
4335 return;
4336
4337 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004338 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004339 for (DeclUpdateMap::iterator
4340 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4341 const Decl *D = I->first;
4342 UpdateRecord &URec = I->second;
4343
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004344 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004345 continue; // The decl will be written completely,no need to store updates.
4346
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004347 uint64_t Offset = Stream.GetCurrentBitNo();
4348 Stream.EmitRecord(DECL_UPDATES, URec);
4349
4350 OffsetsRecord.push_back(GetDeclRef(D));
4351 OffsetsRecord.push_back(Offset);
4352 }
4353 Stream.ExitBlock();
4354 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4355}
4356
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004357void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004358 if (ReplacedDecls.empty())
4359 return;
4360
4361 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004362 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4363 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004364 Record.push_back(I->ID);
4365 Record.push_back(I->Offset);
4366 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004367 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004368 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004369}
4370
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004371void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004372 Record.push_back(Loc.getRawEncoding());
4373}
4374
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004375void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004376 AddSourceLocation(Range.getBegin(), Record);
4377 AddSourceLocation(Range.getEnd(), Record);
4378}
4379
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004380void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004381 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004382 const uint64_t *Words = Value.getRawData();
4383 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004384}
4385
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004386void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004387 Record.push_back(Value.isUnsigned());
4388 AddAPInt(Value, Record);
4389}
4390
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004391void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004392 AddAPInt(Value.bitcastToAPInt(), Record);
4393}
4394
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004395void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004396 Record.push_back(getIdentifierRef(II));
4397}
4398
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004399IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004400 if (II == 0)
4401 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004402
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004403 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004404 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004405 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004406 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004407}
4408
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004409MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004410 // Don't emit builtin macros like __LINE__ to the AST file unless they
4411 // have been redefined by the header (in which case they are not
4412 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004413 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004414 return 0;
4415
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004416 MacroID &ID = MacroIDs[MI];
4417 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004418 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004419 MacroInfoToEmitData Info = { Name, MI, ID };
4420 MacroInfosToEmit.push_back(Info);
4421 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004422 return ID;
4423}
4424
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004425MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4426 if (MI == 0 || MI->isBuiltinMacro())
4427 return 0;
4428
4429 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4430 return MacroIDs[MI];
4431}
4432
4433uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4434 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4435 return IdentMacroDirectivesOffsetMap[Name];
4436}
4437
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004438void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004439 Record.push_back(getSelectorRef(SelRef));
4440}
4441
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004442SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004443 if (Sel.getAsOpaquePtr() == 0) {
4444 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004445 }
4446
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004447 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004448 if (SID == 0 && Chain) {
4449 // This might trigger a ReadSelector callback, which will set the ID for
4450 // this selector.
4451 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004452 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004453 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004454 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004455 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004456 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004457 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004458 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004459}
4460
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004461void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004462 AddDeclRef(Temp->getDestructor(), Record);
4463}
4464
Douglas Gregor7c789c12010-10-29 22:39:52 +00004465void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4466 CXXBaseSpecifier const *BasesEnd,
4467 RecordDataImpl &Record) {
4468 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4469 CXXBaseSpecifiersToWrite.push_back(
4470 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4471 Bases, BasesEnd));
4472 Record.push_back(NextCXXBaseSpecifiersID++);
4473}
4474
Sebastian Redla4232eb2010-08-18 23:56:21 +00004475void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004476 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004477 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004478 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004479 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004480 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004481 break;
4482 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004483 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004484 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004485 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004486 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004487 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004488 break;
4489 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004490 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004491 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004492 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004493 break;
John McCall833ca992009-10-29 08:12:44 +00004494 case TemplateArgument::Null:
4495 case TemplateArgument::Integral:
4496 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004497 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004498 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004499 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004500 break;
4501 }
4502}
4503
Sebastian Redla4232eb2010-08-18 23:56:21 +00004504void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004505 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004506 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004507
4508 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4509 bool InfoHasSameExpr
4510 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4511 Record.push_back(InfoHasSameExpr);
4512 if (InfoHasSameExpr)
4513 return; // Avoid storing the same expr twice.
4514 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004515 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4516 Record);
4517}
4518
Douglas Gregordc355712011-02-25 00:36:19 +00004519void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4520 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004521 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004522 AddTypeRef(QualType(), Record);
4523 return;
4524 }
4525
Douglas Gregordc355712011-02-25 00:36:19 +00004526 AddTypeLoc(TInfo->getTypeLoc(), Record);
4527}
4528
4529void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4530 AddTypeRef(TL.getType(), Record);
4531
John McCalla1ee0c52009-10-16 21:56:05 +00004532 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004533 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004534 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004535}
4536
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004537void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004538 Record.push_back(GetOrCreateTypeID(T));
4539}
4540
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004541TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004542 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004543 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004544 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4545}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004546
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004547TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004548 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004549 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004550 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004551}
4552
4553TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4554 if (T.isNull())
4555 return TypeIdx();
4556 assert(!T.getLocalFastQualifiers());
4557
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004558 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004559 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004560 if (DoneWritingDeclsAndTypes) {
4561 assert(0 && "New type seen after serializing all the types to emit!");
4562 return TypeIdx();
4563 }
4564
Douglas Gregor366809a2009-04-26 03:49:13 +00004565 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004566 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004567 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004568 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004569 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004570 return Idx;
4571}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004572
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004573TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004574 if (T.isNull())
4575 return TypeIdx();
4576 assert(!T.getLocalFastQualifiers());
4577
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004578 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4579 assert(I != TypeIdxs.end() && "Type not emitted!");
4580 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004581}
4582
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004583void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004584 Record.push_back(GetDeclRef(D));
4585}
4586
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004587DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004588 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4589
Douglas Gregor2cf26342009-04-09 22:27:44 +00004590 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004591 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004592 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004593
4594 // If D comes from an AST file, its declaration ID is already known and
4595 // fixed.
4596 if (D->isFromASTFile())
4597 return D->getGlobalID();
4598
Douglas Gregor97475832010-10-05 18:37:06 +00004599 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004600 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004601 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004602 if (DoneWritingDeclsAndTypes) {
4603 assert(0 && "New decl seen after serializing all the decls to emit!");
4604 return 0;
4605 }
4606
Douglas Gregor2cf26342009-04-09 22:27:44 +00004607 // We haven't seen this declaration before. Give it a new ID and
4608 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004609 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004610 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004611 }
4612
Sebastian Redl681d7232010-07-27 00:17:23 +00004613 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004614}
4615
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004616DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004617 if (D == 0)
4618 return 0;
4619
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004620 // If D comes from an AST file, its declaration ID is already known and
4621 // fixed.
4622 if (D->isFromASTFile())
4623 return D->getGlobalID();
4624
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004625 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4626 return DeclIDs[D];
4627}
4628
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004629void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004630 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004631 assert(D);
4632
4633 SourceLocation Loc = D->getLocation();
4634 if (Loc.isInvalid())
4635 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004636
4637 // We only keep track of the file-level declarations of each file.
4638 if (!D->getLexicalDeclContext()->isFileContext())
4639 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004640 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4641 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004642 if (isa<ParmVarDecl>(D))
4643 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004644
4645 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004646 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004647 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004648 FileID FID;
4649 unsigned Offset;
4650 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004651 if (FID.isInvalid())
4652 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004653 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004654
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004655 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004656 if (!Info)
4657 Info = new DeclIDInFileInfo();
4658
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004659 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004660 LocDeclIDsTy &Decls = Info->DeclIDs;
4661
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004662 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004663 Decls.push_back(LocDecl);
4664 return;
4665 }
4666
Benjamin Kramer809d2542013-08-24 13:22:59 +00004667 LocDeclIDsTy::iterator I =
4668 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004669
4670 Decls.insert(I, LocDecl);
4671}
4672
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004673void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004674 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004675 Record.push_back(Name.getNameKind());
4676 switch (Name.getNameKind()) {
4677 case DeclarationName::Identifier:
4678 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4679 break;
4680
4681 case DeclarationName::ObjCZeroArgSelector:
4682 case DeclarationName::ObjCOneArgSelector:
4683 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004684 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004685 break;
4686
4687 case DeclarationName::CXXConstructorName:
4688 case DeclarationName::CXXDestructorName:
4689 case DeclarationName::CXXConversionFunctionName:
4690 AddTypeRef(Name.getCXXNameType(), Record);
4691 break;
4692
4693 case DeclarationName::CXXOperatorName:
4694 Record.push_back(Name.getCXXOverloadedOperator());
4695 break;
4696
Sean Hunt3e518bd2009-11-29 07:34:05 +00004697 case DeclarationName::CXXLiteralOperatorName:
4698 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4699 break;
4700
Douglas Gregor2cf26342009-04-09 22:27:44 +00004701 case DeclarationName::CXXUsingDirective:
4702 // No extra data to emit
4703 break;
4704 }
4705}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004706
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004707void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004708 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004709 switch (Name.getNameKind()) {
4710 case DeclarationName::CXXConstructorName:
4711 case DeclarationName::CXXDestructorName:
4712 case DeclarationName::CXXConversionFunctionName:
4713 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4714 break;
4715
4716 case DeclarationName::CXXOperatorName:
4717 AddSourceLocation(
4718 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4719 Record);
4720 AddSourceLocation(
4721 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4722 Record);
4723 break;
4724
4725 case DeclarationName::CXXLiteralOperatorName:
4726 AddSourceLocation(
4727 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4728 Record);
4729 break;
4730
4731 case DeclarationName::Identifier:
4732 case DeclarationName::ObjCZeroArgSelector:
4733 case DeclarationName::ObjCOneArgSelector:
4734 case DeclarationName::ObjCMultiArgSelector:
4735 case DeclarationName::CXXUsingDirective:
4736 break;
4737 }
4738}
4739
4740void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004741 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004742 AddDeclarationName(NameInfo.getName(), Record);
4743 AddSourceLocation(NameInfo.getLoc(), Record);
4744 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4745}
4746
4747void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004748 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004749 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004750 Record.push_back(Info.NumTemplParamLists);
4751 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4752 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4753}
4754
Sebastian Redla4232eb2010-08-18 23:56:21 +00004755void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004756 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004757 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004758 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004759 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004760
4761 // Push each of the NNS's onto a stack for serialization in reverse order.
4762 while (NNS) {
4763 NestedNames.push_back(NNS);
4764 NNS = NNS->getPrefix();
4765 }
4766
4767 Record.push_back(NestedNames.size());
4768 while(!NestedNames.empty()) {
4769 NNS = NestedNames.pop_back_val();
4770 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4771 Record.push_back(Kind);
4772 switch (Kind) {
4773 case NestedNameSpecifier::Identifier:
4774 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4775 break;
4776
4777 case NestedNameSpecifier::Namespace:
4778 AddDeclRef(NNS->getAsNamespace(), Record);
4779 break;
4780
Douglas Gregor14aba762011-02-24 02:36:08 +00004781 case NestedNameSpecifier::NamespaceAlias:
4782 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4783 break;
4784
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004785 case NestedNameSpecifier::TypeSpec:
4786 case NestedNameSpecifier::TypeSpecWithTemplate:
4787 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4788 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4789 break;
4790
4791 case NestedNameSpecifier::Global:
4792 // Don't need to write an associated value.
4793 break;
4794 }
4795 }
4796}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004797
Douglas Gregordc355712011-02-25 00:36:19 +00004798void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4799 RecordDataImpl &Record) {
4800 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004801 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004802 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004803
4804 // Push each of the nested-name-specifiers's onto a stack for
4805 // serialization in reverse order.
4806 while (NNS) {
4807 NestedNames.push_back(NNS);
4808 NNS = NNS.getPrefix();
4809 }
4810
4811 Record.push_back(NestedNames.size());
4812 while(!NestedNames.empty()) {
4813 NNS = NestedNames.pop_back_val();
4814 NestedNameSpecifier::SpecifierKind Kind
4815 = NNS.getNestedNameSpecifier()->getKind();
4816 Record.push_back(Kind);
4817 switch (Kind) {
4818 case NestedNameSpecifier::Identifier:
4819 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4820 AddSourceRange(NNS.getLocalSourceRange(), Record);
4821 break;
4822
4823 case NestedNameSpecifier::Namespace:
4824 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4825 AddSourceRange(NNS.getLocalSourceRange(), Record);
4826 break;
4827
4828 case NestedNameSpecifier::NamespaceAlias:
4829 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4830 AddSourceRange(NNS.getLocalSourceRange(), Record);
4831 break;
4832
4833 case NestedNameSpecifier::TypeSpec:
4834 case NestedNameSpecifier::TypeSpecWithTemplate:
4835 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4836 AddTypeLoc(NNS.getTypeLoc(), Record);
4837 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4838 break;
4839
4840 case NestedNameSpecifier::Global:
4841 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4842 break;
4843 }
4844 }
4845}
4846
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004847void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004848 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004849 Record.push_back(Kind);
4850 switch (Kind) {
4851 case TemplateName::Template:
4852 AddDeclRef(Name.getAsTemplateDecl(), Record);
4853 break;
4854
4855 case TemplateName::OverloadedTemplate: {
4856 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4857 Record.push_back(OvT->size());
4858 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4859 I != E; ++I)
4860 AddDeclRef(*I, Record);
4861 break;
4862 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004863
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004864 case TemplateName::QualifiedTemplate: {
4865 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4866 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4867 Record.push_back(QualT->hasTemplateKeyword());
4868 AddDeclRef(QualT->getTemplateDecl(), Record);
4869 break;
4870 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004871
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004872 case TemplateName::DependentTemplate: {
4873 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4874 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4875 Record.push_back(DepT->isIdentifier());
4876 if (DepT->isIdentifier())
4877 AddIdentifierRef(DepT->getIdentifier(), Record);
4878 else
4879 Record.push_back(DepT->getOperator());
4880 break;
4881 }
John McCall14606042011-06-30 08:33:18 +00004882
4883 case TemplateName::SubstTemplateTemplateParm: {
4884 SubstTemplateTemplateParmStorage *subst
4885 = Name.getAsSubstTemplateTemplateParm();
4886 AddDeclRef(subst->getParameter(), Record);
4887 AddTemplateName(subst->getReplacement(), Record);
4888 break;
4889 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004890
4891 case TemplateName::SubstTemplateTemplateParmPack: {
4892 SubstTemplateTemplateParmPackStorage *SubstPack
4893 = Name.getAsSubstTemplateTemplateParmPack();
4894 AddDeclRef(SubstPack->getParameterPack(), Record);
4895 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4896 break;
4897 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004898 }
4899}
4900
Michael J. Spencer20249a12010-10-21 03:16:25 +00004901void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004902 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004903 Record.push_back(Arg.getKind());
4904 switch (Arg.getKind()) {
4905 case TemplateArgument::Null:
4906 break;
4907 case TemplateArgument::Type:
4908 AddTypeRef(Arg.getAsType(), Record);
4909 break;
4910 case TemplateArgument::Declaration:
4911 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004912 Record.push_back(Arg.isDeclForReferenceParam());
4913 break;
4914 case TemplateArgument::NullPtr:
4915 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004916 break;
4917 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004918 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004919 AddTypeRef(Arg.getIntegralType(), Record);
4920 break;
4921 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004922 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4923 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004924 case TemplateArgument::TemplateExpansion:
4925 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004926 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004927 Record.push_back(*NumExpansions + 1);
4928 else
4929 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004930 break;
4931 case TemplateArgument::Expression:
4932 AddStmt(Arg.getAsExpr());
4933 break;
4934 case TemplateArgument::Pack:
4935 Record.push_back(Arg.pack_size());
4936 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4937 I != E; ++I)
4938 AddTemplateArgument(*I, Record);
4939 break;
4940 }
4941}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004942
4943void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004944ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004945 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004946 assert(TemplateParams && "No TemplateParams!");
4947 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4948 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4949 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4950 Record.push_back(TemplateParams->size());
4951 for (TemplateParameterList::const_iterator
4952 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4953 P != PEnd; ++P)
4954 AddDeclRef(*P, Record);
4955}
4956
4957/// \brief Emit a template argument list.
4958void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004959ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004960 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004961 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004962 Record.push_back(TemplateArgs->size());
4963 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004964 AddTemplateArgument(TemplateArgs->get(i), Record);
4965}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004966
Enea Zaffanellac1cef082013-08-10 07:24:53 +00004967void
4968ASTWriter::AddASTTemplateArgumentListInfo
4969(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4970 assert(ASTTemplArgList && "No ASTTemplArgList!");
4971 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4972 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4973 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4974 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4975 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4976 AddTemplateArgumentLoc(TemplArgs[i], Record);
4977}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004978
4979void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004980ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004981 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004982 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004983 I = Set.begin(), E = Set.end(); I != E; ++I) {
4984 AddDeclRef(I.getDecl(), Record);
4985 Record.push_back(I.getAccess());
4986 }
4987}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004988
Sebastian Redla4232eb2010-08-18 23:56:21 +00004989void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004990 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004991 Record.push_back(Base.isVirtual());
4992 Record.push_back(Base.isBaseOfClass());
4993 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004994 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004995 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004996 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004997 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4998 : SourceLocation(),
4999 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005000}
Sebastian Redl30c514c2010-07-14 23:45:08 +00005001
Douglas Gregor7c789c12010-10-29 22:39:52 +00005002void ASTWriter::FlushCXXBaseSpecifiers() {
5003 RecordData Record;
5004 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5005 Record.clear();
5006
5007 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00005008 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005009 if (Index == CXXBaseSpecifiersOffsets.size())
5010 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5011 else {
5012 if (Index > CXXBaseSpecifiersOffsets.size())
5013 CXXBaseSpecifiersOffsets.resize(Index + 1);
5014 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5015 }
5016
5017 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5018 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5019 Record.push_back(BEnd - B);
5020 for (; B != BEnd; ++B)
5021 AddCXXBaseSpecifier(*B, Record);
5022 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00005023
5024 // Flush any expressions that were written as part of the base specifiers.
5025 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00005026 }
5027
5028 CXXBaseSpecifiersToWrite.clear();
5029}
5030
Sean Huntcbb67482011-01-08 20:30:50 +00005031void ASTWriter::AddCXXCtorInitializers(
5032 const CXXCtorInitializer * const *CtorInitializers,
5033 unsigned NumCtorInitializers,
5034 RecordDataImpl &Record) {
5035 Record.push_back(NumCtorInitializers);
5036 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5037 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005038
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005039 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00005040 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00005041 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005042 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00005043 } else if (Init->isDelegatingInitializer()) {
5044 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00005045 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00005046 } else if (Init->isMemberInitializer()){
5047 Record.push_back(CTOR_INITIALIZER_MEMBER);
5048 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005049 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00005050 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5051 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005052 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00005053
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005054 AddSourceLocation(Init->getMemberLocation(), Record);
5055 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005056 AddSourceLocation(Init->getLParenLoc(), Record);
5057 AddSourceLocation(Init->getRParenLoc(), Record);
5058 Record.push_back(Init->isWritten());
5059 if (Init->isWritten()) {
5060 Record.push_back(Init->getSourceOrder());
5061 } else {
5062 Record.push_back(Init->getNumArrayIndices());
5063 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5064 AddDeclRef(Init->getArrayIndex(i), Record);
5065 }
5066 }
5067}
5068
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005069void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5070 assert(D->DefinitionData);
5071 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005072 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005073 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005074 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005075 Record.push_back(Data.Aggregate);
5076 Record.push_back(Data.PlainOldData);
5077 Record.push_back(Data.Empty);
5078 Record.push_back(Data.Polymorphic);
5079 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005080 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005081 Record.push_back(Data.HasNoNonEmptyBases);
5082 Record.push_back(Data.HasPrivateFields);
5083 Record.push_back(Data.HasProtectedFields);
5084 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005085 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00005086 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005087 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005088 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005089 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5090 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5091 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5092 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5093 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5094 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005095 Record.push_back(Data.HasTrivialSpecialMembers);
5096 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005097 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005098 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005099 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005100 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005101 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005102 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005103 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005104 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5105 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5106 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5107 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005108 Record.push_back(Data.FailedImplicitMoveConstructor);
5109 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005110 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005111
5112 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005113 if (Data.NumBases > 0)
5114 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5115 Record);
5116
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005117 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5118 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005119 if (Data.NumVBases > 0)
5120 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5121 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005122
Richard Smithc2d77572013-08-30 04:46:40 +00005123 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5124 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005125 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005126 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005127
5128 // Add lambda-specific data.
5129 if (Data.IsLambda) {
5130 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005131 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005132 Record.push_back(Lambda.NumCaptures);
5133 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005134 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005135 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005136 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Faisal Valifad9e132013-09-26 19:54:12 +00005137 AddStmt(Lambda.TheLambdaExpr);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005138 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5139 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5140 AddSourceLocation(Capture.getLocation(), Record);
5141 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005142 Record.push_back(Capture.getCaptureKind());
5143 switch (Capture.getCaptureKind()) {
5144 case LCK_This:
5145 break;
5146 case LCK_ByCopy:
Richard Smith04fa7a32013-09-28 04:02:39 +00005147 case LCK_ByRef:
Richard Smith0d8e9642013-05-16 06:20:58 +00005148 VarDecl *Var =
5149 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5150 AddDeclRef(Var, Record);
5151 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5152 : SourceLocation(),
5153 Record);
5154 break;
5155 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005156 }
5157 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005158}
5159
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005160void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005161 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005162 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005163 assert(FirstDeclID == NextDeclID &&
5164 FirstTypeID == NextTypeID &&
5165 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005166 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005167 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005168 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005169 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005170
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005171 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005172
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005173 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5174 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5175 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005176 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005177 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005178 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005179 NextDeclID = FirstDeclID;
5180 NextTypeID = FirstTypeID;
5181 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005182 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005183 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005184 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005185}
5186
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005187void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005188 // Always keep the highest ID. See \p TypeRead() for more information.
5189 IdentID &StoredID = IdentifierIDs[II];
5190 if (ID > StoredID)
5191 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005192}
5193
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005194void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005195 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005196 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005197 if (ID > StoredID)
5198 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005199}
5200
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005201void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005202 // Always take the highest-numbered type index. This copes with an interesting
5203 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005204 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005205 // keep the higher-numbered entry so that we can properly write it out to
5206 // the AST file.
5207 TypeIdx &StoredIdx = TypeIdxs[T];
5208 if (Idx.getIndex() >= StoredIdx.getIndex())
5209 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005210}
5211
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005212void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005213 // Always keep the highest ID. See \p TypeRead() for more information.
5214 SelectorID &StoredID = SelectorIDs[S];
5215 if (ID > StoredID)
5216 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005217}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005218
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005219void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005220 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005221 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005222 MacroDefinitions[MD] = ID;
5223}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005224
Douglas Gregora015cab2011-12-02 17:30:13 +00005225void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5226 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5227 SubmoduleIDs[Mod] = ID;
5228}
5229
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005230void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005231 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005232 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005233 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5234 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005235 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005236 // A forward reference was mutated into a definition. Rewrite it.
5237 // FIXME: This happens during template instantiation, should we
5238 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005239 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005240 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005241 }
5242}
Douglas Gregora8235d62012-10-09 23:05:51 +00005243
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005244void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005245 assert(!WritingAST && "Already writing the AST!");
5246
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005247 // TU and namespaces are handled elsewhere.
5248 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5249 return;
5250
Douglas Gregor919814d2011-09-09 23:01:35 +00005251 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005252 return; // Not a source decl added to a DeclContext from PCH.
5253
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005254 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005255 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005256 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005257}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005258
5259void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005260 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005261 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005262 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005263 return; // Not a source member added to a class from PCH.
5264 if (!isa<CXXMethodDecl>(D))
5265 return; // We are interested in lazily declared implicit methods.
5266
5267 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005268 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005269 UpdateRecord &Record = DeclUpdates[RD];
5270 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005271 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005272}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005273
5274void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5275 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005276 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005277 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005278 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005279 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005280 return; // Not a source specialization added to a template from PCH.
5281
5282 UpdateRecord &Record = DeclUpdates[TD];
5283 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005284 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005285}
Douglas Gregor89d99802010-11-30 06:16:57 +00005286
Larisse Voufoef4579c2013-08-06 01:03:05 +00005287void ASTWriter::AddedCXXTemplateSpecialization(
5288 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5289 // The specializations set is kept in the canonical template.
5290 assert(!WritingAST && "Already writing the AST!");
5291 TD = TD->getCanonicalDecl();
5292 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5293 return; // Not a source specialization added to a template from PCH.
5294
5295 UpdateRecord &Record = DeclUpdates[TD];
5296 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5297 Record.push_back(reinterpret_cast<uint64_t>(D));
5298}
5299
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005300void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5301 const FunctionDecl *D) {
5302 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005303 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005304 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005305 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005306 return; // Not a source specialization added to a template from PCH.
5307
5308 UpdateRecord &Record = DeclUpdates[TD];
5309 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005310 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005311}
5312
Richard Smith9dadfab2013-05-11 05:45:24 +00005313void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5314 assert(!WritingAST && "Already writing the AST!");
5315 FD = FD->getCanonicalDecl();
5316 if (!FD->isFromASTFile())
5317 return; // Not a function declared in PCH and defined outside.
5318
5319 UpdateRecord &Record = DeclUpdates[FD];
5320 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5321 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5322}
5323
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005324void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005325 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005326 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005327 return; // Declaration not imported from PCH.
5328
5329 // Implicit decl from a PCH was defined.
5330 // FIXME: Should implicit definition be a separate FunctionDecl?
5331 RewriteDecl(D);
5332}
5333
Sebastian Redlf79a7192011-04-29 08:19:30 +00005334void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005335 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005336 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005337 return;
5338
5339 // Since the actual instantiation is delayed, this really means that we need
5340 // to update the instantiation location.
5341 UpdateRecord &Record = DeclUpdates[D];
5342 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5343 AddSourceLocation(
5344 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5345}
5346
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005347void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5348 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005349 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005350 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005351 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005352
5353 assert(IFD->getDefinition() && "Category on a class without a definition?");
5354 ObjCClassesWithCategories.insert(
5355 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005356}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005357
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005358
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005359void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5360 const ObjCPropertyDecl *OrigProp,
5361 const ObjCCategoryDecl *ClassExt) {
5362 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5363 if (!D)
5364 return;
5365
5366 assert(!WritingAST && "Already writing the AST!");
5367 if (!D->isFromASTFile())
5368 return; // Declaration not imported from PCH.
5369
5370 RewriteDecl(D);
5371}
Eli Friedman86164e82013-09-05 00:02:25 +00005372
5373void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5374 assert(!WritingAST && "Already writing the AST!");
5375 if (!D->isFromASTFile())
5376 return;
5377
5378 UpdateRecord &Record = DeclUpdates[D];
5379 Record.push_back(UPD_DECL_MARKED_USED);
5380}