blob: b13a4e1ff4bdccd6341c0011b6fed6b951e4dd49 [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;
Richard Smithd230de22017-01-26 01:01:01 +00002874 for (const auto &I : *State) {
2875 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002876 Record.push_back(I.first);
2877 Record.push_back((unsigned)I.second.getSeverity());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002878 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002879 }
Richard Smithd230de22017-01-26 01:01:01 +00002880 // Add a sentinel to mark the end of the diag IDs.
2881 Record.push_back(unsigned(-1));
2882 }
2883 };
2884
2885 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
2886 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
2887 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
2888
2889 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2890 if (!FileIDAndFile.first.isValid() ||
2891 !FileIDAndFile.second.HasLocalTransitions)
2892 continue;
2893 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
2894 Record);
2895 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2896 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2897 Record.push_back(StatePoint.Offset);
2898 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002899 }
2900 }
2901
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002902 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002903 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002904}
2905
Douglas Gregorc5046832009-04-27 18:38:38 +00002906//===----------------------------------------------------------------------===//
2907// Type Serialization
2908//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002909
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002910/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002911void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00002912 TypeIdx &IdxRef = TypeIdxs[T];
2913 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
2914 IdxRef = TypeIdx(NextTypeID++);
2915 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00002916
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002917 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002918
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002919 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002920
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002921 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002922 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00002923 W.Visit(T);
2924 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00002925
Richard Smith290d8012016-04-06 17:06:00 +00002926 // Record the offset for this type.
2927 unsigned Index = Idx.getIndex() - FirstTypeID;
2928 if (TypeOffsets.size() == Index)
2929 TypeOffsets.push_back(Offset);
2930 else if (TypeOffsets.size() < Index) {
2931 TypeOffsets.resize(Index + 1);
2932 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00002933 } else {
Richard Smith290d8012016-04-06 17:06:00 +00002934 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002935 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002936}
2937
Douglas Gregorc5046832009-04-27 18:38:38 +00002938//===----------------------------------------------------------------------===//
2939// Declaration Serialization
2940//===----------------------------------------------------------------------===//
2941
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002942/// \brief Write the block containing all of the declaration IDs
2943/// lexically declared within the given DeclContext.
2944///
2945/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2946/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002947uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002948 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002949 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002950 return 0;
2951
Douglas Gregor8f45df52009-04-16 22:23:12 +00002952 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00002953 SmallVector<uint32_t, 128> KindDeclPairs;
2954 for (const auto *D : DC->decls()) {
2955 KindDeclPairs.push_back(D->getKind());
2956 KindDeclPairs.push_back(GetDeclRef(D));
2957 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002958
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002959 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00002960 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00002961 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2962 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002963 return Offset;
2964}
2965
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002966void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002967 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002968
2969 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002970 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002971 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002975 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002976 {
2977 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2978 FirstTypeID - NUM_PREDEF_TYPE_IDS};
2979 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2980 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002981
2982 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00002983 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002984 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002985 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002986 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002987 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00002988 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002989 {
2990 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2991 FirstDeclID - NUM_PREDEF_DECL_IDS};
2992 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2993 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002994}
2995
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002996void ASTWriter::WriteFileDeclIDsMap() {
2997 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002998
Chandler Carruthb306d732015-03-27 00:31:20 +00002999 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3000 FileDeclIDs.begin(), FileDeclIDs.end());
3001 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3002 llvm::less_first());
3003
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003004 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003005 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3006 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3007 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3008 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3009 for (auto &LocDeclEntry : Info.DeclIDs)
3010 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003011 }
3012
David Blaikieb44f0bf2017-01-04 22:36:43 +00003013 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003014 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003017 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003018 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3019 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003020 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003021}
3022
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003023void ASTWriter::WriteComments() {
3024 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003025 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003026 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003027 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003028 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003029 AddSourceRange(I->getSourceRange(), Record);
3030 Record.push_back(I->getKind());
3031 Record.push_back(I->isTrailingComment());
3032 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003033 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3034 }
3035 Stream.ExitBlock();
3036}
3037
Douglas Gregorc5046832009-04-27 18:38:38 +00003038//===----------------------------------------------------------------------===//
3039// Global Method Pool and Selector Serialization
3040//===----------------------------------------------------------------------===//
3041
Douglas Gregore84a9da2009-04-20 20:36:09 +00003042namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003043
Douglas Gregorc78d3462009-04-24 21:10:55 +00003044// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003045class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003046 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003047
3048public:
3049 typedef Selector key_type;
3050 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003051
Sebastian Redl834bb972010-08-04 17:20:04 +00003052 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003053 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003054 ObjCMethodList Instance, Factory;
3055 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003056 typedef const data_type& data_type_ref;
3057
Justin Bogner25463f12014-04-18 20:27:24 +00003058 typedef unsigned hash_value_type;
3059 typedef unsigned offset_type;
3060
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003061 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003062
Justin Bogner25463f12014-04-18 20:27:24 +00003063 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003064 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003065 }
Mike Stump11289f42009-09-09 15:08:12 +00003066
3067 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003068 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003069 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003070 using namespace llvm::support;
3071 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003072 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003073 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003074 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3075 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003076 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003077 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003078 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003079 for (const ObjCMethodList *Method = &Methods.Factory; 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;
Justin Bognere1c147c2014-03-28 22:03:19 +00003083 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003084 return std::make_pair(KeyLen, DataLen);
3085 }
Mike Stump11289f42009-09-09 15:08:12 +00003086
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003087 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003088 using namespace llvm::support;
3089 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003090 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003091 assert((Start >> 32) == 0 && "Selector key offset too large");
3092 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003093 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003094 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003095 if (N == 0)
3096 N = 1;
3097 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003098 LE.write<uint32_t>(
3099 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003100 }
Mike Stump11289f42009-09-09 15:08:12 +00003101
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003102 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003103 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003104 using namespace llvm::support;
3105 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003106 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003107 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003108 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003109 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003110 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003111 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003112 ++NumInstanceMethods;
3113
3114 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003115 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003116 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003117 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003118 ++NumFactoryMethods;
3119
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003120 unsigned InstanceBits = Methods.Instance.getBits();
3121 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003122 unsigned InstanceHasMoreThanOneDeclBit =
3123 Methods.Instance.hasMoreThanOneDecl();
3124 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3125 (InstanceHasMoreThanOneDeclBit << 2) |
3126 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003127 unsigned FactoryBits = Methods.Factory.getBits();
3128 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003129 unsigned FactoryHasMoreThanOneDeclBit =
3130 Methods.Factory.hasMoreThanOneDecl();
3131 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3132 (FactoryHasMoreThanOneDeclBit << 2) |
3133 FactoryBits;
3134 LE.write<uint16_t>(FullInstanceBits);
3135 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003136 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003137 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003138 if (Method->getMethod())
3139 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003140 for (const ObjCMethodList *Method = &Methods.Factory; 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()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003144
3145 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003146 }
3147};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003148
Douglas Gregorc78d3462009-04-24 21:10:55 +00003149} // end anonymous namespace
3150
Sebastian Redla19a67f2010-08-03 21:58:15 +00003151/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003152///
3153/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003154/// in an on-disk hash table indexed by the selector. The hash table also
3155/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003156void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003157 using namespace llvm;
3158
Sebastian Redla19a67f2010-08-03 21:58:15 +00003159 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003160 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003161 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003162 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003163 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003164 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003165 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003166 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003167
Sebastian Redla19a67f2010-08-03 21:58:15 +00003168 // Create the on-disk hash table representation. We walk through every
3169 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003170 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003171 for (auto &SelectorAndID : SelectorIDs) {
3172 Selector S = SelectorAndID.first;
3173 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003174 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003175 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003176 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003177 ObjCMethodList(),
3178 ObjCMethodList()
3179 };
3180 if (F != SemaRef.MethodPool.end()) {
3181 Data.Instance = F->second.first;
3182 Data.Factory = F->second.second;
3183 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003184 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003185 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003186 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003187 // Selector already exists. Did it change?
3188 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003189 for (ObjCMethodList *M = &Data.Instance;
3190 !changed && M && M->getMethod(); M = M->getNext()) {
3191 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003192 changed = true;
3193 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003194 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003195 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003196 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003197 changed = true;
3198 }
3199 if (!changed)
3200 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003201 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003202 // A new method pool entry.
3203 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003204 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003205 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003206 }
3207
Douglas Gregorc78d3462009-04-24 21:10:55 +00003208 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003209 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003210 uint32_t BucketOffset;
3211 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003212 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003213 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003214 llvm::raw_svector_ostream Out(MethodPool);
3215 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003216 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003217 BucketOffset = Generator.Emit(Out, Trait);
3218 }
3219
3220 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003221 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003222 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003226 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003227
Douglas Gregor95c13f52009-04-25 17:48:32 +00003228 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003229 {
3230 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3231 NumTableEntries};
3232 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3233 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003234
3235 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003236 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003237 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003241 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003242
3243 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003244 {
3245 RecordData::value_type Record[] = {
3246 SELECTOR_OFFSETS, SelectorOffsets.size(),
3247 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3248 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3249 bytes(SelectorOffsets));
3250 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003251 }
3252}
3253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003254/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003255void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003256 using namespace llvm;
3257 if (SemaRef.ReferencedSelectors.empty())
3258 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003259
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003260 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003261 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003262
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003263 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003264 // very tricky to fix, and given that @selector shouldn't really appear in
3265 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003266 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3267 Selector Sel = SelectorAndLocation.first;
3268 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003269 Writer.AddSelectorRef(Sel);
3270 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003271 }
Richard Smithe64e7452016-04-18 21:54:58 +00003272 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003273}
3274
Douglas Gregorc5046832009-04-27 18:38:38 +00003275//===----------------------------------------------------------------------===//
3276// Identifier Table Serialization
3277//===----------------------------------------------------------------------===//
3278
Richard Smithb3761912015-02-05 23:08:52 +00003279/// Determine the declaration that should be put into the name lookup table to
3280/// represent the given declaration in this module. This is usually D itself,
3281/// but if D was imported and merged into a local declaration, we want the most
3282/// recent local declaration instead. The chosen declaration will be the most
3283/// recent declaration in any module that imports this one.
3284static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3285 NamedDecl *D) {
3286 if (!LangOpts.Modules || !D->isFromASTFile())
3287 return D;
3288
3289 if (Decl *Redecl = D->getPreviousDecl()) {
3290 // For Redeclarable decls, a prior declaration might be local.
3291 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003292 // If we find a local decl, we're done.
3293 if (!Redecl->isFromASTFile()) {
3294 // Exception: in very rare cases (for injected-class-names), not all
3295 // redeclarations are in the same semantic context. Skip ones in a
3296 // different context. They don't go in this lookup table at all.
3297 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3298 D->getDeclContext()->getRedeclContext()))
3299 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003300 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003301 }
3302
Richard Smithfe620d22015-03-05 23:24:12 +00003303 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003304 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003305 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003306 break;
3307 }
3308 } else if (Decl *First = D->getCanonicalDecl()) {
3309 // For Mergeable decls, the first decl might be local.
3310 if (!First->isFromASTFile())
3311 return cast<NamedDecl>(First);
3312 }
3313
3314 // All declarations are imported. Our most recent declaration will also be
3315 // the most recent one in anyone who imports us.
3316 return D;
3317}
3318
Douglas Gregorc78d3462009-04-24 21:10:55 +00003319namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003320
Richard Smithcf97cf62015-04-19 01:34:23 +00003321class ASTIdentifierTableTrait {
3322 ASTWriter &Writer;
3323 Preprocessor &PP;
3324 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003325 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003326 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003327 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003328
3329 /// \brief Determines whether this is an "interesting" identifier that needs a
3330 /// full IdentifierInfo structure written into the hash table. Notably, this
3331 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3332 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003333 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003334 if (MacroOffset ||
3335 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003336 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003337 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003338 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003339 return true;
3340
3341 return false;
3342 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003343
Douglas Gregore84a9da2009-04-20 20:36:09 +00003344public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003345 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003346 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003347
Sebastian Redl539c5062010-08-18 23:57:32 +00003348 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003349 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003350
Justin Bogner25463f12014-04-18 20:27:24 +00003351 typedef unsigned hash_value_type;
3352 typedef unsigned offset_type;
3353
Richard Smithd7329392015-04-21 21:46:32 +00003354 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003355 IdentifierResolver &IdResolver, bool IsModule,
3356 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003357 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003358 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3359 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003360
Richard Smithd79514e2016-02-05 19:03:40 +00003361 bool needDecls() const { return NeedDecls; }
3362
Justin Bogner25463f12014-04-18 20:27:24 +00003363 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003364 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003365 }
Mike Stump11289f42009-09-09 15:08:12 +00003366
Richard Smith33e0f7e2015-07-22 02:08:40 +00003367 bool isInterestingIdentifier(const IdentifierInfo *II) {
3368 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3369 return isInterestingIdentifier(II, MacroOffset);
3370 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003371
Richard Smith9c254182015-07-19 21:41:12 +00003372 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3373 return isInterestingIdentifier(II, 0);
3374 }
3375
Mike Stump11289f42009-09-09 15:08:12 +00003376 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003377 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003378 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003379 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003380 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3381 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003382 DataLen += 2; // 2 bytes for builtin ID
3383 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003384 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003385 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003386
Richard Smitha534a312015-07-21 23:54:07 +00003387 if (NeedDecls) {
3388 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3389 DEnd = IdResolver.end();
3390 D != DEnd; ++D)
3391 DataLen += 4;
3392 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003393 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003394 using namespace llvm::support;
3395 endian::Writer<little> LE(Out);
3396
Richard Smithdf8a8312015-03-13 04:05:01 +00003397 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003398 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003399 // We emit the key length after the data length so that every
3400 // string is preceded by a 16-bit length. This matches the PTH
3401 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003402 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003403 return std::make_pair(KeyLen, DataLen);
3404 }
Mike Stump11289f42009-09-09 15:08:12 +00003405
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003406 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003407 unsigned KeyLen) {
3408 // Record the location of the key data. This is used when generating
3409 // the mapping from persistent IDs to strings.
3410 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003411
3412 // Emit the offset of the key/data length information to the interesting
3413 // identifiers table if necessary.
3414 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3415 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3416
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003417 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003418 }
Mike Stump11289f42009-09-09 15:08:12 +00003419
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003420 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003421 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003422 using namespace llvm::support;
3423 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003424
Richard Smithd7329392015-04-21 21:46:32 +00003425 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3426 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003427 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003428 return;
3429 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003430
Justin Bognere1c147c2014-03-28 22:03:19 +00003431 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003432 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3433 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003434 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003435 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003436 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003437 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003438 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3439 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003440 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003441 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003442 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003443 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003444
Richard Smithd7329392015-04-21 21:46:32 +00003445 if (HadMacroDefinition)
3446 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003447
Richard Smitha534a312015-07-21 23:54:07 +00003448 if (NeedDecls) {
3449 // Emit the declaration IDs in reverse order, because the
3450 // IdentifierResolver provides the declarations as they would be
3451 // visible (e.g., the function "stat" would come before the struct
3452 // "stat"), but the ASTReader adds declarations to the end of the list
3453 // (so we need to see the struct "stat" before the function "stat").
3454 // Only emit declarations that aren't from a chained PCH, though.
3455 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3456 IdResolver.end());
3457 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3458 DEnd = Decls.rend();
3459 D != DEnd; ++D)
3460 LE.write<uint32_t>(
3461 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3462 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003463 }
3464};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003465
Douglas Gregore84a9da2009-04-20 20:36:09 +00003466} // end anonymous namespace
3467
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003468/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003469///
3470/// The identifier table consists of a blob containing string data
3471/// (the actual identifiers themselves) and a separate "offsets" index
3472/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003473void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3474 IdentifierResolver &IdResolver,
3475 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003476 using namespace llvm;
3477
Richard Smith33e0f7e2015-07-22 02:08:40 +00003478 RecordData InterestingIdents;
3479
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003480 // Create and write out the blob that contains the identifier
3481 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003482 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003483 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003484 ASTIdentifierTableTrait Trait(
3485 *this, PP, IdResolver, IsModule,
3486 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003487
Douglas Gregore6648fb2009-04-28 20:33:11 +00003488 // Look for any identifiers that were named while processing the
3489 // headers, but are otherwise not needed. We add these to the hash
3490 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003491 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003492 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003493 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003494 for (const auto &ID : PP.getIdentifierTable())
3495 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003496 // Sort the identifiers lexicographically before getting them references so
3497 // that their order is stable.
3498 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3499 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003500 if (Trait.isInterestingNonMacroIdentifier(II))
3501 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003502
Sebastian Redlff4a2952010-07-23 23:49:55 +00003503 // Create the on-disk hash table representation. We only store offsets
3504 // for identifiers that appear here for the first time.
3505 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003506 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003507 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003508 IdentID ID = IdentIDPair.second;
3509 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003510 // Write out identifiers if either the ID is local or the identifier has
3511 // changed since it was loaded.
3512 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3513 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003514 (Trait.needDecls() &&
3515 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003516 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003517 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003518
Douglas Gregore84a9da2009-04-20 20:36:09 +00003519 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003520 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003521 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003522 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003523 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003524 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003525 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003526 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003527 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003528 }
3529
3530 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003531 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003532 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003535 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003536
3537 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003538 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003539 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003540 }
3541
3542 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003543 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003544 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003545 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003548 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003549
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003550#ifndef NDEBUG
3551 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3552 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3553#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003554
3555 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3556 IdentifierOffsets.size(),
3557 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003558 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003559 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003560
3561 // In C++, write the list of interesting identifiers (those that are
3562 // defined as macros, poisoned, or similar unusual things).
3563 if (!InterestingIdents.empty())
3564 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003565}
3566
Douglas Gregorc5046832009-04-27 18:38:38 +00003567//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003568// DeclContext's Name Lookup Table Serialization
3569//===----------------------------------------------------------------------===//
3570
3571namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003572
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003573// Trait used for the on-disk hash table used in the method pool.
3574class ASTDeclContextNameLookupTrait {
3575 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003576 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003577
3578public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003579 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003580 typedef key_type key_type_ref;
3581
Richard Smithd88a7f12015-09-01 20:35:42 +00003582 /// A start and end index into DeclIDs, representing a sequence of decls.
3583 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003584 typedef const data_type& data_type_ref;
3585
Justin Bogner25463f12014-04-18 20:27:24 +00003586 typedef unsigned hash_value_type;
3587 typedef unsigned offset_type;
3588
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003589 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3590
Richard Smithd88a7f12015-09-01 20:35:42 +00003591 template<typename Coll>
3592 data_type getData(const Coll &Decls) {
3593 unsigned Start = DeclIDs.size();
3594 for (NamedDecl *D : Decls) {
3595 DeclIDs.push_back(
3596 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3597 }
3598 return std::make_pair(Start, DeclIDs.size());
3599 }
3600
3601 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3602 unsigned Start = DeclIDs.size();
3603 for (auto ID : FromReader)
3604 DeclIDs.push_back(ID);
3605 return std::make_pair(Start, DeclIDs.size());
3606 }
3607
3608 static bool EqualKey(key_type_ref a, key_type_ref b) {
3609 return a == b;
3610 }
3611
Richard Smitha06c7e62015-08-26 23:55:49 +00003612 hash_value_type ComputeHash(DeclarationNameKey Name) {
3613 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003614 }
3615
Richard Smithd88a7f12015-09-01 20:35:42 +00003616 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3617 assert(Writer.hasChain() &&
3618 "have reference to loaded module file but no chain?");
3619
3620 using namespace llvm::support;
3621 endian::Writer<little>(Out)
3622 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3623 }
3624
Richard Smitha06c7e62015-08-26 23:55:49 +00003625 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3626 DeclarationNameKey Name,
3627 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003628 using namespace llvm::support;
3629 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003630 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003631 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003632 case DeclarationName::Identifier:
3633 case DeclarationName::ObjCZeroArgSelector:
3634 case DeclarationName::ObjCOneArgSelector:
3635 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003636 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003637 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003638 KeyLen += 4;
3639 break;
3640 case DeclarationName::CXXOperatorName:
3641 KeyLen += 1;
3642 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003643 case DeclarationName::CXXConstructorName:
3644 case DeclarationName::CXXDestructorName:
3645 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003646 case DeclarationName::CXXUsingDirective:
3647 break;
3648 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003649 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003650
Richard Smithf02662d2015-07-30 03:17:16 +00003651 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003652 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3653 assert(uint16_t(DataLen) == DataLen &&
3654 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003655 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003656
3657 return std::make_pair(KeyLen, DataLen);
3658 }
3659
Richard Smitha06c7e62015-08-26 23:55:49 +00003660 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003661 using namespace llvm::support;
3662 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003663 LE.write<uint8_t>(Name.getKind());
3664 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003665 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003666 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003667 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003668 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003669 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003670 case DeclarationName::ObjCZeroArgSelector:
3671 case DeclarationName::ObjCOneArgSelector:
3672 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003673 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003674 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003675 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003676 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003677 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003678 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003679 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003680 case DeclarationName::CXXConstructorName:
3681 case DeclarationName::CXXDestructorName:
3682 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003683 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003684 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003685 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003686
3687 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003688 }
3689
Richard Smitha06c7e62015-08-26 23:55:49 +00003690 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3691 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003692 using namespace llvm::support;
3693 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003694 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003695 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3696 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003697 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3698 }
3699};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003700
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003701} // end anonymous namespace
3702
Chandler Carruthe972c362015-03-26 03:11:40 +00003703bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3704 DeclContext *DC) {
3705 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3706}
3707
3708bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3709 DeclContext *DC) {
3710 for (auto *D : Result.getLookupResult())
3711 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3712 return false;
3713
3714 return true;
3715}
3716
Richard Smithd88a7f12015-09-01 20:35:42 +00003717void
Chandler Carruthe972c362015-03-26 03:11:40 +00003718ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003719 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003720 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3721 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003722 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003723
Chandler Carruthe972c362015-03-26 03:11:40 +00003724 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003725 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003726 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3727
3728 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003729 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3730 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003731 ASTDeclContextNameLookupTrait Trait(*this);
3732
Chandler Carruthe972c362015-03-26 03:11:40 +00003733 // The first step is to collect the declaration names which we need to
3734 // serialize into the name lookup table, and to collect them in a stable
3735 // order.
3736 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003737
Chandler Carruthe972c362015-03-26 03:11:40 +00003738 // We also build up small sets of the constructor and conversion function
3739 // names which are visible.
3740 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003741
Chandler Carruthe972c362015-03-26 03:11:40 +00003742 for (auto &Lookup : *DC->buildLookup()) {
3743 auto &Name = Lookup.first;
3744 auto &Result = Lookup.second;
3745
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003746 // If there are no local declarations in our lookup result, we
3747 // don't need to write an entry for the name at all. If we can't
3748 // write out a lookup set without performing more deserialization,
3749 // just skip this entry.
3750 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003751 isLookupResultEntirelyExternal(Result, DC))
3752 continue;
3753
3754 // We also skip empty results. If any of the results could be external and
3755 // the currently available results are empty, then all of the results are
3756 // external and we skip it above. So the only way we get here with an empty
3757 // results is when no results could have been external *and* we have
3758 // external results.
3759 //
3760 // FIXME: While we might want to start emitting on-disk entries for negative
3761 // lookups into a decl context as an optimization, today we *have* to skip
3762 // them because there are names with empty lookup results in decl contexts
3763 // which we can't emit in any stable ordering: we lookup constructors and
3764 // conversion functions in the enclosing namespace scope creating empty
3765 // results for them. This in almost certainly a bug in Clang's name lookup,
3766 // but that is likely to be hard or impossible to fix and so we tolerate it
3767 // here by omitting lookups with empty results.
3768 if (Lookup.second.getLookupResult().empty())
3769 continue;
3770
3771 switch (Lookup.first.getNameKind()) {
3772 default:
3773 Names.push_back(Lookup.first);
3774 break;
3775
Richard Smithcd45dbc2014-04-19 03:48:30 +00003776 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003777 assert(isa<CXXRecordDecl>(DC) &&
3778 "Cannot have a constructor name outside of a class!");
3779 ConstructorNameSet.insert(Name);
3780 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003781
3782 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003783 assert(isa<CXXRecordDecl>(DC) &&
3784 "Cannot have a conversion function name outside of a class!");
3785 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003786 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003787 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003788 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003789
Chandler Carruthe972c362015-03-26 03:11:40 +00003790 // Sort the names into a stable order.
3791 std::sort(Names.begin(), Names.end());
3792
Chandler Carruthffbf7052015-03-26 22:27:09 +00003793 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003794 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003795 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003796
Chandler Carruthffbf7052015-03-26 22:27:09 +00003797 // First we try the easy case by forming the current context's constructor
3798 // name and adding that name first. This is a very useful optimization to
3799 // avoid walking the lexical declarations in many cases, and it also
3800 // handles the only case where a constructor name can come from some other
3801 // lexical context -- when that name is an implicit constructor merged from
3802 // another declaration in the redecl chain. Any non-implicit constructor or
3803 // conversion function which doesn't occur in all the lexical contexts
3804 // would be an ODR violation.
3805 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3806 Context->getCanonicalType(Context->getRecordType(D)));
3807 if (ConstructorNameSet.erase(ImplicitCtorName))
3808 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003809
Chandler Carruthffbf7052015-03-26 22:27:09 +00003810 // If we still have constructors or conversion functions, we walk all the
3811 // names in the decl and add the constructors and conversion functions
3812 // which are visible in the order they lexically occur within the context.
3813 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3814 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3815 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3816 auto Name = ChildND->getDeclName();
3817 switch (Name.getNameKind()) {
3818 default:
3819 continue;
3820
3821 case DeclarationName::CXXConstructorName:
3822 if (ConstructorNameSet.erase(Name))
3823 Names.push_back(Name);
3824 break;
3825
3826 case DeclarationName::CXXConversionFunctionName:
3827 if (ConversionNameSet.erase(Name))
3828 Names.push_back(Name);
3829 break;
3830 }
3831
3832 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3833 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003834 }
3835
Chandler Carruthe972c362015-03-26 03:11:40 +00003836 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3837 "constructors by walking all the "
3838 "lexical members of the context.");
3839 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3840 "conversion functions by walking all "
3841 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003842 }
3843
Chandler Carruthe972c362015-03-26 03:11:40 +00003844 // Next we need to do a lookup with each name into this decl context to fully
3845 // populate any results from external sources. We don't actually use the
3846 // results of these lookups because we only want to use the results after all
3847 // results have been loaded and the pointers into them will be stable.
3848 for (auto &Name : Names)
3849 DC->lookup(Name);
3850
3851 // Now we need to insert the results for each name into the hash table. For
3852 // constructor names and conversion function names, we actually need to merge
3853 // all of the results for them into one list of results each and insert
3854 // those.
3855 SmallVector<NamedDecl *, 8> ConstructorDecls;
3856 SmallVector<NamedDecl *, 8> ConversionDecls;
3857
3858 // Now loop over the names, either inserting them or appending for the two
3859 // special cases.
3860 for (auto &Name : Names) {
3861 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3862
3863 switch (Name.getNameKind()) {
3864 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003865 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003866 break;
3867
3868 case DeclarationName::CXXConstructorName:
3869 ConstructorDecls.append(Result.begin(), Result.end());
3870 break;
3871
3872 case DeclarationName::CXXConversionFunctionName:
3873 ConversionDecls.append(Result.begin(), Result.end());
3874 break;
3875 }
3876 }
3877
3878 // Handle our two special cases if we ended up having any. We arbitrarily use
3879 // the first declaration's name here because the name itself isn't part of
3880 // the key, only the kind of name is used.
3881 if (!ConstructorDecls.empty())
3882 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003883 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003884 if (!ConversionDecls.empty())
3885 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003886 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003887
Richard Smithd88a7f12015-09-01 20:35:42 +00003888 // Create the on-disk hash table. Also emit the existing imported and
3889 // merged table if there is one.
3890 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3891 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00003892}
3893
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003894/// \brief Write the block containing all of the declaration IDs
3895/// visible from the given DeclContext.
3896///
3897/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003898/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003899uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3900 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00003901 // If we imported a key declaration of this namespace, write the visible
3902 // lookup results as an update record for it rather than including them
3903 // on this declaration. We will only look at key declarations on reload.
3904 if (isa<NamespaceDecl>(DC) && Chain &&
3905 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3906 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003907 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00003908 Prev = Prev->getPreviousDecl())
3909 if (!Prev->isFromASTFile())
3910 return 0;
3911
3912 // Note that we need to emit an update record for the primary context.
3913 UpdatedDeclContexts.insert(DC->getPrimaryContext());
3914
3915 // Make sure all visible decls are written. They will be recorded later. We
3916 // do this using a side data structure so we can sort the names into
3917 // a deterministic order.
3918 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3919 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3920 LookupResults;
3921 if (Map) {
3922 LookupResults.reserve(Map->size());
3923 for (auto &Entry : *Map)
3924 LookupResults.push_back(
3925 std::make_pair(Entry.first, Entry.second.getLookupResult()));
3926 }
3927
3928 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3929 for (auto &NameAndResult : LookupResults) {
3930 DeclarationName Name = NameAndResult.first;
3931 DeclContext::lookup_result Result = NameAndResult.second;
3932 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3933 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3934 // We have to work around a name lookup bug here where negative lookup
3935 // results for these names get cached in namespace lookup tables (these
3936 // names should never be looked up in a namespace).
3937 assert(Result.empty() && "Cannot have a constructor or conversion "
3938 "function name in a namespace!");
3939 continue;
3940 }
3941
3942 for (NamedDecl *ND : Result)
3943 if (!ND->isFromASTFile())
3944 GetDeclRef(ND);
3945 }
3946
3947 return 0;
3948 }
3949
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003950 if (DC->getPrimaryContext() != DC)
3951 return 0;
3952
Chandler Carruthe972c362015-03-26 03:11:40 +00003953 // Skip contexts which don't support name lookup.
3954 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003955 return 0;
3956
3957 // If not in C++, we perform name lookup for the translation unit via the
3958 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003959 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003960 return 0;
3961
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003962 // Serialize the contents of the mapping used for lookup. Note that,
3963 // although we have two very different code paths, the serialized
3964 // representation is the same for both cases: a declaration name,
3965 // followed by a size, followed by references to the visible
3966 // declarations that have that name.
3967 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003968 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003969 if (!Map || Map->empty())
3970 return 0;
3971
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003972 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003973 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003974 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003975
3976 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00003977 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003978 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00003979 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003980 ++NumVisibleDeclContexts;
3981 return Offset;
3982}
3983
Sebastian Redla4071b42010-08-24 00:50:09 +00003984/// \brief Write an UPDATE_VISIBLE block for the given context.
3985///
3986/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3987/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003988/// (in C++), for namespaces, and for classes with forward-declared unscoped
3989/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003990void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003991 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003992 if (!Map || Map->empty())
3993 return;
3994
Sebastian Redla4071b42010-08-24 00:50:09 +00003995 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003996 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00003997 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003998
Richard Smith5fc18a92015-07-12 23:43:21 +00003999 // If we're updating a namespace, select a key declaration as the key for the
4000 // update record; those are the only ones that will be checked on reload.
4001 if (isa<NamespaceDecl>(DC))
4002 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4003
Sebastian Redla4071b42010-08-24 00:50:09 +00004004 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004005 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004006 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004007}
4008
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004009/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4010void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Mehdi Amini57a41912015-09-10 01:46:39 +00004011 RecordData::value_type Record[] = {Opts.fp_contract};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004012 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4013}
4014
4015/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4016void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004017 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004018 return;
4019
4020 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4021 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004022 for (const auto &I:Opts.OptMap) {
4023 AddString(I.getKey(), Record);
4024 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004025 Record.push_back(V.Supported ? 1 : 0);
4026 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004027 Record.push_back(V.Avail);
4028 Record.push_back(V.Core);
4029 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004030 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4031}
4032
Yaxun Liu5b746652016-12-18 05:18:55 +00004033void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4034 if (!SemaRef.Context.getLangOpts().OpenCL)
4035 return;
4036
4037 RecordData Record;
4038 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4039 Record.push_back(
4040 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4041 Record.push_back(I.second.size());
4042 for (auto Ext : I.second)
4043 AddString(Ext, Record);
4044 }
4045 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4046}
4047
4048void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4049 if (!SemaRef.Context.getLangOpts().OpenCL)
4050 return;
4051
4052 RecordData Record;
4053 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4054 Record.push_back(getDeclID(I.first));
4055 Record.push_back(static_cast<unsigned>(I.second.size()));
4056 for (auto Ext : I.second)
4057 AddString(Ext, Record);
4058 }
4059 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4060}
4061
Justin Lebar67a78a62016-10-08 22:15:58 +00004062void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4063 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4064 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4065 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4066 }
4067}
4068
Douglas Gregor404cdde2012-01-27 01:47:08 +00004069void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004070 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004071 RecordData Categories;
4072
4073 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4074 unsigned Size = 0;
4075 unsigned StartIndex = Categories.size();
4076
4077 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4078
4079 // Allocate space for the size.
4080 Categories.push_back(0);
4081
4082 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004083 for (ObjCInterfaceDecl::known_categories_iterator
4084 Cat = Class->known_categories_begin(),
4085 CatEnd = Class->known_categories_end();
4086 Cat != CatEnd; ++Cat, ++Size) {
4087 assert(getDeclID(*Cat) != 0 && "Bogus category");
4088 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004089 }
4090
4091 // Update the size.
4092 Categories[StartIndex] = Size;
4093
4094 // Record this interface -> category map.
4095 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4096 CategoriesMap.push_back(CatInfo);
4097 }
4098
4099 // Sort the categories map by the definition ID, since the reader will be
4100 // performing binary searches on this information.
4101 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4102
4103 // Emit the categories map.
4104 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004105
David Blaikieb44f0bf2017-01-04 22:36:43 +00004106 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004107 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4109 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004110 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004111
4112 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4113 Stream.EmitRecordWithBlob(AbbrevID, Record,
4114 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004115 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004116
Douglas Gregor404cdde2012-01-27 01:47:08 +00004117 // Emit the category lists.
4118 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4119}
4120
Richard Smithe40f2ba2013-08-07 21:41:30 +00004121void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4122 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4123
4124 if (LPTMap.empty())
4125 return;
4126
4127 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004128 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004129 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004130 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004131 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004132 AddDeclRef(LPT.D, Record);
4133 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004134
Justin Lebar28f09c52016-10-10 16:26:08 +00004135 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004136 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004137 }
4138 }
4139 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4140}
4141
Dario Domizioli13a0a382014-05-23 12:13:25 +00004142/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4143void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4144 RecordData Record;
4145 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4146 AddSourceLocation(PragmaLoc, Record);
4147 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4148}
4149
Nico Weber779355f2016-03-02 23:22:00 +00004150/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4151void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4152 RecordData Record;
4153 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4154 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4155}
4156
Nico Weber42932312016-03-03 00:17:35 +00004157/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4158//module.
4159void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4160 RecordData Record;
4161 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4162 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4163 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4164}
4165
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004166void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4167 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004168 // Enter the extension block.
4169 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4170
4171 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004172 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004173 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4174 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4175 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4176 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4177 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4178 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004179 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004180
4181 // Emit the metadata record.
4182 RecordData Record;
4183 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4184 Record.push_back(EXTENSION_METADATA);
4185 Record.push_back(Metadata.MajorVersion);
4186 Record.push_back(Metadata.MinorVersion);
4187 Record.push_back(Metadata.BlockName.size());
4188 Record.push_back(Metadata.UserInfo.size());
4189 SmallString<64> Buffer;
4190 Buffer += Metadata.BlockName;
4191 Buffer += Metadata.UserInfo;
4192 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4193
4194 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004195 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004196
4197 // Exit the extension block.
4198 Stream.ExitBlock();
4199}
4200
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004201//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004202// General Serialization Routines
4203//===----------------------------------------------------------------------===//
4204
Richard Smith69c82bf2016-04-01 22:52:03 +00004205/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004206void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4207 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004208 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004209 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004210 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004211 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004212
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004213#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004214
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004215 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004216}
4217
John McCallf413f5e2013-05-03 00:10:13 +00004218void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4219 AddSourceLocation(Tok.getLocation(), Record);
4220 Record.push_back(Tok.getLength());
4221
4222 // FIXME: When reading literal tokens, reconstruct the literal pointer
4223 // if it is needed.
4224 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4225 // FIXME: Should translate token kind to a stable encoding.
4226 Record.push_back(Tok.getKind());
4227 // FIXME: Should translate token flags to a stable encoding.
4228 Record.push_back(Tok.getFlags());
4229}
4230
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004231void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004232 Record.push_back(Str.size());
4233 Record.insert(Record.end(), Str.begin(), Str.end());
4234}
4235
Richard Smith7ed1bc92014-12-05 22:42:13 +00004236bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004237 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004238
Richard Smith54cc3c22014-12-11 20:50:24 +00004239 bool Changed =
4240 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004241
4242 // Remove a prefix to make the path relative, if relevant.
4243 const char *PathBegin = Path.data();
4244 const char *PathPtr =
4245 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4246 if (PathPtr != PathBegin) {
4247 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4248 Changed = true;
4249 }
4250
4251 return Changed;
4252}
4253
4254void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4255 SmallString<128> FilePath(Path);
4256 PreparePathForOutput(FilePath);
4257 AddString(FilePath, Record);
4258}
4259
Mehdi Amini57a41912015-09-10 01:46:39 +00004260void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004261 StringRef Path) {
4262 SmallString<128> FilePath(Path);
4263 PreparePathForOutput(FilePath);
4264 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4265}
4266
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004267void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4268 RecordDataImpl &Record) {
4269 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004270 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004271 Record.push_back(*Minor + 1);
4272 else
4273 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004274 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004275 Record.push_back(*Subminor + 1);
4276 else
4277 Record.push_back(0);
4278}
4279
Douglas Gregore84a9da2009-04-20 20:36:09 +00004280/// \brief Note that the identifier II occurs at the given offset
4281/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004282void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004283 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004284 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004285 // up earlier in the chain and thus don't need an offset.
4286 if (ID >= FirstIdentID)
4287 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004288}
4289
Douglas Gregor95c13f52009-04-25 17:48:32 +00004290/// \brief Note that the selector Sel occurs at the given offset
4291/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004292void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004293 unsigned ID = SelectorIDs[Sel];
4294 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004295 // Don't record offsets for selectors that are also available in a different
4296 // file.
4297 if (ID < FirstSelectorID)
4298 return;
4299 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004300}
4301
David Blaikie61137e12017-01-05 18:23:18 +00004302ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004303 SmallVectorImpl<char> &Buffer,
David Blaikie61137e12017-01-05 18:23:18 +00004304 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4305 bool IncludeTimestamps)
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004306 : Stream(Stream), Buffer(Buffer), IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004307 for (const auto &Ext : Extensions) {
4308 if (auto Writer = Ext->createExtensionWriter(*this))
4309 ModuleFileExtensionWriters.push_back(std::move(Writer));
4310 }
4311}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004312
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004313ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004314 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004315}
4316
Richard Smithb3761912015-02-05 23:08:52 +00004317const LangOptions &ASTWriter::getLangOpts() const {
4318 assert(WritingAST && "can't determine lang opts when not writing AST");
4319 return Context->getLangOpts();
4320}
4321
Richard Smithe75ee0f2015-08-17 07:13:32 +00004322time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4323 return IncludeTimestamps ? E->getModificationTime() : 0;
4324}
4325
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004326ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4327 const std::string &OutputFile,
4328 Module *WritingModule, StringRef isysroot,
4329 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004330 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004331
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004332 ASTHasCompilerErrors = hasErrors;
4333
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004334 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004335 Stream.Emit((unsigned)'C', 8);
4336 Stream.Emit((unsigned)'P', 8);
4337 Stream.Emit((unsigned)'C', 8);
4338 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004339
Chris Lattner28fa4e62009-04-26 22:26:21 +00004340 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004341
Douglas Gregoreda8e122011-08-09 15:13:55 +00004342 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004343 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004344 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004345 ASTFileSignature Signature =
4346 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004347 Context = nullptr;
4348 PP = nullptr;
4349 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004350 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004351
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004352 WritingAST = false;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004353 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004354}
4355
Douglas Gregora94a1542011-07-27 21:45:57 +00004356template<typename Vector>
4357static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4358 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004359 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4360 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004361 Writer.AddDeclRef(*I, Record);
4362 }
4363}
4364
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004365ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4366 const std::string &OutputFile,
4367 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004368 using namespace llvm;
4369
Craig Toppera13603a2014-05-22 05:54:18 +00004370 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004371
Douglas Gregorcf68c582011-12-01 22:20:10 +00004372 // Make sure that the AST reader knows to finalize itself.
4373 if (Chain)
4374 Chain->finalizeForWriting();
4375
Sebastian Redl143413f2010-07-12 22:02:52 +00004376 ASTContext &Context = SemaRef.Context;
4377 Preprocessor &PP = SemaRef.PP;
4378
Douglas Gregordab42432011-08-12 00:15:20 +00004379 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004380 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4381 if (D) {
4382 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4383 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004384 }
4385 };
4386 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4387 PREDEF_DECL_TRANSLATION_UNIT_ID);
4388 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4389 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4390 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4391 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4392 PREDEF_DECL_OBJC_PROTOCOL_ID);
4393 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4394 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4395 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4396 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4397 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004398 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004399 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4400 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004401 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004402 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4403 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004404 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4405 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004406 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4407 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004408 RegisterPredefDecl(Context.TypePackElementDecl,
4409 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004410
Chris Lattner0c797362009-09-08 18:19:27 +00004411 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004412 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004413 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004414 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004415 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004416
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004417 // Build a record containing all of the file scoped decls in this file.
4418 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004419 if (!isModule)
4420 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4421 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004422
Douglas Gregor851443c2011-08-12 01:39:19 +00004423 // Build a record containing all of the delegating constructors we still need
4424 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004425 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004426 if (!isModule)
4427 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004428
Douglas Gregor851443c2011-08-12 01:39:19 +00004429 // Write the set of weak, undeclared identifiers. We always write the
4430 // entire table, since later PCH files in a PCH chain are only interested in
4431 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004432 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004433 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4434 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4435 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4436 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4437 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4438 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4439 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004440 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004441
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004442 // Build a record containing all of the ext_vector declarations.
4443 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004444 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004445
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004446 // Build a record containing all of the VTable uses information.
4447 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004448 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004449 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4450 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4451 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4452 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4453 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004454 }
4455
Nico Weber72889432014-09-06 01:25:55 +00004456 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4457 RecordData UnusedLocalTypedefNameCandidates;
4458 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4459 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4460
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004461 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004462 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004463 for (const auto &I : SemaRef.PendingInstantiations) {
4464 AddDeclRef(I.first, PendingInstantiations);
4465 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004466 }
4467 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4468 "There are local ones at end of translation unit!");
4469
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004470 // Build a record containing some declaration references.
4471 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004472 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004473 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4474 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004475 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004476 }
4477
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004478 RecordData CUDASpecialDeclRefs;
4479 if (Context.getcudaConfigureCallDecl()) {
4480 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4481 }
4482
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004483 // Build a record containing all of the known namespaces.
4484 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004485 for (const auto &I : SemaRef.KnownNamespaces) {
4486 if (!I.second)
4487 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004488 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004489
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004490 // Build a record of all used, undefined objects that require definitions.
4491 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004492
4493 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004494 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004495 for (const auto &I : Undefined) {
4496 AddDeclRef(I.first, UndefinedButUsed);
4497 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004498 }
4499
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004500 // Build a record containing all delete-expressions that we would like to
4501 // analyze later in AST.
4502 RecordData DeleteExprsToAnalyze;
4503
4504 for (const auto &DeleteExprsInfo :
4505 SemaRef.getMismatchingDeleteExpressions()) {
4506 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4507 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4508 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4509 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4510 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4511 }
4512 }
4513
Douglas Gregor112b9072012-10-18 05:31:06 +00004514 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004515 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004516
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004517 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004518 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004519
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004520 // This is so that older clang versions, before the introduction
4521 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004522 {
4523 RecordData Record = {VERSION_MAJOR};
4524 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4525 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004526
Douglas Gregor851443c2011-08-12 01:39:19 +00004527 // Create a lexical update block containing all of the declarations in the
4528 // translation unit that do not come from other AST files.
4529 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004530 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4531 for (const auto *D : TU->noload_decls()) {
4532 if (!D->isFromASTFile()) {
4533 NewGlobalKindDeclPairs.push_back(D->getKind());
4534 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4535 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004536 }
4537
David Blaikieb44f0bf2017-01-04 22:36:43 +00004538 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004539 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4540 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004541 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004542 {
4543 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4544 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4545 bytes(NewGlobalKindDeclPairs));
4546 }
4547
Douglas Gregor851443c2011-08-12 01:39:19 +00004548 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004549 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004550 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4551 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004552 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004553 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004554 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004555
4556 // If we have any extern "C" names, write out a visible update for them.
4557 if (Context.ExternCContext)
4558 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004559
4560 // If the translation unit has an anonymous namespace, and we don't already
4561 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004562 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004563 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4564 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004565 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004566 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004567 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004568
Richard Smith5652c0f2014-03-21 01:48:23 +00004569 // Add update records for all mangling numbers and static local numbers.
4570 // These aren't really update records, but this is a convenient way of
4571 // tagging this rare extra data onto the declarations.
4572 for (const auto &Number : Context.MangleNumbers)
4573 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004574 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4575 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004576 for (const auto &Number : Context.StaticLocalNumbers)
4577 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004578 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4579 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004580
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004581 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004582 // an AST file, are registered for serialization. Likewise for template
4583 // specializations added to imported templates.
4584 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004585 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004586 }
4587
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004588 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004589 // serialization, if we're storing decls with identifiers.
4590 if (!WritingModule || !getLangOpts().CPlusPlus) {
4591 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004592 for (const auto &ID : PP.getIdentifierTable()) {
4593 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004594 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4595 IIs.push_back(II);
4596 }
4597 // Sort the identifiers to visit based on their name.
4598 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4599 for (const IdentifierInfo *II : IIs) {
4600 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4601 DEnd = SemaRef.IdResolver.end();
4602 D != DEnd; ++D) {
4603 GetDeclRef(*D);
4604 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004605 }
4606 }
4607
Manman Rena0f31a02016-04-29 19:04:05 +00004608 // For method pool in the module, if it contains an entry for a selector,
4609 // the entry should be complete, containing everything introduced by that
4610 // module and all modules it imports. It's possible that the entry is out of
4611 // date, so we need to pull in the new content here.
4612
4613 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4614 // safe, we copy all selectors out.
4615 llvm::SmallVector<Selector, 256> AllSelectors;
4616 for (auto &SelectorAndID : SelectorIDs)
4617 AllSelectors.push_back(SelectorAndID.first);
4618 for (auto &Selector : AllSelectors)
4619 SemaRef.updateOutOfDateSelector(Selector);
4620
Douglas Gregor5204bde2011-08-02 16:26:37 +00004621 // Form the record of special types.
4622 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004623 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004624 AddTypeRef(Context.getFILEType(), SpecialTypes);
4625 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4626 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4627 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4628 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004629 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004630 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004631
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004632 if (Chain) {
4633 // Write the mapping information describing our module dependencies and how
4634 // each of those modules were mapped into our own offset/ID space, so that
4635 // the reader can build the appropriate mapping to its own offset/ID space.
4636 // The map consists solely of a blob with the following format:
4637 // *(module-name-len:i16 module-name:len*i8
4638 // source-location-offset:i32
4639 // identifier-id:i32
4640 // preprocessed-entity-id:i32
4641 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004642 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004643 // selector-id:i32
4644 // declaration-id:i32
4645 // c++-base-specifiers-id:i32
4646 // type-id:i32)
4647 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004648 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004649 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004651 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004652 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004653 {
4654 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004655 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004656 using namespace llvm::support;
4657 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004658 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004659 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004660 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004661
Ben Langmuir785180e2014-10-20 16:27:30 +00004662 // Note: if a base ID was uint max, it would not be possible to load
4663 // another module after it or have more than one entity inside it.
4664 uint32_t None = std::numeric_limits<uint32_t>::max();
4665
4666 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4667 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4668 if (ShouldWrite)
4669 LE.write<uint32_t>(BaseID);
4670 else
4671 LE.write<uint32_t>(None);
4672 };
4673
Ben Langmuirfe971d92014-08-16 04:54:18 +00004674 // These values should be unique within a chain, since they will be read
4675 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004676 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4677 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4678 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4679 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4680 M.NumPreprocessedEntities);
4681 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4682 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4683 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4684 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004685 }
4686 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004687 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004688 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4689 Buffer.data(), Buffer.size());
4690 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004691
Richard Smithb9eab6d2014-03-20 19:44:17 +00004692 RecordData DeclUpdatesOffsetsRecord;
4693
Richard Smith59442b42014-03-20 20:07:19 +00004694 // Keep writing types, declarations, and declaration update records
4695 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004696 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4697 WriteTypeAbbrevs();
4698 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004699 do {
4700 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4701 while (!DeclTypesToEmit.empty()) {
4702 DeclOrType DOT = DeclTypesToEmit.front();
4703 DeclTypesToEmit.pop();
4704 if (DOT.isType())
4705 WriteType(DOT.getType());
4706 else
4707 WriteDecl(Context, DOT.getDecl());
4708 }
4709 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004710 Stream.ExitBlock();
4711
Richard Smithb9eab6d2014-03-20 19:44:17 +00004712 DoneWritingDeclsAndTypes = true;
4713
4714 // These things can only be done once we've written out decls and types.
4715 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004716 if (!DeclUpdatesOffsetsRecord.empty())
4717 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004718 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004719 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004720 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004721 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004722 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004723 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004724 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004725 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004726 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004727 WriteFPPragmaOptions(SemaRef.getFPOptions());
4728 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004729 WriteOpenCLExtensionTypes(SemaRef);
4730 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004731 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004732
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004733 // If we're emitting a module, write out the submodule information.
4734 if (WritingModule)
4735 WriteSubmodules(WritingModule);
4736
Douglas Gregor5204bde2011-08-02 16:26:37 +00004737 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4738
Douglas Gregord4df8652009-04-22 22:02:47 +00004739 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004740 if (!EagerlyDeserializedDecls.empty())
4741 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004742
David Blaikie9ffe5a32017-01-30 05:00:26 +00004743 if (Context.getLangOpts().ModularCodegen)
4744 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4745
Douglas Gregord4df8652009-04-22 22:02:47 +00004746 // Write the record containing tentative definitions.
4747 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004748 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004749
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004750 // Write the record containing unused file scoped decls.
4751 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004752 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004753
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004754 // Write the record containing weak undeclared identifiers.
4755 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004756 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004757 WeakUndeclaredIdentifiers);
4758
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004759 // Write the record containing ext_vector type names.
4760 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004761 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004762
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004763 // Write the record containing VTable uses information.
4764 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004765 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004766
Nico Weber72889432014-09-06 01:25:55 +00004767 // Write the record containing potentially unused local typedefs.
4768 if (!UnusedLocalTypedefNameCandidates.empty())
4769 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4770 UnusedLocalTypedefNameCandidates);
4771
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004772 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004773 if (!PendingInstantiations.empty())
4774 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004775
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004776 // Write the record containing declaration references of Sema.
4777 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004778 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004779
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004780 // Write the record containing CUDA-specific declaration references.
4781 if (!CUDASpecialDeclRefs.empty())
4782 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004783
4784 // Write the delegating constructors.
4785 if (!DelegatingCtorDecls.empty())
4786 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004787
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004788 // Write the known namespaces.
4789 if (!KnownNamespaces.empty())
4790 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004791
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004792 // Write the undefined internal functions and variables, and inline functions.
4793 if (!UndefinedButUsed.empty())
4794 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004795
4796 if (!DeleteExprsToAnalyze.empty())
4797 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4798
Douglas Gregor851443c2011-08-12 01:39:19 +00004799 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004800 for (auto *DC : UpdatedDeclContexts)
4801 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004802
Douglas Gregor959bb062011-12-03 01:15:29 +00004803 if (!WritingModule) {
4804 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004805 struct ModuleInfo {
4806 uint64_t ID;
4807 Module *M;
4808 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4809 };
Richard Smith56be7542014-03-21 00:33:59 +00004810 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004811 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004812 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004813 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4814 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004815 }
Richard Smith56be7542014-03-21 00:33:59 +00004816
4817 if (!Imports.empty()) {
4818 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4819 return A.ID < B.ID;
4820 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004821 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4822 return A.ID == B.ID;
4823 };
Richard Smith56be7542014-03-21 00:33:59 +00004824
4825 // Sort and deduplicate module IDs.
4826 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004827 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004828 Imports.end());
4829
4830 RecordData ImportedModules;
4831 for (const auto &Import : Imports) {
4832 ImportedModules.push_back(Import.ID);
4833 // FIXME: If the module has macros imported then later has declarations
4834 // imported, this location won't be the right one as a location for the
4835 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004836 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004837 }
4838
Douglas Gregor959bb062011-12-03 01:15:29 +00004839 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4840 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004841 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004842
Douglas Gregor404cdde2012-01-27 01:47:08 +00004843 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004844 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004845 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004846 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004847 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004848 }
Richard Smithe40f2ba2013-08-07 21:41:30 +00004849
Douglas Gregor08f01292009-04-17 22:13:46 +00004850 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004851 RecordData::value_type Record[] = {
4852 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004853 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004854 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004855
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004856 // Write the module file extension blocks.
4857 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004858 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004859
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004860 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004861}
4862
Richard Smithb9eab6d2014-03-20 19:44:17 +00004863void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004864 if (DeclUpdates.empty())
4865 return;
4866
Richard Smith59442b42014-03-20 20:07:19 +00004867 DeclUpdateMap LocalUpdates;
4868 LocalUpdates.swap(DeclUpdates);
4869
Richard Smith6ef42932014-03-20 21:02:00 +00004870 for (auto &DeclUpdate : LocalUpdates) {
4871 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004872
Richard Smithd28ac5b2014-03-22 23:33:22 +00004873 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00004874 RecordData RecordData;
4875 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00004876 for (auto &Update : DeclUpdate.second) {
4877 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4878
Richard Smith69c82bf2016-04-01 22:52:03 +00004879 // An updated body is emitted last, so that the reader doesn't need
4880 // to skip over the lazy body to reach statements for other records.
4881 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
4882 HasUpdatedBody = true;
4883 else
4884 Record.push_back(Kind);
4885
Richard Smith6ef42932014-03-20 21:02:00 +00004886 switch (Kind) {
4887 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4888 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4889 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00004890 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00004891 Record.push_back(GetDeclRef(Update.getDecl()));
4892 break;
4893
Richard Smith4d235792014-08-07 18:53:08 +00004894 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00004895 break;
4896
Richard Smith4d235792014-08-07 18:53:08 +00004897 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Richard Smith69c82bf2016-04-01 22:52:03 +00004898 Record.AddSourceLocation(Update.getLoc());
Richard Smith4d235792014-08-07 18:53:08 +00004899 break;
4900
John McCall32791cc2016-01-06 22:34:54 +00004901 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00004902 Record.AddStmt(const_cast<Expr *>(
4903 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00004904 break;
4905
Richard Smith4b054b22016-08-24 21:25:37 +00004906 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
4907 Record.AddStmt(
4908 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
4909 break;
4910
Richard Smithcd45dbc2014-04-19 03:48:30 +00004911 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4912 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00004913 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00004914 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00004915 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00004916 *Context, const_cast<CXXRecordDecl *>(RD)));
4917
4918 // This state is sometimes updated by template instantiation, when we
4919 // switch from the specialization referring to the template declaration
4920 // to it referring to the template definition.
4921 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4922 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004923 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004924 } else {
4925 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4926 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004927 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00004928
4929 // The instantiation might have been resolved to a partial
4930 // specialization. If so, record which one.
4931 auto From = Spec->getInstantiatedFrom();
4932 if (auto PartialSpec =
4933 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4934 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00004935 Record.AddDeclRef(PartialSpec);
4936 Record.AddTemplateArgumentList(
4937 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00004938 } else {
4939 Record.push_back(false);
4940 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00004941 }
4942 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00004943 Record.AddSourceLocation(RD->getLocation());
4944 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00004945 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004946
4947 // Instantiation may change attributes; write them all out afresh.
4948 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00004949 if (D->hasAttrs())
4950 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00004951
4952 // FIXME: Ensure we don't get here for explicit instantiations.
4953 break;
4954 }
4955
Richard Smithf8134002015-03-10 01:41:22 +00004956 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004957 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00004958 break;
4959
Richard Smith564417a2014-03-20 21:47:22 +00004960 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4961 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00004962 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4963 Record);
4964 break;
4965
Richard Smith6ef42932014-03-20 21:02:00 +00004966 case UPD_CXX_DEDUCED_RETURN_TYPE:
4967 Record.push_back(GetOrCreateTypeID(Update.getType()));
4968 break;
4969
4970 case UPD_DECL_MARKED_USED:
4971 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004972
4973 case UPD_MANGLING_NUMBER:
4974 case UPD_STATIC_LOCAL_NUMBER:
4975 Record.push_back(Update.getNumber());
4976 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004977
Alexey Bataev97720002014-11-11 04:05:39 +00004978 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00004979 Record.AddSourceRange(
4980 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00004981 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00004982
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00004983 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
4984 Record.AddSourceRange(
4985 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
4986 break;
4987
Richard Smith65ebb4a2015-03-26 04:09:53 +00004988 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00004989 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00004990 break;
Alex Denisovfde64952015-06-26 05:28:36 +00004991
4992 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00004993 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00004994 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004995 }
4996 }
4997
Richard Smithd28ac5b2014-03-22 23:33:22 +00004998 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004999 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005000 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005001 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005002 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005003 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005004 }
5005
Richard Smithcd45dbc2014-04-19 03:48:30 +00005006 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005007 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005008 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005009}
5010
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005011void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005012 uint32_t Raw = Loc.getRawEncoding();
5013 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005014}
5015
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005016void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005017 AddSourceLocation(Range.getBegin(), Record);
5018 AddSourceLocation(Range.getEnd(), Record);
5019}
5020
Richard Smithf144b542016-04-08 21:54:32 +00005021void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5022 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005023 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005024 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005025}
5026
Richard Smithf144b542016-04-08 21:54:32 +00005027void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5028 Record->push_back(Value.isUnsigned());
5029 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005030}
5031
Richard Smithf144b542016-04-08 21:54:32 +00005032void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5033 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005034}
5035
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005036void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005037 Record.push_back(getIdentifierRef(II));
5038}
5039
Sebastian Redl539c5062010-08-18 23:57:32 +00005040IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005041 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005042 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005043
Sebastian Redl539c5062010-08-18 23:57:32 +00005044 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005045 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005046 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005047 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005048}
5049
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005050MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005051 // Don't emit builtin macros like __LINE__ to the AST file unless they
5052 // have been redefined by the header (in which case they are not
5053 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005054 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005055 return 0;
5056
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005057 MacroID &ID = MacroIDs[MI];
5058 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005059 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005060 MacroInfoToEmitData Info = { Name, MI, ID };
5061 MacroInfosToEmit.push_back(Info);
5062 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005063 return ID;
5064}
5065
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005066MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005067 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005068 return 0;
5069
5070 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5071 return MacroIDs[MI];
5072}
5073
5074uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005075 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005076}
5077
Richard Smithe64e7452016-04-18 21:54:58 +00005078void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5079 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005080}
5081
Sebastian Redl539c5062010-08-18 23:57:32 +00005082SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005083 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005084 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005085 }
5086
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005087 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005088 if (SID == 0 && Chain) {
5089 // This might trigger a ReadSelector callback, which will set the ID for
5090 // this selector.
5091 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005092 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005093 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005094 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005095 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005096 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005097 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005098 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005099}
5100
Richard Smithe64e7452016-04-18 21:54:58 +00005101void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5102 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005103}
5104
Richard Smith290d8012016-04-06 17:06:00 +00005105void ASTRecordWriter::AddTemplateArgumentLocInfo(
5106 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005107 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005108 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005109 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005110 break;
5111 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005112 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005113 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005114 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005115 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5116 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005117 break;
5118 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005119 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5120 AddSourceLocation(Arg.getTemplateNameLoc());
5121 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005122 break;
John McCall0ad16662009-10-29 08:12:44 +00005123 case TemplateArgument::Null:
5124 case TemplateArgument::Integral:
5125 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005126 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005127 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005128 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005129 break;
5130 }
5131}
5132
Richard Smith290d8012016-04-06 17:06:00 +00005133void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5134 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005135
5136 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5137 bool InfoHasSameExpr
5138 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005139 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005140 if (InfoHasSameExpr)
5141 return; // Avoid storing the same expr twice.
5142 }
Richard Smith290d8012016-04-06 17:06:00 +00005143 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005144}
5145
Richard Smith290d8012016-04-06 17:06:00 +00005146void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005147 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005148 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005149 return;
5150 }
5151
Richard Smith290d8012016-04-06 17:06:00 +00005152 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005153}
5154
Richard Smith290d8012016-04-06 17:06:00 +00005155void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5156 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005157
Richard Smith290d8012016-04-06 17:06:00 +00005158 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005159 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005160 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005161}
5162
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005163void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005164 Record.push_back(GetOrCreateTypeID(T));
5165}
5166
Richard Smith2095ffe2015-03-16 20:11:03 +00005167TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005168 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005169 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5170 if (T.isNull())
5171 return TypeIdx();
5172 assert(!T.getLocalFastQualifiers());
5173
5174 TypeIdx &Idx = TypeIdxs[T];
5175 if (Idx.getIndex() == 0) {
5176 if (DoneWritingDeclsAndTypes) {
5177 assert(0 && "New type seen after serializing all the types to emit!");
5178 return TypeIdx();
5179 }
5180
5181 // We haven't seen this type before. Assign it a new ID and put it
5182 // into the queue of types to emit.
5183 Idx = TypeIdx(NextTypeID++);
5184 DeclTypesToEmit.push(T);
5185 }
5186 return Idx;
5187 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005188}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005189
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005190TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005191 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005192 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5193 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005194 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005195 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005196
Richard Smith2095ffe2015-03-16 20:11:03 +00005197 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5198 assert(I != TypeIdxs.end() && "Type not emitted!");
5199 return I->second;
5200 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005201}
5202
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005203void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005204 Record.push_back(GetDeclRef(D));
5205}
5206
Sebastian Redl539c5062010-08-18 23:57:32 +00005207DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005208 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005209
5210 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005211 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005212 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005213
5214 // If D comes from an AST file, its declaration ID is already known and
5215 // fixed.
5216 if (D->isFromASTFile())
5217 return D->getGlobalID();
5218
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005219 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005220 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005221 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005222 if (DoneWritingDeclsAndTypes) {
5223 assert(0 && "New decl seen after serializing all the decls to emit!");
5224 return 0;
5225 }
5226
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005227 // We haven't seen this declaration before. Give it a new ID and
5228 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005229 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005230 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005231 }
5232
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005233 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005234}
5235
Sebastian Redl539c5062010-08-18 23:57:32 +00005236DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005237 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005238 return 0;
5239
Douglas Gregorb3163e52012-01-05 22:33:30 +00005240 // If D comes from an AST file, its declaration ID is already known and
5241 // fixed.
5242 if (D->isFromASTFile())
5243 return D->getGlobalID();
5244
Douglas Gregore84a9da2009-04-20 20:36:09 +00005245 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5246 return DeclIDs[D];
5247}
5248
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005249void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005250 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005251 assert(D);
5252
5253 SourceLocation Loc = D->getLocation();
5254 if (Loc.isInvalid())
5255 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005256
5257 // We only keep track of the file-level declarations of each file.
5258 if (!D->getLexicalDeclContext()->isFileContext())
5259 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005260 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5261 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005262 if (isa<ParmVarDecl>(D))
5263 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005264
5265 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005266 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005267 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005268 FileID FID;
5269 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005270 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005271 if (FID.isInvalid())
5272 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005273 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005274
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005275 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005276 if (!Info)
5277 Info = new DeclIDInFileInfo();
5278
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005279 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005280 LocDeclIDsTy &Decls = Info->DeclIDs;
5281
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005282 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005283 Decls.push_back(LocDecl);
5284 return;
5285 }
5286
Benjamin Kramer45025c02013-08-24 13:22:59 +00005287 LocDeclIDsTy::iterator I =
5288 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005289
5290 Decls.insert(I, LocDecl);
5291}
5292
Richard Smithe64e7452016-04-18 21:54:58 +00005293void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005294 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005295 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005296 switch (Name.getNameKind()) {
5297 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005298 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005299 break;
5300
5301 case DeclarationName::ObjCZeroArgSelector:
5302 case DeclarationName::ObjCOneArgSelector:
5303 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005304 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005305 break;
5306
5307 case DeclarationName::CXXConstructorName:
5308 case DeclarationName::CXXDestructorName:
5309 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005310 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005311 break;
5312
Richard Smith35845152017-02-07 01:37:30 +00005313 case DeclarationName::CXXDeductionGuideName:
5314 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5315 break;
5316
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005317 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005318 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005319 break;
5320
Alexis Hunt3d221f22009-11-29 07:34:05 +00005321 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005322 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005323 break;
5324
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005325 case DeclarationName::CXXUsingDirective:
5326 // No extra data to emit
5327 break;
5328 }
5329}
Chris Lattnerca025db2010-05-07 21:43:38 +00005330
Richard Smithd08aeb62014-08-28 01:33:39 +00005331unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5332 assert(needsAnonymousDeclarationNumber(D) &&
5333 "expected an anonymous declaration");
5334
5335 // Number the anonymous declarations within this context, if we've not
5336 // already done so.
5337 auto It = AnonymousDeclarationNumbers.find(D);
5338 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005339 auto *DC = D->getLexicalDeclContext();
5340 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5341 AnonymousDeclarationNumbers[ND] = Number;
5342 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005343
5344 It = AnonymousDeclarationNumbers.find(D);
5345 assert(It != AnonymousDeclarationNumbers.end() &&
5346 "declaration not found within its lexical context");
5347 }
5348
5349 return It->second;
5350}
5351
Richard Smith290d8012016-04-06 17:06:00 +00005352void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5353 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005354 switch (Name.getNameKind()) {
5355 case DeclarationName::CXXConstructorName:
5356 case DeclarationName::CXXDestructorName:
5357 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005358 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005359 break;
5360
5361 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005362 AddSourceLocation(SourceLocation::getFromRawEncoding(
5363 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005364 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005365 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005366 break;
5367
5368 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005369 AddSourceLocation(SourceLocation::getFromRawEncoding(
5370 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005371 break;
5372
5373 case DeclarationName::Identifier:
5374 case DeclarationName::ObjCZeroArgSelector:
5375 case DeclarationName::ObjCOneArgSelector:
5376 case DeclarationName::ObjCMultiArgSelector:
5377 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005378 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005379 break;
5380 }
5381}
5382
Richard Smith290d8012016-04-06 17:06:00 +00005383void ASTRecordWriter::AddDeclarationNameInfo(
5384 const DeclarationNameInfo &NameInfo) {
5385 AddDeclarationName(NameInfo.getName());
5386 AddSourceLocation(NameInfo.getLoc());
5387 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005388}
5389
Richard Smith290d8012016-04-06 17:06:00 +00005390void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5391 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5392 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005393 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005394 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005395}
5396
Richard Smithe64e7452016-04-18 21:54:58 +00005397void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005398 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005399 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005400 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005401
5402 // Push each of the NNS's onto a stack for serialization in reverse order.
5403 while (NNS) {
5404 NestedNames.push_back(NNS);
5405 NNS = NNS->getPrefix();
5406 }
5407
Richard Smithe64e7452016-04-18 21:54:58 +00005408 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005409 while(!NestedNames.empty()) {
5410 NNS = NestedNames.pop_back_val();
5411 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005412 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005413 switch (Kind) {
5414 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005415 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005416 break;
5417
5418 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005419 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005420 break;
5421
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005422 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005423 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005424 break;
5425
Chris Lattnerca025db2010-05-07 21:43:38 +00005426 case NestedNameSpecifier::TypeSpec:
5427 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005428 AddTypeRef(QualType(NNS->getAsType(), 0));
5429 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005430 break;
5431
5432 case NestedNameSpecifier::Global:
5433 // Don't need to write an associated value.
5434 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005435
5436 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005437 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005438 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005439 }
5440 }
5441}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005442
Richard Smith290d8012016-04-06 17:06:00 +00005443void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005444 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005445 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005446 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005447
5448 // Push each of the nested-name-specifiers's onto a stack for
5449 // serialization in reverse order.
5450 while (NNS) {
5451 NestedNames.push_back(NNS);
5452 NNS = NNS.getPrefix();
5453 }
5454
Richard Smith290d8012016-04-06 17:06:00 +00005455 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005456 while(!NestedNames.empty()) {
5457 NNS = NestedNames.pop_back_val();
5458 NestedNameSpecifier::SpecifierKind Kind
5459 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005460 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005461 switch (Kind) {
5462 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005463 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5464 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005465 break;
5466
5467 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005468 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5469 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005470 break;
5471
5472 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005473 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5474 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005475 break;
5476
5477 case NestedNameSpecifier::TypeSpec:
5478 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005479 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5480 AddTypeLoc(NNS.getTypeLoc());
5481 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005482 break;
5483
5484 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005485 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005486 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005487
5488 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005489 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5490 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005491 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005492 }
5493 }
5494}
5495
Richard Smith290d8012016-04-06 17:06:00 +00005496void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005497 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005498 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005499 switch (Kind) {
5500 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005501 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005502 break;
5503
5504 case TemplateName::OverloadedTemplate: {
5505 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005506 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005507 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005508 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005509 break;
5510 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005511
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005512 case TemplateName::QualifiedTemplate: {
5513 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005514 AddNestedNameSpecifier(QualT->getQualifier());
5515 Record->push_back(QualT->hasTemplateKeyword());
5516 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005517 break;
5518 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005519
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005520 case TemplateName::DependentTemplate: {
5521 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005522 AddNestedNameSpecifier(DepT->getQualifier());
5523 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005524 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005525 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005526 else
Richard Smith290d8012016-04-06 17:06:00 +00005527 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005528 break;
5529 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005530
5531 case TemplateName::SubstTemplateTemplateParm: {
5532 SubstTemplateTemplateParmStorage *subst
5533 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005534 AddDeclRef(subst->getParameter());
5535 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005536 break;
5537 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005538
5539 case TemplateName::SubstTemplateTemplateParmPack: {
5540 SubstTemplateTemplateParmPackStorage *SubstPack
5541 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005542 AddDeclRef(SubstPack->getParameterPack());
5543 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005544 break;
5545 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005546 }
5547}
5548
Richard Smith290d8012016-04-06 17:06:00 +00005549void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5550 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005551 switch (Arg.getKind()) {
5552 case TemplateArgument::Null:
5553 break;
5554 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005555 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005556 break;
5557 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005558 AddDeclRef(Arg.getAsDecl());
5559 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005560 break;
5561 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005562 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005563 break;
5564 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005565 AddAPSInt(Arg.getAsIntegral());
5566 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005567 break;
5568 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005569 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005570 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005571 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005572 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005573 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005574 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005575 else
Richard Smith290d8012016-04-06 17:06:00 +00005576 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005577 break;
5578 case TemplateArgument::Expression:
5579 AddStmt(Arg.getAsExpr());
5580 break;
5581 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005582 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005583 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005584 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005585 break;
5586 }
5587}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005588
Richard Smithe64e7452016-04-18 21:54:58 +00005589void ASTRecordWriter::AddTemplateParameterList(
5590 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005591 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005592 AddSourceLocation(TemplateParams->getTemplateLoc());
5593 AddSourceLocation(TemplateParams->getLAngleLoc());
5594 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005595 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005596 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005597 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005598 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005599}
5600
5601/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005602void ASTRecordWriter::AddTemplateArgumentList(
5603 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005604 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005605 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005606 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005607 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005608}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005609
Richard Smith290d8012016-04-06 17:06:00 +00005610void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5611 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005612 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005613 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5614 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5615 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005616 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005617 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005618 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005619}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005620
Richard Smithe64e7452016-04-18 21:54:58 +00005621void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5622 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005623 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005624 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005625 AddDeclRef(I.getDecl());
5626 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005627 }
5628}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005629
Richard Smith290d8012016-04-06 17:06:00 +00005630// FIXME: Move this out of the main ASTRecordWriter interface.
5631void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5632 Record->push_back(Base.isVirtual());
5633 Record->push_back(Base.isBaseOfClass());
5634 Record->push_back(Base.getAccessSpecifierAsWritten());
5635 Record->push_back(Base.getInheritConstructors());
5636 AddTypeSourceInfo(Base.getTypeSourceInfo());
5637 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005638 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005639 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005640}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005641
Richard Smith645d2cf2016-04-14 00:29:55 +00005642static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5643 ArrayRef<CXXBaseSpecifier> Bases) {
5644 ASTWriter::RecordData Record;
5645 ASTRecordWriter Writer(W, Record);
5646 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005647
Richard Smith645d2cf2016-04-14 00:29:55 +00005648 for (auto &Base : Bases)
5649 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005650
Richard Smith645d2cf2016-04-14 00:29:55 +00005651 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005652}
5653
Richard Smith290d8012016-04-06 17:06:00 +00005654// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005655void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5656 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5657}
5658
Richard Smithaa165cf2016-04-13 21:57:08 +00005659static uint64_t
5660EmitCXXCtorInitializers(ASTWriter &W,
5661 ArrayRef<CXXCtorInitializer *> CtorInits) {
5662 ASTWriter::RecordData Record;
5663 ASTRecordWriter Writer(W, Record);
5664 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005665
Richard Smithaa165cf2016-04-13 21:57:08 +00005666 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005667 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005668 Writer.push_back(CTOR_INITIALIZER_BASE);
5669 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5670 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005671 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005672 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5673 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005674 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005675 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5676 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005677 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005678 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5679 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005680 }
Francois Pichetd583da02010-12-04 09:14:42 +00005681
Richard Smithaa165cf2016-04-13 21:57:08 +00005682 Writer.AddSourceLocation(Init->getMemberLocation());
5683 Writer.AddStmt(Init->getInit());
5684 Writer.AddSourceLocation(Init->getLParenLoc());
5685 Writer.AddSourceLocation(Init->getRParenLoc());
5686 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005687 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005688 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005689 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005690
5691 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005692}
5693
Richard Smithaa165cf2016-04-13 21:57:08 +00005694// FIXME: Move this out of the main ASTRecordWriter interface.
5695void ASTRecordWriter::AddCXXCtorInitializers(
5696 ArrayRef<CXXCtorInitializer *> CtorInits) {
5697 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005698}
5699
Richard Smith290d8012016-04-06 17:06:00 +00005700void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005701 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005702 Record->push_back(Data.IsLambda);
5703 Record->push_back(Data.UserDeclaredConstructor);
5704 Record->push_back(Data.UserDeclaredSpecialMembers);
5705 Record->push_back(Data.Aggregate);
5706 Record->push_back(Data.PlainOldData);
5707 Record->push_back(Data.Empty);
5708 Record->push_back(Data.Polymorphic);
5709 Record->push_back(Data.Abstract);
5710 Record->push_back(Data.IsStandardLayout);
5711 Record->push_back(Data.HasNoNonEmptyBases);
5712 Record->push_back(Data.HasPrivateFields);
5713 Record->push_back(Data.HasProtectedFields);
5714 Record->push_back(Data.HasPublicFields);
5715 Record->push_back(Data.HasMutableFields);
5716 Record->push_back(Data.HasVariantMembers);
5717 Record->push_back(Data.HasOnlyCMembers);
5718 Record->push_back(Data.HasInClassInitializer);
5719 Record->push_back(Data.HasUninitializedReferenceMember);
5720 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005721 Record->push_back(Data.HasInheritedConstructor);
5722 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005723 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5724 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5725 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5726 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5727 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5728 Record->push_back(Data.DefaultedDestructorIsDeleted);
5729 Record->push_back(Data.HasTrivialSpecialMembers);
5730 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5731 Record->push_back(Data.HasIrrelevantDestructor);
5732 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5733 Record->push_back(Data.HasDefaultedDefaultConstructor);
5734 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5735 Record->push_back(Data.HasConstexprDefaultConstructor);
5736 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5737 Record->push_back(Data.ComputedVisibleConversions);
5738 Record->push_back(Data.UserProvidedDefaultConstructor);
5739 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005740 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5741 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005742 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5743 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5744 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieub6adf542017-02-18 02:09:28 +00005745 Record->push_back(Data.ODRHash);
Richard Smith561fb152012-02-25 07:33:38 +00005746 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005747
Richard Smith290d8012016-04-06 17:06:00 +00005748 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005749 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005750 AddCXXBaseSpecifiers(Data.bases());
5751
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005752 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005753 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005754 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005755 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005756
Richard Smith290d8012016-04-06 17:06:00 +00005757 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5758 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005759 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005760 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005761
5762 // Add lambda-specific data.
5763 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005764 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005765 Record->push_back(Lambda.Dependent);
5766 Record->push_back(Lambda.IsGenericLambda);
5767 Record->push_back(Lambda.CaptureDefault);
5768 Record->push_back(Lambda.NumCaptures);
5769 Record->push_back(Lambda.NumExplicitCaptures);
5770 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005771 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005772 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005773 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005774 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005775 AddSourceLocation(Capture.getLocation());
5776 Record->push_back(Capture.isImplicit());
5777 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005778 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005779 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005780 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005781 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005782 break;
5783 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005784 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005785 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005786 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005787 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005788 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005789 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005790 break;
5791 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005792 }
5793 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005794}
5795
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005796void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005797 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005798 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005799 assert(FirstDeclID == NextDeclID &&
5800 FirstTypeID == NextTypeID &&
5801 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005802 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005803 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005804 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005805 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005806
Sebastian Redl07a89a82010-07-30 00:29:29 +00005807 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005808
Richard Smith7f330cd2015-03-18 01:42:29 +00005809 // Note, this will get called multiple times, once one the reader starts up
5810 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005811 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5812 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5813 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005814 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005815 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005816 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005817 NextDeclID = FirstDeclID;
5818 NextTypeID = FirstTypeID;
5819 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005820 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005821 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005822 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005823}
5824
Sebastian Redl539c5062010-08-18 23:57:32 +00005825void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005826 // Always keep the highest ID. See \p TypeRead() for more information.
5827 IdentID &StoredID = IdentifierIDs[II];
5828 if (ID > StoredID)
5829 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005830}
5831
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005832void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005833 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005834 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005835 if (ID > StoredID)
5836 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005837}
5838
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005839void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005840 // Always take the highest-numbered type index. This copes with an interesting
5841 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005842 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005843 // keep the higher-numbered entry so that we can properly write it out to
5844 // the AST file.
5845 TypeIdx &StoredIdx = TypeIdxs[T];
5846 if (Idx.getIndex() >= StoredIdx.getIndex())
5847 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005848}
5849
Sebastian Redl539c5062010-08-18 23:57:32 +00005850void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005851 // Always keep the highest ID. See \p TypeRead() for more information.
5852 SelectorID &StoredID = SelectorIDs[S];
5853 if (ID > StoredID)
5854 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005855}
Douglas Gregor91096292010-10-02 19:29:26 +00005856
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005857void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00005858 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005859 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005860 MacroDefinitions[MD] = ID;
5861}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005862
Douglas Gregore37a85a2011-12-02 17:30:13 +00005863void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5864 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5865 SubmoduleIDs[Mod] = ID;
5866}
5867
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005868void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005869 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00005870 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005871 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005872 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005873 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005874 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005875 // A forward reference was mutated into a definition. Rewrite it.
5876 // FIXME: This happens during template instantiation, should we
5877 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00005878 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5879 "completed a tag from another module but not by instantiation?");
5880 DeclUpdates[RD].push_back(
5881 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005882 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005883 }
5884}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005885
Richard Smithf983f7f2015-10-13 00:23:25 +00005886static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5887 if (D->isFromASTFile())
5888 return true;
5889
Richard Smithf983f7f2015-10-13 00:23:25 +00005890 // The predefined __va_list_tag struct is imported if we imported any decls.
5891 // FIXME: This is a gross hack.
5892 return D == D->getASTContext().getVaListTagDecl();
5893}
5894
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005895void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005896 if (Chain && Chain->isProcessingUpdateRecords()) return;
5897 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00005898 "Should not add lookup results to non-lookup contexts!");
5899
Vassil Vassilev632eac32016-03-16 11:17:04 +00005900 // TU is handled elsewhere.
5901 if (isa<TranslationUnitDecl>(DC))
5902 return;
5903
5904 // Namespaces are handled elsewhere, except for template instantiations of
5905 // FunctionTemplateDecls in namespaces. We are interested in cases where the
5906 // local instantiations are added to an imported context. Only happens when
5907 // adding ADL lookup candidates, for example templated friends.
5908 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
5909 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005910 return;
5911
Richard Smithf983f7f2015-10-13 00:23:25 +00005912 // We're only interested in cases where a local declaration is added to an
5913 // imported context.
5914 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5915 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005916
Richard Smith0f4e2c42015-08-06 04:23:48 +00005917 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00005918 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00005919 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00005920 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5921 // We're adding a visible declaration to a predefined decl context. Ensure
5922 // that we write out all of its lookup results so we don't get a nasty
5923 // surprise when we try to emit its lookup table.
5924 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00005925 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00005926 }
Richard Smithc26d9742016-10-06 20:30:51 +00005927 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005928}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005929
5930void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005931 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005932 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00005933
5934 // We're only interested in cases where a local declaration is added to an
5935 // imported context.
5936 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5937 return;
5938
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005939 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00005940 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005941
5942 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005943 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00005944 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00005945 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005946}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005947
Richard Smith564417a2014-03-20 21:47:22 +00005948void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005949 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00005950 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5951 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005952 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005953 // If we don't already know the exception specification for this redecl
5954 // chain, add an update record for it.
5955 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5956 ->getType()
5957 ->castAs<FunctionProtoType>()
5958 ->getExceptionSpecType()))
5959 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5960 });
Richard Smith564417a2014-03-20 21:47:22 +00005961}
5962
Richard Smith1fa5d642013-05-11 05:45:24 +00005963void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005964 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00005965 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00005966 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005967 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005968 DeclUpdates[D].push_back(
5969 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5970 });
Richard Smith1fa5d642013-05-11 05:45:24 +00005971}
5972
Richard Smithf8134002015-03-10 01:41:22 +00005973void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5974 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005975 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00005976 assert(!WritingAST && "Already writing the AST!");
5977 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00005978 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00005979 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00005980 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5981 });
Richard Smithf8134002015-03-10 01:41:22 +00005982}
5983
Sebastian Redlab238a72011-04-24 16:28:06 +00005984void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005985 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005986 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005987 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005988 return; // Declaration not imported from PCH.
5989
Richard Smith4d235792014-08-07 18:53:08 +00005990 // Implicit function decl from a PCH was defined.
5991 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00005992}
5993
Richard Smithd28ac5b2014-03-22 23:33:22 +00005994void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00005995 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00005996 assert(!WritingAST && "Already writing the AST!");
5997 if (!D->isFromASTFile())
5998 return;
5999
Richard Smith9e2341d2015-03-23 03:25:59 +00006000 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006001}
6002
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006003void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006004 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006005 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006006 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006007 return;
6008
6009 // Since the actual instantiation is delayed, this really means that we need
6010 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006011 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006012 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6013 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006014}
6015
John McCall32791cc2016-01-06 22:34:54 +00006016void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006017 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006018 assert(!WritingAST && "Already writing the AST!");
6019 if (!D->isFromASTFile())
6020 return;
6021
6022 DeclUpdates[D].push_back(
6023 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6024}
6025
Richard Smith4b054b22016-08-24 21:25:37 +00006026void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6027 assert(!WritingAST && "Already writing the AST!");
6028 if (!D->isFromASTFile())
6029 return;
6030
6031 DeclUpdates[D].push_back(
6032 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6033}
6034
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006035void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6036 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006037 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006038 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006039 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006040 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006041
6042 assert(IFD->getDefinition() && "Category on a class without a definition?");
6043 ObjCClassesWithCategories.insert(
6044 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006045}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006046
Eli Friedman276dd182013-09-05 00:02:25 +00006047void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006048 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006049 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006050
6051 // If there is *any* declaration of the entity that's not from an AST file,
6052 // we can skip writing the update record. We make sure that isUsed() triggers
6053 // completion of the redeclaration chain of the entity.
6054 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6055 if (IsLocalDecl(Prev))
6056 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006057
Aaron Ballman4f45b712014-03-21 15:22:56 +00006058 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006059}
Alexey Bataev97720002014-11-11 04:05:39 +00006060
6061void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006062 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006063 assert(!WritingAST && "Already writing the AST!");
6064 if (!D->isFromASTFile())
6065 return;
6066
6067 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6068}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006069
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006070void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6071 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006072 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006073 assert(!WritingAST && "Already writing the AST!");
6074 if (!D->isFromASTFile())
6075 return;
6076
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006077 DeclUpdates[D].push_back(
6078 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006079}
6080
Richard Smith4caa4492015-05-15 02:34:32 +00006081void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006082 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006083 assert(!WritingAST && "Already writing the AST!");
6084 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006085 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006086}
Alex Denisovfde64952015-06-26 05:28:36 +00006087
6088void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6089 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006090 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006091 assert(!WritingAST && "Already writing the AST!");
6092 if (!Record->isFromASTFile())
6093 return;
6094 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6095}
Richard Smithc26d9742016-10-06 20:30:51 +00006096
6097void ASTWriter::AddedCXXTemplateSpecialization(
6098 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6099 assert(!WritingAST && "Already writing the AST!");
6100
6101 if (!TD->getFirstDecl()->isFromASTFile())
6102 return;
6103 if (Chain && Chain->isProcessingUpdateRecords())
6104 return;
6105
6106 DeclsToEmitEvenIfUnreferenced.push_back(D);
6107}
6108
6109void ASTWriter::AddedCXXTemplateSpecialization(
6110 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6111 assert(!WritingAST && "Already writing the AST!");
6112
6113 if (!TD->getFirstDecl()->isFromASTFile())
6114 return;
6115 if (Chain && Chain->isProcessingUpdateRecords())
6116 return;
6117
6118 DeclsToEmitEvenIfUnreferenced.push_back(D);
6119}
6120
6121void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6122 const FunctionDecl *D) {
6123 assert(!WritingAST && "Already writing the AST!");
6124
6125 if (!TD->getFirstDecl()->isFromASTFile())
6126 return;
6127 if (Chain && Chain->isProcessingUpdateRecords())
6128 return;
6129
6130 DeclsToEmitEvenIfUnreferenced.push_back(D);
6131}