blob: 35da82c6e4c2575d5d8b7bf49e22efb517d6139b [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"
Stephen Hines651f13c2014-04-23 16:59:28 -070020#include "clang/AST/DeclLookups.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070026#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000028#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070049#include "llvm/Support/EndianStream.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000050#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000051#include "llvm/Support/MemoryBuffer.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000053#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000055#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000056#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000057#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000058using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000059using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000060
Sebastian Redlade50002010-07-30 17:03:48 +000061template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const std::vector<T, Allocator> &v) {
63 if (v.empty()) return StringRef();
64 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000067
68template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000069static StringRef data(const SmallVectorImpl<T> &v) {
70 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000071 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000072}
73
Douglas Gregor2cf26342009-04-09 22:27:44 +000074//===----------------------------------------------------------------------===//
75// Type serialization
76//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000077
Douglas Gregor2cf26342009-04-09 22:27:44 +000078namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000079 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000080 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 public:
84 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000086
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000087 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000088 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000089
90 void VisitArrayType(const ArrayType *T);
91 void VisitFunctionType(const FunctionType *T);
92 void VisitTagType(const TagType *T);
93
94#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
95#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000096#include "clang/AST/TypeNodes.def"
97 };
98}
99
Sebastian Redl3397c552010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000101 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102}
103
Sebastian Redl3397c552010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000106 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Sebastian Redl3397c552010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000111 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000112}
113
Reid Kleckner12df2462013-06-24 17:51:48 +0000114void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
115 Writer.AddTypeRef(T->getOriginalType(), Record);
116 Code = TYPE_DECAYED;
117}
118
Stephen Hines651f13c2014-04-23 16:59:28 -0700119void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
120 Writer.AddTypeRef(T->getOriginalType(), Record);
121 Writer.AddTypeRef(T->getAdjustedType(), Record);
122 Code = TYPE_ADJUSTED;
123}
124
Sebastian Redl3397c552010-08-18 23:56:27 +0000125void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000126 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000131 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
132 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000133 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000137 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000142 Writer.AddTypeRef(T->getPointeeType(), Record);
143 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000150 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151}
152
Sebastian Redl3397c552010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154 VisitArrayType(T);
155 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000156 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157}
158
Sebastian Redl3397c552010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000161 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162}
163
Sebastian Redl3397c552010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000166 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
167 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000168 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000169 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170}
171
Sebastian Redl3397c552010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173 Writer.AddTypeRef(T->getElementType(), Record);
174 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000175 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000176 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177}
178
Sebastian Redl3397c552010-08-18 23:56:27 +0000179void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000181 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182}
183
Sebastian Redl3397c552010-08-18 23:56:27 +0000184void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700185 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000186 FunctionType::ExtInfo C = T->getExtInfo();
187 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000188 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000189 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000190 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000191 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000192 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193}
194
Sebastian Redl3397c552010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000196 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000197 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198}
199
Stephen Hines651f13c2014-04-23 16:59:28 -0700200static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
201 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl60618fa2011-03-12 11:50:43 +0000202 Record.push_back(T->getExceptionSpecType());
203 if (T->getExceptionSpecType() == EST_Dynamic) {
204 Record.push_back(T->getNumExceptions());
205 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
206 Writer.AddTypeRef(T->getExceptionType(I), Record);
207 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
208 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000209 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
210 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
211 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000212 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
213 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000214 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700215}
216
217void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
218 VisitFunctionType(T);
219 Record.push_back(T->getNumParams());
220 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
221 Writer.AddTypeRef(T->getParamType(I), Record);
222 Record.push_back(T->isVariadic());
223 Record.push_back(T->hasTrailingReturn());
224 Record.push_back(T->getTypeQuals());
225 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
226 addExceptionSpec(Writer, T, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000227 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228}
229
Sebastian Redl3397c552010-08-18 23:56:27 +0000230void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000231 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000232 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000233}
John McCalled976492009-12-04 22:46:56 +0000234
Sebastian Redl3397c552010-08-18 23:56:27 +0000235void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000237 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
238 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000239 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000240}
241
Sebastian Redl3397c552010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000243 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000244 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000248 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000249 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000250}
251
Sebastian Redl3397c552010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000253 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000254 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000256}
257
Sean Huntca63c202011-05-24 22:41:36 +0000258void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
259 Writer.AddTypeRef(T->getBaseType(), Record);
260 Writer.AddTypeRef(T->getUnderlyingType(), Record);
261 Record.push_back(T->getUTTKind());
262 Code = TYPE_UNARY_TRANSFORM;
263}
264
Richard Smith34b41d92011-02-20 03:19:35 +0000265void ASTTypeWriter::VisitAutoType(const AutoType *T) {
266 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smitha2c36462013-04-26 16:15:35 +0000267 Record.push_back(T->isDecltypeAuto());
Richard Smithdc7a4f52013-04-30 13:56:41 +0000268 if (T->getDeducedType().isNull())
269 Record.push_back(T->isDependentType());
Richard Smith34b41d92011-02-20 03:19:35 +0000270 Code = TYPE_AUTO;
271}
272
Sebastian Redl3397c552010-08-18 23:56:27 +0000273void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000274 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000275 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000276 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000277 "Cannot serialize in the middle of a type definition");
278}
279
Sebastian Redl3397c552010-08-18 23:56:27 +0000280void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000281 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000282 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283}
284
Sebastian Redl3397c552010-08-18 23:56:27 +0000285void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000286 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000287 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000288}
289
John McCall9d156a72011-01-06 01:58:22 +0000290void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
291 Writer.AddTypeRef(T->getModifiedType(), Record);
292 Writer.AddTypeRef(T->getEquivalentType(), Record);
293 Record.push_back(T->getAttrKind());
294 Code = TYPE_ATTRIBUTED;
295}
296
Mike Stump1eb44332009-09-09 15:08:12 +0000297void
Sebastian Redl3397c552010-08-18 23:56:27 +0000298ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000299 const SubstTemplateTypeParmType *T) {
300 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
301 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000303}
304
305void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000306ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
307 const SubstTemplateTypeParmPackType *T) {
308 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
309 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
310 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
311}
312
313void
Sebastian Redl3397c552010-08-18 23:56:27 +0000314ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000315 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000316 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000317 Writer.AddTemplateName(T->getTemplateName(), Record);
318 Record.push_back(T->getNumArgs());
319 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
320 ArgI != ArgE; ++ArgI)
321 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000322 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
323 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000324 : T->getCanonicalTypeInternal(),
325 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000326 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl3397c552010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000331 VisitArrayType(T);
332 Writer.AddStmt(T->getSizeExpr());
333 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000334 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000335}
336
337void
Sebastian Redl3397c552010-08-18 23:56:27 +0000338ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000339 const DependentSizedExtVectorType *T) {
340 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000341 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000342}
343
344void
Sebastian Redl3397c552010-08-18 23:56:27 +0000345ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000346 Record.push_back(T->getDepth());
347 Record.push_back(T->getIndex());
348 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000349 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000350 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000351}
352
353void
Sebastian Redl3397c552010-08-18 23:56:27 +0000354ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000355 Record.push_back(T->getKeyword());
356 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
357 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000358 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
359 : T->getCanonicalTypeInternal(),
360 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000361 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000362}
363
364void
Sebastian Redl3397c552010-08-18 23:56:27 +0000365ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000366 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000367 Record.push_back(T->getKeyword());
368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddIdentifierRef(T->getIdentifier(), Record);
370 Record.push_back(T->getNumArgs());
371 for (DependentTemplateSpecializationType::iterator
372 I = T->begin(), E = T->end(); I != E; ++I)
373 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000375}
376
Douglas Gregor7536dd52010-12-20 02:24:11 +0000377void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
378 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000379 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000380 Record.push_back(*NumExpansions + 1);
381 else
382 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000383 Code = TYPE_PACK_EXPANSION;
384}
385
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000386void ASTTypeWriter::VisitParenType(const ParenType *T) {
387 Writer.AddTypeRef(T->getInnerType(), Record);
388 Code = TYPE_PAREN;
389}
390
Sebastian Redl3397c552010-08-18 23:56:27 +0000391void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000392 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000393 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
394 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000395 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000396}
397
Sebastian Redl3397c552010-08-18 23:56:27 +0000398void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000399 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000400 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000401 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000402}
403
Sebastian Redl3397c552010-08-18 23:56:27 +0000404void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000405 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000406 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000407}
408
Sebastian Redl3397c552010-08-18 23:56:27 +0000409void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000410 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000411 Record.push_back(T->getNumProtocols());
Stephen Hines651f13c2014-04-23 16:59:28 -0700412 for (const auto *I : T->quals())
413 Writer.AddDeclRef(I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000414 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000415}
416
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000417void
Sebastian Redl3397c552010-08-18 23:56:27 +0000418ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000419 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000420 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000421}
422
Eli Friedmanb001de72011-10-06 23:00:33 +0000423void
424ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
425 Writer.AddTypeRef(T->getValueType(), Record);
426 Code = TYPE_ATOMIC;
427}
428
John McCalla1ee0c52009-10-16 21:56:05 +0000429namespace {
430
431class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000432 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000433 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000434
435public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000436 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000437 : Writer(Writer), Record(Record) { }
438
John McCall51bd8032009-10-18 01:05:36 +0000439#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000440#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000441 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000442#include "clang/AST/TypeLocNodes.def"
443
John McCall51bd8032009-10-18 01:05:36 +0000444 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
445 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000446};
447
448}
449
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
451 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000454 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
455 if (TL.needsExtraLocalData()) {
456 Record.push_back(TL.getWrittenTypeSpec());
457 Record.push_back(TL.getWrittenSignSpec());
458 Record.push_back(TL.getWrittenWidthSpec());
459 Record.push_back(TL.hasModeAttr());
460 }
John McCalla1ee0c52009-10-16 21:56:05 +0000461}
John McCall51bd8032009-10-18 01:05:36 +0000462void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000464}
John McCall51bd8032009-10-18 01:05:36 +0000465void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000467}
Reid Kleckner12df2462013-06-24 17:51:48 +0000468void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
469 // nothing to do
470}
Stephen Hines651f13c2014-04-23 16:59:28 -0700471void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
472 // nothing to do
473}
John McCall51bd8032009-10-18 01:05:36 +0000474void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000476}
John McCall51bd8032009-10-18 01:05:36 +0000477void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000479}
John McCall51bd8032009-10-18 01:05:36 +0000480void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000482}
John McCall51bd8032009-10-18 01:05:36 +0000483void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000485 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000486}
John McCall51bd8032009-10-18 01:05:36 +0000487void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
489 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
490 Record.push_back(TL.getSizeExpr() ? 1 : 0);
491 if (TL.getSizeExpr())
492 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000493}
John McCall51bd8032009-10-18 01:05:36 +0000494void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
495 VisitArrayTypeLoc(TL);
496}
497void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
498 VisitArrayTypeLoc(TL);
499}
500void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
501 VisitArrayTypeLoc(TL);
502}
503void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
504 DependentSizedArrayTypeLoc TL) {
505 VisitArrayTypeLoc(TL);
506}
507void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
508 DependentSizedExtVectorTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getNameLoc(), Record);
510}
511void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
514void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
517void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000518 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000519 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
520 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000521 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Stephen Hines651f13c2014-04-23 16:59:28 -0700522 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
523 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000524}
525void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
526 VisitFunctionTypeLoc(TL);
527}
528void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
529 VisitFunctionTypeLoc(TL);
530}
John McCalled976492009-12-04 22:46:56 +0000531void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
John McCall51bd8032009-10-18 01:05:36 +0000534void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
537void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000538 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
539 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
540 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000541}
542void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000543 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
544 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
545 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
546 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000547}
548void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
Sean Huntca63c202011-05-24 22:41:36 +0000551void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getKWLoc(), Record);
553 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
554 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
555 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
556}
Richard Smith34b41d92011-02-20 03:19:35 +0000557void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
John McCall51bd8032009-10-18 01:05:36 +0000560void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562}
563void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getNameLoc(), Record);
565}
John McCall9d156a72011-01-06 01:58:22 +0000566void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
567 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
568 if (TL.hasAttrOperand()) {
569 SourceRange range = TL.getAttrOperandParensRange();
570 Writer.AddSourceLocation(range.getBegin(), Record);
571 Writer.AddSourceLocation(range.getEnd(), Record);
572 }
573 if (TL.hasAttrExprOperand()) {
574 Expr *operand = TL.getAttrExprOperand();
575 Record.push_back(operand ? 1 : 0);
576 if (operand) Writer.AddStmt(operand);
577 } else if (TL.hasAttrEnumOperand()) {
578 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
579 }
580}
John McCall51bd8032009-10-18 01:05:36 +0000581void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583}
John McCall49a832b2009-10-18 09:09:24 +0000584void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
585 SubstTemplateTypeParmTypeLoc TL) {
586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
587}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000588void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
589 SubstTemplateTypeParmPackTypeLoc TL) {
590 Writer.AddSourceLocation(TL.getNameLoc(), Record);
591}
John McCall51bd8032009-10-18 01:05:36 +0000592void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
593 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000594 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000595 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
596 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
597 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
598 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000599 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
600 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000601}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000602void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
604 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
605}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000606void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000607 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000608 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000609}
John McCall3cb0ebd2010-03-10 03:28:59 +0000610void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
611 Writer.AddSourceLocation(TL.getNameLoc(), Record);
612}
Douglas Gregor4714c122010-03-31 17:34:00 +0000613void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000614 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000615 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000616 Writer.AddSourceLocation(TL.getNameLoc(), Record);
617}
John McCall33500952010-06-11 00:33:02 +0000618void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
619 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000620 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000621 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000622 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000623 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000624 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
625 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
626 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000627 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
628 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000629}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000630void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
631 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
632}
John McCall51bd8032009-10-18 01:05:36 +0000633void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
634 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000635}
636void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
637 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000638 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
639 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
640 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
641 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000642}
John McCall54e14c42009-10-22 22:37:11 +0000643void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000645}
Eli Friedmanb001de72011-10-06 23:00:33 +0000646void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getKWLoc(), Record);
648 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
649 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
650}
John McCalla1ee0c52009-10-16 21:56:05 +0000651
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000652//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000653// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000654//===----------------------------------------------------------------------===//
655
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000656static void EmitBlockID(unsigned ID, const char *Name,
657 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000658 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000659 Record.clear();
660 Record.push_back(ID);
661 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
662
663 // Emit the block name if present.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700664 if (!Name || Name[0] == 0)
665 return;
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000666 Record.clear();
667 while (*Name)
668 Record.push_back(*Name++);
669 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
670}
671
672static void EmitRecordID(unsigned ID, const char *Name,
673 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000674 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000675 Record.clear();
676 Record.push_back(ID);
677 while (*Name)
678 Record.push_back(*Name++);
679 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000680}
681
682static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000683 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000684#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000685 RECORD(STMT_STOP);
686 RECORD(STMT_NULL_PTR);
687 RECORD(STMT_NULL);
688 RECORD(STMT_COMPOUND);
689 RECORD(STMT_CASE);
690 RECORD(STMT_DEFAULT);
691 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000692 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000693 RECORD(STMT_IF);
694 RECORD(STMT_SWITCH);
695 RECORD(STMT_WHILE);
696 RECORD(STMT_DO);
697 RECORD(STMT_FOR);
698 RECORD(STMT_GOTO);
699 RECORD(STMT_INDIRECT_GOTO);
700 RECORD(STMT_CONTINUE);
701 RECORD(STMT_BREAK);
702 RECORD(STMT_RETURN);
703 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000704 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000705 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000706 RECORD(EXPR_PREDEFINED);
707 RECORD(EXPR_DECL_REF);
708 RECORD(EXPR_INTEGER_LITERAL);
709 RECORD(EXPR_FLOATING_LITERAL);
710 RECORD(EXPR_IMAGINARY_LITERAL);
711 RECORD(EXPR_STRING_LITERAL);
712 RECORD(EXPR_CHARACTER_LITERAL);
713 RECORD(EXPR_PAREN);
714 RECORD(EXPR_UNARY_OPERATOR);
715 RECORD(EXPR_SIZEOF_ALIGN_OF);
716 RECORD(EXPR_ARRAY_SUBSCRIPT);
717 RECORD(EXPR_CALL);
718 RECORD(EXPR_MEMBER);
719 RECORD(EXPR_BINARY_OPERATOR);
720 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
721 RECORD(EXPR_CONDITIONAL_OPERATOR);
722 RECORD(EXPR_IMPLICIT_CAST);
723 RECORD(EXPR_CSTYLE_CAST);
724 RECORD(EXPR_COMPOUND_LITERAL);
725 RECORD(EXPR_EXT_VECTOR_ELEMENT);
726 RECORD(EXPR_INIT_LIST);
727 RECORD(EXPR_DESIGNATED_INIT);
728 RECORD(EXPR_IMPLICIT_VALUE_INIT);
729 RECORD(EXPR_VA_ARG);
730 RECORD(EXPR_ADDR_LABEL);
731 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000732 RECORD(EXPR_CHOOSE);
733 RECORD(EXPR_GNU_NULL);
734 RECORD(EXPR_SHUFFLE_VECTOR);
735 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000736 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000737 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000738 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000739 RECORD(EXPR_OBJC_ARRAY_LITERAL);
740 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000741 RECORD(EXPR_OBJC_ENCODE);
742 RECORD(EXPR_OBJC_SELECTOR_EXPR);
743 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
744 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
745 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
746 RECORD(EXPR_OBJC_KVC_REF_EXPR);
747 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000748 RECORD(STMT_OBJC_FOR_COLLECTION);
749 RECORD(STMT_OBJC_CATCH);
750 RECORD(STMT_OBJC_FINALLY);
751 RECORD(STMT_OBJC_AT_TRY);
752 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
753 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000754 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000755 RECORD(EXPR_CXX_OPERATOR_CALL);
756 RECORD(EXPR_CXX_CONSTRUCT);
757 RECORD(EXPR_CXX_STATIC_CAST);
758 RECORD(EXPR_CXX_DYNAMIC_CAST);
759 RECORD(EXPR_CXX_REINTERPRET_CAST);
760 RECORD(EXPR_CXX_CONST_CAST);
761 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000762 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smith7c3e6152013-06-12 22:31:48 +0000763 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000764 RECORD(EXPR_CXX_BOOL_LITERAL);
765 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000766 RECORD(EXPR_CXX_TYPEID_EXPR);
767 RECORD(EXPR_CXX_TYPEID_TYPE);
768 RECORD(EXPR_CXX_UUIDOF_EXPR);
769 RECORD(EXPR_CXX_UUIDOF_TYPE);
770 RECORD(EXPR_CXX_THIS);
771 RECORD(EXPR_CXX_THROW);
772 RECORD(EXPR_CXX_DEFAULT_ARG);
773 RECORD(EXPR_CXX_BIND_TEMPORARY);
774 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
775 RECORD(EXPR_CXX_NEW);
776 RECORD(EXPR_CXX_DELETE);
777 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
778 RECORD(EXPR_EXPR_WITH_CLEANUPS);
779 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
780 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
781 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
782 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
783 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000784 RECORD(EXPR_CXX_NOEXCEPT);
785 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000786 RECORD(EXPR_PACK_EXPANSION);
787 RECORD(EXPR_SIZEOF_PACK);
788 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000789 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000790#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000791}
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Sebastian Redla4232eb2010-08-18 23:56:21 +0000793void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000794 RecordData Record;
795 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000797#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
798#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000800 // Control Block.
801 BLOCK(CONTROL_BLOCK);
802 RECORD(METADATA);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700803 RECORD(MODULE_NAME);
804 RECORD(MODULE_MAP_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000805 RECORD(IMPORTS);
806 RECORD(LANGUAGE_OPTIONS);
807 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000808 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000809 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000810 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000811 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000812 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000813 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000814 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000815 RECORD(PREPROCESSOR_OPTIONS);
816
Douglas Gregorc337fef2012-10-19 00:45:00 +0000817 BLOCK(INPUT_FILES_BLOCK);
818 RECORD(INPUT_FILE);
819
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000820 // AST Top-Level Block.
821 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000822 RECORD(TYPE_OFFSET);
823 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000824 RECORD(IDENTIFIER_OFFSET);
825 RECORD(IDENTIFIER_TABLE);
Stephen Hines651f13c2014-04-23 16:59:28 -0700826 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000827 RECORD(SPECIAL_TYPES);
828 RECORD(STATISTICS);
829 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000830 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000831 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000832 RECORD(SELECTOR_OFFSETS);
833 RECORD(METHOD_POOL);
834 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000835 RECORD(SOURCE_LOCATION_OFFSETS);
836 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000837 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000838 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000839 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000840 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000841 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000842 RECORD(SEMA_DECL_REFS);
843 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
844 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
845 RECORD(DECL_REPLACEMENTS);
846 RECORD(UPDATE_VISIBLE);
847 RECORD(DECL_UPDATE_OFFSETS);
848 RECORD(DECL_UPDATES);
849 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
850 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000851 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000852 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000853 RECORD(FP_PRAGMA_OPTIONS);
854 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000855 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000856 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000857 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000858 RECORD(MODULE_OFFSET_MAP);
859 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000860 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000861 RECORD(FILE_SORTED_DECLS);
862 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000863 RECORD(MERGED_DECLARATIONS);
864 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000865 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000866 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000867 RECORD(MACRO_TABLE);
Richard Smithac32d902013-08-07 21:41:30 +0000868 RECORD(LATE_PARSED_TEMPLATE);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700869 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000870
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000871 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000872 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000873 RECORD(SM_SLOC_FILE_ENTRY);
874 RECORD(SM_SLOC_BUFFER_ENTRY);
875 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000876 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000878 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000879 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000880 RECORD(PP_MACRO_OBJECT_LIKE);
881 RECORD(PP_MACRO_FUNCTION_LIKE);
882 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000883
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000884 // Decls and Types block.
885 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000886 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000887 RECORD(TYPE_COMPLEX);
888 RECORD(TYPE_POINTER);
889 RECORD(TYPE_BLOCK_POINTER);
890 RECORD(TYPE_LVALUE_REFERENCE);
891 RECORD(TYPE_RVALUE_REFERENCE);
892 RECORD(TYPE_MEMBER_POINTER);
893 RECORD(TYPE_CONSTANT_ARRAY);
894 RECORD(TYPE_INCOMPLETE_ARRAY);
895 RECORD(TYPE_VARIABLE_ARRAY);
896 RECORD(TYPE_VECTOR);
897 RECORD(TYPE_EXT_VECTOR);
898 RECORD(TYPE_FUNCTION_PROTO);
899 RECORD(TYPE_FUNCTION_NO_PROTO);
900 RECORD(TYPE_TYPEDEF);
901 RECORD(TYPE_TYPEOF_EXPR);
902 RECORD(TYPE_TYPEOF);
903 RECORD(TYPE_RECORD);
904 RECORD(TYPE_ENUM);
905 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000906 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000907 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000908 RECORD(TYPE_DECLTYPE);
909 RECORD(TYPE_ELABORATED);
910 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
911 RECORD(TYPE_UNRESOLVED_USING);
912 RECORD(TYPE_INJECTED_CLASS_NAME);
913 RECORD(TYPE_OBJC_OBJECT);
914 RECORD(TYPE_TEMPLATE_TYPE_PARM);
915 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
916 RECORD(TYPE_DEPENDENT_NAME);
917 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
918 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
919 RECORD(TYPE_PAREN);
920 RECORD(TYPE_PACK_EXPANSION);
921 RECORD(TYPE_ATTRIBUTED);
922 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000923 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000924 RECORD(DECL_TYPEDEF);
925 RECORD(DECL_ENUM);
926 RECORD(DECL_RECORD);
927 RECORD(DECL_ENUM_CONSTANT);
928 RECORD(DECL_FUNCTION);
929 RECORD(DECL_OBJC_METHOD);
930 RECORD(DECL_OBJC_INTERFACE);
931 RECORD(DECL_OBJC_PROTOCOL);
932 RECORD(DECL_OBJC_IVAR);
933 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000934 RECORD(DECL_OBJC_CATEGORY);
935 RECORD(DECL_OBJC_CATEGORY_IMPL);
936 RECORD(DECL_OBJC_IMPLEMENTATION);
937 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
938 RECORD(DECL_OBJC_PROPERTY);
939 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000940 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000941 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000942 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000943 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000944 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000945 RECORD(DECL_FILE_SCOPE_ASM);
946 RECORD(DECL_BLOCK);
947 RECORD(DECL_CONTEXT_LEXICAL);
948 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000949 RECORD(DECL_NAMESPACE);
950 RECORD(DECL_NAMESPACE_ALIAS);
951 RECORD(DECL_USING);
952 RECORD(DECL_USING_SHADOW);
953 RECORD(DECL_USING_DIRECTIVE);
954 RECORD(DECL_UNRESOLVED_USING_VALUE);
955 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
956 RECORD(DECL_LINKAGE_SPEC);
957 RECORD(DECL_CXX_RECORD);
958 RECORD(DECL_CXX_METHOD);
959 RECORD(DECL_CXX_CONSTRUCTOR);
960 RECORD(DECL_CXX_DESTRUCTOR);
961 RECORD(DECL_CXX_CONVERSION);
962 RECORD(DECL_ACCESS_SPEC);
963 RECORD(DECL_FRIEND);
964 RECORD(DECL_FRIEND_TEMPLATE);
965 RECORD(DECL_CLASS_TEMPLATE);
966 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
967 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000968 RECORD(DECL_VAR_TEMPLATE);
969 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
970 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000971 RECORD(DECL_FUNCTION_TEMPLATE);
972 RECORD(DECL_TEMPLATE_TYPE_PARM);
973 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
974 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
975 RECORD(DECL_STATIC_ASSERT);
976 RECORD(DECL_CXX_BASE_SPECIFIERS);
977 RECORD(DECL_INDIRECTFIELD);
978 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
979
Douglas Gregora72d8c42011-06-03 02:27:19 +0000980 // Statements and Exprs can occur in the Decls and Types block.
981 AddStmtsExprs(Stream, Record);
982
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000983 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000984 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000985 RECORD(PPD_MACRO_DEFINITION);
986 RECORD(PPD_INCLUSION_DIRECTIVE);
987
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000988#undef RECORD
989#undef BLOCK
990 Stream.ExitBlock();
991}
992
Douglas Gregore650c8c2009-07-07 00:12:59 +0000993/// \brief Adjusts the given filename to only write out the portion of the
994/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000995///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000996/// \param Filename the file name to adjust.
997///
998/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
999/// the returned filename will be adjusted by this system root.
1000///
1001/// \returns either the original filename (if it needs no adjustment) or the
1002/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +00001003static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +00001004adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001005 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Douglas Gregor832d6202011-07-22 16:35:34 +00001007 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001008 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregore650c8c2009-07-07 00:12:59 +00001010 // Verify that the filename and the system root have the same prefix.
1011 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +00001012 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +00001013 if (Filename[Pos] != isysroot[Pos])
1014 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregore650c8c2009-07-07 00:12:59 +00001016 // We hit the end of the filename before we hit the end of the system root.
1017 if (!Filename[Pos])
1018 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Douglas Gregore650c8c2009-07-07 00:12:59 +00001020 // If the file name has a '/' at the current position, skip over the '/'.
1021 // We distinguish sysroot-based includes from absolute includes by the
1022 // absence of '/' at the beginning of sysroot-based includes.
1023 if (Filename[Pos] == '/')
1024 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregore650c8c2009-07-07 00:12:59 +00001026 return Filename + Pos;
1027}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001028
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001029/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001030void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1031 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001032 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001033 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001034 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1035 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001036
Douglas Gregore650c8c2009-07-07 00:12:59 +00001037 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001038 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1039 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1040 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1041 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1042 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1043 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1044 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1045 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1046 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1047 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1048 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001049 Record.push_back(VERSION_MAJOR);
1050 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001051 Record.push_back(CLANG_VERSION_MAJOR);
1052 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001053 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001054 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001055 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1056 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001057
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001058 // Module name
1059 if (WritingModule) {
1060 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1061 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1063 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1064 RecordData Record;
1065 Record.push_back(MODULE_NAME);
1066 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1067 }
1068
1069 // Module map file
1070 if (WritingModule) {
1071 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1072 Abbrev->Add(BitCodeAbbrevOp(MODULE_MAP_FILE));
1073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Filename
1074 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1075
1076 assert(WritingModule->ModuleMap && "missing module map");
1077 SmallString<128> ModuleMap(WritingModule->ModuleMap->getName());
1078 llvm::sys::fs::make_absolute(ModuleMap);
1079 RecordData Record;
1080 Record.push_back(MODULE_MAP_FILE);
1081 Stream.EmitRecordWithBlob(AbbrevCode, Record, ModuleMap.str());
1082 }
1083
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001084 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001085 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001086 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregore95b9192011-08-17 21:07:30 +00001087 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001088
1089 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1090 M != MEnd; ++M) {
1091 // Skip modules that weren't directly imported.
1092 if (!(*M)->isDirectlyImported())
1093 continue;
1094
1095 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001096 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001097 Record.push_back((*M)->File->getSize());
1098 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001099 const std::string &FileName = (*M)->FileName;
1100 Record.push_back(FileName.size());
1101 Record.append(FileName.begin(), FileName.end());
1102 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001103 Stream.EmitRecord(IMPORTS, Record);
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001106 // Language options.
1107 Record.clear();
1108 const LangOptions &LangOpts = Context.getLangOpts();
1109#define LANGOPT(Name, Bits, Default, Description) \
1110 Record.push_back(LangOpts.Name);
1111#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1112 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1113#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001114#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1115#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001116
1117 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1118 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1119
1120 Record.push_back(LangOpts.CurrentModule.size());
1121 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001122
1123 // Comment options.
1124 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1125 for (CommentOptions::BlockCommandNamesTy::const_iterator
1126 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1127 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1128 I != IEnd; ++I) {
1129 AddString(*I, Record);
1130 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001131 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001132
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001133 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1134
Douglas Gregoree097c12012-10-18 17:58:09 +00001135 // Target options.
1136 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001137 const TargetInfo &Target = Context.getTargetInfo();
1138 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001139 AddString(TargetOpts.Triple, Record);
1140 AddString(TargetOpts.CPU, Record);
1141 AddString(TargetOpts.ABI, Record);
Douglas Gregoree097c12012-10-18 17:58:09 +00001142 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1143 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1144 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1145 }
1146 Record.push_back(TargetOpts.Features.size());
1147 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1148 AddString(TargetOpts.Features[I], Record);
1149 }
1150 Stream.EmitRecord(TARGET_OPTIONS, Record);
1151
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001152 // Diagnostic options.
1153 Record.clear();
1154 const DiagnosticOptions &DiagOpts
1155 = Context.getDiagnostics().getDiagnosticOptions();
1156#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1157#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1158 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1159#include "clang/Basic/DiagnosticOptions.def"
1160 Record.push_back(DiagOpts.Warnings.size());
1161 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1162 AddString(DiagOpts.Warnings[I], Record);
1163 // Note: we don't serialize the log or serialization file names, because they
1164 // are generally transient files and will almost always be overridden.
1165 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1166
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001167 // File system options.
1168 Record.clear();
1169 const FileSystemOptions &FSOpts
1170 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1171 AddString(FSOpts.WorkingDir, Record);
1172 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1173
Douglas Gregorbbf38312012-10-24 16:50:34 +00001174 // Header search options.
1175 Record.clear();
1176 const HeaderSearchOptions &HSOpts
1177 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1178 AddString(HSOpts.Sysroot, Record);
1179
1180 // Include entries.
1181 Record.push_back(HSOpts.UserEntries.size());
1182 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1183 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1184 AddString(Entry.Path, Record);
1185 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001186 Record.push_back(Entry.IsFramework);
1187 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001188 }
1189
1190 // System header prefixes.
1191 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1192 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1193 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1194 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1195 }
1196
1197 AddString(HSOpts.ResourceDir, Record);
1198 AddString(HSOpts.ModuleCachePath, Record);
Stephen Hines651f13c2014-04-23 16:59:28 -07001199 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001200 Record.push_back(HSOpts.DisableModuleHash);
1201 Record.push_back(HSOpts.UseBuiltinIncludes);
1202 Record.push_back(HSOpts.UseStandardSystemIncludes);
1203 Record.push_back(HSOpts.UseStandardCXXIncludes);
1204 Record.push_back(HSOpts.UseLibcxx);
1205 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1206
Douglas Gregora71a7d82012-10-24 20:05:57 +00001207 // Preprocessor options.
1208 Record.clear();
1209 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1210
1211 // Macro definitions.
1212 Record.push_back(PPOpts.Macros.size());
1213 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1214 AddString(PPOpts.Macros[I].first, Record);
1215 Record.push_back(PPOpts.Macros[I].second);
1216 }
1217
1218 // Includes
1219 Record.push_back(PPOpts.Includes.size());
1220 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1221 AddString(PPOpts.Includes[I], Record);
1222
1223 // Macro includes
1224 Record.push_back(PPOpts.MacroIncludes.size());
1225 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1226 AddString(PPOpts.MacroIncludes[I], Record);
1227
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001228 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001229 // Detailed record is important since it is used for the module cache hash.
1230 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001231 AddString(PPOpts.ImplicitPCHInclude, Record);
1232 AddString(PPOpts.ImplicitPTHInclude, Record);
1233 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1234 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1235
Douglas Gregor31d375f2011-05-06 21:43:30 +00001236 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001237 SourceManager &SM = Context.getSourceManager();
1238 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1239 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001240 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1241 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001242 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1243 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1244
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001245 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001247 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001248
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001249 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001250 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001251 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001252 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001253 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001254 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001255 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001256 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001257
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001258 Record.clear();
1259 Record.push_back(SM.getMainFileID().getOpaqueValue());
1260 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1261
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001262 // Original PCH directory
1263 if (!OutputFile.empty() && OutputFile != "-") {
1264 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1265 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1267 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1268
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001269 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001270
1271 llvm::sys::fs::make_absolute(OutputPath);
1272 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1273
1274 RecordData Record;
1275 Record.push_back(ORIGINAL_PCH_DIR);
1276 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1277 }
1278
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001279 WriteInputFiles(Context.SourceMgr,
1280 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregorb22d1942013-07-22 20:48:33 +00001281 isysroot,
1282 PP.getLangOpts().Modules);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001283 Stream.ExitBlock();
1284}
1285
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001286namespace {
1287 /// \brief An input file.
1288 struct InputFileEntry {
1289 const FileEntry *File;
1290 bool IsSystemFile;
1291 bool BufferOverridden;
1292 };
1293}
1294
1295void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1296 HeaderSearchOptions &HSOpts,
Douglas Gregorb22d1942013-07-22 20:48:33 +00001297 StringRef isysroot,
1298 bool Modules) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001299 using namespace llvm;
1300 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1301 RecordData Record;
1302
1303 // Create input-file abbreviation.
1304 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1305 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001306 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001307 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1308 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001309 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001310 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1311 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1312
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001313 // Get all ContentCache objects for files, sorted by whether the file is a
1314 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001315 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001316 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1317 // Get this source location entry.
1318 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001319 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001320
1321 // We only care about file entries that were not overridden.
1322 if (!SLoc->isFile())
1323 continue;
1324 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001325 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001326 continue;
1327
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001328 InputFileEntry Entry;
1329 Entry.File = Cache->OrigEntry;
1330 Entry.IsSystemFile = Cache->IsSystemFile;
1331 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001332 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001333 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001334 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001335 SortedFiles.push_front(Entry);
1336 }
1337
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001338 unsigned UserFilesNum = 0;
1339 // Write out all of the input files.
1340 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001341 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001342 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001343 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001344
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001345 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001346 if (InputFileID != 0)
1347 continue; // already recorded this file.
1348
Douglas Gregora930dc92012-10-22 18:42:04 +00001349 // Record this entry's offset.
1350 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001351
1352 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001353
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001354 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001355 ++UserFilesNum;
1356
Douglas Gregor745e6f12012-10-19 00:38:02 +00001357 Record.clear();
1358 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001359 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001360
1361 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001362 Record.push_back(Entry.File->getSize());
1363 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001364
Douglas Gregora930dc92012-10-22 18:42:04 +00001365 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001366 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001367
Douglas Gregor745e6f12012-10-19 00:38:02 +00001368 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001369 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001370 SmallString<128> FilePath(Filename);
1371
1372 // Ask the file manager to fixup the relative path for us. This will
1373 // honor the working directory.
Stephen Hines651f13c2014-04-23 16:59:28 -07001374 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001375
1376 // FIXME: This call to make_absolute shouldn't be necessary, the
1377 // call to FixupRelativePath should always return an absolute path.
1378 llvm::sys::fs::make_absolute(FilePath);
1379 Filename = FilePath.c_str();
1380
1381 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1382
1383 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1384 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001385
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001386 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001387
1388 // Create input file offsets abbreviation.
1389 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1390 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1391 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001392 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1393 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001394 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1395 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1396
1397 // Write input file offsets.
1398 Record.clear();
1399 Record.push_back(INPUT_FILE_OFFSETS);
1400 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001401 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001402 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001403}
1404
Douglas Gregor14f79002009-04-10 03:52:48 +00001405//===----------------------------------------------------------------------===//
1406// Source Manager Serialization
1407//===----------------------------------------------------------------------===//
1408
1409/// \brief Create an abbreviation for the SLocEntry that refers to a
1410/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001411static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001412 using namespace llvm;
1413 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001414 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001419 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
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.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001429static unsigned CreateSLocBufferAbbrev(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_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1435 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001438 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001439}
1440
1441/// \brief Create an abbreviation for the SLocEntry that refers to a
1442/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001443static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001444 using namespace llvm;
1445 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001446 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001448 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001449}
1450
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001451/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1452/// expansion.
1453static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001454 using namespace llvm;
1455 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001456 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001461 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001462 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001463}
1464
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465namespace {
1466 // Trait used for the on-disk hash table of header search information.
1467 class HeaderFileInfoTrait {
1468 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001469 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001470
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001471 // Keep track of the framework names we've used during serialization.
1472 SmallVector<char, 128> FrameworkStringData;
1473 llvm::StringMap<unsigned> FrameworkNameOffset;
1474
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001475 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001476 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1477 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001478
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001479 struct key_type {
1480 const FileEntry *FE;
1481 const char *Filename;
1482 };
1483 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001484
1485 typedef HeaderFileInfo data_type;
1486 typedef const data_type &data_type_ref;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001487 typedef unsigned hash_value_type;
1488 typedef unsigned offset_type;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001489
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001490 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001491 // The hash is based only on size/time of the file, so that the reader can
1492 // match even when symlinking or excess path elements ("foo/../", "../")
1493 // change the form of the name. However, complete path is still the key.
1494 return llvm::hash_combine(key.FE->getSize(),
1495 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001496 }
1497
1498 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001499 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001500 using namespace llvm::support;
1501 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001502 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001503 Writer.write<uint16_t>(KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001504 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001505 if (Data.isModuleHeader)
1506 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07001507 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001508 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001509 }
1510
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001511 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001512 using namespace llvm::support;
1513 endian::Writer<little> LE(Out);
1514 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001515 KeyLen -= 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001516 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001517 KeyLen -= 8;
1518 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001519 }
1520
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001521 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001522 data_type_ref Data, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001523 using namespace llvm::support;
1524 endian::Writer<little> LE(Out);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001525 uint64_t Start = Out.tell(); (void)Start;
1526
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001527 unsigned char Flags = (Data.HeaderRole << 6)
1528 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001529 | (Data.isPragmaOnce << 4)
1530 | (Data.DirInfo << 2)
1531 | (Data.Resolved << 1)
1532 | Data.IndexHeaderMapHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -07001533 LE.write<uint8_t>(Flags);
1534 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001535
1536 if (!Data.ControllingMacro)
Stephen Hines651f13c2014-04-23 16:59:28 -07001537 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001538 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001539 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001540
1541 unsigned Offset = 0;
1542 if (!Data.Framework.empty()) {
1543 // If this header refers into a framework, save the framework name.
1544 llvm::StringMap<unsigned>::iterator Pos
1545 = FrameworkNameOffset.find(Data.Framework);
1546 if (Pos == FrameworkNameOffset.end()) {
1547 Offset = FrameworkStringData.size() + 1;
1548 FrameworkStringData.append(Data.Framework.begin(),
1549 Data.Framework.end());
1550 FrameworkStringData.push_back(0);
1551
1552 FrameworkNameOffset[Data.Framework] = Offset;
1553 } else
1554 Offset = Pos->second;
1555 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001556 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001557
1558 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001559 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Stephen Hines651f13c2014-04-23 16:59:28 -07001560 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001561 }
1562
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001563 assert(Out.tell() - Start == DataLen && "Wrong data length");
1564 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001565
1566 const char *strings_begin() const { return FrameworkStringData.begin(); }
1567 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001568 };
1569} // end anonymous namespace
1570
1571/// \brief Write the header search block for the list of files that
1572///
1573/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001574void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001575 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001576 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1577
1578 if (FilesByUID.size() > HS.header_file_size())
1579 FilesByUID.resize(HS.header_file_size());
1580
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001581 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001582 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001583 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001584 unsigned NumHeaderSearchEntries = 0;
1585 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1586 const FileEntry *File = FilesByUID[UID];
1587 if (!File)
1588 continue;
1589
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001590 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1591 // from the external source if it was not provided already.
Stephen Hines651f13c2014-04-23 16:59:28 -07001592 HeaderFileInfo HFI;
1593 if (!HS.tryGetFileInfo(File, HFI) ||
1594 (HFI.External && Chain) ||
1595 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001596 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001597
1598 // Turn the file name into an absolute path, if it isn't already.
1599 const char *Filename = File->getName();
1600 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1601
1602 // If we performed any translation on the file name at all, we need to
1603 // save this string, since the generator will refer to it later.
1604 if (Filename != File->getName()) {
1605 Filename = strdup(Filename);
1606 SavedStrings.push_back(Filename);
1607 }
1608
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001609 HeaderFileInfoTrait::key_type key = { File, Filename };
1610 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001611 ++NumHeaderSearchEntries;
1612 }
1613
1614 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001615 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001616 uint32_t BucketOffset;
1617 {
Stephen Hines651f13c2014-04-23 16:59:28 -07001618 using namespace llvm::support;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001619 llvm::raw_svector_ostream Out(TableData);
1620 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07001621 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001622 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1623 }
1624
1625 // Create a blob abbreviation
1626 using namespace llvm;
1627 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1628 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1633 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1634
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001635 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001636 RecordData Record;
1637 Record.push_back(HEADER_SEARCH_TABLE);
1638 Record.push_back(BucketOffset);
1639 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001640 Record.push_back(TableData.size());
1641 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001642 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1643
1644 // Free all of the strings we had to duplicate.
1645 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001646 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001647}
1648
Douglas Gregor14f79002009-04-10 03:52:48 +00001649/// \brief Writes the block containing the serialized form of the
1650/// source manager.
1651///
1652/// TODO: We should probably use an on-disk hash table (stored in a
1653/// blob), indexed based on the file name, so that we only create
1654/// entries for files that we actually need. In the common case (no
1655/// errors), we probably won't have to create file entries for any of
1656/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001657void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001658 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001659 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001660 RecordData Record;
1661
Chris Lattnerf04ad692009-04-10 17:16:57 +00001662 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001663 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001664
1665 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001666 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1667 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1668 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001669 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001670
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001671 // Write out the source location entry table. We skip the first
1672 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001673 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001674 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001675 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1676 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001677 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001678 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001679 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001680 FileID FID = FileID::get(I);
1681 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001682
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001683 // Record the offset of this source-location entry.
1684 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1685
1686 // Figure out which record code to use.
1687 unsigned Code;
1688 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001689 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1690 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001691 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001692 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001693 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001694 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001695 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001696 Record.clear();
1697 Record.push_back(Code);
1698
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001699 // Starting offset of this entry within this module, so skip the dummy.
1700 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001701 if (SLoc->isFile()) {
1702 const SrcMgr::FileInfo &File = SLoc->getFile();
1703 Record.push_back(File.getIncludeLoc().getRawEncoding());
1704 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1705 Record.push_back(File.hasLineDirectives());
1706
1707 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001708 if (Content->OrigEntry) {
1709 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001710 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001711
Douglas Gregora930dc92012-10-22 18:42:04 +00001712 // The source location entry is a file. Emit input file ID.
1713 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1714 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001716 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001717
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001718 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001719 if (FDI != FileDeclIDs.end()) {
1720 Record.push_back(FDI->second->FirstDeclIndex);
1721 Record.push_back(FDI->second->DeclIDs.size());
1722 } else {
1723 Record.push_back(0);
1724 Record.push_back(0);
1725 }
Douglas Gregora081da52011-11-16 20:05:18 +00001726
Douglas Gregora930dc92012-10-22 18:42:04 +00001727 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001728
1729 if (Content->BufferOverridden) {
1730 Record.clear();
1731 Record.push_back(SM_SLOC_BUFFER_BLOB);
1732 const llvm::MemoryBuffer *Buffer
1733 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1734 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1735 StringRef(Buffer->getBufferStart(),
1736 Buffer->getBufferSize() + 1));
1737 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001738 } else {
1739 // The source location entry is a buffer. The blob associated
1740 // with this entry contains the contents of the buffer.
1741
1742 // We add one to the size so that we capture the trailing NULL
1743 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1744 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001745 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001746 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001747 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001748 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001749 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001750 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001751 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001752 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001753 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001754 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001755
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001756 if (strcmp(Name, "<built-in>") == 0) {
1757 PreloadSLocs.push_back(SLocEntryOffsets.size());
1758 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001759 }
1760 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001761 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001762 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001763 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1764 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001765 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1766 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001767
1768 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001769 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001770 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001771 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001772 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001773 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001774 }
1775 }
1776
Douglas Gregorc9490c02009-04-16 22:23:12 +00001777 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001778
1779 if (SLocEntryOffsets.empty())
1780 return;
1781
Sebastian Redl3397c552010-08-18 23:56:27 +00001782 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001783 // table is used for lazily loading source-location information.
1784 using namespace llvm;
1785 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001786 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1790 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001792 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001793 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001794 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001795 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001796 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001797
Sebastian Redl3397c552010-08-18 23:56:27 +00001798 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001799 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001800 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001801
1802 // Write the line table. It depends on remapping working, so it must come
1803 // after the source location offsets.
1804 if (SourceMgr.hasLineTable()) {
1805 LineTableInfo &LineTable = SourceMgr.getLineTable();
1806
1807 Record.clear();
1808 // Emit the file names
1809 Record.push_back(LineTable.getNumFilenames());
1810 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1811 // Emit the file name
1812 const char *Filename = LineTable.getFilename(I);
1813 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1814 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1815 Record.push_back(FilenameLen);
1816 if (FilenameLen)
1817 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1818 }
1819
1820 // Emit the line entries
1821 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1822 L != LEnd; ++L) {
1823 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001824 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001825 continue;
1826
1827 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001828 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001829
1830 // Emit the line entries
1831 Record.push_back(L->second.size());
1832 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1833 LEEnd = L->second.end();
1834 LE != LEEnd; ++LE) {
1835 Record.push_back(LE->FileOffset);
1836 Record.push_back(LE->LineNo);
1837 Record.push_back(LE->FilenameID);
1838 Record.push_back((unsigned)LE->FileKind);
1839 Record.push_back(LE->IncludeOffset);
1840 }
1841 }
1842 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1843 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001844}
1845
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001846//===----------------------------------------------------------------------===//
1847// Preprocessor Serialization
1848//===----------------------------------------------------------------------===//
1849
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001850namespace {
1851class ASTMacroTableTrait {
1852public:
1853 typedef IdentID key_type;
1854 typedef key_type key_type_ref;
1855
1856 struct Data {
1857 uint32_t MacroDirectivesOffset;
1858 };
1859
1860 typedef Data data_type;
1861 typedef const data_type &data_type_ref;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001862 typedef unsigned hash_value_type;
1863 typedef unsigned offset_type;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001864
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001865 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001866 return llvm::hash_value(IdID);
1867 }
1868
1869 std::pair<unsigned,unsigned>
1870 static EmitKeyDataLength(raw_ostream& Out,
1871 key_type_ref Key, data_type_ref Data) {
1872 unsigned KeyLen = 4; // IdentID.
1873 unsigned DataLen = 4; // MacroDirectivesOffset.
1874 return std::make_pair(KeyLen, DataLen);
1875 }
1876
1877 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001878 using namespace llvm::support;
1879 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001880 }
1881
1882 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1883 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001884 using namespace llvm::support;
1885 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001886 }
1887};
1888} // end anonymous namespace
1889
Benjamin Kramer767b3d22013-09-22 14:10:29 +00001890static int compareMacroDirectives(
1891 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1892 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1893 return X->first->getName().compare(Y->first->getName());
Douglas Gregor9c736102011-02-10 18:20:09 +00001894}
1895
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001896static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1897 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001898 if (MacroInfo *MI = MD->getMacroInfo())
1899 if (MI->isBuiltinMacro())
1900 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001901
1902 if (IsModule) {
1903 SourceLocation Loc = MD->getLocation();
1904 if (Loc.isInvalid())
1905 return true;
1906 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1907 return true;
1908 }
1909
1910 return false;
1911}
1912
Chris Lattner0b1fb982009-04-10 17:15:23 +00001913/// \brief Writes the block containing the serialized form of the
1914/// preprocessor.
1915///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001916void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001917 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1918 if (PPRec)
1919 WritePreprocessorDetail(*PPRec);
1920
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001921 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001922
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001923 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1924 if (PP.getCounterValue() != 0) {
1925 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001926 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001927 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001928 }
1929
1930 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001931 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Sebastian Redl3397c552010-08-18 23:56:27 +00001933 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001934 // FIXME: use diagnostics subsystem for localization etc.
1935 if (PP.SawDateOrTime())
1936 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Douglas Gregorecdcb882010-10-20 22:00:55 +00001938
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001939 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001940 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001941
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001942 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001943 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001944 MacroDirectives;
1945 for (Preprocessor::macro_iterator
1946 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1947 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001948 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001949 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001950 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001951
Douglas Gregor9c736102011-02-10 18:20:09 +00001952 // Sort the set of macro definitions that need to be serialized by the
1953 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001954 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1955 &compareMacroDirectives);
1956
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001957 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001958
1959 // Emit the macro directives as a list and associate the offset with the
1960 // identifier they belong to.
1961 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1962 const IdentifierInfo *Name = MacroDirectives[I].first;
1963 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1964 MacroDirective *MD = MacroDirectives[I].second;
1965
1966 // If the macro or identifier need no updates, don't write the macro history
1967 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001968 // FIXME: Chain the macro history instead of re-writing it.
1969 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001970 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1971 continue;
1972
1973 // Emit the macro directives in reverse source order.
1974 for (; MD; MD = MD->getPrevious()) {
1975 if (shouldIgnoreMacro(MD, IsModule, PP))
1976 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001977
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001978 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001979 Record.push_back(MD->getKind());
1980 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1981 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1982 Record.push_back(InfoID);
1983 Record.push_back(DefMD->isImported());
1984 Record.push_back(DefMD->isAmbiguous());
1985
1986 } else if (VisibilityMacroDirective *
1987 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1988 Record.push_back(VisMD->isPublic());
1989 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001990 }
1991 if (Record.empty())
1992 continue;
1993
1994 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1995 Record.clear();
1996
1997 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1998
1999 IdentID NameID = getIdentifierRef(Name);
2000 ASTMacroTableTrait::Data data;
2001 data.MacroDirectivesOffset = MacroDirectiveOffset;
2002 Generator.insert(NameID, data);
2003 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002004
Douglas Gregora8235d62012-10-09 23:05:51 +00002005 /// \brief Offsets of each of the macros into the bitstream, indexed by
2006 /// the local macro ID
2007 ///
2008 /// For each identifier that is associated with a macro, this map
2009 /// provides the offset into the bitstream where that macro is
2010 /// defined.
2011 std::vector<uint32_t> MacroOffsets;
2012
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002013 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2014 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2015 MacroInfo *MI = MacroInfosToEmit[I].MI;
2016 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00002017
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002018 if (ID < FirstMacroID) {
2019 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2020 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00002021 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002022
2023 // Record the local offset of this macro.
2024 unsigned Index = ID - FirstMacroID;
2025 if (Index == MacroOffsets.size())
2026 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2027 else {
2028 if (Index > MacroOffsets.size())
2029 MacroOffsets.resize(Index + 1);
2030
2031 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2032 }
2033
2034 AddIdentifierRef(Name, Record);
2035 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2036 AddSourceLocation(MI->getDefinitionLoc(), Record);
2037 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2038 Record.push_back(MI->isUsed());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002039 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002040 unsigned Code;
2041 if (MI->isObjectLike()) {
2042 Code = PP_MACRO_OBJECT_LIKE;
2043 } else {
2044 Code = PP_MACRO_FUNCTION_LIKE;
2045
2046 Record.push_back(MI->isC99Varargs());
2047 Record.push_back(MI->isGNUVarargs());
2048 Record.push_back(MI->hasCommaPasting());
2049 Record.push_back(MI->getNumArgs());
2050 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2051 I != E; ++I)
2052 AddIdentifierRef(*I, Record);
2053 }
2054
2055 // If we have a detailed preprocessing record, record the macro definition
2056 // ID that corresponds to this macro.
2057 if (PPRec)
2058 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2059
2060 Stream.EmitRecord(Code, Record);
2061 Record.clear();
2062
2063 // Emit the tokens array.
2064 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2065 // Note that we know that the preprocessor does not have any annotation
2066 // tokens in it because they are created by the parser, and thus can't
2067 // be in a macro definition.
2068 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002069 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002070 Stream.EmitRecord(PP_TOKEN, Record);
2071 Record.clear();
2072 }
2073 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002074 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002075
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002076 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002077
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002078 // Create the on-disk hash table in a buffer.
2079 SmallString<4096> MacroTable;
2080 uint32_t BucketOffset;
2081 {
Stephen Hines651f13c2014-04-23 16:59:28 -07002082 using namespace llvm::support;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002083 llvm::raw_svector_ostream Out(MacroTable);
2084 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07002085 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002086 BucketOffset = Generator.Emit(Out);
2087 }
2088
2089 // Write the macro table
2090 using namespace llvm;
2091 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2092 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2095 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2096
2097 Record.push_back(MACRO_TABLE);
2098 Record.push_back(BucketOffset);
2099 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2100 Record.clear();
2101
Douglas Gregora8235d62012-10-09 23:05:51 +00002102 // Write the offsets table for macro IDs.
2103 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002104 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002105 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2109
2110 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2111 Record.clear();
2112 Record.push_back(MACRO_OFFSET);
2113 Record.push_back(MacroOffsets.size());
2114 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2115 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2116 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002117}
2118
2119void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002120 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002121 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002122
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002123 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002124
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002125 // Enter the preprocessor block.
2126 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002127
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002128 // If the preprocessor has a preprocessing record, emit it.
2129 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002130 using namespace llvm;
2131
2132 // Set up the abbreviation for
2133 unsigned InclusionAbbrev = 0;
2134 {
2135 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2136 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002140 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2142 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2143 }
2144
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002145 unsigned FirstPreprocessorEntityID
2146 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2147 + NUM_PREDEF_PP_ENTITY_IDS;
2148 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002149 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002150 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2151 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002152 E != EEnd;
2153 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002154 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002155
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002156 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2157 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002158
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002159 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002160 // Record this macro definition's ID.
2161 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002162
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002163 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002164 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2165 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002166 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002167
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002168 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002169 Record.push_back(ME->isBuiltinMacro());
2170 if (ME->isBuiltinMacro())
2171 AddIdentifierRef(ME->getName(), Record);
2172 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002173 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002174 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002175 continue;
2176 }
2177
2178 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2179 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002180 Record.push_back(ID->getFileName().size());
2181 Record.push_back(ID->wasInQuotes());
2182 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002183 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002184 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002185 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002186 // Check that the FileEntry is not null because it was not resolved and
2187 // we create a PCH even with compiler errors.
2188 if (ID->getFile())
2189 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002190 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2191 continue;
2192 }
2193
2194 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2195 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002196 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002197
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002198 // Write the offsets table for the preprocessing record.
2199 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002200 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2201
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002202 // Write the offsets table for identifier IDs.
2203 using namespace llvm;
2204 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002205 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002208 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002209
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002210 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002211 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002212 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002213 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2214 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002215 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002216}
2217
Douglas Gregore209e502011-12-06 01:10:29 +00002218unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2219 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2220 if (Known != SubmoduleIDs.end())
2221 return Known->second;
2222
2223 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2224}
2225
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002226unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2227 if (!Mod)
2228 return 0;
2229
2230 llvm::DenseMap<Module *, unsigned>::const_iterator
2231 Known = SubmoduleIDs.find(Mod);
2232 if (Known != SubmoduleIDs.end())
2233 return Known->second;
2234
2235 return 0;
2236}
2237
Douglas Gregor26ced122011-12-01 00:59:36 +00002238/// \brief Compute the number of modules within the given tree (including the
2239/// given module).
2240static unsigned getNumberOfModules(Module *Mod) {
2241 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002242 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2243 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002244 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002245 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002246
2247 return ChildModules + 1;
2248}
2249
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002250void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002251 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002252 // FIXME: This feels like it belongs somewhere else, but there are no
2253 // other consumers of this information.
2254 SourceManager &SrcMgr = PP->getSourceManager();
2255 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Stephen Hines651f13c2014-04-23 16:59:28 -07002256 for (const auto *I : Context->local_imports()) {
Douglas Gregor55988682011-12-05 16:33:54 +00002257 if (Module *ImportedFrom
2258 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2259 SrcMgr))) {
2260 ImportedFrom->Imports.push_back(I->getImportedModule());
2261 }
2262 }
2263
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002264 // Enter the submodule description block.
2265 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2266
2267 // Write the abbreviations needed for the submodules block.
2268 using namespace llvm;
2269 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2270 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Stephen Hines651f13c2014-04-23 16:59:28 -07002275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2282 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2283
2284 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002285 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2287 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2288
2289 Abbrev = new BitCodeAbbrev();
2290 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2292 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002293
2294 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002295 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2297 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2298
2299 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002300 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2302 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2303
Douglas Gregor51f564f2011-12-31 04:05:44 +00002304 Abbrev = new BitCodeAbbrev();
2305 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smith5794b532013-10-28 22:18:19 +00002306 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor51f564f2011-12-31 04:05:44 +00002308 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2309
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002310 Abbrev = new BitCodeAbbrev();
2311 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2313 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2314
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002315 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002316 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2318 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2319
2320 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002321 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2324 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2325
Douglas Gregor63a72682013-03-20 00:22:05 +00002326 Abbrev = new BitCodeAbbrev();
2327 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2329 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2330
Douglas Gregor906d66a2013-03-20 21:10:35 +00002331 Abbrev = new BitCodeAbbrev();
2332 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2335 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2336
Douglas Gregor26ced122011-12-01 00:59:36 +00002337 // Write the submodule metadata block.
2338 RecordData Record;
2339 Record.push_back(getNumberOfModules(WritingModule));
2340 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2341 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2342
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002343 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002344 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002345 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002346 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002347 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002348 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002349 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002350
2351 // Emit the definition of the block.
2352 Record.clear();
2353 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002354 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002355 if (Mod->Parent) {
2356 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2357 Record.push_back(SubmoduleIDs[Mod->Parent]);
2358 } else {
2359 Record.push_back(0);
2360 }
2361 Record.push_back(Mod->IsFramework);
2362 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002363 Record.push_back(Mod->IsSystem);
Stephen Hines651f13c2014-04-23 16:59:28 -07002364 Record.push_back(Mod->IsExternC);
Douglas Gregor1e123682011-12-05 22:27:44 +00002365 Record.push_back(Mod->InferSubmodules);
2366 Record.push_back(Mod->InferExplicitSubmodules);
2367 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002368 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002369 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2370
Douglas Gregor51f564f2011-12-31 04:05:44 +00002371 // Emit the requirements.
Richard Smith5794b532013-10-28 22:18:19 +00002372 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +00002373 Record.clear();
2374 Record.push_back(SUBMODULE_REQUIRES);
Richard Smith5794b532013-10-28 22:18:19 +00002375 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002376 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smith5794b532013-10-28 22:18:19 +00002377 Mod->Requirements[I].first);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002378 }
2379
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002380 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002381 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002382 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002383 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002384 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002385 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002386 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2387 Record.clear();
2388 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2389 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2390 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002391 }
2392
2393 // Emit the headers.
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002394 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002395 Record.clear();
2396 Record.push_back(SUBMODULE_HEADER);
2397 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002398 Mod->NormalHeaders[I]->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002399 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002400 // Emit the excluded headers.
2401 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2402 Record.clear();
2403 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2404 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2405 Mod->ExcludedHeaders[I]->getName());
2406 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002407 // Emit the private headers.
2408 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2409 Record.clear();
2410 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2411 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2412 Mod->PrivateHeaders[I]->getName());
2413 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002414 ArrayRef<const FileEntry *>
2415 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2416 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002417 Record.clear();
2418 Record.push_back(SUBMODULE_TOPHEADER);
2419 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002420 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002421 }
Douglas Gregor55988682011-12-05 16:33:54 +00002422
2423 // Emit the imports.
2424 if (!Mod->Imports.empty()) {
2425 Record.clear();
2426 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002427 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002428 assert(ImportedID && "Unknown submodule!");
2429 Record.push_back(ImportedID);
2430 }
2431 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2432 }
2433
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002434 // Emit the exports.
2435 if (!Mod->Exports.empty()) {
2436 Record.clear();
2437 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002438 if (Module *Exported = Mod->Exports[I].getPointer()) {
2439 unsigned ExportedID = SubmoduleIDs[Exported];
2440 assert(ExportedID > 0 && "Unknown submodule ID?");
2441 Record.push_back(ExportedID);
2442 } else {
2443 Record.push_back(0);
2444 }
2445
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002446 Record.push_back(Mod->Exports[I].getInt());
2447 }
2448 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2449 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002450
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00002451 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2452 // Might be unnecessary as use declarations are only used to build the
2453 // module itself.
2454
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002455 // Emit the link libraries.
2456 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2457 Record.clear();
2458 Record.push_back(SUBMODULE_LINK_LIBRARY);
2459 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2460 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2461 Mod->LinkLibraries[I].Library);
2462 }
2463
Douglas Gregor906d66a2013-03-20 21:10:35 +00002464 // Emit the conflicts.
2465 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2466 Record.clear();
2467 Record.push_back(SUBMODULE_CONFLICT);
2468 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2469 assert(OtherID && "Unknown submodule!");
2470 Record.push_back(OtherID);
2471 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2472 Mod->Conflicts[I].Message);
2473 }
2474
Douglas Gregor63a72682013-03-20 00:22:05 +00002475 // Emit the configuration macros.
2476 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2477 Record.clear();
2478 Record.push_back(SUBMODULE_CONFIG_MACRO);
2479 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2480 Mod->ConfigMacros[I]);
2481 }
2482
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002483 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002484 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2485 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002486 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002487 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002488 }
2489
2490 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002491
2492 assert((NextSubmoduleID - FirstSubmoduleID
2493 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002494}
2495
Douglas Gregor185dbd72011-12-01 02:07:58 +00002496serialization::SubmoduleID
2497ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002498 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002499 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002500
2501 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002502 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002503 Module *OwningMod
2504 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002505 if (!OwningMod)
2506 return 0;
2507
Douglas Gregore209e502011-12-06 01:10:29 +00002508 // Check whether this submodule is part of our own module.
2509 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002510 return 0;
2511
Douglas Gregore209e502011-12-06 01:10:29 +00002512 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002513}
2514
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002515void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2516 bool isModule) {
2517 // Make sure set diagnostic pragmas don't affect the translation unit that
2518 // imports the module.
2519 // FIXME: Make diagnostic pragma sections work properly with modules.
2520 if (isModule)
2521 return;
2522
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002523 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2524 DiagStateIDMap;
2525 unsigned CurrID = 0;
2526 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002527 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002528 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002529 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2530 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002531 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002532 if (point.Loc.isInvalid())
2533 continue;
2534
2535 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002536 unsigned &DiagStateID = DiagStateIDMap[point.State];
2537 Record.push_back(DiagStateID);
2538
2539 if (DiagStateID == 0) {
2540 DiagStateID = ++CurrID;
2541 for (DiagnosticsEngine::DiagState::const_iterator
2542 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2543 if (I->second.isPragma()) {
2544 Record.push_back(I->first);
2545 Record.push_back(I->second.getMapping());
2546 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002547 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002548 Record.push_back(-1); // mark the end of the diag/map pairs for this
2549 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002550 }
2551 }
2552
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002553 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002554 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002555}
2556
Anders Carlssonc8505782011-03-06 18:41:18 +00002557void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2558 if (CXXBaseSpecifiersOffsets.empty())
2559 return;
2560
2561 RecordData Record;
2562
2563 // Create a blob abbreviation for the C++ base specifiers offsets.
2564 using namespace llvm;
2565
2566 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2567 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2570 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2571
Douglas Gregore92b8a12011-08-04 00:01:48 +00002572 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002573 Record.clear();
2574 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2575 Record.push_back(CXXBaseSpecifiersOffsets.size());
2576 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002577 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002578}
2579
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002580//===----------------------------------------------------------------------===//
2581// Type Serialization
2582//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002583
Sebastian Redl3397c552010-08-18 23:56:27 +00002584/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002585void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002586 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002587 if (Idx.getIndex() == 0) // we haven't seen this type before.
2588 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Douglas Gregor97475832010-10-05 18:37:06 +00002590 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002591
Douglas Gregor2cf26342009-04-09 22:27:44 +00002592 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002593 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002594 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002595 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002596 else if (TypeOffsets.size() < Index) {
2597 TypeOffsets.resize(Index + 1);
2598 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002599 }
2600
2601 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002602
Douglas Gregor2cf26342009-04-09 22:27:44 +00002603 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002604 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002605
Douglas Gregora4923eb2009-11-16 21:35:15 +00002606 if (T.hasLocalNonFastQualifiers()) {
2607 Qualifiers Qs = T.getLocalQualifiers();
2608 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002609 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002610 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002611 } else {
2612 switch (T->getTypeClass()) {
2613 // For all of the concrete, non-dependent types, call the
2614 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002615#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002616 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002617#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002618#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002619 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002620 }
2621
2622 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002623 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002624
2625 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002626 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002627}
2628
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002629//===----------------------------------------------------------------------===//
2630// Declaration Serialization
2631//===----------------------------------------------------------------------===//
2632
Douglas Gregor2cf26342009-04-09 22:27:44 +00002633/// \brief Write the block containing all of the declaration IDs
2634/// lexically declared within the given DeclContext.
2635///
2636/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2637/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002638uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002639 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002640 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002641 return 0;
2642
Douglas Gregorc9490c02009-04-16 22:23:12 +00002643 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002644 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002645 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002646 SmallVector<KindDeclIDPair, 64> Decls;
Stephen Hines651f13c2014-04-23 16:59:28 -07002647 for (const auto *D : DC->decls())
2648 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002649
Douglas Gregor25123082009-04-22 22:34:57 +00002650 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002651 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002652 return Offset;
2653}
2654
Sebastian Redla4232eb2010-08-18 23:56:21 +00002655void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002656 using namespace llvm;
2657 RecordData Record;
2658
2659 // Write the type offsets array
2660 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002662 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2665 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2666 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002667 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002668 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002669 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002670 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002671
2672 // Write the declaration offsets array
2673 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002674 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2678 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2679 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002680 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002681 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002682 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002683 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002684}
2685
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002686void ASTWriter::WriteFileDeclIDsMap() {
2687 using namespace llvm;
2688 RecordData Record;
2689
2690 // Join the vectors of DeclIDs from all files.
2691 SmallVector<DeclID, 256> FileSortedIDs;
2692 for (FileDeclIDsTy::iterator
2693 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2694 DeclIDInFileInfo &Info = *FI->second;
2695 Info.FirstDeclIndex = FileSortedIDs.size();
2696 for (LocDeclIDsTy::iterator
2697 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2698 FileSortedIDs.push_back(DI->second);
2699 }
2700
2701 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2702 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002703 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2705 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2706 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002707 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002708 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2709}
2710
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002711void ASTWriter::WriteComments() {
2712 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002713 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002714 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002715 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2716 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002717 I != E; ++I) {
2718 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002719 AddSourceRange((*I)->getSourceRange(), Record);
2720 Record.push_back((*I)->getKind());
2721 Record.push_back((*I)->isTrailingComment());
2722 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002723 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2724 }
2725 Stream.ExitBlock();
2726}
2727
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002728//===----------------------------------------------------------------------===//
2729// Global Method Pool and Selector Serialization
2730//===----------------------------------------------------------------------===//
2731
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002732namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002733// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002734class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002735 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002736
2737public:
2738 typedef Selector key_type;
2739 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002740
Sebastian Redl5d050072010-08-04 17:20:04 +00002741 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002742 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002743 ObjCMethodList Instance, Factory;
2744 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002745 typedef const data_type& data_type_ref;
2746
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002747 typedef unsigned hash_value_type;
2748 typedef unsigned offset_type;
2749
Sebastian Redl3397c552010-08-18 23:56:27 +00002750 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002752 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002753 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002754 }
Mike Stump1eb44332009-09-09 15:08:12 +00002755
2756 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002757 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002758 data_type_ref Methods) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002759 using namespace llvm::support;
2760 endian::Writer<little> LE(Out);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002761 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Stephen Hines651f13c2014-04-23 16:59:28 -07002762 LE.write<uint16_t>(KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002763 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2764 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002765 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002766 if (Method->Method)
2767 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002768 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002769 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002770 if (Method->Method)
2771 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07002772 LE.write<uint16_t>(DataLen);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002773 return std::make_pair(KeyLen, DataLen);
2774 }
Mike Stump1eb44332009-09-09 15:08:12 +00002775
Chris Lattner5f9e2722011-07-23 10:55:15 +00002776 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002777 using namespace llvm::support;
2778 endian::Writer<little> LE(Out);
Mike Stump1eb44332009-09-09 15:08:12 +00002779 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002780 assert((Start >> 32) == 0 && "Selector key offset too large");
2781 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002782 unsigned N = Sel.getNumArgs();
Stephen Hines651f13c2014-04-23 16:59:28 -07002783 LE.write<uint16_t>(N);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002784 if (N == 0)
2785 N = 1;
2786 for (unsigned I = 0; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07002787 LE.write<uint32_t>(
2788 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002789 }
Mike Stump1eb44332009-09-09 15:08:12 +00002790
Chris Lattner5f9e2722011-07-23 10:55:15 +00002791 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002792 data_type_ref Methods, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002793 using namespace llvm::support;
2794 endian::Writer<little> LE(Out);
Douglas Gregora67e58c2009-04-24 21:49:02 +00002795 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07002796 LE.write<uint32_t>(Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002797 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002798 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002799 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002800 if (Method->Method)
2801 ++NumInstanceMethods;
2802
2803 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002804 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002805 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002806 if (Method->Method)
2807 ++NumFactoryMethods;
2808
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002809 unsigned InstanceBits = Methods.Instance.getBits();
2810 assert(InstanceBits < 4);
2811 unsigned NumInstanceMethodsAndBits =
2812 (NumInstanceMethods << 2) | InstanceBits;
2813 unsigned FactoryBits = Methods.Factory.getBits();
2814 assert(FactoryBits < 4);
2815 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Stephen Hines651f13c2014-04-23 16:59:28 -07002816 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2817 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002818 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002819 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002820 if (Method->Method)
Stephen Hines651f13c2014-04-23 16:59:28 -07002821 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002822 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002823 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002824 if (Method->Method)
Stephen Hines651f13c2014-04-23 16:59:28 -07002825 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002826
2827 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002828 }
2829};
2830} // end anonymous namespace
2831
Sebastian Redl059612d2010-08-03 21:58:15 +00002832/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002833///
2834/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002835/// in an on-disk hash table indexed by the selector. The hash table also
2836/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002837void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002838 using namespace llvm;
2839
Sebastian Redl059612d2010-08-03 21:58:15 +00002840 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002841 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002842 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002843 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002844 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002845 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002846 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002847 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002848
Sebastian Redl059612d2010-08-03 21:58:15 +00002849 // Create the on-disk hash table representation. We walk through every
2850 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002851 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002852 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002853 I = SelectorIDs.begin(), E = SelectorIDs.end();
2854 I != E; ++I) {
2855 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002856 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002857 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002858 I->second,
2859 ObjCMethodList(),
2860 ObjCMethodList()
2861 };
2862 if (F != SemaRef.MethodPool.end()) {
2863 Data.Instance = F->second.first;
2864 Data.Factory = F->second.second;
2865 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002866 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002867 // changed.
2868 if (Chain && I->second < FirstSelectorID) {
2869 // Selector already exists. Did it change?
2870 bool changed = false;
2871 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002872 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002873 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002874 changed = true;
2875 }
2876 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002877 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002878 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002879 changed = true;
2880 }
2881 if (!changed)
2882 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002883 } else if (Data.Instance.Method || Data.Factory.Method) {
2884 // A new method pool entry.
2885 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002886 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002887 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002888 }
2889
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002890 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002891 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002892 uint32_t BucketOffset;
2893 {
Stephen Hines651f13c2014-04-23 16:59:28 -07002894 using namespace llvm::support;
Sebastian Redl3397c552010-08-18 23:56:27 +00002895 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002896 llvm::raw_svector_ostream Out(MethodPool);
2897 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07002898 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002899 BucketOffset = Generator.Emit(Out, Trait);
2900 }
2901
2902 // Create a blob abbreviation
2903 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002904 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2908 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2909
Douglas Gregor83941df2009-04-25 17:48:32 +00002910 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002911 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002912 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002913 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002914 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002915 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002916
2917 // Create a blob abbreviation for the selector table offsets.
2918 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002919 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2923 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2924
2925 // Write the selector offsets table.
2926 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002927 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002928 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002929 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002930 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002931 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002932 }
2933}
2934
Sebastian Redl3397c552010-08-18 23:56:27 +00002935/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002936void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002937 using namespace llvm;
2938 if (SemaRef.ReferencedSelectors.empty())
2939 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002940
Fariborz Jahanian32019832010-07-23 19:11:11 +00002941 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002942
Sebastian Redl3397c552010-08-18 23:56:27 +00002943 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002944 // very tricky to fix, and given that @selector shouldn't really appear in
2945 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002946 for (DenseMap<Selector, SourceLocation>::iterator S =
2947 SemaRef.ReferencedSelectors.begin(),
2948 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2949 Selector Sel = (*S).first;
2950 SourceLocation Loc = (*S).second;
2951 AddSelectorRef(Sel, Record);
2952 AddSourceLocation(Loc, Record);
2953 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002954 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002955}
2956
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002957//===----------------------------------------------------------------------===//
2958// Identifier Table Serialization
2959//===----------------------------------------------------------------------===//
2960
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002961namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002962class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002963 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002964 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002965 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002966 bool IsModule;
2967
Douglas Gregora92193e2009-04-28 21:18:29 +00002968 /// \brief Determines whether this is an "interesting" identifier
2969 /// that needs a full IdentifierInfo structure written into the hash
2970 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002971 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002972 if (II->isPoisoned() ||
2973 II->isExtensionToken() ||
2974 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002975 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002976 II->getFETokenInfo<void>())
2977 return true;
2978
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002979 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002980 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002981
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002982 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002983 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002984 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002985
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002986 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2987 if (!IsModule)
2988 return !shouldIgnoreMacro(Macro, IsModule, PP);
2989 SubmoduleID ModID;
2990 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2991 return true;
2992 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002993
2994 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002995 }
2996
Stephen Hines651f13c2014-04-23 16:59:28 -07002997 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2998
2999 MacroDirective *
3000 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003001 ModID = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07003002 llvm::SmallVector<SubmoduleID, 1> Overridden;
3003 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
3004 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
3005 return NextMD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003006 return nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003007 }
3008
Stephen Hines651f13c2014-04-23 16:59:28 -07003009 MacroDirective *
3010 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
3011 OverriddenList &Overridden) {
3012 if (MacroDirective *NextMD =
3013 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
3014 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
3015 return NextMD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003016 return nullptr;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003017 }
3018
3019 /// \brief Traverses the macro directives history and returns the latest
Stephen Hines651f13c2014-04-23 16:59:28 -07003020 /// public macro definition or undefinition that is not in ModID.
3021 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003022 /// will still be considered as defined/exported from submodule A.
Stephen Hines651f13c2014-04-23 16:59:28 -07003023 /// ModID is updated to the module containing the returned directive.
3024 ///
3025 /// FIXME: This process breaks down if a module defines a macro, imports
3026 /// another submodule that changes the macro, then changes the
3027 /// macro again itself.
3028 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
3029 SubmoduleID &ModID,
3030 OverriddenList &Overridden) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003031 if (!MD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003032 return nullptr;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003033
Stephen Hines651f13c2014-04-23 16:59:28 -07003034 Overridden.clear();
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003035 SubmoduleID OrigModID = ModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003036 Optional<bool> IsPublic;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003037 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003038 SubmoduleID ThisModID = getSubmoduleID(MD);
3039 if (ThisModID == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003040 IsPublic = Optional<bool>();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003041 continue;
3042 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003043 if (ThisModID != ModID) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003044 ModID = ThisModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003045 IsPublic = Optional<bool>();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003046 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003047
3048 // If this is a definition from a submodule import, that submodule's
3049 // definition is overridden by the definition or undefinition that we
3050 // started with.
3051 // FIXME: This should only apply to macros defined in OrigModID.
3052 // We can't do that currently, because a #include of a different submodule
3053 // of the same module just leaks through macros instead of providing new
3054 // DefMacroDirectives for them.
3055 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3056 // Figure out which submodule the macro was originally defined within.
3057 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3058 if (!SourceID) {
3059 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3060 if (DefLoc == MD->getLocation())
3061 SourceID = ThisModID;
3062 else
3063 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3064 }
3065 if (SourceID != OrigModID)
3066 Overridden.push_back(SourceID);
3067 }
3068
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003069 // We are looking for a definition in a different submodule than the one
3070 // that we started with. If a submodule has re-definitions of the same
3071 // macro, only the last definition will be used as the "exported" one.
3072 if (ModID == OrigModID)
3073 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003074
Stephen Hines651f13c2014-04-23 16:59:28 -07003075 // The latest visibility directive for a name in a submodule affects all
3076 // the directives that come before it.
3077 if (VisibilityMacroDirective *VisMD =
3078 dyn_cast<VisibilityMacroDirective>(MD)) {
3079 if (!IsPublic.hasValue())
3080 IsPublic = VisMD->isPublic();
3081 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3082 // FIXME: If we find an imported macro, we should include its list of
3083 // overrides in our export.
3084 return MD;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003085 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003086 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003087
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003088 return nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003089 }
3090
3091 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003092 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003093 }
3094
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003095public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003096 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003097 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003099 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003100 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003101
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003102 typedef unsigned hash_value_type;
3103 typedef unsigned offset_type;
3104
Douglas Gregoreee242f2011-10-27 09:33:13 +00003105 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3106 IdentifierResolver &IdResolver, bool IsModule)
3107 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003108
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003109 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003110 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003111 }
Mike Stump1eb44332009-09-09 15:08:12 +00003112
3113 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003114 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003115 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003116 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003117 MacroDirective *Macro = nullptr;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003118 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003119 DataLen += 2; // 2 bytes for builtin ID
3120 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003121 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003122 DataLen += 4; // MacroDirectives offset.
3123 if (IsModule) {
3124 SubmoduleID ModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003125 llvm::SmallVector<SubmoduleID, 4> Overridden;
3126 for (MacroDirective *
3127 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3128 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3129 // Previous macro's overrides.
3130 if (!Overridden.empty())
3131 DataLen += 4 * (1 + Overridden.size());
3132 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003133 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003134 // Previous macro's overrides.
3135 if (!Overridden.empty())
3136 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003137 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003138 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003139 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003140
Douglas Gregoreee242f2011-10-27 09:33:13 +00003141 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3142 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003143 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003144 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003145 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003146 using namespace llvm::support;
3147 endian::Writer<little> LE(Out);
3148
3149 LE.write<uint16_t>(DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003150 // We emit the key length after the data length so that every
3151 // string is preceded by a 16-bit length. This matches the PTH
3152 // format for storing identifiers.
Stephen Hines651f13c2014-04-23 16:59:28 -07003153 LE.write<uint16_t>(KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003154 return std::make_pair(KeyLen, DataLen);
3155 }
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Chris Lattner5f9e2722011-07-23 10:55:15 +00003157 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003158 unsigned KeyLen) {
3159 // Record the location of the key data. This is used when generating
3160 // the mapping from persistent IDs to strings.
3161 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003162 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003163 }
Mike Stump1eb44332009-09-09 15:08:12 +00003164
Stephen Hines651f13c2014-04-23 16:59:28 -07003165 static void emitMacroOverrides(raw_ostream &Out,
3166 llvm::ArrayRef<SubmoduleID> Overridden) {
3167 if (!Overridden.empty()) {
3168 using namespace llvm::support;
3169 endian::Writer<little> LE(Out);
3170 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
3171 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3172 LE.write<uint32_t>(Overridden[I]);
3173 }
3174 }
3175
Douglas Gregor7143aab2011-09-01 17:04:32 +00003176 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003177 IdentID ID, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003178 using namespace llvm::support;
3179 endian::Writer<little> LE(Out);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003180 MacroDirective *Macro = nullptr;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003181 if (!isInterestingIdentifier(II, Macro)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003182 LE.write<uint32_t>(ID << 1);
Douglas Gregora92193e2009-04-28 21:18:29 +00003183 return;
3184 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003185
Stephen Hines651f13c2014-04-23 16:59:28 -07003186 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003187 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3188 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Stephen Hines651f13c2014-04-23 16:59:28 -07003189 LE.write<uint16_t>(Bits);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003190 Bits = 0;
3191 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003192 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003193 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003194 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3195 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003196 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003197 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Stephen Hines651f13c2014-04-23 16:59:28 -07003198 LE.write<uint16_t>(Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003199
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003200 if (HadMacroDefinition) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003201 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003202 if (IsModule) {
3203 // Write the IDs of macros coming from different submodules.
3204 SubmoduleID ModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003205 llvm::SmallVector<SubmoduleID, 4> Overridden;
3206 for (MacroDirective *
3207 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3208 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3209 MacroID InfoID = 0;
3210 emitMacroOverrides(Out, Overridden);
3211 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3212 InfoID = Writer.getMacroID(DefMD->getInfo());
3213 assert(InfoID);
3214 LE.write<uint32_t>(InfoID << 1);
3215 } else {
3216 assert(isa<UndefMacroDirective>(MD));
3217 LE.write<uint32_t>((ModID << 1) | 1);
3218 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003219 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003220 emitMacroOverrides(Out, Overridden);
3221 LE.write<uint32_t>(0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003222 }
Douglas Gregor13292642011-12-02 15:45:10 +00003223 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003224
Douglas Gregor668c1a42009-04-21 22:25:48 +00003225 // Emit the declaration IDs in reverse order, because the
3226 // IdentifierResolver provides the declarations as they would be
3227 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003228 // "stat"), but the ASTReader adds declarations to the end of the list
3229 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003230 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003231 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3232 IdResolver.end());
Craig Topper09d19ef2013-07-04 03:08:24 +00003233 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003234 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003235 D != DEnd; ++D)
Stephen Hines651f13c2014-04-23 16:59:28 -07003236 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003237 }
3238
3239 /// \brief Returns the most recent local decl or the given decl if there are
3240 /// no local ones. The given decl is assumed to be the most recent one.
3241 Decl *getMostRecentLocalDecl(Decl *Orig) {
3242 // The only way a "from AST file" decl would be more recent from a local one
3243 // is if it came from a module.
3244 if (!PP.getLangOpts().Modules)
3245 return Orig;
3246
3247 // Look for a local in the decl chain.
3248 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3249 if (!D->isFromASTFile())
3250 return D;
3251 // If we come up a decl from a (chained-)PCH stop since we won't find a
3252 // local one.
3253 if (D->getOwningModuleID() == 0)
3254 break;
3255 }
3256
3257 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003258 }
3259};
3260} // end anonymous namespace
3261
Sebastian Redl3397c552010-08-18 23:56:27 +00003262/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003263///
3264/// The identifier table consists of a blob containing string data
3265/// (the actual identifiers themselves) and a separate "offsets" index
3266/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003267void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3268 IdentifierResolver &IdResolver,
3269 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003270 using namespace llvm;
3271
3272 // Create and write out the blob that contains the identifier
3273 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003274 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003275 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003276 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003277
Douglas Gregor92b059e2009-04-28 20:33:11 +00003278 // Look for any identifiers that were named while processing the
3279 // headers, but are otherwise not needed. We add these to the hash
3280 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003281 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003282 // file.
3283 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3284 IDEnd = PP.getIdentifierTable().end();
3285 ID != IDEnd; ++ID)
3286 getIdentifierRef(ID->second);
3287
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003288 // Create the on-disk hash table representation. We only store offsets
3289 // for identifiers that appear here for the first time.
3290 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003291 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003292 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3293 ID != IDEnd; ++ID) {
3294 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003295 if (!Chain || !ID->first->isFromAST() ||
3296 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003297 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003298 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003299 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003300
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003301 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003302 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003303 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003304 {
Stephen Hines651f13c2014-04-23 16:59:28 -07003305 using namespace llvm::support;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003306 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003307 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003308 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07003309 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003310 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003311 }
3312
3313 // Create a blob abbreviation
3314 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003315 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003318 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003319
3320 // Write the identifier table
3321 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003322 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003323 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003324 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003325 }
3326
3327 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003328 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003329 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3333 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3334
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003335#ifndef NDEBUG
3336 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3337 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3338#endif
3339
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003340 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003341 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003342 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003343 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003344 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003345 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003346}
3347
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003348//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003349// DeclContext's Name Lookup Table Serialization
3350//===----------------------------------------------------------------------===//
3351
3352namespace {
3353// Trait used for the on-disk hash table used in the method pool.
3354class ASTDeclContextNameLookupTrait {
3355 ASTWriter &Writer;
3356
3357public:
3358 typedef DeclarationName key_type;
3359 typedef key_type key_type_ref;
3360
3361 typedef DeclContext::lookup_result data_type;
3362 typedef const data_type& data_type_ref;
3363
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003364 typedef unsigned hash_value_type;
3365 typedef unsigned offset_type;
3366
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003367 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3368
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003369 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003370 llvm::FoldingSetNodeID ID;
3371 ID.AddInteger(Name.getNameKind());
3372
3373 switch (Name.getNameKind()) {
3374 case DeclarationName::Identifier:
3375 ID.AddString(Name.getAsIdentifierInfo()->getName());
3376 break;
3377 case DeclarationName::ObjCZeroArgSelector:
3378 case DeclarationName::ObjCOneArgSelector:
3379 case DeclarationName::ObjCMultiArgSelector:
3380 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3381 break;
3382 case DeclarationName::CXXConstructorName:
3383 case DeclarationName::CXXDestructorName:
3384 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003385 break;
3386 case DeclarationName::CXXOperatorName:
3387 ID.AddInteger(Name.getCXXOverloadedOperator());
3388 break;
3389 case DeclarationName::CXXLiteralOperatorName:
3390 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3391 case DeclarationName::CXXUsingDirective:
3392 break;
3393 }
3394
3395 return ID.ComputeHash();
3396 }
3397
3398 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003399 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003400 data_type_ref Lookup) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003401 using namespace llvm::support;
3402 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003403 unsigned KeyLen = 1;
3404 switch (Name.getNameKind()) {
3405 case DeclarationName::Identifier:
3406 case DeclarationName::ObjCZeroArgSelector:
3407 case DeclarationName::ObjCOneArgSelector:
3408 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003409 case DeclarationName::CXXLiteralOperatorName:
3410 KeyLen += 4;
3411 break;
3412 case DeclarationName::CXXOperatorName:
3413 KeyLen += 1;
3414 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003415 case DeclarationName::CXXConstructorName:
3416 case DeclarationName::CXXDestructorName:
3417 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003418 case DeclarationName::CXXUsingDirective:
3419 break;
3420 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003421 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003422
3423 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003424 unsigned DataLen = 2 + 4 * Lookup.size();
Stephen Hines651f13c2014-04-23 16:59:28 -07003425 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003426
3427 return std::make_pair(KeyLen, DataLen);
3428 }
3429
Chris Lattner5f9e2722011-07-23 10:55:15 +00003430 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003431 using namespace llvm::support;
3432 endian::Writer<little> LE(Out);
3433 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003434 switch (Name.getNameKind()) {
3435 case DeclarationName::Identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -07003436 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003437 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003438 case DeclarationName::ObjCZeroArgSelector:
3439 case DeclarationName::ObjCOneArgSelector:
3440 case DeclarationName::ObjCMultiArgSelector:
Stephen Hines651f13c2014-04-23 16:59:28 -07003441 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003442 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003443 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003444 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3445 "Invalid operator?");
Stephen Hines651f13c2014-04-23 16:59:28 -07003446 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003447 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003448 case DeclarationName::CXXLiteralOperatorName:
Stephen Hines651f13c2014-04-23 16:59:28 -07003449 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003450 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003451 case DeclarationName::CXXConstructorName:
3452 case DeclarationName::CXXDestructorName:
3453 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003454 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003455 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003456 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003457
3458 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003459 }
3460
Chris Lattner5f9e2722011-07-23 10:55:15 +00003461 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003462 data_type Lookup, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003463 using namespace llvm::support;
3464 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003465 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07003466 LE.write<uint16_t>(Lookup.size());
David Blaikie3bc93e32012-12-19 00:45:41 +00003467 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3468 I != E; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07003469 LE.write<uint32_t>(Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003470
3471 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3472 }
3473};
3474} // end anonymous namespace
3475
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003476template<typename Visitor>
3477static void visitLocalLookupResults(const DeclContext *ConstDC,
3478 bool NeedToReconcileExternalVisibleStorage,
3479 Visitor AddLookupResult) {
3480 // FIXME: We need to build the lookups table, which is logically const.
3481 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Stephen Hines651f13c2014-04-23 16:59:28 -07003482 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3483
Stephen Hines651f13c2014-04-23 16:59:28 -07003484 SmallVector<DeclarationName, 16> ExternalNames;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003485 for (auto &Lookup : *DC->buildLookup()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003486 if (Lookup.second.hasExternalDecls() ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003487 NeedToReconcileExternalVisibleStorage) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003488 // We don't know for sure what declarations are found by this name,
3489 // because the external source might have a different set from the set
3490 // that are in the lookup map, and we can't update it now without
3491 // risking invalidating our lookup iterator. So add it to a queue to
3492 // deal with later.
3493 ExternalNames.push_back(Lookup.first);
3494 continue;
3495 }
3496
3497 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3498 }
3499
3500 // Add the names we needed to defer. Note, this shouldn't add any new decls
3501 // to the list we need to serialize: any new declarations we find here should
3502 // be imported from an external source.
3503 // FIXME: What if the external source isn't an ASTReader?
3504 for (const auto &Name : ExternalNames)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003505 AddLookupResult(Name, DC->lookup(Name));
3506}
3507
3508void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
3509 if (UpdatedDeclContexts.insert(DC) && WritingAST) {
3510 // Ensure we emit all the visible declarations.
3511 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3512 [&](DeclarationName Name,
3513 DeclContext::lookup_const_result Result) {
3514 for (auto *Decl : Result)
3515 GetDeclRef(Decl);
3516 });
3517 }
3518}
3519
3520uint32_t
3521ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3522 llvm::SmallVectorImpl<char> &LookupTable) {
3523 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3524
3525 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3526 Generator;
3527 ASTDeclContextNameLookupTrait Trait(*this);
3528
3529 // Create the on-disk hash table representation.
3530 DeclarationName ConstructorName;
3531 DeclarationName ConversionName;
3532 SmallVector<NamedDecl *, 8> ConstructorDecls;
3533 SmallVector<NamedDecl *, 4> ConversionDecls;
3534
3535 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3536 [&](DeclarationName Name,
3537 DeclContext::lookup_result Result) {
3538 if (Result.empty())
3539 return;
3540
3541 // Different DeclarationName values of certain kinds are mapped to
3542 // identical serialized keys, because we don't want to use type
3543 // identifiers in the keys (since type ids are local to the module).
3544 switch (Name.getNameKind()) {
3545 case DeclarationName::CXXConstructorName:
3546 // There may be different CXXConstructorName DeclarationName values
3547 // in a DeclContext because a UsingDecl that inherits constructors
3548 // has the DeclarationName of the inherited constructors.
3549 if (!ConstructorName)
3550 ConstructorName = Name;
3551 ConstructorDecls.append(Result.begin(), Result.end());
3552 return;
3553
3554 case DeclarationName::CXXConversionFunctionName:
3555 if (!ConversionName)
3556 ConversionName = Name;
3557 ConversionDecls.append(Result.begin(), Result.end());
3558 return;
3559
3560 default:
3561 break;
3562 }
3563
3564 Generator.insert(Name, Result, Trait);
3565 });
Stephen Hines651f13c2014-04-23 16:59:28 -07003566
3567 // Add the constructors.
3568 if (!ConstructorDecls.empty()) {
3569 Generator.insert(ConstructorName,
3570 DeclContext::lookup_result(ConstructorDecls.begin(),
3571 ConstructorDecls.end()),
3572 Trait);
3573 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003574
Stephen Hines651f13c2014-04-23 16:59:28 -07003575 // Add the conversion functions.
3576 if (!ConversionDecls.empty()) {
3577 Generator.insert(ConversionName,
3578 DeclContext::lookup_result(ConversionDecls.begin(),
3579 ConversionDecls.end()),
3580 Trait);
3581 }
3582
3583 // Create the on-disk hash table in a buffer.
3584 llvm::raw_svector_ostream Out(LookupTable);
3585 // Make sure that no bucket is at offset 0
3586 using namespace llvm::support;
3587 endian::Writer<little>(Out).write<uint32_t>(0);
3588 return Generator.Emit(Out, Trait);
3589}
3590
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003591/// \brief Write the block containing all of the declaration IDs
3592/// visible from the given DeclContext.
3593///
3594/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003595/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003596uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3597 DeclContext *DC) {
3598 if (DC->getPrimaryContext() != DC)
3599 return 0;
3600
3601 // Since there is no name lookup into functions or methods, don't bother to
3602 // build a visible-declarations table for these entities.
3603 if (DC->isFunctionOrMethod())
3604 return 0;
3605
3606 // If not in C++, we perform name lookup for the translation unit via the
3607 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003608 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003609 return 0;
3610
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003611 // Serialize the contents of the mapping used for lookup. Note that,
3612 // although we have two very different code paths, the serialized
3613 // representation is the same for both cases: a declaration name,
3614 // followed by a size, followed by references to the visible
3615 // declarations that have that name.
3616 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003617 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003618 if (!Map || Map->empty())
3619 return 0;
3620
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003621 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003622 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003623 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003624
3625 // Write the lookup table
3626 RecordData Record;
3627 Record.push_back(DECL_CONTEXT_VISIBLE);
3628 Record.push_back(BucketOffset);
3629 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3630 LookupTable.str());
3631
3632 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3633 ++NumVisibleDeclContexts;
3634 return Offset;
3635}
3636
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003637/// \brief Write an UPDATE_VISIBLE block for the given context.
3638///
3639/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3640/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003641/// (in C++), for namespaces, and for classes with forward-declared unscoped
3642/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003643void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003644 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003645 if (!Map || Map->empty())
3646 return;
3647
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003648 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003649 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003650 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003651
3652 // Write the lookup table
3653 RecordData Record;
3654 Record.push_back(UPDATE_VISIBLE);
3655 Record.push_back(getDeclID(cast<Decl>(DC)));
3656 Record.push_back(BucketOffset);
3657 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3658}
3659
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003660/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3661void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3662 RecordData Record;
3663 Record.push_back(Opts.fp_contract);
3664 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3665}
3666
3667/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3668void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003669 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003670 return;
3671
3672 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3673 RecordData Record;
3674#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3675#include "clang/Basic/OpenCLExtensions.def"
3676 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3677}
3678
Douglas Gregor2171bf12012-01-15 16:58:34 +00003679void ASTWriter::WriteRedeclarations() {
3680 RecordData LocalRedeclChains;
3681 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3682
3683 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3684 Decl *First = Redeclarations[I];
Rafael Espindola7693b322013-10-19 02:13:21 +00003685 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor2171bf12012-01-15 16:58:34 +00003686
3687 Decl *MostRecent = First->getMostRecentDecl();
3688
3689 // If we only have a single declaration, there is no point in storing
3690 // a redeclaration chain.
3691 if (First == MostRecent)
3692 continue;
3693
3694 unsigned Offset = LocalRedeclChains.size();
3695 unsigned Size = 0;
3696 LocalRedeclChains.push_back(0); // Placeholder for the size.
3697
3698 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003699 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003700 Prev = Prev->getPreviousDecl()) {
3701 if (!Prev->isFromASTFile()) {
3702 AddDeclRef(Prev, LocalRedeclChains);
3703 ++Size;
3704 }
3705 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003706
3707 if (!First->isFromASTFile() && Chain) {
3708 Decl *FirstFromAST = MostRecent;
3709 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3710 if (Prev->isFromASTFile())
3711 FirstFromAST = Prev;
3712 }
3713
3714 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3715 }
3716
Douglas Gregor2171bf12012-01-15 16:58:34 +00003717 LocalRedeclChains[Offset] = Size;
3718
3719 // Reverse the set of local redeclarations, so that we store them in
3720 // order (since we found them in reverse order).
3721 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3722
Douglas Gregoraa945902013-02-18 15:53:43 +00003723 // Add the mapping from the first ID from the AST to the set of local
3724 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003725 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3726 LocalRedeclsMap.push_back(Info);
3727
3728 assert(N == Redeclarations.size() &&
3729 "Deserialized a declaration we shouldn't have");
3730 }
3731
3732 if (LocalRedeclChains.empty())
3733 return;
3734
3735 // Sort the local redeclarations map by the first declaration ID,
3736 // since the reader will be performing binary searches on this information.
3737 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3738
3739 // Emit the local redeclarations map.
3740 using namespace llvm;
3741 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3742 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3745 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3746
3747 RecordData Record;
3748 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3749 Record.push_back(LocalRedeclsMap.size());
3750 Stream.EmitRecordWithBlob(AbbrevID, Record,
3751 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3752 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3753
3754 // Emit the redeclaration chains.
3755 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3756}
3757
Douglas Gregorcff9f262012-01-27 01:47:08 +00003758void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003759 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003760 RecordData Categories;
3761
3762 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3763 unsigned Size = 0;
3764 unsigned StartIndex = Categories.size();
3765
3766 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3767
3768 // Allocate space for the size.
3769 Categories.push_back(0);
3770
3771 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003772 for (ObjCInterfaceDecl::known_categories_iterator
3773 Cat = Class->known_categories_begin(),
3774 CatEnd = Class->known_categories_end();
3775 Cat != CatEnd; ++Cat, ++Size) {
3776 assert(getDeclID(*Cat) != 0 && "Bogus category");
3777 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003778 }
3779
3780 // Update the size.
3781 Categories[StartIndex] = Size;
3782
3783 // Record this interface -> category map.
3784 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3785 CategoriesMap.push_back(CatInfo);
3786 }
3787
3788 // Sort the categories map by the definition ID, since the reader will be
3789 // performing binary searches on this information.
3790 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3791
3792 // Emit the categories map.
3793 using namespace llvm;
3794 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3795 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3798 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3799
3800 RecordData Record;
3801 Record.push_back(OBJC_CATEGORIES_MAP);
3802 Record.push_back(CategoriesMap.size());
3803 Stream.EmitRecordWithBlob(AbbrevID, Record,
3804 reinterpret_cast<char*>(CategoriesMap.data()),
3805 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3806
3807 // Emit the category lists.
3808 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3809}
3810
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003811void ASTWriter::WriteMergedDecls() {
3812 if (!Chain || Chain->MergedDecls.empty())
3813 return;
3814
3815 RecordData Record;
3816 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3817 IEnd = Chain->MergedDecls.end();
3818 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003819 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003820 : GetDeclRef(I->first);
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003821 assert(CanonID && "Merged declaration not known?");
3822
3823 Record.push_back(CanonID);
3824 Record.push_back(I->second.size());
3825 Record.append(I->second.begin(), I->second.end());
3826 }
3827 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3828}
3829
Richard Smithac32d902013-08-07 21:41:30 +00003830void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3831 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3832
3833 if (LPTMap.empty())
3834 return;
3835
3836 RecordData Record;
3837 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3838 ItEnd = LPTMap.end();
3839 It != ItEnd; ++It) {
3840 LateParsedTemplate *LPT = It->second;
3841 AddDeclRef(It->first, Record);
3842 AddDeclRef(LPT->D, Record);
3843 Record.push_back(LPT->Toks.size());
3844
3845 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3846 TokEnd = LPT->Toks.end();
3847 TokIt != TokEnd; ++TokIt) {
3848 AddToken(*TokIt, Record);
3849 }
3850 }
3851 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3852}
3853
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003854/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3855void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3856 RecordData Record;
3857 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3858 AddSourceLocation(PragmaLoc, Record);
3859 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3860}
3861
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003862//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003863// General Serialization Routines
3864//===----------------------------------------------------------------------===//
3865
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003866/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003867void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3868 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003869 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003870 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3871 e = Attrs.end(); i != e; ++i){
3872 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003873 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003874 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003875
Sean Huntcf807c42010-08-18 23:23:40 +00003876#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003877
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003878 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003879}
3880
John McCallaeeacf72013-05-03 00:10:13 +00003881void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3882 AddSourceLocation(Tok.getLocation(), Record);
3883 Record.push_back(Tok.getLength());
3884
3885 // FIXME: When reading literal tokens, reconstruct the literal pointer
3886 // if it is needed.
3887 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3888 // FIXME: Should translate token kind to a stable encoding.
3889 Record.push_back(Tok.getKind());
3890 // FIXME: Should translate token flags to a stable encoding.
3891 Record.push_back(Tok.getFlags());
3892}
3893
Chris Lattner5f9e2722011-07-23 10:55:15 +00003894void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003895 Record.push_back(Str.size());
3896 Record.insert(Record.end(), Str.begin(), Str.end());
3897}
3898
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003899void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3900 RecordDataImpl &Record) {
3901 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003902 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003903 Record.push_back(*Minor + 1);
3904 else
3905 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003906 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003907 Record.push_back(*Subminor + 1);
3908 else
3909 Record.push_back(0);
3910}
3911
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003912/// \brief Note that the identifier II occurs at the given offset
3913/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003914void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003915 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003916 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003917 // up earlier in the chain and thus don't need an offset.
3918 if (ID >= FirstIdentID)
3919 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003920}
3921
Douglas Gregor83941df2009-04-25 17:48:32 +00003922/// \brief Note that the selector Sel occurs at the given offset
3923/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003924void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003925 unsigned ID = SelectorIDs[Sel];
3926 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003927 // Don't record offsets for selectors that are also available in a different
3928 // file.
3929 if (ID < FirstSelectorID)
3930 return;
3931 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003932}
3933
Sebastian Redla4232eb2010-08-18 23:56:21 +00003934ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003935 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
3936 WritingModule(nullptr), WritingAST(false), DoneWritingDeclsAndTypes(false),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003937 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003938 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003939 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003940 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3941 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003942 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3943 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003944 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003945 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003946 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003947 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003948 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003949 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003950 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3951 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3952 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003953 DeclTypedefAbbrev(0),
3954 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3955 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003956{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003957}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003958
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003959ASTWriter::~ASTWriter() {
Stephen Hines651f13c2014-04-23 16:59:28 -07003960 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003961}
3962
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003963void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003964 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003965 Module *WritingModule, StringRef isysroot,
3966 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003967 WritingAST = true;
3968
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003969 ASTHasCompilerErrors = hasErrors;
3970
Douglas Gregor2cf26342009-04-09 22:27:44 +00003971 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003972 Stream.Emit((unsigned)'C', 8);
3973 Stream.Emit((unsigned)'P', 8);
3974 Stream.Emit((unsigned)'C', 8);
3975 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003976
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003977 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003978
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003979 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003980 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003981 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003982 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003983 Context = nullptr;
3984 PP = nullptr;
3985 this->WritingModule = nullptr;
3986
Douglas Gregor61c5e342011-09-17 00:05:03 +00003987 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003988}
3989
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003990template<typename Vector>
3991static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3992 ASTWriter::RecordData &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003993 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
3994 I != E; ++I) {
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003995 Writer.AddDeclRef(*I, Record);
3996 }
3997}
3998
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003999void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00004000 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00004001 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004002 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004003 using namespace llvm;
4004
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004005 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004006
Douglas Gregorecc2c092011-12-01 22:20:10 +00004007 // Make sure that the AST reader knows to finalize itself.
4008 if (Chain)
4009 Chain->finalizeForWriting();
4010
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004011 ASTContext &Context = SemaRef.Context;
4012 Preprocessor &PP = SemaRef.PP;
4013
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004014 // Set up predefined declaration IDs.
4015 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004016 if (Context.ObjCIdDecl)
4017 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004018 if (Context.ObjCSelDecl)
4019 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00004020 if (Context.ObjCClassDecl)
4021 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004022 if (Context.ObjCProtocolClassDecl)
4023 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00004024 if (Context.Int128Decl)
4025 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4026 if (Context.UInt128Decl)
4027 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00004028 if (Context.ObjCInstanceTypeDecl)
4029 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00004030 if (Context.BuiltinVaListDecl)
4031 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4032
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004033 if (!Chain) {
4034 // Make sure that we emit IdentifierInfos (and any attached
4035 // declarations) for builtins. We don't need to do this when we're
4036 // emitting chained PCH files, because all of the builtins will be
4037 // in the original PCH file.
4038 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00004039 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00004040 SmallVector<const char *, 32> BuiltinNames;
Eli Bendersky97a03cf2013-07-11 16:53:04 +00004041 if (!Context.getLangOpts().NoBuiltin) {
4042 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4043 }
Douglas Gregor2deaea32009-04-22 18:49:13 +00004044 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4045 getIdentifierRef(&Table.get(BuiltinNames[I]));
4046 }
4047
Douglas Gregoreee242f2011-10-27 09:33:13 +00004048 // If there are any out-of-date identifiers, bring them up to date.
4049 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00004050 // Find out-of-date identifiers.
4051 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00004052 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4053 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00004054 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004055 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00004056 OutOfDate.push_back(ID->second);
4057 }
4058
4059 // Update the out-of-date identifiers.
4060 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4061 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4062 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00004063 }
4064
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004065 // If we saw any DeclContext updates before we started writing the AST file,
4066 // make sure all visible decls in those DeclContexts are written out.
4067 if (!UpdatedDeclContexts.empty()) {
4068 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4069 UpdatedDeclContexts.clear();
4070 for (auto *DC : OldUpdatedDeclContexts)
4071 AddUpdatedDeclContext(DC);
4072 }
4073
Chris Lattner63d65f82009-09-08 18:19:27 +00004074 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00004075 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00004076 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004077 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004078 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00004079
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004080 // Build a record containing all of the file scoped decls in this file.
4081 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00004082 if (!isModule)
4083 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4084 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00004085
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004086 // Build a record containing all of the delegating constructors we still need
4087 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00004088 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004089 if (!isModule)
4090 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004091
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004092 // Write the set of weak, undeclared identifiers. We always write the
4093 // entire table, since later PCH files in a PCH chain are only interested in
4094 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004095 RecordData WeakUndeclaredIdentifiers;
4096 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00004097 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004098 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4099 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4100 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4101 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4102 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4103 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4104 }
4105 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004106
Richard Smith5ea6ef42013-01-10 23:43:47 +00004107 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00004108 // declarations in this header file. Generally, this record will be
4109 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00004110 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00004111 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00004112 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00004113 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00004114 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4115 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00004116 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00004117 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00004118 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00004119 }
4120
Douglas Gregorb81c1702009-04-27 20:06:05 +00004121 // Build a record containing all of the ext_vector declarations.
4122 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00004123 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004124
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004125 // Build a record containing all of the VTable uses information.
4126 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004127 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004128 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4129 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4130 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4131 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4132 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004133 }
4134
4135 // Build a record containing all of dynamic classes declarations.
4136 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00004137 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004138
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004139 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004140 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004141 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00004142 I = SemaRef.PendingInstantiations.begin(),
4143 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4144 AddDeclRef(I->first, PendingInstantiations);
4145 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004146 }
4147 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4148 "There are local ones at end of translation unit!");
4149
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004150 // Build a record containing some declaration references.
4151 RecordData SemaDeclRefs;
4152 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4153 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4154 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4155 }
4156
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004157 RecordData CUDASpecialDeclRefs;
4158 if (Context.getcudaConfigureCallDecl()) {
4159 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4160 }
4161
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004162 // Build a record containing all of the known namespaces.
4163 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00004164 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004165 I = SemaRef.KnownNamespaces.begin(),
4166 IEnd = SemaRef.KnownNamespaces.end();
4167 I != IEnd; ++I) {
4168 if (!I->second)
4169 AddDeclRef(I->first, KnownNamespaces);
4170 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004171
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004172 // Build a record of all used, undefined objects that require definitions.
4173 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00004174
4175 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004176 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00004177 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4178 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004179 AddDeclRef(I->first, UndefinedButUsed);
4180 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00004181 }
4182
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004183 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00004184 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004185
Sebastian Redl3397c552010-08-18 23:56:27 +00004186 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00004187 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004188 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004189
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00004190 // This is so that older clang versions, before the introduction
4191 // of the control block, can read and reject the newer PCH format.
4192 Record.clear();
4193 Record.push_back(VERSION_MAJOR);
4194 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4195
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004196 // Create a lexical update block containing all of the declarations in the
4197 // translation unit that do not come from other AST files.
4198 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4199 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Stephen Hines651f13c2014-04-23 16:59:28 -07004200 for (const auto *I : TU->noload_decls()) {
4201 if (!I->isFromASTFile())
4202 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004203 }
4204
4205 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4206 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4207 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4208 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4209 Record.clear();
4210 Record.push_back(TU_UPDATE_LEXICAL);
4211 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4212 data(NewGlobalDecls));
4213
4214 // And a visible updates block for the translation unit.
4215 Abv = new llvm::BitCodeAbbrev();
4216 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4217 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4218 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4219 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4220 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4221 WriteDeclContextVisibleUpdate(TU);
4222
4223 // If the translation unit has an anonymous namespace, and we don't already
4224 // have an update block for it, write it as an update block.
Stephen Hines651f13c2014-04-23 16:59:28 -07004225 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004226 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4227 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Stephen Hines651f13c2014-04-23 16:59:28 -07004228 if (Record.empty())
4229 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004230 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004231
Stephen Hines651f13c2014-04-23 16:59:28 -07004232 // Add update records for all mangling numbers and static local numbers.
4233 // These aren't really update records, but this is a convenient way of
4234 // tagging this rare extra data onto the declarations.
4235 for (const auto &Number : Context.MangleNumbers)
4236 if (!Number.first->isFromASTFile())
4237 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4238 Number.second));
4239 for (const auto &Number : Context.StaticLocalNumbers)
4240 if (!Number.first->isFromASTFile())
4241 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4242 Number.second));
4243
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004244 // Make sure visible decls, added to DeclContexts previously loaded from
4245 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004246 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004247 I = UpdatingVisibleDecls.begin(),
4248 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4249 GetDeclRef(*I);
4250 }
4251
Argyrios Kyrtzidis51e75ae2013-08-07 21:17:33 +00004252 // Make sure all decls associated with an identifier are registered for
4253 // serialization.
4254 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4255 IDEnd = PP.getIdentifierTable().end();
4256 ID != IDEnd; ++ID) {
4257 const IdentifierInfo *II = ID->second;
4258 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4259 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4260 DEnd = SemaRef.IdResolver.end();
4261 D != DEnd; ++D) {
4262 GetDeclRef(*D);
4263 }
4264 }
4265 }
4266
Douglas Gregora119da02011-08-02 16:26:37 +00004267 // Form the record of special types.
4268 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004269 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004270 AddTypeRef(Context.getFILEType(), SpecialTypes);
4271 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4272 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4273 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4274 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004275 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004276 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004277
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004278 if (Chain) {
4279 // Write the mapping information describing our module dependencies and how
4280 // each of those modules were mapped into our own offset/ID space, so that
4281 // the reader can build the appropriate mapping to its own offset/ID space.
4282 // The map consists solely of a blob with the following format:
4283 // *(module-name-len:i16 module-name:len*i8
4284 // source-location-offset:i32
4285 // identifier-id:i32
4286 // preprocessed-entity-id:i32
4287 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004288 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004289 // selector-id:i32
4290 // declaration-id:i32
4291 // c++-base-specifiers-id:i32
4292 // type-id:i32)
4293 //
4294 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4295 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4297 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004298 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004299 {
4300 llvm::raw_svector_ostream Out(Buffer);
4301 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004302 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004303 M != MEnd; ++M) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004304 using namespace llvm::support;
4305 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004306 StringRef FileName = (*M)->FileName;
Stephen Hines651f13c2014-04-23 16:59:28 -07004307 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004308 Out.write(FileName.data(), FileName.size());
Stephen Hines651f13c2014-04-23 16:59:28 -07004309 LE.write<uint32_t>((*M)->SLocEntryBaseOffset);
4310 LE.write<uint32_t>((*M)->BaseIdentifierID);
4311 LE.write<uint32_t>((*M)->BaseMacroID);
4312 LE.write<uint32_t>((*M)->BasePreprocessedEntityID);
4313 LE.write<uint32_t>((*M)->BaseSubmoduleID);
4314 LE.write<uint32_t>((*M)->BaseSelectorID);
4315 LE.write<uint32_t>((*M)->BaseDeclID);
4316 LE.write<uint32_t>((*M)->BaseTypeIndex);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004317 }
4318 }
4319 Record.clear();
4320 Record.push_back(MODULE_OFFSET_MAP);
4321 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4322 Buffer.data(), Buffer.size());
4323 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004324
4325 RecordData DeclUpdatesOffsetsRecord;
4326
4327 // Keep writing types, declarations, and declaration update records
4328 // until we've emitted all of them.
4329 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4330 WriteDeclsBlockAbbrevs();
4331 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4332 E = DeclsToRewrite.end();
4333 I != E; ++I)
4334 DeclTypesToEmit.push(const_cast<Decl*>(*I));
4335 do {
4336 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4337 while (!DeclTypesToEmit.empty()) {
4338 DeclOrType DOT = DeclTypesToEmit.front();
4339 DeclTypesToEmit.pop();
4340 if (DOT.isType())
4341 WriteType(DOT.getType());
4342 else
4343 WriteDecl(Context, DOT.getDecl());
4344 }
4345 } while (!DeclUpdates.empty());
4346 Stream.ExitBlock();
4347
4348 DoneWritingDeclsAndTypes = true;
4349
4350 // These things can only be done once we've written out decls and types.
4351 WriteTypeDeclOffsets();
4352 if (!DeclUpdatesOffsetsRecord.empty())
4353 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4354 WriteCXXBaseSpecifiersOffsets();
4355 WriteFileDeclIDsMap();
4356 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4357
4358 WriteComments();
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004359 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004360 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004361 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004362 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004363 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004364 WriteFPPragmaOptions(SemaRef.getFPOptions());
4365 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004366 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004367
Douglas Gregore209e502011-12-06 01:10:29 +00004368 // If we're emitting a module, write out the submodule information.
4369 if (WritingModule)
4370 WriteSubmodules(WritingModule);
4371
Douglas Gregora119da02011-08-02 16:26:37 +00004372 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4373
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004374 // Write the record containing external, unnamed definitions.
Stephen Hines651f13c2014-04-23 16:59:28 -07004375 if (!EagerlyDeserializedDecls.empty())
4376 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004377
4378 // Write the record containing tentative definitions.
4379 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004380 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004381
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004382 // Write the record containing unused file scoped decls.
4383 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004384 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004385
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004386 // Write the record containing weak undeclared identifiers.
4387 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004388 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004389 WeakUndeclaredIdentifiers);
4390
Richard Smith5ea6ef42013-01-10 23:43:47 +00004391 // Write the record containing locally-scoped extern "C" definitions.
4392 if (!LocallyScopedExternCDecls.empty())
4393 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4394 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004395
4396 // Write the record containing ext_vector type names.
4397 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004398 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004399
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004400 // Write the record containing VTable uses information.
4401 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004402 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004403
4404 // Write the record containing dynamic classes declarations.
4405 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004406 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004407
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004408 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004409 if (!PendingInstantiations.empty())
4410 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004411
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004412 // Write the record containing declaration references of Sema.
4413 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004414 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004415
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004416 // Write the record containing CUDA-specific declaration references.
4417 if (!CUDASpecialDeclRefs.empty())
4418 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004419
4420 // Write the delegating constructors.
4421 if (!DelegatingCtorDecls.empty())
4422 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004423
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004424 // Write the known namespaces.
4425 if (!KnownNamespaces.empty())
4426 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004427
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004428 // Write the undefined internal functions and variables, and inline functions.
4429 if (!UndefinedButUsed.empty())
4430 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004431
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004432 // Write the visible updates to DeclContexts.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004433 for (auto *DC : UpdatedDeclContexts)
4434 WriteDeclContextVisibleUpdate(DC);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004435
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004436 if (!WritingModule) {
4437 // Write the submodules that were imported, if any.
Stephen Hines651f13c2014-04-23 16:59:28 -07004438 struct ModuleInfo {
4439 uint64_t ID;
4440 Module *M;
4441 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4442 };
4443 llvm::SmallVector<ModuleInfo, 64> Imports;
4444 for (const auto *I : Context.local_imports()) {
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004445 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Stephen Hines651f13c2014-04-23 16:59:28 -07004446 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4447 I->getImportedModule()));
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004448 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004449
4450 if (!Imports.empty()) {
4451 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4452 return A.ID < B.ID;
4453 };
4454
4455 // Sort and deduplicate module IDs.
4456 std::sort(Imports.begin(), Imports.end(), Cmp);
4457 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4458 Imports.end());
4459
4460 RecordData ImportedModules;
4461 for (const auto &Import : Imports) {
4462 ImportedModules.push_back(Import.ID);
4463 // FIXME: If the module has macros imported then later has declarations
4464 // imported, this location won't be the right one as a location for the
4465 // declaration imports.
4466 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4467 }
4468
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004469 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4470 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004471 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004472
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004473 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004474 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004475 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004476 WriteObjCCategories();
Richard Smithac32d902013-08-07 21:41:30 +00004477 WriteLateParsedTemplates(SemaRef);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004478 if(!WritingModule)
4479 WriteOptimizePragmaOptions(SemaRef);
Richard Smithac32d902013-08-07 21:41:30 +00004480
Douglas Gregor3e1af842009-04-17 22:13:46 +00004481 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004482 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004483 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004484 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004485 Record.push_back(NumLexicalDeclContexts);
4486 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004487 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004488 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004489}
4490
Stephen Hines651f13c2014-04-23 16:59:28 -07004491void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004492 if (DeclUpdates.empty())
4493 return;
4494
Stephen Hines651f13c2014-04-23 16:59:28 -07004495 DeclUpdateMap LocalUpdates;
4496 LocalUpdates.swap(DeclUpdates);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004497
Stephen Hines651f13c2014-04-23 16:59:28 -07004498 for (auto &DeclUpdate : LocalUpdates) {
4499 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004500 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004501 continue; // The decl will be written completely,no need to store updates.
4502
Stephen Hines651f13c2014-04-23 16:59:28 -07004503 bool HasUpdatedBody = false;
4504 RecordData Record;
4505 for (auto &Update : DeclUpdate.second) {
4506 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4507
4508 Record.push_back(Kind);
4509 switch (Kind) {
4510 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4511 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4512 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004513 assert(Update.getDecl() && "no decl to add?");
Stephen Hines651f13c2014-04-23 16:59:28 -07004514 Record.push_back(GetDeclRef(Update.getDecl()));
4515 break;
4516
4517 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4518 AddSourceLocation(Update.getLoc(), Record);
4519 break;
4520
4521 case UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION:
4522 // An updated body is emitted last, so that the reader doesn't need
4523 // to skip over the lazy body to reach statements for other records.
4524 Record.pop_back();
4525 HasUpdatedBody = true;
4526 break;
4527
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004528 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4529 auto *RD = cast<CXXRecordDecl>(D);
4530 AddUpdatedDeclContext(RD->getPrimaryContext());
4531 AddCXXDefinitionData(RD, Record);
4532 Record.push_back(WriteDeclContextLexicalBlock(
4533 *Context, const_cast<CXXRecordDecl *>(RD)));
4534
4535 // This state is sometimes updated by template instantiation, when we
4536 // switch from the specialization referring to the template declaration
4537 // to it referring to the template definition.
4538 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4539 Record.push_back(MSInfo->getTemplateSpecializationKind());
4540 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4541 } else {
4542 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4543 Record.push_back(Spec->getTemplateSpecializationKind());
4544 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
4545
4546 // The instantiation might have been resolved to a partial
4547 // specialization. If so, record which one.
4548 auto From = Spec->getInstantiatedFrom();
4549 if (auto PartialSpec =
4550 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4551 Record.push_back(true);
4552 AddDeclRef(PartialSpec, Record);
4553 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4554 Record);
4555 } else {
4556 Record.push_back(false);
4557 }
4558 }
4559 Record.push_back(RD->getTagKind());
4560 AddSourceLocation(RD->getLocation(), Record);
4561 AddSourceLocation(RD->getLocStart(), Record);
4562 AddSourceLocation(RD->getRBraceLoc(), Record);
4563
4564 // Instantiation may change attributes; write them all out afresh.
4565 Record.push_back(D->hasAttrs());
4566 if (Record.back())
4567 WriteAttributes(ArrayRef<const Attr*>(D->getAttrs().begin(),
4568 D->getAttrs().size()), Record);
4569
4570 // FIXME: Ensure we don't get here for explicit instantiations.
4571 break;
4572 }
4573
Stephen Hines651f13c2014-04-23 16:59:28 -07004574 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4575 addExceptionSpec(
4576 *this,
4577 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4578 Record);
4579 break;
4580
4581 case UPD_CXX_DEDUCED_RETURN_TYPE:
4582 Record.push_back(GetOrCreateTypeID(Update.getType()));
4583 break;
4584
4585 case UPD_DECL_MARKED_USED:
4586 break;
4587
4588 case UPD_MANGLING_NUMBER:
4589 case UPD_STATIC_LOCAL_NUMBER:
4590 Record.push_back(Update.getNumber());
4591 break;
4592 }
4593 }
4594
4595 if (HasUpdatedBody) {
4596 const FunctionDecl *Def = cast<FunctionDecl>(D);
4597 Record.push_back(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION);
4598 Record.push_back(Def->isInlined());
4599 AddSourceLocation(Def->getInnerLocStart(), Record);
4600 AddFunctionDefinition(Def, Record);
4601 }
4602
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004603 OffsetsRecord.push_back(GetDeclRef(D));
4604 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4605
Stephen Hines651f13c2014-04-23 16:59:28 -07004606 Stream.EmitRecord(DECL_UPDATES, Record);
4607
4608 // Flush any statements that were written as part of this update record.
4609 FlushStmts();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004610
4611 // Flush C++ base specifiers, if there are any.
4612 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004613 }
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004614}
4615
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004616void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004617 if (ReplacedDecls.empty())
4618 return;
4619
4620 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004621 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4622 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004623 Record.push_back(I->ID);
4624 Record.push_back(I->Offset);
4625 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004626 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004627 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004628}
4629
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004630void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004631 Record.push_back(Loc.getRawEncoding());
4632}
4633
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004634void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004635 AddSourceLocation(Range.getBegin(), Record);
4636 AddSourceLocation(Range.getEnd(), Record);
4637}
4638
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004639void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004640 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004641 const uint64_t *Words = Value.getRawData();
4642 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004643}
4644
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004645void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004646 Record.push_back(Value.isUnsigned());
4647 AddAPInt(Value, Record);
4648}
4649
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004650void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004651 AddAPInt(Value.bitcastToAPInt(), Record);
4652}
4653
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004654void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004655 Record.push_back(getIdentifierRef(II));
4656}
4657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004658IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004659 if (!II)
Douglas Gregor2deaea32009-04-22 18:49:13 +00004660 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004661
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004662 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004663 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004664 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004665 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004666}
4667
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004668MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004669 // Don't emit builtin macros like __LINE__ to the AST file unless they
4670 // have been redefined by the header (in which case they are not
4671 // isBuiltinMacro).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004672 if (!MI || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004673 return 0;
4674
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004675 MacroID &ID = MacroIDs[MI];
4676 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004677 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004678 MacroInfoToEmitData Info = { Name, MI, ID };
4679 MacroInfosToEmit.push_back(Info);
4680 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004681 return ID;
4682}
4683
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004684MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004685 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004686 return 0;
4687
4688 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4689 return MacroIDs[MI];
4690}
4691
4692uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4693 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4694 return IdentMacroDirectivesOffsetMap[Name];
4695}
4696
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004697void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004698 Record.push_back(getSelectorRef(SelRef));
4699}
4700
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004701SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004702 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004703 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004704 }
4705
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004706 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004707 if (SID == 0 && Chain) {
4708 // This might trigger a ReadSelector callback, which will set the ID for
4709 // this selector.
4710 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004711 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004712 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004713 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004714 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004715 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004716 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004717 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004718}
4719
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004720void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004721 AddDeclRef(Temp->getDestructor(), Record);
4722}
4723
Douglas Gregor7c789c12010-10-29 22:39:52 +00004724void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4725 CXXBaseSpecifier const *BasesEnd,
4726 RecordDataImpl &Record) {
4727 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4728 CXXBaseSpecifiersToWrite.push_back(
4729 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4730 Bases, BasesEnd));
4731 Record.push_back(NextCXXBaseSpecifiersID++);
4732}
4733
Sebastian Redla4232eb2010-08-18 23:56:21 +00004734void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004735 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004736 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004737 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004738 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004739 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004740 break;
4741 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004742 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004743 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004744 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004745 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004746 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004747 break;
4748 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004749 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004750 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004751 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004752 break;
John McCall833ca992009-10-29 08:12:44 +00004753 case TemplateArgument::Null:
4754 case TemplateArgument::Integral:
4755 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004756 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004757 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004758 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004759 break;
4760 }
4761}
4762
Sebastian Redla4232eb2010-08-18 23:56:21 +00004763void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004764 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004765 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004766
4767 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4768 bool InfoHasSameExpr
4769 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4770 Record.push_back(InfoHasSameExpr);
4771 if (InfoHasSameExpr)
4772 return; // Avoid storing the same expr twice.
4773 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004774 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4775 Record);
4776}
4777
Douglas Gregordc355712011-02-25 00:36:19 +00004778void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4779 RecordDataImpl &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004780 if (!TInfo) {
John McCalla1ee0c52009-10-16 21:56:05 +00004781 AddTypeRef(QualType(), Record);
4782 return;
4783 }
4784
Douglas Gregordc355712011-02-25 00:36:19 +00004785 AddTypeLoc(TInfo->getTypeLoc(), Record);
4786}
4787
4788void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4789 AddTypeRef(TL.getType(), Record);
4790
John McCalla1ee0c52009-10-16 21:56:05 +00004791 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004792 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004793 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004794}
4795
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004796void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004797 Record.push_back(GetOrCreateTypeID(T));
4798}
4799
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004800TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004801 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004802 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004803 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4804}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004805
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004806TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004807 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004808 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004809 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004810}
4811
4812TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4813 if (T.isNull())
4814 return TypeIdx();
4815 assert(!T.getLocalFastQualifiers());
4816
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004817 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004818 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004819 if (DoneWritingDeclsAndTypes) {
4820 assert(0 && "New type seen after serializing all the types to emit!");
4821 return TypeIdx();
4822 }
4823
Douglas Gregor366809a2009-04-26 03:49:13 +00004824 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004825 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004826 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004827 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004828 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004829 return Idx;
4830}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004831
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004832TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004833 if (T.isNull())
4834 return TypeIdx();
4835 assert(!T.getLocalFastQualifiers());
4836
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004837 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4838 assert(I != TypeIdxs.end() && "Type not emitted!");
4839 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004840}
4841
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004842void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004843 Record.push_back(GetDeclRef(D));
4844}
4845
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004846DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004847 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004848
4849 if (!D) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004850 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004851 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004852
4853 // If D comes from an AST file, its declaration ID is already known and
4854 // fixed.
4855 if (D->isFromASTFile())
4856 return D->getGlobalID();
4857
Douglas Gregor97475832010-10-05 18:37:06 +00004858 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004859 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004860 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004861 if (DoneWritingDeclsAndTypes) {
4862 assert(0 && "New decl seen after serializing all the decls to emit!");
4863 return 0;
4864 }
4865
Douglas Gregor2cf26342009-04-09 22:27:44 +00004866 // We haven't seen this declaration before. Give it a new ID and
4867 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004868 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004869 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004870 }
4871
Sebastian Redl681d7232010-07-27 00:17:23 +00004872 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004873}
4874
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004875DeclID ASTWriter::getDeclID(const Decl *D) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004876 if (!D)
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004877 return 0;
4878
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004879 // If D comes from an AST file, its declaration ID is already known and
4880 // fixed.
4881 if (D->isFromASTFile())
4882 return D->getGlobalID();
4883
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004884 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4885 return DeclIDs[D];
4886}
4887
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004888void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004889 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004890 assert(D);
4891
4892 SourceLocation Loc = D->getLocation();
4893 if (Loc.isInvalid())
4894 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004895
4896 // We only keep track of the file-level declarations of each file.
4897 if (!D->getLexicalDeclContext()->isFileContext())
4898 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004899 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4900 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004901 if (isa<ParmVarDecl>(D))
4902 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004903
4904 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004905 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004906 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004907 FileID FID;
4908 unsigned Offset;
Stephen Hines651f13c2014-04-23 16:59:28 -07004909 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004910 if (FID.isInvalid())
4911 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004912 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004913
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004914 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004915 if (!Info)
4916 Info = new DeclIDInFileInfo();
4917
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004918 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004919 LocDeclIDsTy &Decls = Info->DeclIDs;
4920
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004921 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004922 Decls.push_back(LocDecl);
4923 return;
4924 }
4925
Benjamin Kramer809d2542013-08-24 13:22:59 +00004926 LocDeclIDsTy::iterator I =
4927 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004928
4929 Decls.insert(I, LocDecl);
4930}
4931
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004932void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004933 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004934 Record.push_back(Name.getNameKind());
4935 switch (Name.getNameKind()) {
4936 case DeclarationName::Identifier:
4937 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4938 break;
4939
4940 case DeclarationName::ObjCZeroArgSelector:
4941 case DeclarationName::ObjCOneArgSelector:
4942 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004943 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004944 break;
4945
4946 case DeclarationName::CXXConstructorName:
4947 case DeclarationName::CXXDestructorName:
4948 case DeclarationName::CXXConversionFunctionName:
4949 AddTypeRef(Name.getCXXNameType(), Record);
4950 break;
4951
4952 case DeclarationName::CXXOperatorName:
4953 Record.push_back(Name.getCXXOverloadedOperator());
4954 break;
4955
Sean Hunt3e518bd2009-11-29 07:34:05 +00004956 case DeclarationName::CXXLiteralOperatorName:
4957 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4958 break;
4959
Douglas Gregor2cf26342009-04-09 22:27:44 +00004960 case DeclarationName::CXXUsingDirective:
4961 // No extra data to emit
4962 break;
4963 }
4964}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004965
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004966void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004967 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004968 switch (Name.getNameKind()) {
4969 case DeclarationName::CXXConstructorName:
4970 case DeclarationName::CXXDestructorName:
4971 case DeclarationName::CXXConversionFunctionName:
4972 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4973 break;
4974
4975 case DeclarationName::CXXOperatorName:
4976 AddSourceLocation(
4977 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4978 Record);
4979 AddSourceLocation(
4980 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4981 Record);
4982 break;
4983
4984 case DeclarationName::CXXLiteralOperatorName:
4985 AddSourceLocation(
4986 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4987 Record);
4988 break;
4989
4990 case DeclarationName::Identifier:
4991 case DeclarationName::ObjCZeroArgSelector:
4992 case DeclarationName::ObjCOneArgSelector:
4993 case DeclarationName::ObjCMultiArgSelector:
4994 case DeclarationName::CXXUsingDirective:
4995 break;
4996 }
4997}
4998
4999void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005000 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005001 AddDeclarationName(NameInfo.getName(), Record);
5002 AddSourceLocation(NameInfo.getLoc(), Record);
5003 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5004}
5005
5006void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005007 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005008 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005009 Record.push_back(Info.NumTemplParamLists);
5010 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5011 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5012}
5013
Sebastian Redla4232eb2010-08-18 23:56:21 +00005014void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005015 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005016 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00005017 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005018 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005019
5020 // Push each of the NNS's onto a stack for serialization in reverse order.
5021 while (NNS) {
5022 NestedNames.push_back(NNS);
5023 NNS = NNS->getPrefix();
5024 }
5025
5026 Record.push_back(NestedNames.size());
5027 while(!NestedNames.empty()) {
5028 NNS = NestedNames.pop_back_val();
5029 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5030 Record.push_back(Kind);
5031 switch (Kind) {
5032 case NestedNameSpecifier::Identifier:
5033 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5034 break;
5035
5036 case NestedNameSpecifier::Namespace:
5037 AddDeclRef(NNS->getAsNamespace(), Record);
5038 break;
5039
Douglas Gregor14aba762011-02-24 02:36:08 +00005040 case NestedNameSpecifier::NamespaceAlias:
5041 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5042 break;
5043
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005044 case NestedNameSpecifier::TypeSpec:
5045 case NestedNameSpecifier::TypeSpecWithTemplate:
5046 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5047 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5048 break;
5049
5050 case NestedNameSpecifier::Global:
5051 // Don't need to write an associated value.
5052 break;
5053 }
5054 }
5055}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005056
Douglas Gregordc355712011-02-25 00:36:19 +00005057void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5058 RecordDataImpl &Record) {
5059 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00005060 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005061 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00005062
5063 // Push each of the nested-name-specifiers's onto a stack for
5064 // serialization in reverse order.
5065 while (NNS) {
5066 NestedNames.push_back(NNS);
5067 NNS = NNS.getPrefix();
5068 }
5069
5070 Record.push_back(NestedNames.size());
5071 while(!NestedNames.empty()) {
5072 NNS = NestedNames.pop_back_val();
5073 NestedNameSpecifier::SpecifierKind Kind
5074 = NNS.getNestedNameSpecifier()->getKind();
5075 Record.push_back(Kind);
5076 switch (Kind) {
5077 case NestedNameSpecifier::Identifier:
5078 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5079 AddSourceRange(NNS.getLocalSourceRange(), Record);
5080 break;
5081
5082 case NestedNameSpecifier::Namespace:
5083 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5084 AddSourceRange(NNS.getLocalSourceRange(), Record);
5085 break;
5086
5087 case NestedNameSpecifier::NamespaceAlias:
5088 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5089 AddSourceRange(NNS.getLocalSourceRange(), Record);
5090 break;
5091
5092 case NestedNameSpecifier::TypeSpec:
5093 case NestedNameSpecifier::TypeSpecWithTemplate:
5094 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5095 AddTypeLoc(NNS.getTypeLoc(), Record);
5096 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5097 break;
5098
5099 case NestedNameSpecifier::Global:
5100 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5101 break;
5102 }
5103 }
5104}
5105
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005106void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005107 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005108 Record.push_back(Kind);
5109 switch (Kind) {
5110 case TemplateName::Template:
5111 AddDeclRef(Name.getAsTemplateDecl(), Record);
5112 break;
5113
5114 case TemplateName::OverloadedTemplate: {
5115 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5116 Record.push_back(OvT->size());
5117 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5118 I != E; ++I)
5119 AddDeclRef(*I, Record);
5120 break;
5121 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005122
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005123 case TemplateName::QualifiedTemplate: {
5124 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5125 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5126 Record.push_back(QualT->hasTemplateKeyword());
5127 AddDeclRef(QualT->getTemplateDecl(), Record);
5128 break;
5129 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005130
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005131 case TemplateName::DependentTemplate: {
5132 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5133 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5134 Record.push_back(DepT->isIdentifier());
5135 if (DepT->isIdentifier())
5136 AddIdentifierRef(DepT->getIdentifier(), Record);
5137 else
5138 Record.push_back(DepT->getOperator());
5139 break;
5140 }
John McCall14606042011-06-30 08:33:18 +00005141
5142 case TemplateName::SubstTemplateTemplateParm: {
5143 SubstTemplateTemplateParmStorage *subst
5144 = Name.getAsSubstTemplateTemplateParm();
5145 AddDeclRef(subst->getParameter(), Record);
5146 AddTemplateName(subst->getReplacement(), Record);
5147 break;
5148 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005149
5150 case TemplateName::SubstTemplateTemplateParmPack: {
5151 SubstTemplateTemplateParmPackStorage *SubstPack
5152 = Name.getAsSubstTemplateTemplateParmPack();
5153 AddDeclRef(SubstPack->getParameterPack(), Record);
5154 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5155 break;
5156 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005157 }
5158}
5159
Michael J. Spencer20249a12010-10-21 03:16:25 +00005160void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005161 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005162 Record.push_back(Arg.getKind());
5163 switch (Arg.getKind()) {
5164 case TemplateArgument::Null:
5165 break;
5166 case TemplateArgument::Type:
5167 AddTypeRef(Arg.getAsType(), Record);
5168 break;
5169 case TemplateArgument::Declaration:
5170 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00005171 Record.push_back(Arg.isDeclForReferenceParam());
5172 break;
5173 case TemplateArgument::NullPtr:
5174 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005175 break;
5176 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00005177 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005178 AddTypeRef(Arg.getIntegralType(), Record);
5179 break;
5180 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00005181 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5182 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00005183 case TemplateArgument::TemplateExpansion:
5184 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00005185 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00005186 Record.push_back(*NumExpansions + 1);
5187 else
5188 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005189 break;
5190 case TemplateArgument::Expression:
5191 AddStmt(Arg.getAsExpr());
5192 break;
5193 case TemplateArgument::Pack:
5194 Record.push_back(Arg.pack_size());
5195 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
5196 I != E; ++I)
5197 AddTemplateArgument(*I, Record);
5198 break;
5199 }
5200}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005201
5202void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005203ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005204 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005205 assert(TemplateParams && "No TemplateParams!");
5206 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5207 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5208 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5209 Record.push_back(TemplateParams->size());
5210 for (TemplateParameterList::const_iterator
5211 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5212 P != PEnd; ++P)
5213 AddDeclRef(*P, Record);
5214}
5215
5216/// \brief Emit a template argument list.
5217void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005218ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005219 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005220 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00005221 Record.push_back(TemplateArgs->size());
5222 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005223 AddTemplateArgument(TemplateArgs->get(i), Record);
5224}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005225
Enea Zaffanellac1cef082013-08-10 07:24:53 +00005226void
5227ASTWriter::AddASTTemplateArgumentListInfo
5228(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5229 assert(ASTTemplArgList && "No ASTTemplArgList!");
5230 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5231 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5232 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5233 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5234 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5235 AddTemplateArgumentLoc(TemplArgs[i], Record);
5236}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005237
5238void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005239ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005240 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005241 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005242 I = Set.begin(), E = Set.end(); I != E; ++I) {
5243 AddDeclRef(I.getDecl(), Record);
5244 Record.push_back(I.getAccess());
5245 }
5246}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005247
Sebastian Redla4232eb2010-08-18 23:56:21 +00005248void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005249 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005250 Record.push_back(Base.isVirtual());
5251 Record.push_back(Base.isBaseOfClass());
5252 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00005253 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00005254 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005255 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005256 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5257 : SourceLocation(),
5258 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005259}
Sebastian Redl30c514c2010-07-14 23:45:08 +00005260
Douglas Gregor7c789c12010-10-29 22:39:52 +00005261void ASTWriter::FlushCXXBaseSpecifiers() {
5262 RecordData Record;
5263 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5264 Record.clear();
5265
5266 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00005267 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005268 if (Index == CXXBaseSpecifiersOffsets.size())
5269 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5270 else {
5271 if (Index > CXXBaseSpecifiersOffsets.size())
5272 CXXBaseSpecifiersOffsets.resize(Index + 1);
5273 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5274 }
5275
5276 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5277 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5278 Record.push_back(BEnd - B);
5279 for (; B != BEnd; ++B)
5280 AddCXXBaseSpecifier(*B, Record);
5281 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00005282
5283 // Flush any expressions that were written as part of the base specifiers.
5284 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00005285 }
5286
5287 CXXBaseSpecifiersToWrite.clear();
5288}
5289
Sean Huntcbb67482011-01-08 20:30:50 +00005290void ASTWriter::AddCXXCtorInitializers(
5291 const CXXCtorInitializer * const *CtorInitializers,
5292 unsigned NumCtorInitializers,
5293 RecordDataImpl &Record) {
5294 Record.push_back(NumCtorInitializers);
5295 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5296 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005297
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005298 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00005299 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00005300 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005301 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00005302 } else if (Init->isDelegatingInitializer()) {
5303 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00005304 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00005305 } else if (Init->isMemberInitializer()){
5306 Record.push_back(CTOR_INITIALIZER_MEMBER);
5307 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005308 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00005309 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5310 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005311 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00005312
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005313 AddSourceLocation(Init->getMemberLocation(), Record);
5314 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005315 AddSourceLocation(Init->getLParenLoc(), Record);
5316 AddSourceLocation(Init->getRParenLoc(), Record);
5317 Record.push_back(Init->isWritten());
5318 if (Init->isWritten()) {
5319 Record.push_back(Init->getSourceOrder());
5320 } else {
5321 Record.push_back(Init->getNumArrayIndices());
5322 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5323 AddDeclRef(Init->getArrayIndex(i), Record);
5324 }
5325 }
5326}
5327
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005328void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005329 auto &Data = D->data();
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005330 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005331 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005332 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005333 Record.push_back(Data.Aggregate);
5334 Record.push_back(Data.PlainOldData);
5335 Record.push_back(Data.Empty);
5336 Record.push_back(Data.Polymorphic);
5337 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005338 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005339 Record.push_back(Data.HasNoNonEmptyBases);
5340 Record.push_back(Data.HasPrivateFields);
5341 Record.push_back(Data.HasProtectedFields);
5342 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005343 Record.push_back(Data.HasMutableFields);
Stephen Hines651f13c2014-04-23 16:59:28 -07005344 Record.push_back(Data.HasVariantMembers);
Richard Smithdfefb842012-02-25 07:33:38 +00005345 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005346 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005347 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005348 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5349 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5350 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5351 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5352 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5353 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005354 Record.push_back(Data.HasTrivialSpecialMembers);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005355 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005356 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005357 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005358 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005359 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005360 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005361 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005362 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005363 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005364 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5365 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5366 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5367 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smithdfefb842012-02-25 07:33:38 +00005368 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005369
5370 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005371 if (Data.NumBases > 0)
5372 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5373 Record);
5374
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005375 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5376 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005377 if (Data.NumVBases > 0)
5378 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5379 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005380
Richard Smithc2d77572013-08-30 04:46:40 +00005381 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5382 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005383 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005384 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005385
5386 // Add lambda-specific data.
5387 if (Data.IsLambda) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005388 auto &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005389 Record.push_back(Lambda.Dependent);
Faisal Valibef582b2013-10-23 16:10:50 +00005390 Record.push_back(Lambda.IsGenericLambda);
5391 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005392 Record.push_back(Lambda.NumCaptures);
5393 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005394 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005395 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005396 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005397 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005398 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005399 AddSourceLocation(Capture.getLocation(), Record);
5400 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005401 Record.push_back(Capture.getCaptureKind());
5402 switch (Capture.getCaptureKind()) {
5403 case LCK_This:
5404 break;
5405 case LCK_ByCopy:
Richard Smith04fa7a32013-09-28 04:02:39 +00005406 case LCK_ByRef:
Richard Smith0d8e9642013-05-16 06:20:58 +00005407 VarDecl *Var =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005408 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith0d8e9642013-05-16 06:20:58 +00005409 AddDeclRef(Var, Record);
5410 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5411 : SourceLocation(),
5412 Record);
5413 break;
5414 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005415 }
5416 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005417}
5418
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005419void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005420 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005421 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005422 assert(FirstDeclID == NextDeclID &&
5423 FirstTypeID == NextTypeID &&
5424 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005425 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005426 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005427 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005428 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005429
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005430 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005431
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005432 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5433 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5434 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005435 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005436 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005437 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005438 NextDeclID = FirstDeclID;
5439 NextTypeID = FirstTypeID;
5440 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005441 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005442 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005443 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005444}
5445
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005446void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005447 // Always keep the highest ID. See \p TypeRead() for more information.
5448 IdentID &StoredID = IdentifierIDs[II];
5449 if (ID > StoredID)
5450 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005451}
5452
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005453void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005454 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005455 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005456 if (ID > StoredID)
5457 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005458}
5459
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005460void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005461 // Always take the highest-numbered type index. This copes with an interesting
5462 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005463 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005464 // keep the higher-numbered entry so that we can properly write it out to
5465 // the AST file.
5466 TypeIdx &StoredIdx = TypeIdxs[T];
5467 if (Idx.getIndex() >= StoredIdx.getIndex())
5468 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005469}
5470
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005471void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005472 // Always keep the highest ID. See \p TypeRead() for more information.
5473 SelectorID &StoredID = SelectorIDs[S];
5474 if (ID > StoredID)
5475 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005476}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005477
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005478void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005479 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005480 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005481 MacroDefinitions[MD] = ID;
5482}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005483
Douglas Gregora015cab2011-12-02 17:30:13 +00005484void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5485 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5486 SubmoduleIDs[Mod] = ID;
5487}
5488
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005489void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005490 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005491 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005492 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5493 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005494 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005495 // A forward reference was mutated into a definition. Rewrite it.
5496 // FIXME: This happens during template instantiation, should we
5497 // have created a new definition decl instead ?
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005498 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5499 "completed a tag from another module but not by instantiation?");
5500 DeclUpdates[RD].push_back(
5501 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005502 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005503 }
5504}
Douglas Gregora8235d62012-10-09 23:05:51 +00005505
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005506void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005507 assert(!WritingAST && "Already writing the AST!");
5508
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005509 // TU and namespaces are handled elsewhere.
5510 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5511 return;
5512
Douglas Gregor919814d2011-09-09 23:01:35 +00005513 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005514 return; // Not a source decl added to a DeclContext from PCH.
5515
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005516 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005517 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005518 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005519}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005520
5521void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005522 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005523 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005524 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005525 return; // Not a source member added to a class from PCH.
5526 if (!isa<CXXMethodDecl>(D))
5527 return; // We are interested in lazily declared implicit methods.
5528
5529 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005530 assert(RD->isCompleteDefinition());
Stephen Hines651f13c2014-04-23 16:59:28 -07005531 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005532}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005533
5534void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5535 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005536 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005537 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005538 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005539 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005540 return; // Not a source specialization added to a template from PCH.
5541
Stephen Hines651f13c2014-04-23 16:59:28 -07005542 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5543 D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005544}
Douglas Gregor89d99802010-11-30 06:16:57 +00005545
Larisse Voufoef4579c2013-08-06 01:03:05 +00005546void ASTWriter::AddedCXXTemplateSpecialization(
5547 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5548 // The specializations set is kept in the canonical template.
5549 assert(!WritingAST && "Already writing the AST!");
5550 TD = TD->getCanonicalDecl();
5551 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5552 return; // Not a source specialization added to a template from PCH.
5553
Stephen Hines651f13c2014-04-23 16:59:28 -07005554 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5555 D));
Larisse Voufoef4579c2013-08-06 01:03:05 +00005556}
5557
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005558void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5559 const FunctionDecl *D) {
5560 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005561 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005562 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005563 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005564 return; // Not a source specialization added to a template from PCH.
5565
Stephen Hines651f13c2014-04-23 16:59:28 -07005566 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5567 D));
5568}
5569
5570void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5571 assert(!WritingAST && "Already writing the AST!");
5572 FD = FD->getCanonicalDecl();
5573 if (!FD->isFromASTFile())
5574 return; // Not a function declared in PCH and defined outside.
5575
5576 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005577}
5578
Richard Smith9dadfab2013-05-11 05:45:24 +00005579void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5580 assert(!WritingAST && "Already writing the AST!");
5581 FD = FD->getCanonicalDecl();
5582 if (!FD->isFromASTFile())
5583 return; // Not a function declared in PCH and defined outside.
5584
Stephen Hines651f13c2014-04-23 16:59:28 -07005585 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith9dadfab2013-05-11 05:45:24 +00005586}
5587
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005588void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005589 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005590 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005591 return; // Declaration not imported from PCH.
5592
5593 // Implicit decl from a PCH was defined.
5594 // FIXME: Should implicit definition be a separate FunctionDecl?
5595 RewriteDecl(D);
5596}
5597
Stephen Hines651f13c2014-04-23 16:59:28 -07005598void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5599 assert(!WritingAST && "Already writing the AST!");
5600 if (!D->isFromASTFile())
5601 return;
5602
5603 // Since the actual instantiation is delayed, this really means that we need
5604 // to update the instantiation location.
5605 DeclUpdates[D].push_back(
5606 DeclUpdate(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION));
5607}
5608
Sebastian Redlf79a7192011-04-29 08:19:30 +00005609void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005610 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005611 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005612 return;
5613
5614 // Since the actual instantiation is delayed, this really means that we need
5615 // to update the instantiation location.
Stephen Hines651f13c2014-04-23 16:59:28 -07005616 DeclUpdates[D].push_back(
5617 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5618 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redlf79a7192011-04-29 08:19:30 +00005619}
5620
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005621void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5622 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005623 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005624 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005625 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005626
5627 assert(IFD->getDefinition() && "Category on a class without a definition?");
5628 ObjCClassesWithCategories.insert(
5629 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005630}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005631
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005632
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005633void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5634 const ObjCPropertyDecl *OrigProp,
5635 const ObjCCategoryDecl *ClassExt) {
5636 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5637 if (!D)
5638 return;
5639
5640 assert(!WritingAST && "Already writing the AST!");
5641 if (!D->isFromASTFile())
5642 return; // Declaration not imported from PCH.
5643
5644 RewriteDecl(D);
5645}
Eli Friedman86164e82013-09-05 00:02:25 +00005646
5647void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5648 assert(!WritingAST && "Already writing the AST!");
5649 if (!D->isFromASTFile())
5650 return;
5651
Stephen Hines651f13c2014-04-23 16:59:28 -07005652 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman86164e82013-09-05 00:02:25 +00005653}