blob: e9e64c2a66f82fe0ab551921d6efeadf202fd39f [file] [log] [blame]
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001//===--- ASTWriter.cpp - AST File Writer ------------------------*- C++ -*-===//
Douglas Gregoref84c4b2009-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 Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Richard Smithd88a7f12015-09-01 20:35:42 +000016#include "ASTReaderInternals.h"
17#include "MultiOnDiskHashTable.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000019#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000020#include "clang/AST/Decl.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000021#include "clang/AST/DeclCXX.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000022#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000025#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000026#include "clang/AST/ExprCXX.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000027#include "clang/AST/LambdaCapture.h"
28#include "clang/AST/NestedNameSpecifier.h"
29#include "clang/AST/RawCommentList.h"
30#include "clang/AST/TemplateName.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000031#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000032#include "clang/AST/TypeLocVisitor.h"
Benjamin Kramerf3ca26982014-05-10 16:31:55 +000033#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000035#include "clang/Basic/FileSystemOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000036#include "clang/Basic/LLVM.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000037#include "clang/Basic/LangOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000038#include "clang/Basic/Module.h"
39#include "clang/Basic/ObjCRuntime.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000040#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000041#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000042#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000043#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000044#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000045#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000046#include "clang/Lex/HeaderSearch.h"
47#include "clang/Lex/HeaderSearchOptions.h"
48#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000049#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000050#include "clang/Lex/PreprocessingRecord.h"
51#include "clang/Lex/Preprocessor.h"
52#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000053#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000054#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000055#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000056#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000057#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000058#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000059#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000060#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000061#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000062#include "llvm/ADT/APFloat.h"
63#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000064#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000065#include "llvm/ADT/IntrusiveRefCntPtr.h"
66#include "llvm/ADT/Optional.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000067#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000068#include "llvm/ADT/SmallSet.h"
69#include "llvm/ADT/SmallString.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000070#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000071#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000073#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000074#include "llvm/Support/Compression.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000075#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000076#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000077#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000078#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000079#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000080#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000081#include "llvm/Support/Process.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000082#include "llvm/Support/SHA1.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000083#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000084#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000085#include <cassert>
86#include <cstdint>
87#include <cstdlib>
88#include <cstring>
89#include <deque>
90#include <limits>
91#include <new>
92#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000093#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000094
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000096using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097
Sebastian Redl3df5a082010-07-30 17:03:48 +000098template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +000099static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000100 if (v.empty()) return StringRef();
101 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000102 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000103}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000104
105template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000106static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000107 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000108 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000109}
110
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111//===----------------------------------------------------------------------===//
112// Type serialization
113//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000114
Richard Smith290d8012016-04-06 17:06:00 +0000115namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000117 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000118 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000119 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000122 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000123 /// \brief Abbreviation to use for the record, if any.
124 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125
Richard Smith290d8012016-04-06 17:06:00 +0000126 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000127 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000128 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
129
130 uint64_t Emit() {
131 return Record.Emit(Code, AbbrevToUse);
132 }
133
134 void Visit(QualType T) {
135 if (T.hasLocalNonFastQualifiers()) {
136 Qualifiers Qs = T.getLocalQualifiers();
137 Record.AddTypeRef(T.getLocalUnqualifiedType());
138 Record.push_back(Qs.getAsOpaqueValue());
139 Code = TYPE_EXT_QUAL;
140 AbbrevToUse = Writer.TypeExtQualAbbrev;
141 } else {
142 switch (T->getTypeClass()) {
143 // For all of the concrete, non-dependent types, call the
144 // appropriate visitor function.
145#define TYPE(Class, Base) \
146 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
147#define ABSTRACT_TYPE(Class, Base)
148#include "clang/AST/TypeNodes.def"
149 }
150 }
151 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152
153 void VisitArrayType(const ArrayType *T);
154 void VisitFunctionType(const FunctionType *T);
155 void VisitTagType(const TagType *T);
156
157#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
158#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159#include "clang/AST/TypeNodes.def"
160 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000161
Richard Smith290d8012016-04-06 17:06:00 +0000162} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000165 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000169 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000170 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171}
172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000173void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000174 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000175 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176}
177
Reid Kleckner8a365022013-06-24 17:51:48 +0000178void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000179 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000180 Code = TYPE_DECAYED;
181}
182
Reid Kleckner0503a872013-12-05 01:23:43 +0000183void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000184 Record.AddTypeRef(T->getOriginalType());
185 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000186 Code = TYPE_ADJUSTED;
187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000190 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000192}
193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000195 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000196 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000197 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198}
199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000201 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000202 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203}
204
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000206 Record.AddTypeRef(T->getPointeeType());
207 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000212 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000214 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215}
216
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000219 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000220 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000221}
222
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000225 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226}
227
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000230 Record.AddSourceLocation(T->getLBracketLoc());
231 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000232 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000233 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234}
235
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000236void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000237 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000238 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000239 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000240 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246}
247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000249 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000250 FunctionType::ExtInfo C = T->getExtInfo();
251 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000252 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000253 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000254 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000255 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000256 Record.push_back(C.getProducesResult());
Richard Smith01b2cb42014-07-26 06:37:51 +0000257
258 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
259 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260}
261
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000264 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000265}
266
Richard Smith290d8012016-04-06 17:06:00 +0000267static void addExceptionSpec(const FunctionProtoType *T,
268 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000269 Record.push_back(T->getExceptionSpecType());
270 if (T->getExceptionSpecType() == EST_Dynamic) {
271 Record.push_back(T->getNumExceptions());
272 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000273 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000274 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000275 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000276 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000277 Record.AddDeclRef(T->getExceptionSpecDecl());
278 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000279 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000280 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000281 }
Richard Smith564417a2014-03-20 21:47:22 +0000282}
283
284void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
285 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000286
Richard Smith564417a2014-03-20 21:47:22 +0000287 Record.push_back(T->isVariadic());
288 Record.push_back(T->hasTrailingReturn());
289 Record.push_back(T->getTypeQuals());
290 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000291 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000292
293 Record.push_back(T->getNumParams());
294 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000295 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000296
John McCall18afab72016-03-01 00:49:02 +0000297 if (T->hasExtParameterInfos()) {
298 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
299 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
300 }
301
Richard Smith01b2cb42014-07-26 06:37:51 +0000302 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000303 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
304 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000305 AbbrevToUse = 0;
306
Sebastian Redl539c5062010-08-18 23:57:32 +0000307 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000308}
309
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000310void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000311 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000312 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000313}
John McCallb96ec562009-12-04 22:46:56 +0000314
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000315void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000316 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000317 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000318 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000319 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000320}
321
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000323 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000324 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000325}
326
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000327void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000328 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000329 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000330}
331
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000332void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000333 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000334 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000335 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000336}
337
Alexis Hunte852b102011-05-24 22:41:36 +0000338void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000339 Record.AddTypeRef(T->getBaseType());
340 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000341 Record.push_back(T->getUTTKind());
342 Code = TYPE_UNARY_TRANSFORM;
343}
344
Richard Smith30482bc2011-02-20 03:19:35 +0000345void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000346 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000347 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000348 if (T->getDeducedType().isNull())
349 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000350 Code = TYPE_AUTO;
351}
352
Richard Smith600b5262017-01-26 20:40:47 +0000353void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
354 const DeducedTemplateSpecializationType *T) {
355 Record.AddTemplateName(T->getTemplateName());
356 Record.AddTypeRef(T->getDeducedType());
357 if (T->getDeducedType().isNull())
358 Record.push_back(T->isDependentType());
359 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
360}
361
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000362void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000363 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000364 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000365 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000366 "Cannot serialize in the middle of a type definition");
367}
368
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000370 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000371 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000372}
373
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000374void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000375 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000376 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000377}
378
John McCall81904512011-01-06 01:58:22 +0000379void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000380 Record.AddTypeRef(T->getModifiedType());
381 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000382 Record.push_back(T->getAttrKind());
383 Code = TYPE_ATTRIBUTED;
384}
385
Mike Stump11289f42009-09-09 15:08:12 +0000386void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000387ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000388 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000389 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
390 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000391 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000392}
393
394void
Douglas Gregorada4b792011-01-14 02:55:32 +0000395ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
396 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000397 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
398 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000399 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
400}
401
402void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000403ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000404 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000405 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000406 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000407 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000408 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000409 Record.AddTemplateArgument(ArgI);
410 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
411 : T->isCanonicalUnqualified()
412 ? QualType()
413 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000414 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000415}
416
417void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000418ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000419 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000420 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000421 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000422 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000423}
424
425void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000426ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000427 const DependentSizedExtVectorType *T) {
428 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000429 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000430}
431
432void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000433ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000434 Record.push_back(T->getDepth());
435 Record.push_back(T->getIndex());
436 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000437 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000438 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000439}
440
441void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000442ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000443 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000444 Record.AddNestedNameSpecifier(T->getQualifier());
445 Record.AddIdentifierRef(T->getIdentifier());
446 Record.AddTypeRef(
447 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000448 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000449}
450
451void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000452ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000453 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000454 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000455 Record.AddNestedNameSpecifier(T->getQualifier());
456 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000457 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000458 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000459 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000460 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000461}
462
Douglas Gregord2fa7662010-12-20 02:24:11 +0000463void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000464 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000465 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000466 Record.push_back(*NumExpansions + 1);
467 else
468 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000469 Code = TYPE_PACK_EXPANSION;
470}
471
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000472void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000473 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000474 Code = TYPE_PAREN;
475}
476
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000477void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000478 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000479 Record.AddNestedNameSpecifier(T->getQualifier());
480 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000481 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000482}
483
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000484void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000485 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
486 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000487 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000488}
489
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000490void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000491 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000492 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000493}
494
Manman Rene6be26c2016-09-13 17:25:08 +0000495void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
496 Record.AddDeclRef(T->getDecl());
497 Record.push_back(T->getNumProtocols());
498 for (const auto *I : T->quals())
499 Record.AddDeclRef(I);
500 Code = TYPE_OBJC_TYPE_PARAM;
501}
502
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000503void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000504 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000505 Record.push_back(T->getTypeArgsAsWritten().size());
506 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000507 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000508 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000509 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000510 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000511 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000512 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000513}
514
Steve Narofffb4330f2009-06-17 22:40:22 +0000515void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000516ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000517 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000518 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000519}
520
Eli Friedman0dfb8892011-10-06 23:00:33 +0000521void
522ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000523 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000524 Code = TYPE_ATOMIC;
525}
526
Xiuli Pan9c14e282016-01-09 12:53:17 +0000527void
528ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000529 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000530 Record.push_back(T->isReadOnly());
531 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000532}
533
John McCall8f115c62009-10-16 21:56:05 +0000534namespace {
535
536class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000537 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000538
539public:
Richard Smith290d8012016-04-06 17:06:00 +0000540 TypeLocWriter(ASTRecordWriter &Record)
541 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000542
John McCall17001972009-10-18 01:05:36 +0000543#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000544#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000545 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000546#include "clang/AST/TypeLocNodes.def"
547
John McCall17001972009-10-18 01:05:36 +0000548 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
549 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000550};
551
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000552} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000553
John McCall17001972009-10-18 01:05:36 +0000554void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
555 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000556}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000557
John McCall17001972009-10-18 01:05:36 +0000558void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000559 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000560 if (TL.needsExtraLocalData()) {
561 Record.push_back(TL.getWrittenTypeSpec());
562 Record.push_back(TL.getWrittenSignSpec());
563 Record.push_back(TL.getWrittenWidthSpec());
564 Record.push_back(TL.hasModeAttr());
565 }
John McCall8f115c62009-10-16 21:56:05 +0000566}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000567
John McCall17001972009-10-18 01:05:36 +0000568void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000569 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000570}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000571
John McCall17001972009-10-18 01:05:36 +0000572void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000573 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000574}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000575
Reid Kleckner8a365022013-06-24 17:51:48 +0000576void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
577 // nothing to do
578}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000579
Reid Kleckner0503a872013-12-05 01:23:43 +0000580void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
581 // nothing to do
582}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000583
John McCall17001972009-10-18 01:05:36 +0000584void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000585 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000586}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000587
John McCall17001972009-10-18 01:05:36 +0000588void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000589 Record.AddSourceLocation(TL.getAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000590}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000591
John McCall17001972009-10-18 01:05:36 +0000592void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000593 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000594}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000595
John McCall17001972009-10-18 01:05:36 +0000596void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000597 Record.AddSourceLocation(TL.getStarLoc());
598 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000599}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000600
John McCall17001972009-10-18 01:05:36 +0000601void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000602 Record.AddSourceLocation(TL.getLBracketLoc());
603 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000604 Record.push_back(TL.getSizeExpr() ? 1 : 0);
605 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000606 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000607}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000608
John McCall17001972009-10-18 01:05:36 +0000609void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
610 VisitArrayTypeLoc(TL);
611}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000612
John McCall17001972009-10-18 01:05:36 +0000613void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
614 VisitArrayTypeLoc(TL);
615}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000616
John McCall17001972009-10-18 01:05:36 +0000617void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
618 VisitArrayTypeLoc(TL);
619}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000620
John McCall17001972009-10-18 01:05:36 +0000621void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
622 DependentSizedArrayTypeLoc TL) {
623 VisitArrayTypeLoc(TL);
624}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000625
John McCall17001972009-10-18 01:05:36 +0000626void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
627 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000628 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000629}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000630
John McCall17001972009-10-18 01:05:36 +0000631void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000632 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000633}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000634
John McCall17001972009-10-18 01:05:36 +0000635void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000636 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000637}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000638
John McCall17001972009-10-18 01:05:36 +0000639void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000640 Record.AddSourceLocation(TL.getLocalRangeBegin());
641 Record.AddSourceLocation(TL.getLParenLoc());
642 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000643 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000644 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000645 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000646 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000647}
648void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
649 VisitFunctionTypeLoc(TL);
650}
651void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
652 VisitFunctionTypeLoc(TL);
653}
John McCallb96ec562009-12-04 22:46:56 +0000654void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000655 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000656}
John McCall17001972009-10-18 01:05:36 +0000657void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000658 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000659}
Manman Rene6be26c2016-09-13 17:25:08 +0000660void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
661 if (TL.getNumProtocols()) {
662 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
663 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
664 }
665 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
666 Record.AddSourceLocation(TL.getProtocolLoc(i));
667}
John McCall17001972009-10-18 01:05:36 +0000668void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000669 Record.AddSourceLocation(TL.getTypeofLoc());
670 Record.AddSourceLocation(TL.getLParenLoc());
671 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000672}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000673
John McCall17001972009-10-18 01:05:36 +0000674void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000675 Record.AddSourceLocation(TL.getTypeofLoc());
676 Record.AddSourceLocation(TL.getLParenLoc());
677 Record.AddSourceLocation(TL.getRParenLoc());
678 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000679}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000680
John McCall17001972009-10-18 01:05:36 +0000681void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000682 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000683}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000684
Alexis Hunte852b102011-05-24 22:41:36 +0000685void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000686 Record.AddSourceLocation(TL.getKWLoc());
687 Record.AddSourceLocation(TL.getLParenLoc());
688 Record.AddSourceLocation(TL.getRParenLoc());
689 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000690}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000691
Richard Smith30482bc2011-02-20 03:19:35 +0000692void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000693 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000694}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000695
Richard Smith600b5262017-01-26 20:40:47 +0000696void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
697 DeducedTemplateSpecializationTypeLoc TL) {
698 Record.AddSourceLocation(TL.getTemplateNameLoc());
699}
700
John McCall17001972009-10-18 01:05:36 +0000701void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000702 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000703}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000704
John McCall17001972009-10-18 01:05:36 +0000705void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000706 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000707}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000708
John McCall81904512011-01-06 01:58:22 +0000709void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000710 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000711 if (TL.hasAttrOperand()) {
712 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000713 Record.AddSourceLocation(range.getBegin());
714 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000715 }
716 if (TL.hasAttrExprOperand()) {
717 Expr *operand = TL.getAttrExprOperand();
718 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000719 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000720 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000721 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000722 }
723}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000724
John McCall17001972009-10-18 01:05:36 +0000725void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000726 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000727}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000728
John McCallcebee162009-10-18 09:09:24 +0000729void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
730 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000731 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000732}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000733
Douglas Gregorada4b792011-01-14 02:55:32 +0000734void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
735 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000736 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000737}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000738
John McCall17001972009-10-18 01:05:36 +0000739void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
740 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000741 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
742 Record.AddSourceLocation(TL.getTemplateNameLoc());
743 Record.AddSourceLocation(TL.getLAngleLoc());
744 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000745 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000746 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
747 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000748}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000749
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000750void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000751 Record.AddSourceLocation(TL.getLParenLoc());
752 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000753}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000754
Abramo Bagnara6150c882010-05-11 21:36:43 +0000755void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000756 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
757 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000758}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000759
John McCalle78aac42010-03-10 03:28:59 +0000760void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000761 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000762}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000763
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000764void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000765 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
766 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
767 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000768}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000769
John McCallc392f372010-06-11 00:33:02 +0000770void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
771 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000772 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
773 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
774 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
775 Record.AddSourceLocation(TL.getTemplateNameLoc());
776 Record.AddSourceLocation(TL.getLAngleLoc());
777 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000778 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000779 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
780 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000781}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000782
Douglas Gregord2fa7662010-12-20 02:24:11 +0000783void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000784 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000785}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000786
John McCall17001972009-10-18 01:05:36 +0000787void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000788 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000789}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000790
John McCall8b07ec22010-05-15 11:32:37 +0000791void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
792 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000793 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
794 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000795 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000796 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
797 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
798 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000799 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000800 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000801}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000802
John McCallfc93cf92009-10-22 22:37:11 +0000803void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000804 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000805}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000806
Eli Friedman0dfb8892011-10-06 23:00:33 +0000807void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000808 Record.AddSourceLocation(TL.getKWLoc());
809 Record.AddSourceLocation(TL.getLParenLoc());
810 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000811}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000812
Xiuli Pan9c14e282016-01-09 12:53:17 +0000813void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000814 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000815}
John McCall8f115c62009-10-16 21:56:05 +0000816
Richard Smith01b2cb42014-07-26 06:37:51 +0000817void ASTWriter::WriteTypeAbbrevs() {
818 using namespace llvm;
819
David Blaikieb44f0bf2017-01-04 22:36:43 +0000820 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000821
822 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000823 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000824 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
825 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
826 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000827 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000828
829 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000830 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000831 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
832 // FunctionType
833 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
834 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
835 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
836 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
837 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
838 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
839 // FunctionProtoType
840 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
841 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
842 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
843 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
844 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
845 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
846 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
847 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000848 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000849}
850
Chris Lattner19cea4e2009-04-22 05:57:30 +0000851//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000852// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000853//===----------------------------------------------------------------------===//
854
Chris Lattner28fa4e62009-04-26 22:26:21 +0000855static void EmitBlockID(unsigned ID, const char *Name,
856 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000857 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000858 Record.clear();
859 Record.push_back(ID);
860 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
861
862 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000863 if (!Name || Name[0] == 0)
864 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000865 Record.clear();
866 while (*Name)
867 Record.push_back(*Name++);
868 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
869}
870
871static void EmitRecordID(unsigned ID, const char *Name,
872 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000873 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000874 Record.clear();
875 Record.push_back(ID);
876 while (*Name)
877 Record.push_back(*Name++);
878 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000879}
880
881static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000882 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000883#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000884 RECORD(STMT_STOP);
885 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000886 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000887 RECORD(STMT_NULL);
888 RECORD(STMT_COMPOUND);
889 RECORD(STMT_CASE);
890 RECORD(STMT_DEFAULT);
891 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000892 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000893 RECORD(STMT_IF);
894 RECORD(STMT_SWITCH);
895 RECORD(STMT_WHILE);
896 RECORD(STMT_DO);
897 RECORD(STMT_FOR);
898 RECORD(STMT_GOTO);
899 RECORD(STMT_INDIRECT_GOTO);
900 RECORD(STMT_CONTINUE);
901 RECORD(STMT_BREAK);
902 RECORD(STMT_RETURN);
903 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000904 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000905 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000906 RECORD(EXPR_PREDEFINED);
907 RECORD(EXPR_DECL_REF);
908 RECORD(EXPR_INTEGER_LITERAL);
909 RECORD(EXPR_FLOATING_LITERAL);
910 RECORD(EXPR_IMAGINARY_LITERAL);
911 RECORD(EXPR_STRING_LITERAL);
912 RECORD(EXPR_CHARACTER_LITERAL);
913 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000914 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000915 RECORD(EXPR_UNARY_OPERATOR);
916 RECORD(EXPR_SIZEOF_ALIGN_OF);
917 RECORD(EXPR_ARRAY_SUBSCRIPT);
918 RECORD(EXPR_CALL);
919 RECORD(EXPR_MEMBER);
920 RECORD(EXPR_BINARY_OPERATOR);
921 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
922 RECORD(EXPR_CONDITIONAL_OPERATOR);
923 RECORD(EXPR_IMPLICIT_CAST);
924 RECORD(EXPR_CSTYLE_CAST);
925 RECORD(EXPR_COMPOUND_LITERAL);
926 RECORD(EXPR_EXT_VECTOR_ELEMENT);
927 RECORD(EXPR_INIT_LIST);
928 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000929 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000930 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000931 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000932 RECORD(EXPR_VA_ARG);
933 RECORD(EXPR_ADDR_LABEL);
934 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000935 RECORD(EXPR_CHOOSE);
936 RECORD(EXPR_GNU_NULL);
937 RECORD(EXPR_SHUFFLE_VECTOR);
938 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000939 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000940 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000941 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000942 RECORD(EXPR_OBJC_ARRAY_LITERAL);
943 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000944 RECORD(EXPR_OBJC_ENCODE);
945 RECORD(EXPR_OBJC_SELECTOR_EXPR);
946 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
947 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
948 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
949 RECORD(EXPR_OBJC_KVC_REF_EXPR);
950 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000951 RECORD(STMT_OBJC_FOR_COLLECTION);
952 RECORD(STMT_OBJC_CATCH);
953 RECORD(STMT_OBJC_FINALLY);
954 RECORD(STMT_OBJC_AT_TRY);
955 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
956 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000957 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000958 RECORD(STMT_CXX_CATCH);
959 RECORD(STMT_CXX_TRY);
960 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000961 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000962 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000963 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000964 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000965 RECORD(EXPR_CXX_STATIC_CAST);
966 RECORD(EXPR_CXX_DYNAMIC_CAST);
967 RECORD(EXPR_CXX_REINTERPRET_CAST);
968 RECORD(EXPR_CXX_CONST_CAST);
969 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000970 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000971 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000972 RECORD(EXPR_CXX_BOOL_LITERAL);
973 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000974 RECORD(EXPR_CXX_TYPEID_EXPR);
975 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000976 RECORD(EXPR_CXX_THIS);
977 RECORD(EXPR_CXX_THROW);
978 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000979 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000980 RECORD(EXPR_CXX_BIND_TEMPORARY);
981 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
982 RECORD(EXPR_CXX_NEW);
983 RECORD(EXPR_CXX_DELETE);
984 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
985 RECORD(EXPR_EXPR_WITH_CLEANUPS);
986 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
987 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
988 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
989 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
990 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000991 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000992 RECORD(EXPR_CXX_NOEXCEPT);
993 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000994 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
995 RECORD(EXPR_TYPE_TRAIT);
996 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000997 RECORD(EXPR_PACK_EXPANSION);
998 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +0000999 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001000 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001001 RECORD(EXPR_FUNCTION_PARM_PACK);
1002 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001003 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001004 RECORD(EXPR_CXX_UUIDOF_EXPR);
1005 RECORD(EXPR_CXX_UUIDOF_TYPE);
1006 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001007#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001008}
Mike Stump11289f42009-09-09 15:08:12 +00001009
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001010void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001011 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001012 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001013
Sebastian Redl539c5062010-08-18 23:57:32 +00001014#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1015#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001016
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001017 // Control Block.
1018 BLOCK(CONTROL_BLOCK);
1019 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001020 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001021 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001022 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001023 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001024 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001025 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001026 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001027 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001028
1029 BLOCK(OPTIONS_BLOCK);
1030 RECORD(LANGUAGE_OPTIONS);
1031 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001032 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001033 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001034 RECORD(PREPROCESSOR_OPTIONS);
1035
Douglas Gregor108cb222012-10-19 00:45:00 +00001036 BLOCK(INPUT_FILES_BLOCK);
1037 RECORD(INPUT_FILE);
1038
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001039 // AST Top-Level Block.
1040 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001041 RECORD(TYPE_OFFSET);
1042 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001043 RECORD(IDENTIFIER_OFFSET);
1044 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001045 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001046 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001047 RECORD(SPECIAL_TYPES);
1048 RECORD(STATISTICS);
1049 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001050 RECORD(SELECTOR_OFFSETS);
1051 RECORD(METHOD_POOL);
1052 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001053 RECORD(SOURCE_LOCATION_OFFSETS);
1054 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001055 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001056 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001057 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001058 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001059 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001060 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001061 RECORD(SEMA_DECL_REFS);
1062 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1063 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001064 RECORD(UPDATE_VISIBLE);
1065 RECORD(DECL_UPDATE_OFFSETS);
1066 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001067 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001068 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001069 RECORD(FP_PRAGMA_OPTIONS);
1070 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001071 RECORD(OPENCL_EXTENSION_TYPES);
1072 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001073 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001074 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001075 RECORD(MODULE_OFFSET_MAP);
1076 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001077 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001078 RECORD(FILE_SORTED_DECLS);
1079 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001080 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001081 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001082 RECORD(INTERESTING_IDENTIFIERS);
1083 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001084 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001085 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001086 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001087 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001088 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001089 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001090 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Douglas Gregor358cd442012-01-15 16:58:34 +00001091
Chris Lattner28fa4e62009-04-26 22:26:21 +00001092 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001093 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001094 RECORD(SM_SLOC_FILE_ENTRY);
1095 RECORD(SM_SLOC_BUFFER_ENTRY);
1096 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001097 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001098 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001099
Chris Lattner28fa4e62009-04-26 22:26:21 +00001100 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001101 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001102 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001103 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001104 RECORD(PP_MACRO_OBJECT_LIKE);
1105 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001106 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001107
Richard Smithfa715652015-08-31 22:43:10 +00001108 // Submodule Block.
1109 BLOCK(SUBMODULE_BLOCK);
1110 RECORD(SUBMODULE_METADATA);
1111 RECORD(SUBMODULE_DEFINITION);
1112 RECORD(SUBMODULE_UMBRELLA_HEADER);
1113 RECORD(SUBMODULE_HEADER);
1114 RECORD(SUBMODULE_TOPHEADER);
1115 RECORD(SUBMODULE_UMBRELLA_DIR);
1116 RECORD(SUBMODULE_IMPORTS);
1117 RECORD(SUBMODULE_EXPORTS);
1118 RECORD(SUBMODULE_REQUIRES);
1119 RECORD(SUBMODULE_EXCLUDED_HEADER);
1120 RECORD(SUBMODULE_LINK_LIBRARY);
1121 RECORD(SUBMODULE_CONFIG_MACRO);
1122 RECORD(SUBMODULE_CONFLICT);
1123 RECORD(SUBMODULE_PRIVATE_HEADER);
1124 RECORD(SUBMODULE_TEXTUAL_HEADER);
1125 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001126 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001127
1128 // Comments Block.
1129 BLOCK(COMMENTS_BLOCK);
1130 RECORD(COMMENTS_RAW_COMMENT);
1131
Douglas Gregor12bfa382009-10-17 00:13:19 +00001132 // Decls and Types block.
1133 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001134 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001135 RECORD(TYPE_COMPLEX);
1136 RECORD(TYPE_POINTER);
1137 RECORD(TYPE_BLOCK_POINTER);
1138 RECORD(TYPE_LVALUE_REFERENCE);
1139 RECORD(TYPE_RVALUE_REFERENCE);
1140 RECORD(TYPE_MEMBER_POINTER);
1141 RECORD(TYPE_CONSTANT_ARRAY);
1142 RECORD(TYPE_INCOMPLETE_ARRAY);
1143 RECORD(TYPE_VARIABLE_ARRAY);
1144 RECORD(TYPE_VECTOR);
1145 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001146 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001147 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001148 RECORD(TYPE_TYPEDEF);
1149 RECORD(TYPE_TYPEOF_EXPR);
1150 RECORD(TYPE_TYPEOF);
1151 RECORD(TYPE_RECORD);
1152 RECORD(TYPE_ENUM);
1153 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001154 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001155 RECORD(TYPE_DECLTYPE);
1156 RECORD(TYPE_ELABORATED);
1157 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1158 RECORD(TYPE_UNRESOLVED_USING);
1159 RECORD(TYPE_INJECTED_CLASS_NAME);
1160 RECORD(TYPE_OBJC_OBJECT);
1161 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1162 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1163 RECORD(TYPE_DEPENDENT_NAME);
1164 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1165 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1166 RECORD(TYPE_PAREN);
1167 RECORD(TYPE_PACK_EXPANSION);
1168 RECORD(TYPE_ATTRIBUTED);
1169 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001170 RECORD(TYPE_AUTO);
1171 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001172 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001173 RECORD(TYPE_DECAYED);
1174 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001175 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001176 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001177 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001178 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001179 RECORD(DECL_ENUM);
1180 RECORD(DECL_RECORD);
1181 RECORD(DECL_ENUM_CONSTANT);
1182 RECORD(DECL_FUNCTION);
1183 RECORD(DECL_OBJC_METHOD);
1184 RECORD(DECL_OBJC_INTERFACE);
1185 RECORD(DECL_OBJC_PROTOCOL);
1186 RECORD(DECL_OBJC_IVAR);
1187 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001188 RECORD(DECL_OBJC_CATEGORY);
1189 RECORD(DECL_OBJC_CATEGORY_IMPL);
1190 RECORD(DECL_OBJC_IMPLEMENTATION);
1191 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1192 RECORD(DECL_OBJC_PROPERTY);
1193 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001194 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001195 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001196 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001197 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001198 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001199 RECORD(DECL_FILE_SCOPE_ASM);
1200 RECORD(DECL_BLOCK);
1201 RECORD(DECL_CONTEXT_LEXICAL);
1202 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001203 RECORD(DECL_NAMESPACE);
1204 RECORD(DECL_NAMESPACE_ALIAS);
1205 RECORD(DECL_USING);
1206 RECORD(DECL_USING_SHADOW);
1207 RECORD(DECL_USING_DIRECTIVE);
1208 RECORD(DECL_UNRESOLVED_USING_VALUE);
1209 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1210 RECORD(DECL_LINKAGE_SPEC);
1211 RECORD(DECL_CXX_RECORD);
1212 RECORD(DECL_CXX_METHOD);
1213 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001214 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001215 RECORD(DECL_CXX_DESTRUCTOR);
1216 RECORD(DECL_CXX_CONVERSION);
1217 RECORD(DECL_ACCESS_SPEC);
1218 RECORD(DECL_FRIEND);
1219 RECORD(DECL_FRIEND_TEMPLATE);
1220 RECORD(DECL_CLASS_TEMPLATE);
1221 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1222 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001223 RECORD(DECL_VAR_TEMPLATE);
1224 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1225 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001226 RECORD(DECL_FUNCTION_TEMPLATE);
1227 RECORD(DECL_TEMPLATE_TYPE_PARM);
1228 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1229 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001230 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001231 RECORD(DECL_STATIC_ASSERT);
1232 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001233 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001234 RECORD(DECL_INDIRECTFIELD);
1235 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001236 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1237 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1238 RECORD(DECL_IMPORT);
1239 RECORD(DECL_OMP_THREADPRIVATE);
1240 RECORD(DECL_EMPTY);
1241 RECORD(DECL_OBJC_TYPE_PARAM);
1242 RECORD(DECL_OMP_CAPTUREDEXPR);
1243 RECORD(DECL_PRAGMA_COMMENT);
1244 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1245 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001246
Douglas Gregor03412ba2011-06-03 02:27:19 +00001247 // Statements and Exprs can occur in the Decls and Types block.
1248 AddStmtsExprs(Stream, Record);
1249
Douglas Gregor92a96f52011-02-08 21:58:10 +00001250 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001251 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001252 RECORD(PPD_MACRO_DEFINITION);
1253 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001254
1255 // Decls and Types block.
1256 BLOCK(EXTENSION_BLOCK);
1257 RECORD(EXTENSION_METADATA);
1258
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001259 BLOCK(UNHASHED_CONTROL_BLOCK);
1260 RECORD(SIGNATURE);
1261 RECORD(DIAGNOSTIC_OPTIONS);
1262 RECORD(DIAG_PRAGMA_MAPPINGS);
1263
Chris Lattner28fa4e62009-04-26 22:26:21 +00001264#undef RECORD
1265#undef BLOCK
1266 Stream.ExitBlock();
1267}
1268
Richard Smith54cc3c22014-12-11 20:50:24 +00001269/// \brief Prepares a path for being written to an AST file by converting it
1270/// to an absolute path and removing nested './'s.
1271///
1272/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001273static bool cleanPathForOutput(FileManager &FileMgr,
1274 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001275 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001276 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001277}
1278
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001279/// \brief Adjusts the given filename to only write out the portion of the
1280/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001281///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001282/// \param Filename the file name to adjust.
1283///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001284/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1285/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001286///
1287/// \returns either the original filename (if it needs no adjustment) or the
1288/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001289static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001290adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001291 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001292
Richard Smith7ed1bc92014-12-05 22:42:13 +00001293 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001294 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001295
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001296 // Verify that the filename and the system root have the same prefix.
1297 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001298 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1299 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001300 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001302 // We hit the end of the filename before we hit the end of the system root.
1303 if (!Filename[Pos])
1304 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001305
Richard Smith7ed1bc92014-12-05 22:42:13 +00001306 // If there's not a path separator at the end of the base directory nor
1307 // immediately after it, then this isn't within the base directory.
1308 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1309 if (!llvm::sys::path::is_separator(BaseDir.back()))
1310 return Filename;
1311 } else {
1312 // If the file name has a '/' at the current position, skip over the '/'.
1313 // We distinguish relative paths from absolute paths by the
1314 // absence of '/' at the beginning of relative paths.
1315 //
1316 // FIXME: This is wrong. We distinguish them by asking if the path is
1317 // absolute, which isn't the same thing. And there might be multiple '/'s
1318 // in a row. Use a better mechanism to indicate whether we have emitted an
1319 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001320 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001321 }
Mike Stump11289f42009-09-09 15:08:12 +00001322
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001323 return Filename + Pos;
1324}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001325
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001326ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1327 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1328 llvm::SHA1 Hasher;
1329 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1330 auto Hash = Hasher.result();
1331
1332 // Convert to an array [5*i32].
1333 ASTFileSignature Signature;
1334 auto LShift = [&](unsigned char Val, unsigned Shift) {
1335 return (uint32_t)Val << Shift;
1336 };
1337 for (int I = 0; I != 5; ++I)
1338 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1339 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1340
1341 return Signature;
1342}
1343
1344ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1345 ASTContext &Context) {
1346 // Flush first to prepare the PCM hash (signature).
1347 Stream.FlushToWord();
1348 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1349
1350 // Enter the block and prepare to write records.
1351 RecordData Record;
1352 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1353
1354 // For implicit modules, write the hash of the PCM as its signature.
1355 ASTFileSignature Signature;
1356 if (WritingModule &&
1357 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1358 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1359 Record.append(Signature.begin(), Signature.end());
1360 Stream.EmitRecord(SIGNATURE, Record);
1361 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001362 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001363
1364 // Diagnostic options.
1365 const auto &Diags = Context.getDiagnostics();
1366 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1367#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1368#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1369 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1370#include "clang/Basic/DiagnosticOptions.def"
1371 Record.push_back(DiagOpts.Warnings.size());
1372 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1373 AddString(DiagOpts.Warnings[I], Record);
1374 Record.push_back(DiagOpts.Remarks.size());
1375 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1376 AddString(DiagOpts.Remarks[I], Record);
1377 // Note: we don't serialize the log or serialization file names, because they
1378 // are generally transient files and will almost always be overridden.
1379 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1380
1381 // Write out the diagnostic/pragma mappings.
1382 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1383
1384 // Leave the options block.
1385 Stream.ExitBlock();
1386 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001387}
1388
Douglas Gregor112b9072012-10-18 05:31:06 +00001389/// \brief Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001390void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1391 StringRef isysroot,
1392 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001393 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001394 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001395 RecordData Record;
1396
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001397 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001398 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001399 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1400 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1401 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1402 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1403 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1404 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001405 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001406 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1407 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001408 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001409 assert((!WritingModule || isysroot.empty()) &&
1410 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001411 {
1412 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1413 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1414 !isysroot.empty(), IncludeTimestamps,
1415 ASTHasCompilerErrors};
1416 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1417 getClangFullRepositoryVersion());
1418 }
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001419 if (WritingModule) {
Chandler Carruth885e78c2015-03-24 21:18:10 +00001420
Richard Smith7ed1bc92014-12-05 22:42:13 +00001421 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001422 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001423 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001425 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001426 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001427 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1428 }
1429
Richard Smith7ed1bc92014-12-05 22:42:13 +00001430 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001431 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001432 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001433
1434 // If the home of the module is the current working directory, then we
1435 // want to pick up the cwd of the build process loading the module, not
1436 // our cwd, when we load this module.
1437 if (!PP.getHeaderSearchInfo()
1438 .getHeaderSearchOpts()
1439 .ModuleMapFileHomeIsCwd ||
1440 WritingModule->Directory->getName() != StringRef(".")) {
1441 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001442 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001443 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001445 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001446
Mehdi Amini57a41912015-09-10 01:46:39 +00001447 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001448 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1449 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001450
1451 // Write out all other paths relative to the base directory if possible.
1452 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1453 } else if (!isysroot.empty()) {
1454 // Write out paths relative to the sysroot if possible.
1455 BaseDirectory = isysroot;
1456 }
1457
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001458 // Module map file
1459 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001460 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001461
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001462 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1463
1464 // Primary module map file.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001465 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001466
1467 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001468 if (auto *AdditionalModMaps =
1469 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001470 Record.push_back(AdditionalModMaps->size());
1471 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001472 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001473 } else {
1474 Record.push_back(0);
1475 }
1476
1477 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001478 }
1479
Douglas Gregor112b9072012-10-18 05:31:06 +00001480 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001481 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001482 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001483 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001484
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001485 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001486 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001487 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001488 continue;
1489
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001490 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1491 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001492
1493 // If we have calculated signature, there is no need to store
1494 // the size or timestamp.
1495 Record.push_back(M.Signature ? 0 : M.File->getSize());
1496 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1497
1498 for (auto I : M.Signature)
1499 Record.push_back(I);
1500
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001501 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001502 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001503 Stream.EmitRecord(IMPORTS, Record);
1504 }
Mike Stump11289f42009-09-09 15:08:12 +00001505
Richard Smith0516b182015-09-08 19:40:14 +00001506 // Write the options block.
1507 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1508
Douglas Gregor112b9072012-10-18 05:31:06 +00001509 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001510 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001511 const LangOptions &LangOpts = Context.getLangOpts();
1512#define LANGOPT(Name, Bits, Default, Description) \
1513 Record.push_back(LangOpts.Name);
1514#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1515 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001516#include "clang/Basic/LangOptions.def"
1517#define SANITIZER(NAME, ID) \
1518 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001519#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001520
Ben Langmuircd98cb72015-06-23 18:20:18 +00001521 Record.push_back(LangOpts.ModuleFeatures.size());
1522 for (StringRef Feature : LangOpts.ModuleFeatures)
1523 AddString(Feature, Record);
1524
Douglas Gregor112b9072012-10-18 05:31:06 +00001525 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1526 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001527
1528 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001529
1530 // Comment options.
1531 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001532 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1533 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001534 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001535 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001536
Samuel Antaoee8fb302016-01-06 13:42:12 +00001537 // OpenMP offloading options.
1538 Record.push_back(LangOpts.OMPTargetTriples.size());
1539 for (auto &T : LangOpts.OMPTargetTriples)
1540 AddString(T.getTriple(), Record);
1541
1542 AddString(LangOpts.OMPHostIRFile, Record);
1543
Douglas Gregor112b9072012-10-18 05:31:06 +00001544 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1545
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001546 // Target options.
1547 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001548 const TargetInfo &Target = Context.getTargetInfo();
1549 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001550 AddString(TargetOpts.Triple, Record);
1551 AddString(TargetOpts.CPU, Record);
1552 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001553 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1554 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1555 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1556 }
1557 Record.push_back(TargetOpts.Features.size());
1558 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1559 AddString(TargetOpts.Features[I], Record);
1560 }
1561 Stream.EmitRecord(TARGET_OPTIONS, Record);
1562
Douglas Gregorc6317db2012-10-24 15:49:58 +00001563 // File system options.
1564 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001565 const FileSystemOptions &FSOpts =
1566 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001567 AddString(FSOpts.WorkingDir, Record);
1568 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1569
Douglas Gregor2d302362012-10-24 16:50:34 +00001570 // Header search options.
1571 Record.clear();
1572 const HeaderSearchOptions &HSOpts
1573 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1574 AddString(HSOpts.Sysroot, Record);
1575
1576 // Include entries.
1577 Record.push_back(HSOpts.UserEntries.size());
1578 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1579 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1580 AddString(Entry.Path, Record);
1581 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001582 Record.push_back(Entry.IsFramework);
1583 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001584 }
1585
1586 // System header prefixes.
1587 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1588 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1589 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1590 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1591 }
1592
1593 AddString(HSOpts.ResourceDir, Record);
1594 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001595 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001596 Record.push_back(HSOpts.DisableModuleHash);
1597 Record.push_back(HSOpts.UseBuiltinIncludes);
1598 Record.push_back(HSOpts.UseStandardSystemIncludes);
1599 Record.push_back(HSOpts.UseStandardCXXIncludes);
1600 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001601 // Write out the specific module cache path that contains the module files.
1602 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001603 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1604
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001605 // Preprocessor options.
1606 Record.clear();
1607 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1608
1609 // Macro definitions.
1610 Record.push_back(PPOpts.Macros.size());
1611 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1612 AddString(PPOpts.Macros[I].first, Record);
1613 Record.push_back(PPOpts.Macros[I].second);
1614 }
1615
1616 // Includes
1617 Record.push_back(PPOpts.Includes.size());
1618 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1619 AddString(PPOpts.Includes[I], Record);
1620
1621 // Macro includes
1622 Record.push_back(PPOpts.MacroIncludes.size());
1623 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1624 AddString(PPOpts.MacroIncludes[I], Record);
1625
Douglas Gregorb6368752012-10-24 23:41:50 +00001626 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001627 // Detailed record is important since it is used for the module cache hash.
1628 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001629 AddString(PPOpts.ImplicitPCHInclude, Record);
1630 AddString(PPOpts.ImplicitPTHInclude, Record);
1631 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1632 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1633
Richard Smith0516b182015-09-08 19:40:14 +00001634 // Leave the options block.
1635 Stream.ExitBlock();
1636
Douglas Gregora3b20262011-05-06 21:43:30 +00001637 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001638 SourceManager &SM = Context.getSourceManager();
1639 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001640 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001641 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1642 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001643 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001644 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001645
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001646 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001647 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001648 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001649 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001650 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001651
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001652 Record.clear();
1653 Record.push_back(SM.getMainFileID().getOpaqueValue());
1654 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1655
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001656 // Original PCH directory
1657 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001658 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001659 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001661 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001662
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001663 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001664
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001665 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001666 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1667
Mehdi Amini57a41912015-09-10 01:46:39 +00001668 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001669 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1670 }
1671
Douglas Gregor49491f72013-03-15 22:15:07 +00001672 WriteInputFiles(Context.SourceMgr,
1673 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001674 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001675 Stream.ExitBlock();
1676}
1677
Douglas Gregor49491f72013-03-15 22:15:07 +00001678namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001679
Douglas Gregor49491f72013-03-15 22:15:07 +00001680 /// \brief An input file.
1681 struct InputFileEntry {
1682 const FileEntry *File;
1683 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001684 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001685 bool BufferOverridden;
1686 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001687
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001688} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001689
1690void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1691 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001692 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001693 using namespace llvm;
1694 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001695
Douglas Gregor72be3902012-10-19 00:38:02 +00001696 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001697 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001698 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001699 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001700 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1701 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001702 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001703 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001704 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001705 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001706
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001707 // Get all ContentCache objects for files, sorted by whether the file is a
1708 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001709 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001710 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1711 // Get this source location entry.
1712 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001713 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001714
1715 // We only care about file entries that were not overridden.
1716 if (!SLoc->isFile())
1717 continue;
1718 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001719 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001720 continue;
1721
Douglas Gregor49491f72013-03-15 22:15:07 +00001722 InputFileEntry Entry;
1723 Entry.File = Cache->OrigEntry;
1724 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001725 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001726 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001727 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001728 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001729 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001730 SortedFiles.push_front(Entry);
1731 }
1732
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001733 unsigned UserFilesNum = 0;
1734 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001735 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001736 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001737 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001738 if (InputFileID != 0)
1739 continue; // already recorded this file.
1740
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001741 // Record this entry's offset.
1742 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001743
1744 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001745
Douglas Gregor49491f72013-03-15 22:15:07 +00001746 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001747 ++UserFilesNum;
1748
Douglas Gregor72be3902012-10-19 00:38:02 +00001749 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001750 // And whether this file was overridden.
1751 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001752 INPUT_FILE,
1753 InputFileOffsets.size(),
1754 (uint64_t)Entry.File->getSize(),
1755 (uint64_t)getTimestampForOutput(Entry.File),
1756 Entry.BufferOverridden,
1757 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001758
Richard Smith7ed1bc92014-12-05 22:42:13 +00001759 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1760 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001761
Douglas Gregor112b9072012-10-18 05:31:06 +00001762 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001763
1764 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001765 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001766 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1767 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001768 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1769 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001770 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001771 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001772
1773 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001774 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1775 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001776 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001777}
1778
Douglas Gregora7f71a92009-04-10 03:52:48 +00001779//===----------------------------------------------------------------------===//
1780// Source Manager Serialization
1781//===----------------------------------------------------------------------===//
1782
1783/// \brief Create an abbreviation for the SLocEntry that refers to a
1784/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001785static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001786 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001787
David Blaikieb44f0bf2017-01-04 22:36:43 +00001788 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001789 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001794 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001799 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001800}
1801
1802/// \brief Create an abbreviation for the SLocEntry that refers to a
1803/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001804static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001805 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001806
David Blaikieb44f0bf2017-01-04 22:36:43 +00001807 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001808 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001814 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001815}
1816
1817/// \brief Create an abbreviation for the SLocEntry that refers to a
1818/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001819static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1820 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001821 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001822
David Blaikieb44f0bf2017-01-04 22:36:43 +00001823 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001824 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1825 : SM_SLOC_BUFFER_BLOB));
1826 if (Compressed)
1827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001829 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001830}
1831
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001832/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1833/// expansion.
1834static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001835 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001836
David Blaikieb44f0bf2017-01-04 22:36:43 +00001837 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001838 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001844 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001845}
1846
Douglas Gregor09b69892011-02-10 17:09:37 +00001847namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001848
Douglas Gregor09b69892011-02-10 17:09:37 +00001849 // Trait used for the on-disk hash table of header search information.
1850 class HeaderFileInfoTrait {
1851 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001852 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001853
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001854 // Keep track of the framework names we've used during serialization.
1855 SmallVector<char, 128> FrameworkStringData;
1856 llvm::StringMap<unsigned> FrameworkNameOffset;
1857
Douglas Gregor09b69892011-02-10 17:09:37 +00001858 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001859 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1860 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001861
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001862 struct key_type {
1863 const FileEntry *FE;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001864 StringRef Filename;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001865 };
1866 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001867
1868 typedef HeaderFileInfo data_type;
1869 typedef const data_type &data_type_ref;
Justin Bogner25463f12014-04-18 20:27:24 +00001870 typedef unsigned hash_value_type;
1871 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001872
Richard Smithe75ee0f2015-08-17 07:13:32 +00001873 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001874 // The hash is based only on size/time of the file, so that the reader can
1875 // match even when symlinking or excess path elements ("foo/../", "../")
1876 // change the form of the name. However, complete path is still the key.
1877 return llvm::hash_combine(key.FE->getSize(),
Richard Smithe75ee0f2015-08-17 07:13:32 +00001878 Writer.getTimestampForOutput(key.FE));
Douglas Gregor09b69892011-02-10 17:09:37 +00001879 }
1880
1881 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001882 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001883 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001884 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001885 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001886 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001887 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith386bb072015-08-18 23:42:23 +00001888 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1889 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1890 DataLen += 4;
1891 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001892 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001893 }
1894
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001895 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001896 using namespace llvm::support;
1897 endian::Writer<little> LE(Out);
1898 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001899 KeyLen -= 8;
Richard Smithe75ee0f2015-08-17 07:13:32 +00001900 LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001901 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001902 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001903 }
1904
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001905 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001906 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001907 using namespace llvm::support;
1908 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001909 uint64_t Start = Out.tell(); (void)Start;
1910
Richard Smith386bb072015-08-18 23:42:23 +00001911 unsigned char Flags = (Data.isImport << 4)
1912 | (Data.isPragmaOnce << 3)
1913 | (Data.DirInfo << 1)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001914 | Data.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001915 LE.write<uint8_t>(Flags);
1916 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001917
1918 if (!Data.ControllingMacro)
Justin Bognere1c147c2014-03-28 22:03:19 +00001919 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001920 else
Justin Bognere1c147c2014-03-28 22:03:19 +00001921 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001922
1923 unsigned Offset = 0;
1924 if (!Data.Framework.empty()) {
1925 // If this header refers into a framework, save the framework name.
1926 llvm::StringMap<unsigned>::iterator Pos
1927 = FrameworkNameOffset.find(Data.Framework);
1928 if (Pos == FrameworkNameOffset.end()) {
1929 Offset = FrameworkStringData.size() + 1;
1930 FrameworkStringData.append(Data.Framework.begin(),
1931 Data.Framework.end());
1932 FrameworkStringData.push_back(0);
1933
1934 FrameworkNameOffset[Data.Framework] = Offset;
1935 } else
1936 Offset = Pos->second;
1937 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001938 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001939
Richard Smith386bb072015-08-18 23:42:23 +00001940 // FIXME: If the header is excluded, we should write out some
1941 // record of that fact.
1942 for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1943 if (uint32_t ModID =
1944 Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1945 uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1946 assert((Value >> 2) == ModID && "overflow in header module info");
1947 LE.write<uint32_t>(Value);
1948 }
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001949 }
1950
Douglas Gregor09b69892011-02-10 17:09:37 +00001951 assert(Out.tell() - Start == DataLen && "Wrong data length");
1952 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001953
1954 const char *strings_begin() const { return FrameworkStringData.begin(); }
1955 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001956 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001957
Douglas Gregor09b69892011-02-10 17:09:37 +00001958} // end anonymous namespace
1959
1960/// \brief Write the header search block for the list of files that
1961///
1962/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001963void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001964 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001965 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1966
1967 if (FilesByUID.size() > HS.header_file_size())
1968 FilesByUID.resize(HS.header_file_size());
1969
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001970 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Justin Bognerbb094f02014-04-18 19:57:06 +00001971 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001972 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001973 unsigned NumHeaderSearchEntries = 0;
1974 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1975 const FileEntry *File = FilesByUID[UID];
1976 if (!File)
1977 continue;
1978
Richard Smith386bb072015-08-18 23:42:23 +00001979 // Get the file info. This will load info from the external source if
1980 // necessary. Skip emitting this file if we have no information on it
1981 // as a header file (in which case HFI will be null) or if it hasn't
1982 // changed since it was loaded. Also skip it if it's for a modular header
1983 // from a different module; in that case, we rely on the module(s)
1984 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00001985 const HeaderFileInfo *HFI =
1986 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1987 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001988 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001989
Richard Smith7ed1bc92014-12-05 22:42:13 +00001990 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001991 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00001992 SmallString<128> FilenameTmp(Filename);
1993 if (PreparePathForOutput(FilenameTmp)) {
1994 // If we performed any translation on the file name at all, we need to
1995 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00001996 Filename = StringRef(strdup(FilenameTmp.c_str()));
1997 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00001998 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001999
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00002000 HeaderFileInfoTrait::key_type key = { File, Filename };
Richard Smith386bb072015-08-18 23:42:23 +00002001 Generator.insert(key, *HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002002 ++NumHeaderSearchEntries;
2003 }
2004
2005 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002006 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002007 uint32_t BucketOffset;
2008 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002009 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00002010 llvm::raw_svector_ostream Out(TableData);
2011 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002012 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00002013 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2014 }
2015
2016 // Create a blob abbreviation
2017 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002018
David Blaikieb44f0bf2017-01-04 22:36:43 +00002019 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002020 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002025 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00002026
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002027 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002028 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2029 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002030 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002031 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00002032
2033 // Free all of the strings we had to duplicate.
2034 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002035 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002036}
2037
George Rimarc39f5492017-01-17 15:45:31 +00002038static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2039 unsigned SLocBufferBlobCompressedAbbrv,
2040 unsigned SLocBufferBlobAbbrv) {
2041 typedef ASTWriter::RecordData::value_type RecordDataType;
2042
2043 // Compress the buffer if possible. We expect that almost all PCM
2044 // consumers will not want its contents.
2045 SmallString<0> CompressedBuffer;
2046 if (llvm::zlib::isAvailable()) {
2047 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2048 if (!E) {
2049 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2050 Blob.size() - 1};
2051 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2052 CompressedBuffer);
2053 return;
2054 }
2055 llvm::consumeError(std::move(E));
2056 }
2057
2058 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2059 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2060}
2061
Douglas Gregora7f71a92009-04-10 03:52:48 +00002062/// \brief Writes the block containing the serialized form of the
2063/// source manager.
2064///
2065/// TODO: We should probably use an on-disk hash table (stored in a
2066/// blob), indexed based on the file name, so that we only create
2067/// entries for files that we actually need. In the common case (no
2068/// errors), we probably won't have to create file entries for any of
2069/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002070void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002071 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002072 RecordData Record;
2073
Chris Lattner0910e3b2009-04-10 17:16:57 +00002074 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002075 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002076
2077 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002078 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2079 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002080 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2081 unsigned SLocBufferBlobCompressedAbbrv =
2082 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002083 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002084
Douglas Gregor258ae542009-04-27 06:38:32 +00002085 // Write out the source location entry table. We skip the first
2086 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002087 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002088 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002089 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2090 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002091 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002092 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002093 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002094 FileID FID = FileID::get(I);
2095 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002096
Douglas Gregor258ae542009-04-27 06:38:32 +00002097 // Record the offset of this source-location entry.
2098 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2099
2100 // Figure out which record code to use.
2101 unsigned Code;
2102 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002103 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2104 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002105 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002106 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002107 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002108 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002109 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002110 Record.clear();
2111 Record.push_back(Code);
2112
Douglas Gregor925296b2011-07-19 16:10:42 +00002113 // Starting offset of this entry within this module, so skip the dummy.
2114 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002115 if (SLoc->isFile()) {
2116 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002117 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002118 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2119 Record.push_back(File.hasLineDirectives());
2120
2121 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002122 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002123 if (Content->OrigEntry) {
2124 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002125 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002126
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002127 // The source location entry is a file. Emit input file ID.
2128 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2129 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002130
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002131 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002132
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002133 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002134 if (FDI != FileDeclIDs.end()) {
2135 Record.push_back(FDI->second->FirstDeclIndex);
2136 Record.push_back(FDI->second->DeclIDs.size());
2137 } else {
2138 Record.push_back(0);
2139 Record.push_back(0);
2140 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002141
2142 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2143
Richard Smithaada85c2016-02-06 02:06:43 +00002144 if (Content->BufferOverridden || Content->IsTransient)
2145 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002146 } else {
2147 // The source location entry is a buffer. The blob associated
2148 // with this entry contains the contents of the buffer.
2149
2150 // We add one to the size so that we capture the trailing NULL
2151 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2152 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002153 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002154 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002155 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002156 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002157 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002158 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002159
Mehdi Amini99d1b292016-10-01 16:38:28 +00002160 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002161 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002162 }
Richard Smithaada85c2016-02-06 02:06:43 +00002163
2164 if (EmitBlob) {
2165 // Include the implicit terminating null character in the on-disk buffer
2166 // if we're writing it uncompressed.
2167 const llvm::MemoryBuffer *Buffer =
2168 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2169 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002170 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2171 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002172 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002173 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002174 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002175 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002176 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2177 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2178 AddSourceLocation(Expansion.isMacroArgExpansion()
2179 ? SourceLocation()
2180 : Expansion.getExpansionLocEnd(),
2181 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002182
2183 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002184 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002185 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002186 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002187 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002188 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002189 }
2190 }
2191
Douglas Gregor8f45df52009-04-16 22:23:12 +00002192 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002193
2194 if (SLocEntryOffsets.empty())
2195 return;
2196
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002197 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002198 // table is used for lazily loading source-location information.
2199 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002200
David Blaikieb44f0bf2017-01-04 22:36:43 +00002201 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002202 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002206 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002207 {
2208 RecordData::value_type Record[] = {
2209 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2210 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2211 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2212 bytes(SLocEntryOffsets));
2213 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002214 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002215 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002216 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002217
2218 // Write the line table. It depends on remapping working, so it must come
2219 // after the source location offsets.
2220 if (SourceMgr.hasLineTable()) {
2221 LineTableInfo &LineTable = SourceMgr.getLineTable();
2222
2223 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002224
2225 // Emit the needed file names.
2226 llvm::DenseMap<int, int> FilenameMap;
2227 for (const auto &L : LineTable) {
2228 if (L.first.ID < 0)
2229 continue;
2230 for (auto &LE : L.second) {
2231 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2232 FilenameMap.size())).second)
2233 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2234 }
2235 }
2236 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002237
2238 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002239 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002240 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002241 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002242 continue;
2243
2244 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002245 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002246
2247 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002248 Record.push_back(L.second.size());
2249 for (const auto &LE : L.second) {
2250 Record.push_back(LE.FileOffset);
2251 Record.push_back(LE.LineNo);
2252 Record.push_back(FilenameMap[LE.FilenameID]);
2253 Record.push_back((unsigned)LE.FileKind);
2254 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002255 }
2256 }
Richard Smith63078492015-09-01 07:41:55 +00002257
Douglas Gregor925296b2011-07-19 16:10:42 +00002258 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2259 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002260}
2261
Douglas Gregorc5046832009-04-27 18:38:38 +00002262//===----------------------------------------------------------------------===//
2263// Preprocessor Serialization
2264//===----------------------------------------------------------------------===//
2265
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002266static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2267 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002268 if (MacroInfo *MI = MD->getMacroInfo())
2269 if (MI->isBuiltinMacro())
2270 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002271
2272 if (IsModule) {
2273 SourceLocation Loc = MD->getLocation();
2274 if (Loc.isInvalid())
2275 return true;
2276 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2277 return true;
2278 }
2279
2280 return false;
2281}
2282
Chris Lattnereeffaef2009-04-10 17:15:23 +00002283/// \brief Writes the block containing the serialized form of the
2284/// preprocessor.
2285///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002286void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002287 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2288 if (PPRec)
2289 WritePreprocessorDetail(*PPRec);
2290
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002291 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002292 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002293
Chris Lattner0af3ba12009-04-13 01:29:17 +00002294 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2295 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002296 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002297 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002298 }
2299
2300 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002301 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002302
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002303 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002304 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002305 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002306 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002307
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002308 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002309 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002310
Richard Smithee977932015-05-01 21:22:17 +00002311 // Construct the list of identifiers with macro directives that need to be
2312 // serialized.
2313 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2314 for (auto &Id : PP.getIdentifierTable())
2315 if (Id.second->hadMacroDefinition() &&
2316 (!Id.second->isFromAST() ||
2317 Id.second->hasChangedSinceDeserialization()))
2318 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002319 // Sort the set of macro definitions that need to be serialized by the
2320 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002321 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2322 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002323
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002324 // Emit the macro directives as a list and associate the offset with the
2325 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002326 for (const IdentifierInfo *Name : MacroIdentifiers) {
2327 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002328 auto StartOffset = Stream.GetCurrentBitNo();
2329
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002330 // Emit the macro directives in reverse source order.
2331 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002332 // Once we hit an ignored macro, we're done: the rest of the chain
2333 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002334 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002335 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002336
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002337 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002338 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002339 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002340 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002341 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002342 Record.push_back(VisMD->isPublic());
2343 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002344 }
Richard Smithd7329392015-04-21 21:46:32 +00002345
Richard Smithb8b2ed62015-04-23 18:18:26 +00002346 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002347 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002348 // We write out exported module macros for PCH as well.
2349 auto Leafs = PP.getLeafModuleMacros(Name);
2350 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2351 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2352 while (!Worklist.empty()) {
2353 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002354
Manman Ren33d80292016-05-31 18:19:32 +00002355 // Emit a record indicating this submodule exports this macro.
2356 ModuleMacroRecord.push_back(
2357 getSubmoduleID(Macro->getOwningModule()));
2358 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2359 for (auto *M : Macro->overrides())
2360 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002361
Manman Ren33d80292016-05-31 18:19:32 +00002362 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2363 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002364
Manman Ren33d80292016-05-31 18:19:32 +00002365 // Enqueue overridden macros once we've visited all their ancestors.
2366 for (auto *M : Macro->overrides())
2367 if (++Visits[M] == M->getNumOverridingMacros())
2368 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002369
Manman Ren33d80292016-05-31 18:19:32 +00002370 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002371 }
Richard Smithd7329392015-04-21 21:46:32 +00002372
Richard Smithee977932015-05-01 21:22:17 +00002373 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002374 continue;
2375
Richard Smithd7329392015-04-21 21:46:32 +00002376 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002377 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2378 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002379 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002380
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002381 /// \brief Offsets of each of the macros into the bitstream, indexed by
2382 /// the local macro ID
2383 ///
2384 /// For each identifier that is associated with a macro, this map
2385 /// provides the offset into the bitstream where that macro is
2386 /// defined.
2387 std::vector<uint32_t> MacroOffsets;
2388
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002389 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2390 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2391 MacroInfo *MI = MacroInfosToEmit[I].MI;
2392 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002393
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002394 if (ID < FirstMacroID) {
2395 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2396 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002397 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002398
2399 // Record the local offset of this macro.
2400 unsigned Index = ID - FirstMacroID;
2401 if (Index == MacroOffsets.size())
2402 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2403 else {
2404 if (Index > MacroOffsets.size())
2405 MacroOffsets.resize(Index + 1);
2406
2407 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2408 }
2409
2410 AddIdentifierRef(Name, Record);
2411 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2412 AddSourceLocation(MI->getDefinitionLoc(), Record);
2413 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2414 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002415 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002416 unsigned Code;
2417 if (MI->isObjectLike()) {
2418 Code = PP_MACRO_OBJECT_LIKE;
2419 } else {
2420 Code = PP_MACRO_FUNCTION_LIKE;
2421
2422 Record.push_back(MI->isC99Varargs());
2423 Record.push_back(MI->isGNUVarargs());
2424 Record.push_back(MI->hasCommaPasting());
2425 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002426 for (const IdentifierInfo *Arg : MI->args())
2427 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002428 }
2429
2430 // If we have a detailed preprocessing record, record the macro definition
2431 // ID that corresponds to this macro.
2432 if (PPRec)
2433 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2434
2435 Stream.EmitRecord(Code, Record);
2436 Record.clear();
2437
2438 // Emit the tokens array.
2439 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2440 // Note that we know that the preprocessor does not have any annotation
2441 // tokens in it because they are created by the parser, and thus can't
2442 // be in a macro definition.
2443 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002444 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002445 Stream.EmitRecord(PP_TOKEN, Record);
2446 Record.clear();
2447 }
2448 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002449 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002450
Douglas Gregor92a96f52011-02-08 21:58:10 +00002451 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002452
2453 // Write the offsets table for macro IDs.
2454 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002455
David Blaikieb44f0bf2017-01-04 22:36:43 +00002456 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002457 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2460 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2461
David Blaikieb44f0bf2017-01-04 22:36:43 +00002462 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002463 {
2464 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2465 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2466 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2467 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002468}
2469
2470void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002471 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002472 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002473
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002474 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002475
Douglas Gregor92a96f52011-02-08 21:58:10 +00002476 // Enter the preprocessor block.
2477 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002478
Douglas Gregoraae92242010-03-19 21:51:54 +00002479 // If the preprocessor has a preprocessing record, emit it.
2480 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002481 using namespace llvm;
2482
2483 // Set up the abbreviation for
2484 unsigned InclusionAbbrev = 0;
2485 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002486 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002487 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2489 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002492 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002493 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002494 }
2495
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002496 unsigned FirstPreprocessorEntityID
2497 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2498 + NUM_PREDEF_PP_ENTITY_IDS;
2499 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002500 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002501 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2502 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002503 E != EEnd;
2504 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002505 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002506
Richard Smith66a81862015-05-04 02:25:31 +00002507 PreprocessedEntityOffsets.push_back(
2508 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002509
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002510 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002511 // Record this macro definition's ID.
2512 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002513
Douglas Gregor92a96f52011-02-08 21:58:10 +00002514 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002515 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2516 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002517 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002518
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002519 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002520 Record.push_back(ME->isBuiltinMacro());
2521 if (ME->isBuiltinMacro())
2522 AddIdentifierRef(ME->getName(), Record);
2523 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002524 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002525 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002526 continue;
2527 }
2528
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002529 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002530 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002531 Record.push_back(ID->getFileName().size());
2532 Record.push_back(ID->wasInQuotes());
2533 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002534 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002535 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002536 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002537 // Check that the FileEntry is not null because it was not resolved and
2538 // we create a PCH even with compiler errors.
2539 if (ID->getFile())
2540 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002541 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002542 continue;
2543 }
2544
2545 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2546 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002547 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002548
Douglas Gregoraae92242010-03-19 21:51:54 +00002549 // Write the offsets table for the preprocessing record.
2550 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002551 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2552
Douglas Gregoraae92242010-03-19 21:51:54 +00002553 // Write the offsets table for identifier IDs.
2554 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002555
David Blaikieb44f0bf2017-01-04 22:36:43 +00002556 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002557 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002560 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002561
Mehdi Amini57a41912015-09-10 01:46:39 +00002562 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2563 FirstPreprocessorEntityID -
2564 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002565 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002566 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002567 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002568}
2569
Richard Smith386bb072015-08-18 23:42:23 +00002570unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002571 if (!Mod)
2572 return 0;
2573
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002574 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2575 if (Known != SubmoduleIDs.end())
2576 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002577
Richard Smithdc1f0422016-07-20 19:10:16 +00002578 auto *Top = Mod->getTopLevelModule();
2579 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002580 (getLangOpts().CompilingPCH ||
2581 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002582 return 0;
2583
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002584 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2585}
2586
Richard Smith386bb072015-08-18 23:42:23 +00002587unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2588 // FIXME: This can easily happen, if we have a reference to a submodule that
2589 // did not result in us loading a module file for that submodule. For
2590 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2591 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2592 assert((ID || !Mod) &&
2593 "asked for module ID for non-local, non-imported module");
2594 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002595}
2596
Douglas Gregor253eefe2011-12-01 00:59:36 +00002597/// \brief Compute the number of modules within the given tree (including the
2598/// given module).
2599static unsigned getNumberOfModules(Module *Mod) {
2600 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002601 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002602 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002603 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002604
2605 return ChildModules + 1;
2606}
2607
Douglas Gregorde3ef502011-11-30 23:21:26 +00002608void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002609 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002610 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002611
2612 // Write the abbreviations needed for the submodules block.
2613 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002614
David Blaikieb44f0bf2017-01-04 22:36:43 +00002615 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002616 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
David Blaikie4d923012017-01-31 21:28:19 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // WithCodegen
Douglas Gregor69021972011-11-30 17:33:56 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002629 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002630
David Blaikieb44f0bf2017-01-04 22:36:43 +00002631 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002632 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002634 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002635
David Blaikieb44f0bf2017-01-04 22:36:43 +00002636 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002637 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002639 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002640
David Blaikieb44f0bf2017-01-04 22:36:43 +00002641 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002642 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002644 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002645
David Blaikieb44f0bf2017-01-04 22:36:43 +00002646 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002647 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2648 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002649 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002650
David Blaikieb44f0bf2017-01-04 22:36:43 +00002651 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002652 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002653 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002655 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002656
David Blaikieb44f0bf2017-01-04 22:36:43 +00002657 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002658 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002660 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002661
David Blaikieb44f0bf2017-01-04 22:36:43 +00002662 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002663 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002665 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002666
David Blaikieb44f0bf2017-01-04 22:36:43 +00002667 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002668 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002670 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002671
David Blaikieb44f0bf2017-01-04 22:36:43 +00002672 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002673 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2674 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002675 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002676
David Blaikieb44f0bf2017-01-04 22:36:43 +00002677 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002678 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002681 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002682
David Blaikieb44f0bf2017-01-04 22:36:43 +00002683 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002684 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2685 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002686 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002687
David Blaikieb44f0bf2017-01-04 22:36:43 +00002688 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002689 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2690 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2691 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002692 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002693
Douglas Gregor253eefe2011-12-01 00:59:36 +00002694 // Write the submodule metadata block.
Mehdi Amini57a41912015-09-10 01:46:39 +00002695 RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2696 FirstSubmoduleID -
2697 NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002698 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2699
Douglas Gregor69021972011-11-30 17:33:56 +00002700 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002701 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002702 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002703 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002704 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002705 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002706 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002707
2708 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002709 if (Mod->Parent) {
2710 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002711 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002712 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002713
2714 // Emit the definition of the block.
2715 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002716 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2717 ID,
2718 ParentID,
2719 Mod->IsFramework,
2720 Mod->IsExplicit,
2721 Mod->IsSystem,
2722 Mod->IsExternC,
2723 Mod->InferSubmodules,
2724 Mod->InferExplicitSubmodules,
2725 Mod->InferExportWildcard,
2726 Mod->ConfigMacrosExhaustive,
David Blaikie4d923012017-01-31 21:28:19 +00002727 Context->getLangOpts().ModularCodegen && WritingModule};
Mehdi Amini57a41912015-09-10 01:46:39 +00002728 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2729 }
2730
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002731 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002732 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002733 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002734 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002735 }
2736
Douglas Gregor69021972011-11-30 17:33:56 +00002737 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002738 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002739 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002740 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2741 UmbrellaHeader.NameAsWritten);
2742 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002743 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2744 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002745 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002746 }
Richard Smith306d8922014-10-22 23:50:56 +00002747
Douglas Gregor69021972011-11-30 17:33:56 +00002748 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002749 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002750 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002751 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002752 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002753 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002754 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2755 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2756 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002757 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002758 Module::HK_PrivateTextual},
2759 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002760 };
2761 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002762 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002763 for (auto &H : Mod->Headers[HL.HeaderKind])
2764 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2765 }
2766
2767 // Emit the top headers.
2768 {
2769 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002770 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002771 for (auto *H : TopHeaders)
2772 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002773 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002774
2775 // Emit the imports.
2776 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002777 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002778 for (auto *I : Mod->Imports)
2779 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002780 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2781 }
2782
Douglas Gregor24bb9232011-12-02 18:58:38 +00002783 // Emit the exports.
2784 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002785 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002786 for (const auto &E : Mod->Exports) {
2787 // FIXME: This may fail; we don't require that all exported modules
2788 // are local or imported.
2789 Record.push_back(getSubmoduleID(E.getPointer()));
2790 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002791 }
2792 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2793 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002794
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002795 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2796 // Might be unnecessary as use declarations are only used to build the
2797 // module itself.
2798
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002799 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002800 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002801 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2802 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002803 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002804 }
2805
Douglas Gregorfb912652013-03-20 21:10:35 +00002806 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002807 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002808 // FIXME: This may fail; we don't require that all conflicting modules
2809 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002810 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2811 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002812 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002813 }
2814
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002815 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002816 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002817 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002818 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002819 }
2820
Richard Smithdc1f0422016-07-20 19:10:16 +00002821 // Emit the initializers, if any.
2822 RecordData Inits;
2823 for (Decl *D : Context->getModuleInitializers(Mod))
2824 Inits.push_back(GetDeclRef(D));
2825 if (!Inits.empty())
2826 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2827
Douglas Gregor69021972011-11-30 17:33:56 +00002828 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002829 for (auto *M : Mod->submodules())
2830 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002831 }
2832
2833 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002834
Richard Smith23d8d032015-05-14 00:45:20 +00002835 assert((NextSubmoduleID - FirstSubmoduleID ==
2836 getNumberOfModules(WritingModule)) &&
2837 "Wrong # of submodules; found a reference to a non-local, "
2838 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002839}
2840
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002841serialization::SubmoduleID
2842ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002843 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002844 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002845
2846 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002847 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002848 Module *OwningMod
2849 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002850 if (!OwningMod)
2851 return 0;
2852
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002853 // Check whether this submodule is part of our own module.
2854 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002855 return 0;
2856
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002857 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002858}
2859
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002860void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2861 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002862 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2863 DiagStateIDMap;
2864 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002865 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002866
Richard Smithd230de22017-01-26 01:01:01 +00002867 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2868 bool IncludeNonPragmaStates) {
2869 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002870 Record.push_back(DiagStateID);
Richard Smithd230de22017-01-26 01:01:01 +00002871
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002872 if (DiagStateID == 0) {
2873 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002874
2875 // Add a placeholder for the number of mappings.
2876 auto SizeIdx = Record.size();
2877 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00002878 for (const auto &I : *State) {
2879 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002880 Record.push_back(I.first);
2881 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002882 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002883 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002884 // Update the placeholder.
2885 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00002886 }
2887 };
2888
2889 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2890 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2891 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2892
2893 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2894 if (!FileIDAndFile.first.isValid() ||
2895 !FileIDAndFile.second.HasLocalTransitions)
2896 continue;
2897 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
2898 Record);
2899 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2900 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2901 Record.push_back(StatePoint.Offset);
2902 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002903 }
2904 }
2905
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002906 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002907 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002908}
2909
Douglas Gregorc5046832009-04-27 18:38:38 +00002910//===----------------------------------------------------------------------===//
2911// Type Serialization
2912//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002913
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002914/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002915void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002916 TypeIdx &IdxRef = TypeIdxs[T];
2917 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2918 IdxRef = TypeIdx(NextTypeID++);
2919 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002920
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002921 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002922
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002923 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002924
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002925 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002926 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002927 W.Visit(T);
2928 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002929
Richard Smith290d8012016-04-06 17:06:00 +00002930 // Record the offset for this type.
2931 unsigned Index = Idx.getIndex() - FirstTypeID;
2932 if (TypeOffsets.size() == Index)
2933 TypeOffsets.push_back(Offset);
2934 else if (TypeOffsets.size() < Index) {
2935 TypeOffsets.resize(Index + 1);
2936 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002937 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002938 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002939 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002940}
2941
Douglas Gregorc5046832009-04-27 18:38:38 +00002942//===----------------------------------------------------------------------===//
2943// Declaration Serialization
2944//===----------------------------------------------------------------------===//
2945
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002946/// \brief Write the block containing all of the declaration IDs
2947/// lexically declared within the given DeclContext.
2948///
2949/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2950/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002951uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002952 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002953 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002954 return 0;
2955
Douglas Gregor8f45df52009-04-16 22:23:12 +00002956 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002957 SmallVector<uint32_t, 128> KindDeclPairs;
2958 for (const auto *D : DC->decls()) {
2959 KindDeclPairs.push_back(D->getKind());
2960 KindDeclPairs.push_back(GetDeclRef(D));
2961 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002962
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002963 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002964 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002965 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2966 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002967 return Offset;
2968}
2969
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002970void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002971 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002972
2973 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002974 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002975 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002979 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002980 {
2981 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2982 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2983 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2984 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002985
2986 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002987 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002988 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002992 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002993 {
2994 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2995 FirstDeclID - NUM_PREDEF_DECL_IDS};
2996 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2997 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002998}
2999
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003000void ASTWriter::WriteFileDeclIDsMap() {
3001 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003002
Chandler Carruthb306d732015-03-27 00:31:20 +00003003 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3004 FileDeclIDs.begin(), FileDeclIDs.end());
3005 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3006 llvm::less_first());
3007
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003008 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003009 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3010 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3011 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3012 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3013 for (auto &LocDeclEntry : Info.DeclIDs)
3014 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003015 }
3016
David Blaikieb44f0bf2017-01-04 22:36:43 +00003017 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003018 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003021 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003022 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3023 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003024 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003025}
3026
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003027void ASTWriter::WriteComments() {
3028 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003029 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003030 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003031 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003032 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003033 AddSourceRange(I->getSourceRange(), Record);
3034 Record.push_back(I->getKind());
3035 Record.push_back(I->isTrailingComment());
3036 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003037 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3038 }
3039 Stream.ExitBlock();
3040}
3041
Douglas Gregorc5046832009-04-27 18:38:38 +00003042//===----------------------------------------------------------------------===//
3043// Global Method Pool and Selector Serialization
3044//===----------------------------------------------------------------------===//
3045
Douglas Gregore84a9da2009-04-20 20:36:09 +00003046namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003047
Douglas Gregorc78d3462009-04-24 21:10:55 +00003048// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003049class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003050 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003051
3052public:
3053 typedef Selector key_type;
3054 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003055
Sebastian Redl834bb972010-08-04 17:20:04 +00003056 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003057 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003058 ObjCMethodList Instance, Factory;
3059 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003060 typedef const data_type& data_type_ref;
3061
Justin Bogner25463f12014-04-18 20:27:24 +00003062 typedef unsigned hash_value_type;
3063 typedef unsigned offset_type;
3064
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003065 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003066
Justin Bogner25463f12014-04-18 20:27:24 +00003067 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003068 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003069 }
Mike Stump11289f42009-09-09 15:08:12 +00003070
3071 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003072 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003073 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003074 using namespace llvm::support;
3075 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003076 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003077 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003078 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3079 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003080 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003081 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003082 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003083 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003084 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003085 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003086 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003087 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003088 return std::make_pair(KeyLen, DataLen);
3089 }
Mike Stump11289f42009-09-09 15:08:12 +00003090
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003091 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003092 using namespace llvm::support;
3093 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003094 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003095 assert((Start >> 32) == 0 && "Selector key offset too large");
3096 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003097 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003098 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003099 if (N == 0)
3100 N = 1;
3101 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003102 LE.write<uint32_t>(
3103 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003104 }
Mike Stump11289f42009-09-09 15:08:12 +00003105
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003106 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003107 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003108 using namespace llvm::support;
3109 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003110 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003111 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003112 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003113 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003114 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003115 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003116 ++NumInstanceMethods;
3117
3118 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003119 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003120 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003121 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003122 ++NumFactoryMethods;
3123
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003124 unsigned InstanceBits = Methods.Instance.getBits();
3125 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003126 unsigned InstanceHasMoreThanOneDeclBit =
3127 Methods.Instance.hasMoreThanOneDecl();
3128 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3129 (InstanceHasMoreThanOneDeclBit << 2) |
3130 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003131 unsigned FactoryBits = Methods.Factory.getBits();
3132 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003133 unsigned FactoryHasMoreThanOneDeclBit =
3134 Methods.Factory.hasMoreThanOneDecl();
3135 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3136 (FactoryHasMoreThanOneDeclBit << 2) |
3137 FactoryBits;
3138 LE.write<uint16_t>(FullInstanceBits);
3139 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003140 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003141 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003142 if (Method->getMethod())
3143 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003144 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003145 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003146 if (Method->getMethod())
3147 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003148
3149 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003150 }
3151};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003152
Douglas Gregorc78d3462009-04-24 21:10:55 +00003153} // end anonymous namespace
3154
Sebastian Redla19a67f2010-08-03 21:58:15 +00003155/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003156///
3157/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003158/// in an on-disk hash table indexed by the selector. The hash table also
3159/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003160void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003161 using namespace llvm;
3162
Sebastian Redla19a67f2010-08-03 21:58:15 +00003163 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003164 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003165 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003166 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003167 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003168 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003169 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003170 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003171
Sebastian Redla19a67f2010-08-03 21:58:15 +00003172 // Create the on-disk hash table representation. We walk through every
3173 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003174 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003175 for (auto &SelectorAndID : SelectorIDs) {
3176 Selector S = SelectorAndID.first;
3177 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003178 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003179 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003180 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003181 ObjCMethodList(),
3182 ObjCMethodList()
3183 };
3184 if (F != SemaRef.MethodPool.end()) {
3185 Data.Instance = F->second.first;
3186 Data.Factory = F->second.second;
3187 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003188 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003189 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003190 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003191 // Selector already exists. Did it change?
3192 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003193 for (ObjCMethodList *M = &Data.Instance;
3194 !changed && M && M->getMethod(); M = M->getNext()) {
3195 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003196 changed = true;
3197 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003198 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003199 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003200 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003201 changed = true;
3202 }
3203 if (!changed)
3204 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003205 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003206 // A new method pool entry.
3207 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003208 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003209 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003210 }
3211
Douglas Gregorc78d3462009-04-24 21:10:55 +00003212 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003213 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003214 uint32_t BucketOffset;
3215 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003216 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003217 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003218 llvm::raw_svector_ostream Out(MethodPool);
3219 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003220 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003221 BucketOffset = Generator.Emit(Out, Trait);
3222 }
3223
3224 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003225 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003226 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003230 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003231
Douglas Gregor95c13f52009-04-25 17:48:32 +00003232 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003233 {
3234 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3235 NumTableEntries};
3236 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3237 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003238
3239 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003240 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003241 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003245 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003246
3247 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003248 {
3249 RecordData::value_type Record[] = {
3250 SELECTOR_OFFSETS, SelectorOffsets.size(),
3251 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3252 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3253 bytes(SelectorOffsets));
3254 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003255 }
3256}
3257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003258/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003259void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003260 using namespace llvm;
3261 if (SemaRef.ReferencedSelectors.empty())
3262 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003263
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003264 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003265 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003266
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003267 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003268 // very tricky to fix, and given that @selector shouldn't really appear in
3269 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003270 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3271 Selector Sel = SelectorAndLocation.first;
3272 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003273 Writer.AddSelectorRef(Sel);
3274 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003275 }
Richard Smithe64e7452016-04-18 21:54:58 +00003276 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003277}
3278
Douglas Gregorc5046832009-04-27 18:38:38 +00003279//===----------------------------------------------------------------------===//
3280// Identifier Table Serialization
3281//===----------------------------------------------------------------------===//
3282
Richard Smithb3761912015-02-05 23:08:52 +00003283/// Determine the declaration that should be put into the name lookup table to
3284/// represent the given declaration in this module. This is usually D itself,
3285/// but if D was imported and merged into a local declaration, we want the most
3286/// recent local declaration instead. The chosen declaration will be the most
3287/// recent declaration in any module that imports this one.
3288static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3289 NamedDecl *D) {
3290 if (!LangOpts.Modules || !D->isFromASTFile())
3291 return D;
3292
3293 if (Decl *Redecl = D->getPreviousDecl()) {
3294 // For Redeclarable decls, a prior declaration might be local.
3295 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003296 // If we find a local decl, we're done.
3297 if (!Redecl->isFromASTFile()) {
3298 // Exception: in very rare cases (for injected-class-names), not all
3299 // redeclarations are in the same semantic context. Skip ones in a
3300 // different context. They don't go in this lookup table at all.
3301 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3302 D->getDeclContext()->getRedeclContext()))
3303 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003304 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003305 }
3306
Richard Smithfe620d22015-03-05 23:24:12 +00003307 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003308 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003309 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003310 break;
3311 }
3312 } else if (Decl *First = D->getCanonicalDecl()) {
3313 // For Mergeable decls, the first decl might be local.
3314 if (!First->isFromASTFile())
3315 return cast<NamedDecl>(First);
3316 }
3317
3318 // All declarations are imported. Our most recent declaration will also be
3319 // the most recent one in anyone who imports us.
3320 return D;
3321}
3322
Douglas Gregorc78d3462009-04-24 21:10:55 +00003323namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003324
Richard Smithcf97cf62015-04-19 01:34:23 +00003325class ASTIdentifierTableTrait {
3326 ASTWriter &Writer;
3327 Preprocessor &PP;
3328 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003329 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003330 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003331 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003332
3333 /// \brief Determines whether this is an "interesting" identifier that needs a
3334 /// full IdentifierInfo structure written into the hash table. Notably, this
3335 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3336 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003337 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003338 if (MacroOffset ||
3339 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003340 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003341 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003342 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003343 return true;
3344
3345 return false;
3346 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003347
Douglas Gregore84a9da2009-04-20 20:36:09 +00003348public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003349 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003350 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003351
Sebastian Redl539c5062010-08-18 23:57:32 +00003352 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003353 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003354
Justin Bogner25463f12014-04-18 20:27:24 +00003355 typedef unsigned hash_value_type;
3356 typedef unsigned offset_type;
3357
Richard Smithd7329392015-04-21 21:46:32 +00003358 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003359 IdentifierResolver &IdResolver, bool IsModule,
3360 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003361 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003362 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3363 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003364
Richard Smithd79514e2016-02-05 19:03:40 +00003365 bool needDecls() const { return NeedDecls; }
3366
Justin Bogner25463f12014-04-18 20:27:24 +00003367 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003368 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003369 }
Mike Stump11289f42009-09-09 15:08:12 +00003370
Richard Smith33e0f7e2015-07-22 02:08:40 +00003371 bool isInterestingIdentifier(const IdentifierInfo *II) {
3372 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3373 return isInterestingIdentifier(II, MacroOffset);
3374 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003375
Richard Smith9c254182015-07-19 21:41:12 +00003376 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3377 return isInterestingIdentifier(II, 0);
3378 }
3379
Mike Stump11289f42009-09-09 15:08:12 +00003380 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003381 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003382 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003383 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003384 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3385 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003386 DataLen += 2; // 2 bytes for builtin ID
3387 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003388 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003389 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003390
Richard Smitha534a312015-07-21 23:54:07 +00003391 if (NeedDecls) {
3392 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3393 DEnd = IdResolver.end();
3394 D != DEnd; ++D)
3395 DataLen += 4;
3396 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003397 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003398 using namespace llvm::support;
3399 endian::Writer<little> LE(Out);
3400
Richard Smithdf8a8312015-03-13 04:05:01 +00003401 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003402 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003403 // We emit the key length after the data length so that every
3404 // string is preceded by a 16-bit length. This matches the PTH
3405 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003406 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003407 return std::make_pair(KeyLen, DataLen);
3408 }
Mike Stump11289f42009-09-09 15:08:12 +00003409
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003410 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003411 unsigned KeyLen) {
3412 // Record the location of the key data. This is used when generating
3413 // the mapping from persistent IDs to strings.
3414 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003415
3416 // Emit the offset of the key/data length information to the interesting
3417 // identifiers table if necessary.
3418 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3419 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3420
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003421 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003422 }
Mike Stump11289f42009-09-09 15:08:12 +00003423
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003424 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003425 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003426 using namespace llvm::support;
3427 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003428
Richard Smithd7329392015-04-21 21:46:32 +00003429 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3430 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003431 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003432 return;
3433 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003434
Justin Bognere1c147c2014-03-28 22:03:19 +00003435 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003436 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3437 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003438 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003439 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003440 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003441 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003442 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3443 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003444 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003445 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003446 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003447 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003448
Richard Smithd7329392015-04-21 21:46:32 +00003449 if (HadMacroDefinition)
3450 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003451
Richard Smitha534a312015-07-21 23:54:07 +00003452 if (NeedDecls) {
3453 // Emit the declaration IDs in reverse order, because the
3454 // IdentifierResolver provides the declarations as they would be
3455 // visible (e.g., the function "stat" would come before the struct
3456 // "stat"), but the ASTReader adds declarations to the end of the list
3457 // (so we need to see the struct "stat" before the function "stat").
3458 // Only emit declarations that aren't from a chained PCH, though.
3459 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3460 IdResolver.end());
3461 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3462 DEnd = Decls.rend();
3463 D != DEnd; ++D)
3464 LE.write<uint32_t>(
3465 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3466 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003467 }
3468};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003469
Douglas Gregore84a9da2009-04-20 20:36:09 +00003470} // end anonymous namespace
3471
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003472/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003473///
3474/// The identifier table consists of a blob containing string data
3475/// (the actual identifiers themselves) and a separate "offsets" index
3476/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003477void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3478 IdentifierResolver &IdResolver,
3479 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003480 using namespace llvm;
3481
Richard Smith33e0f7e2015-07-22 02:08:40 +00003482 RecordData InterestingIdents;
3483
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003484 // Create and write out the blob that contains the identifier
3485 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003486 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003487 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003488 ASTIdentifierTableTrait Trait(
3489 *this, PP, IdResolver, IsModule,
3490 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003491
Douglas Gregore6648fb2009-04-28 20:33:11 +00003492 // Look for any identifiers that were named while processing the
3493 // headers, but are otherwise not needed. We add these to the hash
3494 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003495 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003496 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003497 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003498 for (const auto &ID : PP.getIdentifierTable())
3499 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003500 // Sort the identifiers lexicographically before getting them references so
3501 // that their order is stable.
3502 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3503 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003504 if (Trait.isInterestingNonMacroIdentifier(II))
3505 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003506
Sebastian Redlff4a2952010-07-23 23:49:55 +00003507 // Create the on-disk hash table representation. We only store offsets
3508 // for identifiers that appear here for the first time.
3509 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003510 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003511 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003512 IdentID ID = IdentIDPair.second;
3513 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003514 // Write out identifiers if either the ID is local or the identifier has
3515 // changed since it was loaded.
3516 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3517 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003518 (Trait.needDecls() &&
3519 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003520 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003521 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003522
Douglas Gregore84a9da2009-04-20 20:36:09 +00003523 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003524 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003525 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003526 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003527 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003528 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003529 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003530 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003531 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003532 }
3533
3534 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003535 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003536 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003539 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003540
3541 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003542 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003543 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003544 }
3545
3546 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003547 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003548 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003552 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003553
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003554#ifndef NDEBUG
3555 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3556 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3557#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003558
3559 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3560 IdentifierOffsets.size(),
3561 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003562 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003563 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003564
3565 // In C++, write the list of interesting identifiers (those that are
3566 // defined as macros, poisoned, or similar unusual things).
3567 if (!InterestingIdents.empty())
3568 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003569}
3570
Douglas Gregorc5046832009-04-27 18:38:38 +00003571//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003572// DeclContext's Name Lookup Table Serialization
3573//===----------------------------------------------------------------------===//
3574
3575namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003576
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003577// Trait used for the on-disk hash table used in the method pool.
3578class ASTDeclContextNameLookupTrait {
3579 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003580 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003581
3582public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003583 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003584 typedef key_type key_type_ref;
3585
Richard Smithd88a7f12015-09-01 20:35:42 +00003586 /// A start and end index into DeclIDs, representing a sequence of decls.
3587 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003588 typedef const data_type& data_type_ref;
3589
Justin Bogner25463f12014-04-18 20:27:24 +00003590 typedef unsigned hash_value_type;
3591 typedef unsigned offset_type;
3592
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003593 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3594
Richard Smithd88a7f12015-09-01 20:35:42 +00003595 template<typename Coll>
3596 data_type getData(const Coll &Decls) {
3597 unsigned Start = DeclIDs.size();
3598 for (NamedDecl *D : Decls) {
3599 DeclIDs.push_back(
3600 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3601 }
3602 return std::make_pair(Start, DeclIDs.size());
3603 }
3604
3605 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3606 unsigned Start = DeclIDs.size();
3607 for (auto ID : FromReader)
3608 DeclIDs.push_back(ID);
3609 return std::make_pair(Start, DeclIDs.size());
3610 }
3611
3612 static bool EqualKey(key_type_ref a, key_type_ref b) {
3613 return a == b;
3614 }
3615
Richard Smitha06c7e62015-08-26 23:55:49 +00003616 hash_value_type ComputeHash(DeclarationNameKey Name) {
3617 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003618 }
3619
Richard Smithd88a7f12015-09-01 20:35:42 +00003620 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3621 assert(Writer.hasChain() &&
3622 "have reference to loaded module file but no chain?");
3623
3624 using namespace llvm::support;
3625 endian::Writer<little>(Out)
3626 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3627 }
3628
Richard Smitha06c7e62015-08-26 23:55:49 +00003629 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3630 DeclarationNameKey Name,
3631 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003632 using namespace llvm::support;
3633 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003634 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003635 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003636 case DeclarationName::Identifier:
3637 case DeclarationName::ObjCZeroArgSelector:
3638 case DeclarationName::ObjCOneArgSelector:
3639 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003640 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003641 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003642 KeyLen += 4;
3643 break;
3644 case DeclarationName::CXXOperatorName:
3645 KeyLen += 1;
3646 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003647 case DeclarationName::CXXConstructorName:
3648 case DeclarationName::CXXDestructorName:
3649 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003650 case DeclarationName::CXXUsingDirective:
3651 break;
3652 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003653 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003654
Richard Smithf02662d2015-07-30 03:17:16 +00003655 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003656 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3657 assert(uint16_t(DataLen) == DataLen &&
3658 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003659 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003660
3661 return std::make_pair(KeyLen, DataLen);
3662 }
3663
Richard Smitha06c7e62015-08-26 23:55:49 +00003664 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003665 using namespace llvm::support;
3666 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003667 LE.write<uint8_t>(Name.getKind());
3668 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003669 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003670 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003671 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003672 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003673 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003674 case DeclarationName::ObjCZeroArgSelector:
3675 case DeclarationName::ObjCOneArgSelector:
3676 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003677 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003678 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003679 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003680 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003681 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003682 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003683 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003684 case DeclarationName::CXXConstructorName:
3685 case DeclarationName::CXXDestructorName:
3686 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003687 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003688 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003689 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003690
3691 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003692 }
3693
Richard Smitha06c7e62015-08-26 23:55:49 +00003694 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3695 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003696 using namespace llvm::support;
3697 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003698 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003699 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3700 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003701 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3702 }
3703};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003704
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003705} // end anonymous namespace
3706
Chandler Carruthe972c362015-03-26 03:11:40 +00003707bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3708 DeclContext *DC) {
3709 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3710}
3711
3712bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3713 DeclContext *DC) {
3714 for (auto *D : Result.getLookupResult())
3715 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3716 return false;
3717
3718 return true;
3719}
3720
Richard Smithd88a7f12015-09-01 20:35:42 +00003721void
Chandler Carruthe972c362015-03-26 03:11:40 +00003722ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003723 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003724 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3725 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003726 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003727
Chandler Carruthe972c362015-03-26 03:11:40 +00003728 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003729 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003730 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3731
3732 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003733 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3734 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003735 ASTDeclContextNameLookupTrait Trait(*this);
3736
Chandler Carruthe972c362015-03-26 03:11:40 +00003737 // The first step is to collect the declaration names which we need to
3738 // serialize into the name lookup table, and to collect them in a stable
3739 // order.
3740 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003741
Chandler Carruthe972c362015-03-26 03:11:40 +00003742 // We also build up small sets of the constructor and conversion function
3743 // names which are visible.
3744 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003745
Chandler Carruthe972c362015-03-26 03:11:40 +00003746 for (auto &Lookup : *DC->buildLookup()) {
3747 auto &Name = Lookup.first;
3748 auto &Result = Lookup.second;
3749
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003750 // If there are no local declarations in our lookup result, we
3751 // don't need to write an entry for the name at all. If we can't
3752 // write out a lookup set without performing more deserialization,
3753 // just skip this entry.
3754 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003755 isLookupResultEntirelyExternal(Result, DC))
3756 continue;
3757
3758 // We also skip empty results. If any of the results could be external and
3759 // the currently available results are empty, then all of the results are
3760 // external and we skip it above. So the only way we get here with an empty
3761 // results is when no results could have been external *and* we have
3762 // external results.
3763 //
3764 // FIXME: While we might want to start emitting on-disk entries for negative
3765 // lookups into a decl context as an optimization, today we *have* to skip
3766 // them because there are names with empty lookup results in decl contexts
3767 // which we can't emit in any stable ordering: we lookup constructors and
3768 // conversion functions in the enclosing namespace scope creating empty
3769 // results for them. This in almost certainly a bug in Clang's name lookup,
3770 // but that is likely to be hard or impossible to fix and so we tolerate it
3771 // here by omitting lookups with empty results.
3772 if (Lookup.second.getLookupResult().empty())
3773 continue;
3774
3775 switch (Lookup.first.getNameKind()) {
3776 default:
3777 Names.push_back(Lookup.first);
3778 break;
3779
Richard Smithcd45dbc2014-04-19 03:48:30 +00003780 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003781 assert(isa<CXXRecordDecl>(DC) &&
3782 "Cannot have a constructor name outside of a class!");
3783 ConstructorNameSet.insert(Name);
3784 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003785
3786 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003787 assert(isa<CXXRecordDecl>(DC) &&
3788 "Cannot have a conversion function name outside of a class!");
3789 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003790 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003791 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003792 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003793
Chandler Carruthe972c362015-03-26 03:11:40 +00003794 // Sort the names into a stable order.
3795 std::sort(Names.begin(), Names.end());
3796
Chandler Carruthffbf7052015-03-26 22:27:09 +00003797 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003798 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003799 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003800
Chandler Carruthffbf7052015-03-26 22:27:09 +00003801 // First we try the easy case by forming the current context's constructor
3802 // name and adding that name first. This is a very useful optimization to
3803 // avoid walking the lexical declarations in many cases, and it also
3804 // handles the only case where a constructor name can come from some other
3805 // lexical context -- when that name is an implicit constructor merged from
3806 // another declaration in the redecl chain. Any non-implicit constructor or
3807 // conversion function which doesn't occur in all the lexical contexts
3808 // would be an ODR violation.
3809 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3810 Context->getCanonicalType(Context->getRecordType(D)));
3811 if (ConstructorNameSet.erase(ImplicitCtorName))
3812 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003813
Chandler Carruthffbf7052015-03-26 22:27:09 +00003814 // If we still have constructors or conversion functions, we walk all the
3815 // names in the decl and add the constructors and conversion functions
3816 // which are visible in the order they lexically occur within the context.
3817 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3818 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3819 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3820 auto Name = ChildND->getDeclName();
3821 switch (Name.getNameKind()) {
3822 default:
3823 continue;
3824
3825 case DeclarationName::CXXConstructorName:
3826 if (ConstructorNameSet.erase(Name))
3827 Names.push_back(Name);
3828 break;
3829
3830 case DeclarationName::CXXConversionFunctionName:
3831 if (ConversionNameSet.erase(Name))
3832 Names.push_back(Name);
3833 break;
3834 }
3835
3836 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3837 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003838 }
3839
Chandler Carruthe972c362015-03-26 03:11:40 +00003840 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3841 "constructors by walking all the "
3842 "lexical members of the context.");
3843 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3844 "conversion functions by walking all "
3845 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003846 }
3847
Chandler Carruthe972c362015-03-26 03:11:40 +00003848 // Next we need to do a lookup with each name into this decl context to fully
3849 // populate any results from external sources. We don't actually use the
3850 // results of these lookups because we only want to use the results after all
3851 // results have been loaded and the pointers into them will be stable.
3852 for (auto &Name : Names)
3853 DC->lookup(Name);
3854
3855 // Now we need to insert the results for each name into the hash table. For
3856 // constructor names and conversion function names, we actually need to merge
3857 // all of the results for them into one list of results each and insert
3858 // those.
3859 SmallVector<NamedDecl *, 8> ConstructorDecls;
3860 SmallVector<NamedDecl *, 8> ConversionDecls;
3861
3862 // Now loop over the names, either inserting them or appending for the two
3863 // special cases.
3864 for (auto &Name : Names) {
3865 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3866
3867 switch (Name.getNameKind()) {
3868 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003869 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003870 break;
3871
3872 case DeclarationName::CXXConstructorName:
3873 ConstructorDecls.append(Result.begin(), Result.end());
3874 break;
3875
3876 case DeclarationName::CXXConversionFunctionName:
3877 ConversionDecls.append(Result.begin(), Result.end());
3878 break;
3879 }
3880 }
3881
3882 // Handle our two special cases if we ended up having any. We arbitrarily use
3883 // the first declaration's name here because the name itself isn't part of
3884 // the key, only the kind of name is used.
3885 if (!ConstructorDecls.empty())
3886 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003887 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003888 if (!ConversionDecls.empty())
3889 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003890 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003891
Richard Smithd88a7f12015-09-01 20:35:42 +00003892 // Create the on-disk hash table. Also emit the existing imported and
3893 // merged table if there is one.
3894 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3895 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003896}
3897
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003898/// \brief Write the block containing all of the declaration IDs
3899/// visible from the given DeclContext.
3900///
3901/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003902/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003903uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3904 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003905 // If we imported a key declaration of this namespace, write the visible
3906 // lookup results as an update record for it rather than including them
3907 // on this declaration. We will only look at key declarations on reload.
3908 if (isa<NamespaceDecl>(DC) && Chain &&
3909 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3910 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003911 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003912 Prev = Prev->getPreviousDecl())
3913 if (!Prev->isFromASTFile())
3914 return 0;
3915
3916 // Note that we need to emit an update record for the primary context.
3917 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3918
3919 // Make sure all visible decls are written. They will be recorded later. We
3920 // do this using a side data structure so we can sort the names into
3921 // a deterministic order.
3922 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3923 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3924 LookupResults;
3925 if (Map) {
3926 LookupResults.reserve(Map->size());
3927 for (auto &Entry : *Map)
3928 LookupResults.push_back(
3929 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3930 }
3931
3932 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3933 for (auto &NameAndResult : LookupResults) {
3934 DeclarationName Name = NameAndResult.first;
3935 DeclContext::lookup_result Result = NameAndResult.second;
3936 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3937 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3938 // We have to work around a name lookup bug here where negative lookup
3939 // results for these names get cached in namespace lookup tables (these
3940 // names should never be looked up in a namespace).
3941 assert(Result.empty() && "Cannot have a constructor or conversion "
3942 "function name in a namespace!");
3943 continue;
3944 }
3945
3946 for (NamedDecl *ND : Result)
3947 if (!ND->isFromASTFile())
3948 GetDeclRef(ND);
3949 }
3950
3951 return 0;
3952 }
3953
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003954 if (DC->getPrimaryContext() != DC)
3955 return 0;
3956
Chandler Carruthe972c362015-03-26 03:11:40 +00003957 // Skip contexts which don't support name lookup.
3958 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003959 return 0;
3960
3961 // If not in C++, we perform name lookup for the translation unit via the
3962 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003963 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003964 return 0;
3965
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003966 // Serialize the contents of the mapping used for lookup. Note that,
3967 // although we have two very different code paths, the serialized
3968 // representation is the same for both cases: a declaration name,
3969 // followed by a size, followed by references to the visible
3970 // declarations that have that name.
3971 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003972 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003973 if (!Map || Map->empty())
3974 return 0;
3975
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003976 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003977 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003978 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003979
3980 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003981 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003982 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003983 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003984 ++NumVisibleDeclContexts;
3985 return Offset;
3986}
3987
Sebastian Redla4071b42010-08-24 00:50:09 +00003988/// \brief Write an UPDATE_VISIBLE block for the given context.
3989///
3990/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3991/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003992/// (in C++), for namespaces, and for classes with forward-declared unscoped
3993/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003994void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003995 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003996 if (!Map || Map->empty())
3997 return;
3998
Sebastian Redla4071b42010-08-24 00:50:09 +00003999 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004000 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004001 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004002
Richard Smith5fc18a92015-07-12 23:43:21 +00004003 // If we're updating a namespace, select a key declaration as the key for the
4004 // update record; those are the only ones that will be checked on reload.
4005 if (isa<NamespaceDecl>(DC))
4006 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4007
Sebastian Redla4071b42010-08-24 00:50:09 +00004008 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004009 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004010 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004011}
4012
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004013/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4014void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00004015 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004016 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4017}
4018
4019/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4020void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004021 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004022 return;
4023
4024 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4025 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004026 for (const auto &I:Opts.OptMap) {
4027 AddString(I.getKey(), Record);
4028 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004029 Record.push_back(V.Supported ? 1 : 0);
4030 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004031 Record.push_back(V.Avail);
4032 Record.push_back(V.Core);
4033 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004034 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4035}
4036
Yaxun Liu5b746652016-12-18 05:18:55 +00004037void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4038 if (!SemaRef.Context.getLangOpts().OpenCL)
4039 return;
4040
4041 RecordData Record;
4042 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4043 Record.push_back(
4044 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4045 Record.push_back(I.second.size());
4046 for (auto Ext : I.second)
4047 AddString(Ext, Record);
4048 }
4049 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4050}
4051
4052void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4053 if (!SemaRef.Context.getLangOpts().OpenCL)
4054 return;
4055
4056 RecordData Record;
4057 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4058 Record.push_back(getDeclID(I.first));
4059 Record.push_back(static_cast<unsigned>(I.second.size()));
4060 for (auto Ext : I.second)
4061 AddString(Ext, Record);
4062 }
4063 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4064}
4065
Justin Lebar67a78a62016-10-08 22:15:58 +00004066void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4067 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4068 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4069 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4070 }
4071}
4072
Douglas Gregor404cdde2012-01-27 01:47:08 +00004073void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004074 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004075 RecordData Categories;
4076
4077 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4078 unsigned Size = 0;
4079 unsigned StartIndex = Categories.size();
4080
4081 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4082
4083 // Allocate space for the size.
4084 Categories.push_back(0);
4085
4086 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004087 for (ObjCInterfaceDecl::known_categories_iterator
4088 Cat = Class->known_categories_begin(),
4089 CatEnd = Class->known_categories_end();
4090 Cat != CatEnd; ++Cat, ++Size) {
4091 assert(getDeclID(*Cat) != 0 && "Bogus category");
4092 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004093 }
4094
4095 // Update the size.
4096 Categories[StartIndex] = Size;
4097
4098 // Record this interface -> category map.
4099 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4100 CategoriesMap.push_back(CatInfo);
4101 }
4102
4103 // Sort the categories map by the definition ID, since the reader will be
4104 // performing binary searches on this information.
4105 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4106
4107 // Emit the categories map.
4108 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004109
David Blaikieb44f0bf2017-01-04 22:36:43 +00004110 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004111 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004114 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004115
4116 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4117 Stream.EmitRecordWithBlob(AbbrevID, Record,
4118 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004119 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004120
Douglas Gregor404cdde2012-01-27 01:47:08 +00004121 // Emit the category lists.
4122 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4123}
4124
Richard Smithe40f2ba2013-08-07 21:41:30 +00004125void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4126 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4127
4128 if (LPTMap.empty())
4129 return;
4130
4131 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004132 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004133 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004134 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004135 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004136 AddDeclRef(LPT.D, Record);
4137 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004138
Justin Lebar28f09c52016-10-10 16:26:08 +00004139 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004140 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004141 }
4142 }
4143 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4144}
4145
Dario Domizioli13a0a382014-05-23 12:13:25 +00004146/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4147void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4148 RecordData Record;
4149 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4150 AddSourceLocation(PragmaLoc, Record);
4151 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4152}
4153
Nico Weber779355f2016-03-02 23:22:00 +00004154/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4155void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4156 RecordData Record;
4157 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4158 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4159}
4160
Nico Weber42932312016-03-03 00:17:35 +00004161/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4162//module.
4163void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4164 RecordData Record;
4165 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4166 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4167 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4168}
4169
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004170void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4171 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004172 // Enter the extension block.
4173 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4174
4175 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004176 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004177 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4178 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4179 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4180 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4181 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4182 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004183 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004184
4185 // Emit the metadata record.
4186 RecordData Record;
4187 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4188 Record.push_back(EXTENSION_METADATA);
4189 Record.push_back(Metadata.MajorVersion);
4190 Record.push_back(Metadata.MinorVersion);
4191 Record.push_back(Metadata.BlockName.size());
4192 Record.push_back(Metadata.UserInfo.size());
4193 SmallString<64> Buffer;
4194 Buffer += Metadata.BlockName;
4195 Buffer += Metadata.UserInfo;
4196 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4197
4198 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004199 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004200
4201 // Exit the extension block.
4202 Stream.ExitBlock();
4203}
4204
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004205//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004206// General Serialization Routines
4207//===----------------------------------------------------------------------===//
4208
Richard Smith69c82bf2016-04-01 22:52:03 +00004209/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004210void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4211 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004212 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004213 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004214 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004215 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004216
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004217#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004218
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004219 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004220}
4221
John McCallf413f5e2013-05-03 00:10:13 +00004222void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4223 AddSourceLocation(Tok.getLocation(), Record);
4224 Record.push_back(Tok.getLength());
4225
4226 // FIXME: When reading literal tokens, reconstruct the literal pointer
4227 // if it is needed.
4228 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4229 // FIXME: Should translate token kind to a stable encoding.
4230 Record.push_back(Tok.getKind());
4231 // FIXME: Should translate token flags to a stable encoding.
4232 Record.push_back(Tok.getFlags());
4233}
4234
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004235void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004236 Record.push_back(Str.size());
4237 Record.insert(Record.end(), Str.begin(), Str.end());
4238}
4239
Richard Smith7ed1bc92014-12-05 22:42:13 +00004240bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004241 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004242
Richard Smith54cc3c22014-12-11 20:50:24 +00004243 bool Changed =
4244 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004245
4246 // Remove a prefix to make the path relative, if relevant.
4247 const char *PathBegin = Path.data();
4248 const char *PathPtr =
4249 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4250 if (PathPtr != PathBegin) {
4251 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4252 Changed = true;
4253 }
4254
4255 return Changed;
4256}
4257
4258void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4259 SmallString<128> FilePath(Path);
4260 PreparePathForOutput(FilePath);
4261 AddString(FilePath, Record);
4262}
4263
Mehdi Amini57a41912015-09-10 01:46:39 +00004264void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004265 StringRef Path) {
4266 SmallString<128> FilePath(Path);
4267 PreparePathForOutput(FilePath);
4268 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4269}
4270
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004271void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4272 RecordDataImpl &Record) {
4273 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004274 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004275 Record.push_back(*Minor + 1);
4276 else
4277 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004278 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004279 Record.push_back(*Subminor + 1);
4280 else
4281 Record.push_back(0);
4282}
4283
Douglas Gregore84a9da2009-04-20 20:36:09 +00004284/// \brief Note that the identifier II occurs at the given offset
4285/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004286void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004287 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004288 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004289 // up earlier in the chain and thus don't need an offset.
4290 if (ID >= FirstIdentID)
4291 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004292}
4293
Douglas Gregor95c13f52009-04-25 17:48:32 +00004294/// \brief Note that the selector Sel occurs at the given offset
4295/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004296void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004297 unsigned ID = SelectorIDs[Sel];
4298 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004299 // Don't record offsets for selectors that are also available in a different
4300 // file.
4301 if (ID < FirstSelectorID)
4302 return;
4303 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004304}
4305
David Blaikie61137e12017-01-05 18:23:18 +00004306ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004307 SmallVectorImpl<char> &Buffer,
David Blaikie61137e12017-01-05 18:23:18 +00004308 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4309 bool IncludeTimestamps)
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004310 : Stream(Stream), Buffer(Buffer), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004311 for (const auto &Ext : Extensions) {
4312 if (auto Writer = Ext->createExtensionWriter(*this))
4313 ModuleFileExtensionWriters.push_back(std::move(Writer));
4314 }
4315}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004316
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004317ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004318 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004319}
4320
Richard Smithb3761912015-02-05 23:08:52 +00004321const LangOptions &ASTWriter::getLangOpts() const {
4322 assert(WritingAST && "can't determine lang opts when not writing AST");
4323 return Context->getLangOpts();
4324}
4325
Richard Smithe75ee0f2015-08-17 07:13:32 +00004326time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4327 return IncludeTimestamps ? E->getModificationTime() : 0;
4328}
4329
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004330ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4331 const std::string &OutputFile,
4332 Module *WritingModule, StringRef isysroot,
4333 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004334 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004335
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004336 ASTHasCompilerErrors = hasErrors;
4337
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004338 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004339 Stream.Emit((unsigned)'C', 8);
4340 Stream.Emit((unsigned)'P', 8);
4341 Stream.Emit((unsigned)'C', 8);
4342 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004343
Chris Lattner28fa4e62009-04-26 22:26:21 +00004344 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004345
Douglas Gregoreda8e122011-08-09 15:13:55 +00004346 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004347 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004348 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004349 ASTFileSignature Signature =
4350 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004351 Context = nullptr;
4352 PP = nullptr;
4353 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004354 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004355
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004356 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004357 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004358}
4359
Douglas Gregora94a1542011-07-27 21:45:57 +00004360template<typename Vector>
4361static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4362 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004363 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4364 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004365 Writer.AddDeclRef(*I, Record);
4366 }
4367}
4368
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004369ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4370 const std::string &OutputFile,
4371 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004372 using namespace llvm;
4373
Craig Toppera13603a2014-05-22 05:54:18 +00004374 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004375
Douglas Gregorcf68c582011-12-01 22:20:10 +00004376 // Make sure that the AST reader knows to finalize itself.
4377 if (Chain)
4378 Chain->finalizeForWriting();
4379
Sebastian Redl143413f2010-07-12 22:02:52 +00004380 ASTContext &Context = SemaRef.Context;
4381 Preprocessor &PP = SemaRef.PP;
4382
Douglas Gregordab42432011-08-12 00:15:20 +00004383 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004384 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4385 if (D) {
4386 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4387 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004388 }
4389 };
4390 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4391 PREDEF_DECL_TRANSLATION_UNIT_ID);
4392 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4393 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4394 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4395 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4396 PREDEF_DECL_OBJC_PROTOCOL_ID);
4397 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4398 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4399 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4400 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4401 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004402 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004403 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4404 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004405 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004406 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4407 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004408 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4409 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004410 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4411 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004412 RegisterPredefDecl(Context.TypePackElementDecl,
4413 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004414
Chris Lattner0c797362009-09-08 18:19:27 +00004415 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004416 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004417 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004418 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004419 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004420
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004421 // Build a record containing all of the file scoped decls in this file.
4422 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004423 if (!isModule)
4424 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4425 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004426
Douglas Gregor851443c2011-08-12 01:39:19 +00004427 // Build a record containing all of the delegating constructors we still need
4428 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004429 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004430 if (!isModule)
4431 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004432
Douglas Gregor851443c2011-08-12 01:39:19 +00004433 // Write the set of weak, undeclared identifiers. We always write the
4434 // entire table, since later PCH files in a PCH chain are only interested in
4435 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004436 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004437 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4438 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4439 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4440 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4441 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4442 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4443 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004444 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004445
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004446 // Build a record containing all of the ext_vector declarations.
4447 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004448 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004449
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004450 // Build a record containing all of the VTable uses information.
4451 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004452 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004453 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4454 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4455 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4456 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4457 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004458 }
4459
Nico Weber72889432014-09-06 01:25:55 +00004460 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4461 RecordData UnusedLocalTypedefNameCandidates;
4462 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4463 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4464
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004465 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004466 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004467 for (const auto &I : SemaRef.PendingInstantiations) {
4468 AddDeclRef(I.first, PendingInstantiations);
4469 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004470 }
4471 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4472 "There are local ones at end of translation unit!");
4473
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004474 // Build a record containing some declaration references.
4475 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004476 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004477 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4478 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004479 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004480 }
4481
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004482 RecordData CUDASpecialDeclRefs;
4483 if (Context.getcudaConfigureCallDecl()) {
4484 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4485 }
4486
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004487 // Build a record containing all of the known namespaces.
4488 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004489 for (const auto &I : SemaRef.KnownNamespaces) {
4490 if (!I.second)
4491 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004492 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004493
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004494 // Build a record of all used, undefined objects that require definitions.
4495 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004496
4497 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004498 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004499 for (const auto &I : Undefined) {
4500 AddDeclRef(I.first, UndefinedButUsed);
4501 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004502 }
4503
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004504 // Build a record containing all delete-expressions that we would like to
4505 // analyze later in AST.
4506 RecordData DeleteExprsToAnalyze;
4507
4508 for (const auto &DeleteExprsInfo :
4509 SemaRef.getMismatchingDeleteExpressions()) {
4510 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4511 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4512 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4513 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4514 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4515 }
4516 }
4517
Douglas Gregor112b9072012-10-18 05:31:06 +00004518 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004519 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004520
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004521 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004522 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004523
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004524 // This is so that older clang versions, before the introduction
4525 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004526 {
4527 RecordData Record = {VERSION_MAJOR};
4528 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4529 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004530
Douglas Gregor851443c2011-08-12 01:39:19 +00004531 // Create a lexical update block containing all of the declarations in the
4532 // translation unit that do not come from other AST files.
4533 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004534 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4535 for (const auto *D : TU->noload_decls()) {
4536 if (!D->isFromASTFile()) {
4537 NewGlobalKindDeclPairs.push_back(D->getKind());
4538 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4539 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004540 }
4541
David Blaikieb44f0bf2017-01-04 22:36:43 +00004542 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004543 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4544 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004545 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004546 {
4547 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4548 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4549 bytes(NewGlobalKindDeclPairs));
4550 }
4551
Douglas Gregor851443c2011-08-12 01:39:19 +00004552 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004553 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004554 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4555 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004556 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004557 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004558 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004559
4560 // If we have any extern "C" names, write out a visible update for them.
4561 if (Context.ExternCContext)
4562 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004563
4564 // If the translation unit has an anonymous namespace, and we don't already
4565 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004566 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004567 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4568 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004569 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004570 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004571 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004572
Richard Smith5652c0f2014-03-21 01:48:23 +00004573 // Add update records for all mangling numbers and static local numbers.
4574 // These aren't really update records, but this is a convenient way of
4575 // tagging this rare extra data onto the declarations.
4576 for (const auto &Number : Context.MangleNumbers)
4577 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004578 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4579 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004580 for (const auto &Number : Context.StaticLocalNumbers)
4581 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004582 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4583 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004584
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004585 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004586 // an AST file, are registered for serialization. Likewise for template
4587 // specializations added to imported templates.
4588 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004589 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004590 }
4591
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004592 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004593 // serialization, if we're storing decls with identifiers.
4594 if (!WritingModule || !getLangOpts().CPlusPlus) {
4595 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004596 for (const auto &ID : PP.getIdentifierTable()) {
4597 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004598 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4599 IIs.push_back(II);
4600 }
4601 // Sort the identifiers to visit based on their name.
4602 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4603 for (const IdentifierInfo *II : IIs) {
4604 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4605 DEnd = SemaRef.IdResolver.end();
4606 D != DEnd; ++D) {
4607 GetDeclRef(*D);
4608 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004609 }
4610 }
4611
Manman Rena0f31a02016-04-29 19:04:05 +00004612 // For method pool in the module, if it contains an entry for a selector,
4613 // the entry should be complete, containing everything introduced by that
4614 // module and all modules it imports. It's possible that the entry is out of
4615 // date, so we need to pull in the new content here.
4616
4617 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4618 // safe, we copy all selectors out.
4619 llvm::SmallVector<Selector, 256> AllSelectors;
4620 for (auto &SelectorAndID : SelectorIDs)
4621 AllSelectors.push_back(SelectorAndID.first);
4622 for (auto &Selector : AllSelectors)
4623 SemaRef.updateOutOfDateSelector(Selector);
4624
Douglas Gregor5204bde2011-08-02 16:26:37 +00004625 // Form the record of special types.
4626 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004627 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004628 AddTypeRef(Context.getFILEType(), SpecialTypes);
4629 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4630 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4631 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4632 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004633 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004634 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004635
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004636 if (Chain) {
4637 // Write the mapping information describing our module dependencies and how
4638 // each of those modules were mapped into our own offset/ID space, so that
4639 // the reader can build the appropriate mapping to its own offset/ID space.
4640 // The map consists solely of a blob with the following format:
4641 // *(module-name-len:i16 module-name:len*i8
4642 // source-location-offset:i32
4643 // identifier-id:i32
4644 // preprocessed-entity-id:i32
4645 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004646 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004647 // selector-id:i32
4648 // declaration-id:i32
4649 // c++-base-specifiers-id:i32
4650 // type-id:i32)
4651 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004652 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004653 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004655 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004656 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004657 {
4658 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004659 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004660 using namespace llvm::support;
4661 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004662 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004663 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004664 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004665
Ben Langmuir785180e2014-10-20 16:27:30 +00004666 // Note: if a base ID was uint max, it would not be possible to load
4667 // another module after it or have more than one entity inside it.
4668 uint32_t None = std::numeric_limits<uint32_t>::max();
4669
4670 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4671 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4672 if (ShouldWrite)
4673 LE.write<uint32_t>(BaseID);
4674 else
4675 LE.write<uint32_t>(None);
4676 };
4677
Ben Langmuirfe971d92014-08-16 04:54:18 +00004678 // These values should be unique within a chain, since they will be read
4679 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004680 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4681 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4682 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4683 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4684 M.NumPreprocessedEntities);
4685 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4686 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4687 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4688 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004689 }
4690 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004691 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004692 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4693 Buffer.data(), Buffer.size());
4694 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004695
Richard Smithb9eab6d2014-03-20 19:44:17 +00004696 RecordData DeclUpdatesOffsetsRecord;
4697
Richard Smith59442b42014-03-20 20:07:19 +00004698 // Keep writing types, declarations, and declaration update records
4699 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004700 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4701 WriteTypeAbbrevs();
4702 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004703 do {
4704 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4705 while (!DeclTypesToEmit.empty()) {
4706 DeclOrType DOT = DeclTypesToEmit.front();
4707 DeclTypesToEmit.pop();
4708 if (DOT.isType())
4709 WriteType(DOT.getType());
4710 else
4711 WriteDecl(Context, DOT.getDecl());
4712 }
4713 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004714 Stream.ExitBlock();
4715
Richard Smithb9eab6d2014-03-20 19:44:17 +00004716 DoneWritingDeclsAndTypes = true;
4717
4718 // These things can only be done once we've written out decls and types.
4719 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004720 if (!DeclUpdatesOffsetsRecord.empty())
4721 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004722 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004723 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004724 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004725 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004726 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004727 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004728 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004729 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004730 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004731 WriteFPPragmaOptions(SemaRef.getFPOptions());
4732 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004733 WriteOpenCLExtensionTypes(SemaRef);
4734 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004735 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004736
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004737 // If we're emitting a module, write out the submodule information.
4738 if (WritingModule)
4739 WriteSubmodules(WritingModule);
4740
Douglas Gregor5204bde2011-08-02 16:26:37 +00004741 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4742
Douglas Gregord4df8652009-04-22 22:02:47 +00004743 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004744 if (!EagerlyDeserializedDecls.empty())
4745 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004746
David Blaikie9ffe5a32017-01-30 05:00:26 +00004747 if (Context.getLangOpts().ModularCodegen)
4748 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4749
Douglas Gregord4df8652009-04-22 22:02:47 +00004750 // Write the record containing tentative definitions.
4751 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004752 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004753
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004754 // Write the record containing unused file scoped decls.
4755 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004756 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004757
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004758 // Write the record containing weak undeclared identifiers.
4759 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004760 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004761 WeakUndeclaredIdentifiers);
4762
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004763 // Write the record containing ext_vector type names.
4764 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004765 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004766
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004767 // Write the record containing VTable uses information.
4768 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004769 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004770
Nico Weber72889432014-09-06 01:25:55 +00004771 // Write the record containing potentially unused local typedefs.
4772 if (!UnusedLocalTypedefNameCandidates.empty())
4773 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4774 UnusedLocalTypedefNameCandidates);
4775
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004776 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004777 if (!PendingInstantiations.empty())
4778 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004779
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004780 // Write the record containing declaration references of Sema.
4781 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004782 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004783
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004784 // Write the record containing CUDA-specific declaration references.
4785 if (!CUDASpecialDeclRefs.empty())
4786 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004787
4788 // Write the delegating constructors.
4789 if (!DelegatingCtorDecls.empty())
4790 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004791
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004792 // Write the known namespaces.
4793 if (!KnownNamespaces.empty())
4794 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004795
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004796 // Write the undefined internal functions and variables, and inline functions.
4797 if (!UndefinedButUsed.empty())
4798 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004799
4800 if (!DeleteExprsToAnalyze.empty())
4801 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4802
Douglas Gregor851443c2011-08-12 01:39:19 +00004803 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004804 for (auto *DC : UpdatedDeclContexts)
4805 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004806
Douglas Gregor959bb062011-12-03 01:15:29 +00004807 if (!WritingModule) {
4808 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004809 struct ModuleInfo {
4810 uint64_t ID;
4811 Module *M;
4812 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4813 };
Richard Smith56be7542014-03-21 00:33:59 +00004814 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004815 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004816 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004817 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4818 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004819 }
Richard Smith56be7542014-03-21 00:33:59 +00004820
4821 if (!Imports.empty()) {
4822 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4823 return A.ID < B.ID;
4824 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004825 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4826 return A.ID == B.ID;
4827 };
Richard Smith56be7542014-03-21 00:33:59 +00004828
4829 // Sort and deduplicate module IDs.
4830 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004831 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004832 Imports.end());
4833
4834 RecordData ImportedModules;
4835 for (const auto &Import : Imports) {
4836 ImportedModules.push_back(Import.ID);
4837 // FIXME: If the module has macros imported then later has declarations
4838 // imported, this location won't be the right one as a location for the
4839 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004840 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004841 }
4842
Douglas Gregor959bb062011-12-03 01:15:29 +00004843 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4844 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004845 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004846
Douglas Gregor404cdde2012-01-27 01:47:08 +00004847 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004848 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004849 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004850 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004851 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004852 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004853
Douglas Gregor08f01292009-04-17 22:13:46 +00004854 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004855 RecordData::value_type Record[] = {
4856 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004857 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004858 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004859
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004860 // Write the module file extension blocks.
4861 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004862 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004863
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004864 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004865}
4866
Richard Smithb9eab6d2014-03-20 19:44:17 +00004867void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004868 if (DeclUpdates.empty())
4869 return;
4870
Richard Smith59442b42014-03-20 20:07:19 +00004871 DeclUpdateMap LocalUpdates;
4872 LocalUpdates.swap(DeclUpdates);
4873
Richard Smith6ef42932014-03-20 21:02:00 +00004874 for (auto &DeclUpdate : LocalUpdates) {
4875 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004876
Richard Smithd28ac5b2014-03-22 23:33:22 +00004877 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004878 RecordData RecordData;
4879 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004880 for (auto &Update : DeclUpdate.second) {
4881 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4882
Richard Smith69c82bf2016-04-01 22:52:03 +00004883 // An updated body is emitted last, so that the reader doesn't need
4884 // to skip over the lazy body to reach statements for other records.
4885 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4886 HasUpdatedBody = true;
4887 else
4888 Record.push_back(Kind);
4889
Richard Smith6ef42932014-03-20 21:02:00 +00004890 switch (Kind) {
4891 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4892 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4893 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004894 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004895 Record.push_back(GetDeclRef(Update.getDecl()));
4896 break;
4897
Richard Smith4d235792014-08-07 18:53:08 +00004898 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004899 break;
4900
Richard Smith4d235792014-08-07 18:53:08 +00004901 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004902 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004903 break;
4904
John McCall32791cc2016-01-06 22:34:54 +00004905 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004906 Record.AddStmt(const_cast<Expr *>(
4907 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004908 break;
4909
Richard Smith4b054b22016-08-24 21:25:37 +00004910 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4911 Record.AddStmt(
4912 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4913 break;
4914
Richard Smithcd45dbc2014-04-19 03:48:30 +00004915 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4916 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004917 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004918 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004919 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004920 *Context, const_cast<CXXRecordDecl *>(RD)));
4921
4922 // This state is sometimes updated by template instantiation, when we
4923 // switch from the specialization referring to the template declaration
4924 // to it referring to the template definition.
4925 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4926 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004927 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004928 } else {
4929 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4930 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004931 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004932
4933 // The instantiation might have been resolved to a partial
4934 // specialization. If so, record which one.
4935 auto From = Spec->getInstantiatedFrom();
4936 if (auto PartialSpec =
4937 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4938 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004939 Record.AddDeclRef(PartialSpec);
4940 Record.AddTemplateArgumentList(
4941 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004942 } else {
4943 Record.push_back(false);
4944 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004945 }
4946 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004947 Record.AddSourceLocation(RD->getLocation());
4948 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004949 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004950
4951 // Instantiation may change attributes; write them all out afresh.
4952 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004953 if (D->hasAttrs())
4954 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004955
4956 // FIXME: Ensure we don't get here for explicit instantiations.
4957 break;
4958 }
4959
Richard Smithf8134002015-03-10 01:41:22 +00004960 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004961 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004962 break;
4963
Richard Smith564417a2014-03-20 21:47:22 +00004964 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4965 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004966 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4967 Record);
4968 break;
4969
Richard Smith6ef42932014-03-20 21:02:00 +00004970 case UPD_CXX_DEDUCED_RETURN_TYPE:
4971 Record.push_back(GetOrCreateTypeID(Update.getType()));
4972 break;
4973
4974 case UPD_DECL_MARKED_USED:
4975 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004976
4977 case UPD_MANGLING_NUMBER:
4978 case UPD_STATIC_LOCAL_NUMBER:
4979 Record.push_back(Update.getNumber());
4980 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004981
Alexey Bataev97720002014-11-11 04:05:39 +00004982 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004983 Record.AddSourceRange(
4984 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004985 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004986
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004987 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4988 Record.AddSourceRange(
4989 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4990 break;
4991
Richard Smith65ebb4a2015-03-26 04:09:53 +00004992 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004993 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004994 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004995
4996 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004997 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004998 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004999 }
5000 }
5001
Richard Smithd28ac5b2014-03-22 23:33:22 +00005002 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005003 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005004 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005005 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005006 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005007 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005008 }
5009
Richard Smithcd45dbc2014-04-19 03:48:30 +00005010 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005011 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005012 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005013}
5014
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005015void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005016 uint32_t Raw = Loc.getRawEncoding();
5017 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005018}
5019
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005020void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005021 AddSourceLocation(Range.getBegin(), Record);
5022 AddSourceLocation(Range.getEnd(), Record);
5023}
5024
Richard Smithf144b542016-04-08 21:54:32 +00005025void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5026 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005027 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005028 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005029}
5030
Richard Smithf144b542016-04-08 21:54:32 +00005031void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5032 Record->push_back(Value.isUnsigned());
5033 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005034}
5035
Richard Smithf144b542016-04-08 21:54:32 +00005036void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5037 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005038}
5039
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005040void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005041 Record.push_back(getIdentifierRef(II));
5042}
5043
Sebastian Redl539c5062010-08-18 23:57:32 +00005044IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005045 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005046 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005047
Sebastian Redl539c5062010-08-18 23:57:32 +00005048 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005049 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005050 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005051 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005052}
5053
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005054MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005055 // Don't emit builtin macros like __LINE__ to the AST file unless they
5056 // have been redefined by the header (in which case they are not
5057 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005058 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005059 return 0;
5060
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005061 MacroID &ID = MacroIDs[MI];
5062 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005063 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005064 MacroInfoToEmitData Info = { Name, MI, ID };
5065 MacroInfosToEmit.push_back(Info);
5066 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005067 return ID;
5068}
5069
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005070MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005071 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005072 return 0;
5073
5074 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5075 return MacroIDs[MI];
5076}
5077
5078uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005079 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005080}
5081
Richard Smithe64e7452016-04-18 21:54:58 +00005082void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5083 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005084}
5085
Sebastian Redl539c5062010-08-18 23:57:32 +00005086SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005087 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005088 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005089 }
5090
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005091 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005092 if (SID == 0 && Chain) {
5093 // This might trigger a ReadSelector callback, which will set the ID for
5094 // this selector.
5095 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005096 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005097 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005098 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005099 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005100 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005101 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005102 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005103}
5104
Richard Smithe64e7452016-04-18 21:54:58 +00005105void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5106 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005107}
5108
Richard Smith290d8012016-04-06 17:06:00 +00005109void ASTRecordWriter::AddTemplateArgumentLocInfo(
5110 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005111 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005112 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005113 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005114 break;
5115 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005116 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005117 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005118 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005119 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5120 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005121 break;
5122 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005123 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5124 AddSourceLocation(Arg.getTemplateNameLoc());
5125 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005126 break;
John McCall0ad16662009-10-29 08:12:44 +00005127 case TemplateArgument::Null:
5128 case TemplateArgument::Integral:
5129 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005130 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005131 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005132 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005133 break;
5134 }
5135}
5136
Richard Smith290d8012016-04-06 17:06:00 +00005137void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5138 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005139
5140 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5141 bool InfoHasSameExpr
5142 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005143 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005144 if (InfoHasSameExpr)
5145 return; // Avoid storing the same expr twice.
5146 }
Richard Smith290d8012016-04-06 17:06:00 +00005147 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005148}
5149
Richard Smith290d8012016-04-06 17:06:00 +00005150void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005151 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005152 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005153 return;
5154 }
5155
Richard Smith290d8012016-04-06 17:06:00 +00005156 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005157}
5158
Richard Smith290d8012016-04-06 17:06:00 +00005159void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5160 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005161
Richard Smith290d8012016-04-06 17:06:00 +00005162 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005163 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005164 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005165}
5166
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005167void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005168 Record.push_back(GetOrCreateTypeID(T));
5169}
5170
Richard Smith2095ffe2015-03-16 20:11:03 +00005171TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005172 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005173 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5174 if (T.isNull())
5175 return TypeIdx();
5176 assert(!T.getLocalFastQualifiers());
5177
5178 TypeIdx &Idx = TypeIdxs[T];
5179 if (Idx.getIndex() == 0) {
5180 if (DoneWritingDeclsAndTypes) {
5181 assert(0 && "New type seen after serializing all the types to emit!");
5182 return TypeIdx();
5183 }
5184
5185 // We haven't seen this type before. Assign it a new ID and put it
5186 // into the queue of types to emit.
5187 Idx = TypeIdx(NextTypeID++);
5188 DeclTypesToEmit.push(T);
5189 }
5190 return Idx;
5191 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005192}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005193
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005194TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005195 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005196 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5197 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005198 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005199 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005200
Richard Smith2095ffe2015-03-16 20:11:03 +00005201 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5202 assert(I != TypeIdxs.end() && "Type not emitted!");
5203 return I->second;
5204 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005205}
5206
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005207void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005208 Record.push_back(GetDeclRef(D));
5209}
5210
Sebastian Redl539c5062010-08-18 23:57:32 +00005211DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005212 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005213
5214 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005215 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005216 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005217
5218 // If D comes from an AST file, its declaration ID is already known and
5219 // fixed.
5220 if (D->isFromASTFile())
5221 return D->getGlobalID();
5222
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005223 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005224 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005225 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005226 if (DoneWritingDeclsAndTypes) {
5227 assert(0 && "New decl seen after serializing all the decls to emit!");
5228 return 0;
5229 }
5230
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005231 // We haven't seen this declaration before. Give it a new ID and
5232 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005233 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005234 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005235 }
5236
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005237 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005238}
5239
Sebastian Redl539c5062010-08-18 23:57:32 +00005240DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005241 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005242 return 0;
5243
Douglas Gregorb3163e52012-01-05 22:33:30 +00005244 // If D comes from an AST file, its declaration ID is already known and
5245 // fixed.
5246 if (D->isFromASTFile())
5247 return D->getGlobalID();
5248
Douglas Gregore84a9da2009-04-20 20:36:09 +00005249 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5250 return DeclIDs[D];
5251}
5252
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005253void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005254 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005255 assert(D);
5256
5257 SourceLocation Loc = D->getLocation();
5258 if (Loc.isInvalid())
5259 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005260
5261 // We only keep track of the file-level declarations of each file.
5262 if (!D->getLexicalDeclContext()->isFileContext())
5263 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005264 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5265 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005266 if (isa<ParmVarDecl>(D))
5267 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005268
5269 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005270 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005271 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005272 FileID FID;
5273 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005274 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005275 if (FID.isInvalid())
5276 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005277 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005278
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005279 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005280 if (!Info)
5281 Info = new DeclIDInFileInfo();
5282
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005283 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005284 LocDeclIDsTy &Decls = Info->DeclIDs;
5285
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005286 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005287 Decls.push_back(LocDecl);
5288 return;
5289 }
5290
Benjamin Kramer45025c02013-08-24 13:22:59 +00005291 LocDeclIDsTy::iterator I =
5292 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005293
5294 Decls.insert(I, LocDecl);
5295}
5296
Richard Smithe64e7452016-04-18 21:54:58 +00005297void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005298 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005299 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005300 switch (Name.getNameKind()) {
5301 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005302 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005303 break;
5304
5305 case DeclarationName::ObjCZeroArgSelector:
5306 case DeclarationName::ObjCOneArgSelector:
5307 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005308 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005309 break;
5310
5311 case DeclarationName::CXXConstructorName:
5312 case DeclarationName::CXXDestructorName:
5313 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005314 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005315 break;
5316
Richard Smith35845152017-02-07 01:37:30 +00005317 case DeclarationName::CXXDeductionGuideName:
5318 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5319 break;
5320
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005321 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005322 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005323 break;
5324
Alexis Hunt3d221f22009-11-29 07:34:05 +00005325 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005326 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005327 break;
5328
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005329 case DeclarationName::CXXUsingDirective:
5330 // No extra data to emit
5331 break;
5332 }
5333}
Chris Lattnerca025db2010-05-07 21:43:38 +00005334
Richard Smithd08aeb62014-08-28 01:33:39 +00005335unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5336 assert(needsAnonymousDeclarationNumber(D) &&
5337 "expected an anonymous declaration");
5338
5339 // Number the anonymous declarations within this context, if we've not
5340 // already done so.
5341 auto It = AnonymousDeclarationNumbers.find(D);
5342 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005343 auto *DC = D->getLexicalDeclContext();
5344 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5345 AnonymousDeclarationNumbers[ND] = Number;
5346 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005347
5348 It = AnonymousDeclarationNumbers.find(D);
5349 assert(It != AnonymousDeclarationNumbers.end() &&
5350 "declaration not found within its lexical context");
5351 }
5352
5353 return It->second;
5354}
5355
Richard Smith290d8012016-04-06 17:06:00 +00005356void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5357 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005358 switch (Name.getNameKind()) {
5359 case DeclarationName::CXXConstructorName:
5360 case DeclarationName::CXXDestructorName:
5361 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005362 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005363 break;
5364
5365 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005366 AddSourceLocation(SourceLocation::getFromRawEncoding(
5367 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005368 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005369 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005370 break;
5371
5372 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005373 AddSourceLocation(SourceLocation::getFromRawEncoding(
5374 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005375 break;
5376
5377 case DeclarationName::Identifier:
5378 case DeclarationName::ObjCZeroArgSelector:
5379 case DeclarationName::ObjCOneArgSelector:
5380 case DeclarationName::ObjCMultiArgSelector:
5381 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005382 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005383 break;
5384 }
5385}
5386
Richard Smith290d8012016-04-06 17:06:00 +00005387void ASTRecordWriter::AddDeclarationNameInfo(
5388 const DeclarationNameInfo &NameInfo) {
5389 AddDeclarationName(NameInfo.getName());
5390 AddSourceLocation(NameInfo.getLoc());
5391 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005392}
5393
Richard Smith290d8012016-04-06 17:06:00 +00005394void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5395 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5396 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005397 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005398 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005399}
5400
Richard Smithe64e7452016-04-18 21:54:58 +00005401void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005402 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005403 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005404 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005405
5406 // Push each of the NNS's onto a stack for serialization in reverse order.
5407 while (NNS) {
5408 NestedNames.push_back(NNS);
5409 NNS = NNS->getPrefix();
5410 }
5411
Richard Smithe64e7452016-04-18 21:54:58 +00005412 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005413 while(!NestedNames.empty()) {
5414 NNS = NestedNames.pop_back_val();
5415 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005416 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005417 switch (Kind) {
5418 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005419 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005420 break;
5421
5422 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005423 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005424 break;
5425
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005426 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005427 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005428 break;
5429
Chris Lattnerca025db2010-05-07 21:43:38 +00005430 case NestedNameSpecifier::TypeSpec:
5431 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005432 AddTypeRef(QualType(NNS->getAsType(), 0));
5433 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005434 break;
5435
5436 case NestedNameSpecifier::Global:
5437 // Don't need to write an associated value.
5438 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005439
5440 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005441 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005442 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005443 }
5444 }
5445}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005446
Richard Smith290d8012016-04-06 17:06:00 +00005447void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005448 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005449 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005450 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005451
5452 // Push each of the nested-name-specifiers's onto a stack for
5453 // serialization in reverse order.
5454 while (NNS) {
5455 NestedNames.push_back(NNS);
5456 NNS = NNS.getPrefix();
5457 }
5458
Richard Smith290d8012016-04-06 17:06:00 +00005459 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005460 while(!NestedNames.empty()) {
5461 NNS = NestedNames.pop_back_val();
5462 NestedNameSpecifier::SpecifierKind Kind
5463 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005464 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005465 switch (Kind) {
5466 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005467 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5468 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005469 break;
5470
5471 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005472 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5473 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005474 break;
5475
5476 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005477 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5478 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005479 break;
5480
5481 case NestedNameSpecifier::TypeSpec:
5482 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005483 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5484 AddTypeLoc(NNS.getTypeLoc());
5485 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005486 break;
5487
5488 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005489 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005490 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005491
5492 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005493 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5494 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005495 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005496 }
5497 }
5498}
5499
Richard Smith290d8012016-04-06 17:06:00 +00005500void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005501 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005502 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005503 switch (Kind) {
5504 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005505 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005506 break;
5507
5508 case TemplateName::OverloadedTemplate: {
5509 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005510 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005511 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005512 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005513 break;
5514 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005515
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005516 case TemplateName::QualifiedTemplate: {
5517 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005518 AddNestedNameSpecifier(QualT->getQualifier());
5519 Record->push_back(QualT->hasTemplateKeyword());
5520 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005521 break;
5522 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005523
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005524 case TemplateName::DependentTemplate: {
5525 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005526 AddNestedNameSpecifier(DepT->getQualifier());
5527 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005528 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005529 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005530 else
Richard Smith290d8012016-04-06 17:06:00 +00005531 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005532 break;
5533 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005534
5535 case TemplateName::SubstTemplateTemplateParm: {
5536 SubstTemplateTemplateParmStorage *subst
5537 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005538 AddDeclRef(subst->getParameter());
5539 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005540 break;
5541 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005542
5543 case TemplateName::SubstTemplateTemplateParmPack: {
5544 SubstTemplateTemplateParmPackStorage *SubstPack
5545 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005546 AddDeclRef(SubstPack->getParameterPack());
5547 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005548 break;
5549 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005550 }
5551}
5552
Richard Smith290d8012016-04-06 17:06:00 +00005553void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5554 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005555 switch (Arg.getKind()) {
5556 case TemplateArgument::Null:
5557 break;
5558 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005559 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005560 break;
5561 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005562 AddDeclRef(Arg.getAsDecl());
5563 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005564 break;
5565 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005566 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005567 break;
5568 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005569 AddAPSInt(Arg.getAsIntegral());
5570 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005571 break;
5572 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005573 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005574 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005575 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005576 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005577 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005578 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005579 else
Richard Smith290d8012016-04-06 17:06:00 +00005580 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005581 break;
5582 case TemplateArgument::Expression:
5583 AddStmt(Arg.getAsExpr());
5584 break;
5585 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005586 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005587 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005588 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005589 break;
5590 }
5591}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005592
Richard Smithe64e7452016-04-18 21:54:58 +00005593void ASTRecordWriter::AddTemplateParameterList(
5594 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005595 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005596 AddSourceLocation(TemplateParams->getTemplateLoc());
5597 AddSourceLocation(TemplateParams->getLAngleLoc());
5598 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005599 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005600 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005601 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005602 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005603}
5604
5605/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005606void ASTRecordWriter::AddTemplateArgumentList(
5607 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005608 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005609 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005610 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005611 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005612}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005613
Richard Smith290d8012016-04-06 17:06:00 +00005614void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5615 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005616 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005617 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5618 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5619 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005620 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005621 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005622 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005623}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005624
Richard Smithe64e7452016-04-18 21:54:58 +00005625void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5626 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005627 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005628 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005629 AddDeclRef(I.getDecl());
5630 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005631 }
5632}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005633
Richard Smith290d8012016-04-06 17:06:00 +00005634// FIXME: Move this out of the main ASTRecordWriter interface.
5635void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5636 Record->push_back(Base.isVirtual());
5637 Record->push_back(Base.isBaseOfClass());
5638 Record->push_back(Base.getAccessSpecifierAsWritten());
5639 Record->push_back(Base.getInheritConstructors());
5640 AddTypeSourceInfo(Base.getTypeSourceInfo());
5641 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005642 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005643 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005644}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005645
Richard Smith645d2cf2016-04-14 00:29:55 +00005646static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5647 ArrayRef<CXXBaseSpecifier> Bases) {
5648 ASTWriter::RecordData Record;
5649 ASTRecordWriter Writer(W, Record);
5650 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005651
Richard Smith645d2cf2016-04-14 00:29:55 +00005652 for (auto &Base : Bases)
5653 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005654
Richard Smith645d2cf2016-04-14 00:29:55 +00005655 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005656}
5657
Richard Smith290d8012016-04-06 17:06:00 +00005658// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005659void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5660 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5661}
5662
Richard Smithaa165cf2016-04-13 21:57:08 +00005663static uint64_t
5664EmitCXXCtorInitializers(ASTWriter &W,
5665 ArrayRef<CXXCtorInitializer *> CtorInits) {
5666 ASTWriter::RecordData Record;
5667 ASTRecordWriter Writer(W, Record);
5668 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005669
Richard Smithaa165cf2016-04-13 21:57:08 +00005670 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005671 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005672 Writer.push_back(CTOR_INITIALIZER_BASE);
5673 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5674 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005675 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005676 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5677 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005678 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005679 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5680 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005681 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005682 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5683 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005684 }
Francois Pichetd583da02010-12-04 09:14:42 +00005685
Richard Smithaa165cf2016-04-13 21:57:08 +00005686 Writer.AddSourceLocation(Init->getMemberLocation());
5687 Writer.AddStmt(Init->getInit());
5688 Writer.AddSourceLocation(Init->getLParenLoc());
5689 Writer.AddSourceLocation(Init->getRParenLoc());
5690 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005691 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005692 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005693 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005694
5695 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005696}
5697
Richard Smithaa165cf2016-04-13 21:57:08 +00005698// FIXME: Move this out of the main ASTRecordWriter interface.
5699void ASTRecordWriter::AddCXXCtorInitializers(
5700 ArrayRef<CXXCtorInitializer *> CtorInits) {
5701 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005702}
5703
Richard Smith290d8012016-04-06 17:06:00 +00005704void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005705 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005706 Record->push_back(Data.IsLambda);
5707 Record->push_back(Data.UserDeclaredConstructor);
5708 Record->push_back(Data.UserDeclaredSpecialMembers);
5709 Record->push_back(Data.Aggregate);
5710 Record->push_back(Data.PlainOldData);
5711 Record->push_back(Data.Empty);
5712 Record->push_back(Data.Polymorphic);
5713 Record->push_back(Data.Abstract);
5714 Record->push_back(Data.IsStandardLayout);
5715 Record->push_back(Data.HasNoNonEmptyBases);
5716 Record->push_back(Data.HasPrivateFields);
5717 Record->push_back(Data.HasProtectedFields);
5718 Record->push_back(Data.HasPublicFields);
5719 Record->push_back(Data.HasMutableFields);
5720 Record->push_back(Data.HasVariantMembers);
5721 Record->push_back(Data.HasOnlyCMembers);
5722 Record->push_back(Data.HasInClassInitializer);
5723 Record->push_back(Data.HasUninitializedReferenceMember);
5724 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005725 Record->push_back(Data.HasInheritedConstructor);
5726 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005727 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5728 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5729 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5730 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5731 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5732 Record->push_back(Data.DefaultedDestructorIsDeleted);
5733 Record->push_back(Data.HasTrivialSpecialMembers);
5734 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5735 Record->push_back(Data.HasIrrelevantDestructor);
5736 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5737 Record->push_back(Data.HasDefaultedDefaultConstructor);
5738 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5739 Record->push_back(Data.HasConstexprDefaultConstructor);
5740 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5741 Record->push_back(Data.ComputedVisibleConversions);
5742 Record->push_back(Data.UserProvidedDefaultConstructor);
5743 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005744 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5745 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005746 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5747 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5748 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieub6adf542017-02-18 02:09:28 +00005749 Record->push_back(Data.ODRHash);
Richard Smith561fb152012-02-25 07:33:38 +00005750 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005751
Richard Smith290d8012016-04-06 17:06:00 +00005752 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005753 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005754 AddCXXBaseSpecifiers(Data.bases());
5755
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005756 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005757 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005758 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005759 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005760
Richard Smith290d8012016-04-06 17:06:00 +00005761 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5762 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005763 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005764 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005765
5766 // Add lambda-specific data.
5767 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005768 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005769 Record->push_back(Lambda.Dependent);
5770 Record->push_back(Lambda.IsGenericLambda);
5771 Record->push_back(Lambda.CaptureDefault);
5772 Record->push_back(Lambda.NumCaptures);
5773 Record->push_back(Lambda.NumExplicitCaptures);
5774 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005775 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005776 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005777 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005778 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005779 AddSourceLocation(Capture.getLocation());
5780 Record->push_back(Capture.isImplicit());
5781 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005782 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005783 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005784 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005785 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005786 break;
5787 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005788 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005789 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005790 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005791 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005792 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005793 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005794 break;
5795 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005796 }
5797 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005798}
5799
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005800void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005801 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005802 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005803 assert(FirstDeclID == NextDeclID &&
5804 FirstTypeID == NextTypeID &&
5805 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005806 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005807 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005808 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005809 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005810
Sebastian Redl07a89a82010-07-30 00:29:29 +00005811 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005812
Richard Smith7f330cd2015-03-18 01:42:29 +00005813 // Note, this will get called multiple times, once one the reader starts up
5814 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005815 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5816 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5817 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005818 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005819 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005820 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005821 NextDeclID = FirstDeclID;
5822 NextTypeID = FirstTypeID;
5823 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005824 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005825 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005826 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005827}
5828
Sebastian Redl539c5062010-08-18 23:57:32 +00005829void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005830 // Always keep the highest ID. See \p TypeRead() for more information.
5831 IdentID &StoredID = IdentifierIDs[II];
5832 if (ID > StoredID)
5833 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005834}
5835
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005836void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005837 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005838 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005839 if (ID > StoredID)
5840 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005841}
5842
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005843void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005844 // Always take the highest-numbered type index. This copes with an interesting
5845 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005846 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005847 // keep the higher-numbered entry so that we can properly write it out to
5848 // the AST file.
5849 TypeIdx &StoredIdx = TypeIdxs[T];
5850 if (Idx.getIndex() >= StoredIdx.getIndex())
5851 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005852}
5853
Sebastian Redl539c5062010-08-18 23:57:32 +00005854void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005855 // Always keep the highest ID. See \p TypeRead() for more information.
5856 SelectorID &StoredID = SelectorIDs[S];
5857 if (ID > StoredID)
5858 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005859}
Douglas Gregor91096292010-10-02 19:29:26 +00005860
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005861void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005862 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005863 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005864 MacroDefinitions[MD] = ID;
5865}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005866
Douglas Gregore37a85a2011-12-02 17:30:13 +00005867void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5868 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5869 SubmoduleIDs[Mod] = ID;
5870}
5871
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005872void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005873 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005874 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005875 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005876 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005877 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005878 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005879 // A forward reference was mutated into a definition. Rewrite it.
5880 // FIXME: This happens during template instantiation, should we
5881 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005882 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5883 "completed a tag from another module but not by instantiation?");
5884 DeclUpdates[RD].push_back(
5885 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005886 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005887 }
5888}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005889
Richard Smithf983f7f2015-10-13 00:23:25 +00005890static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5891 if (D->isFromASTFile())
5892 return true;
5893
Richard Smithf983f7f2015-10-13 00:23:25 +00005894 // The predefined __va_list_tag struct is imported if we imported any decls.
5895 // FIXME: This is a gross hack.
5896 return D == D->getASTContext().getVaListTagDecl();
5897}
5898
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005899void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005900 if (Chain && Chain->isProcessingUpdateRecords()) return;
5901 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005902 "Should not add lookup results to non-lookup contexts!");
5903
Vassil Vassilev632eac32016-03-16 11:17:04 +00005904 // TU is handled elsewhere.
5905 if (isa<TranslationUnitDecl>(DC))
5906 return;
5907
5908 // Namespaces are handled elsewhere, except for template instantiations of
5909 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5910 // local instantiations are added to an imported context. Only happens when
5911 // adding ADL lookup candidates, for example templated friends.
5912 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5913 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005914 return;
5915
Richard Smithf983f7f2015-10-13 00:23:25 +00005916 // We're only interested in cases where a local declaration is added to an
5917 // imported context.
5918 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5919 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005920
Richard Smith0f4e2c42015-08-06 04:23:48 +00005921 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005922 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005923 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005924 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5925 // We're adding a visible declaration to a predefined decl context. Ensure
5926 // that we write out all of its lookup results so we don't get a nasty
5927 // surprise when we try to emit its lookup table.
5928 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005929 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005930 }
Richard Smithc26d9742016-10-06 20:30:51 +00005931 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005932}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005933
5934void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005935 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005936 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005937
5938 // We're only interested in cases where a local declaration is added to an
5939 // imported context.
5940 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5941 return;
5942
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005943 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005944 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005945
5946 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005947 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005948 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005949 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005950}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005951
Richard Smith564417a2014-03-20 21:47:22 +00005952void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005953 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005954 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5955 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005956 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005957 // If we don't already know the exception specification for this redecl
5958 // chain, add an update record for it.
5959 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5960 ->getType()
5961 ->castAs<FunctionProtoType>()
5962 ->getExceptionSpecType()))
5963 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5964 });
Richard Smith564417a2014-03-20 21:47:22 +00005965}
5966
Richard Smith1fa5d642013-05-11 05:45:24 +00005967void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005968 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005969 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005970 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005971 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005972 DeclUpdates[D].push_back(
5973 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5974 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005975}
5976
Richard Smithf8134002015-03-10 01:41:22 +00005977void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5978 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005979 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005980 assert(!WritingAST && "Already writing the AST!");
5981 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005982 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005983 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005984 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5985 });
Richard Smithf8134002015-03-10 01:41:22 +00005986}
5987
Sebastian Redlab238a72011-04-24 16:28:06 +00005988void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005989 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005990 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005991 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005992 return; // Declaration not imported from PCH.
5993
Richard Smith4d235792014-08-07 18:53:08 +00005994 // Implicit function decl from a PCH was defined.
5995 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005996}
5997
Richard Smithd28ac5b2014-03-22 23:33:22 +00005998void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005999 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006000 assert(!WritingAST && "Already writing the AST!");
6001 if (!D->isFromASTFile())
6002 return;
6003
Richard Smith9e2341d2015-03-23 03:25:59 +00006004 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006005}
6006
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006007void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006008 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006009 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006010 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006011 return;
6012
6013 // Since the actual instantiation is delayed, this really means that we need
6014 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006015 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006016 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6017 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006018}
6019
John McCall32791cc2016-01-06 22:34:54 +00006020void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006021 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006022 assert(!WritingAST && "Already writing the AST!");
6023 if (!D->isFromASTFile())
6024 return;
6025
6026 DeclUpdates[D].push_back(
6027 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6028}
6029
Richard Smith4b054b22016-08-24 21:25:37 +00006030void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6031 assert(!WritingAST && "Already writing the AST!");
6032 if (!D->isFromASTFile())
6033 return;
6034
6035 DeclUpdates[D].push_back(
6036 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6037}
6038
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006039void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6040 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006041 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006042 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006043 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006044 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006045
6046 assert(IFD->getDefinition() && "Category on a class without a definition?");
6047 ObjCClassesWithCategories.insert(
6048 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006049}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006050
Eli Friedman276dd182013-09-05 00:02:25 +00006051void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006052 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006053 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006054
6055 // If there is *any* declaration of the entity that's not from an AST file,
6056 // we can skip writing the update record. We make sure that isUsed() triggers
6057 // completion of the redeclaration chain of the entity.
6058 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6059 if (IsLocalDecl(Prev))
6060 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006061
Aaron Ballman4f45b712014-03-21 15:22:56 +00006062 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006063}
Alexey Bataev97720002014-11-11 04:05:39 +00006064
6065void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006066 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006067 assert(!WritingAST && "Already writing the AST!");
6068 if (!D->isFromASTFile())
6069 return;
6070
6071 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6072}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006073
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006074void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6075 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006076 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006077 assert(!WritingAST && "Already writing the AST!");
6078 if (!D->isFromASTFile())
6079 return;
6080
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006081 DeclUpdates[D].push_back(
6082 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006083}
6084
Richard Smith4caa4492015-05-15 02:34:32 +00006085void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006086 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006087 assert(!WritingAST && "Already writing the AST!");
6088 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006089 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006090}
Alex Denisovfde64952015-06-26 05:28:36 +00006091
6092void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6093 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006094 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006095 assert(!WritingAST && "Already writing the AST!");
6096 if (!Record->isFromASTFile())
6097 return;
6098 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6099}
Richard Smithc26d9742016-10-06 20:30:51 +00006100
6101void ASTWriter::AddedCXXTemplateSpecialization(
6102 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6103 assert(!WritingAST && "Already writing the AST!");
6104
6105 if (!TD->getFirstDecl()->isFromASTFile())
6106 return;
6107 if (Chain && Chain->isProcessingUpdateRecords())
6108 return;
6109
6110 DeclsToEmitEvenIfUnreferenced.push_back(D);
6111}
6112
6113void ASTWriter::AddedCXXTemplateSpecialization(
6114 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6115 assert(!WritingAST && "Already writing the AST!");
6116
6117 if (!TD->getFirstDecl()->isFromASTFile())
6118 return;
6119 if (Chain && Chain->isProcessingUpdateRecords())
6120 return;
6121
6122 DeclsToEmitEvenIfUnreferenced.push_back(D);
6123}
6124
6125void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6126 const FunctionDecl *D) {
6127 assert(!WritingAST && "Already writing the AST!");
6128
6129 if (!TD->getFirstDecl()->isFromASTFile())
6130 return;
6131 if (Chain && Chain->isProcessingUpdateRecords())
6132 return;
6133
6134 DeclsToEmitEvenIfUnreferenced.push_back(D);
6135}