blob: 1c0db14ced148606b5dd4df8993657f45b2a5e22 [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"
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000038#include "clang/Basic/MemoryBufferCache.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000039#include "clang/Basic/Module.h"
40#include "clang/Basic/ObjCRuntime.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000041#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000042#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000043#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000044#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000045#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000046#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000047#include "clang/Lex/HeaderSearch.h"
48#include "clang/Lex/HeaderSearchOptions.h"
49#include "clang/Lex/MacroInfo.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000050#include "clang/Lex/ModuleMap.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000051#include "clang/Lex/PreprocessingRecord.h"
52#include "clang/Lex/Preprocessor.h"
53#include "clang/Lex/PreprocessorOptions.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000054#include "clang/Lex/Token.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000055#include "clang/Sema/IdentifierResolver.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000056#include "clang/Sema/ObjCMethodList.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000057#include "clang/Sema/Sema.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000058#include "clang/Sema/Weak.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000059#include "clang/Serialization/ASTReader.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000060#include "clang/Serialization/Module.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000061#include "clang/Serialization/ModuleFileExtension.h"
Richard Smithb5aaf5a2015-09-01 02:35:58 +000062#include "clang/Serialization/SerializationDiagnostic.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000063#include "llvm/ADT/APFloat.h"
64#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000065#include "llvm/ADT/Hashing.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000066#include "llvm/ADT/IntrusiveRefCntPtr.h"
67#include "llvm/ADT/Optional.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000068#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000069#include "llvm/ADT/SmallSet.h"
70#include "llvm/ADT/SmallString.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000071#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000072#include "llvm/Bitcode/BitCodes.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073#include "llvm/Bitcode/BitstreamWriter.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000074#include "llvm/Support/Casting.h"
Richard Smithaada85c2016-02-06 02:06:43 +000075#include "llvm/Support/Compression.h"
Justin Bognere1c147c2014-03-28 22:03:19 +000076#include "llvm/Support/EndianStream.h"
George Rimarc39f5492017-01-17 15:45:31 +000077#include "llvm/Support/Error.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000078#include "llvm/Support/ErrorHandling.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000079#include "llvm/Support/MemoryBuffer.h"
Justin Bognerbb094f02014-04-18 19:57:06 +000080#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000081#include "llvm/Support/Path.h"
Ben Langmuir487ea142014-10-23 18:05:36 +000082#include "llvm/Support/Process.h"
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000083#include "llvm/Support/SHA1.h"
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000084#include "llvm/Support/raw_ostream.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000085#include <algorithm>
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +000086#include <cassert>
87#include <cstdint>
88#include <cstdlib>
89#include <cstring>
90#include <deque>
91#include <limits>
92#include <new>
93#include <tuple>
Douglas Gregor925296b2011-07-19 16:10:42 +000094#include <utility>
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +000095
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000097using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098
Sebastian Redl3df5a082010-07-30 17:03:48 +000099template <typename T, typename Allocator>
Reid Kleckner012665e2015-05-21 00:13:09 +0000100static StringRef bytes(const std::vector<T, Allocator> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000101 if (v.empty()) return StringRef();
102 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000103 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000104}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000105
106template <typename T>
Reid Kleckner012665e2015-05-21 00:13:09 +0000107static StringRef bytes(const SmallVectorImpl<T> &v) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000108 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +0000109 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +0000110}
111
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112//===----------------------------------------------------------------------===//
113// Type serialization
114//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +0000115
Richard Smith290d8012016-04-06 17:06:00 +0000116namespace clang {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000119 ASTWriter &Writer;
Richard Smith69c82bf2016-04-01 22:52:03 +0000120 ASTRecordWriter Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +0000123 TypeCode Code;
Richard Smith01b2cb42014-07-26 06:37:51 +0000124 /// \brief Abbreviation to use for the record, if any.
125 unsigned AbbrevToUse;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126
Richard Smith290d8012016-04-06 17:06:00 +0000127 public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000128 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Richard Smith290d8012016-04-06 17:06:00 +0000129 : Writer(Writer), Record(Writer, Record), Code((TypeCode)0), AbbrevToUse(0) { }
130
131 uint64_t Emit() {
132 return Record.Emit(Code, AbbrevToUse);
133 }
134
135 void Visit(QualType T) {
136 if (T.hasLocalNonFastQualifiers()) {
137 Qualifiers Qs = T.getLocalQualifiers();
138 Record.AddTypeRef(T.getLocalUnqualifiedType());
139 Record.push_back(Qs.getAsOpaqueValue());
140 Code = TYPE_EXT_QUAL;
141 AbbrevToUse = Writer.TypeExtQualAbbrev;
142 } else {
143 switch (T->getTypeClass()) {
144 // For all of the concrete, non-dependent types, call the
145 // appropriate visitor function.
146#define TYPE(Class, Base) \
147 case Type::Class: Visit##Class##Type(cast<Class##Type>(T)); break;
148#define ABSTRACT_TYPE(Class, Base)
149#include "clang/AST/TypeNodes.def"
150 }
151 }
152 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153
154 void VisitArrayType(const ArrayType *T);
155 void VisitFunctionType(const FunctionType *T);
156 void VisitTagType(const TagType *T);
157
158#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
159#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160#include "clang/AST/TypeNodes.def"
161 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000162
Richard Smith290d8012016-04-06 17:06:00 +0000163} // end namespace clang
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +0000166 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000170 Record.AddTypeRef(T->getElementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000171 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000172}
173
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000174void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000175 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000176 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177}
178
Reid Kleckner8a365022013-06-24 17:51:48 +0000179void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000180 Record.AddTypeRef(T->getOriginalType());
Reid Kleckner8a365022013-06-24 17:51:48 +0000181 Code = TYPE_DECAYED;
182}
183
Reid Kleckner0503a872013-12-05 01:23:43 +0000184void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000185 Record.AddTypeRef(T->getOriginalType());
186 Record.AddTypeRef(T->getAdjustedType());
Reid Kleckner0503a872013-12-05 01:23:43 +0000187 Code = TYPE_ADJUSTED;
188}
189
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000190void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000191 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000192 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193}
194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000196 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Richard Smith0f538462011-04-12 10:38:03 +0000197 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000202 Record.AddTypeRef(T->getPointeeTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000207 Record.AddTypeRef(T->getPointeeType());
208 Record.AddTypeRef(QualType(T->getClass(), 0));
Sebastian Redl539c5062010-08-18 23:57:32 +0000209 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000210}
211
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000213 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000215 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216}
217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000220 Record.AddAPInt(T->getSize());
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227}
228
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000230 VisitArrayType(T);
Richard Smith69c82bf2016-04-01 22:52:03 +0000231 Record.AddSourceLocation(T->getLBracketLoc());
232 Record.AddSourceLocation(T->getRBracketLoc());
Richard Smith290d8012016-04-06 17:06:00 +0000233 Record.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000234 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235}
236
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000237void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000238 Record.AddTypeRef(T->getElementType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000240 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000246 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247}
248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000250 Record.AddTypeRef(T->getReturnType());
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000251 FunctionType::ExtInfo C = T->getExtInfo();
252 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000253 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000254 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000255 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000256 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000257 Record.push_back(C.getProducesResult());
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000258 Record.push_back(C.getNoCallerSavedRegs());
Richard Smith01b2cb42014-07-26 06:37:51 +0000259
260 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
261 AbbrevToUse = 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000264void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000265 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000266 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000267}
268
Richard Smith290d8012016-04-06 17:06:00 +0000269static void addExceptionSpec(const FunctionProtoType *T,
270 ASTRecordWriter &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000271 Record.push_back(T->getExceptionSpecType());
272 if (T->getExceptionSpecType() == EST_Dynamic) {
273 Record.push_back(T->getNumExceptions());
274 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000275 Record.AddTypeRef(T->getExceptionType(I));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000276 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
Richard Smith290d8012016-04-06 17:06:00 +0000277 Record.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000278 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000279 Record.AddDeclRef(T->getExceptionSpecDecl());
280 Record.AddDeclRef(T->getExceptionSpecTemplate());
Richard Smithd3b5c9082012-07-27 04:22:15 +0000281 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000282 Record.AddDeclRef(T->getExceptionSpecDecl());
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000283 }
Richard Smith564417a2014-03-20 21:47:22 +0000284}
285
286void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
287 VisitFunctionType(T);
Richard Smith01b2cb42014-07-26 06:37:51 +0000288
Richard Smith564417a2014-03-20 21:47:22 +0000289 Record.push_back(T->isVariadic());
290 Record.push_back(T->hasTrailingReturn());
291 Record.push_back(T->getTypeQuals());
292 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Richard Smith290d8012016-04-06 17:06:00 +0000293 addExceptionSpec(T, Record);
Richard Smith01b2cb42014-07-26 06:37:51 +0000294
295 Record.push_back(T->getNumParams());
296 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000297 Record.AddTypeRef(T->getParamType(I));
Richard Smith01b2cb42014-07-26 06:37:51 +0000298
John McCall18afab72016-03-01 00:49:02 +0000299 if (T->hasExtParameterInfos()) {
300 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
301 Record.push_back(T->getExtParameterInfo(I).getOpaqueValue());
302 }
303
Richard Smith01b2cb42014-07-26 06:37:51 +0000304 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
John McCall18afab72016-03-01 00:49:02 +0000305 T->getRefQualifier() || T->getExceptionSpecType() != EST_None ||
306 T->hasExtParameterInfos())
Richard Smith01b2cb42014-07-26 06:37:51 +0000307 AbbrevToUse = 0;
308
Sebastian Redl539c5062010-08-18 23:57:32 +0000309 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000310}
311
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000313 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000314 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000315}
John McCallb96ec562009-12-04 22:46:56 +0000316
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000317void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000318 Record.AddDeclRef(T->getDecl());
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000319 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
Richard Smith69c82bf2016-04-01 22:52:03 +0000320 Record.AddTypeRef(T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000321 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000322}
323
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000324void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Richard Smith290d8012016-04-06 17:06:00 +0000325 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000327}
328
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000329void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000330 Record.AddTypeRef(T->getUnderlyingType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000331 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332}
333
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000334void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000335 Record.AddTypeRef(T->getUnderlyingType());
Richard Smith290d8012016-04-06 17:06:00 +0000336 Record.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000337 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000338}
339
Alexis Hunte852b102011-05-24 22:41:36 +0000340void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000341 Record.AddTypeRef(T->getBaseType());
342 Record.AddTypeRef(T->getUnderlyingType());
Alexis Hunte852b102011-05-24 22:41:36 +0000343 Record.push_back(T->getUTTKind());
344 Code = TYPE_UNARY_TRANSFORM;
345}
346
Richard Smith30482bc2011-02-20 03:19:35 +0000347void ASTTypeWriter::VisitAutoType(const AutoType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000348 Record.AddTypeRef(T->getDeducedType());
Richard Smithe301ba22015-11-11 02:02:15 +0000349 Record.push_back((unsigned)T->getKeyword());
Richard Smith27d807c2013-04-30 13:56:41 +0000350 if (T->getDeducedType().isNull())
351 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000352 Code = TYPE_AUTO;
353}
354
Richard Smith600b5262017-01-26 20:40:47 +0000355void ASTTypeWriter::VisitDeducedTemplateSpecializationType(
356 const DeducedTemplateSpecializationType *T) {
357 Record.AddTemplateName(T->getTemplateName());
358 Record.AddTypeRef(T->getDeducedType());
359 if (T->getDeducedType().isNull())
360 Record.push_back(T->isDependentType());
361 Code = TYPE_DEDUCED_TEMPLATE_SPECIALIZATION;
362}
363
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000364void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000365 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000366 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000367 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000368 "Cannot serialize in the middle of a type definition");
369}
370
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000371void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000372 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000373 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000374}
375
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000377 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000378 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000379}
380
John McCall81904512011-01-06 01:58:22 +0000381void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000382 Record.AddTypeRef(T->getModifiedType());
383 Record.AddTypeRef(T->getEquivalentType());
John McCall81904512011-01-06 01:58:22 +0000384 Record.push_back(T->getAttrKind());
385 Code = TYPE_ATTRIBUTED;
386}
387
Mike Stump11289f42009-09-09 15:08:12 +0000388void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000389ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000390 const SubstTemplateTypeParmType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000391 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
392 Record.AddTypeRef(T->getReplacementType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000393 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000394}
395
396void
Douglas Gregorada4b792011-01-14 02:55:32 +0000397ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
398 const SubstTemplateTypeParmPackType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000399 Record.AddTypeRef(QualType(T->getReplacedParameter(), 0));
400 Record.AddTemplateArgument(T->getArgumentPack());
Douglas Gregorada4b792011-01-14 02:55:32 +0000401 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
402}
403
404void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000405ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000406 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000407 Record.push_back(T->isDependentType());
Richard Smith69c82bf2016-04-01 22:52:03 +0000408 Record.AddTemplateName(T->getTemplateName());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000409 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000410 for (const auto &ArgI : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000411 Record.AddTemplateArgument(ArgI);
412 Record.AddTypeRef(T->isTypeAlias() ? T->getAliasedType()
413 : T->isCanonicalUnqualified()
414 ? QualType()
415 : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000416 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000417}
418
419void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000420ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000421 VisitArrayType(T);
Richard Smith290d8012016-04-06 17:06:00 +0000422 Record.AddStmt(T->getSizeExpr());
Richard Smith69c82bf2016-04-01 22:52:03 +0000423 Record.AddSourceRange(T->getBracketsRange());
Sebastian Redl539c5062010-08-18 23:57:32 +0000424 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000425}
426
427void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000428ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000429 const DependentSizedExtVectorType *T) {
Alex Lorenz00aee432017-03-22 10:04:48 +0000430 Record.AddTypeRef(T->getElementType());
431 Record.AddStmt(T->getSizeExpr());
432 Record.AddSourceLocation(T->getAttributeLoc());
433 Code = TYPE_DEPENDENT_SIZED_EXT_VECTOR;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000434}
435
436void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000437ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000438 Record.push_back(T->getDepth());
439 Record.push_back(T->getIndex());
440 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000441 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000442 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000443}
444
445void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000446ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000447 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000448 Record.AddNestedNameSpecifier(T->getQualifier());
449 Record.AddIdentifierRef(T->getIdentifier());
450 Record.AddTypeRef(
451 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000452 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000453}
454
455void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000456ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000457 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000458 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000459 Record.AddNestedNameSpecifier(T->getQualifier());
460 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000461 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000462 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000463 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000464 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000465}
466
Douglas Gregord2fa7662010-12-20 02:24:11 +0000467void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000468 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000469 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000470 Record.push_back(*NumExpansions + 1);
471 else
472 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000473 Code = TYPE_PACK_EXPANSION;
474}
475
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000476void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000477 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000478 Code = TYPE_PAREN;
479}
480
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000481void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000482 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000483 Record.AddNestedNameSpecifier(T->getQualifier());
484 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000485 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000486}
487
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000488void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000489 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
490 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000491 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000492}
493
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000494void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000495 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000496 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000497}
498
Manman Rene6be26c2016-09-13 17:25:08 +0000499void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
500 Record.AddDeclRef(T->getDecl());
501 Record.push_back(T->getNumProtocols());
502 for (const auto *I : T->quals())
503 Record.AddDeclRef(I);
504 Code = TYPE_OBJC_TYPE_PARAM;
505}
506
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000507void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000508 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000509 Record.push_back(T->getTypeArgsAsWritten().size());
510 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000511 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000512 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000513 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000514 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000515 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000516 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000517}
518
Steve Narofffb4330f2009-06-17 22:40:22 +0000519void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000520ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000521 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000522 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000523}
524
Eli Friedman0dfb8892011-10-06 23:00:33 +0000525void
526ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000527 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000528 Code = TYPE_ATOMIC;
529}
530
Xiuli Pan9c14e282016-01-09 12:53:17 +0000531void
532ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000533 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000534 Record.push_back(T->isReadOnly());
535 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000536}
537
John McCall8f115c62009-10-16 21:56:05 +0000538namespace {
539
540class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000541 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000542
543public:
Richard Smith290d8012016-04-06 17:06:00 +0000544 TypeLocWriter(ASTRecordWriter &Record)
545 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000546
John McCall17001972009-10-18 01:05:36 +0000547#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000548#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000549 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000550#include "clang/AST/TypeLocNodes.def"
551
John McCall17001972009-10-18 01:05:36 +0000552 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
553 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000554};
555
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000556} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000557
John McCall17001972009-10-18 01:05:36 +0000558void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
559 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000560}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000561
John McCall17001972009-10-18 01:05:36 +0000562void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000563 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000564 if (TL.needsExtraLocalData()) {
565 Record.push_back(TL.getWrittenTypeSpec());
566 Record.push_back(TL.getWrittenSignSpec());
567 Record.push_back(TL.getWrittenWidthSpec());
568 Record.push_back(TL.hasModeAttr());
569 }
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::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000573 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000574}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000575
John McCall17001972009-10-18 01:05:36 +0000576void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000577 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000578}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000579
Reid Kleckner8a365022013-06-24 17:51:48 +0000580void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
581 // nothing to do
582}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000583
Reid Kleckner0503a872013-12-05 01:23:43 +0000584void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
585 // nothing to do
586}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000587
John McCall17001972009-10-18 01:05:36 +0000588void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000589 Record.AddSourceLocation(TL.getCaretLoc());
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::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000593 Record.AddSourceLocation(TL.getAmpLoc());
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::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000597 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000598}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000599
John McCall17001972009-10-18 01:05:36 +0000600void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000601 Record.AddSourceLocation(TL.getStarLoc());
602 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000603}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000604
John McCall17001972009-10-18 01:05:36 +0000605void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000606 Record.AddSourceLocation(TL.getLBracketLoc());
607 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000608 Record.push_back(TL.getSizeExpr() ? 1 : 0);
609 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000610 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000611}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000612
John McCall17001972009-10-18 01:05:36 +0000613void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
614 VisitArrayTypeLoc(TL);
615}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000616
John McCall17001972009-10-18 01:05:36 +0000617void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
618 VisitArrayTypeLoc(TL);
619}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000620
John McCall17001972009-10-18 01:05:36 +0000621void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
622 VisitArrayTypeLoc(TL);
623}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000624
John McCall17001972009-10-18 01:05:36 +0000625void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
626 DependentSizedArrayTypeLoc TL) {
627 VisitArrayTypeLoc(TL);
628}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000629
John McCall17001972009-10-18 01:05:36 +0000630void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
631 DependentSizedExtVectorTypeLoc 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::VisitVectorTypeLoc(VectorTypeLoc 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::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000640 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000641}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000642
John McCall17001972009-10-18 01:05:36 +0000643void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000644 Record.AddSourceLocation(TL.getLocalRangeBegin());
645 Record.AddSourceLocation(TL.getLParenLoc());
646 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000647 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000648 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000649 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000650 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000651}
652void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
653 VisitFunctionTypeLoc(TL);
654}
655void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
656 VisitFunctionTypeLoc(TL);
657}
John McCallb96ec562009-12-04 22:46:56 +0000658void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000659 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000660}
John McCall17001972009-10-18 01:05:36 +0000661void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000662 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000663}
Manman Rene6be26c2016-09-13 17:25:08 +0000664void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
665 if (TL.getNumProtocols()) {
666 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
667 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
668 }
669 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
670 Record.AddSourceLocation(TL.getProtocolLoc(i));
671}
John McCall17001972009-10-18 01:05:36 +0000672void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000673 Record.AddSourceLocation(TL.getTypeofLoc());
674 Record.AddSourceLocation(TL.getLParenLoc());
675 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000676}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000677
John McCall17001972009-10-18 01:05:36 +0000678void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000679 Record.AddSourceLocation(TL.getTypeofLoc());
680 Record.AddSourceLocation(TL.getLParenLoc());
681 Record.AddSourceLocation(TL.getRParenLoc());
682 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000683}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000684
John McCall17001972009-10-18 01:05:36 +0000685void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000686 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000687}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000688
Alexis Hunte852b102011-05-24 22:41:36 +0000689void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000690 Record.AddSourceLocation(TL.getKWLoc());
691 Record.AddSourceLocation(TL.getLParenLoc());
692 Record.AddSourceLocation(TL.getRParenLoc());
693 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000694}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000695
Richard Smith30482bc2011-02-20 03:19:35 +0000696void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000697 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000698}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000699
Richard Smith600b5262017-01-26 20:40:47 +0000700void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
701 DeducedTemplateSpecializationTypeLoc TL) {
702 Record.AddSourceLocation(TL.getTemplateNameLoc());
703}
704
John McCall17001972009-10-18 01:05:36 +0000705void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc 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 McCall17001972009-10-18 01:05:36 +0000709void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000710 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000711}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000712
John McCall81904512011-01-06 01:58:22 +0000713void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000714 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000715 if (TL.hasAttrOperand()) {
716 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000717 Record.AddSourceLocation(range.getBegin());
718 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000719 }
720 if (TL.hasAttrExprOperand()) {
721 Expr *operand = TL.getAttrExprOperand();
722 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000723 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000724 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000725 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000726 }
727}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000728
John McCall17001972009-10-18 01:05:36 +0000729void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000730 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000731}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000732
John McCallcebee162009-10-18 09:09:24 +0000733void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
734 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000735 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000736}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000737
Douglas Gregorada4b792011-01-14 02:55:32 +0000738void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
739 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000740 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000741}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000742
John McCall17001972009-10-18 01:05:36 +0000743void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
744 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000745 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
746 Record.AddSourceLocation(TL.getTemplateNameLoc());
747 Record.AddSourceLocation(TL.getLAngleLoc());
748 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000749 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000750 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
751 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000752}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000753
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000754void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000755 Record.AddSourceLocation(TL.getLParenLoc());
756 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000757}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000758
Abramo Bagnara6150c882010-05-11 21:36:43 +0000759void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000760 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
761 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000762}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000763
John McCalle78aac42010-03-10 03:28:59 +0000764void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000765 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000766}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000767
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000768void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000769 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
770 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
771 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000772}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000773
John McCallc392f372010-06-11 00:33:02 +0000774void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
775 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000776 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
777 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
778 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
779 Record.AddSourceLocation(TL.getTemplateNameLoc());
780 Record.AddSourceLocation(TL.getLAngleLoc());
781 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000782 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000783 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
784 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000785}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000786
Douglas Gregord2fa7662010-12-20 02:24:11 +0000787void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000788 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000789}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000790
John McCall17001972009-10-18 01:05:36 +0000791void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000792 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000793}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000794
John McCall8b07ec22010-05-15 11:32:37 +0000795void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
796 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000797 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
798 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000799 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000800 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
801 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
802 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000803 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000804 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000805}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000806
John McCallfc93cf92009-10-22 22:37:11 +0000807void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000808 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000809}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000810
Eli Friedman0dfb8892011-10-06 23:00:33 +0000811void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000812 Record.AddSourceLocation(TL.getKWLoc());
813 Record.AddSourceLocation(TL.getLParenLoc());
814 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000815}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000816
Xiuli Pan9c14e282016-01-09 12:53:17 +0000817void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000818 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000819}
John McCall8f115c62009-10-16 21:56:05 +0000820
Richard Smith01b2cb42014-07-26 06:37:51 +0000821void ASTWriter::WriteTypeAbbrevs() {
822 using namespace llvm;
823
David Blaikieb44f0bf2017-01-04 22:36:43 +0000824 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000825
826 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000827 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000828 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
829 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000831 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000832
833 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000834 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000835 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
836 // FunctionType
837 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
838 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
839 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
840 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
841 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
842 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000843 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Richard Smith01b2cb42014-07-26 06:37:51 +0000844 // FunctionProtoType
845 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
846 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
847 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
848 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
849 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
850 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
851 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
852 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000853 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000854}
855
Chris Lattner19cea4e2009-04-22 05:57:30 +0000856//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000857// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000858//===----------------------------------------------------------------------===//
859
Chris Lattner28fa4e62009-04-26 22:26:21 +0000860static void EmitBlockID(unsigned ID, const char *Name,
861 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000862 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000863 Record.clear();
864 Record.push_back(ID);
865 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
866
867 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000868 if (!Name || Name[0] == 0)
869 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000870 Record.clear();
871 while (*Name)
872 Record.push_back(*Name++);
873 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
874}
875
876static void EmitRecordID(unsigned ID, const char *Name,
877 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000878 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000879 Record.clear();
880 Record.push_back(ID);
881 while (*Name)
882 Record.push_back(*Name++);
883 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000884}
885
886static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000887 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000888#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000889 RECORD(STMT_STOP);
890 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000891 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000892 RECORD(STMT_NULL);
893 RECORD(STMT_COMPOUND);
894 RECORD(STMT_CASE);
895 RECORD(STMT_DEFAULT);
896 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000897 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000898 RECORD(STMT_IF);
899 RECORD(STMT_SWITCH);
900 RECORD(STMT_WHILE);
901 RECORD(STMT_DO);
902 RECORD(STMT_FOR);
903 RECORD(STMT_GOTO);
904 RECORD(STMT_INDIRECT_GOTO);
905 RECORD(STMT_CONTINUE);
906 RECORD(STMT_BREAK);
907 RECORD(STMT_RETURN);
908 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000909 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000910 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000911 RECORD(EXPR_PREDEFINED);
912 RECORD(EXPR_DECL_REF);
913 RECORD(EXPR_INTEGER_LITERAL);
914 RECORD(EXPR_FLOATING_LITERAL);
915 RECORD(EXPR_IMAGINARY_LITERAL);
916 RECORD(EXPR_STRING_LITERAL);
917 RECORD(EXPR_CHARACTER_LITERAL);
918 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000919 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000920 RECORD(EXPR_UNARY_OPERATOR);
921 RECORD(EXPR_SIZEOF_ALIGN_OF);
922 RECORD(EXPR_ARRAY_SUBSCRIPT);
923 RECORD(EXPR_CALL);
924 RECORD(EXPR_MEMBER);
925 RECORD(EXPR_BINARY_OPERATOR);
926 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
927 RECORD(EXPR_CONDITIONAL_OPERATOR);
928 RECORD(EXPR_IMPLICIT_CAST);
929 RECORD(EXPR_CSTYLE_CAST);
930 RECORD(EXPR_COMPOUND_LITERAL);
931 RECORD(EXPR_EXT_VECTOR_ELEMENT);
932 RECORD(EXPR_INIT_LIST);
933 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000934 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000935 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000936 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000937 RECORD(EXPR_VA_ARG);
938 RECORD(EXPR_ADDR_LABEL);
939 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000940 RECORD(EXPR_CHOOSE);
941 RECORD(EXPR_GNU_NULL);
942 RECORD(EXPR_SHUFFLE_VECTOR);
943 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000944 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000945 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000946 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000947 RECORD(EXPR_OBJC_ARRAY_LITERAL);
948 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000949 RECORD(EXPR_OBJC_ENCODE);
950 RECORD(EXPR_OBJC_SELECTOR_EXPR);
951 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
952 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
953 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
954 RECORD(EXPR_OBJC_KVC_REF_EXPR);
955 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000956 RECORD(STMT_OBJC_FOR_COLLECTION);
957 RECORD(STMT_OBJC_CATCH);
958 RECORD(STMT_OBJC_FINALLY);
959 RECORD(STMT_OBJC_AT_TRY);
960 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
961 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000962 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000963 RECORD(STMT_CXX_CATCH);
964 RECORD(STMT_CXX_TRY);
965 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000966 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000967 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000968 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000969 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000970 RECORD(EXPR_CXX_STATIC_CAST);
971 RECORD(EXPR_CXX_DYNAMIC_CAST);
972 RECORD(EXPR_CXX_REINTERPRET_CAST);
973 RECORD(EXPR_CXX_CONST_CAST);
974 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000975 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000976 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000977 RECORD(EXPR_CXX_BOOL_LITERAL);
978 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000979 RECORD(EXPR_CXX_TYPEID_EXPR);
980 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000981 RECORD(EXPR_CXX_THIS);
982 RECORD(EXPR_CXX_THROW);
983 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +0000984 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000985 RECORD(EXPR_CXX_BIND_TEMPORARY);
986 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
987 RECORD(EXPR_CXX_NEW);
988 RECORD(EXPR_CXX_DELETE);
989 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
990 RECORD(EXPR_EXPR_WITH_CLEANUPS);
991 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
992 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
993 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
994 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
995 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +0000996 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000997 RECORD(EXPR_CXX_NOEXCEPT);
998 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +0000999 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
1000 RECORD(EXPR_TYPE_TRAIT);
1001 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001002 RECORD(EXPR_PACK_EXPANSION);
1003 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001004 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001005 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001006 RECORD(EXPR_FUNCTION_PARM_PACK);
1007 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001008 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001009 RECORD(EXPR_CXX_UUIDOF_EXPR);
1010 RECORD(EXPR_CXX_UUIDOF_TYPE);
1011 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001012#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001013}
Mike Stump11289f42009-09-09 15:08:12 +00001014
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001015void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001016 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001017 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001018
Sebastian Redl539c5062010-08-18 23:57:32 +00001019#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1020#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001021
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001022 // Control Block.
1023 BLOCK(CONTROL_BLOCK);
1024 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001025 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001026 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001027 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001028 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001029 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001030 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001031 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001032 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001033
1034 BLOCK(OPTIONS_BLOCK);
1035 RECORD(LANGUAGE_OPTIONS);
1036 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001037 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001038 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001039 RECORD(PREPROCESSOR_OPTIONS);
1040
Douglas Gregor108cb222012-10-19 00:45:00 +00001041 BLOCK(INPUT_FILES_BLOCK);
1042 RECORD(INPUT_FILE);
1043
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001044 // AST Top-Level Block.
1045 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001046 RECORD(TYPE_OFFSET);
1047 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001048 RECORD(IDENTIFIER_OFFSET);
1049 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001050 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001051 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001052 RECORD(SPECIAL_TYPES);
1053 RECORD(STATISTICS);
1054 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001055 RECORD(SELECTOR_OFFSETS);
1056 RECORD(METHOD_POOL);
1057 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001058 RECORD(SOURCE_LOCATION_OFFSETS);
1059 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001060 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001061 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001062 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001063 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001064 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001065 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001066 RECORD(SEMA_DECL_REFS);
1067 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1068 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001069 RECORD(UPDATE_VISIBLE);
1070 RECORD(DECL_UPDATE_OFFSETS);
1071 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001072 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001073 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001074 RECORD(FP_PRAGMA_OPTIONS);
1075 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001076 RECORD(OPENCL_EXTENSION_TYPES);
1077 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001078 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001079 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001080 RECORD(MODULE_OFFSET_MAP);
1081 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001082 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001083 RECORD(FILE_SORTED_DECLS);
1084 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001085 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001086 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001087 RECORD(INTERESTING_IDENTIFIERS);
1088 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001089 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001090 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001091 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001092 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001093 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001094 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001095 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00001096 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +00001097
Chris Lattner28fa4e62009-04-26 22:26:21 +00001098 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001099 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001100 RECORD(SM_SLOC_FILE_ENTRY);
1101 RECORD(SM_SLOC_BUFFER_ENTRY);
1102 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001103 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001104 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner28fa4e62009-04-26 22:26:21 +00001106 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001107 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001108 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001109 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001110 RECORD(PP_MACRO_OBJECT_LIKE);
1111 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001112 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001113
Richard Smithfa715652015-08-31 22:43:10 +00001114 // Submodule Block.
1115 BLOCK(SUBMODULE_BLOCK);
1116 RECORD(SUBMODULE_METADATA);
1117 RECORD(SUBMODULE_DEFINITION);
1118 RECORD(SUBMODULE_UMBRELLA_HEADER);
1119 RECORD(SUBMODULE_HEADER);
1120 RECORD(SUBMODULE_TOPHEADER);
1121 RECORD(SUBMODULE_UMBRELLA_DIR);
1122 RECORD(SUBMODULE_IMPORTS);
1123 RECORD(SUBMODULE_EXPORTS);
1124 RECORD(SUBMODULE_REQUIRES);
1125 RECORD(SUBMODULE_EXCLUDED_HEADER);
1126 RECORD(SUBMODULE_LINK_LIBRARY);
1127 RECORD(SUBMODULE_CONFIG_MACRO);
1128 RECORD(SUBMODULE_CONFLICT);
1129 RECORD(SUBMODULE_PRIVATE_HEADER);
1130 RECORD(SUBMODULE_TEXTUAL_HEADER);
1131 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001132 RECORD(SUBMODULE_INITIALIZERS);
Richard Smithfa715652015-08-31 22:43:10 +00001133
1134 // Comments Block.
1135 BLOCK(COMMENTS_BLOCK);
1136 RECORD(COMMENTS_RAW_COMMENT);
1137
Douglas Gregor12bfa382009-10-17 00:13:19 +00001138 // Decls and Types block.
1139 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001140 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001141 RECORD(TYPE_COMPLEX);
1142 RECORD(TYPE_POINTER);
1143 RECORD(TYPE_BLOCK_POINTER);
1144 RECORD(TYPE_LVALUE_REFERENCE);
1145 RECORD(TYPE_RVALUE_REFERENCE);
1146 RECORD(TYPE_MEMBER_POINTER);
1147 RECORD(TYPE_CONSTANT_ARRAY);
1148 RECORD(TYPE_INCOMPLETE_ARRAY);
1149 RECORD(TYPE_VARIABLE_ARRAY);
1150 RECORD(TYPE_VECTOR);
1151 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001152 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001153 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001154 RECORD(TYPE_TYPEDEF);
1155 RECORD(TYPE_TYPEOF_EXPR);
1156 RECORD(TYPE_TYPEOF);
1157 RECORD(TYPE_RECORD);
1158 RECORD(TYPE_ENUM);
1159 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001160 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001161 RECORD(TYPE_DECLTYPE);
1162 RECORD(TYPE_ELABORATED);
1163 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1164 RECORD(TYPE_UNRESOLVED_USING);
1165 RECORD(TYPE_INJECTED_CLASS_NAME);
1166 RECORD(TYPE_OBJC_OBJECT);
1167 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1168 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1169 RECORD(TYPE_DEPENDENT_NAME);
1170 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1171 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1172 RECORD(TYPE_PAREN);
1173 RECORD(TYPE_PACK_EXPANSION);
1174 RECORD(TYPE_ATTRIBUTED);
1175 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001176 RECORD(TYPE_AUTO);
1177 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001178 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001179 RECORD(TYPE_DECAYED);
1180 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001181 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001182 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001183 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001184 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001185 RECORD(DECL_ENUM);
1186 RECORD(DECL_RECORD);
1187 RECORD(DECL_ENUM_CONSTANT);
1188 RECORD(DECL_FUNCTION);
1189 RECORD(DECL_OBJC_METHOD);
1190 RECORD(DECL_OBJC_INTERFACE);
1191 RECORD(DECL_OBJC_PROTOCOL);
1192 RECORD(DECL_OBJC_IVAR);
1193 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001194 RECORD(DECL_OBJC_CATEGORY);
1195 RECORD(DECL_OBJC_CATEGORY_IMPL);
1196 RECORD(DECL_OBJC_IMPLEMENTATION);
1197 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1198 RECORD(DECL_OBJC_PROPERTY);
1199 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001200 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001201 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001202 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001203 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001204 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001205 RECORD(DECL_FILE_SCOPE_ASM);
1206 RECORD(DECL_BLOCK);
1207 RECORD(DECL_CONTEXT_LEXICAL);
1208 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001209 RECORD(DECL_NAMESPACE);
1210 RECORD(DECL_NAMESPACE_ALIAS);
1211 RECORD(DECL_USING);
1212 RECORD(DECL_USING_SHADOW);
1213 RECORD(DECL_USING_DIRECTIVE);
1214 RECORD(DECL_UNRESOLVED_USING_VALUE);
1215 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1216 RECORD(DECL_LINKAGE_SPEC);
1217 RECORD(DECL_CXX_RECORD);
1218 RECORD(DECL_CXX_METHOD);
1219 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001220 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001221 RECORD(DECL_CXX_DESTRUCTOR);
1222 RECORD(DECL_CXX_CONVERSION);
1223 RECORD(DECL_ACCESS_SPEC);
1224 RECORD(DECL_FRIEND);
1225 RECORD(DECL_FRIEND_TEMPLATE);
1226 RECORD(DECL_CLASS_TEMPLATE);
1227 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1228 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001229 RECORD(DECL_VAR_TEMPLATE);
1230 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1231 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001232 RECORD(DECL_FUNCTION_TEMPLATE);
1233 RECORD(DECL_TEMPLATE_TYPE_PARM);
1234 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1235 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001236 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001237 RECORD(DECL_STATIC_ASSERT);
1238 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001239 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001240 RECORD(DECL_INDIRECTFIELD);
1241 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001242 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1243 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1244 RECORD(DECL_IMPORT);
1245 RECORD(DECL_OMP_THREADPRIVATE);
1246 RECORD(DECL_EMPTY);
1247 RECORD(DECL_OBJC_TYPE_PARAM);
1248 RECORD(DECL_OMP_CAPTUREDEXPR);
1249 RECORD(DECL_PRAGMA_COMMENT);
1250 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1251 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001252
Douglas Gregor03412ba2011-06-03 02:27:19 +00001253 // Statements and Exprs can occur in the Decls and Types block.
1254 AddStmtsExprs(Stream, Record);
1255
Douglas Gregor92a96f52011-02-08 21:58:10 +00001256 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001257 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001258 RECORD(PPD_MACRO_DEFINITION);
1259 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001260
1261 // Decls and Types block.
1262 BLOCK(EXTENSION_BLOCK);
1263 RECORD(EXTENSION_METADATA);
1264
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001265 BLOCK(UNHASHED_CONTROL_BLOCK);
1266 RECORD(SIGNATURE);
1267 RECORD(DIAGNOSTIC_OPTIONS);
1268 RECORD(DIAG_PRAGMA_MAPPINGS);
1269
Chris Lattner28fa4e62009-04-26 22:26:21 +00001270#undef RECORD
1271#undef BLOCK
1272 Stream.ExitBlock();
1273}
1274
Richard Smith54cc3c22014-12-11 20:50:24 +00001275/// \brief Prepares a path for being written to an AST file by converting it
1276/// to an absolute path and removing nested './'s.
1277///
1278/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001279static bool cleanPathForOutput(FileManager &FileMgr,
1280 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001281 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001282 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001283}
1284
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001285/// \brief Adjusts the given filename to only write out the portion of the
1286/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001287///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001288/// \param Filename the file name to adjust.
1289///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001290/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1291/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001292///
1293/// \returns either the original filename (if it needs no adjustment) or the
1294/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001295static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001296adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001297 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001298
Richard Smith7ed1bc92014-12-05 22:42:13 +00001299 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001300 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001302 // Verify that the filename and the system root have the same prefix.
1303 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001304 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1305 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001306 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001307
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001308 // We hit the end of the filename before we hit the end of the system root.
1309 if (!Filename[Pos])
1310 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001311
Richard Smith7ed1bc92014-12-05 22:42:13 +00001312 // If there's not a path separator at the end of the base directory nor
1313 // immediately after it, then this isn't within the base directory.
1314 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1315 if (!llvm::sys::path::is_separator(BaseDir.back()))
1316 return Filename;
1317 } else {
1318 // If the file name has a '/' at the current position, skip over the '/'.
1319 // We distinguish relative paths from absolute paths by the
1320 // absence of '/' at the beginning of relative paths.
1321 //
1322 // FIXME: This is wrong. We distinguish them by asking if the path is
1323 // absolute, which isn't the same thing. And there might be multiple '/'s
1324 // in a row. Use a better mechanism to indicate whether we have emitted an
1325 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001326 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001329 return Filename + Pos;
1330}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001331
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001332ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1333 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1334 llvm::SHA1 Hasher;
1335 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1336 auto Hash = Hasher.result();
1337
1338 // Convert to an array [5*i32].
1339 ASTFileSignature Signature;
1340 auto LShift = [&](unsigned char Val, unsigned Shift) {
1341 return (uint32_t)Val << Shift;
1342 };
1343 for (int I = 0; I != 5; ++I)
1344 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1345 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1346
1347 return Signature;
1348}
1349
1350ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1351 ASTContext &Context) {
1352 // Flush first to prepare the PCM hash (signature).
1353 Stream.FlushToWord();
1354 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1355
1356 // Enter the block and prepare to write records.
1357 RecordData Record;
1358 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1359
1360 // For implicit modules, write the hash of the PCM as its signature.
1361 ASTFileSignature Signature;
1362 if (WritingModule &&
1363 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1364 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1365 Record.append(Signature.begin(), Signature.end());
1366 Stream.EmitRecord(SIGNATURE, Record);
1367 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001368 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001369
1370 // Diagnostic options.
1371 const auto &Diags = Context.getDiagnostics();
1372 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1373#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1374#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1375 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1376#include "clang/Basic/DiagnosticOptions.def"
1377 Record.push_back(DiagOpts.Warnings.size());
1378 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1379 AddString(DiagOpts.Warnings[I], Record);
1380 Record.push_back(DiagOpts.Remarks.size());
1381 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1382 AddString(DiagOpts.Remarks[I], Record);
1383 // Note: we don't serialize the log or serialization file names, because they
1384 // are generally transient files and will almost always be overridden.
1385 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1386
1387 // Write out the diagnostic/pragma mappings.
1388 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1389
1390 // Leave the options block.
1391 Stream.ExitBlock();
1392 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001393}
1394
Douglas Gregor112b9072012-10-18 05:31:06 +00001395/// \brief Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001396void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1397 StringRef isysroot,
1398 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001399 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001400 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001401 RecordData Record;
1402
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001403 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001404 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001405 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1406 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1407 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1408 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1409 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1410 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001411 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001412 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1413 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001414 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001415 assert((!WritingModule || isysroot.empty()) &&
1416 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001417 {
1418 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1419 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1420 !isysroot.empty(), IncludeTimestamps,
1421 ASTHasCompilerErrors};
1422 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1423 getClangFullRepositoryVersion());
1424 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001425
Richard Smith8b706102017-05-31 20:56:55 +00001426 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001427 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001428 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001429 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001431 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001432 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001433 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1434 }
1435
Richard Smith7ed1bc92014-12-05 22:42:13 +00001436 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001437 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001438 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001439
1440 // If the home of the module is the current working directory, then we
1441 // want to pick up the cwd of the build process loading the module, not
1442 // our cwd, when we load this module.
1443 if (!PP.getHeaderSearchInfo()
1444 .getHeaderSearchOpts()
1445 .ModuleMapFileHomeIsCwd ||
1446 WritingModule->Directory->getName() != StringRef(".")) {
1447 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001448 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001449 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1450 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001451 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001452
Mehdi Amini57a41912015-09-10 01:46:39 +00001453 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001454 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1455 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001456
1457 // Write out all other paths relative to the base directory if possible.
1458 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1459 } else if (!isysroot.empty()) {
1460 // Write out paths relative to the sysroot if possible.
1461 BaseDirectory = isysroot;
1462 }
1463
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001464 // Module map file
1465 if (WritingModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001466 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001467
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001468 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001469 AddPath(WritingModule->PresumedModuleMapFile.empty()
1470 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1471 : StringRef(WritingModule->PresumedModuleMapFile),
1472 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001473
1474 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001475 if (auto *AdditionalModMaps =
1476 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001477 Record.push_back(AdditionalModMaps->size());
1478 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001479 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001480 } else {
1481 Record.push_back(0);
1482 }
1483
1484 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001485 }
1486
Douglas Gregor112b9072012-10-18 05:31:06 +00001487 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001488 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001489 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001490 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001491
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001492 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001493 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001494 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001495 continue;
1496
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001497 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1498 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001499
1500 // If we have calculated signature, there is no need to store
1501 // the size or timestamp.
1502 Record.push_back(M.Signature ? 0 : M.File->getSize());
1503 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1504
1505 for (auto I : M.Signature)
1506 Record.push_back(I);
1507
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001508 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001509 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001510 Stream.EmitRecord(IMPORTS, Record);
1511 }
Mike Stump11289f42009-09-09 15:08:12 +00001512
Richard Smith0516b182015-09-08 19:40:14 +00001513 // Write the options block.
1514 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1515
Douglas Gregor112b9072012-10-18 05:31:06 +00001516 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001517 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001518 const LangOptions &LangOpts = Context.getLangOpts();
1519#define LANGOPT(Name, Bits, Default, Description) \
1520 Record.push_back(LangOpts.Name);
1521#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1522 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001523#include "clang/Basic/LangOptions.def"
1524#define SANITIZER(NAME, ID) \
1525 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001526#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001527
Ben Langmuircd98cb72015-06-23 18:20:18 +00001528 Record.push_back(LangOpts.ModuleFeatures.size());
1529 for (StringRef Feature : LangOpts.ModuleFeatures)
1530 AddString(Feature, Record);
1531
Douglas Gregor112b9072012-10-18 05:31:06 +00001532 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1533 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001534
1535 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001536
1537 // Comment options.
1538 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001539 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1540 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001541 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001542 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001543
Samuel Antaoee8fb302016-01-06 13:42:12 +00001544 // OpenMP offloading options.
1545 Record.push_back(LangOpts.OMPTargetTriples.size());
1546 for (auto &T : LangOpts.OMPTargetTriples)
1547 AddString(T.getTriple(), Record);
1548
1549 AddString(LangOpts.OMPHostIRFile, Record);
1550
Douglas Gregor112b9072012-10-18 05:31:06 +00001551 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1552
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001553 // Target options.
1554 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001555 const TargetInfo &Target = Context.getTargetInfo();
1556 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001557 AddString(TargetOpts.Triple, Record);
1558 AddString(TargetOpts.CPU, Record);
1559 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001560 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1561 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1562 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1563 }
1564 Record.push_back(TargetOpts.Features.size());
1565 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1566 AddString(TargetOpts.Features[I], Record);
1567 }
1568 Stream.EmitRecord(TARGET_OPTIONS, Record);
1569
Douglas Gregorc6317db2012-10-24 15:49:58 +00001570 // File system options.
1571 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001572 const FileSystemOptions &FSOpts =
1573 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001574 AddString(FSOpts.WorkingDir, Record);
1575 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1576
Douglas Gregor2d302362012-10-24 16:50:34 +00001577 // Header search options.
1578 Record.clear();
1579 const HeaderSearchOptions &HSOpts
1580 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1581 AddString(HSOpts.Sysroot, Record);
1582
1583 // Include entries.
1584 Record.push_back(HSOpts.UserEntries.size());
1585 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1586 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1587 AddString(Entry.Path, Record);
1588 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001589 Record.push_back(Entry.IsFramework);
1590 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001591 }
1592
1593 // System header prefixes.
1594 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1595 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1596 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1597 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1598 }
1599
1600 AddString(HSOpts.ResourceDir, Record);
1601 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001602 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001603 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001604 Record.push_back(HSOpts.ImplicitModuleMaps);
1605 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001606 Record.push_back(HSOpts.UseBuiltinIncludes);
1607 Record.push_back(HSOpts.UseStandardSystemIncludes);
1608 Record.push_back(HSOpts.UseStandardCXXIncludes);
1609 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001610 // Write out the specific module cache path that contains the module files.
1611 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001612 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1613
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001614 // Preprocessor options.
1615 Record.clear();
1616 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1617
1618 // Macro definitions.
1619 Record.push_back(PPOpts.Macros.size());
1620 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1621 AddString(PPOpts.Macros[I].first, Record);
1622 Record.push_back(PPOpts.Macros[I].second);
1623 }
1624
1625 // Includes
1626 Record.push_back(PPOpts.Includes.size());
1627 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1628 AddString(PPOpts.Includes[I], Record);
1629
1630 // Macro includes
1631 Record.push_back(PPOpts.MacroIncludes.size());
1632 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1633 AddString(PPOpts.MacroIncludes[I], Record);
1634
Douglas Gregorb6368752012-10-24 23:41:50 +00001635 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001636 // Detailed record is important since it is used for the module cache hash.
1637 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001638 AddString(PPOpts.ImplicitPCHInclude, Record);
1639 AddString(PPOpts.ImplicitPTHInclude, Record);
1640 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1641 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1642
Richard Smith0516b182015-09-08 19:40:14 +00001643 // Leave the options block.
1644 Stream.ExitBlock();
1645
Douglas Gregora3b20262011-05-06 21:43:30 +00001646 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001647 SourceManager &SM = Context.getSourceManager();
1648 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001649 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001650 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1651 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001652 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001653 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001654
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001655 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001656 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001657 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001658 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001659 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001660
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001661 Record.clear();
1662 Record.push_back(SM.getMainFileID().getOpaqueValue());
1663 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1664
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001665 // Original PCH directory
1666 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001667 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001668 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001670 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001671
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001672 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001673
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001674 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001675 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1676
Mehdi Amini57a41912015-09-10 01:46:39 +00001677 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001678 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1679 }
1680
Douglas Gregor49491f72013-03-15 22:15:07 +00001681 WriteInputFiles(Context.SourceMgr,
1682 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001683 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001684 Stream.ExitBlock();
1685}
1686
Douglas Gregor49491f72013-03-15 22:15:07 +00001687namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001688
Douglas Gregor49491f72013-03-15 22:15:07 +00001689 /// \brief An input file.
1690 struct InputFileEntry {
1691 const FileEntry *File;
1692 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001693 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001694 bool BufferOverridden;
1695 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001696
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001697} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001698
1699void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1700 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001701 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001702 using namespace llvm;
1703 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001704
Douglas Gregor72be3902012-10-19 00:38:02 +00001705 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001706 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001707 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001708 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001709 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1710 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001711 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001712 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Douglas Gregor72be3902012-10-19 00:38:02 +00001713 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001714 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001715
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001716 // Get all ContentCache objects for files, sorted by whether the file is a
1717 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001718 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001719 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1720 // Get this source location entry.
1721 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001722 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001723
1724 // We only care about file entries that were not overridden.
1725 if (!SLoc->isFile())
1726 continue;
1727 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001728 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001729 continue;
1730
Douglas Gregor49491f72013-03-15 22:15:07 +00001731 InputFileEntry Entry;
1732 Entry.File = Cache->OrigEntry;
1733 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001734 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001735 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001736 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001737 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001738 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001739 SortedFiles.push_front(Entry);
1740 }
1741
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001742 unsigned UserFilesNum = 0;
1743 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001744 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001745 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001746 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001747 if (InputFileID != 0)
1748 continue; // already recorded this file.
1749
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001750 // Record this entry's offset.
1751 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001752
1753 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001754
Douglas Gregor49491f72013-03-15 22:15:07 +00001755 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001756 ++UserFilesNum;
1757
Douglas Gregor72be3902012-10-19 00:38:02 +00001758 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001759 // And whether this file was overridden.
1760 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001761 INPUT_FILE,
1762 InputFileOffsets.size(),
1763 (uint64_t)Entry.File->getSize(),
1764 (uint64_t)getTimestampForOutput(Entry.File),
1765 Entry.BufferOverridden,
1766 Entry.IsTransient};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001767
Richard Smith7ed1bc92014-12-05 22:42:13 +00001768 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1769 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001770
Douglas Gregor112b9072012-10-18 05:31:06 +00001771 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001772
1773 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001774 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001775 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1776 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001777 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1778 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001779 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001780 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001781
1782 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001783 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1784 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001785 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001786}
1787
Douglas Gregora7f71a92009-04-10 03:52:48 +00001788//===----------------------------------------------------------------------===//
1789// Source Manager Serialization
1790//===----------------------------------------------------------------------===//
1791
1792/// \brief Create an abbreviation for the SLocEntry that refers to a
1793/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001794static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001795 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001796
David Blaikieb44f0bf2017-01-04 22:36:43 +00001797 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001798 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1800 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1801 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1802 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001803 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001805 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001806 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001808 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001809}
1810
1811/// \brief Create an abbreviation for the SLocEntry that refers to a
1812/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001813static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001814 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001815
David Blaikieb44f0bf2017-01-04 22:36:43 +00001816 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001817 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001818 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1819 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1820 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1821 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1822 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001823 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001824}
1825
1826/// \brief Create an abbreviation for the SLocEntry that refers to a
1827/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001828static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1829 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001830 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001831
David Blaikieb44f0bf2017-01-04 22:36:43 +00001832 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001833 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1834 : SM_SLOC_BUFFER_BLOB));
1835 if (Compressed)
1836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001837 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001838 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001839}
1840
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001841/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1842/// expansion.
1843static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001844 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001845
David Blaikieb44f0bf2017-01-04 22:36:43 +00001846 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001847 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001853 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001854}
1855
Douglas Gregor09b69892011-02-10 17:09:37 +00001856namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001857
Douglas Gregor09b69892011-02-10 17:09:37 +00001858 // Trait used for the on-disk hash table of header search information.
1859 class HeaderFileInfoTrait {
1860 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001861
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001862 // Keep track of the framework names we've used during serialization.
1863 SmallVector<char, 128> FrameworkStringData;
1864 llvm::StringMap<unsigned> FrameworkNameOffset;
1865
Douglas Gregor09b69892011-02-10 17:09:37 +00001866 public:
Richard Smith040e1262017-06-02 01:55:39 +00001867 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1868
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001869 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001870 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001871 off_t Size;
1872 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001873 };
1874 typedef const key_type &key_type_ref;
Richard Smith040e1262017-06-02 01:55:39 +00001875
1876 using UnresolvedModule =
1877 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Douglas Gregor09b69892011-02-10 17:09:37 +00001878
Richard Smith040e1262017-06-02 01:55:39 +00001879 struct data_type {
1880 const HeaderFileInfo &HFI;
1881 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1882 UnresolvedModule Unresolved;
1883 };
Douglas Gregor09b69892011-02-10 17:09:37 +00001884 typedef const data_type &data_type_ref;
Richard Smith040e1262017-06-02 01:55:39 +00001885
Justin Bogner25463f12014-04-18 20:27:24 +00001886 typedef unsigned hash_value_type;
1887 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001888
Richard Smithe75ee0f2015-08-17 07:13:32 +00001889 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001890 // The hash is based only on size/time of the file, so that the reader can
1891 // match even when symlinking or excess path elements ("foo/../", "../")
1892 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001893 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001894 }
1895
1896 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001897 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001898 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001899 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001900 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001901 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001902 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001903 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001904 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1905 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001906 if (Data.Unresolved.getPointer())
1907 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001908 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001909 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001910 }
Richard Smith040e1262017-06-02 01:55:39 +00001911
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001912 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001913 using namespace llvm::support;
1914 endian::Writer<little> LE(Out);
Richard Smith040e1262017-06-02 01:55:39 +00001915 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001916 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001917 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001918 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001919 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001920 }
Richard Smith040e1262017-06-02 01:55:39 +00001921
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001922 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001923 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001924 using namespace llvm::support;
1925 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001926 uint64_t Start = Out.tell(); (void)Start;
1927
Richard Smith040e1262017-06-02 01:55:39 +00001928 unsigned char Flags = (Data.HFI.isImport << 4)
1929 | (Data.HFI.isPragmaOnce << 3)
1930 | (Data.HFI.DirInfo << 1)
1931 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001932 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00001933 LE.write<uint16_t>(Data.HFI.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001934
Richard Smith040e1262017-06-02 01:55:39 +00001935 if (!Data.HFI.ControllingMacro)
1936 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001937 else
Richard Smith040e1262017-06-02 01:55:39 +00001938 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1939
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001940 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00001941 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001942 // If this header refers into a framework, save the framework name.
1943 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00001944 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001945 if (Pos == FrameworkNameOffset.end()) {
1946 Offset = FrameworkStringData.size() + 1;
Richard Smith040e1262017-06-02 01:55:39 +00001947 FrameworkStringData.append(Data.HFI.Framework.begin(),
1948 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001949 FrameworkStringData.push_back(0);
1950
Richard Smith040e1262017-06-02 01:55:39 +00001951 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001952 } else
1953 Offset = Pos->second;
1954 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001955 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001956
Richard Smith040e1262017-06-02 01:55:39 +00001957 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1958 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1959 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00001960 assert((Value >> 2) == ModID && "overflow in header module info");
1961 LE.write<uint32_t>(Value);
1962 }
Richard Smith040e1262017-06-02 01:55:39 +00001963 };
1964
1965 // FIXME: If the header is excluded, we should write out some
1966 // record of that fact.
1967 for (auto ModInfo : Data.KnownHeaders)
1968 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1969 if (Data.Unresolved.getPointer())
1970 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001971
Douglas Gregor09b69892011-02-10 17:09:37 +00001972 assert(Out.tell() - Start == DataLen && "Wrong data length");
1973 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001974
1975 const char *strings_begin() const { return FrameworkStringData.begin(); }
1976 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001977 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001978
Douglas Gregor09b69892011-02-10 17:09:37 +00001979} // end anonymous namespace
1980
1981/// \brief Write the header search block for the list of files that
1982///
1983/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001984void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00001985 HeaderFileInfoTrait GeneratorTrait(*this);
1986 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1987 SmallVector<const char *, 4> SavedStrings;
1988 unsigned NumHeaderSearchEntries = 0;
1989
1990 // Find all unresolved headers for the current module. We generally will
1991 // have resolved them before we get here, but not necessarily: we might be
1992 // compiling a preprocessed module, where there is no requirement for the
1993 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00001994 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00001995 if (WritingModule) {
1996 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
1997 while (!Worklist.empty()) {
1998 Module *M = Worklist.pop_back_val();
1999 if (!M->isAvailable())
2000 continue;
2001
2002 // Map to disk files where possible, to pick up any missing stat
2003 // information. This also means we don't need to check the unresolved
2004 // headers list when emitting resolved headers in the first loop below.
2005 // FIXME: It'd be preferable to avoid doing this if we were given
2006 // sufficient stat information in the module map.
2007 HS.getModuleMap().resolveHeaderDirectives(M);
2008
2009 // If the file didn't exist, we can still create a module if we were given
2010 // enough information in the module map.
2011 for (auto U : M->MissingHeaders) {
2012 // Check that we were given enough information to build a module
2013 // without this file existing on disk.
2014 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2015 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2016 << WritingModule->getFullModuleName() << U.Size.hasValue()
2017 << U.FileName;
2018 continue;
2019 }
2020
2021 // Form the effective relative pathname for the file.
2022 SmallString<128> Filename(M->Directory->getName());
2023 llvm::sys::path::append(Filename, U.FileName);
2024 PreparePathForOutput(Filename);
2025
2026 StringRef FilenameDup = strdup(Filename.c_str());
2027 SavedStrings.push_back(FilenameDup.data());
2028
2029 HeaderFileInfoTrait::key_type Key = {
2030 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
2031 };
2032 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00002033 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00002034 };
2035 // FIXME: Deal with cases where there are multiple unresolved header
2036 // directives in different submodules for the same header.
2037 Generator.insert(Key, Data, GeneratorTrait);
2038 ++NumHeaderSearchEntries;
2039 }
2040
2041 Worklist.append(M->submodule_begin(), M->submodule_end());
2042 }
2043 }
2044
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002045 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00002046 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2047
2048 if (FilesByUID.size() > HS.header_file_size())
2049 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00002050
Douglas Gregor09b69892011-02-10 17:09:37 +00002051 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2052 const FileEntry *File = FilesByUID[UID];
2053 if (!File)
2054 continue;
2055
Richard Smith386bb072015-08-18 23:42:23 +00002056 // Get the file info. This will load info from the external source if
2057 // necessary. Skip emitting this file if we have no information on it
2058 // as a header file (in which case HFI will be null) or if it hasn't
2059 // changed since it was loaded. Also skip it if it's for a modular header
2060 // from a different module; in that case, we rely on the module(s)
2061 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00002062 const HeaderFileInfo *HFI =
2063 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
2064 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00002065 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00002066
Richard Smith7ed1bc92014-12-05 22:42:13 +00002067 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002068 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00002069 SmallString<128> FilenameTmp(Filename);
2070 if (PreparePathForOutput(FilenameTmp)) {
2071 // If we performed any translation on the file name at all, we need to
2072 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002073 Filename = StringRef(strdup(FilenameTmp.c_str()));
2074 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002075 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002076
Richard Smith040e1262017-06-02 01:55:39 +00002077 HeaderFileInfoTrait::key_type Key = {
2078 Filename, File->getSize(), getTimestampForOutput(File)
2079 };
2080 HeaderFileInfoTrait::data_type Data = {
2081 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
2082 };
2083 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002084 ++NumHeaderSearchEntries;
2085 }
Richard Smith040e1262017-06-02 01:55:39 +00002086
Douglas Gregor09b69892011-02-10 17:09:37 +00002087 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002088 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002089 uint32_t BucketOffset;
2090 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002091 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00002092 llvm::raw_svector_ostream Out(TableData);
2093 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002094 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00002095 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2096 }
2097
2098 // Create a blob abbreviation
2099 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002100
David Blaikieb44f0bf2017-01-04 22:36:43 +00002101 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002102 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002107 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00002108
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002109 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002110 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2111 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002112 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002113 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00002114
2115 // Free all of the strings we had to duplicate.
2116 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002117 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002118}
2119
George Rimarc39f5492017-01-17 15:45:31 +00002120static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2121 unsigned SLocBufferBlobCompressedAbbrv,
2122 unsigned SLocBufferBlobAbbrv) {
2123 typedef ASTWriter::RecordData::value_type RecordDataType;
2124
2125 // Compress the buffer if possible. We expect that almost all PCM
2126 // consumers will not want its contents.
2127 SmallString<0> CompressedBuffer;
2128 if (llvm::zlib::isAvailable()) {
2129 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2130 if (!E) {
2131 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2132 Blob.size() - 1};
2133 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2134 CompressedBuffer);
2135 return;
2136 }
2137 llvm::consumeError(std::move(E));
2138 }
2139
2140 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2141 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2142}
2143
Douglas Gregora7f71a92009-04-10 03:52:48 +00002144/// \brief Writes the block containing the serialized form of the
2145/// source manager.
2146///
2147/// TODO: We should probably use an on-disk hash table (stored in a
2148/// blob), indexed based on the file name, so that we only create
2149/// entries for files that we actually need. In the common case (no
2150/// errors), we probably won't have to create file entries for any of
2151/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002152void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002153 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002154 RecordData Record;
2155
Chris Lattner0910e3b2009-04-10 17:16:57 +00002156 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002157 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002158
2159 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002160 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2161 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002162 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2163 unsigned SLocBufferBlobCompressedAbbrv =
2164 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002165 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002166
Douglas Gregor258ae542009-04-27 06:38:32 +00002167 // Write out the source location entry table. We skip the first
2168 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002169 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002170 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002171 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2172 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002173 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002174 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002175 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002176 FileID FID = FileID::get(I);
2177 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002178
Douglas Gregor258ae542009-04-27 06:38:32 +00002179 // Record the offset of this source-location entry.
2180 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2181
2182 // Figure out which record code to use.
2183 unsigned Code;
2184 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002185 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2186 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002187 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002188 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002189 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002190 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002191 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002192 Record.clear();
2193 Record.push_back(Code);
2194
Douglas Gregor925296b2011-07-19 16:10:42 +00002195 // Starting offset of this entry within this module, so skip the dummy.
2196 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002197 if (SLoc->isFile()) {
2198 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002199 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002200 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2201 Record.push_back(File.hasLineDirectives());
2202
2203 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002204 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002205 if (Content->OrigEntry) {
2206 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002207 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002208
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002209 // The source location entry is a file. Emit input file ID.
2210 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2211 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002212
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002213 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002214
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002215 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002216 if (FDI != FileDeclIDs.end()) {
2217 Record.push_back(FDI->second->FirstDeclIndex);
2218 Record.push_back(FDI->second->DeclIDs.size());
2219 } else {
2220 Record.push_back(0);
2221 Record.push_back(0);
2222 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002223
2224 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2225
Richard Smithaada85c2016-02-06 02:06:43 +00002226 if (Content->BufferOverridden || Content->IsTransient)
2227 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002228 } else {
2229 // The source location entry is a buffer. The blob associated
2230 // with this entry contains the contents of the buffer.
2231
2232 // We add one to the size so that we capture the trailing NULL
2233 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2234 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002235 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002236 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002237 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002238 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002239 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002240 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002241
Mehdi Amini99d1b292016-10-01 16:38:28 +00002242 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002243 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002244 }
Richard Smithaada85c2016-02-06 02:06:43 +00002245
2246 if (EmitBlob) {
2247 // Include the implicit terminating null character in the on-disk buffer
2248 // if we're writing it uncompressed.
2249 const llvm::MemoryBuffer *Buffer =
2250 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2251 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002252 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2253 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002254 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002255 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002256 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002257 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002258 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2259 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2260 AddSourceLocation(Expansion.isMacroArgExpansion()
2261 ? SourceLocation()
2262 : Expansion.getExpansionLocEnd(),
2263 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002264
2265 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002266 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002267 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002268 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002269 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002270 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002271 }
2272 }
2273
Douglas Gregor8f45df52009-04-16 22:23:12 +00002274 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002275
2276 if (SLocEntryOffsets.empty())
2277 return;
2278
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002279 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002280 // table is used for lazily loading source-location information.
2281 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002282
David Blaikieb44f0bf2017-01-04 22:36:43 +00002283 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002284 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002288 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002289 {
2290 RecordData::value_type Record[] = {
2291 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2292 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2293 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2294 bytes(SLocEntryOffsets));
2295 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002296 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002297 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002298 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002299
2300 // Write the line table. It depends on remapping working, so it must come
2301 // after the source location offsets.
2302 if (SourceMgr.hasLineTable()) {
2303 LineTableInfo &LineTable = SourceMgr.getLineTable();
2304
2305 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002306
2307 // Emit the needed file names.
2308 llvm::DenseMap<int, int> FilenameMap;
2309 for (const auto &L : LineTable) {
2310 if (L.first.ID < 0)
2311 continue;
2312 for (auto &LE : L.second) {
2313 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2314 FilenameMap.size())).second)
2315 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2316 }
2317 }
2318 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002319
2320 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002321 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002322 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002323 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002324 continue;
2325
2326 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002327 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002328
2329 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002330 Record.push_back(L.second.size());
2331 for (const auto &LE : L.second) {
2332 Record.push_back(LE.FileOffset);
2333 Record.push_back(LE.LineNo);
2334 Record.push_back(FilenameMap[LE.FilenameID]);
2335 Record.push_back((unsigned)LE.FileKind);
2336 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002337 }
2338 }
Richard Smith63078492015-09-01 07:41:55 +00002339
Douglas Gregor925296b2011-07-19 16:10:42 +00002340 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2341 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002342}
2343
Douglas Gregorc5046832009-04-27 18:38:38 +00002344//===----------------------------------------------------------------------===//
2345// Preprocessor Serialization
2346//===----------------------------------------------------------------------===//
2347
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002348static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2349 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002350 if (MacroInfo *MI = MD->getMacroInfo())
2351 if (MI->isBuiltinMacro())
2352 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002353
2354 if (IsModule) {
2355 SourceLocation Loc = MD->getLocation();
2356 if (Loc.isInvalid())
2357 return true;
2358 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2359 return true;
2360 }
2361
2362 return false;
2363}
2364
Chris Lattnereeffaef2009-04-10 17:15:23 +00002365/// \brief Writes the block containing the serialized form of the
2366/// preprocessor.
2367///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002368void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002369 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2370 if (PPRec)
2371 WritePreprocessorDetail(*PPRec);
2372
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002373 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002374 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002375
Chris Lattner0af3ba12009-04-13 01:29:17 +00002376 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2377 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002378 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002379 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002380 }
2381
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002382 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2383 assert(!IsModule);
2384 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2385 AddSourceLocation(Cond.IfLoc, Record);
2386 Record.push_back(Cond.WasSkipping);
2387 Record.push_back(Cond.FoundNonSkip);
2388 Record.push_back(Cond.FoundElse);
2389 }
2390 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2391 Record.clear();
2392 }
2393
Douglas Gregoreda6a892009-04-26 00:07:37 +00002394 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002395 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002396
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002397 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002398 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002399 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002400 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002401
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002402 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002403 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002404
Richard Smithee977932015-05-01 21:22:17 +00002405 // Construct the list of identifiers with macro directives that need to be
2406 // serialized.
2407 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2408 for (auto &Id : PP.getIdentifierTable())
2409 if (Id.second->hadMacroDefinition() &&
2410 (!Id.second->isFromAST() ||
2411 Id.second->hasChangedSinceDeserialization()))
2412 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002413 // Sort the set of macro definitions that need to be serialized by the
2414 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002415 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2416 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002417
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002418 // Emit the macro directives as a list and associate the offset with the
2419 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002420 for (const IdentifierInfo *Name : MacroIdentifiers) {
2421 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002422 auto StartOffset = Stream.GetCurrentBitNo();
2423
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002424 // Emit the macro directives in reverse source order.
2425 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002426 // Once we hit an ignored macro, we're done: the rest of the chain
2427 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002428 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002429 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002430
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002431 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002432 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002433 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002434 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002435 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002436 Record.push_back(VisMD->isPublic());
2437 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002438 }
Richard Smithd7329392015-04-21 21:46:32 +00002439
Richard Smithb8b2ed62015-04-23 18:18:26 +00002440 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002441 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002442 // We write out exported module macros for PCH as well.
2443 auto Leafs = PP.getLeafModuleMacros(Name);
2444 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2445 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2446 while (!Worklist.empty()) {
2447 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002448
Manman Ren33d80292016-05-31 18:19:32 +00002449 // Emit a record indicating this submodule exports this macro.
2450 ModuleMacroRecord.push_back(
2451 getSubmoduleID(Macro->getOwningModule()));
2452 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2453 for (auto *M : Macro->overrides())
2454 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002455
Manman Ren33d80292016-05-31 18:19:32 +00002456 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2457 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002458
Manman Ren33d80292016-05-31 18:19:32 +00002459 // Enqueue overridden macros once we've visited all their ancestors.
2460 for (auto *M : Macro->overrides())
2461 if (++Visits[M] == M->getNumOverridingMacros())
2462 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002463
Manman Ren33d80292016-05-31 18:19:32 +00002464 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002465 }
Richard Smithd7329392015-04-21 21:46:32 +00002466
Richard Smithee977932015-05-01 21:22:17 +00002467 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002468 continue;
2469
Richard Smithd7329392015-04-21 21:46:32 +00002470 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002471 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2472 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002473 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002474
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002475 /// \brief Offsets of each of the macros into the bitstream, indexed by
2476 /// the local macro ID
2477 ///
2478 /// For each identifier that is associated with a macro, this map
2479 /// provides the offset into the bitstream where that macro is
2480 /// defined.
2481 std::vector<uint32_t> MacroOffsets;
2482
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002483 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2484 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2485 MacroInfo *MI = MacroInfosToEmit[I].MI;
2486 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002487
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002488 if (ID < FirstMacroID) {
2489 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2490 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002491 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002492
2493 // Record the local offset of this macro.
2494 unsigned Index = ID - FirstMacroID;
2495 if (Index == MacroOffsets.size())
2496 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2497 else {
2498 if (Index > MacroOffsets.size())
2499 MacroOffsets.resize(Index + 1);
2500
2501 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2502 }
2503
2504 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002505 AddSourceLocation(MI->getDefinitionLoc(), Record);
2506 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2507 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002508 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002509 unsigned Code;
2510 if (MI->isObjectLike()) {
2511 Code = PP_MACRO_OBJECT_LIKE;
2512 } else {
2513 Code = PP_MACRO_FUNCTION_LIKE;
2514
2515 Record.push_back(MI->isC99Varargs());
2516 Record.push_back(MI->isGNUVarargs());
2517 Record.push_back(MI->hasCommaPasting());
2518 Record.push_back(MI->getNumArgs());
Daniel Marjamakie4770da2015-05-29 09:15:24 +00002519 for (const IdentifierInfo *Arg : MI->args())
2520 AddIdentifierRef(Arg, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002521 }
2522
2523 // If we have a detailed preprocessing record, record the macro definition
2524 // ID that corresponds to this macro.
2525 if (PPRec)
2526 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2527
2528 Stream.EmitRecord(Code, Record);
2529 Record.clear();
2530
2531 // Emit the tokens array.
2532 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2533 // Note that we know that the preprocessor does not have any annotation
2534 // tokens in it because they are created by the parser, and thus can't
2535 // be in a macro definition.
2536 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002537 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002538 Stream.EmitRecord(PP_TOKEN, Record);
2539 Record.clear();
2540 }
2541 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002542 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002543
Douglas Gregor92a96f52011-02-08 21:58:10 +00002544 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002545
2546 // Write the offsets table for macro IDs.
2547 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002548
David Blaikieb44f0bf2017-01-04 22:36:43 +00002549 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002550 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2554
David Blaikieb44f0bf2017-01-04 22:36:43 +00002555 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002556 {
2557 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2558 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2559 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2560 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002561}
2562
2563void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002564 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002565 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002566
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002567 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002568
Douglas Gregor92a96f52011-02-08 21:58:10 +00002569 // Enter the preprocessor block.
2570 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002571
Douglas Gregoraae92242010-03-19 21:51:54 +00002572 // If the preprocessor has a preprocessing record, emit it.
2573 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002574 using namespace llvm;
2575
2576 // Set up the abbreviation for
2577 unsigned InclusionAbbrev = 0;
2578 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002579 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002580 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002586 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002587 }
2588
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002589 unsigned FirstPreprocessorEntityID
2590 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2591 + NUM_PREDEF_PP_ENTITY_IDS;
2592 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002593 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002594 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2595 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002596 E != EEnd;
2597 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002598 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002599
Richard Smith66a81862015-05-04 02:25:31 +00002600 PreprocessedEntityOffsets.push_back(
2601 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002602
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002603 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002604 // Record this macro definition's ID.
2605 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002606
Douglas Gregor92a96f52011-02-08 21:58:10 +00002607 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002608 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2609 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002610 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002611
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002612 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002613 Record.push_back(ME->isBuiltinMacro());
2614 if (ME->isBuiltinMacro())
2615 AddIdentifierRef(ME->getName(), Record);
2616 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002617 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002618 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002619 continue;
2620 }
2621
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002622 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002623 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002624 Record.push_back(ID->getFileName().size());
2625 Record.push_back(ID->wasInQuotes());
2626 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002627 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002628 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002629 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002630 // Check that the FileEntry is not null because it was not resolved and
2631 // we create a PCH even with compiler errors.
2632 if (ID->getFile())
2633 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002634 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002635 continue;
2636 }
2637
2638 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2639 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002640 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002641
Douglas Gregoraae92242010-03-19 21:51:54 +00002642 // Write the offsets table for the preprocessing record.
2643 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002644 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2645
Douglas Gregoraae92242010-03-19 21:51:54 +00002646 // Write the offsets table for identifier IDs.
2647 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002648
David Blaikieb44f0bf2017-01-04 22:36:43 +00002649 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002650 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002653 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002654
Mehdi Amini57a41912015-09-10 01:46:39 +00002655 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2656 FirstPreprocessorEntityID -
2657 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002658 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002659 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002660 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002661}
2662
Richard Smith386bb072015-08-18 23:42:23 +00002663unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002664 if (!Mod)
2665 return 0;
2666
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002667 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2668 if (Known != SubmoduleIDs.end())
2669 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002670
Richard Smithdc1f0422016-07-20 19:10:16 +00002671 auto *Top = Mod->getTopLevelModule();
2672 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002673 (getLangOpts().CompilingPCH ||
2674 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002675 return 0;
2676
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002677 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2678}
2679
Richard Smith386bb072015-08-18 23:42:23 +00002680unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2681 // FIXME: This can easily happen, if we have a reference to a submodule that
2682 // did not result in us loading a module file for that submodule. For
2683 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2684 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2685 assert((ID || !Mod) &&
2686 "asked for module ID for non-local, non-imported module");
2687 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002688}
2689
Douglas Gregor253eefe2011-12-01 00:59:36 +00002690/// \brief Compute the number of modules within the given tree (including the
2691/// given module).
2692static unsigned getNumberOfModules(Module *Mod) {
2693 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002694 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002695 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002696 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002697
2698 return ChildModules + 1;
2699}
2700
Douglas Gregorde3ef502011-11-30 23:21:26 +00002701void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002702 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002703 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002704
2705 // Write the abbreviations needed for the submodules block.
2706 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002707
David Blaikieb44f0bf2017-01-04 22:36:43 +00002708 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002709 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2715 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002716 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002717 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002720 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002721 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002722
David Blaikieb44f0bf2017-01-04 22:36:43 +00002723 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002724 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002726 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002727
David Blaikieb44f0bf2017-01-04 22:36:43 +00002728 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002729 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002731 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002732
David Blaikieb44f0bf2017-01-04 22:36:43 +00002733 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002734 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002736 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002737
David Blaikieb44f0bf2017-01-04 22:36:43 +00002738 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002739 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002741 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002742
David Blaikieb44f0bf2017-01-04 22:36:43 +00002743 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002744 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002747 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002748
David Blaikieb44f0bf2017-01-04 22:36:43 +00002749 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002750 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002752 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002753
David Blaikieb44f0bf2017-01-04 22:36:43 +00002754 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002755 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002757 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002758
David Blaikieb44f0bf2017-01-04 22:36:43 +00002759 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002760 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002762 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002763
David Blaikieb44f0bf2017-01-04 22:36:43 +00002764 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002765 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002767 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002768
David Blaikieb44f0bf2017-01-04 22:36:43 +00002769 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002770 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002773 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002774
David Blaikieb44f0bf2017-01-04 22:36:43 +00002775 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002776 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002778 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002779
David Blaikieb44f0bf2017-01-04 22:36:43 +00002780 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002781 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2782 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002784 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002785
Douglas Gregor253eefe2011-12-01 00:59:36 +00002786 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002787 RecordData::value_type Record[] = {
2788 getNumberOfModules(WritingModule),
2789 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS,
Richard Smithe6c8c6d2017-04-25 00:40:40 +00002790 (unsigned)WritingModule->Kind};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002791 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2792
Douglas Gregor69021972011-11-30 17:33:56 +00002793 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002794 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002795 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002796 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002797 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002798 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002799 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002800
2801 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002802 if (Mod->Parent) {
2803 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002804 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002805 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002806
2807 // Emit the definition of the block.
2808 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002809 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2810 ID,
2811 ParentID,
2812 Mod->IsFramework,
2813 Mod->IsExplicit,
2814 Mod->IsSystem,
2815 Mod->IsExternC,
2816 Mod->InferSubmodules,
2817 Mod->InferExplicitSubmodules,
2818 Mod->InferExportWildcard,
David Blaikiee6b7c282017-04-11 20:46:34 +00002819 Mod->ConfigMacrosExhaustive};
Mehdi Amini57a41912015-09-10 01:46:39 +00002820 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2821 }
2822
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002823 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002824 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002825 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002826 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002827 }
2828
Douglas Gregor69021972011-11-30 17:33:56 +00002829 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002830 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002831 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002832 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2833 UmbrellaHeader.NameAsWritten);
2834 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002835 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2836 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002837 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002838 }
Richard Smith306d8922014-10-22 23:50:56 +00002839
Douglas Gregor69021972011-11-30 17:33:56 +00002840 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002841 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002842 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002843 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002844 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002845 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002846 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2847 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2848 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002849 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002850 Module::HK_PrivateTextual},
2851 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002852 };
2853 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002854 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002855 for (auto &H : Mod->Headers[HL.HeaderKind])
2856 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2857 }
2858
2859 // Emit the top headers.
2860 {
2861 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002862 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002863 for (auto *H : TopHeaders)
2864 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002865 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002866
2867 // Emit the imports.
2868 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002869 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002870 for (auto *I : Mod->Imports)
2871 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002872 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2873 }
2874
Douglas Gregor24bb9232011-12-02 18:58:38 +00002875 // Emit the exports.
2876 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002877 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002878 for (const auto &E : Mod->Exports) {
2879 // FIXME: This may fail; we don't require that all exported modules
2880 // are local or imported.
2881 Record.push_back(getSubmoduleID(E.getPointer()));
2882 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002883 }
2884 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2885 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002886
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002887 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2888 // Might be unnecessary as use declarations are only used to build the
2889 // module itself.
2890
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002891 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002892 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002893 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2894 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002895 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002896 }
2897
Douglas Gregorfb912652013-03-20 21:10:35 +00002898 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002899 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002900 // FIXME: This may fail; we don't require that all conflicting modules
2901 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002902 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2903 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002904 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002905 }
2906
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002907 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002908 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002909 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002910 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002911 }
2912
Richard Smithdc1f0422016-07-20 19:10:16 +00002913 // Emit the initializers, if any.
2914 RecordData Inits;
2915 for (Decl *D : Context->getModuleInitializers(Mod))
2916 Inits.push_back(GetDeclRef(D));
2917 if (!Inits.empty())
2918 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2919
Douglas Gregor69021972011-11-30 17:33:56 +00002920 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002921 for (auto *M : Mod->submodules())
2922 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002923 }
2924
2925 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002926
Richard Smith23d8d032015-05-14 00:45:20 +00002927 assert((NextSubmoduleID - FirstSubmoduleID ==
2928 getNumberOfModules(WritingModule)) &&
2929 "Wrong # of submodules; found a reference to a non-local, "
2930 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002931}
2932
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002933void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2934 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002935 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2936 DiagStateIDMap;
2937 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002938 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002939
Richard Smithe37391c2017-05-03 00:28:49 +00002940 auto EncodeDiagStateFlags =
2941 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2942 unsigned Result = (unsigned)DS->ExtBehavior;
2943 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00002944 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2945 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2946 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00002947 Result = (Result << 1) | Val;
2948 return Result;
2949 };
2950
2951 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2952 Record.push_back(Flags);
2953
Richard Smithd230de22017-01-26 01:01:01 +00002954 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2955 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00002956 // Ensure that the diagnostic state wasn't modified since it was created.
2957 // We will not correctly round-trip this information otherwise.
2958 assert(Flags == EncodeDiagStateFlags(State) &&
2959 "diag state flags vary in single AST file");
2960
Richard Smithd230de22017-01-26 01:01:01 +00002961 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002962 Record.push_back(DiagStateID);
Richard Smithd230de22017-01-26 01:01:01 +00002963
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002964 if (DiagStateID == 0) {
2965 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002966
2967 // Add a placeholder for the number of mappings.
2968 auto SizeIdx = Record.size();
2969 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00002970 for (const auto &I : *State) {
2971 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002972 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00002973 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002974 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002975 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00002976 // Update the placeholder.
2977 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00002978 }
2979 };
2980
2981 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00002982
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002983 // Reserve a spot for the number of locations with state transitions.
2984 auto NumLocationsIdx = Record.size();
2985 Record.emplace_back();
2986
2987 // Emit the state transitions.
2988 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00002989 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
2990 if (!FileIDAndFile.first.isValid() ||
2991 !FileIDAndFile.second.HasLocalTransitions)
2992 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00002993 ++NumLocations;
Richard Smithd230de22017-01-26 01:01:01 +00002994 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
2995 Record);
2996 Record.push_back(FileIDAndFile.second.StateTransitions.size());
2997 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
2998 Record.push_back(StatePoint.Offset);
2999 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003000 }
3001 }
3002
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003003 // Backpatch the number of locations.
3004 Record[NumLocationsIdx] = NumLocations;
3005
3006 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00003007 //
3008 // This also protects against a hypothetical corner case with simulating
3009 // -Werror settings for implicit modules in the ASTReader, where reading
3010 // CurDiagState out of context could change whether warning pragmas are
3011 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003012 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3013 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3014
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00003015 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003016}
3017
Douglas Gregorc5046832009-04-27 18:38:38 +00003018//===----------------------------------------------------------------------===//
3019// Type Serialization
3020//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00003021
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003022/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003023void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00003024 TypeIdx &IdxRef = TypeIdxs[T];
3025 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3026 IdxRef = TypeIdx(NextTypeID++);
3027 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00003028
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003029 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00003030
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003031 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00003032
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003033 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003034 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00003035 W.Visit(T);
3036 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00003037
Richard Smith290d8012016-04-06 17:06:00 +00003038 // Record the offset for this type.
3039 unsigned Index = Idx.getIndex() - FirstTypeID;
3040 if (TypeOffsets.size() == Index)
3041 TypeOffsets.push_back(Offset);
3042 else if (TypeOffsets.size() < Index) {
3043 TypeOffsets.resize(Index + 1);
3044 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00003045 } else {
Richard Smith290d8012016-04-06 17:06:00 +00003046 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003047 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003048}
3049
Douglas Gregorc5046832009-04-27 18:38:38 +00003050//===----------------------------------------------------------------------===//
3051// Declaration Serialization
3052//===----------------------------------------------------------------------===//
3053
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003054/// \brief Write the block containing all of the declaration IDs
3055/// lexically declared within the given DeclContext.
3056///
3057/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3058/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003059uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003060 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003061 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003062 return 0;
3063
Douglas Gregor8f45df52009-04-16 22:23:12 +00003064 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003065 SmallVector<uint32_t, 128> KindDeclPairs;
3066 for (const auto *D : DC->decls()) {
3067 KindDeclPairs.push_back(D->getKind());
3068 KindDeclPairs.push_back(GetDeclRef(D));
3069 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003070
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003071 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00003072 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00003073 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3074 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003075 return Offset;
3076}
3077
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003078void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003079 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003080
3081 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003082 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003083 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00003085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003087 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003088 {
3089 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3090 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3091 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3092 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003093
3094 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003095 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003096 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00003098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003100 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003101 {
3102 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3103 FirstDeclID - NUM_PREDEF_DECL_IDS};
3104 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3105 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003106}
3107
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003108void ASTWriter::WriteFileDeclIDsMap() {
3109 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003110
Chandler Carruthb306d732015-03-27 00:31:20 +00003111 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3112 FileDeclIDs.begin(), FileDeclIDs.end());
3113 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3114 llvm::less_first());
3115
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003116 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003117 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3118 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3119 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3120 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3121 for (auto &LocDeclEntry : Info.DeclIDs)
3122 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003123 }
3124
David Blaikieb44f0bf2017-01-04 22:36:43 +00003125 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003126 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003127 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003128 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003129 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003130 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3131 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003132 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003133}
3134
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003135void ASTWriter::WriteComments() {
3136 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003137 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003138 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003139 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003140 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003141 AddSourceRange(I->getSourceRange(), Record);
3142 Record.push_back(I->getKind());
3143 Record.push_back(I->isTrailingComment());
3144 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003145 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3146 }
3147 Stream.ExitBlock();
3148}
3149
Douglas Gregorc5046832009-04-27 18:38:38 +00003150//===----------------------------------------------------------------------===//
3151// Global Method Pool and Selector Serialization
3152//===----------------------------------------------------------------------===//
3153
Douglas Gregore84a9da2009-04-20 20:36:09 +00003154namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003155
Douglas Gregorc78d3462009-04-24 21:10:55 +00003156// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003157class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003158 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003159
3160public:
3161 typedef Selector key_type;
3162 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003163
Sebastian Redl834bb972010-08-04 17:20:04 +00003164 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003165 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003166 ObjCMethodList Instance, Factory;
3167 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003168 typedef const data_type& data_type_ref;
3169
Justin Bogner25463f12014-04-18 20:27:24 +00003170 typedef unsigned hash_value_type;
3171 typedef unsigned offset_type;
3172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003173 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003174
Justin Bogner25463f12014-04-18 20:27:24 +00003175 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003176 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003177 }
Mike Stump11289f42009-09-09 15:08:12 +00003178
3179 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003180 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003181 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003182 using namespace llvm::support;
3183 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003184 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003185 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003186 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3187 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003188 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003189 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003190 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003191 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003192 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003193 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003194 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003195 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003196 return std::make_pair(KeyLen, DataLen);
3197 }
Mike Stump11289f42009-09-09 15:08:12 +00003198
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003199 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003200 using namespace llvm::support;
3201 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003202 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003203 assert((Start >> 32) == 0 && "Selector key offset too large");
3204 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003205 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003206 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003207 if (N == 0)
3208 N = 1;
3209 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003210 LE.write<uint32_t>(
3211 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003212 }
Mike Stump11289f42009-09-09 15:08:12 +00003213
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003214 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003215 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003216 using namespace llvm::support;
3217 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003218 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003219 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003220 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003221 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003222 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003223 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003224 ++NumInstanceMethods;
3225
3226 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003227 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003228 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003229 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003230 ++NumFactoryMethods;
3231
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003232 unsigned InstanceBits = Methods.Instance.getBits();
3233 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003234 unsigned InstanceHasMoreThanOneDeclBit =
3235 Methods.Instance.hasMoreThanOneDecl();
3236 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3237 (InstanceHasMoreThanOneDeclBit << 2) |
3238 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003239 unsigned FactoryBits = Methods.Factory.getBits();
3240 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003241 unsigned FactoryHasMoreThanOneDeclBit =
3242 Methods.Factory.hasMoreThanOneDecl();
3243 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3244 (FactoryHasMoreThanOneDeclBit << 2) |
3245 FactoryBits;
3246 LE.write<uint16_t>(FullInstanceBits);
3247 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003248 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003249 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003250 if (Method->getMethod())
3251 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003252 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003253 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003254 if (Method->getMethod())
3255 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003256
3257 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003258 }
3259};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003260
Douglas Gregorc78d3462009-04-24 21:10:55 +00003261} // end anonymous namespace
3262
Sebastian Redla19a67f2010-08-03 21:58:15 +00003263/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003264///
3265/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003266/// in an on-disk hash table indexed by the selector. The hash table also
3267/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003268void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003269 using namespace llvm;
3270
Sebastian Redla19a67f2010-08-03 21:58:15 +00003271 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003272 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003273 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003274 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003275 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003276 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003277 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003278 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003279
Sebastian Redla19a67f2010-08-03 21:58:15 +00003280 // Create the on-disk hash table representation. We walk through every
3281 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003282 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003283 for (auto &SelectorAndID : SelectorIDs) {
3284 Selector S = SelectorAndID.first;
3285 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003286 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003287 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003288 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003289 ObjCMethodList(),
3290 ObjCMethodList()
3291 };
3292 if (F != SemaRef.MethodPool.end()) {
3293 Data.Instance = F->second.first;
3294 Data.Factory = F->second.second;
3295 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003296 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003297 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003298 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003299 // Selector already exists. Did it change?
3300 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003301 for (ObjCMethodList *M = &Data.Instance;
3302 !changed && M && M->getMethod(); M = M->getNext()) {
3303 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003304 changed = true;
3305 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003306 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003307 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003308 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003309 changed = true;
3310 }
3311 if (!changed)
3312 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003313 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003314 // A new method pool entry.
3315 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003316 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003317 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003318 }
3319
Douglas Gregorc78d3462009-04-24 21:10:55 +00003320 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003321 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003322 uint32_t BucketOffset;
3323 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003324 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003325 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003326 llvm::raw_svector_ostream Out(MethodPool);
3327 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003328 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003329 BucketOffset = Generator.Emit(Out, Trait);
3330 }
3331
3332 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003333 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003334 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003338 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003339
Douglas Gregor95c13f52009-04-25 17:48:32 +00003340 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003341 {
3342 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3343 NumTableEntries};
3344 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3345 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003346
3347 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003348 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003349 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003353 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003354
3355 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003356 {
3357 RecordData::value_type Record[] = {
3358 SELECTOR_OFFSETS, SelectorOffsets.size(),
3359 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3360 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3361 bytes(SelectorOffsets));
3362 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003363 }
3364}
3365
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003366/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003367void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003368 using namespace llvm;
3369 if (SemaRef.ReferencedSelectors.empty())
3370 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003371
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003372 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003373 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003374
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003375 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003376 // very tricky to fix, and given that @selector shouldn't really appear in
3377 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003378 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3379 Selector Sel = SelectorAndLocation.first;
3380 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003381 Writer.AddSelectorRef(Sel);
3382 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003383 }
Richard Smithe64e7452016-04-18 21:54:58 +00003384 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003385}
3386
Douglas Gregorc5046832009-04-27 18:38:38 +00003387//===----------------------------------------------------------------------===//
3388// Identifier Table Serialization
3389//===----------------------------------------------------------------------===//
3390
Richard Smithb3761912015-02-05 23:08:52 +00003391/// Determine the declaration that should be put into the name lookup table to
3392/// represent the given declaration in this module. This is usually D itself,
3393/// but if D was imported and merged into a local declaration, we want the most
3394/// recent local declaration instead. The chosen declaration will be the most
3395/// recent declaration in any module that imports this one.
3396static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3397 NamedDecl *D) {
3398 if (!LangOpts.Modules || !D->isFromASTFile())
3399 return D;
3400
3401 if (Decl *Redecl = D->getPreviousDecl()) {
3402 // For Redeclarable decls, a prior declaration might be local.
3403 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003404 // If we find a local decl, we're done.
3405 if (!Redecl->isFromASTFile()) {
3406 // Exception: in very rare cases (for injected-class-names), not all
3407 // redeclarations are in the same semantic context. Skip ones in a
3408 // different context. They don't go in this lookup table at all.
3409 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3410 D->getDeclContext()->getRedeclContext()))
3411 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003412 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003413 }
3414
Richard Smithfe620d22015-03-05 23:24:12 +00003415 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003416 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003417 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003418 break;
3419 }
3420 } else if (Decl *First = D->getCanonicalDecl()) {
3421 // For Mergeable decls, the first decl might be local.
3422 if (!First->isFromASTFile())
3423 return cast<NamedDecl>(First);
3424 }
3425
3426 // All declarations are imported. Our most recent declaration will also be
3427 // the most recent one in anyone who imports us.
3428 return D;
3429}
3430
Douglas Gregorc78d3462009-04-24 21:10:55 +00003431namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003432
Richard Smithcf97cf62015-04-19 01:34:23 +00003433class ASTIdentifierTableTrait {
3434 ASTWriter &Writer;
3435 Preprocessor &PP;
3436 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003437 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003438 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003439 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003440
3441 /// \brief Determines whether this is an "interesting" identifier that needs a
3442 /// full IdentifierInfo structure written into the hash table. Notably, this
3443 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3444 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003445 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003446 if (MacroOffset ||
3447 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003448 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003449 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003450 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003451 return true;
3452
3453 return false;
3454 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003455
Douglas Gregore84a9da2009-04-20 20:36:09 +00003456public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003457 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003458 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003459
Sebastian Redl539c5062010-08-18 23:57:32 +00003460 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003461 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003462
Justin Bogner25463f12014-04-18 20:27:24 +00003463 typedef unsigned hash_value_type;
3464 typedef unsigned offset_type;
3465
Richard Smithd7329392015-04-21 21:46:32 +00003466 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003467 IdentifierResolver &IdResolver, bool IsModule,
3468 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003469 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003470 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3471 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003472
Richard Smithd79514e2016-02-05 19:03:40 +00003473 bool needDecls() const { return NeedDecls; }
3474
Justin Bogner25463f12014-04-18 20:27:24 +00003475 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003476 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003477 }
Mike Stump11289f42009-09-09 15:08:12 +00003478
Richard Smith33e0f7e2015-07-22 02:08:40 +00003479 bool isInterestingIdentifier(const IdentifierInfo *II) {
3480 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3481 return isInterestingIdentifier(II, MacroOffset);
3482 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003483
Richard Smith9c254182015-07-19 21:41:12 +00003484 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3485 return isInterestingIdentifier(II, 0);
3486 }
3487
Mike Stump11289f42009-09-09 15:08:12 +00003488 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003489 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003490 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003491 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003492 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3493 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003494 DataLen += 2; // 2 bytes for builtin ID
3495 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003496 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003497 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003498
Richard Smitha534a312015-07-21 23:54:07 +00003499 if (NeedDecls) {
3500 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3501 DEnd = IdResolver.end();
3502 D != DEnd; ++D)
3503 DataLen += 4;
3504 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003505 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003506 using namespace llvm::support;
3507 endian::Writer<little> LE(Out);
3508
Richard Smithdf8a8312015-03-13 04:05:01 +00003509 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003510 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003511 // We emit the key length after the data length so that every
3512 // string is preceded by a 16-bit length. This matches the PTH
3513 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003514 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003515 return std::make_pair(KeyLen, DataLen);
3516 }
Mike Stump11289f42009-09-09 15:08:12 +00003517
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003518 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003519 unsigned KeyLen) {
3520 // Record the location of the key data. This is used when generating
3521 // the mapping from persistent IDs to strings.
3522 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003523
3524 // Emit the offset of the key/data length information to the interesting
3525 // identifiers table if necessary.
3526 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3527 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3528
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003529 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003530 }
Mike Stump11289f42009-09-09 15:08:12 +00003531
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003532 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003533 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003534 using namespace llvm::support;
3535 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003536
Richard Smithd7329392015-04-21 21:46:32 +00003537 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3538 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003539 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003540 return;
3541 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003542
Justin Bognere1c147c2014-03-28 22:03:19 +00003543 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003544 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3545 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003546 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003547 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003548 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003549 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003550 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3551 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003552 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003553 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003554 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003555 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003556
Richard Smithd7329392015-04-21 21:46:32 +00003557 if (HadMacroDefinition)
3558 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003559
Richard Smitha534a312015-07-21 23:54:07 +00003560 if (NeedDecls) {
3561 // Emit the declaration IDs in reverse order, because the
3562 // IdentifierResolver provides the declarations as they would be
3563 // visible (e.g., the function "stat" would come before the struct
3564 // "stat"), but the ASTReader adds declarations to the end of the list
3565 // (so we need to see the struct "stat" before the function "stat").
3566 // Only emit declarations that aren't from a chained PCH, though.
3567 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3568 IdResolver.end());
3569 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3570 DEnd = Decls.rend();
3571 D != DEnd; ++D)
3572 LE.write<uint32_t>(
3573 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3574 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003575 }
3576};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003577
Douglas Gregore84a9da2009-04-20 20:36:09 +00003578} // end anonymous namespace
3579
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003580/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003581///
3582/// The identifier table consists of a blob containing string data
3583/// (the actual identifiers themselves) and a separate "offsets" index
3584/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003585void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3586 IdentifierResolver &IdResolver,
3587 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003588 using namespace llvm;
3589
Richard Smith33e0f7e2015-07-22 02:08:40 +00003590 RecordData InterestingIdents;
3591
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003592 // Create and write out the blob that contains the identifier
3593 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003594 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003595 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003596 ASTIdentifierTableTrait Trait(
3597 *this, PP, IdResolver, IsModule,
3598 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003599
Douglas Gregore6648fb2009-04-28 20:33:11 +00003600 // Look for any identifiers that were named while processing the
3601 // headers, but are otherwise not needed. We add these to the hash
3602 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003603 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003604 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003605 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003606 for (const auto &ID : PP.getIdentifierTable())
3607 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003608 // Sort the identifiers lexicographically before getting them references so
3609 // that their order is stable.
3610 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3611 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003612 if (Trait.isInterestingNonMacroIdentifier(II))
3613 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003614
Sebastian Redlff4a2952010-07-23 23:49:55 +00003615 // Create the on-disk hash table representation. We only store offsets
3616 // for identifiers that appear here for the first time.
3617 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003618 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003619 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003620 IdentID ID = IdentIDPair.second;
3621 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003622 // Write out identifiers if either the ID is local or the identifier has
3623 // changed since it was loaded.
3624 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3625 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003626 (Trait.needDecls() &&
3627 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003628 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003629 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003630
Douglas Gregore84a9da2009-04-20 20:36:09 +00003631 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003632 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003633 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003634 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003635 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003636 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003637 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003638 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003639 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003640 }
3641
3642 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003643 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003644 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003647 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003648
3649 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003650 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003651 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003652 }
3653
3654 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003655 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003656 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003657 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003660 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003661
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003662#ifndef NDEBUG
3663 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3664 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3665#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003666
3667 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3668 IdentifierOffsets.size(),
3669 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003670 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003671 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003672
3673 // In C++, write the list of interesting identifiers (those that are
3674 // defined as macros, poisoned, or similar unusual things).
3675 if (!InterestingIdents.empty())
3676 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003677}
3678
Douglas Gregorc5046832009-04-27 18:38:38 +00003679//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003680// DeclContext's Name Lookup Table Serialization
3681//===----------------------------------------------------------------------===//
3682
3683namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003684
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003685// Trait used for the on-disk hash table used in the method pool.
3686class ASTDeclContextNameLookupTrait {
3687 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003688 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003689
3690public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003691 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003692 typedef key_type key_type_ref;
3693
Richard Smithd88a7f12015-09-01 20:35:42 +00003694 /// A start and end index into DeclIDs, representing a sequence of decls.
3695 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003696 typedef const data_type& data_type_ref;
3697
Justin Bogner25463f12014-04-18 20:27:24 +00003698 typedef unsigned hash_value_type;
3699 typedef unsigned offset_type;
3700
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003701 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3702
Richard Smithd88a7f12015-09-01 20:35:42 +00003703 template<typename Coll>
3704 data_type getData(const Coll &Decls) {
3705 unsigned Start = DeclIDs.size();
3706 for (NamedDecl *D : Decls) {
3707 DeclIDs.push_back(
3708 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3709 }
3710 return std::make_pair(Start, DeclIDs.size());
3711 }
3712
3713 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3714 unsigned Start = DeclIDs.size();
3715 for (auto ID : FromReader)
3716 DeclIDs.push_back(ID);
3717 return std::make_pair(Start, DeclIDs.size());
3718 }
3719
3720 static bool EqualKey(key_type_ref a, key_type_ref b) {
3721 return a == b;
3722 }
3723
Richard Smitha06c7e62015-08-26 23:55:49 +00003724 hash_value_type ComputeHash(DeclarationNameKey Name) {
3725 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003726 }
3727
Richard Smithd88a7f12015-09-01 20:35:42 +00003728 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3729 assert(Writer.hasChain() &&
3730 "have reference to loaded module file but no chain?");
3731
3732 using namespace llvm::support;
3733 endian::Writer<little>(Out)
3734 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3735 }
3736
Richard Smitha06c7e62015-08-26 23:55:49 +00003737 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3738 DeclarationNameKey Name,
3739 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003740 using namespace llvm::support;
3741 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003742 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003743 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003744 case DeclarationName::Identifier:
3745 case DeclarationName::ObjCZeroArgSelector:
3746 case DeclarationName::ObjCOneArgSelector:
3747 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003748 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003749 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003750 KeyLen += 4;
3751 break;
3752 case DeclarationName::CXXOperatorName:
3753 KeyLen += 1;
3754 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003755 case DeclarationName::CXXConstructorName:
3756 case DeclarationName::CXXDestructorName:
3757 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003758 case DeclarationName::CXXUsingDirective:
3759 break;
3760 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003761 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003762
Richard Smithf02662d2015-07-30 03:17:16 +00003763 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003764 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3765 assert(uint16_t(DataLen) == DataLen &&
3766 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003767 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003768
3769 return std::make_pair(KeyLen, DataLen);
3770 }
3771
Richard Smitha06c7e62015-08-26 23:55:49 +00003772 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003773 using namespace llvm::support;
3774 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003775 LE.write<uint8_t>(Name.getKind());
3776 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003777 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003778 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003779 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003780 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003781 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003782 case DeclarationName::ObjCZeroArgSelector:
3783 case DeclarationName::ObjCOneArgSelector:
3784 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003785 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003786 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003787 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003788 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003789 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003790 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003791 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003792 case DeclarationName::CXXConstructorName:
3793 case DeclarationName::CXXDestructorName:
3794 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003795 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003796 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003797 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003798
3799 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003800 }
3801
Richard Smitha06c7e62015-08-26 23:55:49 +00003802 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3803 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003804 using namespace llvm::support;
3805 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003806 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003807 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3808 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003809 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3810 }
3811};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003812
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003813} // end anonymous namespace
3814
Chandler Carruthe972c362015-03-26 03:11:40 +00003815bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3816 DeclContext *DC) {
3817 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3818}
3819
3820bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3821 DeclContext *DC) {
3822 for (auto *D : Result.getLookupResult())
3823 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3824 return false;
3825
3826 return true;
3827}
3828
Richard Smithd88a7f12015-09-01 20:35:42 +00003829void
Chandler Carruthe972c362015-03-26 03:11:40 +00003830ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003831 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003832 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3833 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003834 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003835
Chandler Carruthe972c362015-03-26 03:11:40 +00003836 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003837 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003838 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3839
3840 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003841 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3842 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003843 ASTDeclContextNameLookupTrait Trait(*this);
3844
Chandler Carruthe972c362015-03-26 03:11:40 +00003845 // The first step is to collect the declaration names which we need to
3846 // serialize into the name lookup table, and to collect them in a stable
3847 // order.
3848 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003849
Chandler Carruthe972c362015-03-26 03:11:40 +00003850 // We also build up small sets of the constructor and conversion function
3851 // names which are visible.
3852 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003853
Chandler Carruthe972c362015-03-26 03:11:40 +00003854 for (auto &Lookup : *DC->buildLookup()) {
3855 auto &Name = Lookup.first;
3856 auto &Result = Lookup.second;
3857
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003858 // If there are no local declarations in our lookup result, we
3859 // don't need to write an entry for the name at all. If we can't
3860 // write out a lookup set without performing more deserialization,
3861 // just skip this entry.
3862 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003863 isLookupResultEntirelyExternal(Result, DC))
3864 continue;
3865
3866 // We also skip empty results. If any of the results could be external and
3867 // the currently available results are empty, then all of the results are
3868 // external and we skip it above. So the only way we get here with an empty
3869 // results is when no results could have been external *and* we have
3870 // external results.
3871 //
3872 // FIXME: While we might want to start emitting on-disk entries for negative
3873 // lookups into a decl context as an optimization, today we *have* to skip
3874 // them because there are names with empty lookup results in decl contexts
3875 // which we can't emit in any stable ordering: we lookup constructors and
3876 // conversion functions in the enclosing namespace scope creating empty
3877 // results for them. This in almost certainly a bug in Clang's name lookup,
3878 // but that is likely to be hard or impossible to fix and so we tolerate it
3879 // here by omitting lookups with empty results.
3880 if (Lookup.second.getLookupResult().empty())
3881 continue;
3882
3883 switch (Lookup.first.getNameKind()) {
3884 default:
3885 Names.push_back(Lookup.first);
3886 break;
3887
Richard Smithcd45dbc2014-04-19 03:48:30 +00003888 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003889 assert(isa<CXXRecordDecl>(DC) &&
3890 "Cannot have a constructor name outside of a class!");
3891 ConstructorNameSet.insert(Name);
3892 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003893
3894 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003895 assert(isa<CXXRecordDecl>(DC) &&
3896 "Cannot have a conversion function name outside of a class!");
3897 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003898 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003899 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003900 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003901
Chandler Carruthe972c362015-03-26 03:11:40 +00003902 // Sort the names into a stable order.
3903 std::sort(Names.begin(), Names.end());
3904
Chandler Carruthffbf7052015-03-26 22:27:09 +00003905 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003906 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003907 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003908
Chandler Carruthffbf7052015-03-26 22:27:09 +00003909 // First we try the easy case by forming the current context's constructor
3910 // name and adding that name first. This is a very useful optimization to
3911 // avoid walking the lexical declarations in many cases, and it also
3912 // handles the only case where a constructor name can come from some other
3913 // lexical context -- when that name is an implicit constructor merged from
3914 // another declaration in the redecl chain. Any non-implicit constructor or
3915 // conversion function which doesn't occur in all the lexical contexts
3916 // would be an ODR violation.
3917 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3918 Context->getCanonicalType(Context->getRecordType(D)));
3919 if (ConstructorNameSet.erase(ImplicitCtorName))
3920 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003921
Chandler Carruthffbf7052015-03-26 22:27:09 +00003922 // If we still have constructors or conversion functions, we walk all the
3923 // names in the decl and add the constructors and conversion functions
3924 // which are visible in the order they lexically occur within the context.
3925 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3926 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3927 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3928 auto Name = ChildND->getDeclName();
3929 switch (Name.getNameKind()) {
3930 default:
3931 continue;
3932
3933 case DeclarationName::CXXConstructorName:
3934 if (ConstructorNameSet.erase(Name))
3935 Names.push_back(Name);
3936 break;
3937
3938 case DeclarationName::CXXConversionFunctionName:
3939 if (ConversionNameSet.erase(Name))
3940 Names.push_back(Name);
3941 break;
3942 }
3943
3944 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3945 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003946 }
3947
Chandler Carruthe972c362015-03-26 03:11:40 +00003948 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3949 "constructors by walking all the "
3950 "lexical members of the context.");
3951 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3952 "conversion functions by walking all "
3953 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003954 }
3955
Chandler Carruthe972c362015-03-26 03:11:40 +00003956 // Next we need to do a lookup with each name into this decl context to fully
3957 // populate any results from external sources. We don't actually use the
3958 // results of these lookups because we only want to use the results after all
3959 // results have been loaded and the pointers into them will be stable.
3960 for (auto &Name : Names)
3961 DC->lookup(Name);
3962
3963 // Now we need to insert the results for each name into the hash table. For
3964 // constructor names and conversion function names, we actually need to merge
3965 // all of the results for them into one list of results each and insert
3966 // those.
3967 SmallVector<NamedDecl *, 8> ConstructorDecls;
3968 SmallVector<NamedDecl *, 8> ConversionDecls;
3969
3970 // Now loop over the names, either inserting them or appending for the two
3971 // special cases.
3972 for (auto &Name : Names) {
3973 DeclContext::lookup_result Result = DC->noload_lookup(Name);
3974
3975 switch (Name.getNameKind()) {
3976 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00003977 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003978 break;
3979
3980 case DeclarationName::CXXConstructorName:
3981 ConstructorDecls.append(Result.begin(), Result.end());
3982 break;
3983
3984 case DeclarationName::CXXConversionFunctionName:
3985 ConversionDecls.append(Result.begin(), Result.end());
3986 break;
3987 }
3988 }
3989
3990 // Handle our two special cases if we ended up having any. We arbitrarily use
3991 // the first declaration's name here because the name itself isn't part of
3992 // the key, only the kind of name is used.
3993 if (!ConstructorDecls.empty())
3994 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003995 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003996 if (!ConversionDecls.empty())
3997 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00003998 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00003999
Richard Smithd88a7f12015-09-01 20:35:42 +00004000 // Create the on-disk hash table. Also emit the existing imported and
4001 // merged table if there is one.
4002 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4003 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00004004}
4005
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004006/// \brief Write the block containing all of the declaration IDs
4007/// visible from the given DeclContext.
4008///
4009/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00004010/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004011uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4012 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00004013 // If we imported a key declaration of this namespace, write the visible
4014 // lookup results as an update record for it rather than including them
4015 // on this declaration. We will only look at key declarations on reload.
4016 if (isa<NamespaceDecl>(DC) && Chain &&
4017 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4018 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004019 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00004020 Prev = Prev->getPreviousDecl())
4021 if (!Prev->isFromASTFile())
4022 return 0;
4023
4024 // Note that we need to emit an update record for the primary context.
4025 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4026
4027 // Make sure all visible decls are written. They will be recorded later. We
4028 // do this using a side data structure so we can sort the names into
4029 // a deterministic order.
4030 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4031 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4032 LookupResults;
4033 if (Map) {
4034 LookupResults.reserve(Map->size());
4035 for (auto &Entry : *Map)
4036 LookupResults.push_back(
4037 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4038 }
4039
4040 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
4041 for (auto &NameAndResult : LookupResults) {
4042 DeclarationName Name = NameAndResult.first;
4043 DeclContext::lookup_result Result = NameAndResult.second;
4044 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4045 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4046 // We have to work around a name lookup bug here where negative lookup
4047 // results for these names get cached in namespace lookup tables (these
4048 // names should never be looked up in a namespace).
4049 assert(Result.empty() && "Cannot have a constructor or conversion "
4050 "function name in a namespace!");
4051 continue;
4052 }
4053
4054 for (NamedDecl *ND : Result)
4055 if (!ND->isFromASTFile())
4056 GetDeclRef(ND);
4057 }
4058
4059 return 0;
4060 }
4061
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004062 if (DC->getPrimaryContext() != DC)
4063 return 0;
4064
Chandler Carruthe972c362015-03-26 03:11:40 +00004065 // Skip contexts which don't support name lookup.
4066 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004067 return 0;
4068
4069 // If not in C++, we perform name lookup for the translation unit via the
4070 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004071 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004072 return 0;
4073
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004074 // Serialize the contents of the mapping used for lookup. Note that,
4075 // although we have two very different code paths, the serialized
4076 // representation is the same for both cases: a declaration name,
4077 // followed by a size, followed by references to the visible
4078 // declarations that have that name.
4079 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00004080 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004081 if (!Map || Map->empty())
4082 return 0;
4083
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004084 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004085 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004086 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004087
4088 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004089 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004090 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00004091 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004092 ++NumVisibleDeclContexts;
4093 return Offset;
4094}
4095
Sebastian Redla4071b42010-08-24 00:50:09 +00004096/// \brief Write an UPDATE_VISIBLE block for the given context.
4097///
4098/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4099/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00004100/// (in C++), for namespaces, and for classes with forward-declared unscoped
4101/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00004102void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00004103 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00004104 if (!Map || Map->empty())
4105 return;
4106
Sebastian Redla4071b42010-08-24 00:50:09 +00004107 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004108 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004109 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004110
Richard Smith5fc18a92015-07-12 23:43:21 +00004111 // If we're updating a namespace, select a key declaration as the key for the
4112 // update record; those are the only ones that will be checked on reload.
4113 if (isa<NamespaceDecl>(DC))
4114 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4115
Sebastian Redla4071b42010-08-24 00:50:09 +00004116 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004117 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004118 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004119}
4120
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004121/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4122void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004123 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004124 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4125}
4126
4127/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4128void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004129 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004130 return;
4131
4132 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4133 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004134 for (const auto &I:Opts.OptMap) {
4135 AddString(I.getKey(), Record);
4136 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004137 Record.push_back(V.Supported ? 1 : 0);
4138 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004139 Record.push_back(V.Avail);
4140 Record.push_back(V.Core);
4141 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004142 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4143}
4144
Yaxun Liu5b746652016-12-18 05:18:55 +00004145void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4146 if (!SemaRef.Context.getLangOpts().OpenCL)
4147 return;
4148
4149 RecordData Record;
4150 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4151 Record.push_back(
4152 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4153 Record.push_back(I.second.size());
4154 for (auto Ext : I.second)
4155 AddString(Ext, Record);
4156 }
4157 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4158}
4159
4160void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4161 if (!SemaRef.Context.getLangOpts().OpenCL)
4162 return;
4163
4164 RecordData Record;
4165 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4166 Record.push_back(getDeclID(I.first));
4167 Record.push_back(static_cast<unsigned>(I.second.size()));
4168 for (auto Ext : I.second)
4169 AddString(Ext, Record);
4170 }
4171 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4172}
4173
Justin Lebar67a78a62016-10-08 22:15:58 +00004174void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4175 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4176 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4177 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4178 }
4179}
4180
Douglas Gregor404cdde2012-01-27 01:47:08 +00004181void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004182 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004183 RecordData Categories;
4184
4185 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4186 unsigned Size = 0;
4187 unsigned StartIndex = Categories.size();
4188
4189 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4190
4191 // Allocate space for the size.
4192 Categories.push_back(0);
4193
4194 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004195 for (ObjCInterfaceDecl::known_categories_iterator
4196 Cat = Class->known_categories_begin(),
4197 CatEnd = Class->known_categories_end();
4198 Cat != CatEnd; ++Cat, ++Size) {
4199 assert(getDeclID(*Cat) != 0 && "Bogus category");
4200 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004201 }
4202
4203 // Update the size.
4204 Categories[StartIndex] = Size;
4205
4206 // Record this interface -> category map.
4207 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4208 CategoriesMap.push_back(CatInfo);
4209 }
4210
4211 // Sort the categories map by the definition ID, since the reader will be
4212 // performing binary searches on this information.
4213 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4214
4215 // Emit the categories map.
4216 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004217
David Blaikieb44f0bf2017-01-04 22:36:43 +00004218 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004219 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004222 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004223
4224 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4225 Stream.EmitRecordWithBlob(AbbrevID, Record,
4226 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004227 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004228
Douglas Gregor404cdde2012-01-27 01:47:08 +00004229 // Emit the category lists.
4230 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4231}
4232
Richard Smithe40f2ba2013-08-07 21:41:30 +00004233void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4234 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4235
4236 if (LPTMap.empty())
4237 return;
4238
4239 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004240 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004241 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004242 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004243 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004244 AddDeclRef(LPT.D, Record);
4245 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004246
Justin Lebar28f09c52016-10-10 16:26:08 +00004247 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004248 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004249 }
4250 }
4251 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4252}
4253
Dario Domizioli13a0a382014-05-23 12:13:25 +00004254/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4255void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4256 RecordData Record;
4257 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4258 AddSourceLocation(PragmaLoc, Record);
4259 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4260}
4261
Nico Weber779355f2016-03-02 23:22:00 +00004262/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4263void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4264 RecordData Record;
4265 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4266 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4267}
4268
Nico Weber42932312016-03-03 00:17:35 +00004269/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4270//module.
4271void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4272 RecordData Record;
4273 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4274 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4275 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4276}
4277
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004278/// \brief Write the state of 'pragma pack' at the end of the module.
4279void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004280 // Don't serialize pragma pack state for modules, since it should only take
4281 // effect on a per-submodule basis.
4282 if (WritingModule)
4283 return;
4284
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004285 RecordData Record;
4286 Record.push_back(SemaRef.PackStack.CurrentValue);
4287 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4288 Record.push_back(SemaRef.PackStack.Stack.size());
4289 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4290 Record.push_back(StackEntry.Value);
4291 AddSourceLocation(StackEntry.PragmaLocation, Record);
4292 AddString(StackEntry.StackSlotLabel, Record);
4293 }
4294 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4295}
4296
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004297void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4298 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004299 // Enter the extension block.
4300 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4301
4302 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004303 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004304 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4305 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4306 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4307 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4308 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4309 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004310 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004311
4312 // Emit the metadata record.
4313 RecordData Record;
4314 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4315 Record.push_back(EXTENSION_METADATA);
4316 Record.push_back(Metadata.MajorVersion);
4317 Record.push_back(Metadata.MinorVersion);
4318 Record.push_back(Metadata.BlockName.size());
4319 Record.push_back(Metadata.UserInfo.size());
4320 SmallString<64> Buffer;
4321 Buffer += Metadata.BlockName;
4322 Buffer += Metadata.UserInfo;
4323 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4324
4325 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004326 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004327
4328 // Exit the extension block.
4329 Stream.ExitBlock();
4330}
4331
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004332//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004333// General Serialization Routines
4334//===----------------------------------------------------------------------===//
4335
Richard Smith69c82bf2016-04-01 22:52:03 +00004336/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004337void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4338 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004339 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004340 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004341 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004342 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004343
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004344#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004345
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004346 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004347}
4348
John McCallf413f5e2013-05-03 00:10:13 +00004349void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4350 AddSourceLocation(Tok.getLocation(), Record);
4351 Record.push_back(Tok.getLength());
4352
4353 // FIXME: When reading literal tokens, reconstruct the literal pointer
4354 // if it is needed.
4355 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4356 // FIXME: Should translate token kind to a stable encoding.
4357 Record.push_back(Tok.getKind());
4358 // FIXME: Should translate token flags to a stable encoding.
4359 Record.push_back(Tok.getFlags());
4360}
4361
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004362void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004363 Record.push_back(Str.size());
4364 Record.insert(Record.end(), Str.begin(), Str.end());
4365}
4366
Richard Smith7ed1bc92014-12-05 22:42:13 +00004367bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004368 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004369
Richard Smith54cc3c22014-12-11 20:50:24 +00004370 bool Changed =
4371 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004372
4373 // Remove a prefix to make the path relative, if relevant.
4374 const char *PathBegin = Path.data();
4375 const char *PathPtr =
4376 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4377 if (PathPtr != PathBegin) {
4378 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4379 Changed = true;
4380 }
4381
4382 return Changed;
4383}
4384
4385void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4386 SmallString<128> FilePath(Path);
4387 PreparePathForOutput(FilePath);
4388 AddString(FilePath, Record);
4389}
4390
Mehdi Amini57a41912015-09-10 01:46:39 +00004391void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004392 StringRef Path) {
4393 SmallString<128> FilePath(Path);
4394 PreparePathForOutput(FilePath);
4395 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4396}
4397
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004398void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4399 RecordDataImpl &Record) {
4400 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004401 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004402 Record.push_back(*Minor + 1);
4403 else
4404 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004405 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004406 Record.push_back(*Subminor + 1);
4407 else
4408 Record.push_back(0);
4409}
4410
Douglas Gregore84a9da2009-04-20 20:36:09 +00004411/// \brief Note that the identifier II occurs at the given offset
4412/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004413void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004414 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004415 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004416 // up earlier in the chain and thus don't need an offset.
4417 if (ID >= FirstIdentID)
4418 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004419}
4420
Douglas Gregor95c13f52009-04-25 17:48:32 +00004421/// \brief Note that the selector Sel occurs at the given offset
4422/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004423void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004424 unsigned ID = SelectorIDs[Sel];
4425 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004426 // Don't record offsets for selectors that are also available in a different
4427 // file.
4428 if (ID < FirstSelectorID)
4429 return;
4430 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004431}
4432
David Blaikie61137e12017-01-05 18:23:18 +00004433ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004434 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004435 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4436 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004437 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4438 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004439 for (const auto &Ext : Extensions) {
4440 if (auto Writer = Ext->createExtensionWriter(*this))
4441 ModuleFileExtensionWriters.push_back(std::move(Writer));
4442 }
4443}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004444
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004445ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004446 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004447}
4448
Richard Smithb3761912015-02-05 23:08:52 +00004449const LangOptions &ASTWriter::getLangOpts() const {
4450 assert(WritingAST && "can't determine lang opts when not writing AST");
4451 return Context->getLangOpts();
4452}
4453
Richard Smithe75ee0f2015-08-17 07:13:32 +00004454time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4455 return IncludeTimestamps ? E->getModificationTime() : 0;
4456}
4457
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004458ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4459 const std::string &OutputFile,
4460 Module *WritingModule, StringRef isysroot,
4461 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004462 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004463
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004464 ASTHasCompilerErrors = hasErrors;
4465
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004466 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004467 Stream.Emit((unsigned)'C', 8);
4468 Stream.Emit((unsigned)'P', 8);
4469 Stream.Emit((unsigned)'C', 8);
4470 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004471
Chris Lattner28fa4e62009-04-26 22:26:21 +00004472 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004473
Douglas Gregoreda8e122011-08-09 15:13:55 +00004474 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004475 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004476 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004477 ASTFileSignature Signature =
4478 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004479 Context = nullptr;
4480 PP = nullptr;
4481 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004482 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004483
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004484 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004485 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4486 // Construct MemoryBuffer and update buffer manager.
4487 PCMCache.addBuffer(OutputFile,
4488 llvm::MemoryBuffer::getMemBufferCopy(
4489 StringRef(Buffer.begin(), Buffer.size())));
4490 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004491 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004492}
4493
Douglas Gregora94a1542011-07-27 21:45:57 +00004494template<typename Vector>
4495static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4496 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004497 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4498 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004499 Writer.AddDeclRef(*I, Record);
4500 }
4501}
4502
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004503ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4504 const std::string &OutputFile,
4505 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004506 using namespace llvm;
4507
Craig Toppera13603a2014-05-22 05:54:18 +00004508 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004509
Douglas Gregorcf68c582011-12-01 22:20:10 +00004510 // Make sure that the AST reader knows to finalize itself.
4511 if (Chain)
4512 Chain->finalizeForWriting();
4513
Sebastian Redl143413f2010-07-12 22:02:52 +00004514 ASTContext &Context = SemaRef.Context;
4515 Preprocessor &PP = SemaRef.PP;
4516
Douglas Gregordab42432011-08-12 00:15:20 +00004517 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004518 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4519 if (D) {
4520 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4521 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004522 }
4523 };
4524 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4525 PREDEF_DECL_TRANSLATION_UNIT_ID);
4526 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4527 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4528 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4529 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4530 PREDEF_DECL_OBJC_PROTOCOL_ID);
4531 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4532 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4533 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4534 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4535 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004536 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004537 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4538 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004539 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004540 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4541 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004542 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4543 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004544 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4545 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004546 RegisterPredefDecl(Context.TypePackElementDecl,
4547 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004548
Chris Lattner0c797362009-09-08 18:19:27 +00004549 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004550 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004551 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004552 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004553 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004554
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004555 // Build a record containing all of the file scoped decls in this file.
4556 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004557 if (!isModule)
4558 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4559 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004560
Douglas Gregor851443c2011-08-12 01:39:19 +00004561 // Build a record containing all of the delegating constructors we still need
4562 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004563 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004564 if (!isModule)
4565 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004566
Douglas Gregor851443c2011-08-12 01:39:19 +00004567 // Write the set of weak, undeclared identifiers. We always write the
4568 // entire table, since later PCH files in a PCH chain are only interested in
4569 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004570 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004571 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4572 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4573 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4574 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4575 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4576 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4577 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004578 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004579
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004580 // Build a record containing all of the ext_vector declarations.
4581 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004582 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004583
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004584 // Build a record containing all of the VTable uses information.
4585 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004586 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004587 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4588 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4589 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4590 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4591 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004592 }
4593
Nico Weber72889432014-09-06 01:25:55 +00004594 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4595 RecordData UnusedLocalTypedefNameCandidates;
4596 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4597 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4598
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004599 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004600 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004601 for (const auto &I : SemaRef.PendingInstantiations) {
4602 AddDeclRef(I.first, PendingInstantiations);
4603 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004604 }
4605 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4606 "There are local ones at end of translation unit!");
4607
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004608 // Build a record containing some declaration references.
4609 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004610 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004611 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4612 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004613 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004614 }
4615
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004616 RecordData CUDASpecialDeclRefs;
4617 if (Context.getcudaConfigureCallDecl()) {
4618 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4619 }
4620
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004621 // Build a record containing all of the known namespaces.
4622 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004623 for (const auto &I : SemaRef.KnownNamespaces) {
4624 if (!I.second)
4625 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004626 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004627
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004628 // Build a record of all used, undefined objects that require definitions.
4629 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004630
4631 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004632 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004633 for (const auto &I : Undefined) {
4634 AddDeclRef(I.first, UndefinedButUsed);
4635 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004636 }
4637
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004638 // Build a record containing all delete-expressions that we would like to
4639 // analyze later in AST.
4640 RecordData DeleteExprsToAnalyze;
4641
4642 for (const auto &DeleteExprsInfo :
4643 SemaRef.getMismatchingDeleteExpressions()) {
4644 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4645 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4646 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4647 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4648 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4649 }
4650 }
4651
Douglas Gregor112b9072012-10-18 05:31:06 +00004652 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004653 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004654
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004655 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004656 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004657
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004658 // This is so that older clang versions, before the introduction
4659 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004660 {
4661 RecordData Record = {VERSION_MAJOR};
4662 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4663 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004664
Douglas Gregor851443c2011-08-12 01:39:19 +00004665 // Create a lexical update block containing all of the declarations in the
4666 // translation unit that do not come from other AST files.
4667 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004668 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4669 for (const auto *D : TU->noload_decls()) {
4670 if (!D->isFromASTFile()) {
4671 NewGlobalKindDeclPairs.push_back(D->getKind());
4672 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4673 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004674 }
4675
David Blaikieb44f0bf2017-01-04 22:36:43 +00004676 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004677 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4678 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004679 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004680 {
4681 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4682 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4683 bytes(NewGlobalKindDeclPairs));
4684 }
4685
Douglas Gregor851443c2011-08-12 01:39:19 +00004686 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004687 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004688 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4689 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004690 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004691 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004692 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004693
4694 // If we have any extern "C" names, write out a visible update for them.
4695 if (Context.ExternCContext)
4696 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004697
4698 // If the translation unit has an anonymous namespace, and we don't already
4699 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004700 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004701 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4702 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004703 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004704 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004705 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004706
Richard Smith5652c0f2014-03-21 01:48:23 +00004707 // Add update records for all mangling numbers and static local numbers.
4708 // These aren't really update records, but this is a convenient way of
4709 // tagging this rare extra data onto the declarations.
4710 for (const auto &Number : Context.MangleNumbers)
4711 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004712 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4713 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004714 for (const auto &Number : Context.StaticLocalNumbers)
4715 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004716 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4717 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004718
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004719 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004720 // an AST file, are registered for serialization. Likewise for template
4721 // specializations added to imported templates.
4722 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004723 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004724 }
4725
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004726 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004727 // serialization, if we're storing decls with identifiers.
4728 if (!WritingModule || !getLangOpts().CPlusPlus) {
4729 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004730 for (const auto &ID : PP.getIdentifierTable()) {
4731 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004732 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4733 IIs.push_back(II);
4734 }
4735 // Sort the identifiers to visit based on their name.
4736 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4737 for (const IdentifierInfo *II : IIs) {
4738 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4739 DEnd = SemaRef.IdResolver.end();
4740 D != DEnd; ++D) {
4741 GetDeclRef(*D);
4742 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004743 }
4744 }
4745
Manman Rena0f31a02016-04-29 19:04:05 +00004746 // For method pool in the module, if it contains an entry for a selector,
4747 // the entry should be complete, containing everything introduced by that
4748 // module and all modules it imports. It's possible that the entry is out of
4749 // date, so we need to pull in the new content here.
4750
4751 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4752 // safe, we copy all selectors out.
4753 llvm::SmallVector<Selector, 256> AllSelectors;
4754 for (auto &SelectorAndID : SelectorIDs)
4755 AllSelectors.push_back(SelectorAndID.first);
4756 for (auto &Selector : AllSelectors)
4757 SemaRef.updateOutOfDateSelector(Selector);
4758
Douglas Gregor5204bde2011-08-02 16:26:37 +00004759 // Form the record of special types.
4760 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004761 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004762 AddTypeRef(Context.getFILEType(), SpecialTypes);
4763 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4764 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4765 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4766 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004767 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004768 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004769
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004770 if (Chain) {
4771 // Write the mapping information describing our module dependencies and how
4772 // each of those modules were mapped into our own offset/ID space, so that
4773 // the reader can build the appropriate mapping to its own offset/ID space.
4774 // The map consists solely of a blob with the following format:
4775 // *(module-name-len:i16 module-name:len*i8
4776 // source-location-offset:i32
4777 // identifier-id:i32
4778 // preprocessed-entity-id:i32
4779 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004780 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004781 // selector-id:i32
4782 // declaration-id:i32
4783 // c++-base-specifiers-id:i32
4784 // type-id:i32)
4785 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004786 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004787 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004789 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004790 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004791 {
4792 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004793 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004794 using namespace llvm::support;
4795 endian::Writer<little> LE(Out);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004796 StringRef FileName = M.FileName;
Justin Bognere1c147c2014-03-28 22:03:19 +00004797 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004798 Out.write(FileName.data(), FileName.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004799
Ben Langmuir785180e2014-10-20 16:27:30 +00004800 // Note: if a base ID was uint max, it would not be possible to load
4801 // another module after it or have more than one entity inside it.
4802 uint32_t None = std::numeric_limits<uint32_t>::max();
4803
4804 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4805 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4806 if (ShouldWrite)
4807 LE.write<uint32_t>(BaseID);
4808 else
4809 LE.write<uint32_t>(None);
4810 };
4811
Ben Langmuirfe971d92014-08-16 04:54:18 +00004812 // These values should be unique within a chain, since they will be read
4813 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004814 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4815 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4816 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4817 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4818 M.NumPreprocessedEntities);
4819 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4820 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4821 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4822 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004823 }
4824 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004825 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004826 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4827 Buffer.data(), Buffer.size());
4828 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004829
Richard Smithb9eab6d2014-03-20 19:44:17 +00004830 RecordData DeclUpdatesOffsetsRecord;
4831
Richard Smith59442b42014-03-20 20:07:19 +00004832 // Keep writing types, declarations, and declaration update records
4833 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004834 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4835 WriteTypeAbbrevs();
4836 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004837 do {
4838 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4839 while (!DeclTypesToEmit.empty()) {
4840 DeclOrType DOT = DeclTypesToEmit.front();
4841 DeclTypesToEmit.pop();
4842 if (DOT.isType())
4843 WriteType(DOT.getType());
4844 else
4845 WriteDecl(Context, DOT.getDecl());
4846 }
4847 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004848 Stream.ExitBlock();
4849
Richard Smithb9eab6d2014-03-20 19:44:17 +00004850 DoneWritingDeclsAndTypes = true;
4851
4852 // These things can only be done once we've written out decls and types.
4853 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004854 if (!DeclUpdatesOffsetsRecord.empty())
4855 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004856 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004857 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004858 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004859 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004860 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004861 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004862 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004863 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004864 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004865 WriteFPPragmaOptions(SemaRef.getFPOptions());
4866 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004867 WriteOpenCLExtensionTypes(SemaRef);
4868 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004869 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004870
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004871 // If we're emitting a module, write out the submodule information.
4872 if (WritingModule)
4873 WriteSubmodules(WritingModule);
4874
Douglas Gregor5204bde2011-08-02 16:26:37 +00004875 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4876
Douglas Gregord4df8652009-04-22 22:02:47 +00004877 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004878 if (!EagerlyDeserializedDecls.empty())
4879 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004880
David Blaikief63556d2017-04-12 20:58:33 +00004881 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00004882 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4883
Douglas Gregord4df8652009-04-22 22:02:47 +00004884 // Write the record containing tentative definitions.
4885 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004886 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004887
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004888 // Write the record containing unused file scoped decls.
4889 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004890 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004891
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004892 // Write the record containing weak undeclared identifiers.
4893 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004894 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004895 WeakUndeclaredIdentifiers);
4896
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004897 // Write the record containing ext_vector type names.
4898 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004899 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004900
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004901 // Write the record containing VTable uses information.
4902 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004903 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004904
Nico Weber72889432014-09-06 01:25:55 +00004905 // Write the record containing potentially unused local typedefs.
4906 if (!UnusedLocalTypedefNameCandidates.empty())
4907 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4908 UnusedLocalTypedefNameCandidates);
4909
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004910 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004911 if (!PendingInstantiations.empty())
4912 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004913
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004914 // Write the record containing declaration references of Sema.
4915 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004916 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004917
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004918 // Write the record containing CUDA-specific declaration references.
4919 if (!CUDASpecialDeclRefs.empty())
4920 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004921
4922 // Write the delegating constructors.
4923 if (!DelegatingCtorDecls.empty())
4924 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004925
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004926 // Write the known namespaces.
4927 if (!KnownNamespaces.empty())
4928 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004929
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004930 // Write the undefined internal functions and variables, and inline functions.
4931 if (!UndefinedButUsed.empty())
4932 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004933
4934 if (!DeleteExprsToAnalyze.empty())
4935 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4936
Douglas Gregor851443c2011-08-12 01:39:19 +00004937 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004938 for (auto *DC : UpdatedDeclContexts)
4939 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004940
Douglas Gregor959bb062011-12-03 01:15:29 +00004941 if (!WritingModule) {
4942 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004943 struct ModuleInfo {
4944 uint64_t ID;
4945 Module *M;
4946 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4947 };
Richard Smith56be7542014-03-21 00:33:59 +00004948 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004949 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004950 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004951 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4952 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004953 }
Richard Smith56be7542014-03-21 00:33:59 +00004954
4955 if (!Imports.empty()) {
4956 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4957 return A.ID < B.ID;
4958 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004959 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4960 return A.ID == B.ID;
4961 };
Richard Smith56be7542014-03-21 00:33:59 +00004962
4963 // Sort and deduplicate module IDs.
4964 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00004965 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00004966 Imports.end());
4967
4968 RecordData ImportedModules;
4969 for (const auto &Import : Imports) {
4970 ImportedModules.push_back(Import.ID);
4971 // FIXME: If the module has macros imported then later has declarations
4972 // imported, this location won't be the right one as a location for the
4973 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00004974 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00004975 }
4976
Douglas Gregor959bb062011-12-03 01:15:29 +00004977 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4978 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004979 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004980
Douglas Gregor404cdde2012-01-27 01:47:08 +00004981 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00004982 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00004983 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004984 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00004985 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00004986 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004987 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004988
Douglas Gregor08f01292009-04-17 22:13:46 +00004989 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00004990 RecordData::value_type Record[] = {
4991 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00004992 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004993 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00004994
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004995 // Write the module file extension blocks.
4996 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004997 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004998
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004999 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005000}
5001
Richard Smithb9eab6d2014-03-20 19:44:17 +00005002void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005003 if (DeclUpdates.empty())
5004 return;
5005
Richard Smith59442b42014-03-20 20:07:19 +00005006 DeclUpdateMap LocalUpdates;
5007 LocalUpdates.swap(DeclUpdates);
5008
Richard Smith6ef42932014-03-20 21:02:00 +00005009 for (auto &DeclUpdate : LocalUpdates) {
5010 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00005011
Richard Smithd28ac5b2014-03-22 23:33:22 +00005012 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00005013 RecordData RecordData;
5014 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00005015 for (auto &Update : DeclUpdate.second) {
5016 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5017
Richard Smith69c82bf2016-04-01 22:52:03 +00005018 // An updated body is emitted last, so that the reader doesn't need
5019 // to skip over the lazy body to reach statements for other records.
5020 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5021 HasUpdatedBody = true;
5022 else
5023 Record.push_back(Kind);
5024
Richard Smith6ef42932014-03-20 21:02:00 +00005025 switch (Kind) {
5026 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5027 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5028 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00005029 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00005030 Record.push_back(GetDeclRef(Update.getDecl()));
5031 break;
5032
Richard Smith4d235792014-08-07 18:53:08 +00005033 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00005034 break;
5035
Richard Smith05a21352017-06-22 22:18:46 +00005036 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: {
5037 const VarDecl *VD = cast<VarDecl>(D);
Richard Smith69c82bf2016-04-01 22:52:03 +00005038 Record.AddSourceLocation(Update.getLoc());
Richard Smith05a21352017-06-22 22:18:46 +00005039 if (VD->getInit()) {
5040 Record.push_back(!VD->isInitKnownICE() ? 1
5041 : (VD->isInitICE() ? 3 : 2));
5042 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
5043 } else {
5044 Record.push_back(0);
5045 }
Richard Smith4d235792014-08-07 18:53:08 +00005046 break;
Richard Smith05a21352017-06-22 22:18:46 +00005047 }
Richard Smith4d235792014-08-07 18:53:08 +00005048
John McCall32791cc2016-01-06 22:34:54 +00005049 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00005050 Record.AddStmt(const_cast<Expr *>(
5051 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00005052 break;
5053
Richard Smith4b054b22016-08-24 21:25:37 +00005054 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5055 Record.AddStmt(
5056 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5057 break;
5058
Richard Smithcd45dbc2014-04-19 03:48:30 +00005059 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5060 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005061 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00005062 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00005063 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00005064 *Context, const_cast<CXXRecordDecl *>(RD)));
5065
5066 // This state is sometimes updated by template instantiation, when we
5067 // switch from the specialization referring to the template declaration
5068 // to it referring to the template definition.
5069 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5070 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005071 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005072 } else {
5073 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5074 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005075 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00005076
5077 // The instantiation might have been resolved to a partial
5078 // specialization. If so, record which one.
5079 auto From = Spec->getInstantiatedFrom();
5080 if (auto PartialSpec =
5081 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5082 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00005083 Record.AddDeclRef(PartialSpec);
5084 Record.AddTemplateArgumentList(
5085 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00005086 } else {
5087 Record.push_back(false);
5088 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00005089 }
5090 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005091 Record.AddSourceLocation(RD->getLocation());
5092 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00005093 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005094
5095 // Instantiation may change attributes; write them all out afresh.
5096 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00005097 if (D->hasAttrs())
5098 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005099
5100 // FIXME: Ensure we don't get here for explicit instantiations.
5101 break;
5102 }
5103
Richard Smithf8134002015-03-10 01:41:22 +00005104 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005105 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00005106 break;
5107
Richard Smith564417a2014-03-20 21:47:22 +00005108 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
5109 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00005110 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
5111 Record);
5112 break;
5113
Richard Smith6ef42932014-03-20 21:02:00 +00005114 case UPD_CXX_DEDUCED_RETURN_TYPE:
5115 Record.push_back(GetOrCreateTypeID(Update.getType()));
5116 break;
5117
5118 case UPD_DECL_MARKED_USED:
5119 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005120
5121 case UPD_MANGLING_NUMBER:
5122 case UPD_STATIC_LOCAL_NUMBER:
5123 Record.push_back(Update.getNumber());
5124 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005125
Alexey Bataev97720002014-11-11 04:05:39 +00005126 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005127 Record.AddSourceRange(
5128 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005129 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005130
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005131 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5132 Record.AddSourceRange(
5133 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5134 break;
5135
Richard Smith65ebb4a2015-03-26 04:09:53 +00005136 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005137 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005138 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005139
5140 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005141 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005142 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005143 }
5144 }
5145
Richard Smithd28ac5b2014-03-22 23:33:22 +00005146 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005147 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005148 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005149 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005150 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005151 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005152 }
5153
Richard Smithcd45dbc2014-04-19 03:48:30 +00005154 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005155 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005156 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005157}
5158
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005159void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005160 uint32_t Raw = Loc.getRawEncoding();
5161 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005162}
5163
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005164void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005165 AddSourceLocation(Range.getBegin(), Record);
5166 AddSourceLocation(Range.getEnd(), Record);
5167}
5168
Richard Smithf144b542016-04-08 21:54:32 +00005169void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5170 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005171 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005172 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005173}
5174
Richard Smithf144b542016-04-08 21:54:32 +00005175void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5176 Record->push_back(Value.isUnsigned());
5177 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005178}
5179
Richard Smithf144b542016-04-08 21:54:32 +00005180void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5181 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005182}
5183
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005184void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005185 Record.push_back(getIdentifierRef(II));
5186}
5187
Sebastian Redl539c5062010-08-18 23:57:32 +00005188IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005189 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005190 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005191
Sebastian Redl539c5062010-08-18 23:57:32 +00005192 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005193 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005194 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005195 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005196}
5197
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005198MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005199 // Don't emit builtin macros like __LINE__ to the AST file unless they
5200 // have been redefined by the header (in which case they are not
5201 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005202 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005203 return 0;
5204
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005205 MacroID &ID = MacroIDs[MI];
5206 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005207 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005208 MacroInfoToEmitData Info = { Name, MI, ID };
5209 MacroInfosToEmit.push_back(Info);
5210 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005211 return ID;
5212}
5213
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005214MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005215 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005216 return 0;
5217
5218 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5219 return MacroIDs[MI];
5220}
5221
5222uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005223 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005224}
5225
Richard Smithe64e7452016-04-18 21:54:58 +00005226void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5227 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005228}
5229
Sebastian Redl539c5062010-08-18 23:57:32 +00005230SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005231 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005232 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005233 }
5234
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005235 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005236 if (SID == 0 && Chain) {
5237 // This might trigger a ReadSelector callback, which will set the ID for
5238 // this selector.
5239 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005240 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005241 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005242 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005243 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005244 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005245 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005246 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005247}
5248
Richard Smithe64e7452016-04-18 21:54:58 +00005249void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5250 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005251}
5252
Richard Smith290d8012016-04-06 17:06:00 +00005253void ASTRecordWriter::AddTemplateArgumentLocInfo(
5254 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005255 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005256 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005257 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005258 break;
5259 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005260 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005261 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005262 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005263 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5264 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005265 break;
5266 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005267 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5268 AddSourceLocation(Arg.getTemplateNameLoc());
5269 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005270 break;
John McCall0ad16662009-10-29 08:12:44 +00005271 case TemplateArgument::Null:
5272 case TemplateArgument::Integral:
5273 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005274 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005275 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005276 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005277 break;
5278 }
5279}
5280
Richard Smith290d8012016-04-06 17:06:00 +00005281void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5282 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005283
5284 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5285 bool InfoHasSameExpr
5286 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005287 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005288 if (InfoHasSameExpr)
5289 return; // Avoid storing the same expr twice.
5290 }
Richard Smith290d8012016-04-06 17:06:00 +00005291 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005292}
5293
Richard Smith290d8012016-04-06 17:06:00 +00005294void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005295 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005296 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005297 return;
5298 }
5299
Richard Smith290d8012016-04-06 17:06:00 +00005300 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005301}
5302
Richard Smith290d8012016-04-06 17:06:00 +00005303void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5304 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005305
Richard Smith290d8012016-04-06 17:06:00 +00005306 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005307 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005308 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005309}
5310
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005311void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005312 Record.push_back(GetOrCreateTypeID(T));
5313}
5314
Richard Smith2095ffe2015-03-16 20:11:03 +00005315TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005316 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005317 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5318 if (T.isNull())
5319 return TypeIdx();
5320 assert(!T.getLocalFastQualifiers());
5321
5322 TypeIdx &Idx = TypeIdxs[T];
5323 if (Idx.getIndex() == 0) {
5324 if (DoneWritingDeclsAndTypes) {
5325 assert(0 && "New type seen after serializing all the types to emit!");
5326 return TypeIdx();
5327 }
5328
5329 // We haven't seen this type before. Assign it a new ID and put it
5330 // into the queue of types to emit.
5331 Idx = TypeIdx(NextTypeID++);
5332 DeclTypesToEmit.push(T);
5333 }
5334 return Idx;
5335 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005336}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005337
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005338TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005339 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005340 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5341 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005342 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005343 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005344
Richard Smith2095ffe2015-03-16 20:11:03 +00005345 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5346 assert(I != TypeIdxs.end() && "Type not emitted!");
5347 return I->second;
5348 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005349}
5350
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005351void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005352 Record.push_back(GetDeclRef(D));
5353}
5354
Sebastian Redl539c5062010-08-18 23:57:32 +00005355DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005356 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005357
5358 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005359 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005360 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005361
5362 // If D comes from an AST file, its declaration ID is already known and
5363 // fixed.
5364 if (D->isFromASTFile())
5365 return D->getGlobalID();
5366
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005367 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005368 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005369 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005370 if (DoneWritingDeclsAndTypes) {
5371 assert(0 && "New decl seen after serializing all the decls to emit!");
5372 return 0;
5373 }
5374
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005375 // We haven't seen this declaration before. Give it a new ID and
5376 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005377 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005378 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005379 }
5380
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005381 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005382}
5383
Sebastian Redl539c5062010-08-18 23:57:32 +00005384DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005385 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005386 return 0;
5387
Douglas Gregorb3163e52012-01-05 22:33:30 +00005388 // If D comes from an AST file, its declaration ID is already known and
5389 // fixed.
5390 if (D->isFromASTFile())
5391 return D->getGlobalID();
5392
Douglas Gregore84a9da2009-04-20 20:36:09 +00005393 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5394 return DeclIDs[D];
5395}
5396
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005397void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005398 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005399 assert(D);
5400
5401 SourceLocation Loc = D->getLocation();
5402 if (Loc.isInvalid())
5403 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005404
5405 // We only keep track of the file-level declarations of each file.
5406 if (!D->getLexicalDeclContext()->isFileContext())
5407 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005408 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5409 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005410 if (isa<ParmVarDecl>(D))
5411 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005412
5413 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005414 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005415 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005416 FileID FID;
5417 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005418 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005419 if (FID.isInvalid())
5420 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005421 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005422
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005423 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005424 if (!Info)
5425 Info = new DeclIDInFileInfo();
5426
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005427 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005428 LocDeclIDsTy &Decls = Info->DeclIDs;
5429
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005430 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005431 Decls.push_back(LocDecl);
5432 return;
5433 }
5434
Benjamin Kramer45025c02013-08-24 13:22:59 +00005435 LocDeclIDsTy::iterator I =
5436 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005437
5438 Decls.insert(I, LocDecl);
5439}
5440
Richard Smithe64e7452016-04-18 21:54:58 +00005441void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005442 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005443 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005444 switch (Name.getNameKind()) {
5445 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005446 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005447 break;
5448
5449 case DeclarationName::ObjCZeroArgSelector:
5450 case DeclarationName::ObjCOneArgSelector:
5451 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005452 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005453 break;
5454
5455 case DeclarationName::CXXConstructorName:
5456 case DeclarationName::CXXDestructorName:
5457 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005458 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005459 break;
5460
Richard Smith35845152017-02-07 01:37:30 +00005461 case DeclarationName::CXXDeductionGuideName:
5462 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5463 break;
5464
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005465 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005466 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005467 break;
5468
Alexis Hunt3d221f22009-11-29 07:34:05 +00005469 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005470 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005471 break;
5472
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005473 case DeclarationName::CXXUsingDirective:
5474 // No extra data to emit
5475 break;
5476 }
5477}
Chris Lattnerca025db2010-05-07 21:43:38 +00005478
Richard Smithd08aeb62014-08-28 01:33:39 +00005479unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5480 assert(needsAnonymousDeclarationNumber(D) &&
5481 "expected an anonymous declaration");
5482
5483 // Number the anonymous declarations within this context, if we've not
5484 // already done so.
5485 auto It = AnonymousDeclarationNumbers.find(D);
5486 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005487 auto *DC = D->getLexicalDeclContext();
5488 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5489 AnonymousDeclarationNumbers[ND] = Number;
5490 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005491
5492 It = AnonymousDeclarationNumbers.find(D);
5493 assert(It != AnonymousDeclarationNumbers.end() &&
5494 "declaration not found within its lexical context");
5495 }
5496
5497 return It->second;
5498}
5499
Richard Smith290d8012016-04-06 17:06:00 +00005500void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5501 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005502 switch (Name.getNameKind()) {
5503 case DeclarationName::CXXConstructorName:
5504 case DeclarationName::CXXDestructorName:
5505 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005506 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005507 break;
5508
5509 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005510 AddSourceLocation(SourceLocation::getFromRawEncoding(
5511 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005512 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005513 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005514 break;
5515
5516 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005517 AddSourceLocation(SourceLocation::getFromRawEncoding(
5518 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005519 break;
5520
5521 case DeclarationName::Identifier:
5522 case DeclarationName::ObjCZeroArgSelector:
5523 case DeclarationName::ObjCOneArgSelector:
5524 case DeclarationName::ObjCMultiArgSelector:
5525 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005526 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005527 break;
5528 }
5529}
5530
Richard Smith290d8012016-04-06 17:06:00 +00005531void ASTRecordWriter::AddDeclarationNameInfo(
5532 const DeclarationNameInfo &NameInfo) {
5533 AddDeclarationName(NameInfo.getName());
5534 AddSourceLocation(NameInfo.getLoc());
5535 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005536}
5537
Richard Smith290d8012016-04-06 17:06:00 +00005538void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5539 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5540 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005541 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005542 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005543}
5544
Richard Smithe64e7452016-04-18 21:54:58 +00005545void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005546 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005547 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005548 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005549
5550 // Push each of the NNS's onto a stack for serialization in reverse order.
5551 while (NNS) {
5552 NestedNames.push_back(NNS);
5553 NNS = NNS->getPrefix();
5554 }
5555
Richard Smithe64e7452016-04-18 21:54:58 +00005556 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005557 while(!NestedNames.empty()) {
5558 NNS = NestedNames.pop_back_val();
5559 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005560 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005561 switch (Kind) {
5562 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005563 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005564 break;
5565
5566 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005567 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005568 break;
5569
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005570 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005571 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005572 break;
5573
Chris Lattnerca025db2010-05-07 21:43:38 +00005574 case NestedNameSpecifier::TypeSpec:
5575 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005576 AddTypeRef(QualType(NNS->getAsType(), 0));
5577 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005578 break;
5579
5580 case NestedNameSpecifier::Global:
5581 // Don't need to write an associated value.
5582 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005583
5584 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005585 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005586 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005587 }
5588 }
5589}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005590
Richard Smith290d8012016-04-06 17:06:00 +00005591void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005592 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005593 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005594 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005595
5596 // Push each of the nested-name-specifiers's onto a stack for
5597 // serialization in reverse order.
5598 while (NNS) {
5599 NestedNames.push_back(NNS);
5600 NNS = NNS.getPrefix();
5601 }
5602
Richard Smith290d8012016-04-06 17:06:00 +00005603 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005604 while(!NestedNames.empty()) {
5605 NNS = NestedNames.pop_back_val();
5606 NestedNameSpecifier::SpecifierKind Kind
5607 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005608 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005609 switch (Kind) {
5610 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005611 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5612 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005613 break;
5614
5615 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005616 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5617 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005618 break;
5619
5620 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005621 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5622 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005623 break;
5624
5625 case NestedNameSpecifier::TypeSpec:
5626 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005627 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5628 AddTypeLoc(NNS.getTypeLoc());
5629 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005630 break;
5631
5632 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005633 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005634 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005635
5636 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005637 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5638 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005639 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005640 }
5641 }
5642}
5643
Richard Smith290d8012016-04-06 17:06:00 +00005644void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005645 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005646 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005647 switch (Kind) {
5648 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005649 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005650 break;
5651
5652 case TemplateName::OverloadedTemplate: {
5653 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005654 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005655 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005656 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005657 break;
5658 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005659
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005660 case TemplateName::QualifiedTemplate: {
5661 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005662 AddNestedNameSpecifier(QualT->getQualifier());
5663 Record->push_back(QualT->hasTemplateKeyword());
5664 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005665 break;
5666 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005667
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005668 case TemplateName::DependentTemplate: {
5669 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005670 AddNestedNameSpecifier(DepT->getQualifier());
5671 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005672 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005673 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005674 else
Richard Smith290d8012016-04-06 17:06:00 +00005675 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005676 break;
5677 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005678
5679 case TemplateName::SubstTemplateTemplateParm: {
5680 SubstTemplateTemplateParmStorage *subst
5681 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005682 AddDeclRef(subst->getParameter());
5683 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005684 break;
5685 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005686
5687 case TemplateName::SubstTemplateTemplateParmPack: {
5688 SubstTemplateTemplateParmPackStorage *SubstPack
5689 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005690 AddDeclRef(SubstPack->getParameterPack());
5691 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005692 break;
5693 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005694 }
5695}
5696
Richard Smith290d8012016-04-06 17:06:00 +00005697void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5698 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005699 switch (Arg.getKind()) {
5700 case TemplateArgument::Null:
5701 break;
5702 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005703 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005704 break;
5705 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005706 AddDeclRef(Arg.getAsDecl());
5707 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005708 break;
5709 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005710 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005711 break;
5712 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005713 AddAPSInt(Arg.getAsIntegral());
5714 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005715 break;
5716 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005717 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005718 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005719 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005720 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005721 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005722 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005723 else
Richard Smith290d8012016-04-06 17:06:00 +00005724 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005725 break;
5726 case TemplateArgument::Expression:
5727 AddStmt(Arg.getAsExpr());
5728 break;
5729 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005730 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005731 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005732 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005733 break;
5734 }
5735}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005736
Richard Smithe64e7452016-04-18 21:54:58 +00005737void ASTRecordWriter::AddTemplateParameterList(
5738 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005739 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005740 AddSourceLocation(TemplateParams->getTemplateLoc());
5741 AddSourceLocation(TemplateParams->getLAngleLoc());
5742 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005743 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005744 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005745 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005746 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005747}
5748
5749/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005750void ASTRecordWriter::AddTemplateArgumentList(
5751 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005752 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005753 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005754 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005755 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005756}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005757
Richard Smith290d8012016-04-06 17:06:00 +00005758void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5759 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005760 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005761 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5762 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5763 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005764 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005765 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005766 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005767}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005768
Richard Smithe64e7452016-04-18 21:54:58 +00005769void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5770 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005771 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005772 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005773 AddDeclRef(I.getDecl());
5774 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005775 }
5776}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005777
Richard Smith290d8012016-04-06 17:06:00 +00005778// FIXME: Move this out of the main ASTRecordWriter interface.
5779void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5780 Record->push_back(Base.isVirtual());
5781 Record->push_back(Base.isBaseOfClass());
5782 Record->push_back(Base.getAccessSpecifierAsWritten());
5783 Record->push_back(Base.getInheritConstructors());
5784 AddTypeSourceInfo(Base.getTypeSourceInfo());
5785 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005786 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005787 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005788}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005789
Richard Smith645d2cf2016-04-14 00:29:55 +00005790static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5791 ArrayRef<CXXBaseSpecifier> Bases) {
5792 ASTWriter::RecordData Record;
5793 ASTRecordWriter Writer(W, Record);
5794 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005795
Richard Smith645d2cf2016-04-14 00:29:55 +00005796 for (auto &Base : Bases)
5797 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005798
Richard Smith645d2cf2016-04-14 00:29:55 +00005799 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005800}
5801
Richard Smith290d8012016-04-06 17:06:00 +00005802// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005803void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5804 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5805}
5806
Richard Smithaa165cf2016-04-13 21:57:08 +00005807static uint64_t
5808EmitCXXCtorInitializers(ASTWriter &W,
5809 ArrayRef<CXXCtorInitializer *> CtorInits) {
5810 ASTWriter::RecordData Record;
5811 ASTRecordWriter Writer(W, Record);
5812 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005813
Richard Smithaa165cf2016-04-13 21:57:08 +00005814 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005815 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005816 Writer.push_back(CTOR_INITIALIZER_BASE);
5817 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5818 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005819 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005820 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5821 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005822 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005823 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5824 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005825 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005826 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5827 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005828 }
Francois Pichetd583da02010-12-04 09:14:42 +00005829
Richard Smithaa165cf2016-04-13 21:57:08 +00005830 Writer.AddSourceLocation(Init->getMemberLocation());
5831 Writer.AddStmt(Init->getInit());
5832 Writer.AddSourceLocation(Init->getLParenLoc());
5833 Writer.AddSourceLocation(Init->getRParenLoc());
5834 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005835 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005836 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005837 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005838
5839 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005840}
5841
Richard Smithaa165cf2016-04-13 21:57:08 +00005842// FIXME: Move this out of the main ASTRecordWriter interface.
5843void ASTRecordWriter::AddCXXCtorInitializers(
5844 ArrayRef<CXXCtorInitializer *> CtorInits) {
5845 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005846}
5847
Richard Smith290d8012016-04-06 17:06:00 +00005848void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005849 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005850 Record->push_back(Data.IsLambda);
5851 Record->push_back(Data.UserDeclaredConstructor);
5852 Record->push_back(Data.UserDeclaredSpecialMembers);
5853 Record->push_back(Data.Aggregate);
5854 Record->push_back(Data.PlainOldData);
5855 Record->push_back(Data.Empty);
5856 Record->push_back(Data.Polymorphic);
5857 Record->push_back(Data.Abstract);
5858 Record->push_back(Data.IsStandardLayout);
5859 Record->push_back(Data.HasNoNonEmptyBases);
5860 Record->push_back(Data.HasPrivateFields);
5861 Record->push_back(Data.HasProtectedFields);
5862 Record->push_back(Data.HasPublicFields);
5863 Record->push_back(Data.HasMutableFields);
5864 Record->push_back(Data.HasVariantMembers);
5865 Record->push_back(Data.HasOnlyCMembers);
5866 Record->push_back(Data.HasInClassInitializer);
5867 Record->push_back(Data.HasUninitializedReferenceMember);
5868 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005869 Record->push_back(Data.HasInheritedConstructor);
5870 Record->push_back(Data.HasInheritedAssignment);
Richard Smith290d8012016-04-06 17:06:00 +00005871 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5872 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5873 Record->push_back(Data.NeedOverloadResolutionForDestructor);
5874 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5875 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5876 Record->push_back(Data.DefaultedDestructorIsDeleted);
5877 Record->push_back(Data.HasTrivialSpecialMembers);
5878 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5879 Record->push_back(Data.HasIrrelevantDestructor);
5880 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5881 Record->push_back(Data.HasDefaultedDefaultConstructor);
5882 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5883 Record->push_back(Data.HasConstexprDefaultConstructor);
5884 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5885 Record->push_back(Data.ComputedVisibleConversions);
5886 Record->push_back(Data.UserProvidedDefaultConstructor);
5887 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005888 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5889 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005890 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5891 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5892 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieufd1acbb2017-04-11 21:31:00 +00005893
5894 // getODRHash will compute the ODRHash if it has not been previously computed.
5895 Record->push_back(D->getODRHash());
David Blaikief63556d2017-04-12 20:58:33 +00005896 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
5897 Writer->WritingModule && !D->isDependentType();
5898 Record->push_back(ModulesDebugInfo);
5899 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00005900 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
5901
Richard Smith561fb152012-02-25 07:33:38 +00005902 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005903
Richard Smith290d8012016-04-06 17:06:00 +00005904 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005905 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005906 AddCXXBaseSpecifiers(Data.bases());
5907
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005908 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005909 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005910 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005911 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005912
Richard Smith290d8012016-04-06 17:06:00 +00005913 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5914 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005915 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005916 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005917
5918 // Add lambda-specific data.
5919 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005920 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005921 Record->push_back(Lambda.Dependent);
5922 Record->push_back(Lambda.IsGenericLambda);
5923 Record->push_back(Lambda.CaptureDefault);
5924 Record->push_back(Lambda.NumCaptures);
5925 Record->push_back(Lambda.NumExplicitCaptures);
5926 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005927 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005928 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005929 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005930 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005931 AddSourceLocation(Capture.getLocation());
5932 Record->push_back(Capture.isImplicit());
5933 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005934 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005935 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005936 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005937 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005938 break;
5939 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005940 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005941 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005942 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005943 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005944 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005945 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005946 break;
5947 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005948 }
5949 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005950}
5951
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005952void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005953 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005954 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005955 assert(FirstDeclID == NextDeclID &&
5956 FirstTypeID == NextTypeID &&
5957 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005958 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005959 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005960 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005961 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005962
Sebastian Redl07a89a82010-07-30 00:29:29 +00005963 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005964
Richard Smith7f330cd2015-03-18 01:42:29 +00005965 // Note, this will get called multiple times, once one the reader starts up
5966 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00005967 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5968 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5969 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005970 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005971 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005972 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005973 NextDeclID = FirstDeclID;
5974 NextTypeID = FirstTypeID;
5975 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005976 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005977 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005978 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005979}
5980
Sebastian Redl539c5062010-08-18 23:57:32 +00005981void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005982 // Always keep the highest ID. See \p TypeRead() for more information.
5983 IdentID &StoredID = IdentifierIDs[II];
5984 if (ID > StoredID)
5985 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005986}
5987
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005988void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005989 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005990 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005991 if (ID > StoredID)
5992 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005993}
5994
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005995void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005996 // Always take the highest-numbered type index. This copes with an interesting
5997 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005998 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005999 // keep the higher-numbered entry so that we can properly write it out to
6000 // the AST file.
6001 TypeIdx &StoredIdx = TypeIdxs[T];
6002 if (Idx.getIndex() >= StoredIdx.getIndex())
6003 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006004}
6005
Sebastian Redl539c5062010-08-18 23:57:32 +00006006void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006007 // Always keep the highest ID. See \p TypeRead() for more information.
6008 SelectorID &StoredID = SelectorIDs[S];
6009 if (ID > StoredID)
6010 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00006011}
Douglas Gregor91096292010-10-02 19:29:26 +00006012
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006013void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00006014 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006015 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00006016 MacroDefinitions[MD] = ID;
6017}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006018
Douglas Gregore37a85a2011-12-02 17:30:13 +00006019void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6020 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
6021 SubmoduleIDs[Mod] = ID;
6022}
6023
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006024void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006025 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00006026 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006027 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006028 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006029 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00006030 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006031 // A forward reference was mutated into a definition. Rewrite it.
6032 // FIXME: This happens during template instantiation, should we
6033 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00006034 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6035 "completed a tag from another module but not by instantiation?");
6036 DeclUpdates[RD].push_back(
6037 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006038 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006039 }
6040}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006041
Richard Smithf983f7f2015-10-13 00:23:25 +00006042static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6043 if (D->isFromASTFile())
6044 return true;
6045
Richard Smithf983f7f2015-10-13 00:23:25 +00006046 // The predefined __va_list_tag struct is imported if we imported any decls.
6047 // FIXME: This is a gross hack.
6048 return D == D->getASTContext().getVaListTagDecl();
6049}
6050
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006051void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006052 if (Chain && Chain->isProcessingUpdateRecords()) return;
6053 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00006054 "Should not add lookup results to non-lookup contexts!");
6055
Vassil Vassilev632eac32016-03-16 11:17:04 +00006056 // TU is handled elsewhere.
6057 if (isa<TranslationUnitDecl>(DC))
6058 return;
6059
6060 // Namespaces are handled elsewhere, except for template instantiations of
6061 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6062 // local instantiations are added to an imported context. Only happens when
6063 // adding ADL lookup candidates, for example templated friends.
6064 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6065 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006066 return;
6067
Richard Smithf983f7f2015-10-13 00:23:25 +00006068 // We're only interested in cases where a local declaration is added to an
6069 // imported context.
6070 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6071 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006072
Richard Smith0f4e2c42015-08-06 04:23:48 +00006073 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00006074 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00006075 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00006076 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6077 // We're adding a visible declaration to a predefined decl context. Ensure
6078 // that we write out all of its lookup results so we don't get a nasty
6079 // surprise when we try to emit its lookup table.
6080 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00006081 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00006082 }
Richard Smithc26d9742016-10-06 20:30:51 +00006083 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006084}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006085
6086void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006087 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006088 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00006089
6090 // We're only interested in cases where a local declaration is added to an
6091 // imported context.
6092 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6093 return;
6094
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006095 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00006096 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006097
6098 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00006099 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00006100 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00006101 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006102}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00006103
Richard Smith564417a2014-03-20 21:47:22 +00006104void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006105 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00006106 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6107 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006108 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006109 // If we don't already know the exception specification for this redecl
6110 // chain, add an update record for it.
6111 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6112 ->getType()
6113 ->castAs<FunctionProtoType>()
6114 ->getExceptionSpecType()))
6115 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6116 });
Richard Smith564417a2014-03-20 21:47:22 +00006117}
6118
Richard Smith1fa5d642013-05-11 05:45:24 +00006119void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006120 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006121 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006122 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006123 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006124 DeclUpdates[D].push_back(
6125 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6126 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006127}
6128
Richard Smithf8134002015-03-10 01:41:22 +00006129void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6130 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006131 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006132 assert(!WritingAST && "Already writing the AST!");
6133 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006134 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006135 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006136 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6137 });
Richard Smithf8134002015-03-10 01:41:22 +00006138}
6139
Sebastian Redlab238a72011-04-24 16:28:06 +00006140void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006141 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006142 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006143 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006144 return; // Declaration not imported from PCH.
6145
Richard Smith4d235792014-08-07 18:53:08 +00006146 // Implicit function decl from a PCH was defined.
6147 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006148}
6149
Richard Smithd28ac5b2014-03-22 23:33:22 +00006150void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006151 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006152 assert(!WritingAST && "Already writing the AST!");
6153 if (!D->isFromASTFile())
6154 return;
6155
Richard Smith9e2341d2015-03-23 03:25:59 +00006156 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006157}
6158
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006159void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006160 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006161 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006162 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006163 return;
6164
6165 // Since the actual instantiation is delayed, this really means that we need
6166 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006167 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006168 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6169 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006170}
6171
John McCall32791cc2016-01-06 22:34:54 +00006172void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006173 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006174 assert(!WritingAST && "Already writing the AST!");
6175 if (!D->isFromASTFile())
6176 return;
6177
6178 DeclUpdates[D].push_back(
6179 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6180}
6181
Richard Smith4b054b22016-08-24 21:25:37 +00006182void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6183 assert(!WritingAST && "Already writing the AST!");
6184 if (!D->isFromASTFile())
6185 return;
6186
6187 DeclUpdates[D].push_back(
6188 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6189}
6190
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006191void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6192 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006193 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006194 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006195 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006196 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006197
6198 assert(IFD->getDefinition() && "Category on a class without a definition?");
6199 ObjCClassesWithCategories.insert(
6200 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006201}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006202
Eli Friedman276dd182013-09-05 00:02:25 +00006203void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006204 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006205 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006206
6207 // If there is *any* declaration of the entity that's not from an AST file,
6208 // we can skip writing the update record. We make sure that isUsed() triggers
6209 // completion of the redeclaration chain of the entity.
6210 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6211 if (IsLocalDecl(Prev))
6212 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006213
Aaron Ballman4f45b712014-03-21 15:22:56 +00006214 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006215}
Alexey Bataev97720002014-11-11 04:05:39 +00006216
6217void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006218 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006219 assert(!WritingAST && "Already writing the AST!");
6220 if (!D->isFromASTFile())
6221 return;
6222
6223 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6224}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006225
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006226void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6227 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006228 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006229 assert(!WritingAST && "Already writing the AST!");
6230 if (!D->isFromASTFile())
6231 return;
6232
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006233 DeclUpdates[D].push_back(
6234 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006235}
6236
Richard Smith4caa4492015-05-15 02:34:32 +00006237void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006238 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006239 assert(!WritingAST && "Already writing the AST!");
6240 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006241 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006242}
Alex Denisovfde64952015-06-26 05:28:36 +00006243
6244void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6245 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006246 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006247 assert(!WritingAST && "Already writing the AST!");
6248 if (!Record->isFromASTFile())
6249 return;
6250 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6251}
Richard Smithc26d9742016-10-06 20:30:51 +00006252
6253void ASTWriter::AddedCXXTemplateSpecialization(
6254 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6255 assert(!WritingAST && "Already writing the AST!");
6256
6257 if (!TD->getFirstDecl()->isFromASTFile())
6258 return;
6259 if (Chain && Chain->isProcessingUpdateRecords())
6260 return;
6261
6262 DeclsToEmitEvenIfUnreferenced.push_back(D);
6263}
6264
6265void ASTWriter::AddedCXXTemplateSpecialization(
6266 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6267 assert(!WritingAST && "Already writing the AST!");
6268
6269 if (!TD->getFirstDecl()->isFromASTFile())
6270 return;
6271 if (Chain && Chain->isProcessingUpdateRecords())
6272 return;
6273
6274 DeclsToEmitEvenIfUnreferenced.push_back(D);
6275}
6276
6277void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6278 const FunctionDecl *D) {
6279 assert(!WritingAST && "Already writing the AST!");
6280
6281 if (!TD->getFirstDecl()->isFromASTFile())
6282 return;
6283 if (Chain && Chain->isProcessingUpdateRecords())
6284 return;
6285
6286 DeclsToEmitEvenIfUnreferenced.push_back(D);
6287}