blob: 166db2c1af385aa54be39bd123076c77027acf1b [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
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000436void
437ASTTypeWriter::VisitDependentAddressSpaceType(
438 const DependentAddressSpaceType *T) {
439 Record.AddTypeRef(T->getPointeeType());
440 Record.AddStmt(T->getAddrSpaceExpr());
441 Record.AddSourceLocation(T->getAttributeLoc());
442 Code = TYPE_DEPENDENT_ADDRESS_SPACE;
443}
444
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000445void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000446ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000447 Record.push_back(T->getDepth());
448 Record.push_back(T->getIndex());
449 Record.push_back(T->isParameterPack());
Richard Smith69c82bf2016-04-01 22:52:03 +0000450 Record.AddDeclRef(T->getDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000451 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000452}
453
454void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000455ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000456 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000457 Record.AddNestedNameSpecifier(T->getQualifier());
458 Record.AddIdentifierRef(T->getIdentifier());
459 Record.AddTypeRef(
460 T->isCanonicalUnqualified() ? QualType() : T->getCanonicalTypeInternal());
Sebastian Redl539c5062010-08-18 23:57:32 +0000461 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000462}
463
464void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000465ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000466 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000467 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000468 Record.AddNestedNameSpecifier(T->getQualifier());
469 Record.AddIdentifierRef(T->getIdentifier());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000470 Record.push_back(T->getNumArgs());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000471 for (const auto &I : *T)
Richard Smith69c82bf2016-04-01 22:52:03 +0000472 Record.AddTemplateArgument(I);
Sebastian Redl539c5062010-08-18 23:57:32 +0000473 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000474}
475
Douglas Gregord2fa7662010-12-20 02:24:11 +0000476void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000477 Record.AddTypeRef(T->getPattern());
David Blaikie05785d12013-02-20 22:23:23 +0000478 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000479 Record.push_back(*NumExpansions + 1);
480 else
481 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000482 Code = TYPE_PACK_EXPANSION;
483}
484
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000485void ASTTypeWriter::VisitParenType(const ParenType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000486 Record.AddTypeRef(T->getInnerType());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000487 Code = TYPE_PAREN;
488}
489
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000490void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000491 Record.push_back(T->getKeyword());
Richard Smith69c82bf2016-04-01 22:52:03 +0000492 Record.AddNestedNameSpecifier(T->getQualifier());
493 Record.AddTypeRef(T->getNamedType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000494 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000495}
496
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000497void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000498 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
499 Record.AddTypeRef(T->getInjectedSpecializationType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000500 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000501}
502
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000503void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000504 Record.AddDeclRef(T->getDecl()->getCanonicalDecl());
Sebastian Redl539c5062010-08-18 23:57:32 +0000505 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000506}
507
Manman Rene6be26c2016-09-13 17:25:08 +0000508void ASTTypeWriter::VisitObjCTypeParamType(const ObjCTypeParamType *T) {
509 Record.AddDeclRef(T->getDecl());
510 Record.push_back(T->getNumProtocols());
511 for (const auto *I : T->quals())
512 Record.AddDeclRef(I);
513 Code = TYPE_OBJC_TYPE_PARAM;
514}
515
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000516void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000517 Record.AddTypeRef(T->getBaseType());
Douglas Gregore83b9562015-07-07 03:57:53 +0000518 Record.push_back(T->getTypeArgsAsWritten().size());
519 for (auto TypeArg : T->getTypeArgsAsWritten())
Richard Smith69c82bf2016-04-01 22:52:03 +0000520 Record.AddTypeRef(TypeArg);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000521 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000522 for (const auto *I : T->quals())
Richard Smith69c82bf2016-04-01 22:52:03 +0000523 Record.AddDeclRef(I);
Douglas Gregorab209d82015-07-07 03:58:42 +0000524 Record.push_back(T->isKindOfTypeAsWritten());
Sebastian Redl539c5062010-08-18 23:57:32 +0000525 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000526}
527
Steve Narofffb4330f2009-06-17 22:40:22 +0000528void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000529ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000530 Record.AddTypeRef(T->getPointeeType());
Sebastian Redl539c5062010-08-18 23:57:32 +0000531 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000532}
533
Eli Friedman0dfb8892011-10-06 23:00:33 +0000534void
535ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000536 Record.AddTypeRef(T->getValueType());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000537 Code = TYPE_ATOMIC;
538}
539
Xiuli Pan9c14e282016-01-09 12:53:17 +0000540void
541ASTTypeWriter::VisitPipeType(const PipeType *T) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000542 Record.AddTypeRef(T->getElementType());
Joey Goulye3c85de2016-12-01 11:30:49 +0000543 Record.push_back(T->isReadOnly());
544 Code = TYPE_PIPE;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000545}
546
John McCall8f115c62009-10-16 21:56:05 +0000547namespace {
548
549class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Richard Smith290d8012016-04-06 17:06:00 +0000550 ASTRecordWriter &Record;
John McCall8f115c62009-10-16 21:56:05 +0000551
552public:
Richard Smith290d8012016-04-06 17:06:00 +0000553 TypeLocWriter(ASTRecordWriter &Record)
554 : Record(Record) { }
John McCall8f115c62009-10-16 21:56:05 +0000555
John McCall17001972009-10-18 01:05:36 +0000556#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000557#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000558 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000559#include "clang/AST/TypeLocNodes.def"
560
John McCall17001972009-10-18 01:05:36 +0000561 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
562 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000563};
564
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +0000565} // end anonymous namespace
John McCall8f115c62009-10-16 21:56:05 +0000566
John McCall17001972009-10-18 01:05:36 +0000567void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
568 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000569}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000570
John McCall17001972009-10-18 01:05:36 +0000571void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000572 Record.AddSourceLocation(TL.getBuiltinLoc());
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000573 if (TL.needsExtraLocalData()) {
574 Record.push_back(TL.getWrittenTypeSpec());
575 Record.push_back(TL.getWrittenSignSpec());
576 Record.push_back(TL.getWrittenWidthSpec());
577 Record.push_back(TL.hasModeAttr());
578 }
John McCall8f115c62009-10-16 21:56:05 +0000579}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000580
John McCall17001972009-10-18 01:05:36 +0000581void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000582 Record.AddSourceLocation(TL.getNameLoc());
John McCall8f115c62009-10-16 21:56:05 +0000583}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000584
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000586 Record.AddSourceLocation(TL.getStarLoc());
John McCall8f115c62009-10-16 21:56:05 +0000587}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000588
Reid Kleckner8a365022013-06-24 17:51:48 +0000589void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
590 // nothing to do
591}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000592
Reid Kleckner0503a872013-12-05 01:23:43 +0000593void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
594 // nothing to do
595}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000596
John McCall17001972009-10-18 01:05:36 +0000597void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000598 Record.AddSourceLocation(TL.getCaretLoc());
John McCall8f115c62009-10-16 21:56:05 +0000599}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000600
John McCall17001972009-10-18 01:05:36 +0000601void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000602 Record.AddSourceLocation(TL.getAmpLoc());
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::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000606 Record.AddSourceLocation(TL.getAmpAmpLoc());
John McCall8f115c62009-10-16 21:56:05 +0000607}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000608
John McCall17001972009-10-18 01:05:36 +0000609void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000610 Record.AddSourceLocation(TL.getStarLoc());
611 Record.AddTypeSourceInfo(TL.getClassTInfo());
John McCall8f115c62009-10-16 21:56:05 +0000612}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000613
John McCall17001972009-10-18 01:05:36 +0000614void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000615 Record.AddSourceLocation(TL.getLBracketLoc());
616 Record.AddSourceLocation(TL.getRBracketLoc());
John McCall17001972009-10-18 01:05:36 +0000617 Record.push_back(TL.getSizeExpr() ? 1 : 0);
618 if (TL.getSizeExpr())
Richard Smith290d8012016-04-06 17:06:00 +0000619 Record.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000620}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000621
John McCall17001972009-10-18 01:05:36 +0000622void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
623 VisitArrayTypeLoc(TL);
624}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000625
John McCall17001972009-10-18 01:05:36 +0000626void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
627 VisitArrayTypeLoc(TL);
628}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000629
John McCall17001972009-10-18 01:05:36 +0000630void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
631 VisitArrayTypeLoc(TL);
632}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000633
John McCall17001972009-10-18 01:05:36 +0000634void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
635 DependentSizedArrayTypeLoc TL) {
636 VisitArrayTypeLoc(TL);
637}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000638
Andrew Gozillon572bbb02017-10-02 06:25:51 +0000639void TypeLocWriter::VisitDependentAddressSpaceTypeLoc(
640 DependentAddressSpaceTypeLoc TL) {
641 Record.AddSourceLocation(TL.getAttrNameLoc());
642 SourceRange range = TL.getAttrOperandParensRange();
643 Record.AddSourceLocation(range.getBegin());
644 Record.AddSourceLocation(range.getEnd());
645 Record.AddStmt(TL.getAttrExprOperand());
646}
647
John McCall17001972009-10-18 01:05:36 +0000648void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
649 DependentSizedExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000650 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000651}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000652
John McCall17001972009-10-18 01:05:36 +0000653void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000654 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000655}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000656
John McCall17001972009-10-18 01:05:36 +0000657void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000658 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000659}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000660
John McCall17001972009-10-18 01:05:36 +0000661void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000662 Record.AddSourceLocation(TL.getLocalRangeBegin());
663 Record.AddSourceLocation(TL.getLParenLoc());
664 Record.AddSourceLocation(TL.getRParenLoc());
Malcolm Parsonsa3220ce2017-01-12 16:11:28 +0000665 Record.AddSourceRange(TL.getExceptionSpecRange());
Richard Smith69c82bf2016-04-01 22:52:03 +0000666 Record.AddSourceLocation(TL.getLocalRangeEnd());
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000667 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000668 Record.AddDeclRef(TL.getParam(i));
John McCall17001972009-10-18 01:05:36 +0000669}
670void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
671 VisitFunctionTypeLoc(TL);
672}
673void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
674 VisitFunctionTypeLoc(TL);
675}
John McCallb96ec562009-12-04 22:46:56 +0000676void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000677 Record.AddSourceLocation(TL.getNameLoc());
John McCallb96ec562009-12-04 22:46:56 +0000678}
John McCall17001972009-10-18 01:05:36 +0000679void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000680 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000681}
Manman Rene6be26c2016-09-13 17:25:08 +0000682void TypeLocWriter::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
683 if (TL.getNumProtocols()) {
684 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
685 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
686 }
687 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
688 Record.AddSourceLocation(TL.getProtocolLoc(i));
689}
John McCall17001972009-10-18 01:05:36 +0000690void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000691 Record.AddSourceLocation(TL.getTypeofLoc());
692 Record.AddSourceLocation(TL.getLParenLoc());
693 Record.AddSourceLocation(TL.getRParenLoc());
John McCall17001972009-10-18 01:05:36 +0000694}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000695
John McCall17001972009-10-18 01:05:36 +0000696void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000697 Record.AddSourceLocation(TL.getTypeofLoc());
698 Record.AddSourceLocation(TL.getLParenLoc());
699 Record.AddSourceLocation(TL.getRParenLoc());
700 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
John McCall17001972009-10-18 01:05:36 +0000701}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000702
John McCall17001972009-10-18 01:05:36 +0000703void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000704 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000705}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000706
Alexis Hunte852b102011-05-24 22:41:36 +0000707void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000708 Record.AddSourceLocation(TL.getKWLoc());
709 Record.AddSourceLocation(TL.getLParenLoc());
710 Record.AddSourceLocation(TL.getRParenLoc());
711 Record.AddTypeSourceInfo(TL.getUnderlyingTInfo());
Alexis Hunte852b102011-05-24 22:41:36 +0000712}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000713
Richard Smith30482bc2011-02-20 03:19:35 +0000714void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000715 Record.AddSourceLocation(TL.getNameLoc());
Richard Smith30482bc2011-02-20 03:19:35 +0000716}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000717
Richard Smith600b5262017-01-26 20:40:47 +0000718void TypeLocWriter::VisitDeducedTemplateSpecializationTypeLoc(
719 DeducedTemplateSpecializationTypeLoc TL) {
720 Record.AddSourceLocation(TL.getTemplateNameLoc());
721}
722
John McCall17001972009-10-18 01:05:36 +0000723void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000724 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000725}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000726
John McCall17001972009-10-18 01:05:36 +0000727void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000728 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000729}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000730
John McCall81904512011-01-06 01:58:22 +0000731void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000732 Record.AddSourceLocation(TL.getAttrNameLoc());
John McCall81904512011-01-06 01:58:22 +0000733 if (TL.hasAttrOperand()) {
734 SourceRange range = TL.getAttrOperandParensRange();
Richard Smith69c82bf2016-04-01 22:52:03 +0000735 Record.AddSourceLocation(range.getBegin());
736 Record.AddSourceLocation(range.getEnd());
John McCall81904512011-01-06 01:58:22 +0000737 }
738 if (TL.hasAttrExprOperand()) {
739 Expr *operand = TL.getAttrExprOperand();
740 Record.push_back(operand ? 1 : 0);
Richard Smith290d8012016-04-06 17:06:00 +0000741 if (operand) Record.AddStmt(operand);
John McCall81904512011-01-06 01:58:22 +0000742 } else if (TL.hasAttrEnumOperand()) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000743 Record.AddSourceLocation(TL.getAttrEnumOperandLoc());
John McCall81904512011-01-06 01:58:22 +0000744 }
745}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000746
John McCall17001972009-10-18 01:05:36 +0000747void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000748 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000749}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000750
John McCallcebee162009-10-18 09:09:24 +0000751void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
752 SubstTemplateTypeParmTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000753 Record.AddSourceLocation(TL.getNameLoc());
John McCallcebee162009-10-18 09:09:24 +0000754}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000755
Douglas Gregorada4b792011-01-14 02:55:32 +0000756void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
757 SubstTemplateTypeParmPackTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000758 Record.AddSourceLocation(TL.getNameLoc());
Douglas Gregorada4b792011-01-14 02:55:32 +0000759}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000760
John McCall17001972009-10-18 01:05:36 +0000761void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
762 TemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000763 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
764 Record.AddSourceLocation(TL.getTemplateNameLoc());
765 Record.AddSourceLocation(TL.getLAngleLoc());
766 Record.AddSourceLocation(TL.getRAngleLoc());
John McCall0ad16662009-10-29 08:12:44 +0000767 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000768 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
769 TL.getArgLoc(i).getLocInfo());
John McCall17001972009-10-18 01:05:36 +0000770}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000771
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000772void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000773 Record.AddSourceLocation(TL.getLParenLoc());
774 Record.AddSourceLocation(TL.getRParenLoc());
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000775}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000776
Abramo Bagnara6150c882010-05-11 21:36:43 +0000777void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000778 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
779 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
John McCall17001972009-10-18 01:05:36 +0000780}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000781
John McCalle78aac42010-03-10 03:28:59 +0000782void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000783 Record.AddSourceLocation(TL.getNameLoc());
John McCalle78aac42010-03-10 03:28:59 +0000784}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000785
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000786void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000787 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
788 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
789 Record.AddSourceLocation(TL.getNameLoc());
John McCall17001972009-10-18 01:05:36 +0000790}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000791
John McCallc392f372010-06-11 00:33:02 +0000792void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
793 DependentTemplateSpecializationTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000794 Record.AddSourceLocation(TL.getElaboratedKeywordLoc());
795 Record.AddNestedNameSpecifierLoc(TL.getQualifierLoc());
796 Record.AddSourceLocation(TL.getTemplateKeywordLoc());
797 Record.AddSourceLocation(TL.getTemplateNameLoc());
798 Record.AddSourceLocation(TL.getLAngleLoc());
799 Record.AddSourceLocation(TL.getRAngleLoc());
John McCallc392f372010-06-11 00:33:02 +0000800 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Richard Smith69c82bf2016-04-01 22:52:03 +0000801 Record.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
802 TL.getArgLoc(I).getLocInfo());
John McCallc392f372010-06-11 00:33:02 +0000803}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000804
Douglas Gregord2fa7662010-12-20 02:24:11 +0000805void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000806 Record.AddSourceLocation(TL.getEllipsisLoc());
Douglas Gregord2fa7662010-12-20 02:24:11 +0000807}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000808
John McCall17001972009-10-18 01:05:36 +0000809void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000810 Record.AddSourceLocation(TL.getNameLoc());
John McCall8b07ec22010-05-15 11:32:37 +0000811}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000812
John McCall8b07ec22010-05-15 11:32:37 +0000813void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
814 Record.push_back(TL.hasBaseTypeAsWritten());
Richard Smith69c82bf2016-04-01 22:52:03 +0000815 Record.AddSourceLocation(TL.getTypeArgsLAngleLoc());
816 Record.AddSourceLocation(TL.getTypeArgsRAngleLoc());
Douglas Gregore9d95f12015-07-07 03:57:35 +0000817 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000818 Record.AddTypeSourceInfo(TL.getTypeArgTInfo(i));
819 Record.AddSourceLocation(TL.getProtocolLAngleLoc());
820 Record.AddSourceLocation(TL.getProtocolRAngleLoc());
John McCall17001972009-10-18 01:05:36 +0000821 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
Richard Smith69c82bf2016-04-01 22:52:03 +0000822 Record.AddSourceLocation(TL.getProtocolLoc(i));
John McCall8f115c62009-10-16 21:56:05 +0000823}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000824
John McCallfc93cf92009-10-22 22:37:11 +0000825void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000826 Record.AddSourceLocation(TL.getStarLoc());
John McCallfc93cf92009-10-22 22:37:11 +0000827}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000828
Eli Friedman0dfb8892011-10-06 23:00:33 +0000829void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000830 Record.AddSourceLocation(TL.getKWLoc());
831 Record.AddSourceLocation(TL.getLParenLoc());
832 Record.AddSourceLocation(TL.getRParenLoc());
Eli Friedman0dfb8892011-10-06 23:00:33 +0000833}
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +0000834
Xiuli Pan9c14e282016-01-09 12:53:17 +0000835void TypeLocWriter::VisitPipeTypeLoc(PipeTypeLoc TL) {
Richard Smith69c82bf2016-04-01 22:52:03 +0000836 Record.AddSourceLocation(TL.getKWLoc());
Xiuli Pan9c14e282016-01-09 12:53:17 +0000837}
John McCall8f115c62009-10-16 21:56:05 +0000838
Richard Smith01b2cb42014-07-26 06:37:51 +0000839void ASTWriter::WriteTypeAbbrevs() {
840 using namespace llvm;
841
David Blaikieb44f0bf2017-01-04 22:36:43 +0000842 std::shared_ptr<BitCodeAbbrev> Abv;
Richard Smith01b2cb42014-07-26 06:37:51 +0000843
844 // Abbreviation for TYPE_EXT_QUAL
David Blaikieb44f0bf2017-01-04 22:36:43 +0000845 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000846 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
847 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
848 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
David Blaikieb44f0bf2017-01-04 22:36:43 +0000849 TypeExtQualAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000850
851 // Abbreviation for TYPE_FUNCTION_PROTO
David Blaikieb44f0bf2017-01-04 22:36:43 +0000852 Abv = std::make_shared<BitCodeAbbrev>();
Richard Smith01b2cb42014-07-26 06:37:51 +0000853 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
854 // FunctionType
855 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
856 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
857 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
858 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
859 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
860 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
Oren Ben Simhon318a6ea2017-04-27 12:01:00 +0000861 Abv->Add(BitCodeAbbrevOp(0)); // NoCallerSavedRegs
Richard Smith01b2cb42014-07-26 06:37:51 +0000862 // FunctionProtoType
863 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
864 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
865 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
866 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
867 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
868 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
869 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
870 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
David Blaikieb44f0bf2017-01-04 22:36:43 +0000871 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(std::move(Abv));
Richard Smith01b2cb42014-07-26 06:37:51 +0000872}
873
Chris Lattner19cea4e2009-04-22 05:57:30 +0000874//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000875// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000876//===----------------------------------------------------------------------===//
877
Chris Lattner28fa4e62009-04-26 22:26:21 +0000878static void EmitBlockID(unsigned ID, const char *Name,
879 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000880 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000881 Record.clear();
882 Record.push_back(ID);
883 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
884
885 // Emit the block name if present.
Craig Toppera13603a2014-05-22 05:54:18 +0000886 if (!Name || Name[0] == 0)
887 return;
Chris Lattner28fa4e62009-04-26 22:26:21 +0000888 Record.clear();
889 while (*Name)
890 Record.push_back(*Name++);
891 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
892}
893
894static void EmitRecordID(unsigned ID, const char *Name,
895 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000896 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000897 Record.clear();
898 Record.push_back(ID);
899 while (*Name)
900 Record.push_back(*Name++);
901 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000902}
903
904static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000905 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000906#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000907 RECORD(STMT_STOP);
908 RECORD(STMT_NULL_PTR);
Richard Smith01b2cb42014-07-26 06:37:51 +0000909 RECORD(STMT_REF_PTR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000910 RECORD(STMT_NULL);
911 RECORD(STMT_COMPOUND);
912 RECORD(STMT_CASE);
913 RECORD(STMT_DEFAULT);
914 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000915 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000916 RECORD(STMT_IF);
917 RECORD(STMT_SWITCH);
918 RECORD(STMT_WHILE);
919 RECORD(STMT_DO);
920 RECORD(STMT_FOR);
921 RECORD(STMT_GOTO);
922 RECORD(STMT_INDIRECT_GOTO);
923 RECORD(STMT_CONTINUE);
924 RECORD(STMT_BREAK);
925 RECORD(STMT_RETURN);
926 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000927 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000928 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000929 RECORD(EXPR_PREDEFINED);
930 RECORD(EXPR_DECL_REF);
931 RECORD(EXPR_INTEGER_LITERAL);
932 RECORD(EXPR_FLOATING_LITERAL);
933 RECORD(EXPR_IMAGINARY_LITERAL);
934 RECORD(EXPR_STRING_LITERAL);
935 RECORD(EXPR_CHARACTER_LITERAL);
936 RECORD(EXPR_PAREN);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000937 RECORD(EXPR_PAREN_LIST);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000938 RECORD(EXPR_UNARY_OPERATOR);
939 RECORD(EXPR_SIZEOF_ALIGN_OF);
940 RECORD(EXPR_ARRAY_SUBSCRIPT);
941 RECORD(EXPR_CALL);
942 RECORD(EXPR_MEMBER);
943 RECORD(EXPR_BINARY_OPERATOR);
944 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
945 RECORD(EXPR_CONDITIONAL_OPERATOR);
946 RECORD(EXPR_IMPLICIT_CAST);
947 RECORD(EXPR_CSTYLE_CAST);
948 RECORD(EXPR_COMPOUND_LITERAL);
949 RECORD(EXPR_EXT_VECTOR_ELEMENT);
950 RECORD(EXPR_INIT_LIST);
951 RECORD(EXPR_DESIGNATED_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000952 RECORD(EXPR_DESIGNATED_INIT_UPDATE);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000953 RECORD(EXPR_IMPLICIT_VALUE_INIT);
Yunzhong Gaocb779302015-06-10 00:27:52 +0000954 RECORD(EXPR_NO_INIT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000955 RECORD(EXPR_VA_ARG);
956 RECORD(EXPR_ADDR_LABEL);
957 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000958 RECORD(EXPR_CHOOSE);
959 RECORD(EXPR_GNU_NULL);
960 RECORD(EXPR_SHUFFLE_VECTOR);
961 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000962 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000963 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000964 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000965 RECORD(EXPR_OBJC_ARRAY_LITERAL);
966 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000967 RECORD(EXPR_OBJC_ENCODE);
968 RECORD(EXPR_OBJC_SELECTOR_EXPR);
969 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
970 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
971 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
972 RECORD(EXPR_OBJC_KVC_REF_EXPR);
973 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000974 RECORD(STMT_OBJC_FOR_COLLECTION);
975 RECORD(STMT_OBJC_CATCH);
976 RECORD(STMT_OBJC_FINALLY);
977 RECORD(STMT_OBJC_AT_TRY);
978 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
979 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000980 RECORD(EXPR_OBJC_BOOL_LITERAL);
Richard Smith01b2cb42014-07-26 06:37:51 +0000981 RECORD(STMT_CXX_CATCH);
982 RECORD(STMT_CXX_TRY);
983 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000984 RECORD(EXPR_CXX_OPERATOR_CALL);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000985 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000986 RECORD(EXPR_CXX_CONSTRUCT);
Richard Smithf1b4b8b2014-07-27 04:29:04 +0000987 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000988 RECORD(EXPR_CXX_STATIC_CAST);
989 RECORD(EXPR_CXX_DYNAMIC_CAST);
990 RECORD(EXPR_CXX_REINTERPRET_CAST);
991 RECORD(EXPR_CXX_CONST_CAST);
992 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000993 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000994 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000995 RECORD(EXPR_CXX_BOOL_LITERAL);
996 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000997 RECORD(EXPR_CXX_TYPEID_EXPR);
998 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000999 RECORD(EXPR_CXX_THIS);
1000 RECORD(EXPR_CXX_THROW);
1001 RECORD(EXPR_CXX_DEFAULT_ARG);
Richard Smith01b2cb42014-07-26 06:37:51 +00001002 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001003 RECORD(EXPR_CXX_BIND_TEMPORARY);
1004 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
1005 RECORD(EXPR_CXX_NEW);
1006 RECORD(EXPR_CXX_DELETE);
1007 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
1008 RECORD(EXPR_EXPR_WITH_CLEANUPS);
1009 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
1010 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
1011 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
1012 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
1013 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Richard Smith01b2cb42014-07-26 06:37:51 +00001014 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001015 RECORD(EXPR_CXX_NOEXCEPT);
1016 RECORD(EXPR_OPAQUE_VALUE);
Richard Smith01b2cb42014-07-26 06:37:51 +00001017 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
1018 RECORD(EXPR_TYPE_TRAIT);
1019 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001020 RECORD(EXPR_PACK_EXPANSION);
1021 RECORD(EXPR_SIZEOF_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001022 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001023 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smith01b2cb42014-07-26 06:37:51 +00001024 RECORD(EXPR_FUNCTION_PARM_PACK);
1025 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbourne41f85462011-02-09 21:07:24 +00001026 RECORD(EXPR_CUDA_KERNEL_CALL);
Richard Smith01b2cb42014-07-26 06:37:51 +00001027 RECORD(EXPR_CXX_UUIDOF_EXPR);
1028 RECORD(EXPR_CXX_UUIDOF_TYPE);
1029 RECORD(EXPR_LAMBDA);
Chris Lattnerccac3a62009-04-27 00:49:53 +00001030#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +00001031}
Mike Stump11289f42009-09-09 15:08:12 +00001032
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001033void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +00001034 RecordData Record;
Peter Collingbourned3a6c702016-11-01 01:18:57 +00001035 Stream.EnterBlockInfoBlock();
Mike Stump11289f42009-09-09 15:08:12 +00001036
Sebastian Redl539c5062010-08-18 23:57:32 +00001037#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
1038#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +00001039
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001040 // Control Block.
1041 BLOCK(CONTROL_BLOCK);
1042 RECORD(METADATA);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001043 RECORD(MODULE_NAME);
Richard Smithfa715652015-08-31 22:43:10 +00001044 RECORD(MODULE_DIRECTORY);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001045 RECORD(MODULE_MAP_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001046 RECORD(IMPORTS);
Douglas Gregorfad10d82012-10-18 18:36:53 +00001047 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001048 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001049 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001050 RECORD(INPUT_FILE_OFFSETS);
Richard Smith0516b182015-09-08 19:40:14 +00001051
1052 BLOCK(OPTIONS_BLOCK);
1053 RECORD(LANGUAGE_OPTIONS);
1054 RECORD(TARGET_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +00001055 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +00001056 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001057 RECORD(PREPROCESSOR_OPTIONS);
1058
Douglas Gregor108cb222012-10-19 00:45:00 +00001059 BLOCK(INPUT_FILES_BLOCK);
1060 RECORD(INPUT_FILE);
1061
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001062 // AST Top-Level Block.
1063 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001064 RECORD(TYPE_OFFSET);
1065 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001066 RECORD(IDENTIFIER_OFFSET);
1067 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +00001068 RECORD(EAGERLY_DESERIALIZED_DECLS);
David Blaikie9ffe5a32017-01-30 05:00:26 +00001069 RECORD(MODULAR_CODEGEN_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001070 RECORD(SPECIAL_TYPES);
1071 RECORD(STATISTICS);
1072 RECORD(TENTATIVE_DEFINITIONS);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001073 RECORD(SELECTOR_OFFSETS);
1074 RECORD(METHOD_POOL);
1075 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +00001076 RECORD(SOURCE_LOCATION_OFFSETS);
1077 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00001078 RECORD(EXT_VECTOR_DECLS);
Richard Smithfa715652015-08-31 22:43:10 +00001079 RECORD(UNUSED_FILESCOPED_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001080 RECORD(PPD_ENTITIES_OFFSETS);
Richard Smithfa715652015-08-31 22:43:10 +00001081 RECORD(VTABLE_USES);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001082 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001083 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001084 RECORD(SEMA_DECL_REFS);
1085 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
1086 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001087 RECORD(UPDATE_VISIBLE);
1088 RECORD(DECL_UPDATE_OFFSETS);
1089 RECORD(DECL_UPDATES);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001090 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001091 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001092 RECORD(FP_PRAGMA_OPTIONS);
1093 RECORD(OPENCL_EXTENSIONS);
Yaxun Liu5b746652016-12-18 05:18:55 +00001094 RECORD(OPENCL_EXTENSION_TYPES);
1095 RECORD(OPENCL_EXTENSION_DECLS);
Alexis Hunt27a761d2011-05-04 23:29:54 +00001096 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00001097 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +00001098 RECORD(MODULE_OFFSET_MAP);
1099 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001100 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +00001101 RECORD(FILE_SORTED_DECLS);
1102 RECORD(IMPORTED_MODULES);
Douglas Gregor404cdde2012-01-27 01:47:08 +00001103 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001104 RECORD(MACRO_OFFSET);
Richard Smithfa715652015-08-31 22:43:10 +00001105 RECORD(INTERESTING_IDENTIFIERS);
1106 RECORD(UNDEFINED_BUT_USED);
Richard Smithe40f2ba2013-08-07 21:41:30 +00001107 RECORD(LATE_PARSED_TEMPLATE);
Dario Domizioli13a0a382014-05-23 12:13:25 +00001108 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Nico Weber779355f2016-03-02 23:22:00 +00001109 RECORD(MSSTRUCT_PRAGMA_OPTIONS);
Nico Weber42932312016-03-03 00:17:35 +00001110 RECORD(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS);
Richard Smithfa715652015-08-31 22:43:10 +00001111 RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
Richard Smithfa715652015-08-31 22:43:10 +00001112 RECORD(DELETE_EXPRS_TO_ANALYZE);
Justin Lebar67a78a62016-10-08 22:15:58 +00001113 RECORD(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH);
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00001114 RECORD(PP_CONDITIONAL_STACK);
Douglas Gregor358cd442012-01-15 16:58:34 +00001115
Chris Lattner28fa4e62009-04-26 22:26:21 +00001116 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +00001117 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001118 RECORD(SM_SLOC_FILE_ENTRY);
1119 RECORD(SM_SLOC_BUFFER_ENTRY);
1120 RECORD(SM_SLOC_BUFFER_BLOB);
Richard Smithaada85c2016-02-06 02:06:43 +00001121 RECORD(SM_SLOC_BUFFER_BLOB_COMPRESSED);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001122 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +00001123
Chris Lattner28fa4e62009-04-26 22:26:21 +00001124 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +00001125 BLOCK(PREPROCESSOR_BLOCK);
Richard Smithec216502015-02-13 19:48:37 +00001126 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001127 RECORD(PP_MACRO_FUNCTION_LIKE);
Richard Smithd7329392015-04-21 21:46:32 +00001128 RECORD(PP_MACRO_OBJECT_LIKE);
1129 RECORD(PP_MODULE_MACRO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001130 RECORD(PP_TOKEN);
Richard Smithec216502015-02-13 19:48:37 +00001131
Richard Smithfa715652015-08-31 22:43:10 +00001132 // Submodule Block.
1133 BLOCK(SUBMODULE_BLOCK);
1134 RECORD(SUBMODULE_METADATA);
1135 RECORD(SUBMODULE_DEFINITION);
1136 RECORD(SUBMODULE_UMBRELLA_HEADER);
1137 RECORD(SUBMODULE_HEADER);
1138 RECORD(SUBMODULE_TOPHEADER);
1139 RECORD(SUBMODULE_UMBRELLA_DIR);
1140 RECORD(SUBMODULE_IMPORTS);
1141 RECORD(SUBMODULE_EXPORTS);
1142 RECORD(SUBMODULE_REQUIRES);
1143 RECORD(SUBMODULE_EXCLUDED_HEADER);
1144 RECORD(SUBMODULE_LINK_LIBRARY);
1145 RECORD(SUBMODULE_CONFIG_MACRO);
1146 RECORD(SUBMODULE_CONFLICT);
1147 RECORD(SUBMODULE_PRIVATE_HEADER);
1148 RECORD(SUBMODULE_TEXTUAL_HEADER);
1149 RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
Richard Smithdc1f0422016-07-20 19:10:16 +00001150 RECORD(SUBMODULE_INITIALIZERS);
Douglas Gregorf0b11de2017-09-14 23:38:44 +00001151 RECORD(SUBMODULE_EXPORT_AS);
Richard Smithfa715652015-08-31 22:43:10 +00001152
1153 // Comments Block.
1154 BLOCK(COMMENTS_BLOCK);
1155 RECORD(COMMENTS_RAW_COMMENT);
1156
Douglas Gregor12bfa382009-10-17 00:13:19 +00001157 // Decls and Types block.
1158 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001159 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001160 RECORD(TYPE_COMPLEX);
1161 RECORD(TYPE_POINTER);
1162 RECORD(TYPE_BLOCK_POINTER);
1163 RECORD(TYPE_LVALUE_REFERENCE);
1164 RECORD(TYPE_RVALUE_REFERENCE);
1165 RECORD(TYPE_MEMBER_POINTER);
1166 RECORD(TYPE_CONSTANT_ARRAY);
1167 RECORD(TYPE_INCOMPLETE_ARRAY);
1168 RECORD(TYPE_VARIABLE_ARRAY);
1169 RECORD(TYPE_VECTOR);
1170 RECORD(TYPE_EXT_VECTOR);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001171 RECORD(TYPE_FUNCTION_NO_PROTO);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001172 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001173 RECORD(TYPE_TYPEDEF);
1174 RECORD(TYPE_TYPEOF_EXPR);
1175 RECORD(TYPE_TYPEOF);
1176 RECORD(TYPE_RECORD);
1177 RECORD(TYPE_ENUM);
1178 RECORD(TYPE_OBJC_INTERFACE);
Steve Narofffb4330f2009-06-17 22:40:22 +00001179 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001180 RECORD(TYPE_DECLTYPE);
1181 RECORD(TYPE_ELABORATED);
1182 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1183 RECORD(TYPE_UNRESOLVED_USING);
1184 RECORD(TYPE_INJECTED_CLASS_NAME);
1185 RECORD(TYPE_OBJC_OBJECT);
1186 RECORD(TYPE_TEMPLATE_TYPE_PARM);
1187 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1188 RECORD(TYPE_DEPENDENT_NAME);
1189 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1190 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1191 RECORD(TYPE_PAREN);
1192 RECORD(TYPE_PACK_EXPANSION);
1193 RECORD(TYPE_ATTRIBUTED);
1194 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001195 RECORD(TYPE_AUTO);
1196 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedman0dfb8892011-10-06 23:00:33 +00001197 RECORD(TYPE_ATOMIC);
Richard Smithf1b4b8b2014-07-27 04:29:04 +00001198 RECORD(TYPE_DECAYED);
1199 RECORD(TYPE_ADJUSTED);
Manman Rene6be26c2016-09-13 17:25:08 +00001200 RECORD(TYPE_OBJC_TYPE_PARAM);
Richard Smithd61d4ac2015-08-22 20:13:39 +00001201 RECORD(LOCAL_REDECLARATIONS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001202 RECORD(DECL_TYPEDEF);
Richard Smith01b2cb42014-07-26 06:37:51 +00001203 RECORD(DECL_TYPEALIAS);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001204 RECORD(DECL_ENUM);
1205 RECORD(DECL_RECORD);
1206 RECORD(DECL_ENUM_CONSTANT);
1207 RECORD(DECL_FUNCTION);
1208 RECORD(DECL_OBJC_METHOD);
1209 RECORD(DECL_OBJC_INTERFACE);
1210 RECORD(DECL_OBJC_PROTOCOL);
1211 RECORD(DECL_OBJC_IVAR);
1212 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001213 RECORD(DECL_OBJC_CATEGORY);
1214 RECORD(DECL_OBJC_CATEGORY_IMPL);
1215 RECORD(DECL_OBJC_IMPLEMENTATION);
1216 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1217 RECORD(DECL_OBJC_PROPERTY);
1218 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001219 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +00001220 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001221 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001222 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +00001223 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +00001224 RECORD(DECL_FILE_SCOPE_ASM);
1225 RECORD(DECL_BLOCK);
1226 RECORD(DECL_CONTEXT_LEXICAL);
1227 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001228 RECORD(DECL_NAMESPACE);
1229 RECORD(DECL_NAMESPACE_ALIAS);
1230 RECORD(DECL_USING);
1231 RECORD(DECL_USING_SHADOW);
1232 RECORD(DECL_USING_DIRECTIVE);
1233 RECORD(DECL_UNRESOLVED_USING_VALUE);
1234 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1235 RECORD(DECL_LINKAGE_SPEC);
1236 RECORD(DECL_CXX_RECORD);
1237 RECORD(DECL_CXX_METHOD);
1238 RECORD(DECL_CXX_CONSTRUCTOR);
Richard Smith5179eb72016-06-28 19:03:57 +00001239 RECORD(DECL_CXX_INHERITED_CONSTRUCTOR);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001240 RECORD(DECL_CXX_DESTRUCTOR);
1241 RECORD(DECL_CXX_CONVERSION);
1242 RECORD(DECL_ACCESS_SPEC);
1243 RECORD(DECL_FRIEND);
1244 RECORD(DECL_FRIEND_TEMPLATE);
1245 RECORD(DECL_CLASS_TEMPLATE);
1246 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1247 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +00001248 RECORD(DECL_VAR_TEMPLATE);
1249 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1250 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001251 RECORD(DECL_FUNCTION_TEMPLATE);
1252 RECORD(DECL_TEMPLATE_TYPE_PARM);
1253 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1254 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
Richard Smithefc884a2016-04-13 07:41:35 +00001255 RECORD(DECL_TYPE_ALIAS_TEMPLATE);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001256 RECORD(DECL_STATIC_ASSERT);
1257 RECORD(DECL_CXX_BASE_SPECIFIERS);
Richard Smithefc884a2016-04-13 07:41:35 +00001258 RECORD(DECL_CXX_CTOR_INITIALIZERS);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001259 RECORD(DECL_INDIRECTFIELD);
1260 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
Richard Smithefc884a2016-04-13 07:41:35 +00001261 RECORD(DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK);
1262 RECORD(DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION);
1263 RECORD(DECL_IMPORT);
1264 RECORD(DECL_OMP_THREADPRIVATE);
1265 RECORD(DECL_EMPTY);
1266 RECORD(DECL_OBJC_TYPE_PARAM);
1267 RECORD(DECL_OMP_CAPTUREDEXPR);
1268 RECORD(DECL_PRAGMA_COMMENT);
1269 RECORD(DECL_PRAGMA_DETECT_MISMATCH);
1270 RECORD(DECL_OMP_DECLARE_REDUCTION);
Douglas Gregor0beaec02011-02-08 16:34:17 +00001271
Douglas Gregor03412ba2011-06-03 02:27:19 +00001272 // Statements and Exprs can occur in the Decls and Types block.
1273 AddStmtsExprs(Stream, Record);
1274
Douglas Gregor92a96f52011-02-08 21:58:10 +00001275 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001276 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001277 RECORD(PPD_MACRO_DEFINITION);
1278 RECORD(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00001279
1280 // Decls and Types block.
1281 BLOCK(EXTENSION_BLOCK);
1282 RECORD(EXTENSION_METADATA);
1283
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001284 BLOCK(UNHASHED_CONTROL_BLOCK);
1285 RECORD(SIGNATURE);
1286 RECORD(DIAGNOSTIC_OPTIONS);
1287 RECORD(DIAG_PRAGMA_MAPPINGS);
1288
Chris Lattner28fa4e62009-04-26 22:26:21 +00001289#undef RECORD
1290#undef BLOCK
1291 Stream.ExitBlock();
1292}
1293
Richard Smith54cc3c22014-12-11 20:50:24 +00001294/// \brief Prepares a path for being written to an AST file by converting it
1295/// to an absolute path and removing nested './'s.
1296///
1297/// \return \c true if the path was changed.
Benjamin Kramer6a96ae52015-02-06 18:36:04 +00001298static bool cleanPathForOutput(FileManager &FileMgr,
1299 SmallVectorImpl<char> &Path) {
Argyrios Kyrtzidis403cbcb2015-07-31 01:39:23 +00001300 bool Changed = FileMgr.makeAbsolutePath(Path);
Mike Aizatskyaeb9dd92015-11-09 19:12:18 +00001301 return Changed | llvm::sys::path::remove_dots(Path);
Richard Smith54cc3c22014-12-11 20:50:24 +00001302}
1303
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001304/// \brief Adjusts the given filename to only write out the portion of the
1305/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +00001306///
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001307/// \param Filename the file name to adjust.
1308///
Richard Smith7ed1bc92014-12-05 22:42:13 +00001309/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1310/// the returned filename will be adjusted by this root directory.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001311///
1312/// \returns either the original filename (if it needs no adjustment) or the
1313/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +00001314static const char *
Richard Smith7ed1bc92014-12-05 22:42:13 +00001315adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001316 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001317
Richard Smith7ed1bc92014-12-05 22:42:13 +00001318 if (BaseDir.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001319 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001320
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001321 // Verify that the filename and the system root have the same prefix.
1322 unsigned Pos = 0;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001323 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1324 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001325 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001326
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001327 // We hit the end of the filename before we hit the end of the system root.
1328 if (!Filename[Pos])
1329 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001330
Richard Smith7ed1bc92014-12-05 22:42:13 +00001331 // If there's not a path separator at the end of the base directory nor
1332 // immediately after it, then this isn't within the base directory.
1333 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1334 if (!llvm::sys::path::is_separator(BaseDir.back()))
1335 return Filename;
1336 } else {
1337 // If the file name has a '/' at the current position, skip over the '/'.
1338 // We distinguish relative paths from absolute paths by the
1339 // absence of '/' at the beginning of relative paths.
1340 //
1341 // FIXME: This is wrong. We distinguish them by asking if the path is
1342 // absolute, which isn't the same thing. And there might be multiple '/'s
1343 // in a row. Use a better mechanism to indicate whether we have emitted an
1344 // absolute or relative path.
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001345 ++Pos;
Richard Smith7ed1bc92014-12-05 22:42:13 +00001346 }
Mike Stump11289f42009-09-09 15:08:12 +00001347
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001348 return Filename + Pos;
1349}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001350
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001351ASTFileSignature ASTWriter::createSignature(StringRef Bytes) {
1352 // Calculate the hash till start of UNHASHED_CONTROL_BLOCK.
1353 llvm::SHA1 Hasher;
1354 Hasher.update(ArrayRef<uint8_t>(Bytes.bytes_begin(), Bytes.size()));
1355 auto Hash = Hasher.result();
1356
1357 // Convert to an array [5*i32].
1358 ASTFileSignature Signature;
1359 auto LShift = [&](unsigned char Val, unsigned Shift) {
1360 return (uint32_t)Val << Shift;
1361 };
1362 for (int I = 0; I != 5; ++I)
1363 Signature[I] = LShift(Hash[I * 4 + 0], 24) | LShift(Hash[I * 4 + 1], 16) |
1364 LShift(Hash[I * 4 + 2], 8) | LShift(Hash[I * 4 + 3], 0);
1365
1366 return Signature;
1367}
1368
1369ASTFileSignature ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1370 ASTContext &Context) {
1371 // Flush first to prepare the PCM hash (signature).
1372 Stream.FlushToWord();
1373 auto StartOfUnhashedControl = Stream.GetCurrentBitNo() >> 3;
1374
1375 // Enter the block and prepare to write records.
1376 RecordData Record;
1377 Stream.EnterSubblock(UNHASHED_CONTROL_BLOCK_ID, 5);
1378
1379 // For implicit modules, write the hash of the PCM as its signature.
1380 ASTFileSignature Signature;
1381 if (WritingModule &&
1382 PP.getHeaderSearchInfo().getHeaderSearchOpts().ModulesHashContent) {
1383 Signature = createSignature(StringRef(Buffer.begin(), StartOfUnhashedControl));
1384 Record.append(Signature.begin(), Signature.end());
1385 Stream.EmitRecord(SIGNATURE, Record);
1386 Record.clear();
Ben Langmuir487ea142014-10-23 18:05:36 +00001387 }
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001388
1389 // Diagnostic options.
1390 const auto &Diags = Context.getDiagnostics();
1391 const DiagnosticOptions &DiagOpts = Diags.getDiagnosticOptions();
1392#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1393#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1394 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1395#include "clang/Basic/DiagnosticOptions.def"
1396 Record.push_back(DiagOpts.Warnings.size());
1397 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1398 AddString(DiagOpts.Warnings[I], Record);
1399 Record.push_back(DiagOpts.Remarks.size());
1400 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1401 AddString(DiagOpts.Remarks[I], Record);
1402 // Note: we don't serialize the log or serialization file names, because they
1403 // are generally transient files and will almost always be overridden.
1404 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1405
1406 // Write out the diagnostic/pragma mappings.
1407 WritePragmaDiagnosticMappings(Diags, /* IsModule = */ WritingModule);
1408
1409 // Leave the options block.
1410 Stream.ExitBlock();
1411 return Signature;
Ben Langmuir487ea142014-10-23 18:05:36 +00001412}
1413
Douglas Gregor112b9072012-10-18 05:31:06 +00001414/// \brief Write the control block.
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001415void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1416 StringRef isysroot,
1417 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001418 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001419 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001420 RecordData Record;
1421
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001422 // Metadata
David Blaikieb44f0bf2017-01-04 22:36:43 +00001423 auto MetadataAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001424 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1425 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1426 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1427 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1428 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1429 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Richard Smithe75ee0f2015-08-17 07:13:32 +00001430 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001431 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1432 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
David Blaikieb44f0bf2017-01-04 22:36:43 +00001433 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(std::move(MetadataAbbrev));
Richard Smith7ed1bc92014-12-05 22:42:13 +00001434 assert((!WritingModule || isysroot.empty()) &&
1435 "writing module as a relocatable PCH?");
Mehdi Amini57a41912015-09-10 01:46:39 +00001436 {
1437 RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1438 CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1439 !isysroot.empty(), IncludeTimestamps,
1440 ASTHasCompilerErrors};
1441 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1442 getClangFullRepositoryVersion());
1443 }
Chandler Carruth885e78c2015-03-24 21:18:10 +00001444
Richard Smith8b706102017-05-31 20:56:55 +00001445 if (WritingModule) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001446 // Module name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001447 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001448 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001450 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00001451 RecordData::value_type Record[] = {MODULE_NAME};
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001452 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1453 }
1454
Richard Smith7ed1bc92014-12-05 22:42:13 +00001455 if (WritingModule && WritingModule->Directory) {
Richard Smith7ed1bc92014-12-05 22:42:13 +00001456 SmallString<128> BaseDir(WritingModule->Directory->getName());
Richard Smith54cc3c22014-12-11 20:50:24 +00001457 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
Richard Smithf7b41372015-08-13 23:47:44 +00001458
1459 // If the home of the module is the current working directory, then we
1460 // want to pick up the cwd of the build process loading the module, not
1461 // our cwd, when we load this module.
1462 if (!PP.getHeaderSearchInfo()
1463 .getHeaderSearchOpts()
1464 .ModuleMapFileHomeIsCwd ||
1465 WritingModule->Directory->getName() != StringRef(".")) {
1466 // Module directory.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001467 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithf7b41372015-08-13 23:47:44 +00001468 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1469 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
David Blaikieb44f0bf2017-01-04 22:36:43 +00001470 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smithf7b41372015-08-13 23:47:44 +00001471
Mehdi Amini57a41912015-09-10 01:46:39 +00001472 RecordData::value_type Record[] = {MODULE_DIRECTORY};
Richard Smithf7b41372015-08-13 23:47:44 +00001473 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1474 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00001475
1476 // Write out all other paths relative to the base directory if possible.
1477 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1478 } else if (!isysroot.empty()) {
1479 // Write out paths relative to the sysroot if possible.
1480 BaseDirectory = isysroot;
1481 }
1482
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001483 // Module map file
Richard Smithd19389a2017-07-05 07:47:11 +00001484 if (WritingModule && WritingModule->Kind == Module::ModuleMapModule) {
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001485 Record.clear();
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001486
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001487 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
Richard Smith8b706102017-05-31 20:56:55 +00001488 AddPath(WritingModule->PresumedModuleMapFile.empty()
1489 ? Map.getModuleMapFileForUniquing(WritingModule)->getName()
1490 : StringRef(WritingModule->PresumedModuleMapFile),
1491 Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001492
1493 // Additional module map files.
Richard Smith7ed1bc92014-12-05 22:42:13 +00001494 if (auto *AdditionalModMaps =
1495 Map.getAdditionalModuleMapFiles(WritingModule)) {
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001496 Record.push_back(AdditionalModMaps->size());
1497 for (const FileEntry *F : *AdditionalModMaps)
Richard Smith7ed1bc92014-12-05 22:42:13 +00001498 AddPath(F->getName(), Record);
Ben Langmuir4b8a9e92014-08-12 16:42:33 +00001499 } else {
1500 Record.push_back(0);
1501 }
1502
1503 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Ben Langmuirbeee15e2014-04-14 18:00:01 +00001504 }
1505
Douglas Gregor112b9072012-10-18 05:31:06 +00001506 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001507 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001508 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001509 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001510
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001511 for (ModuleFile &M : Mgr) {
Douglas Gregordf0c1512011-08-18 04:12:04 +00001512 // Skip modules that weren't directly imported.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001513 if (!M.isDirectlyImported())
Douglas Gregordf0c1512011-08-18 04:12:04 +00001514 continue;
1515
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001516 Record.push_back((unsigned)M.Kind); // FIXME: Stable encoding
1517 AddSourceLocation(M.ImportLoc, Record);
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00001518
1519 // If we have calculated signature, there is no need to store
1520 // the size or timestamp.
1521 Record.push_back(M.Signature ? 0 : M.File->getSize());
1522 Record.push_back(M.Signature ? 0 : getTimestampForOutput(M.File));
1523
1524 for (auto I : M.Signature)
1525 Record.push_back(I);
1526
Boris Kolpackovd30446f2017-08-31 06:26:43 +00001527 AddString(M.ModuleName, Record);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00001528 AddPath(M.FileName, Record);
Douglas Gregordf0c1512011-08-18 04:12:04 +00001529 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001530 Stream.EmitRecord(IMPORTS, Record);
1531 }
Mike Stump11289f42009-09-09 15:08:12 +00001532
Richard Smith0516b182015-09-08 19:40:14 +00001533 // Write the options block.
1534 Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1535
Douglas Gregor112b9072012-10-18 05:31:06 +00001536 // Language options.
Mehdi Amini5ae4a852015-09-09 20:35:37 +00001537 Record.clear();
Douglas Gregor112b9072012-10-18 05:31:06 +00001538 const LangOptions &LangOpts = Context.getLangOpts();
1539#define LANGOPT(Name, Bits, Default, Description) \
1540 Record.push_back(LangOpts.Name);
1541#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1542 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Alexey Samsonovedf99a92014-11-07 22:29:38 +00001543#include "clang/Basic/LangOptions.def"
1544#define SANITIZER(NAME, ID) \
1545 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietzf54319c2013-01-18 11:30:38 +00001546#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001547
Ben Langmuircd98cb72015-06-23 18:20:18 +00001548 Record.push_back(LangOpts.ModuleFeatures.size());
1549 for (StringRef Feature : LangOpts.ModuleFeatures)
1550 AddString(Feature, Record);
1551
Douglas Gregor112b9072012-10-18 05:31:06 +00001552 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1553 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Ben Langmuird4a667a2015-06-23 18:20:23 +00001554
1555 AddString(LangOpts.CurrentModule, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001556
1557 // Comment options.
1558 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001559 for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1560 AddString(I, Record);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001561 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001562 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001563
Samuel Antaoee8fb302016-01-06 13:42:12 +00001564 // OpenMP offloading options.
1565 Record.push_back(LangOpts.OMPTargetTriples.size());
1566 for (auto &T : LangOpts.OMPTargetTriples)
1567 AddString(T.getTriple(), Record);
1568
1569 AddString(LangOpts.OMPHostIRFile, Record);
1570
Douglas Gregor112b9072012-10-18 05:31:06 +00001571 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1572
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001573 // Target options.
1574 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001575 const TargetInfo &Target = Context.getTargetInfo();
1576 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001577 AddString(TargetOpts.Triple, Record);
1578 AddString(TargetOpts.CPU, Record);
1579 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001580 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1581 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1582 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1583 }
1584 Record.push_back(TargetOpts.Features.size());
1585 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1586 AddString(TargetOpts.Features[I], Record);
1587 }
1588 Stream.EmitRecord(TARGET_OPTIONS, Record);
1589
Douglas Gregorc6317db2012-10-24 15:49:58 +00001590 // File system options.
1591 Record.clear();
Yaron Keren8dec46c2015-08-26 08:10:22 +00001592 const FileSystemOptions &FSOpts =
1593 Context.getSourceManager().getFileManager().getFileSystemOpts();
Douglas Gregorc6317db2012-10-24 15:49:58 +00001594 AddString(FSOpts.WorkingDir, Record);
1595 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1596
Douglas Gregor2d302362012-10-24 16:50:34 +00001597 // Header search options.
1598 Record.clear();
1599 const HeaderSearchOptions &HSOpts
1600 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1601 AddString(HSOpts.Sysroot, Record);
1602
1603 // Include entries.
1604 Record.push_back(HSOpts.UserEntries.size());
1605 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1606 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1607 AddString(Entry.Path, Record);
1608 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001609 Record.push_back(Entry.IsFramework);
1610 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001611 }
1612
1613 // System header prefixes.
1614 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1615 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1616 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1617 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1618 }
1619
1620 AddString(HSOpts.ResourceDir, Record);
1621 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001622 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001623 Record.push_back(HSOpts.DisableModuleHash);
Richard Smith18934752017-06-06 00:32:01 +00001624 Record.push_back(HSOpts.ImplicitModuleMaps);
1625 Record.push_back(HSOpts.ModuleMapFileHomeIsCwd);
Douglas Gregor2d302362012-10-24 16:50:34 +00001626 Record.push_back(HSOpts.UseBuiltinIncludes);
1627 Record.push_back(HSOpts.UseStandardSystemIncludes);
1628 Record.push_back(HSOpts.UseStandardCXXIncludes);
1629 Record.push_back(HSOpts.UseLibcxx);
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +00001630 // Write out the specific module cache path that contains the module files.
1631 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001632 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1633
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001634 // Preprocessor options.
1635 Record.clear();
1636 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1637
1638 // Macro definitions.
1639 Record.push_back(PPOpts.Macros.size());
1640 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1641 AddString(PPOpts.Macros[I].first, Record);
1642 Record.push_back(PPOpts.Macros[I].second);
1643 }
1644
1645 // Includes
1646 Record.push_back(PPOpts.Includes.size());
1647 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1648 AddString(PPOpts.Includes[I], Record);
1649
1650 // Macro includes
1651 Record.push_back(PPOpts.MacroIncludes.size());
1652 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1653 AddString(PPOpts.MacroIncludes[I], Record);
1654
Douglas Gregorb6368752012-10-24 23:41:50 +00001655 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001656 // Detailed record is important since it is used for the module cache hash.
1657 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001658 AddString(PPOpts.ImplicitPCHInclude, Record);
1659 AddString(PPOpts.ImplicitPTHInclude, Record);
1660 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1661 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1662
Richard Smith0516b182015-09-08 19:40:14 +00001663 // Leave the options block.
1664 Stream.ExitBlock();
1665
Douglas Gregora3b20262011-05-06 21:43:30 +00001666 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001667 SourceManager &SM = Context.getSourceManager();
1668 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001669 auto FileAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001670 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1671 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001672 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001673 unsigned FileAbbrevCode = Stream.EmitAbbrev(std::move(FileAbbrev));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001674
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001675 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001676 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001677 Record.push_back(SM.getMainFileID().getOpaqueValue());
Richard Smith7ed1bc92014-12-05 22:42:13 +00001678 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregor45fe0362009-05-12 01:31:05 +00001679 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001680
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001681 Record.clear();
1682 Record.push_back(SM.getMainFileID().getOpaqueValue());
1683 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1684
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001685 // Original PCH directory
1686 if (!OutputFile.empty() && OutputFile != "-") {
David Blaikieb44f0bf2017-01-04 22:36:43 +00001687 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001688 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001690 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001691
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001692 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001693
Argyrios Kyrtzidisc56419e2015-07-31 00:58:32 +00001694 SM.getFileManager().makeAbsolutePath(OutputPath);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001695 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1696
Mehdi Amini57a41912015-09-10 01:46:39 +00001697 RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001698 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1699 }
1700
Douglas Gregor49491f72013-03-15 22:15:07 +00001701 WriteInputFiles(Context.SourceMgr,
1702 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001703 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001704 Stream.ExitBlock();
1705}
1706
Douglas Gregor49491f72013-03-15 22:15:07 +00001707namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001708
Douglas Gregor49491f72013-03-15 22:15:07 +00001709 /// \brief An input file.
1710 struct InputFileEntry {
1711 const FileEntry *File;
1712 bool IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001713 bool IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001714 bool BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001715 bool IsTopLevelModuleMap;
Douglas Gregor49491f72013-03-15 22:15:07 +00001716 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001717
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001718} // end anonymous namespace
Douglas Gregor49491f72013-03-15 22:15:07 +00001719
1720void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1721 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001722 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001723 using namespace llvm;
1724 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
Mehdi Amini57a41912015-09-10 01:46:39 +00001725
Douglas Gregor72be3902012-10-19 00:38:02 +00001726 // Create input-file abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001727 auto IFAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor72be3902012-10-19 00:38:02 +00001728 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001729 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001730 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1731 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001732 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Richard Smitha8cfffa2015-11-26 02:04:16 +00001733 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
Richard Smithf3f84612017-06-29 02:19:42 +00001734 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Module map
Douglas Gregor72be3902012-10-19 00:38:02 +00001735 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
David Blaikieb44f0bf2017-01-04 22:36:43 +00001736 unsigned IFAbbrevCode = Stream.EmitAbbrev(std::move(IFAbbrev));
Douglas Gregor72be3902012-10-19 00:38:02 +00001737
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001738 // Get all ContentCache objects for files, sorted by whether the file is a
1739 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001740 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001741 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1742 // Get this source location entry.
1743 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001744 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001745
1746 // We only care about file entries that were not overridden.
1747 if (!SLoc->isFile())
1748 continue;
Richard Smithf3f84612017-06-29 02:19:42 +00001749 const SrcMgr::FileInfo &File = SLoc->getFile();
1750 const SrcMgr::ContentCache *Cache = File.getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001751 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001752 continue;
1753
Douglas Gregor49491f72013-03-15 22:15:07 +00001754 InputFileEntry Entry;
1755 Entry.File = Cache->OrigEntry;
1756 Entry.IsSystemFile = Cache->IsSystemFile;
Richard Smitha8cfffa2015-11-26 02:04:16 +00001757 Entry.IsTransient = Cache->IsTransient;
Douglas Gregor49491f72013-03-15 22:15:07 +00001758 Entry.BufferOverridden = Cache->BufferOverridden;
Richard Smithf3f84612017-06-29 02:19:42 +00001759 Entry.IsTopLevelModuleMap = isModuleMap(File.getFileCharacteristic()) &&
1760 File.getIncludeLoc().isInvalid();
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001761 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001762 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001763 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001764 SortedFiles.push_front(Entry);
1765 }
1766
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001767 unsigned UserFilesNum = 0;
1768 // Write out all of the input files.
Richard Smithec216502015-02-13 19:48:37 +00001769 std::vector<uint64_t> InputFileOffsets;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001770 for (const auto &Entry : SortedFiles) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001771 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001772 if (InputFileID != 0)
1773 continue; // already recorded this file.
1774
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001775 // Record this entry's offset.
1776 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001777
1778 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001779
Douglas Gregor49491f72013-03-15 22:15:07 +00001780 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001781 ++UserFilesNum;
1782
Douglas Gregor72be3902012-10-19 00:38:02 +00001783 // Emit size/modification time for this file.
Mehdi Amini57a41912015-09-10 01:46:39 +00001784 // And whether this file was overridden.
1785 RecordData::value_type Record[] = {
Richard Smitha8cfffa2015-11-26 02:04:16 +00001786 INPUT_FILE,
1787 InputFileOffsets.size(),
1788 (uint64_t)Entry.File->getSize(),
1789 (uint64_t)getTimestampForOutput(Entry.File),
1790 Entry.BufferOverridden,
Richard Smithf3f84612017-06-29 02:19:42 +00001791 Entry.IsTransient,
1792 Entry.IsTopLevelModuleMap};
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001793
Richard Smith7ed1bc92014-12-05 22:42:13 +00001794 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1795 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001796
Douglas Gregor112b9072012-10-18 05:31:06 +00001797 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001798
1799 // Create input file offsets abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00001800 auto OffsetsAbbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001801 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1802 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001803 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1804 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001805 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
David Blaikieb44f0bf2017-01-04 22:36:43 +00001806 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(std::move(OffsetsAbbrev));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001807
1808 // Write input file offsets.
Mehdi Amini57a41912015-09-10 01:46:39 +00001809 RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1810 InputFileOffsets.size(), UserFilesNum};
Reid Kleckner012665e2015-05-21 00:13:09 +00001811 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001812}
1813
Douglas Gregora7f71a92009-04-10 03:52:48 +00001814//===----------------------------------------------------------------------===//
1815// Source Manager Serialization
1816//===----------------------------------------------------------------------===//
1817
1818/// \brief Create an abbreviation for the SLocEntry that refers to a
1819/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001820static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001821 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001822
David Blaikieb44f0bf2017-01-04 22:36:43 +00001823 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001824 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001829 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
David Blaikieb44f0bf2017-01-04 22:36:43 +00001834 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001835}
1836
1837/// \brief Create an abbreviation for the SLocEntry that refers to a
1838/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001839static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001840 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001841
David Blaikieb44f0bf2017-01-04 22:36:43 +00001842 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
Richard Smithf3f84612017-06-29 02:19:42 +00001846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Characteristic
Douglas Gregora7f71a92009-04-10 03:52:48 +00001847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001849 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001850}
1851
1852/// \brief Create an abbreviation for the SLocEntry that refers to a
1853/// buffer's blob.
Richard Smithaada85c2016-02-06 02:06:43 +00001854static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream,
1855 bool Compressed) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001856 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001857
David Blaikieb44f0bf2017-01-04 22:36:43 +00001858 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smithaada85c2016-02-06 02:06:43 +00001859 Abbrev->Add(BitCodeAbbrevOp(Compressed ? SM_SLOC_BUFFER_BLOB_COMPRESSED
1860 : SM_SLOC_BUFFER_BLOB));
1861 if (Compressed)
1862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Uncompressed size
Douglas Gregora7f71a92009-04-10 03:52:48 +00001863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
David Blaikieb44f0bf2017-01-04 22:36:43 +00001864 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001865}
1866
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001867/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1868/// expansion.
1869static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001870 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00001871
David Blaikieb44f0bf2017-01-04 22:36:43 +00001872 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001873 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
David Blaikieb44f0bf2017-01-04 22:36:43 +00001879 return Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001880}
1881
Douglas Gregor09b69892011-02-10 17:09:37 +00001882namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00001883
Douglas Gregor09b69892011-02-10 17:09:37 +00001884 // Trait used for the on-disk hash table of header search information.
1885 class HeaderFileInfoTrait {
1886 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001887
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001888 // Keep track of the framework names we've used during serialization.
1889 SmallVector<char, 128> FrameworkStringData;
1890 llvm::StringMap<unsigned> FrameworkNameOffset;
1891
Douglas Gregor09b69892011-02-10 17:09:37 +00001892 public:
Richard Smith040e1262017-06-02 01:55:39 +00001893 HeaderFileInfoTrait(ASTWriter &Writer) : Writer(Writer) {}
1894
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001895 struct key_type {
Mehdi Amini004b9c72016-10-10 22:52:47 +00001896 StringRef Filename;
Richard Smith040e1262017-06-02 01:55:39 +00001897 off_t Size;
1898 time_t ModTime;
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001899 };
1900 typedef const key_type &key_type_ref;
Richard Smith040e1262017-06-02 01:55:39 +00001901
1902 using UnresolvedModule =
1903 llvm::PointerIntPair<Module *, 2, ModuleMap::ModuleHeaderRole>;
Douglas Gregor09b69892011-02-10 17:09:37 +00001904
Richard Smith040e1262017-06-02 01:55:39 +00001905 struct data_type {
1906 const HeaderFileInfo &HFI;
1907 ArrayRef<ModuleMap::KnownHeader> KnownHeaders;
1908 UnresolvedModule Unresolved;
1909 };
Douglas Gregor09b69892011-02-10 17:09:37 +00001910 typedef const data_type &data_type_ref;
Richard Smith040e1262017-06-02 01:55:39 +00001911
Justin Bogner25463f12014-04-18 20:27:24 +00001912 typedef unsigned hash_value_type;
1913 typedef unsigned offset_type;
Douglas Gregor09b69892011-02-10 17:09:37 +00001914
Richard Smithe75ee0f2015-08-17 07:13:32 +00001915 hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001916 // The hash is based only on size/time of the file, so that the reader can
1917 // match even when symlinking or excess path elements ("foo/../", "../")
1918 // change the form of the name. However, complete path is still the key.
Richard Smith040e1262017-06-02 01:55:39 +00001919 return llvm::hash_combine(key.Size, key.ModTime);
Douglas Gregor09b69892011-02-10 17:09:37 +00001920 }
1921
1922 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001923 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001924 using namespace llvm::support;
Richard Smith386bb072015-08-18 23:42:23 +00001925 endian::Writer<little> LE(Out);
Mehdi Amini004b9c72016-10-10 22:52:47 +00001926 unsigned KeyLen = key.Filename.size() + 1 + 8 + 8;
Richard Smith386bb072015-08-18 23:42:23 +00001927 LE.write<uint16_t>(KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001928 unsigned DataLen = 1 + 2 + 4 + 4;
Richard Smith040e1262017-06-02 01:55:39 +00001929 for (auto ModInfo : Data.KnownHeaders)
Richard Smith386bb072015-08-18 23:42:23 +00001930 if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1931 DataLen += 4;
Richard Smith040e1262017-06-02 01:55:39 +00001932 if (Data.Unresolved.getPointer())
1933 DataLen += 4;
Richard Smith386bb072015-08-18 23:42:23 +00001934 LE.write<uint8_t>(DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001935 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001936 }
Richard Smith040e1262017-06-02 01:55:39 +00001937
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001938 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001939 using namespace llvm::support;
1940 endian::Writer<little> LE(Out);
Richard Smith040e1262017-06-02 01:55:39 +00001941 LE.write<uint64_t>(key.Size);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001942 KeyLen -= 8;
Richard Smith040e1262017-06-02 01:55:39 +00001943 LE.write<uint64_t>(key.ModTime);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001944 KeyLen -= 8;
Mehdi Amini004b9c72016-10-10 22:52:47 +00001945 Out.write(key.Filename.data(), KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001946 }
Richard Smith040e1262017-06-02 01:55:39 +00001947
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001948 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001949 data_type_ref Data, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00001950 using namespace llvm::support;
1951 endian::Writer<little> LE(Out);
Douglas Gregor09b69892011-02-10 17:09:37 +00001952 uint64_t Start = Out.tell(); (void)Start;
1953
Richard Smithf3f84612017-06-29 02:19:42 +00001954 unsigned char Flags = (Data.HFI.isImport << 5)
1955 | (Data.HFI.isPragmaOnce << 4)
Richard Smith040e1262017-06-02 01:55:39 +00001956 | (Data.HFI.DirInfo << 1)
1957 | Data.HFI.IndexHeaderMapHeader;
Justin Bognere1c147c2014-03-28 22:03:19 +00001958 LE.write<uint8_t>(Flags);
Richard Smith040e1262017-06-02 01:55:39 +00001959 LE.write<uint16_t>(Data.HFI.NumIncludes);
Douglas Gregor09b69892011-02-10 17:09:37 +00001960
Richard Smith040e1262017-06-02 01:55:39 +00001961 if (!Data.HFI.ControllingMacro)
1962 LE.write<uint32_t>(Data.HFI.ControllingMacroID);
Douglas Gregor09b69892011-02-10 17:09:37 +00001963 else
Richard Smith040e1262017-06-02 01:55:39 +00001964 LE.write<uint32_t>(Writer.getIdentifierRef(Data.HFI.ControllingMacro));
1965
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001966 unsigned Offset = 0;
Richard Smith040e1262017-06-02 01:55:39 +00001967 if (!Data.HFI.Framework.empty()) {
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001968 // If this header refers into a framework, save the framework name.
1969 llvm::StringMap<unsigned>::iterator Pos
Richard Smith040e1262017-06-02 01:55:39 +00001970 = FrameworkNameOffset.find(Data.HFI.Framework);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001971 if (Pos == FrameworkNameOffset.end()) {
1972 Offset = FrameworkStringData.size() + 1;
Richard Smith040e1262017-06-02 01:55:39 +00001973 FrameworkStringData.append(Data.HFI.Framework.begin(),
1974 Data.HFI.Framework.end());
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001975 FrameworkStringData.push_back(0);
1976
Richard Smith040e1262017-06-02 01:55:39 +00001977 FrameworkNameOffset[Data.HFI.Framework] = Offset;
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001978 } else
1979 Offset = Pos->second;
1980 }
Justin Bognere1c147c2014-03-28 22:03:19 +00001981 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001982
Richard Smith040e1262017-06-02 01:55:39 +00001983 auto EmitModule = [&](Module *M, ModuleMap::ModuleHeaderRole Role) {
1984 if (uint32_t ModID = Writer.getLocalOrImportedSubmoduleID(M)) {
1985 uint32_t Value = (ModID << 2) | (unsigned)Role;
Richard Smith386bb072015-08-18 23:42:23 +00001986 assert((Value >> 2) == ModID && "overflow in header module info");
1987 LE.write<uint32_t>(Value);
1988 }
Richard Smith040e1262017-06-02 01:55:39 +00001989 };
1990
1991 // FIXME: If the header is excluded, we should write out some
1992 // record of that fact.
1993 for (auto ModInfo : Data.KnownHeaders)
1994 EmitModule(ModInfo.getModule(), ModInfo.getRole());
1995 if (Data.Unresolved.getPointer())
1996 EmitModule(Data.Unresolved.getPointer(), Data.Unresolved.getInt());
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001997
Douglas Gregor09b69892011-02-10 17:09:37 +00001998 assert(Out.tell() - Start == DataLen && "Wrong data length");
1999 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002000
2001 const char *strings_begin() const { return FrameworkStringData.begin(); }
2002 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00002003 };
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00002004
Douglas Gregor09b69892011-02-10 17:09:37 +00002005} // end anonymous namespace
2006
2007/// \brief Write the header search block for the list of files that
2008///
2009/// \param HS The header search structure to save.
Richard Smith7ed1bc92014-12-05 22:42:13 +00002010void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Richard Smith040e1262017-06-02 01:55:39 +00002011 HeaderFileInfoTrait GeneratorTrait(*this);
2012 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
2013 SmallVector<const char *, 4> SavedStrings;
2014 unsigned NumHeaderSearchEntries = 0;
2015
2016 // Find all unresolved headers for the current module. We generally will
2017 // have resolved them before we get here, but not necessarily: we might be
2018 // compiling a preprocessed module, where there is no requirement for the
2019 // original files to exist any more.
Benjamin Krameree021d52017-06-02 17:30:24 +00002020 const HeaderFileInfo Empty; // So we can take a reference.
Richard Smith040e1262017-06-02 01:55:39 +00002021 if (WritingModule) {
2022 llvm::SmallVector<Module *, 16> Worklist(1, WritingModule);
2023 while (!Worklist.empty()) {
2024 Module *M = Worklist.pop_back_val();
2025 if (!M->isAvailable())
2026 continue;
2027
2028 // Map to disk files where possible, to pick up any missing stat
2029 // information. This also means we don't need to check the unresolved
2030 // headers list when emitting resolved headers in the first loop below.
2031 // FIXME: It'd be preferable to avoid doing this if we were given
2032 // sufficient stat information in the module map.
2033 HS.getModuleMap().resolveHeaderDirectives(M);
2034
2035 // If the file didn't exist, we can still create a module if we were given
2036 // enough information in the module map.
2037 for (auto U : M->MissingHeaders) {
2038 // Check that we were given enough information to build a module
2039 // without this file existing on disk.
2040 if (!U.Size || (!U.ModTime && IncludeTimestamps)) {
2041 PP->Diag(U.FileNameLoc, diag::err_module_no_size_mtime_for_header)
2042 << WritingModule->getFullModuleName() << U.Size.hasValue()
2043 << U.FileName;
2044 continue;
2045 }
2046
2047 // Form the effective relative pathname for the file.
2048 SmallString<128> Filename(M->Directory->getName());
2049 llvm::sys::path::append(Filename, U.FileName);
2050 PreparePathForOutput(Filename);
2051
2052 StringRef FilenameDup = strdup(Filename.c_str());
2053 SavedStrings.push_back(FilenameDup.data());
2054
2055 HeaderFileInfoTrait::key_type Key = {
2056 FilenameDup, *U.Size, IncludeTimestamps ? *U.ModTime : 0
2057 };
2058 HeaderFileInfoTrait::data_type Data = {
Benjamin Krameree021d52017-06-02 17:30:24 +00002059 Empty, {}, {M, ModuleMap::headerKindToRole(U.Kind)}
Richard Smith040e1262017-06-02 01:55:39 +00002060 };
2061 // FIXME: Deal with cases where there are multiple unresolved header
2062 // directives in different submodules for the same header.
2063 Generator.insert(Key, Data, GeneratorTrait);
2064 ++NumHeaderSearchEntries;
2065 }
2066
2067 Worklist.append(M->submodule_begin(), M->submodule_end());
2068 }
2069 }
2070
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002071 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00002072 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
2073
2074 if (FilesByUID.size() > HS.header_file_size())
2075 FilesByUID.resize(HS.header_file_size());
Richard Smith040e1262017-06-02 01:55:39 +00002076
Douglas Gregor09b69892011-02-10 17:09:37 +00002077 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
2078 const FileEntry *File = FilesByUID[UID];
2079 if (!File)
2080 continue;
2081
Richard Smith386bb072015-08-18 23:42:23 +00002082 // Get the file info. This will load info from the external source if
2083 // necessary. Skip emitting this file if we have no information on it
2084 // as a header file (in which case HFI will be null) or if it hasn't
2085 // changed since it was loaded. Also skip it if it's for a modular header
2086 // from a different module; in that case, we rely on the module(s)
2087 // containing the header to provide this information.
Richard Smithd8879c82015-08-24 21:59:32 +00002088 const HeaderFileInfo *HFI =
2089 HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
2090 if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00002091 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00002092
Richard Smith7ed1bc92014-12-05 22:42:13 +00002093 // Massage the file path into an appropriate form.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002094 StringRef Filename = File->getName();
Richard Smith7ed1bc92014-12-05 22:42:13 +00002095 SmallString<128> FilenameTmp(Filename);
2096 if (PreparePathForOutput(FilenameTmp)) {
2097 // If we performed any translation on the file name at all, we need to
2098 // save this string, since the generator will refer to it later.
Mehdi Amini004b9c72016-10-10 22:52:47 +00002099 Filename = StringRef(strdup(FilenameTmp.c_str()));
2100 SavedStrings.push_back(Filename.data());
Douglas Gregor09b69892011-02-10 17:09:37 +00002101 }
Richard Smith7ed1bc92014-12-05 22:42:13 +00002102
Richard Smith040e1262017-06-02 01:55:39 +00002103 HeaderFileInfoTrait::key_type Key = {
2104 Filename, File->getSize(), getTimestampForOutput(File)
2105 };
2106 HeaderFileInfoTrait::data_type Data = {
2107 *HFI, HS.getModuleMap().findAllModulesForHeader(File), {}
2108 };
2109 Generator.insert(Key, Data, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00002110 ++NumHeaderSearchEntries;
2111 }
Richard Smith040e1262017-06-02 01:55:39 +00002112
Douglas Gregor09b69892011-02-10 17:09:37 +00002113 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002114 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00002115 uint32_t BucketOffset;
2116 {
Justin Bognere1c147c2014-03-28 22:03:19 +00002117 using namespace llvm::support;
Douglas Gregor09b69892011-02-10 17:09:37 +00002118 llvm::raw_svector_ostream Out(TableData);
2119 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00002120 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor09b69892011-02-10 17:09:37 +00002121 BucketOffset = Generator.Emit(Out, GeneratorTrait);
2122 }
2123
2124 // Create a blob abbreviation
2125 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002126
David Blaikieb44f0bf2017-01-04 22:36:43 +00002127 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor09b69892011-02-10 17:09:37 +00002128 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
2129 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2130 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00002132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002133 unsigned TableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor09b69892011-02-10 17:09:37 +00002134
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002135 // Write the header search table
Mehdi Amini57a41912015-09-10 01:46:39 +00002136 RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
2137 NumHeaderSearchEntries, TableData.size()};
Douglas Gregor4b123cb2011-07-28 04:50:02 +00002138 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Yaron Keren92e1b622015-03-18 10:17:07 +00002139 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
Douglas Gregor09b69892011-02-10 17:09:37 +00002140
2141 // Free all of the strings we had to duplicate.
2142 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00002143 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00002144}
2145
George Rimarc39f5492017-01-17 15:45:31 +00002146static void emitBlob(llvm::BitstreamWriter &Stream, StringRef Blob,
2147 unsigned SLocBufferBlobCompressedAbbrv,
2148 unsigned SLocBufferBlobAbbrv) {
2149 typedef ASTWriter::RecordData::value_type RecordDataType;
2150
2151 // Compress the buffer if possible. We expect that almost all PCM
2152 // consumers will not want its contents.
2153 SmallString<0> CompressedBuffer;
2154 if (llvm::zlib::isAvailable()) {
2155 llvm::Error E = llvm::zlib::compress(Blob.drop_back(1), CompressedBuffer);
2156 if (!E) {
2157 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB_COMPRESSED,
2158 Blob.size() - 1};
2159 Stream.EmitRecordWithBlob(SLocBufferBlobCompressedAbbrv, Record,
2160 CompressedBuffer);
2161 return;
2162 }
2163 llvm::consumeError(std::move(E));
2164 }
2165
2166 RecordDataType Record[] = {SM_SLOC_BUFFER_BLOB};
2167 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record, Blob);
2168}
2169
Douglas Gregora7f71a92009-04-10 03:52:48 +00002170/// \brief Writes the block containing the serialized form of the
2171/// source manager.
2172///
2173/// TODO: We should probably use an on-disk hash table (stored in a
2174/// blob), indexed based on the file name, so that we only create
2175/// entries for files that we actually need. In the common case (no
2176/// errors), we probably won't have to create file entries for any of
2177/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002178void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Richard Smith7ed1bc92014-12-05 22:42:13 +00002179 const Preprocessor &PP) {
Douglas Gregor258ae542009-04-27 06:38:32 +00002180 RecordData Record;
2181
Chris Lattner0910e3b2009-04-10 17:16:57 +00002182 // Enter the source manager block.
Richard Smithaada85c2016-02-06 02:06:43 +00002183 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 4);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002184
2185 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00002186 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
2187 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
Richard Smithaada85c2016-02-06 02:06:43 +00002188 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream, false);
2189 unsigned SLocBufferBlobCompressedAbbrv =
2190 CreateSLocBufferBlobAbbrev(Stream, true);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002191 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00002192
Douglas Gregor258ae542009-04-27 06:38:32 +00002193 // Write out the source location entry table. We skip the first
2194 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00002195 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00002196 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00002197 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
2198 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00002199 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00002200 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00002201 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002202 FileID FID = FileID::get(I);
2203 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002204
Douglas Gregor258ae542009-04-27 06:38:32 +00002205 // Record the offset of this source-location entry.
2206 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
2207
2208 // Figure out which record code to use.
2209 unsigned Code;
2210 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00002211 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
2212 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002213 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00002214 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002216 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002217 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00002218 Record.clear();
2219 Record.push_back(Code);
2220
Douglas Gregor925296b2011-07-19 16:10:42 +00002221 // Starting offset of this entry within this module, so skip the dummy.
2222 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00002223 if (SLoc->isFile()) {
2224 const SrcMgr::FileInfo &File = SLoc->getFile();
Richard Smithb22a1d12016-03-27 20:13:24 +00002225 AddSourceLocation(File.getIncludeLoc(), Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002226 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
2227 Record.push_back(File.hasLineDirectives());
2228
2229 const SrcMgr::ContentCache *Content = File.getContentCache();
Richard Smithaada85c2016-02-06 02:06:43 +00002230 bool EmitBlob = false;
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002231 if (Content->OrigEntry) {
2232 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00002233 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00002234
Douglas Gregor3120d2c2012-10-22 18:42:04 +00002235 // The source location entry is a file. Emit input file ID.
2236 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
2237 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00002238
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00002239 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002240
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00002241 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002242 if (FDI != FileDeclIDs.end()) {
2243 Record.push_back(FDI->second->FirstDeclIndex);
2244 Record.push_back(FDI->second->DeclIDs.size());
2245 } else {
2246 Record.push_back(0);
2247 Record.push_back(0);
2248 }
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002249
2250 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
2251
Richard Smithaada85c2016-02-06 02:06:43 +00002252 if (Content->BufferOverridden || Content->IsTransient)
2253 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002254 } else {
2255 // The source location entry is a buffer. The blob associated
2256 // with this entry contains the contents of the buffer.
2257
2258 // We add one to the size so that we capture the trailing NULL
2259 // that is required by llvm::MemoryBuffer::getMemBuffer (on
2260 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00002261 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00002262 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Mehdi Amini99d1b292016-10-01 16:38:28 +00002263 StringRef Name = Buffer->getBufferIdentifier();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002264 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Mehdi Amini99d1b292016-10-01 16:38:28 +00002265 StringRef(Name.data(), Name.size() + 1));
Richard Smithaada85c2016-02-06 02:06:43 +00002266 EmitBlob = true;
Douglas Gregor258ae542009-04-27 06:38:32 +00002267
Mehdi Amini99d1b292016-10-01 16:38:28 +00002268 if (Name == "<built-in>")
Douglas Gregor925296b2011-07-19 16:10:42 +00002269 PreloadSLocs.push_back(SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00002270 }
Richard Smithaada85c2016-02-06 02:06:43 +00002271
2272 if (EmitBlob) {
2273 // Include the implicit terminating null character in the on-disk buffer
2274 // if we're writing it uncompressed.
2275 const llvm::MemoryBuffer *Buffer =
2276 Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
2277 StringRef Blob(Buffer->getBufferStart(), Buffer->getBufferSize() + 1);
George Rimarc39f5492017-01-17 15:45:31 +00002278 emitBlob(Stream, Blob, SLocBufferBlobCompressedAbbrv,
2279 SLocBufferBlobAbbrv);
Richard Smithaada85c2016-02-06 02:06:43 +00002280 }
Douglas Gregor258ae542009-04-27 06:38:32 +00002281 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002282 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00002283 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Richard Smithb22a1d12016-03-27 20:13:24 +00002284 AddSourceLocation(Expansion.getSpellingLoc(), Record);
2285 AddSourceLocation(Expansion.getExpansionLocStart(), Record);
2286 AddSourceLocation(Expansion.isMacroArgExpansion()
2287 ? SourceLocation()
2288 : Expansion.getExpansionLocEnd(),
2289 Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002290
2291 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00002292 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00002293 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00002294 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00002295 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002296 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00002297 }
2298 }
2299
Douglas Gregor8f45df52009-04-16 22:23:12 +00002300 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00002301
2302 if (SLocEntryOffsets.empty())
2303 return;
2304
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002305 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00002306 // table is used for lazily loading source-location information.
2307 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002308
David Blaikieb44f0bf2017-01-04 22:36:43 +00002309 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00002310 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00002311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00002312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00002313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
David Blaikieb44f0bf2017-01-04 22:36:43 +00002314 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002315 {
2316 RecordData::value_type Record[] = {
2317 SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
2318 SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
2319 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
2320 bytes(SLocEntryOffsets));
2321 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002322 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00002323 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00002324 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00002325
2326 // Write the line table. It depends on remapping working, so it must come
2327 // after the source location offsets.
2328 if (SourceMgr.hasLineTable()) {
2329 LineTableInfo &LineTable = SourceMgr.getLineTable();
2330
2331 Record.clear();
Richard Smith63078492015-09-01 07:41:55 +00002332
2333 // Emit the needed file names.
2334 llvm::DenseMap<int, int> FilenameMap;
2335 for (const auto &L : LineTable) {
2336 if (L.first.ID < 0)
2337 continue;
2338 for (auto &LE : L.second) {
2339 if (FilenameMap.insert(std::make_pair(LE.FilenameID,
2340 FilenameMap.size())).second)
2341 AddPath(LineTable.getFilename(LE.FilenameID), Record);
2342 }
2343 }
2344 Record.push_back(0);
Douglas Gregor925296b2011-07-19 16:10:42 +00002345
2346 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002347 for (const auto &L : LineTable) {
Douglas Gregor925296b2011-07-19 16:10:42 +00002348 // Only emit entries for local files.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002349 if (L.first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00002350 continue;
2351
2352 // Emit the file ID
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002353 Record.push_back(L.first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00002354
2355 // Emit the line entries
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002356 Record.push_back(L.second.size());
2357 for (const auto &LE : L.second) {
2358 Record.push_back(LE.FileOffset);
2359 Record.push_back(LE.LineNo);
2360 Record.push_back(FilenameMap[LE.FilenameID]);
2361 Record.push_back((unsigned)LE.FileKind);
2362 Record.push_back(LE.IncludeOffset);
Douglas Gregor925296b2011-07-19 16:10:42 +00002363 }
2364 }
Richard Smith63078492015-09-01 07:41:55 +00002365
Douglas Gregor925296b2011-07-19 16:10:42 +00002366 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2367 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00002368}
2369
Douglas Gregorc5046832009-04-27 18:38:38 +00002370//===----------------------------------------------------------------------===//
2371// Preprocessor Serialization
2372//===----------------------------------------------------------------------===//
2373
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002374static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2375 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002376 if (MacroInfo *MI = MD->getMacroInfo())
2377 if (MI->isBuiltinMacro())
2378 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00002379
2380 if (IsModule) {
2381 SourceLocation Loc = MD->getLocation();
2382 if (Loc.isInvalid())
2383 return true;
2384 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2385 return true;
2386 }
2387
2388 return false;
2389}
2390
Chris Lattnereeffaef2009-04-10 17:15:23 +00002391/// \brief Writes the block containing the serialized form of the
2392/// preprocessor.
2393///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002394void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002395 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2396 if (PPRec)
2397 WritePreprocessorDetail(*PPRec);
2398
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002399 RecordData Record;
Richard Smithd7329392015-04-21 21:46:32 +00002400 RecordData ModuleMacroRecord;
Chris Lattner0910e3b2009-04-10 17:16:57 +00002401
Chris Lattner0af3ba12009-04-13 01:29:17 +00002402 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2403 if (PP.getCounterValue() != 0) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002404 RecordData::value_type Record[] = {PP.getCounterValue()};
Sebastian Redl539c5062010-08-18 23:57:32 +00002405 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Douglas Gregoreda6a892009-04-26 00:07:37 +00002406 }
2407
Erik Verbruggenb34c79f2017-05-30 11:54:55 +00002408 if (PP.isRecordingPreamble() && PP.hasRecordedPreamble()) {
2409 assert(!IsModule);
2410 for (const auto &Cond : PP.getPreambleConditionalStack()) {
2411 AddSourceLocation(Cond.IfLoc, Record);
2412 Record.push_back(Cond.WasSkipping);
2413 Record.push_back(Cond.FoundNonSkip);
2414 Record.push_back(Cond.FoundElse);
2415 }
2416 Stream.EmitRecord(PP_CONDITIONAL_STACK, Record);
2417 Record.clear();
2418 }
2419
Douglas Gregoreda6a892009-04-26 00:07:37 +00002420 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00002421 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00002422
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002423 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002424 // FIXME: Include a location for the use, and say which one was used.
Douglas Gregoreda6a892009-04-26 00:07:37 +00002425 if (PP.SawDateOrTime())
Richard Smithb5aaf5a2015-09-01 02:35:58 +00002426 PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
Douglas Gregor796d76a2010-10-20 22:00:55 +00002427
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002428 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002429 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002430
Richard Smithee977932015-05-01 21:22:17 +00002431 // Construct the list of identifiers with macro directives that need to be
2432 // serialized.
2433 SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2434 for (auto &Id : PP.getIdentifierTable())
2435 if (Id.second->hadMacroDefinition() &&
2436 (!Id.second->isFromAST() ||
2437 Id.second->hasChangedSinceDeserialization()))
2438 MacroIdentifiers.push_back(Id.second);
Douglas Gregor2e5571d2011-02-10 18:20:09 +00002439 // Sort the set of macro definitions that need to be serialized by the
2440 // name of the macro, to provide a stable ordering.
Richard Smithee977932015-05-01 21:22:17 +00002441 std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2442 llvm::less_ptr<IdentifierInfo>());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002443
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002444 // Emit the macro directives as a list and associate the offset with the
2445 // identifier they belong to.
Richard Smithee977932015-05-01 21:22:17 +00002446 for (const IdentifierInfo *Name : MacroIdentifiers) {
2447 MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
Richard Smithd7329392015-04-21 21:46:32 +00002448 auto StartOffset = Stream.GetCurrentBitNo();
2449
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002450 // Emit the macro directives in reverse source order.
2451 for (; MD; MD = MD->getPrevious()) {
Richard Smithd7329392015-04-21 21:46:32 +00002452 // Once we hit an ignored macro, we're done: the rest of the chain
2453 // will all be ignored macros.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002454 if (shouldIgnoreMacro(MD, IsModule, PP))
Richard Smithd7329392015-04-21 21:46:32 +00002455 break;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002456
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002457 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002458 Record.push_back(MD->getKind());
Richard Smithdaa69e02014-07-25 04:40:03 +00002459 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Richard Smith3981b172015-04-30 02:16:23 +00002460 Record.push_back(getMacroRef(DefMD->getInfo(), Name));
Richard Smith713369b2015-04-23 20:40:50 +00002461 } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002462 Record.push_back(VisMD->isPublic());
2463 }
Richard Smithb8b2ed62015-04-23 18:18:26 +00002464 }
Richard Smithd7329392015-04-21 21:46:32 +00002465
Richard Smithb8b2ed62015-04-23 18:18:26 +00002466 // Write out any exported module macros.
Richard Smithee977932015-05-01 21:22:17 +00002467 bool EmittedModuleMacros = false;
Manman Ren33d80292016-05-31 18:19:32 +00002468 // We write out exported module macros for PCH as well.
2469 auto Leafs = PP.getLeafModuleMacros(Name);
2470 SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2471 llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2472 while (!Worklist.empty()) {
2473 auto *Macro = Worklist.pop_back_val();
Richard Smithd7329392015-04-21 21:46:32 +00002474
Manman Ren33d80292016-05-31 18:19:32 +00002475 // Emit a record indicating this submodule exports this macro.
2476 ModuleMacroRecord.push_back(
2477 getSubmoduleID(Macro->getOwningModule()));
2478 ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2479 for (auto *M : Macro->overrides())
2480 ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
Richard Smithd7329392015-04-21 21:46:32 +00002481
Manman Ren33d80292016-05-31 18:19:32 +00002482 Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2483 ModuleMacroRecord.clear();
Richard Smithd7329392015-04-21 21:46:32 +00002484
Manman Ren33d80292016-05-31 18:19:32 +00002485 // Enqueue overridden macros once we've visited all their ancestors.
2486 for (auto *M : Macro->overrides())
2487 if (++Visits[M] == M->getNumOverridingMacros())
2488 Worklist.push_back(M);
Richard Smithee977932015-05-01 21:22:17 +00002489
Manman Ren33d80292016-05-31 18:19:32 +00002490 EmittedModuleMacros = true;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002491 }
Richard Smithd7329392015-04-21 21:46:32 +00002492
Richard Smithee977932015-05-01 21:22:17 +00002493 if (Record.empty() && !EmittedModuleMacros)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002494 continue;
2495
Richard Smithd7329392015-04-21 21:46:32 +00002496 IdentMacroDirectivesOffsetMap[Name] = StartOffset;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002497 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2498 Record.clear();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002499 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002500
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002501 /// \brief Offsets of each of the macros into the bitstream, indexed by
2502 /// the local macro ID
2503 ///
2504 /// For each identifier that is associated with a macro, this map
2505 /// provides the offset into the bitstream where that macro is
2506 /// defined.
2507 std::vector<uint32_t> MacroOffsets;
2508
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002509 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2510 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2511 MacroInfo *MI = MacroInfosToEmit[I].MI;
2512 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00002513
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002514 if (ID < FirstMacroID) {
2515 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2516 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002517 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002518
2519 // Record the local offset of this macro.
2520 unsigned Index = ID - FirstMacroID;
2521 if (Index == MacroOffsets.size())
2522 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2523 else {
2524 if (Index > MacroOffsets.size())
2525 MacroOffsets.resize(Index + 1);
2526
2527 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2528 }
2529
2530 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002531 AddSourceLocation(MI->getDefinitionLoc(), Record);
2532 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2533 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +00002534 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002535 unsigned Code;
2536 if (MI->isObjectLike()) {
2537 Code = PP_MACRO_OBJECT_LIKE;
2538 } else {
2539 Code = PP_MACRO_FUNCTION_LIKE;
2540
2541 Record.push_back(MI->isC99Varargs());
2542 Record.push_back(MI->isGNUVarargs());
2543 Record.push_back(MI->hasCommaPasting());
Faisal Valiac506d72017-07-17 17:18:43 +00002544 Record.push_back(MI->getNumParams());
2545 for (const IdentifierInfo *Param : MI->params())
2546 AddIdentifierRef(Param, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002547 }
2548
2549 // If we have a detailed preprocessing record, record the macro definition
2550 // ID that corresponds to this macro.
2551 if (PPRec)
2552 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2553
2554 Stream.EmitRecord(Code, Record);
2555 Record.clear();
2556
2557 // Emit the tokens array.
2558 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2559 // Note that we know that the preprocessor does not have any annotation
2560 // tokens in it because they are created by the parser, and thus can't
2561 // be in a macro definition.
2562 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002563 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002564 Stream.EmitRecord(PP_TOKEN, Record);
2565 Record.clear();
2566 }
2567 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002568 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002569
Douglas Gregor92a96f52011-02-08 21:58:10 +00002570 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002571
2572 // Write the offsets table for macro IDs.
2573 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002574
David Blaikieb44f0bf2017-01-04 22:36:43 +00002575 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002576 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2580
David Blaikieb44f0bf2017-01-04 22:36:43 +00002581 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00002582 {
2583 RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2584 FirstMacroID - NUM_PREDEF_MACRO_IDS};
2585 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2586 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00002587}
2588
2589void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002590 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002591 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002592
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002593 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002594
Douglas Gregor92a96f52011-02-08 21:58:10 +00002595 // Enter the preprocessor block.
2596 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002597
Douglas Gregoraae92242010-03-19 21:51:54 +00002598 // If the preprocessor has a preprocessing record, emit it.
2599 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002600 using namespace llvm;
2601
2602 // Set up the abbreviation for
2603 unsigned InclusionAbbrev = 0;
2604 {
David Blaikieb44f0bf2017-01-04 22:36:43 +00002605 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002606 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002612 InclusionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002613 }
2614
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002615 unsigned FirstPreprocessorEntityID
2616 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2617 + NUM_PREDEF_PP_ENTITY_IDS;
2618 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002619 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002620 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2621 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002622 E != EEnd;
2623 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002624 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002625
Richard Smith66a81862015-05-04 02:25:31 +00002626 PreprocessedEntityOffsets.push_back(
2627 PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002628
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002629 if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002630 // Record this macro definition's ID.
2631 MacroDefinitions[MD] = NextPreprocessorEntityID;
Richard Smith66a81862015-05-04 02:25:31 +00002632
Douglas Gregor92a96f52011-02-08 21:58:10 +00002633 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002634 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2635 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002636 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002637
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002638 if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002639 Record.push_back(ME->isBuiltinMacro());
2640 if (ME->isBuiltinMacro())
2641 AddIdentifierRef(ME->getName(), Record);
2642 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002643 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002644 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002645 continue;
2646 }
2647
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002648 if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002649 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002650 Record.push_back(ID->getFileName().size());
2651 Record.push_back(ID->wasInQuotes());
2652 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002653 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002654 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002655 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002656 // Check that the FileEntry is not null because it was not resolved and
2657 // we create a PCH even with compiler errors.
2658 if (ID->getFile())
2659 Buffer += ID->getFile()->getName();
Mehdi Amini5ae4a852015-09-09 20:35:37 +00002660 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002661 continue;
2662 }
2663
2664 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2665 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002666 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002667
Douglas Gregoraae92242010-03-19 21:51:54 +00002668 // Write the offsets table for the preprocessing record.
2669 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002670 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2671
Douglas Gregoraae92242010-03-19 21:51:54 +00002672 // Write the offsets table for identifier IDs.
2673 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002674
David Blaikieb44f0bf2017-01-04 22:36:43 +00002675 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002676 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002678 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00002679 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002680
Mehdi Amini57a41912015-09-10 01:46:39 +00002681 RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2682 FirstPreprocessorEntityID -
2683 NUM_PREDEF_PP_ENTITY_IDS};
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002684 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00002685 bytes(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002686 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002687}
2688
Richard Smith386bb072015-08-18 23:42:23 +00002689unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
Richard Smith080056b2015-08-18 21:53:42 +00002690 if (!Mod)
2691 return 0;
2692
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002693 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2694 if (Known != SubmoduleIDs.end())
2695 return Known->second;
Richard Smith386bb072015-08-18 23:42:23 +00002696
Richard Smithdc1f0422016-07-20 19:10:16 +00002697 auto *Top = Mod->getTopLevelModule();
2698 if (Top != WritingModule &&
Bruno Cardoso Lopes17da34d2017-02-07 21:54:57 +00002699 (getLangOpts().CompilingPCH ||
2700 !Top->fullModuleNameIs(StringRef(getLangOpts().CurrentModule))))
Richard Smith386bb072015-08-18 23:42:23 +00002701 return 0;
2702
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002703 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2704}
2705
Richard Smith386bb072015-08-18 23:42:23 +00002706unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2707 // FIXME: This can easily happen, if we have a reference to a submodule that
2708 // did not result in us loading a module file for that submodule. For
2709 // instance, a cross-top-level-module 'conflict' declaration will hit this.
2710 unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2711 assert((ID || !Mod) &&
2712 "asked for module ID for non-local, non-imported module");
2713 return ID;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002714}
2715
Douglas Gregor253eefe2011-12-01 00:59:36 +00002716/// \brief Compute the number of modules within the given tree (including the
2717/// given module).
2718static unsigned getNumberOfModules(Module *Mod) {
2719 unsigned ChildModules = 0;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002720 for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002721 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002722 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002723
2724 return ChildModules + 1;
2725}
2726
Douglas Gregorde3ef502011-11-30 23:21:26 +00002727void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor69021972011-11-30 17:33:56 +00002728 // Enter the submodule description block.
Richard Smith202210b2014-10-24 20:23:01 +00002729 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor69021972011-11-30 17:33:56 +00002730
2731 // Write the abbreviations needed for the submodules block.
2732 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00002733
David Blaikieb44f0bf2017-01-04 22:36:43 +00002734 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002735 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
Richard Smithdd8b5332017-09-04 05:37:53 +00002738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Kind
Douglas Gregor69021972011-11-30 17:33:56 +00002739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002741 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2742 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002748 unsigned DefinitionAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002749
David Blaikieb44f0bf2017-01-04 22:36:43 +00002750 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002751 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002753 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor69021972011-11-30 17:33:56 +00002754
David Blaikieb44f0bf2017-01-04 22:36:43 +00002755 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor69021972011-11-30 17:33:56 +00002756 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002758 unsigned HeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002759
David Blaikieb44f0bf2017-01-04 22:36:43 +00002760 Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002761 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002763 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002764
David Blaikieb44f0bf2017-01-04 22:36:43 +00002765 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002766 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002768 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor524e33e2011-12-08 19:11:24 +00002769
David Blaikieb44f0bf2017-01-04 22:36:43 +00002770 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002771 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
David Blaikieb44f0bf2017-01-04 22:36:43 +00002774 unsigned RequiresAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002775
David Blaikieb44f0bf2017-01-04 22:36:43 +00002776 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor59527662012-10-15 06:28:11 +00002777 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002779 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor59527662012-10-15 06:28:11 +00002780
David Blaikieb44f0bf2017-01-04 22:36:43 +00002781 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith306d8922014-10-22 23:50:56 +00002782 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002784 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith306d8922014-10-22 23:50:56 +00002785
David Blaikieb44f0bf2017-01-04 22:36:43 +00002786 Abbrev = std::make_shared<BitCodeAbbrev>();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002787 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002789 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002790
David Blaikieb44f0bf2017-01-04 22:36:43 +00002791 Abbrev = std::make_shared<BitCodeAbbrev>();
Richard Smith202210b2014-10-24 20:23:01 +00002792 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002794 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Richard Smith202210b2014-10-24 20:23:01 +00002795
David Blaikieb44f0bf2017-01-04 22:36:43 +00002796 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002797 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002800 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002801
David Blaikieb44f0bf2017-01-04 22:36:43 +00002802 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002803 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2804 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
David Blaikieb44f0bf2017-01-04 22:36:43 +00002805 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002806
David Blaikieb44f0bf2017-01-04 22:36:43 +00002807 Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregorfb912652013-03-20 21:10:35 +00002808 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
David Blaikieb44f0bf2017-01-04 22:36:43 +00002811 unsigned ConflictAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorfb912652013-03-20 21:10:35 +00002812
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002813 Abbrev = std::make_shared<BitCodeAbbrev>();
2814 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXPORT_AS));
2815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2816 unsigned ExportAsAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
2817
Douglas Gregor253eefe2011-12-01 00:59:36 +00002818 // Write the submodule metadata block.
Richard Smith145e15a2017-04-24 23:12:30 +00002819 RecordData::value_type Record[] = {
2820 getNumberOfModules(WritingModule),
Richard Smithdd8b5332017-09-04 05:37:53 +00002821 FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS};
Douglas Gregor253eefe2011-12-01 00:59:36 +00002822 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2823
Douglas Gregor69021972011-11-30 17:33:56 +00002824 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002825 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002826 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002827 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002828 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002829 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002830 unsigned ID = getSubmoduleID(Mod);
Mehdi Amini57a41912015-09-10 01:46:39 +00002831
2832 uint64_t ParentID = 0;
Douglas Gregor69021972011-11-30 17:33:56 +00002833 if (Mod->Parent) {
2834 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
Mehdi Amini57a41912015-09-10 01:46:39 +00002835 ParentID = SubmoduleIDs[Mod->Parent];
Douglas Gregor69021972011-11-30 17:33:56 +00002836 }
Mehdi Amini57a41912015-09-10 01:46:39 +00002837
2838 // Emit the definition of the block.
2839 {
David Blaikie9ffe5a32017-01-30 05:00:26 +00002840 RecordData::value_type Record[] = {SUBMODULE_DEFINITION,
2841 ID,
2842 ParentID,
Simon Pilgrimf331a662017-09-04 10:54:39 +00002843 (RecordData::value_type)Mod->Kind,
David Blaikie9ffe5a32017-01-30 05:00:26 +00002844 Mod->IsFramework,
2845 Mod->IsExplicit,
2846 Mod->IsSystem,
2847 Mod->IsExternC,
2848 Mod->InferSubmodules,
2849 Mod->InferExplicitSubmodules,
2850 Mod->InferExportWildcard,
David Blaikiee6b7c282017-04-11 20:46:34 +00002851 Mod->ConfigMacrosExhaustive};
Mehdi Amini57a41912015-09-10 01:46:39 +00002852 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2853 }
2854
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002855 // Emit the requirements.
Richard Smith080056b2015-08-18 21:53:42 +00002856 for (const auto &R : Mod->Requirements) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002857 RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
Richard Smith080056b2015-08-18 21:53:42 +00002858 Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002859 }
2860
Douglas Gregor69021972011-11-30 17:33:56 +00002861 // Emit the umbrella header, if there is one.
Richard Smith2b63d152015-05-16 02:28:53 +00002862 if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002863 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
Richard Smith2b63d152015-05-16 02:28:53 +00002864 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2865 UmbrellaHeader.NameAsWritten);
2866 } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002867 RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2868 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
Richard Smith2b63d152015-05-16 02:28:53 +00002869 UmbrellaDir.NameAsWritten);
Douglas Gregor69021972011-11-30 17:33:56 +00002870 }
Richard Smith306d8922014-10-22 23:50:56 +00002871
Douglas Gregor69021972011-11-30 17:33:56 +00002872 // Emit the headers.
Richard Smith202210b2014-10-24 20:23:01 +00002873 struct {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002874 unsigned RecordKind;
Richard Smith202210b2014-10-24 20:23:01 +00002875 unsigned Abbrev;
Richard Smith3c1a41a2014-12-02 00:08:08 +00002876 Module::HeaderKind HeaderKind;
Richard Smith202210b2014-10-24 20:23:01 +00002877 } HeaderLists[] = {
Richard Smith3c1a41a2014-12-02 00:08:08 +00002878 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2879 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2880 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Richard Smith202210b2014-10-24 20:23:01 +00002881 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Richard Smith3c1a41a2014-12-02 00:08:08 +00002882 Module::HK_PrivateTextual},
2883 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Richard Smith202210b2014-10-24 20:23:01 +00002884 };
2885 for (auto &HL : HeaderLists) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002886 RecordData::value_type Record[] = {HL.RecordKind};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002887 for (auto &H : Mod->Headers[HL.HeaderKind])
2888 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2889 }
2890
2891 // Emit the top headers.
2892 {
2893 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
Mehdi Amini57a41912015-09-10 01:46:39 +00002894 RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
Richard Smith3c1a41a2014-12-02 00:08:08 +00002895 for (auto *H : TopHeaders)
2896 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002897 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002898
2899 // Emit the imports.
2900 if (!Mod->Imports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002901 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002902 for (auto *I : Mod->Imports)
2903 Record.push_back(getSubmoduleID(I));
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002904 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2905 }
2906
Douglas Gregor24bb9232011-12-02 18:58:38 +00002907 // Emit the exports.
2908 if (!Mod->Exports.empty()) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002909 RecordData Record;
Richard Smith080056b2015-08-18 21:53:42 +00002910 for (const auto &E : Mod->Exports) {
2911 // FIXME: This may fail; we don't require that all exported modules
2912 // are local or imported.
2913 Record.push_back(getSubmoduleID(E.getPointer()));
2914 Record.push_back(E.getInt());
Douglas Gregor24bb9232011-12-02 18:58:38 +00002915 }
2916 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2917 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002918
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002919 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2920 // Might be unnecessary as use declarations are only used to build the
2921 // module itself.
2922
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002923 // Emit the link libraries.
Richard Smith080056b2015-08-18 21:53:42 +00002924 for (const auto &LL : Mod->LinkLibraries) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002925 RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2926 LL.IsFramework};
Richard Smith080056b2015-08-18 21:53:42 +00002927 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002928 }
2929
Douglas Gregorfb912652013-03-20 21:10:35 +00002930 // Emit the conflicts.
Richard Smith080056b2015-08-18 21:53:42 +00002931 for (const auto &C : Mod->Conflicts) {
Richard Smith080056b2015-08-18 21:53:42 +00002932 // FIXME: This may fail; we don't require that all conflicting modules
2933 // are local or imported.
Mehdi Amini57a41912015-09-10 01:46:39 +00002934 RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2935 getSubmoduleID(C.Other)};
Richard Smith080056b2015-08-18 21:53:42 +00002936 Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
Douglas Gregorfb912652013-03-20 21:10:35 +00002937 }
2938
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002939 // Emit the configuration macros.
Richard Smith080056b2015-08-18 21:53:42 +00002940 for (const auto &CM : Mod->ConfigMacros) {
Mehdi Amini57a41912015-09-10 01:46:39 +00002941 RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
Richard Smith080056b2015-08-18 21:53:42 +00002942 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002943 }
2944
Richard Smithdc1f0422016-07-20 19:10:16 +00002945 // Emit the initializers, if any.
2946 RecordData Inits;
2947 for (Decl *D : Context->getModuleInitializers(Mod))
2948 Inits.push_back(GetDeclRef(D));
2949 if (!Inits.empty())
2950 Stream.EmitRecord(SUBMODULE_INITIALIZERS, Inits);
2951
Douglas Gregorf0b11de2017-09-14 23:38:44 +00002952 // Emit the name of the re-exported module, if any.
2953 if (!Mod->ExportAsModule.empty()) {
2954 RecordData::value_type Record[] = {SUBMODULE_EXPORT_AS};
2955 Stream.EmitRecordWithBlob(ExportAsAbbrev, Record, Mod->ExportAsModule);
2956 }
2957
Douglas Gregor69021972011-11-30 17:33:56 +00002958 // Queue up the submodules of this module.
Richard Smith080056b2015-08-18 21:53:42 +00002959 for (auto *M : Mod->submodules())
2960 Q.push(M);
Douglas Gregor69021972011-11-30 17:33:56 +00002961 }
2962
2963 Stream.ExitBlock();
Richard Smith23d8d032015-05-14 00:45:20 +00002964
Richard Smith23d8d032015-05-14 00:45:20 +00002965 assert((NextSubmoduleID - FirstSubmoduleID ==
2966 getNumberOfModules(WritingModule)) &&
2967 "Wrong # of submodules; found a reference to a non-local, "
2968 "non-imported submodule?");
Douglas Gregor69021972011-11-30 17:33:56 +00002969}
2970
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002971void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2972 bool isModule) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002973 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2974 DiagStateIDMap;
2975 unsigned CurrID = 0;
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002976 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002977
Richard Smithe37391c2017-05-03 00:28:49 +00002978 auto EncodeDiagStateFlags =
2979 [](const DiagnosticsEngine::DiagState *DS) -> unsigned {
2980 unsigned Result = (unsigned)DS->ExtBehavior;
2981 for (unsigned Val :
Daniel Jasperba9aefc2017-05-03 07:48:27 +00002982 {(unsigned)DS->IgnoreAllWarnings, (unsigned)DS->EnableAllWarnings,
2983 (unsigned)DS->WarningsAsErrors, (unsigned)DS->ErrorsAsFatal,
2984 (unsigned)DS->SuppressSystemWarnings})
Richard Smithe37391c2017-05-03 00:28:49 +00002985 Result = (Result << 1) | Val;
2986 return Result;
2987 };
2988
2989 unsigned Flags = EncodeDiagStateFlags(Diag.DiagStatesByLoc.FirstDiagState);
2990 Record.push_back(Flags);
2991
Richard Smithd230de22017-01-26 01:01:01 +00002992 auto AddDiagState = [&](const DiagnosticsEngine::DiagState *State,
2993 bool IncludeNonPragmaStates) {
Richard Smithe37391c2017-05-03 00:28:49 +00002994 // Ensure that the diagnostic state wasn't modified since it was created.
2995 // We will not correctly round-trip this information otherwise.
2996 assert(Flags == EncodeDiagStateFlags(State) &&
2997 "diag state flags vary in single AST file");
2998
Richard Smithd230de22017-01-26 01:01:01 +00002999 unsigned &DiagStateID = DiagStateIDMap[State];
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003000 Record.push_back(DiagStateID);
Richard Smithd230de22017-01-26 01:01:01 +00003001
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003002 if (DiagStateID == 0) {
3003 DiagStateID = ++CurrID;
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003004
3005 // Add a placeholder for the number of mappings.
3006 auto SizeIdx = Record.size();
3007 Record.emplace_back();
Richard Smithd230de22017-01-26 01:01:01 +00003008 for (const auto &I : *State) {
3009 if (I.second.isPragma() || IncludeNonPragmaStates) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003010 Record.push_back(I.first);
Richard Smithe37391c2017-05-03 00:28:49 +00003011 Record.push_back(I.second.serialize());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00003012 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003013 }
Duncan P. N. Exon Smith3cb183b2017-03-14 19:31:27 +00003014 // Update the placeholder.
3015 Record[SizeIdx] = (Record.size() - SizeIdx) / 2;
Richard Smithd230de22017-01-26 01:01:01 +00003016 }
3017 };
3018
3019 AddDiagState(Diag.DiagStatesByLoc.FirstDiagState, isModule);
Richard Smithd230de22017-01-26 01:01:01 +00003020
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003021 // Reserve a spot for the number of locations with state transitions.
3022 auto NumLocationsIdx = Record.size();
3023 Record.emplace_back();
3024
3025 // Emit the state transitions.
3026 unsigned NumLocations = 0;
Richard Smithd230de22017-01-26 01:01:01 +00003027 for (auto &FileIDAndFile : Diag.DiagStatesByLoc.Files) {
3028 if (!FileIDAndFile.first.isValid() ||
3029 !FileIDAndFile.second.HasLocalTransitions)
3030 continue;
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003031 ++NumLocations;
Richard Smithd230de22017-01-26 01:01:01 +00003032 AddSourceLocation(Diag.SourceMgr->getLocForStartOfFile(FileIDAndFile.first),
3033 Record);
3034 Record.push_back(FileIDAndFile.second.StateTransitions.size());
3035 for (auto &StatePoint : FileIDAndFile.second.StateTransitions) {
3036 Record.push_back(StatePoint.Offset);
3037 AddDiagState(StatePoint.State, false);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003038 }
3039 }
3040
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003041 // Backpatch the number of locations.
3042 Record[NumLocationsIdx] = NumLocations;
3043
3044 // Emit CurDiagStateLoc. Do it last in order to match source order.
Duncan P. N. Exon Smith900f8172017-04-12 03:58:58 +00003045 //
3046 // This also protects against a hypothetical corner case with simulating
3047 // -Werror settings for implicit modules in the ASTReader, where reading
3048 // CurDiagState out of context could change whether warning pragmas are
3049 // treated as errors.
Duncan P. N. Exon Smitha351c102017-04-12 03:45:32 +00003050 AddSourceLocation(Diag.DiagStatesByLoc.CurDiagStateLoc, Record);
3051 AddDiagState(Diag.DiagStatesByLoc.CurDiagState, false);
3052
Duncan P. N. Exon Smithf2435b92017-04-12 02:31:17 +00003053 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003054}
3055
Douglas Gregorc5046832009-04-27 18:38:38 +00003056//===----------------------------------------------------------------------===//
3057// Type Serialization
3058//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00003059
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003060/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003061void ASTWriter::WriteType(QualType T) {
Richard Smith290d8012016-04-06 17:06:00 +00003062 TypeIdx &IdxRef = TypeIdxs[T];
3063 if (IdxRef.getIndex() == 0) // we haven't seen this type before.
3064 IdxRef = TypeIdx(NextTypeID++);
3065 TypeIdx Idx = IdxRef;
Mike Stump11289f42009-09-09 15:08:12 +00003066
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003067 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00003068
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003069 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00003070
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003071 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003072 ASTTypeWriter W(*this, Record);
Richard Smith290d8012016-04-06 17:06:00 +00003073 W.Visit(T);
3074 uint64_t Offset = W.Emit();
John McCall8ccfcb52009-09-24 19:53:00 +00003075
Richard Smith290d8012016-04-06 17:06:00 +00003076 // Record the offset for this type.
3077 unsigned Index = Idx.getIndex() - FirstTypeID;
3078 if (TypeOffsets.size() == Index)
3079 TypeOffsets.push_back(Offset);
3080 else if (TypeOffsets.size() < Index) {
3081 TypeOffsets.resize(Index + 1);
3082 TypeOffsets[Index] = Offset;
John McCall8ccfcb52009-09-24 19:53:00 +00003083 } else {
Richard Smith290d8012016-04-06 17:06:00 +00003084 llvm_unreachable("Types emitted in wrong order");
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003085 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003086}
3087
Douglas Gregorc5046832009-04-27 18:38:38 +00003088//===----------------------------------------------------------------------===//
3089// Declaration Serialization
3090//===----------------------------------------------------------------------===//
3091
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003092/// \brief Write the block containing all of the declaration IDs
3093/// lexically declared within the given DeclContext.
3094///
3095/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
3096/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003097uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003098 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003099 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003100 return 0;
3101
Douglas Gregor8f45df52009-04-16 22:23:12 +00003102 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smith82f8fcd2015-08-06 22:07:25 +00003103 SmallVector<uint32_t, 128> KindDeclPairs;
3104 for (const auto *D : DC->decls()) {
3105 KindDeclPairs.push_back(D->getKind());
3106 KindDeclPairs.push_back(GetDeclRef(D));
3107 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003108
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003109 ++NumLexicalDeclContexts;
Mehdi Amini57a41912015-09-10 01:46:39 +00003110 RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
Richard Smith82f8fcd2015-08-06 22:07:25 +00003111 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
3112 bytes(KindDeclPairs));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003113 return Offset;
3114}
3115
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003116void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003117 using namespace llvm;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003118
3119 // Write the type offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003120 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003121 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00003123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003125 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003126 {
3127 RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
3128 FirstTypeID - NUM_PREDEF_TYPE_IDS};
3129 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
3130 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003131
3132 // Write the declaration offsets array
David Blaikieb44f0bf2017-01-04 22:36:43 +00003133 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003134 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003135 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00003136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
David Blaikieb44f0bf2017-01-04 22:36:43 +00003138 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003139 {
3140 RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
3141 FirstDeclID - NUM_PREDEF_DECL_IDS};
3142 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
3143 }
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003144}
3145
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003146void ASTWriter::WriteFileDeclIDsMap() {
3147 using namespace llvm;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003148
Chandler Carruthb306d732015-03-27 00:31:20 +00003149 SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
3150 FileDeclIDs.begin(), FileDeclIDs.end());
3151 std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
3152 llvm::less_first());
3153
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003154 // Join the vectors of DeclIDs from all files.
Chandler Carruthb306d732015-03-27 00:31:20 +00003155 SmallVector<DeclID, 256> FileGroupedDeclIDs;
3156 for (auto &FileDeclEntry : SortedFileDeclIDs) {
3157 DeclIDInFileInfo &Info = *FileDeclEntry.second;
3158 Info.FirstDeclIndex = FileGroupedDeclIDs.size();
3159 for (auto &LocDeclEntry : Info.DeclIDs)
3160 FileGroupedDeclIDs.push_back(LocDeclEntry.second);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003161 }
3162
David Blaikieb44f0bf2017-01-04 22:36:43 +00003163 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003164 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00003165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003167 unsigned AbbrevCode = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00003168 RecordData::value_type Record[] = {FILE_SORTED_DECLS,
3169 FileGroupedDeclIDs.size()};
Reid Kleckner012665e2015-05-21 00:13:09 +00003170 Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003171}
3172
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003173void ASTWriter::WriteComments() {
3174 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00003175 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003176 RecordData Record;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003177 for (const auto *I : RawComments) {
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003178 Record.clear();
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003179 AddSourceRange(I->getSourceRange(), Record);
3180 Record.push_back(I->getKind());
3181 Record.push_back(I->isTrailingComment());
3182 Record.push_back(I->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003183 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
3184 }
3185 Stream.ExitBlock();
3186}
3187
Douglas Gregorc5046832009-04-27 18:38:38 +00003188//===----------------------------------------------------------------------===//
3189// Global Method Pool and Selector Serialization
3190//===----------------------------------------------------------------------===//
3191
Douglas Gregore84a9da2009-04-20 20:36:09 +00003192namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003193
Douglas Gregorc78d3462009-04-24 21:10:55 +00003194// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003195class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003196 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003197
3198public:
3199 typedef Selector key_type;
3200 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003201
Sebastian Redl834bb972010-08-04 17:20:04 +00003202 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00003203 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00003204 ObjCMethodList Instance, Factory;
3205 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00003206 typedef const data_type& data_type_ref;
3207
Justin Bogner25463f12014-04-18 20:27:24 +00003208 typedef unsigned hash_value_type;
3209 typedef unsigned offset_type;
3210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003211 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00003212
Justin Bogner25463f12014-04-18 20:27:24 +00003213 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00003214 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003215 }
Mike Stump11289f42009-09-09 15:08:12 +00003216
3217 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003218 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00003219 data_type_ref Methods) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003220 using namespace llvm::support;
3221 endian::Writer<little> LE(Out);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003222 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Justin Bognere1c147c2014-03-28 22:03:19 +00003223 LE.write<uint16_t>(KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00003224 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
3225 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003226 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003227 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003228 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00003229 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003230 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003231 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003232 DataLen += 4;
Justin Bognere1c147c2014-03-28 22:03:19 +00003233 LE.write<uint16_t>(DataLen);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003234 return std::make_pair(KeyLen, DataLen);
3235 }
Mike Stump11289f42009-09-09 15:08:12 +00003236
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003237 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003238 using namespace llvm::support;
3239 endian::Writer<little> LE(Out);
Mike Stump11289f42009-09-09 15:08:12 +00003240 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00003241 assert((Start >> 32) == 0 && "Selector key offset too large");
3242 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003243 unsigned N = Sel.getNumArgs();
Justin Bognere1c147c2014-03-28 22:03:19 +00003244 LE.write<uint16_t>(N);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003245 if (N == 0)
3246 N = 1;
3247 for (unsigned I = 0; I != N; ++I)
Justin Bognere1c147c2014-03-28 22:03:19 +00003248 LE.write<uint32_t>(
3249 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003250 }
Mike Stump11289f42009-09-09 15:08:12 +00003251
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003252 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003253 data_type_ref Methods, unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003254 using namespace llvm::support;
3255 endian::Writer<little> LE(Out);
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003256 uint64_t Start = Out.tell(); (void)Start;
Justin Bognere1c147c2014-03-28 22:03:19 +00003257 LE.write<uint32_t>(Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003258 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003259 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003260 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003261 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003262 ++NumInstanceMethods;
3263
3264 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00003265 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003266 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003267 if (Method->getMethod())
Douglas Gregorc78d3462009-04-24 21:10:55 +00003268 ++NumFactoryMethods;
3269
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003270 unsigned InstanceBits = Methods.Instance.getBits();
3271 assert(InstanceBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003272 unsigned InstanceHasMoreThanOneDeclBit =
3273 Methods.Instance.hasMoreThanOneDecl();
3274 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
3275 (InstanceHasMoreThanOneDeclBit << 2) |
3276 InstanceBits;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003277 unsigned FactoryBits = Methods.Factory.getBits();
3278 assert(FactoryBits < 4);
Nico Weberff4b35e2014-12-27 22:14:15 +00003279 unsigned FactoryHasMoreThanOneDeclBit =
3280 Methods.Factory.hasMoreThanOneDecl();
3281 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
3282 (FactoryHasMoreThanOneDeclBit << 2) |
3283 FactoryBits;
3284 LE.write<uint16_t>(FullInstanceBits);
3285 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00003286 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003287 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003288 if (Method->getMethod())
3289 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl834bb972010-08-04 17:20:04 +00003290 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003291 Method = Method->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00003292 if (Method->getMethod())
3293 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003294
3295 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00003296 }
3297};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003298
Douglas Gregorc78d3462009-04-24 21:10:55 +00003299} // end anonymous namespace
3300
Sebastian Redla19a67f2010-08-03 21:58:15 +00003301/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003302///
3303/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00003304/// in an on-disk hash table indexed by the selector. The hash table also
3305/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003306void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00003307 using namespace llvm;
3308
Sebastian Redla19a67f2010-08-03 21:58:15 +00003309 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00003310 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00003311 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003312 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003313 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00003314 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003315 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003316 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00003317
Sebastian Redla19a67f2010-08-03 21:58:15 +00003318 // Create the on-disk hash table representation. We walk through every
3319 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00003320 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003321 for (auto &SelectorAndID : SelectorIDs) {
3322 Selector S = SelectorAndID.first;
3323 SelectorID ID = SelectorAndID.second;
Sebastian Redla19a67f2010-08-03 21:58:15 +00003324 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003325 ASTMethodPoolTrait::data_type Data = {
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003326 ID,
Sebastian Redl834bb972010-08-04 17:20:04 +00003327 ObjCMethodList(),
3328 ObjCMethodList()
3329 };
3330 if (F != SemaRef.MethodPool.end()) {
3331 Data.Instance = F->second.first;
3332 Data.Factory = F->second.second;
3333 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003334 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00003335 // changed.
Chandler Carruthacbbeb92015-03-27 00:47:43 +00003336 if (Chain && ID < FirstSelectorID) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003337 // Selector already exists. Did it change?
3338 bool changed = false;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003339 for (ObjCMethodList *M = &Data.Instance;
3340 !changed && M && M->getMethod(); M = M->getNext()) {
3341 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003342 changed = true;
3343 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00003344 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00003345 M = M->getNext()) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00003346 if (!M->getMethod()->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00003347 changed = true;
3348 }
3349 if (!changed)
3350 continue;
Nico Weber2e0c8f72014-12-27 03:58:08 +00003351 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00003352 // A new method pool entry.
3353 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00003354 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003355 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003356 }
3357
Douglas Gregorc78d3462009-04-24 21:10:55 +00003358 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003359 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00003360 uint32_t BucketOffset;
3361 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003362 using namespace llvm::support;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003363 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003364 llvm::raw_svector_ostream Out(MethodPool);
3365 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003366 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003367 BucketOffset = Generator.Emit(Out, Trait);
3368 }
3369
3370 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003371 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003372 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003376 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregorc78d3462009-04-24 21:10:55 +00003377
Douglas Gregor95c13f52009-04-25 17:48:32 +00003378 // Write the method pool
Mehdi Amini57a41912015-09-10 01:46:39 +00003379 {
3380 RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3381 NumTableEntries};
3382 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3383 }
Douglas Gregor95c13f52009-04-25 17:48:32 +00003384
3385 // Create a blob abbreviation for the selector table offsets.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003386 Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003387 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00003390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003391 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor95c13f52009-04-25 17:48:32 +00003392
3393 // Write the selector offsets table.
Mehdi Amini57a41912015-09-10 01:46:39 +00003394 {
3395 RecordData::value_type Record[] = {
3396 SELECTOR_OFFSETS, SelectorOffsets.size(),
3397 FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3398 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3399 bytes(SelectorOffsets));
3400 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00003401 }
3402}
3403
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003404/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003405void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003406 using namespace llvm;
3407 if (SemaRef.ReferencedSelectors.empty())
3408 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00003409
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003410 RecordData Record;
Richard Smithe64e7452016-04-18 21:54:58 +00003411 ASTRecordWriter Writer(*this, Record);
Sebastian Redlada023c2010-08-04 20:40:17 +00003412
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003413 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00003414 // very tricky to fix, and given that @selector shouldn't really appear in
3415 // headers, probably not worth it. It's not a correctness issue.
Chandler Carruth12c8f652015-03-27 00:55:05 +00003416 for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3417 Selector Sel = SelectorAndLocation.first;
3418 SourceLocation Loc = SelectorAndLocation.second;
Richard Smithe64e7452016-04-18 21:54:58 +00003419 Writer.AddSelectorRef(Sel);
3420 Writer.AddSourceLocation(Loc);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003421 }
Richard Smithe64e7452016-04-18 21:54:58 +00003422 Writer.Emit(REFERENCED_SELECTOR_POOL);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003423}
3424
Douglas Gregorc5046832009-04-27 18:38:38 +00003425//===----------------------------------------------------------------------===//
3426// Identifier Table Serialization
3427//===----------------------------------------------------------------------===//
3428
Richard Smithb3761912015-02-05 23:08:52 +00003429/// Determine the declaration that should be put into the name lookup table to
3430/// represent the given declaration in this module. This is usually D itself,
3431/// but if D was imported and merged into a local declaration, we want the most
3432/// recent local declaration instead. The chosen declaration will be the most
3433/// recent declaration in any module that imports this one.
3434static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3435 NamedDecl *D) {
3436 if (!LangOpts.Modules || !D->isFromASTFile())
3437 return D;
3438
3439 if (Decl *Redecl = D->getPreviousDecl()) {
3440 // For Redeclarable decls, a prior declaration might be local.
3441 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
Richard Smithd49941b2016-04-26 23:40:43 +00003442 // If we find a local decl, we're done.
3443 if (!Redecl->isFromASTFile()) {
3444 // Exception: in very rare cases (for injected-class-names), not all
3445 // redeclarations are in the same semantic context. Skip ones in a
3446 // different context. They don't go in this lookup table at all.
3447 if (!Redecl->getDeclContext()->getRedeclContext()->Equals(
3448 D->getDeclContext()->getRedeclContext()))
3449 continue;
Richard Smithb3761912015-02-05 23:08:52 +00003450 return cast<NamedDecl>(Redecl);
Richard Smithd49941b2016-04-26 23:40:43 +00003451 }
3452
Richard Smithfe620d22015-03-05 23:24:12 +00003453 // If we find a decl from a (chained-)PCH stop since we won't find a
Richard Smithb3761912015-02-05 23:08:52 +00003454 // local one.
Richard Smithd49941b2016-04-26 23:40:43 +00003455 if (Redecl->getOwningModuleID() == 0)
Richard Smithb3761912015-02-05 23:08:52 +00003456 break;
3457 }
3458 } else if (Decl *First = D->getCanonicalDecl()) {
3459 // For Mergeable decls, the first decl might be local.
3460 if (!First->isFromASTFile())
3461 return cast<NamedDecl>(First);
3462 }
3463
3464 // All declarations are imported. Our most recent declaration will also be
3465 // the most recent one in anyone who imports us.
3466 return D;
3467}
3468
Douglas Gregorc78d3462009-04-24 21:10:55 +00003469namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003470
Richard Smithcf97cf62015-04-19 01:34:23 +00003471class ASTIdentifierTableTrait {
3472 ASTWriter &Writer;
3473 Preprocessor &PP;
3474 IdentifierResolver &IdResolver;
Richard Smith9c254182015-07-19 21:41:12 +00003475 bool IsModule;
Richard Smitha534a312015-07-21 23:54:07 +00003476 bool NeedDecls;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003477 ASTWriter::RecordData *InterestingIdentifierOffsets;
Richard Smithcf97cf62015-04-19 01:34:23 +00003478
3479 /// \brief Determines whether this is an "interesting" identifier that needs a
3480 /// full IdentifierInfo structure written into the hash table. Notably, this
3481 /// doesn't check whether the name has macros defined; use PublicMacroIterator
3482 /// to check that.
Richard Smith9c254182015-07-19 21:41:12 +00003483 bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
Richard Smithd7329392015-04-21 21:46:32 +00003484 if (MacroOffset ||
3485 II->isPoisoned() ||
Richard Smith9c254182015-07-19 21:41:12 +00003486 (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
Richard Smithcf97cf62015-04-19 01:34:23 +00003487 II->hasRevertedTokenIDToIdentifier() ||
Richard Smitha534a312015-07-21 23:54:07 +00003488 (NeedDecls && II->getFETokenInfo<void>()))
Richard Smithcf97cf62015-04-19 01:34:23 +00003489 return true;
3490
3491 return false;
3492 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003493
Douglas Gregore84a9da2009-04-20 20:36:09 +00003494public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003495 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003496 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003497
Sebastian Redl539c5062010-08-18 23:57:32 +00003498 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003499 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003500
Justin Bogner25463f12014-04-18 20:27:24 +00003501 typedef unsigned hash_value_type;
3502 typedef unsigned offset_type;
3503
Richard Smithd7329392015-04-21 21:46:32 +00003504 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
Richard Smith33e0f7e2015-07-22 02:08:40 +00003505 IdentifierResolver &IdResolver, bool IsModule,
3506 ASTWriter::RecordData *InterestingIdentifierOffsets)
Richard Smitha534a312015-07-21 23:54:07 +00003507 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
Richard Smith33e0f7e2015-07-22 02:08:40 +00003508 NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3509 InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
Douglas Gregore84a9da2009-04-20 20:36:09 +00003510
Richard Smithd79514e2016-02-05 19:03:40 +00003511 bool needDecls() const { return NeedDecls; }
3512
Justin Bogner25463f12014-04-18 20:27:24 +00003513 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003514 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003515 }
Mike Stump11289f42009-09-09 15:08:12 +00003516
Richard Smith33e0f7e2015-07-22 02:08:40 +00003517 bool isInterestingIdentifier(const IdentifierInfo *II) {
3518 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3519 return isInterestingIdentifier(II, MacroOffset);
3520 }
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003521
Richard Smith9c254182015-07-19 21:41:12 +00003522 bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3523 return isInterestingIdentifier(II, 0);
3524 }
3525
Mike Stump11289f42009-09-09 15:08:12 +00003526 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003527 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003528 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003529 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Richard Smithd7329392015-04-21 21:46:32 +00003530 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3531 if (isInterestingIdentifier(II, MacroOffset)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003532 DataLen += 2; // 2 bytes for builtin ID
3533 DataLen += 2; // 2 bytes for flags
Richard Smithd7329392015-04-21 21:46:32 +00003534 if (MacroOffset)
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003535 DataLen += 4; // MacroDirectives offset.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003536
Richard Smitha534a312015-07-21 23:54:07 +00003537 if (NeedDecls) {
3538 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3539 DEnd = IdResolver.end();
3540 D != DEnd; ++D)
3541 DataLen += 4;
3542 }
Douglas Gregor1d583f22009-04-28 21:18:29 +00003543 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003544 using namespace llvm::support;
3545 endian::Writer<little> LE(Out);
3546
Richard Smithdf8a8312015-03-13 04:05:01 +00003547 assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
Justin Bognere1c147c2014-03-28 22:03:19 +00003548 LE.write<uint16_t>(DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003549 // We emit the key length after the data length so that every
3550 // string is preceded by a 16-bit length. This matches the PTH
3551 // format for storing identifiers.
Justin Bognere1c147c2014-03-28 22:03:19 +00003552 LE.write<uint16_t>(KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003553 return std::make_pair(KeyLen, DataLen);
3554 }
Mike Stump11289f42009-09-09 15:08:12 +00003555
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003556 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003557 unsigned KeyLen) {
3558 // Record the location of the key data. This is used when generating
3559 // the mapping from persistent IDs to strings.
3560 Writer.SetIdentifierOffset(II, Out.tell());
Richard Smith33e0f7e2015-07-22 02:08:40 +00003561
3562 // Emit the offset of the key/data length information to the interesting
3563 // identifiers table if necessary.
3564 if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3565 InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3566
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003567 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003568 }
Mike Stump11289f42009-09-09 15:08:12 +00003569
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003570 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003571 IdentID ID, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003572 using namespace llvm::support;
3573 endian::Writer<little> LE(Out);
Richard Smithcf97cf62015-04-19 01:34:23 +00003574
Richard Smithd7329392015-04-21 21:46:32 +00003575 auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3576 if (!isInterestingIdentifier(II, MacroOffset)) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003577 LE.write<uint32_t>(ID << 1);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003578 return;
3579 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003580
Justin Bognere1c147c2014-03-28 22:03:19 +00003581 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003582 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3583 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Justin Bognere1c147c2014-03-28 22:03:19 +00003584 LE.write<uint16_t>(Bits);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003585 Bits = 0;
Richard Smithd7329392015-04-21 21:46:32 +00003586 bool HadMacroDefinition = MacroOffset != 0;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003587 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003588 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3589 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Richard Smith9c254182015-07-19 21:41:12 +00003590 Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003591 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003592 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Justin Bognere1c147c2014-03-28 22:03:19 +00003593 LE.write<uint16_t>(Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003594
Richard Smithd7329392015-04-21 21:46:32 +00003595 if (HadMacroDefinition)
3596 LE.write<uint32_t>(MacroOffset);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003597
Richard Smitha534a312015-07-21 23:54:07 +00003598 if (NeedDecls) {
3599 // Emit the declaration IDs in reverse order, because the
3600 // IdentifierResolver provides the declarations as they would be
3601 // visible (e.g., the function "stat" would come before the struct
3602 // "stat"), but the ASTReader adds declarations to the end of the list
3603 // (so we need to see the struct "stat" before the function "stat").
3604 // Only emit declarations that aren't from a chained PCH, though.
3605 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3606 IdResolver.end());
3607 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3608 DEnd = Decls.rend();
3609 D != DEnd; ++D)
3610 LE.write<uint32_t>(
3611 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3612 }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003613 }
3614};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003615
Douglas Gregore84a9da2009-04-20 20:36:09 +00003616} // end anonymous namespace
3617
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003618/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003619///
3620/// The identifier table consists of a blob containing string data
3621/// (the actual identifiers themselves) and a separate "offsets" index
3622/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003623void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3624 IdentifierResolver &IdResolver,
3625 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003626 using namespace llvm;
3627
Richard Smith33e0f7e2015-07-22 02:08:40 +00003628 RecordData InterestingIdents;
3629
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003630 // Create and write out the blob that contains the identifier
3631 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003632 {
Justin Bognerbb094f02014-04-18 19:57:06 +00003633 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Richard Smith33e0f7e2015-07-22 02:08:40 +00003634 ASTIdentifierTableTrait Trait(
3635 *this, PP, IdResolver, IsModule,
3636 (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00003637
Douglas Gregore6648fb2009-04-28 20:33:11 +00003638 // Look for any identifiers that were named while processing the
3639 // headers, but are otherwise not needed. We add these to the hash
3640 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003641 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003642 // file.
Chandler Carruth8440c982015-03-26 23:54:15 +00003643 SmallVector<const IdentifierInfo *, 128> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003644 for (const auto &ID : PP.getIdentifierTable())
3645 IIs.push_back(ID.second);
Chandler Carruth8440c982015-03-26 23:54:15 +00003646 // Sort the identifiers lexicographically before getting them references so
3647 // that their order is stable.
3648 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3649 for (const IdentifierInfo *II : IIs)
Richard Smith9c254182015-07-19 21:41:12 +00003650 if (Trait.isInterestingNonMacroIdentifier(II))
3651 getIdentifierRef(II);
Douglas Gregore6648fb2009-04-28 20:33:11 +00003652
Sebastian Redlff4a2952010-07-23 23:49:55 +00003653 // Create the on-disk hash table representation. We only store offsets
3654 // for identifiers that appear here for the first time.
3655 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003656 for (auto IdentIDPair : IdentifierIDs) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003657 auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
Chandler Carruth885e78c2015-03-24 21:18:10 +00003658 IdentID ID = IdentIDPair.second;
3659 assert(II && "NULL identifier in identifier table");
Vassil Vassilev262f41e2016-03-30 20:16:03 +00003660 // Write out identifiers if either the ID is local or the identifier has
3661 // changed since it was loaded.
3662 if (ID >= FirstIdentID || !Chain || !II->isFromAST()
3663 || II->hasChangedSinceDeserialization() ||
Richard Smithd79514e2016-02-05 19:03:40 +00003664 (Trait.needDecls() &&
3665 II->hasFETokenInfoChangedSinceDeserialization()))
Chandler Carruth885e78c2015-03-24 21:18:10 +00003666 Generator.insert(II, ID, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003667 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003668
Douglas Gregore84a9da2009-04-20 20:36:09 +00003669 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003670 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003671 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003672 {
Justin Bognere1c147c2014-03-28 22:03:19 +00003673 using namespace llvm::support;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003674 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003675 // Make sure that no bucket is at offset 0
Justin Bognere1c147c2014-03-28 22:03:19 +00003676 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003677 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003678 }
3679
3680 // Create a blob abbreviation
David Blaikieb44f0bf2017-01-04 22:36:43 +00003681 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003682 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003684 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003685 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003686
3687 // Write the identifier table
Mehdi Amini57a41912015-09-10 01:46:39 +00003688 RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
Yaron Keren92e1b622015-03-18 10:17:07 +00003689 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003690 }
3691
3692 // Write the offsets table for identifier IDs.
David Blaikieb44f0bf2017-01-04 22:36:43 +00003693 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Sebastian Redl539c5062010-08-18 23:57:32 +00003694 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003696 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00003698 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Douglas Gregor0e149972009-04-25 19:10:14 +00003699
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003700#ifndef NDEBUG
3701 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3702 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3703#endif
Mehdi Amini57a41912015-09-10 01:46:39 +00003704
3705 RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3706 IdentifierOffsets.size(),
3707 FirstIdentID - NUM_PREDEF_IDENT_IDS};
Douglas Gregor0e149972009-04-25 19:10:14 +00003708 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Reid Kleckner012665e2015-05-21 00:13:09 +00003709 bytes(IdentifierOffsets));
Richard Smith33e0f7e2015-07-22 02:08:40 +00003710
3711 // In C++, write the list of interesting identifiers (those that are
3712 // defined as macros, poisoned, or similar unusual things).
3713 if (!InterestingIdents.empty())
3714 Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003715}
3716
Douglas Gregorc5046832009-04-27 18:38:38 +00003717//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003718// DeclContext's Name Lookup Table Serialization
3719//===----------------------------------------------------------------------===//
3720
3721namespace {
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003722
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003723// Trait used for the on-disk hash table used in the method pool.
3724class ASTDeclContextNameLookupTrait {
3725 ASTWriter &Writer;
Richard Smithd88a7f12015-09-01 20:35:42 +00003726 llvm::SmallVector<DeclID, 64> DeclIDs;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003727
3728public:
Richard Smitha06c7e62015-08-26 23:55:49 +00003729 typedef DeclarationNameKey key_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003730 typedef key_type key_type_ref;
3731
Richard Smithd88a7f12015-09-01 20:35:42 +00003732 /// A start and end index into DeclIDs, representing a sequence of decls.
3733 typedef std::pair<unsigned, unsigned> data_type;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003734 typedef const data_type& data_type_ref;
3735
Justin Bogner25463f12014-04-18 20:27:24 +00003736 typedef unsigned hash_value_type;
3737 typedef unsigned offset_type;
3738
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003739 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3740
Richard Smithd88a7f12015-09-01 20:35:42 +00003741 template<typename Coll>
3742 data_type getData(const Coll &Decls) {
3743 unsigned Start = DeclIDs.size();
3744 for (NamedDecl *D : Decls) {
3745 DeclIDs.push_back(
3746 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3747 }
3748 return std::make_pair(Start, DeclIDs.size());
3749 }
3750
3751 data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3752 unsigned Start = DeclIDs.size();
3753 for (auto ID : FromReader)
3754 DeclIDs.push_back(ID);
3755 return std::make_pair(Start, DeclIDs.size());
3756 }
3757
3758 static bool EqualKey(key_type_ref a, key_type_ref b) {
3759 return a == b;
3760 }
3761
Richard Smitha06c7e62015-08-26 23:55:49 +00003762 hash_value_type ComputeHash(DeclarationNameKey Name) {
3763 return Name.getHash();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003764 }
3765
Richard Smithd88a7f12015-09-01 20:35:42 +00003766 void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3767 assert(Writer.hasChain() &&
3768 "have reference to loaded module file but no chain?");
3769
3770 using namespace llvm::support;
3771 endian::Writer<little>(Out)
3772 .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3773 }
3774
Richard Smitha06c7e62015-08-26 23:55:49 +00003775 std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3776 DeclarationNameKey Name,
3777 data_type_ref Lookup) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003778 using namespace llvm::support;
3779 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003780 unsigned KeyLen = 1;
Richard Smitha06c7e62015-08-26 23:55:49 +00003781 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003782 case DeclarationName::Identifier:
3783 case DeclarationName::ObjCZeroArgSelector:
3784 case DeclarationName::ObjCOneArgSelector:
3785 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003786 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003787 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003788 KeyLen += 4;
3789 break;
3790 case DeclarationName::CXXOperatorName:
3791 KeyLen += 1;
3792 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003793 case DeclarationName::CXXConstructorName:
3794 case DeclarationName::CXXDestructorName:
3795 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003796 case DeclarationName::CXXUsingDirective:
3797 break;
3798 }
Justin Bognere1c147c2014-03-28 22:03:19 +00003799 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003800
Richard Smithf02662d2015-07-30 03:17:16 +00003801 // 4 bytes for each DeclID.
Richard Smithd88a7f12015-09-01 20:35:42 +00003802 unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3803 assert(uint16_t(DataLen) == DataLen &&
3804 "too many decls for serialized lookup result");
Justin Bognere1c147c2014-03-28 22:03:19 +00003805 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003806
3807 return std::make_pair(KeyLen, DataLen);
3808 }
3809
Richard Smitha06c7e62015-08-26 23:55:49 +00003810 void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003811 using namespace llvm::support;
3812 endian::Writer<little> LE(Out);
Richard Smitha06c7e62015-08-26 23:55:49 +00003813 LE.write<uint8_t>(Name.getKind());
3814 switch (Name.getKind()) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003815 case DeclarationName::Identifier:
Richard Smitha06c7e62015-08-26 23:55:49 +00003816 case DeclarationName::CXXLiteralOperatorName:
Richard Smith35845152017-02-07 01:37:30 +00003817 case DeclarationName::CXXDeductionGuideName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003818 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003819 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003820 case DeclarationName::ObjCZeroArgSelector:
3821 case DeclarationName::ObjCOneArgSelector:
3822 case DeclarationName::ObjCMultiArgSelector:
Richard Smitha06c7e62015-08-26 23:55:49 +00003823 LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003824 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003825 case DeclarationName::CXXOperatorName:
Richard Smitha06c7e62015-08-26 23:55:49 +00003826 assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
Benjamin Kramer53750b12012-09-19 13:40:40 +00003827 "Invalid operator?");
Richard Smitha06c7e62015-08-26 23:55:49 +00003828 LE.write<uint8_t>(Name.getOperatorKind());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003829 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003830 case DeclarationName::CXXConstructorName:
3831 case DeclarationName::CXXDestructorName:
3832 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003833 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003834 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003835 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003836
3837 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003838 }
3839
Richard Smitha06c7e62015-08-26 23:55:49 +00003840 void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3841 unsigned DataLen) {
Justin Bognere1c147c2014-03-28 22:03:19 +00003842 using namespace llvm::support;
3843 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003844 uint64_t Start = Out.tell(); (void)Start;
Richard Smithd88a7f12015-09-01 20:35:42 +00003845 for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3846 LE.write<uint32_t>(DeclIDs[I]);
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003847 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3848 }
3849};
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00003850
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003851} // end anonymous namespace
3852
Chandler Carruthe972c362015-03-26 03:11:40 +00003853bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3854 DeclContext *DC) {
3855 return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3856}
3857
3858bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3859 DeclContext *DC) {
3860 for (auto *D : Result.getLookupResult())
3861 if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3862 return false;
3863
3864 return true;
3865}
3866
Richard Smithd88a7f12015-09-01 20:35:42 +00003867void
Chandler Carruthe972c362015-03-26 03:11:40 +00003868ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
Richard Smithcd45dbc2014-04-19 03:48:30 +00003869 llvm::SmallVectorImpl<char> &LookupTable) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003870 assert(!ConstDC->HasLazyLocalLexicalLookups &&
3871 !ConstDC->HasLazyExternalLexicalLookups &&
Richard Smith9e2341d2015-03-23 03:25:59 +00003872 "must call buildLookups first");
Richard Smithcd45dbc2014-04-19 03:48:30 +00003873
Chandler Carruthe972c362015-03-26 03:11:40 +00003874 // FIXME: We need to build the lookups table, which is logically const.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00003875 auto *DC = const_cast<DeclContext*>(ConstDC);
Chandler Carruthe972c362015-03-26 03:11:40 +00003876 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3877
3878 // Create the on-disk hash table representation.
Richard Smithd88a7f12015-09-01 20:35:42 +00003879 MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3880 ASTDeclContextNameLookupTrait> Generator;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003881 ASTDeclContextNameLookupTrait Trait(*this);
3882
Chandler Carruthe972c362015-03-26 03:11:40 +00003883 // The first step is to collect the declaration names which we need to
3884 // serialize into the name lookup table, and to collect them in a stable
3885 // order.
3886 SmallVector<DeclarationName, 16> Names;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003887
Chandler Carruthe972c362015-03-26 03:11:40 +00003888 // We also build up small sets of the constructor and conversion function
3889 // names which are visible.
3890 llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003891
Chandler Carruthe972c362015-03-26 03:11:40 +00003892 for (auto &Lookup : *DC->buildLookup()) {
3893 auto &Name = Lookup.first;
3894 auto &Result = Lookup.second;
3895
Douglas Gregor7dd37e52015-11-03 01:20:54 +00003896 // If there are no local declarations in our lookup result, we
3897 // don't need to write an entry for the name at all. If we can't
3898 // write out a lookup set without performing more deserialization,
3899 // just skip this entry.
3900 if (isLookupResultExternal(Result, DC) &&
Chandler Carruthe972c362015-03-26 03:11:40 +00003901 isLookupResultEntirelyExternal(Result, DC))
3902 continue;
3903
3904 // We also skip empty results. If any of the results could be external and
3905 // the currently available results are empty, then all of the results are
3906 // external and we skip it above. So the only way we get here with an empty
3907 // results is when no results could have been external *and* we have
3908 // external results.
3909 //
3910 // FIXME: While we might want to start emitting on-disk entries for negative
3911 // lookups into a decl context as an optimization, today we *have* to skip
3912 // them because there are names with empty lookup results in decl contexts
3913 // which we can't emit in any stable ordering: we lookup constructors and
3914 // conversion functions in the enclosing namespace scope creating empty
3915 // results for them. This in almost certainly a bug in Clang's name lookup,
3916 // but that is likely to be hard or impossible to fix and so we tolerate it
3917 // here by omitting lookups with empty results.
3918 if (Lookup.second.getLookupResult().empty())
3919 continue;
3920
3921 switch (Lookup.first.getNameKind()) {
3922 default:
3923 Names.push_back(Lookup.first);
3924 break;
3925
Richard Smithcd45dbc2014-04-19 03:48:30 +00003926 case DeclarationName::CXXConstructorName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003927 assert(isa<CXXRecordDecl>(DC) &&
3928 "Cannot have a constructor name outside of a class!");
3929 ConstructorNameSet.insert(Name);
3930 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003931
3932 case DeclarationName::CXXConversionFunctionName:
Chandler Carruthe972c362015-03-26 03:11:40 +00003933 assert(isa<CXXRecordDecl>(DC) &&
3934 "Cannot have a conversion function name outside of a class!");
3935 ConversionNameSet.insert(Name);
Rafael Espindolac606b3e2015-03-25 04:43:15 +00003936 break;
Richard Smithcd45dbc2014-04-19 03:48:30 +00003937 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003938 }
Chandler Carruth75c9f132015-03-25 00:34:51 +00003939
Chandler Carruthe972c362015-03-26 03:11:40 +00003940 // Sort the names into a stable order.
3941 std::sort(Names.begin(), Names.end());
3942
Chandler Carruthffbf7052015-03-26 22:27:09 +00003943 if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
Chandler Carruthe972c362015-03-26 03:11:40 +00003944 // We need to establish an ordering of constructor and conversion function
Chandler Carruthffbf7052015-03-26 22:27:09 +00003945 // names, and they don't have an intrinsic ordering.
Chandler Carruthe972c362015-03-26 03:11:40 +00003946
Chandler Carruthffbf7052015-03-26 22:27:09 +00003947 // First we try the easy case by forming the current context's constructor
3948 // name and adding that name first. This is a very useful optimization to
3949 // avoid walking the lexical declarations in many cases, and it also
3950 // handles the only case where a constructor name can come from some other
3951 // lexical context -- when that name is an implicit constructor merged from
3952 // another declaration in the redecl chain. Any non-implicit constructor or
3953 // conversion function which doesn't occur in all the lexical contexts
3954 // would be an ODR violation.
3955 auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3956 Context->getCanonicalType(Context->getRecordType(D)));
3957 if (ConstructorNameSet.erase(ImplicitCtorName))
3958 Names.push_back(ImplicitCtorName);
Chandler Carruthe972c362015-03-26 03:11:40 +00003959
Chandler Carruthffbf7052015-03-26 22:27:09 +00003960 // If we still have constructors or conversion functions, we walk all the
3961 // names in the decl and add the constructors and conversion functions
3962 // which are visible in the order they lexically occur within the context.
3963 if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3964 for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3965 if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3966 auto Name = ChildND->getDeclName();
3967 switch (Name.getNameKind()) {
3968 default:
3969 continue;
3970
3971 case DeclarationName::CXXConstructorName:
3972 if (ConstructorNameSet.erase(Name))
3973 Names.push_back(Name);
3974 break;
3975
3976 case DeclarationName::CXXConversionFunctionName:
3977 if (ConversionNameSet.erase(Name))
3978 Names.push_back(Name);
3979 break;
3980 }
3981
3982 if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3983 break;
Chandler Carruthe972c362015-03-26 03:11:40 +00003984 }
3985
Chandler Carruthe972c362015-03-26 03:11:40 +00003986 assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3987 "constructors by walking all the "
3988 "lexical members of the context.");
3989 assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3990 "conversion functions by walking all "
3991 "the lexical members of the context.");
Richard Smith961eae52014-03-25 01:14:22 +00003992 }
3993
Chandler Carruthe972c362015-03-26 03:11:40 +00003994 // Next we need to do a lookup with each name into this decl context to fully
3995 // populate any results from external sources. We don't actually use the
3996 // results of these lookups because we only want to use the results after all
3997 // results have been loaded and the pointers into them will be stable.
3998 for (auto &Name : Names)
3999 DC->lookup(Name);
4000
4001 // Now we need to insert the results for each name into the hash table. For
4002 // constructor names and conversion function names, we actually need to merge
4003 // all of the results for them into one list of results each and insert
4004 // those.
4005 SmallVector<NamedDecl *, 8> ConstructorDecls;
4006 SmallVector<NamedDecl *, 8> ConversionDecls;
4007
4008 // Now loop over the names, either inserting them or appending for the two
4009 // special cases.
4010 for (auto &Name : Names) {
4011 DeclContext::lookup_result Result = DC->noload_lookup(Name);
4012
4013 switch (Name.getNameKind()) {
4014 default:
Richard Smithd88a7f12015-09-01 20:35:42 +00004015 Generator.insert(Name, Trait.getData(Result), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004016 break;
4017
4018 case DeclarationName::CXXConstructorName:
4019 ConstructorDecls.append(Result.begin(), Result.end());
4020 break;
4021
4022 case DeclarationName::CXXConversionFunctionName:
4023 ConversionDecls.append(Result.begin(), Result.end());
4024 break;
4025 }
4026 }
4027
4028 // Handle our two special cases if we ended up having any. We arbitrarily use
4029 // the first declaration's name here because the name itself isn't part of
4030 // the key, only the kind of name is used.
4031 if (!ConstructorDecls.empty())
4032 Generator.insert(ConstructorDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004033 Trait.getData(ConstructorDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004034 if (!ConversionDecls.empty())
4035 Generator.insert(ConversionDecls.front()->getDeclName(),
Richard Smithd88a7f12015-09-01 20:35:42 +00004036 Trait.getData(ConversionDecls), Trait);
Chandler Carruthe972c362015-03-26 03:11:40 +00004037
Richard Smithd88a7f12015-09-01 20:35:42 +00004038 // Create the on-disk hash table. Also emit the existing imported and
4039 // merged table if there is one.
4040 auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
4041 Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
Richard Smith961eae52014-03-25 01:14:22 +00004042}
4043
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004044/// \brief Write the block containing all of the declaration IDs
4045/// visible from the given DeclContext.
4046///
4047/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00004048/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004049uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
4050 DeclContext *DC) {
Richard Smith5fc18a92015-07-12 23:43:21 +00004051 // If we imported a key declaration of this namespace, write the visible
4052 // lookup results as an update record for it rather than including them
4053 // on this declaration. We will only look at key declarations on reload.
4054 if (isa<NamespaceDecl>(DC) && Chain &&
4055 Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
4056 // Only do this once, for the first local declaration of the namespace.
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004057 for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
Richard Smith5fc18a92015-07-12 23:43:21 +00004058 Prev = Prev->getPreviousDecl())
4059 if (!Prev->isFromASTFile())
4060 return 0;
4061
4062 // Note that we need to emit an update record for the primary context.
4063 UpdatedDeclContexts.insert(DC->getPrimaryContext());
4064
4065 // Make sure all visible decls are written. They will be recorded later. We
4066 // do this using a side data structure so we can sort the names into
4067 // a deterministic order.
4068 StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
4069 SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
4070 LookupResults;
4071 if (Map) {
4072 LookupResults.reserve(Map->size());
4073 for (auto &Entry : *Map)
4074 LookupResults.push_back(
4075 std::make_pair(Entry.first, Entry.second.getLookupResult()));
4076 }
4077
4078 std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
4079 for (auto &NameAndResult : LookupResults) {
4080 DeclarationName Name = NameAndResult.first;
4081 DeclContext::lookup_result Result = NameAndResult.second;
4082 if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
4083 Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
4084 // We have to work around a name lookup bug here where negative lookup
4085 // results for these names get cached in namespace lookup tables (these
4086 // names should never be looked up in a namespace).
4087 assert(Result.empty() && "Cannot have a constructor or conversion "
4088 "function name in a namespace!");
4089 continue;
4090 }
4091
4092 for (NamedDecl *ND : Result)
4093 if (!ND->isFromASTFile())
4094 GetDeclRef(ND);
4095 }
4096
4097 return 0;
4098 }
4099
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004100 if (DC->getPrimaryContext() != DC)
4101 return 0;
4102
Chandler Carruthe972c362015-03-26 03:11:40 +00004103 // Skip contexts which don't support name lookup.
4104 if (!DC->isLookupContext())
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004105 return 0;
4106
4107 // If not in C++, we perform name lookup for the translation unit via the
4108 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004109 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004110 return 0;
4111
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004112 // Serialize the contents of the mapping used for lookup. Note that,
4113 // although we have two very different code paths, the serialized
4114 // representation is the same for both cases: a declaration name,
4115 // followed by a size, followed by references to the visible
4116 // declarations that have that name.
4117 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00004118 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004119 if (!Map || Map->empty())
4120 return 0;
4121
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004122 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004123 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004124 GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004125
4126 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004127 RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004128 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
Yaron Keren92e1b622015-03-18 10:17:07 +00004129 LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00004130 ++NumVisibleDeclContexts;
4131 return Offset;
4132}
4133
Sebastian Redla4071b42010-08-24 00:50:09 +00004134/// \brief Write an UPDATE_VISIBLE block for the given context.
4135///
4136/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
4137/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00004138/// (in C++), for namespaces, and for classes with forward-declared unscoped
4139/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00004140void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00004141 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00004142 if (!Map || Map->empty())
4143 return;
4144
Sebastian Redla4071b42010-08-24 00:50:09 +00004145 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004146 SmallString<4096> LookupTable;
Richard Smithd88a7f12015-09-01 20:35:42 +00004147 GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004148
Richard Smith5fc18a92015-07-12 23:43:21 +00004149 // If we're updating a namespace, select a key declaration as the key for the
4150 // update record; those are the only ones that will be checked on reload.
4151 if (isa<NamespaceDecl>(DC))
4152 DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
4153
Sebastian Redla4071b42010-08-24 00:50:09 +00004154 // Write the lookup table
Mehdi Amini57a41912015-09-10 01:46:39 +00004155 RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
Yaron Keren92e1b622015-03-18 10:17:07 +00004156 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00004157}
4158
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004159/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
4160void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
Adam Nemet484aa452017-03-27 19:17:25 +00004161 RecordData::value_type Record[] = {Opts.getInt()};
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004162 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
4163}
4164
4165/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
4166void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004167 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004168 return;
4169
4170 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
4171 RecordData Record;
Yaxun Liu5b746652016-12-18 05:18:55 +00004172 for (const auto &I:Opts.OptMap) {
4173 AddString(I.getKey(), Record);
4174 auto V = I.getValue();
Yaxun Liucc2741c2016-12-18 06:35:06 +00004175 Record.push_back(V.Supported ? 1 : 0);
4176 Record.push_back(V.Enabled ? 1 : 0);
Yaxun Liu5b746652016-12-18 05:18:55 +00004177 Record.push_back(V.Avail);
4178 Record.push_back(V.Core);
4179 }
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004180 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
4181}
4182
Yaxun Liu5b746652016-12-18 05:18:55 +00004183void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) {
4184 if (!SemaRef.Context.getLangOpts().OpenCL)
4185 return;
4186
4187 RecordData Record;
4188 for (const auto &I : SemaRef.OpenCLTypeExtMap) {
4189 Record.push_back(
4190 static_cast<unsigned>(getTypeID(I.first->getCanonicalTypeInternal())));
4191 Record.push_back(I.second.size());
4192 for (auto Ext : I.second)
4193 AddString(Ext, Record);
4194 }
4195 Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record);
4196}
4197
4198void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) {
4199 if (!SemaRef.Context.getLangOpts().OpenCL)
4200 return;
4201
4202 RecordData Record;
4203 for (const auto &I : SemaRef.OpenCLDeclExtMap) {
4204 Record.push_back(getDeclID(I.first));
4205 Record.push_back(static_cast<unsigned>(I.second.size()));
4206 for (auto Ext : I.second)
4207 AddString(Ext, Record);
4208 }
4209 Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record);
4210}
4211
Justin Lebar67a78a62016-10-08 22:15:58 +00004212void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
4213 if (SemaRef.ForceCUDAHostDeviceDepth > 0) {
4214 RecordData::value_type Record[] = {SemaRef.ForceCUDAHostDeviceDepth};
4215 Stream.EmitRecord(CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH, Record);
4216 }
4217}
4218
Douglas Gregor404cdde2012-01-27 01:47:08 +00004219void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004220 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00004221 RecordData Categories;
4222
4223 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
4224 unsigned Size = 0;
4225 unsigned StartIndex = Categories.size();
4226
4227 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
4228
4229 // Allocate space for the size.
4230 Categories.push_back(0);
4231
4232 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00004233 for (ObjCInterfaceDecl::known_categories_iterator
4234 Cat = Class->known_categories_begin(),
4235 CatEnd = Class->known_categories_end();
4236 Cat != CatEnd; ++Cat, ++Size) {
4237 assert(getDeclID(*Cat) != 0 && "Bogus category");
4238 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00004239 }
4240
4241 // Update the size.
4242 Categories[StartIndex] = Size;
4243
4244 // Record this interface -> category map.
4245 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
4246 CategoriesMap.push_back(CatInfo);
4247 }
4248
4249 // Sort the categories map by the definition ID, since the reader will be
4250 // performing binary searches on this information.
4251 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
4252
4253 // Emit the categories map.
4254 using namespace llvm;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004255
David Blaikieb44f0bf2017-01-04 22:36:43 +00004256 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004257 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
4258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
4259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004260 unsigned AbbrevID = Stream.EmitAbbrev(std::move(Abbrev));
Mehdi Amini57a41912015-09-10 01:46:39 +00004261
4262 RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
4263 Stream.EmitRecordWithBlob(AbbrevID, Record,
4264 reinterpret_cast<char *>(CategoriesMap.data()),
Douglas Gregor404cdde2012-01-27 01:47:08 +00004265 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
Mehdi Amini57a41912015-09-10 01:46:39 +00004266
Douglas Gregor404cdde2012-01-27 01:47:08 +00004267 // Emit the category lists.
4268 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
4269}
4270
Richard Smithe40f2ba2013-08-07 21:41:30 +00004271void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4272 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4273
4274 if (LPTMap.empty())
4275 return;
4276
4277 RecordData Record;
Justin Lebar28f09c52016-10-10 16:26:08 +00004278 for (auto &LPTMapEntry : LPTMap) {
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004279 const FunctionDecl *FD = LPTMapEntry.first;
Justin Lebar28f09c52016-10-10 16:26:08 +00004280 LateParsedTemplate &LPT = *LPTMapEntry.second;
Chandler Carruth52cee4d2015-03-26 09:08:15 +00004281 AddDeclRef(FD, Record);
Justin Lebar28f09c52016-10-10 16:26:08 +00004282 AddDeclRef(LPT.D, Record);
4283 Record.push_back(LPT.Toks.size());
Richard Smithe40f2ba2013-08-07 21:41:30 +00004284
Justin Lebar28f09c52016-10-10 16:26:08 +00004285 for (const auto &Tok : LPT.Toks) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004286 AddToken(Tok, Record);
Richard Smithe40f2ba2013-08-07 21:41:30 +00004287 }
4288 }
4289 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4290}
4291
Dario Domizioli13a0a382014-05-23 12:13:25 +00004292/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4293void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4294 RecordData Record;
4295 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4296 AddSourceLocation(PragmaLoc, Record);
4297 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4298}
4299
Nico Weber779355f2016-03-02 23:22:00 +00004300/// \brief Write the state of 'pragma ms_struct' at the end of the module.
4301void ASTWriter::WriteMSStructPragmaOptions(Sema &SemaRef) {
4302 RecordData Record;
4303 Record.push_back(SemaRef.MSStructPragmaOn ? PMSST_ON : PMSST_OFF);
4304 Stream.EmitRecord(MSSTRUCT_PRAGMA_OPTIONS, Record);
4305}
4306
Nico Weber42932312016-03-03 00:17:35 +00004307/// \brief Write the state of 'pragma pointers_to_members' at the end of the
4308//module.
4309void ASTWriter::WriteMSPointersToMembersPragmaOptions(Sema &SemaRef) {
4310 RecordData Record;
4311 Record.push_back(SemaRef.MSPointerToMemberRepresentationMethod);
4312 AddSourceLocation(SemaRef.ImplicitMSInheritanceAttrLoc, Record);
4313 Stream.EmitRecord(POINTERS_TO_MEMBERS_PRAGMA_OPTIONS, Record);
4314}
4315
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004316/// \brief Write the state of 'pragma pack' at the end of the module.
4317void ASTWriter::WritePackPragmaOptions(Sema &SemaRef) {
Duncan P. N. Exon Smith03df14c2017-04-15 00:07:57 +00004318 // Don't serialize pragma pack state for modules, since it should only take
4319 // effect on a per-submodule basis.
4320 if (WritingModule)
4321 return;
4322
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004323 RecordData Record;
4324 Record.push_back(SemaRef.PackStack.CurrentValue);
4325 AddSourceLocation(SemaRef.PackStack.CurrentPragmaLocation, Record);
4326 Record.push_back(SemaRef.PackStack.Stack.size());
4327 for (const auto &StackEntry : SemaRef.PackStack.Stack) {
4328 Record.push_back(StackEntry.Value);
4329 AddSourceLocation(StackEntry.PragmaLocation, Record);
Alex Lorenz45b40142017-07-28 14:41:21 +00004330 AddSourceLocation(StackEntry.PragmaPushLocation, Record);
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00004331 AddString(StackEntry.StackSlotLabel, Record);
4332 }
4333 Stream.EmitRecord(PACK_PRAGMA_OPTIONS, Record);
4334}
4335
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004336void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
4337 ModuleFileExtensionWriter &Writer) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004338 // Enter the extension block.
4339 Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
4340
4341 // Emit the metadata record abbreviation.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004342 auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004343 Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
4344 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4345 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4346 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4347 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4348 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004349 unsigned Abbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004350
4351 // Emit the metadata record.
4352 RecordData Record;
4353 auto Metadata = Writer.getExtension()->getExtensionMetadata();
4354 Record.push_back(EXTENSION_METADATA);
4355 Record.push_back(Metadata.MajorVersion);
4356 Record.push_back(Metadata.MinorVersion);
4357 Record.push_back(Metadata.BlockName.size());
4358 Record.push_back(Metadata.UserInfo.size());
4359 SmallString<64> Buffer;
4360 Buffer += Metadata.BlockName;
4361 Buffer += Metadata.UserInfo;
4362 Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
4363
4364 // Emit the contents of the extension block.
Douglas Gregor8f64ca12015-12-08 22:43:32 +00004365 Writer.writeExtensionContents(SemaRef, Stream);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004366
4367 // Exit the extension block.
4368 Stream.ExitBlock();
4369}
4370
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004371//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00004372// General Serialization Routines
4373//===----------------------------------------------------------------------===//
4374
Richard Smith69c82bf2016-04-01 22:52:03 +00004375/// \brief Emit the list of attributes to the specified record.
Richard Smith290d8012016-04-06 17:06:00 +00004376void ASTRecordWriter::AddAttributes(ArrayRef<const Attr *> Attrs) {
4377 auto &Record = *this;
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00004378 Record.push_back(Attrs.size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004379 for (const auto *A : Attrs) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004380 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Richard Smith290d8012016-04-06 17:06:00 +00004381 Record.AddSourceRange(A->getRange());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004382
Alexis Huntdcfba7b2010-08-18 23:23:40 +00004383#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00004384
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004385 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004386}
4387
John McCallf413f5e2013-05-03 00:10:13 +00004388void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4389 AddSourceLocation(Tok.getLocation(), Record);
4390 Record.push_back(Tok.getLength());
4391
4392 // FIXME: When reading literal tokens, reconstruct the literal pointer
4393 // if it is needed.
4394 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4395 // FIXME: Should translate token kind to a stable encoding.
4396 Record.push_back(Tok.getKind());
4397 // FIXME: Should translate token flags to a stable encoding.
4398 Record.push_back(Tok.getFlags());
4399}
4400
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004401void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00004402 Record.push_back(Str.size());
4403 Record.insert(Record.end(), Str.begin(), Str.end());
4404}
4405
Richard Smith7ed1bc92014-12-05 22:42:13 +00004406bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
Richard Smith54cc3c22014-12-11 20:50:24 +00004407 assert(Context && "should have context when outputting path");
Richard Smith7ed1bc92014-12-05 22:42:13 +00004408
Richard Smith54cc3c22014-12-11 20:50:24 +00004409 bool Changed =
4410 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004411
4412 // Remove a prefix to make the path relative, if relevant.
4413 const char *PathBegin = Path.data();
4414 const char *PathPtr =
4415 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4416 if (PathPtr != PathBegin) {
4417 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4418 Changed = true;
4419 }
4420
4421 return Changed;
4422}
4423
4424void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4425 SmallString<128> FilePath(Path);
4426 PreparePathForOutput(FilePath);
4427 AddString(FilePath, Record);
4428}
4429
Mehdi Amini57a41912015-09-10 01:46:39 +00004430void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
Richard Smith7ed1bc92014-12-05 22:42:13 +00004431 StringRef Path) {
4432 SmallString<128> FilePath(Path);
4433 PreparePathForOutput(FilePath);
4434 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4435}
4436
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004437void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4438 RecordDataImpl &Record) {
4439 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00004440 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004441 Record.push_back(*Minor + 1);
4442 else
4443 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00004444 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00004445 Record.push_back(*Subminor + 1);
4446 else
4447 Record.push_back(0);
4448}
4449
Douglas Gregore84a9da2009-04-20 20:36:09 +00004450/// \brief Note that the identifier II occurs at the given offset
4451/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004452void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00004453 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004454 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00004455 // up earlier in the chain and thus don't need an offset.
4456 if (ID >= FirstIdentID)
4457 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00004458}
4459
Douglas Gregor95c13f52009-04-25 17:48:32 +00004460/// \brief Note that the selector Sel occurs at the given offset
4461/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004462void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00004463 unsigned ID = SelectorIDs[Sel];
4464 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00004465 // Don't record offsets for selectors that are also available in a different
4466 // file.
4467 if (ID < FirstSelectorID)
4468 return;
4469 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00004470}
4471
David Blaikie61137e12017-01-05 18:23:18 +00004472ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004473 SmallVectorImpl<char> &Buffer, MemoryBufferCache &PCMCache,
David Blaikie61137e12017-01-05 18:23:18 +00004474 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
4475 bool IncludeTimestamps)
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004476 : Stream(Stream), Buffer(Buffer), PCMCache(PCMCache),
4477 IncludeTimestamps(IncludeTimestamps) {
Douglas Gregor6623e1f2015-11-03 18:33:07 +00004478 for (const auto &Ext : Extensions) {
4479 if (auto Writer = Ext->createExtensionWriter(*this))
4480 ModuleFileExtensionWriters.push_back(std::move(Writer));
4481 }
4482}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004483
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004484ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00004485 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004486}
4487
Richard Smithb3761912015-02-05 23:08:52 +00004488const LangOptions &ASTWriter::getLangOpts() const {
4489 assert(WritingAST && "can't determine lang opts when not writing AST");
4490 return Context->getLangOpts();
4491}
4492
Richard Smithe75ee0f2015-08-17 07:13:32 +00004493time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4494 return IncludeTimestamps ? E->getModificationTime() : 0;
4495}
4496
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004497ASTFileSignature ASTWriter::WriteAST(Sema &SemaRef,
4498 const std::string &OutputFile,
4499 Module *WritingModule, StringRef isysroot,
4500 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004501 WritingAST = true;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004502
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00004503 ASTHasCompilerErrors = hasErrors;
4504
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004505 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00004506 Stream.Emit((unsigned)'C', 8);
4507 Stream.Emit((unsigned)'P', 8);
4508 Stream.Emit((unsigned)'C', 8);
4509 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00004510
Chris Lattner28fa4e62009-04-26 22:26:21 +00004511 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004512
Douglas Gregoreda8e122011-08-09 15:13:55 +00004513 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004514 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004515 this->WritingModule = WritingModule;
Adrian Prantladbd2b12015-09-22 23:26:31 +00004516 ASTFileSignature Signature =
4517 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Craig Toppera13603a2014-05-22 05:54:18 +00004518 Context = nullptr;
4519 PP = nullptr;
4520 this->WritingModule = nullptr;
Richard Smith7ed1bc92014-12-05 22:42:13 +00004521 this->BaseDirectory.clear();
Craig Toppera13603a2014-05-22 05:54:18 +00004522
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004523 WritingAST = false;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +00004524 if (SemaRef.Context.getLangOpts().ImplicitModules && WritingModule) {
4525 // Construct MemoryBuffer and update buffer manager.
4526 PCMCache.addBuffer(OutputFile,
4527 llvm::MemoryBuffer::getMemBufferCopy(
4528 StringRef(Buffer.begin(), Buffer.size())));
4529 }
Adrian Prantladbd2b12015-09-22 23:26:31 +00004530 return Signature;
Sebastian Redl143413f2010-07-12 22:02:52 +00004531}
4532
Douglas Gregora94a1542011-07-27 21:45:57 +00004533template<typename Vector>
4534static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4535 ASTWriter::RecordData &Record) {
Craig Toppera13603a2014-05-22 05:54:18 +00004536 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4537 I != E; ++I) {
Douglas Gregora94a1542011-07-27 21:45:57 +00004538 Writer.AddDeclRef(*I, Record);
4539 }
4540}
4541
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004542ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4543 const std::string &OutputFile,
4544 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00004545 using namespace llvm;
4546
Craig Toppera13603a2014-05-22 05:54:18 +00004547 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004548
Douglas Gregorcf68c582011-12-01 22:20:10 +00004549 // Make sure that the AST reader knows to finalize itself.
4550 if (Chain)
4551 Chain->finalizeForWriting();
4552
Sebastian Redl143413f2010-07-12 22:02:52 +00004553 ASTContext &Context = SemaRef.Context;
4554 Preprocessor &PP = SemaRef.PP;
4555
Douglas Gregordab42432011-08-12 00:15:20 +00004556 // Set up predefined declaration IDs.
Richard Smith5fc18a92015-07-12 23:43:21 +00004557 auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4558 if (D) {
4559 assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4560 DeclIDs[D] = ID;
Richard Smith5fc18a92015-07-12 23:43:21 +00004561 }
4562 };
4563 RegisterPredefDecl(Context.getTranslationUnitDecl(),
4564 PREDEF_DECL_TRANSLATION_UNIT_ID);
4565 RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4566 RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4567 RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4568 RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4569 PREDEF_DECL_OBJC_PROTOCOL_ID);
4570 RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4571 RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4572 RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4573 PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4574 RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
Richard Smith9b88a4c2015-07-27 05:40:23 +00004575 RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
Charles Davisc7d5c942015-09-17 20:55:33 +00004576 RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4577 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
Richard Smith5fc18a92015-07-12 23:43:21 +00004578 RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
David Majnemerd9b1a4f2015-11-04 03:40:30 +00004579 RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4580 PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
Quentin Colombet043406b2016-02-03 22:41:00 +00004581 RegisterPredefDecl(Context.CFConstantStringTypeDecl,
4582 PREDEF_DECL_CF_CONSTANT_STRING_ID);
Ben Langmuirf5416742016-02-04 00:55:24 +00004583 RegisterPredefDecl(Context.CFConstantStringTagDecl,
4584 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID);
Eric Fiselier6ad68552016-07-01 01:24:09 +00004585 RegisterPredefDecl(Context.TypePackElementDecl,
4586 PREDEF_DECL_TYPE_PACK_ELEMENT_ID);
Meador Inge5d3fb222012-06-16 03:34:49 +00004587
Chris Lattner0c797362009-09-08 18:19:27 +00004588 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00004589 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00004590 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00004591 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00004592 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00004593
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004594 // Build a record containing all of the file scoped decls in this file.
4595 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00004596 if (!isModule)
4597 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4598 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00004599
Douglas Gregor851443c2011-08-12 01:39:19 +00004600 // Build a record containing all of the delegating constructors we still need
4601 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00004602 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004603 if (!isModule)
4604 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004605
Douglas Gregor851443c2011-08-12 01:39:19 +00004606 // Write the set of weak, undeclared identifiers. We always write the
4607 // entire table, since later PCH files in a PCH chain are only interested in
4608 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004609 RecordData WeakUndeclaredIdentifiers;
Chandler Carruthf85d9822015-03-26 08:32:49 +00004610 for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4611 IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4612 WeakInfo &WI = WeakUndeclaredIdentifier.second;
4613 AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4614 AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4615 AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4616 WeakUndeclaredIdentifiers.push_back(WI.getUsed());
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004617 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004618
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004619 // Build a record containing all of the ext_vector declarations.
4620 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004621 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004622
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004623 // Build a record containing all of the VTable uses information.
4624 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004625 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004626 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4627 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4628 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4629 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4630 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004631 }
4632
Nico Weber72889432014-09-06 01:25:55 +00004633 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4634 RecordData UnusedLocalTypedefNameCandidates;
4635 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4636 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4637
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004638 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004639 RecordData PendingInstantiations;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004640 for (const auto &I : SemaRef.PendingInstantiations) {
4641 AddDeclRef(I.first, PendingInstantiations);
4642 AddSourceLocation(I.second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004643 }
4644 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4645 "There are local ones at end of translation unit!");
4646
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004647 // Build a record containing some declaration references.
4648 RecordData SemaDeclRefs;
Richard Smith96269c52016-09-29 22:49:46 +00004649 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004650 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4651 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
Richard Smith96269c52016-09-29 22:49:46 +00004652 AddDeclRef(SemaRef.getStdAlignValT(), SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004653 }
4654
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004655 RecordData CUDASpecialDeclRefs;
4656 if (Context.getcudaConfigureCallDecl()) {
4657 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4658 }
4659
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004660 // Build a record containing all of the known namespaces.
4661 RecordData KnownNamespaces;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004662 for (const auto &I : SemaRef.KnownNamespaces) {
4663 if (!I.second)
4664 AddDeclRef(I.first, KnownNamespaces);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004665 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004666
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004667 // Build a record of all used, undefined objects that require definitions.
4668 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004669
4670 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004671 SemaRef.getUndefinedButUsed(Undefined);
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004672 for (const auto &I : Undefined) {
4673 AddDeclRef(I.first, UndefinedButUsed);
4674 AddSourceLocation(I.second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004675 }
4676
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004677 // Build a record containing all delete-expressions that we would like to
4678 // analyze later in AST.
4679 RecordData DeleteExprsToAnalyze;
4680
4681 for (const auto &DeleteExprsInfo :
4682 SemaRef.getMismatchingDeleteExpressions()) {
4683 AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4684 DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4685 for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4686 AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4687 DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4688 }
4689 }
4690
Douglas Gregor112b9072012-10-18 05:31:06 +00004691 // Write the control block
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00004692 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004693
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004694 // Write the remaining AST contents.
Sebastian Redl539c5062010-08-18 23:57:32 +00004695 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004696
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004697 // This is so that older clang versions, before the introduction
4698 // of the control block, can read and reject the newer PCH format.
Mehdi Amini57a41912015-09-10 01:46:39 +00004699 {
4700 RecordData Record = {VERSION_MAJOR};
4701 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4702 }
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004703
Douglas Gregor851443c2011-08-12 01:39:19 +00004704 // Create a lexical update block containing all of the declarations in the
4705 // translation unit that do not come from other AST files.
4706 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Richard Smith82f8fcd2015-08-06 22:07:25 +00004707 SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4708 for (const auto *D : TU->noload_decls()) {
4709 if (!D->isFromASTFile()) {
4710 NewGlobalKindDeclPairs.push_back(D->getKind());
4711 NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4712 }
Douglas Gregor851443c2011-08-12 01:39:19 +00004713 }
4714
David Blaikieb44f0bf2017-01-04 22:36:43 +00004715 auto Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004716 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4717 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004718 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
Mehdi Amini57a41912015-09-10 01:46:39 +00004719 {
4720 RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4721 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4722 bytes(NewGlobalKindDeclPairs));
4723 }
4724
Douglas Gregor851443c2011-08-12 01:39:19 +00004725 // And a visible updates block for the translation unit.
David Blaikieb44f0bf2017-01-04 22:36:43 +00004726 Abv = std::make_shared<BitCodeAbbrev>();
Douglas Gregor851443c2011-08-12 01:39:19 +00004727 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4728 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
Douglas Gregor851443c2011-08-12 01:39:19 +00004729 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004730 UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
Douglas Gregor851443c2011-08-12 01:39:19 +00004731 WriteDeclContextVisibleUpdate(TU);
Richard Smithf19e1272015-03-07 00:04:49 +00004732
4733 // If we have any extern "C" names, write out a visible update for them.
4734 if (Context.ExternCContext)
4735 WriteDeclContextVisibleUpdate(Context.ExternCContext);
Douglas Gregor851443c2011-08-12 01:39:19 +00004736
4737 // If the translation unit has an anonymous namespace, and we don't already
4738 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004739 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004740 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4741 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004742 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004743 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004744 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004745
Richard Smith5652c0f2014-03-21 01:48:23 +00004746 // Add update records for all mangling numbers and static local numbers.
4747 // These aren't really update records, but this is a convenient way of
4748 // tagging this rare extra data onto the declarations.
4749 for (const auto &Number : Context.MangleNumbers)
4750 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004751 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4752 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004753 for (const auto &Number : Context.StaticLocalNumbers)
4754 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004755 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4756 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004757
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004758 // Make sure visible decls, added to DeclContexts previously loaded from
Richard Smithc26d9742016-10-06 20:30:51 +00004759 // an AST file, are registered for serialization. Likewise for template
4760 // specializations added to imported templates.
4761 for (const auto *I : DeclsToEmitEvenIfUnreferenced) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004762 GetDeclRef(I);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004763 }
4764
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004765 // Make sure all decls associated with an identifier are registered for
Richard Smith79bf9202015-08-24 03:33:22 +00004766 // serialization, if we're storing decls with identifiers.
4767 if (!WritingModule || !getLangOpts().CPlusPlus) {
4768 llvm::SmallVector<const IdentifierInfo*, 256> IIs;
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00004769 for (const auto &ID : PP.getIdentifierTable()) {
4770 const IdentifierInfo *II = ID.second;
Richard Smith79bf9202015-08-24 03:33:22 +00004771 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4772 IIs.push_back(II);
4773 }
4774 // Sort the identifiers to visit based on their name.
4775 std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4776 for (const IdentifierInfo *II : IIs) {
4777 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4778 DEnd = SemaRef.IdResolver.end();
4779 D != DEnd; ++D) {
4780 GetDeclRef(*D);
4781 }
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004782 }
4783 }
4784
Manman Rena0f31a02016-04-29 19:04:05 +00004785 // For method pool in the module, if it contains an entry for a selector,
4786 // the entry should be complete, containing everything introduced by that
4787 // module and all modules it imports. It's possible that the entry is out of
4788 // date, so we need to pull in the new content here.
4789
4790 // It's possible that updateOutOfDateSelector can update SelectorIDs. To be
4791 // safe, we copy all selectors out.
4792 llvm::SmallVector<Selector, 256> AllSelectors;
4793 for (auto &SelectorAndID : SelectorIDs)
4794 AllSelectors.push_back(SelectorAndID.first);
4795 for (auto &Selector : AllSelectors)
4796 SemaRef.updateOutOfDateSelector(Selector);
4797
Douglas Gregor5204bde2011-08-02 16:26:37 +00004798 // Form the record of special types.
4799 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004800 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004801 AddTypeRef(Context.getFILEType(), SpecialTypes);
4802 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4803 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4804 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4805 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004806 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004807 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004808
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004809 if (Chain) {
4810 // Write the mapping information describing our module dependencies and how
4811 // each of those modules were mapped into our own offset/ID space, so that
4812 // the reader can build the appropriate mapping to its own offset/ID space.
4813 // The map consists solely of a blob with the following format:
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004814 // *(module-kind:i8
4815 // module-name-len:i16 module-name:len*i8
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004816 // source-location-offset:i32
4817 // identifier-id:i32
4818 // preprocessed-entity-id:i32
4819 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004820 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004821 // selector-id:i32
4822 // declaration-id:i32
4823 // c++-base-specifiers-id:i32
4824 // type-id:i32)
4825 //
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004826 // module-kind is the ModuleKind enum value. If it is MK_PrebuiltModule or
4827 // MK_ExplicitModule, then the module-name is the module name. Otherwise,
4828 // it is the module file name.
4829 //
David Blaikieb44f0bf2017-01-04 22:36:43 +00004830 auto Abbrev = std::make_shared<BitCodeAbbrev>();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004831 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4832 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
David Blaikieb44f0bf2017-01-04 22:36:43 +00004833 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004834 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004835 {
4836 llvm::raw_svector_ostream Out(Buffer);
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004837 for (ModuleFile &M : Chain->ModuleMgr) {
Justin Bognere1c147c2014-03-28 22:03:19 +00004838 using namespace llvm::support;
4839 endian::Writer<little> LE(Out);
Boris Kolpackovd30446f2017-08-31 06:26:43 +00004840 LE.write<uint8_t>(static_cast<uint8_t>(M.Kind));
4841 StringRef Name =
4842 M.Kind == MK_PrebuiltModule || M.Kind == MK_ExplicitModule
4843 ? M.ModuleName
4844 : M.FileName;
4845 LE.write<uint16_t>(Name.size());
4846 Out.write(Name.data(), Name.size());
Ben Langmuirfe971d92014-08-16 04:54:18 +00004847
Ben Langmuir785180e2014-10-20 16:27:30 +00004848 // Note: if a base ID was uint max, it would not be possible to load
4849 // another module after it or have more than one entity inside it.
4850 uint32_t None = std::numeric_limits<uint32_t>::max();
4851
4852 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4853 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4854 if (ShouldWrite)
4855 LE.write<uint32_t>(BaseID);
4856 else
4857 LE.write<uint32_t>(None);
4858 };
4859
Ben Langmuirfe971d92014-08-16 04:54:18 +00004860 // These values should be unique within a chain, since they will be read
4861 // as keys into ContinuousRangeMaps.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +00004862 writeBaseIDOrNone(M.SLocEntryBaseOffset, M.LocalNumSLocEntries);
4863 writeBaseIDOrNone(M.BaseIdentifierID, M.LocalNumIdentifiers);
4864 writeBaseIDOrNone(M.BaseMacroID, M.LocalNumMacros);
4865 writeBaseIDOrNone(M.BasePreprocessedEntityID,
4866 M.NumPreprocessedEntities);
4867 writeBaseIDOrNone(M.BaseSubmoduleID, M.LocalNumSubmodules);
4868 writeBaseIDOrNone(M.BaseSelectorID, M.LocalNumSelectors);
4869 writeBaseIDOrNone(M.BaseDeclID, M.LocalNumDecls);
4870 writeBaseIDOrNone(M.BaseTypeIndex, M.LocalNumTypes);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004871 }
4872 }
Mehdi Amini57a41912015-09-10 01:46:39 +00004873 RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004874 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4875 Buffer.data(), Buffer.size());
4876 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004877
Richard Smithb9eab6d2014-03-20 19:44:17 +00004878 RecordData DeclUpdatesOffsetsRecord;
4879
Richard Smith59442b42014-03-20 20:07:19 +00004880 // Keep writing types, declarations, and declaration update records
4881 // until we've emitted all of them.
Richard Smith01b2cb42014-07-26 06:37:51 +00004882 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4883 WriteTypeAbbrevs();
4884 WriteDeclAbbrevs();
Richard Smith59442b42014-03-20 20:07:19 +00004885 do {
4886 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4887 while (!DeclTypesToEmit.empty()) {
4888 DeclOrType DOT = DeclTypesToEmit.front();
4889 DeclTypesToEmit.pop();
4890 if (DOT.isType())
4891 WriteType(DOT.getType());
4892 else
4893 WriteDecl(Context, DOT.getDecl());
4894 }
4895 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004896 Stream.ExitBlock();
4897
Richard Smithb9eab6d2014-03-20 19:44:17 +00004898 DoneWritingDeclsAndTypes = true;
4899
4900 // These things can only be done once we've written out decls and types.
4901 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004902 if (!DeclUpdatesOffsetsRecord.empty())
4903 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004904 WriteFileDeclIDsMap();
Richard Smith7ed1bc92014-12-05 22:42:13 +00004905 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004906 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004907 WritePreprocessor(PP, isModule);
Richard Smith7ed1bc92014-12-05 22:42:13 +00004908 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redla19a67f2010-08-03 21:58:15 +00004909 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004910 WriteReferencedSelectorsPool(SemaRef);
Richard Smith9c254182015-07-19 21:41:12 +00004911 WriteLateParsedTemplates(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004912 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004913 WriteFPPragmaOptions(SemaRef.getFPOptions());
4914 WriteOpenCLExtensions(SemaRef);
Yaxun Liu5b746652016-12-18 05:18:55 +00004915 WriteOpenCLExtensionTypes(SemaRef);
4916 WriteOpenCLExtensionDecls(SemaRef);
Justin Lebar67a78a62016-10-08 22:15:58 +00004917 WriteCUDAPragmas(SemaRef);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004918
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004919 // If we're emitting a module, write out the submodule information.
4920 if (WritingModule)
4921 WriteSubmodules(WritingModule);
4922
Douglas Gregor5204bde2011-08-02 16:26:37 +00004923 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4924
Douglas Gregord4df8652009-04-22 22:02:47 +00004925 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004926 if (!EagerlyDeserializedDecls.empty())
4927 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004928
David Blaikief63556d2017-04-12 20:58:33 +00004929 if (!ModularCodegenDecls.empty())
David Blaikie9ffe5a32017-01-30 05:00:26 +00004930 Stream.EmitRecord(MODULAR_CODEGEN_DECLS, ModularCodegenDecls);
4931
Douglas Gregord4df8652009-04-22 22:02:47 +00004932 // Write the record containing tentative definitions.
4933 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004934 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004935
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004936 // Write the record containing unused file scoped decls.
4937 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004938 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004939
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004940 // Write the record containing weak undeclared identifiers.
4941 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004942 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004943 WeakUndeclaredIdentifiers);
4944
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004945 // Write the record containing ext_vector type names.
4946 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004947 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004948
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004949 // Write the record containing VTable uses information.
4950 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004951 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004952
Nico Weber72889432014-09-06 01:25:55 +00004953 // Write the record containing potentially unused local typedefs.
4954 if (!UnusedLocalTypedefNameCandidates.empty())
4955 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4956 UnusedLocalTypedefNameCandidates);
4957
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004958 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004959 if (!PendingInstantiations.empty())
4960 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004961
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004962 // Write the record containing declaration references of Sema.
4963 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004964 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004965
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004966 // Write the record containing CUDA-specific declaration references.
4967 if (!CUDASpecialDeclRefs.empty())
4968 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004969
4970 // Write the delegating constructors.
4971 if (!DelegatingCtorDecls.empty())
4972 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004973
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004974 // Write the known namespaces.
4975 if (!KnownNamespaces.empty())
4976 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004977
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004978 // Write the undefined internal functions and variables, and inline functions.
4979 if (!UndefinedButUsed.empty())
4980 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Ismail Pazarbasie5768d12015-05-18 19:59:11 +00004981
4982 if (!DeleteExprsToAnalyze.empty())
4983 Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4984
Douglas Gregor851443c2011-08-12 01:39:19 +00004985 // Write the visible updates to DeclContexts.
Richard Smithcd45dbc2014-04-19 03:48:30 +00004986 for (auto *DC : UpdatedDeclContexts)
4987 WriteDeclContextVisibleUpdate(DC);
Douglas Gregor851443c2011-08-12 01:39:19 +00004988
Douglas Gregor959bb062011-12-03 01:15:29 +00004989 if (!WritingModule) {
4990 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004991 struct ModuleInfo {
4992 uint64_t ID;
4993 Module *M;
4994 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4995 };
Richard Smith56be7542014-03-21 00:33:59 +00004996 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004997 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004998 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004999 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
5000 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00005001 }
Richard Smith56be7542014-03-21 00:33:59 +00005002
5003 if (!Imports.empty()) {
5004 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
5005 return A.ID < B.ID;
5006 };
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005007 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
5008 return A.ID == B.ID;
5009 };
Richard Smith56be7542014-03-21 00:33:59 +00005010
5011 // Sort and deduplicate module IDs.
5012 std::sort(Imports.begin(), Imports.end(), Cmp);
Ben Langmuir5f95c8f2014-09-08 20:36:26 +00005013 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Richard Smith56be7542014-03-21 00:33:59 +00005014 Imports.end());
5015
5016 RecordData ImportedModules;
5017 for (const auto &Import : Imports) {
5018 ImportedModules.push_back(Import.ID);
5019 // FIXME: If the module has macros imported then later has declarations
5020 // imported, this location won't be the right one as a location for the
5021 // declaration imports.
Richard Smith10434f32015-05-02 02:08:26 +00005022 AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
Richard Smith56be7542014-03-21 00:33:59 +00005023 }
5024
Douglas Gregor959bb062011-12-03 01:15:29 +00005025 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
5026 }
Douglas Gregor0a839132011-12-03 00:59:55 +00005027 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005028
Douglas Gregor404cdde2012-01-27 01:47:08 +00005029 WriteObjCCategories();
Nico Weber779355f2016-03-02 23:22:00 +00005030 if(!WritingModule) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00005031 WriteOptimizePragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005032 WriteMSStructPragmaOptions(SemaRef);
Nico Weber42932312016-03-03 00:17:35 +00005033 WriteMSPointersToMembersPragmaOptions(SemaRef);
Nico Weber779355f2016-03-02 23:22:00 +00005034 }
Alex Lorenz7d7e1e02017-03-31 15:36:21 +00005035 WritePackPragmaOptions(SemaRef);
Richard Smithe40f2ba2013-08-07 21:41:30 +00005036
Douglas Gregor08f01292009-04-17 22:13:46 +00005037 // Some simple statistics
Mehdi Amini57a41912015-09-10 01:46:39 +00005038 RecordData::value_type Record[] = {
5039 NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
Sebastian Redl539c5062010-08-18 23:57:32 +00005040 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00005041 Stream.ExitBlock();
Adrian Prantladbd2b12015-09-22 23:26:31 +00005042
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005043 // Write the module file extension blocks.
5044 for (const auto &ExtWriter : ModuleFileExtensionWriters)
Douglas Gregor8f64ca12015-12-08 22:43:32 +00005045 WriteModuleFileExtension(SemaRef, *ExtWriter);
Douglas Gregor6623e1f2015-11-03 18:33:07 +00005046
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +00005047 return writeUnhashedControlBlock(PP, Context);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005048}
5049
Richard Smithb9eab6d2014-03-20 19:44:17 +00005050void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005051 if (DeclUpdates.empty())
5052 return;
5053
Richard Smith59442b42014-03-20 20:07:19 +00005054 DeclUpdateMap LocalUpdates;
5055 LocalUpdates.swap(DeclUpdates);
5056
Richard Smith6ef42932014-03-20 21:02:00 +00005057 for (auto &DeclUpdate : LocalUpdates) {
5058 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00005059
Richard Smithd28ac5b2014-03-22 23:33:22 +00005060 bool HasUpdatedBody = false;
Richard Smith69c82bf2016-04-01 22:52:03 +00005061 RecordData RecordData;
5062 ASTRecordWriter Record(*this, RecordData);
Richard Smith6ef42932014-03-20 21:02:00 +00005063 for (auto &Update : DeclUpdate.second) {
5064 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
5065
Richard Smith69c82bf2016-04-01 22:52:03 +00005066 // An updated body is emitted last, so that the reader doesn't need
5067 // to skip over the lazy body to reach statements for other records.
5068 if (Kind == UPD_CXX_ADDED_FUNCTION_DEFINITION)
5069 HasUpdatedBody = true;
5070 else
5071 Record.push_back(Kind);
5072
Richard Smith6ef42932014-03-20 21:02:00 +00005073 switch (Kind) {
5074 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
5075 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
5076 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Richard Smithcd45dbc2014-04-19 03:48:30 +00005077 assert(Update.getDecl() && "no decl to add?");
Richard Smith6ef42932014-03-20 21:02:00 +00005078 Record.push_back(GetDeclRef(Update.getDecl()));
5079 break;
5080
Richard Smith4d235792014-08-07 18:53:08 +00005081 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Richard Smithd28ac5b2014-03-22 23:33:22 +00005082 break;
5083
Richard Smith05a21352017-06-22 22:18:46 +00005084 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER: {
5085 const VarDecl *VD = cast<VarDecl>(D);
Richard Smith69c82bf2016-04-01 22:52:03 +00005086 Record.AddSourceLocation(Update.getLoc());
Richard Smith05a21352017-06-22 22:18:46 +00005087 if (VD->getInit()) {
5088 Record.push_back(!VD->isInitKnownICE() ? 1
5089 : (VD->isInitICE() ? 3 : 2));
5090 Record.AddStmt(const_cast<Expr*>(VD->getInit()));
5091 } else {
5092 Record.push_back(0);
5093 }
Richard Smith4d235792014-08-07 18:53:08 +00005094 break;
Richard Smith05a21352017-06-22 22:18:46 +00005095 }
Richard Smith4d235792014-08-07 18:53:08 +00005096
John McCall32791cc2016-01-06 22:34:54 +00005097 case UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT:
Richard Smith290d8012016-04-06 17:06:00 +00005098 Record.AddStmt(const_cast<Expr *>(
5099 cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
John McCall32791cc2016-01-06 22:34:54 +00005100 break;
5101
Richard Smith4b054b22016-08-24 21:25:37 +00005102 case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
5103 Record.AddStmt(
5104 cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
5105 break;
5106
Richard Smithcd45dbc2014-04-19 03:48:30 +00005107 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
5108 auto *RD = cast<CXXRecordDecl>(D);
Chandler Carruth8a3d24d2015-03-26 04:27:10 +00005109 UpdatedDeclContexts.insert(RD->getPrimaryContext());
Richard Smith69c82bf2016-04-01 22:52:03 +00005110 Record.AddCXXDefinitionData(RD);
Richard Smith72e50fe2016-04-14 00:50:18 +00005111 Record.AddOffset(WriteDeclContextLexicalBlock(
Richard Smithcd45dbc2014-04-19 03:48:30 +00005112 *Context, const_cast<CXXRecordDecl *>(RD)));
5113
5114 // This state is sometimes updated by template instantiation, when we
5115 // switch from the specialization referring to the template declaration
5116 // to it referring to the template definition.
5117 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
5118 Record.push_back(MSInfo->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005119 Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005120 } else {
5121 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
5122 Record.push_back(Spec->getTemplateSpecializationKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005123 Record.AddSourceLocation(Spec->getPointOfInstantiation());
Richard Smithdf352052014-05-22 20:59:29 +00005124
5125 // The instantiation might have been resolved to a partial
5126 // specialization. If so, record which one.
5127 auto From = Spec->getInstantiatedFrom();
5128 if (auto PartialSpec =
5129 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
5130 Record.push_back(true);
Richard Smith69c82bf2016-04-01 22:52:03 +00005131 Record.AddDeclRef(PartialSpec);
5132 Record.AddTemplateArgumentList(
5133 &Spec->getTemplateInstantiationArgs());
Richard Smithdf352052014-05-22 20:59:29 +00005134 } else {
5135 Record.push_back(false);
5136 }
Richard Smithcd45dbc2014-04-19 03:48:30 +00005137 }
5138 Record.push_back(RD->getTagKind());
Richard Smith69c82bf2016-04-01 22:52:03 +00005139 Record.AddSourceLocation(RD->getLocation());
5140 Record.AddSourceLocation(RD->getLocStart());
Argyrios Kyrtzidisd798c052016-07-15 18:11:33 +00005141 Record.AddSourceRange(RD->getBraceRange());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005142
5143 // Instantiation may change attributes; write them all out afresh.
5144 Record.push_back(D->hasAttrs());
Richard Smith69c82bf2016-04-01 22:52:03 +00005145 if (D->hasAttrs())
5146 Record.AddAttributes(D->getAttrs());
Richard Smithcd45dbc2014-04-19 03:48:30 +00005147
5148 // FIXME: Ensure we don't get here for explicit instantiations.
5149 break;
5150 }
5151
Richard Smithf8134002015-03-10 01:41:22 +00005152 case UPD_CXX_RESOLVED_DTOR_DELETE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005153 Record.AddDeclRef(Update.getDecl());
Richard Smithf8134002015-03-10 01:41:22 +00005154 break;
5155
Richard Smith564417a2014-03-20 21:47:22 +00005156 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
5157 addExceptionSpec(
Richard Smith564417a2014-03-20 21:47:22 +00005158 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
5159 Record);
5160 break;
5161
Richard Smith6ef42932014-03-20 21:02:00 +00005162 case UPD_CXX_DEDUCED_RETURN_TYPE:
5163 Record.push_back(GetOrCreateTypeID(Update.getType()));
5164 break;
5165
5166 case UPD_DECL_MARKED_USED:
5167 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00005168
5169 case UPD_MANGLING_NUMBER:
5170 case UPD_STATIC_LOCAL_NUMBER:
5171 Record.push_back(Update.getNumber());
5172 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005173
Alexey Bataev97720002014-11-11 04:05:39 +00005174 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
Richard Smith69c82bf2016-04-01 22:52:03 +00005175 Record.AddSourceRange(
5176 D->getAttr<OMPThreadPrivateDeclAttr>()->getRange());
Alexey Bataev97720002014-11-11 04:05:39 +00005177 break;
Richard Smith65ebb4a2015-03-26 04:09:53 +00005178
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00005179 case UPD_DECL_MARKED_OPENMP_DECLARETARGET:
5180 Record.AddSourceRange(
5181 D->getAttr<OMPDeclareTargetDeclAttr>()->getRange());
5182 break;
5183
Richard Smith65ebb4a2015-03-26 04:09:53 +00005184 case UPD_DECL_EXPORTED:
Richard Smith4caa4492015-05-15 02:34:32 +00005185 Record.push_back(getSubmoduleID(Update.getModule()));
Richard Smith65ebb4a2015-03-26 04:09:53 +00005186 break;
Alex Denisovfde64952015-06-26 05:28:36 +00005187
5188 case UPD_ADDED_ATTR_TO_RECORD:
Richard Smith69c82bf2016-04-01 22:52:03 +00005189 Record.AddAttributes(llvm::makeArrayRef(Update.getAttr()));
Alex Denisovfde64952015-06-26 05:28:36 +00005190 break;
Richard Smith6ef42932014-03-20 21:02:00 +00005191 }
5192 }
5193
Richard Smithd28ac5b2014-03-22 23:33:22 +00005194 if (HasUpdatedBody) {
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005195 const auto *Def = cast<FunctionDecl>(D);
Richard Smith4d235792014-08-07 18:53:08 +00005196 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005197 Record.push_back(Def->isInlined());
Richard Smith69c82bf2016-04-01 22:52:03 +00005198 Record.AddSourceLocation(Def->getInnerLocStart());
Richard Smith290d8012016-04-06 17:06:00 +00005199 Record.AddFunctionDefinition(Def);
Richard Smithd28ac5b2014-03-22 23:33:22 +00005200 }
5201
Richard Smithcd45dbc2014-04-19 03:48:30 +00005202 OffsetsRecord.push_back(GetDeclRef(D));
Richard Smith69c82bf2016-04-01 22:52:03 +00005203 OffsetsRecord.push_back(Record.Emit(DECL_UPDATES));
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005204 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005205}
5206
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005207void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Richard Smithb22a1d12016-03-27 20:13:24 +00005208 uint32_t Raw = Loc.getRawEncoding();
5209 Record.push_back((Raw << 1) | (Raw >> 31));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005210}
5211
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005212void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005213 AddSourceLocation(Range.getBegin(), Record);
5214 AddSourceLocation(Range.getEnd(), Record);
5215}
5216
Richard Smithf144b542016-04-08 21:54:32 +00005217void ASTRecordWriter::AddAPInt(const llvm::APInt &Value) {
5218 Record->push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00005219 const uint64_t *Words = Value.getRawData();
Richard Smithf144b542016-04-08 21:54:32 +00005220 Record->append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005221}
5222
Richard Smithf144b542016-04-08 21:54:32 +00005223void ASTRecordWriter::AddAPSInt(const llvm::APSInt &Value) {
5224 Record->push_back(Value.isUnsigned());
5225 AddAPInt(Value);
Douglas Gregor1daeb692009-04-13 18:14:40 +00005226}
5227
Richard Smithf144b542016-04-08 21:54:32 +00005228void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
5229 AddAPInt(Value.bitcastToAPInt());
Douglas Gregore0a3a512009-04-14 21:55:33 +00005230}
5231
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005232void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005233 Record.push_back(getIdentifierRef(II));
5234}
5235
Sebastian Redl539c5062010-08-18 23:57:32 +00005236IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Craig Toppera13603a2014-05-22 05:54:18 +00005237 if (!II)
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005238 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005239
Sebastian Redl539c5062010-08-18 23:57:32 +00005240 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00005241 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00005242 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00005243 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005244}
5245
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005246MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005247 // Don't emit builtin macros like __LINE__ to the AST file unless they
5248 // have been redefined by the header (in which case they are not
5249 // isBuiltinMacro).
Craig Toppera13603a2014-05-22 05:54:18 +00005250 if (!MI || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005251 return 0;
5252
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005253 MacroID &ID = MacroIDs[MI];
5254 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005255 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005256 MacroInfoToEmitData Info = { Name, MI, ID };
5257 MacroInfosToEmit.push_back(Info);
5258 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005259 return ID;
5260}
5261
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005262MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Craig Toppera13603a2014-05-22 05:54:18 +00005263 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005264 return 0;
5265
5266 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
5267 return MacroIDs[MI];
5268}
5269
5270uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
Richard Smithd7329392015-04-21 21:46:32 +00005271 return IdentMacroDirectivesOffsetMap.lookup(Name);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005272}
5273
Richard Smithe64e7452016-04-18 21:54:58 +00005274void ASTRecordWriter::AddSelectorRef(const Selector SelRef) {
5275 Record->push_back(Writer->getSelectorRef(SelRef));
Sebastian Redl834bb972010-08-04 17:20:04 +00005276}
5277
Sebastian Redl539c5062010-08-18 23:57:32 +00005278SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Craig Toppera13603a2014-05-22 05:54:18 +00005279 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl834bb972010-08-04 17:20:04 +00005280 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00005281 }
5282
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005283 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005284 if (SID == 0 && Chain) {
5285 // This might trigger a ReadSelector callback, which will set the ID for
5286 // this selector.
5287 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005288 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00005289 }
Steve Naroff2ddea052009-04-23 10:39:46 +00005290 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00005291 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005292 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005293 }
Sebastian Redl834bb972010-08-04 17:20:04 +00005294 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00005295}
5296
Richard Smithe64e7452016-04-18 21:54:58 +00005297void ASTRecordWriter::AddCXXTemporary(const CXXTemporary *Temp) {
5298 AddDeclRef(Temp->getDestructor());
Chris Lattnercba86142010-05-10 00:25:06 +00005299}
5300
Richard Smith290d8012016-04-06 17:06:00 +00005301void ASTRecordWriter::AddTemplateArgumentLocInfo(
5302 TemplateArgument::ArgKind Kind, const TemplateArgumentLocInfo &Arg) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005303 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00005304 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005305 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00005306 break;
5307 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005308 AddTypeSourceInfo(Arg.getAsTypeSourceInfo());
John McCall0ad16662009-10-29 08:12:44 +00005309 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005310 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005311 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5312 AddSourceLocation(Arg.getTemplateNameLoc());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005313 break;
5314 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005315 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc());
5316 AddSourceLocation(Arg.getTemplateNameLoc());
5317 AddSourceLocation(Arg.getTemplateEllipsisLoc());
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005318 break;
John McCall0ad16662009-10-29 08:12:44 +00005319 case TemplateArgument::Null:
5320 case TemplateArgument::Integral:
5321 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00005322 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00005323 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00005324 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00005325 break;
5326 }
5327}
5328
Richard Smith290d8012016-04-06 17:06:00 +00005329void ASTRecordWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg) {
5330 AddTemplateArgument(Arg.getArgument());
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005331
5332 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5333 bool InfoHasSameExpr
5334 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
Richard Smith290d8012016-04-06 17:06:00 +00005335 Record->push_back(InfoHasSameExpr);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00005336 if (InfoHasSameExpr)
5337 return; // Avoid storing the same expr twice.
5338 }
Richard Smith290d8012016-04-06 17:06:00 +00005339 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo());
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00005340}
5341
Richard Smith290d8012016-04-06 17:06:00 +00005342void ASTRecordWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo) {
Craig Toppera13603a2014-05-22 05:54:18 +00005343 if (!TInfo) {
Richard Smith290d8012016-04-06 17:06:00 +00005344 AddTypeRef(QualType());
John McCall8f115c62009-10-16 21:56:05 +00005345 return;
5346 }
5347
Richard Smith290d8012016-04-06 17:06:00 +00005348 AddTypeLoc(TInfo->getTypeLoc());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005349}
5350
Richard Smith290d8012016-04-06 17:06:00 +00005351void ASTRecordWriter::AddTypeLoc(TypeLoc TL) {
5352 AddTypeRef(TL.getType());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005353
Richard Smith290d8012016-04-06 17:06:00 +00005354 TypeLocWriter TLW(*this);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005355 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00005356 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00005357}
5358
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005359void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00005360 Record.push_back(GetOrCreateTypeID(T));
5361}
5362
Richard Smith2095ffe2015-03-16 20:11:03 +00005363TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00005364 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005365 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5366 if (T.isNull())
5367 return TypeIdx();
5368 assert(!T.getLocalFastQualifiers());
5369
5370 TypeIdx &Idx = TypeIdxs[T];
5371 if (Idx.getIndex() == 0) {
5372 if (DoneWritingDeclsAndTypes) {
5373 assert(0 && "New type seen after serializing all the types to emit!");
5374 return TypeIdx();
5375 }
5376
5377 // We haven't seen this type before. Assign it a new ID and put it
5378 // into the queue of types to emit.
5379 Idx = TypeIdx(NextTypeID++);
5380 DeclTypesToEmit.push(T);
5381 }
5382 return Idx;
5383 });
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00005384}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005385
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00005386TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00005387 assert(Context);
Richard Smith2095ffe2015-03-16 20:11:03 +00005388 return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
5389 if (T.isNull())
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005390 return TypeIdx();
Richard Smith2095ffe2015-03-16 20:11:03 +00005391 assert(!T.getLocalFastQualifiers());
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005392
Richard Smith2095ffe2015-03-16 20:11:03 +00005393 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5394 assert(I != TypeIdxs.end() && "Type not emitted!");
5395 return I->second;
5396 });
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005397}
5398
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005399void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005400 Record.push_back(GetDeclRef(D));
5401}
5402
Sebastian Redl539c5062010-08-18 23:57:32 +00005403DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005404 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Craig Toppera13603a2014-05-22 05:54:18 +00005405
5406 if (!D) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005407 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005408 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00005409
5410 // If D comes from an AST file, its declaration ID is already known and
5411 // fixed.
5412 if (D->isFromASTFile())
5413 return D->getGlobalID();
5414
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005415 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00005416 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00005417 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005418 if (DoneWritingDeclsAndTypes) {
5419 assert(0 && "New decl seen after serializing all the decls to emit!");
5420 return 0;
5421 }
5422
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005423 // We haven't seen this declaration before. Give it a new ID and
5424 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00005425 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00005426 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005427 }
5428
Sebastian Redl66c5eef2010-07-27 00:17:23 +00005429 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005430}
5431
Sebastian Redl539c5062010-08-18 23:57:32 +00005432DeclID ASTWriter::getDeclID(const Decl *D) {
Craig Toppera13603a2014-05-22 05:54:18 +00005433 if (!D)
Douglas Gregore84a9da2009-04-20 20:36:09 +00005434 return 0;
5435
Douglas Gregorb3163e52012-01-05 22:33:30 +00005436 // If D comes from an AST file, its declaration ID is already known and
5437 // fixed.
5438 if (D->isFromASTFile())
5439 return D->getGlobalID();
5440
Douglas Gregore84a9da2009-04-20 20:36:09 +00005441 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5442 return DeclIDs[D];
5443}
5444
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005445void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005446 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005447 assert(D);
5448
5449 SourceLocation Loc = D->getLocation();
5450 if (Loc.isInvalid())
5451 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005452
5453 // We only keep track of the file-level declarations of each file.
5454 if (!D->getLexicalDeclContext()->isFileContext())
5455 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00005456 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5457 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00005458 if (isa<ParmVarDecl>(D))
5459 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005460
5461 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00005462 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005463 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005464 FileID FID;
5465 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00005466 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005467 if (FID.isInvalid())
5468 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005469 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005470
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00005471 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005472 if (!Info)
5473 Info = new DeclIDInFileInfo();
5474
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005475 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005476 LocDeclIDsTy &Decls = Info->DeclIDs;
5477
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00005478 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005479 Decls.push_back(LocDecl);
5480 return;
5481 }
5482
Benjamin Kramer45025c02013-08-24 13:22:59 +00005483 LocDeclIDsTy::iterator I =
5484 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00005485
5486 Decls.insert(I, LocDecl);
5487}
5488
Richard Smithe64e7452016-04-18 21:54:58 +00005489void ASTRecordWriter::AddDeclarationName(DeclarationName Name) {
Chris Lattner258172e2009-04-27 07:35:58 +00005490 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Richard Smithe64e7452016-04-18 21:54:58 +00005491 Record->push_back(Name.getNameKind());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005492 switch (Name.getNameKind()) {
5493 case DeclarationName::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005494 AddIdentifierRef(Name.getAsIdentifierInfo());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005495 break;
5496
5497 case DeclarationName::ObjCZeroArgSelector:
5498 case DeclarationName::ObjCOneArgSelector:
5499 case DeclarationName::ObjCMultiArgSelector:
Richard Smithe64e7452016-04-18 21:54:58 +00005500 AddSelectorRef(Name.getObjCSelector());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005501 break;
5502
5503 case DeclarationName::CXXConstructorName:
5504 case DeclarationName::CXXDestructorName:
5505 case DeclarationName::CXXConversionFunctionName:
Richard Smithe64e7452016-04-18 21:54:58 +00005506 AddTypeRef(Name.getCXXNameType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005507 break;
5508
Richard Smith35845152017-02-07 01:37:30 +00005509 case DeclarationName::CXXDeductionGuideName:
5510 AddDeclRef(Name.getCXXDeductionGuideTemplate());
5511 break;
5512
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005513 case DeclarationName::CXXOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005514 Record->push_back(Name.getCXXOverloadedOperator());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005515 break;
5516
Alexis Hunt3d221f22009-11-29 07:34:05 +00005517 case DeclarationName::CXXLiteralOperatorName:
Richard Smithe64e7452016-04-18 21:54:58 +00005518 AddIdentifierRef(Name.getCXXLiteralIdentifier());
Alexis Hunt3d221f22009-11-29 07:34:05 +00005519 break;
5520
Douglas Gregoref84c4b2009-04-09 22:27:44 +00005521 case DeclarationName::CXXUsingDirective:
5522 // No extra data to emit
5523 break;
5524 }
5525}
Chris Lattnerca025db2010-05-07 21:43:38 +00005526
Richard Smithd08aeb62014-08-28 01:33:39 +00005527unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5528 assert(needsAnonymousDeclarationNumber(D) &&
5529 "expected an anonymous declaration");
5530
5531 // Number the anonymous declarations within this context, if we've not
5532 // already done so.
5533 auto It = AnonymousDeclarationNumbers.find(D);
5534 if (It == AnonymousDeclarationNumbers.end()) {
Richard Smith2b560572015-02-07 03:11:11 +00005535 auto *DC = D->getLexicalDeclContext();
5536 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5537 AnonymousDeclarationNumbers[ND] = Number;
5538 });
Richard Smithd08aeb62014-08-28 01:33:39 +00005539
5540 It = AnonymousDeclarationNumbers.find(D);
5541 assert(It != AnonymousDeclarationNumbers.end() &&
5542 "declaration not found within its lexical context");
5543 }
5544
5545 return It->second;
5546}
5547
Richard Smith290d8012016-04-06 17:06:00 +00005548void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5549 DeclarationName Name) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005550 switch (Name.getNameKind()) {
5551 case DeclarationName::CXXConstructorName:
5552 case DeclarationName::CXXDestructorName:
5553 case DeclarationName::CXXConversionFunctionName:
Richard Smith290d8012016-04-06 17:06:00 +00005554 AddTypeSourceInfo(DNLoc.NamedType.TInfo);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005555 break;
5556
5557 case DeclarationName::CXXOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005558 AddSourceLocation(SourceLocation::getFromRawEncoding(
5559 DNLoc.CXXOperatorName.BeginOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005560 AddSourceLocation(
Richard Smith290d8012016-04-06 17:06:00 +00005561 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005562 break;
5563
5564 case DeclarationName::CXXLiteralOperatorName:
Richard Smith290d8012016-04-06 17:06:00 +00005565 AddSourceLocation(SourceLocation::getFromRawEncoding(
5566 DNLoc.CXXLiteralOperatorName.OpNameLoc));
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005567 break;
5568
5569 case DeclarationName::Identifier:
5570 case DeclarationName::ObjCZeroArgSelector:
5571 case DeclarationName::ObjCOneArgSelector:
5572 case DeclarationName::ObjCMultiArgSelector:
5573 case DeclarationName::CXXUsingDirective:
Richard Smith35845152017-02-07 01:37:30 +00005574 case DeclarationName::CXXDeductionGuideName:
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005575 break;
5576 }
5577}
5578
Richard Smith290d8012016-04-06 17:06:00 +00005579void ASTRecordWriter::AddDeclarationNameInfo(
5580 const DeclarationNameInfo &NameInfo) {
5581 AddDeclarationName(NameInfo.getName());
5582 AddSourceLocation(NameInfo.getLoc());
5583 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName());
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005584}
5585
Richard Smith290d8012016-04-06 17:06:00 +00005586void ASTRecordWriter::AddQualifierInfo(const QualifierInfo &Info) {
5587 AddNestedNameSpecifierLoc(Info.QualifierLoc);
5588 Record->push_back(Info.NumTemplParamLists);
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005589 for (unsigned i = 0, e = Info.NumTemplParamLists; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005590 AddTemplateParameterList(Info.TemplParamLists[i]);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00005591}
5592
Richard Smithe64e7452016-04-18 21:54:58 +00005593void ASTRecordWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS) {
Chris Lattnerca025db2010-05-07 21:43:38 +00005594 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005595 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005596 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00005597
5598 // Push each of the NNS's onto a stack for serialization in reverse order.
5599 while (NNS) {
5600 NestedNames.push_back(NNS);
5601 NNS = NNS->getPrefix();
5602 }
5603
Richard Smithe64e7452016-04-18 21:54:58 +00005604 Record->push_back(NestedNames.size());
Chris Lattnerca025db2010-05-07 21:43:38 +00005605 while(!NestedNames.empty()) {
5606 NNS = NestedNames.pop_back_val();
5607 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
Richard Smithe64e7452016-04-18 21:54:58 +00005608 Record->push_back(Kind);
Chris Lattnerca025db2010-05-07 21:43:38 +00005609 switch (Kind) {
5610 case NestedNameSpecifier::Identifier:
Richard Smithe64e7452016-04-18 21:54:58 +00005611 AddIdentifierRef(NNS->getAsIdentifier());
Chris Lattnerca025db2010-05-07 21:43:38 +00005612 break;
5613
5614 case NestedNameSpecifier::Namespace:
Richard Smithe64e7452016-04-18 21:54:58 +00005615 AddDeclRef(NNS->getAsNamespace());
Chris Lattnerca025db2010-05-07 21:43:38 +00005616 break;
5617
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005618 case NestedNameSpecifier::NamespaceAlias:
Richard Smithe64e7452016-04-18 21:54:58 +00005619 AddDeclRef(NNS->getAsNamespaceAlias());
Douglas Gregor7b26ff92011-02-24 02:36:08 +00005620 break;
5621
Chris Lattnerca025db2010-05-07 21:43:38 +00005622 case NestedNameSpecifier::TypeSpec:
5623 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smithe64e7452016-04-18 21:54:58 +00005624 AddTypeRef(QualType(NNS->getAsType(), 0));
5625 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
Chris Lattnerca025db2010-05-07 21:43:38 +00005626 break;
5627
5628 case NestedNameSpecifier::Global:
5629 // Don't need to write an associated value.
5630 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005631
5632 case NestedNameSpecifier::Super:
Richard Smithe64e7452016-04-18 21:54:58 +00005633 AddDeclRef(NNS->getAsRecordDecl());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005634 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00005635 }
5636 }
5637}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005638
Richard Smith290d8012016-04-06 17:06:00 +00005639void ASTRecordWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS) {
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005640 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00005641 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005642 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005643
5644 // Push each of the nested-name-specifiers's onto a stack for
5645 // serialization in reverse order.
5646 while (NNS) {
5647 NestedNames.push_back(NNS);
5648 NNS = NNS.getPrefix();
5649 }
5650
Richard Smith290d8012016-04-06 17:06:00 +00005651 Record->push_back(NestedNames.size());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005652 while(!NestedNames.empty()) {
5653 NNS = NestedNames.pop_back_val();
5654 NestedNameSpecifier::SpecifierKind Kind
5655 = NNS.getNestedNameSpecifier()->getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005656 Record->push_back(Kind);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005657 switch (Kind) {
5658 case NestedNameSpecifier::Identifier:
Richard Smith290d8012016-04-06 17:06:00 +00005659 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier());
5660 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005661 break;
5662
5663 case NestedNameSpecifier::Namespace:
Richard Smith290d8012016-04-06 17:06:00 +00005664 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace());
5665 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005666 break;
5667
5668 case NestedNameSpecifier::NamespaceAlias:
Richard Smith290d8012016-04-06 17:06:00 +00005669 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias());
5670 AddSourceRange(NNS.getLocalSourceRange());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005671 break;
5672
5673 case NestedNameSpecifier::TypeSpec:
5674 case NestedNameSpecifier::TypeSpecWithTemplate:
Richard Smith290d8012016-04-06 17:06:00 +00005675 Record->push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5676 AddTypeLoc(NNS.getTypeLoc());
5677 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005678 break;
5679
5680 case NestedNameSpecifier::Global:
Richard Smith290d8012016-04-06 17:06:00 +00005681 AddSourceLocation(NNS.getLocalSourceRange().getEnd());
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005682 break;
Nikola Smiljanic67860242014-09-26 00:28:20 +00005683
5684 case NestedNameSpecifier::Super:
Richard Smith290d8012016-04-06 17:06:00 +00005685 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl());
5686 AddSourceRange(NNS.getLocalSourceRange());
Nikola Smiljanic67860242014-09-26 00:28:20 +00005687 break;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00005688 }
5689 }
5690}
5691
Richard Smith290d8012016-04-06 17:06:00 +00005692void ASTRecordWriter::AddTemplateName(TemplateName Name) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005693 TemplateName::NameKind Kind = Name.getKind();
Richard Smith290d8012016-04-06 17:06:00 +00005694 Record->push_back(Kind);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005695 switch (Kind) {
5696 case TemplateName::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005697 AddDeclRef(Name.getAsTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005698 break;
5699
5700 case TemplateName::OverloadedTemplate: {
5701 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
Richard Smith290d8012016-04-06 17:06:00 +00005702 Record->push_back(OvT->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005703 for (const auto &I : *OvT)
Richard Smith290d8012016-04-06 17:06:00 +00005704 AddDeclRef(I);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005705 break;
5706 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005707
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005708 case TemplateName::QualifiedTemplate: {
5709 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005710 AddNestedNameSpecifier(QualT->getQualifier());
5711 Record->push_back(QualT->hasTemplateKeyword());
5712 AddDeclRef(QualT->getTemplateDecl());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005713 break;
5714 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005715
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005716 case TemplateName::DependentTemplate: {
5717 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
Richard Smith290d8012016-04-06 17:06:00 +00005718 AddNestedNameSpecifier(DepT->getQualifier());
5719 Record->push_back(DepT->isIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005720 if (DepT->isIdentifier())
Richard Smith290d8012016-04-06 17:06:00 +00005721 AddIdentifierRef(DepT->getIdentifier());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005722 else
Richard Smith290d8012016-04-06 17:06:00 +00005723 Record->push_back(DepT->getOperator());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005724 break;
5725 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005726
5727 case TemplateName::SubstTemplateTemplateParm: {
5728 SubstTemplateTemplateParmStorage *subst
5729 = Name.getAsSubstTemplateTemplateParm();
Richard Smith290d8012016-04-06 17:06:00 +00005730 AddDeclRef(subst->getParameter());
5731 AddTemplateName(subst->getReplacement());
John McCalld9dfe3a2011-06-30 08:33:18 +00005732 break;
5733 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005734
5735 case TemplateName::SubstTemplateTemplateParmPack: {
5736 SubstTemplateTemplateParmPackStorage *SubstPack
5737 = Name.getAsSubstTemplateTemplateParmPack();
Richard Smith290d8012016-04-06 17:06:00 +00005738 AddDeclRef(SubstPack->getParameterPack());
5739 AddTemplateArgument(SubstPack->getArgumentPack());
Douglas Gregor5590be02011-01-15 06:45:20 +00005740 break;
5741 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005742 }
5743}
5744
Richard Smith290d8012016-04-06 17:06:00 +00005745void ASTRecordWriter::AddTemplateArgument(const TemplateArgument &Arg) {
5746 Record->push_back(Arg.getKind());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005747 switch (Arg.getKind()) {
5748 case TemplateArgument::Null:
5749 break;
5750 case TemplateArgument::Type:
Richard Smith290d8012016-04-06 17:06:00 +00005751 AddTypeRef(Arg.getAsType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005752 break;
5753 case TemplateArgument::Declaration:
Richard Smith290d8012016-04-06 17:06:00 +00005754 AddDeclRef(Arg.getAsDecl());
5755 AddTypeRef(Arg.getParamTypeForDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +00005756 break;
5757 case TemplateArgument::NullPtr:
Richard Smith290d8012016-04-06 17:06:00 +00005758 AddTypeRef(Arg.getNullPtrType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005759 break;
5760 case TemplateArgument::Integral:
Richard Smith290d8012016-04-06 17:06:00 +00005761 AddAPSInt(Arg.getAsIntegral());
5762 AddTypeRef(Arg.getIntegralType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005763 break;
5764 case TemplateArgument::Template:
Richard Smith290d8012016-04-06 17:06:00 +00005765 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
Douglas Gregore1d60df2011-01-14 23:41:42 +00005766 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005767 case TemplateArgument::TemplateExpansion:
Richard Smith290d8012016-04-06 17:06:00 +00005768 AddTemplateName(Arg.getAsTemplateOrTemplatePattern());
David Blaikie05785d12013-02-20 22:23:23 +00005769 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Richard Smith290d8012016-04-06 17:06:00 +00005770 Record->push_back(*NumExpansions + 1);
Douglas Gregore1d60df2011-01-14 23:41:42 +00005771 else
Richard Smith290d8012016-04-06 17:06:00 +00005772 Record->push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005773 break;
5774 case TemplateArgument::Expression:
5775 AddStmt(Arg.getAsExpr());
5776 break;
5777 case TemplateArgument::Pack:
Richard Smith290d8012016-04-06 17:06:00 +00005778 Record->push_back(Arg.pack_size());
Aaron Ballman2a89e852014-07-15 21:32:31 +00005779 for (const auto &P : Arg.pack_elements())
Richard Smith290d8012016-04-06 17:06:00 +00005780 AddTemplateArgument(P);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005781 break;
5782 }
5783}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005784
Richard Smithe64e7452016-04-18 21:54:58 +00005785void ASTRecordWriter::AddTemplateParameterList(
5786 const TemplateParameterList *TemplateParams) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005787 assert(TemplateParams && "No TemplateParams!");
Richard Smithe64e7452016-04-18 21:54:58 +00005788 AddSourceLocation(TemplateParams->getTemplateLoc());
5789 AddSourceLocation(TemplateParams->getLAngleLoc());
5790 AddSourceLocation(TemplateParams->getRAngleLoc());
Hubert Tonge4a0c0e2016-07-30 22:33:34 +00005791 // TODO: Concepts
Richard Smithe64e7452016-04-18 21:54:58 +00005792 Record->push_back(TemplateParams->size());
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00005793 for (const auto &P : *TemplateParams)
Richard Smithe64e7452016-04-18 21:54:58 +00005794 AddDeclRef(P);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005795}
5796
5797/// \brief Emit a template argument list.
Richard Smith290d8012016-04-06 17:06:00 +00005798void ASTRecordWriter::AddTemplateArgumentList(
5799 const TemplateArgumentList *TemplateArgs) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005800 assert(TemplateArgs && "No TemplateArgs!");
Richard Smith290d8012016-04-06 17:06:00 +00005801 Record->push_back(TemplateArgs->size());
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005802 for (int i = 0, e = TemplateArgs->size(); i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005803 AddTemplateArgument(TemplateArgs->get(i));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005804}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005805
Richard Smith290d8012016-04-06 17:06:00 +00005806void ASTRecordWriter::AddASTTemplateArgumentListInfo(
5807 const ASTTemplateArgumentListInfo *ASTTemplArgList) {
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005808 assert(ASTTemplArgList && "No ASTTemplArgList!");
Richard Smith290d8012016-04-06 17:06:00 +00005809 AddSourceLocation(ASTTemplArgList->LAngleLoc);
5810 AddSourceLocation(ASTTemplArgList->RAngleLoc);
5811 Record->push_back(ASTTemplArgList->NumTemplateArgs);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005812 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
Eugene Zelenkoe95e7d52016-09-07 21:53:17 +00005813 for (int i = 0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
Richard Smith290d8012016-04-06 17:06:00 +00005814 AddTemplateArgumentLoc(TemplArgs[i]);
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005815}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005816
Richard Smithe64e7452016-04-18 21:54:58 +00005817void ASTRecordWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set) {
5818 Record->push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005819 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005820 I = Set.begin(), E = Set.end(); I != E; ++I) {
Richard Smithe64e7452016-04-18 21:54:58 +00005821 AddDeclRef(I.getDecl());
5822 Record->push_back(I.getAccess());
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005823 }
5824}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005825
Richard Smith290d8012016-04-06 17:06:00 +00005826// FIXME: Move this out of the main ASTRecordWriter interface.
5827void ASTRecordWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base) {
5828 Record->push_back(Base.isVirtual());
5829 Record->push_back(Base.isBaseOfClass());
5830 Record->push_back(Base.getAccessSpecifierAsWritten());
5831 Record->push_back(Base.getInheritConstructors());
5832 AddTypeSourceInfo(Base.getTypeSourceInfo());
5833 AddSourceRange(Base.getSourceRange());
Douglas Gregor752a5952011-01-03 22:36:02 +00005834 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005835 : SourceLocation());
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005836}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005837
Richard Smith645d2cf2016-04-14 00:29:55 +00005838static uint64_t EmitCXXBaseSpecifiers(ASTWriter &W,
5839 ArrayRef<CXXBaseSpecifier> Bases) {
5840 ASTWriter::RecordData Record;
5841 ASTRecordWriter Writer(W, Record);
5842 Writer.push_back(Bases.size());
Dmitry Polukhin790b5402016-04-06 10:01:46 +00005843
Richard Smith645d2cf2016-04-14 00:29:55 +00005844 for (auto &Base : Bases)
5845 Writer.AddCXXBaseSpecifier(Base);
Richard Smith290d8012016-04-06 17:06:00 +00005846
Richard Smith645d2cf2016-04-14 00:29:55 +00005847 return Writer.Emit(serialization::DECL_CXX_BASE_SPECIFIERS);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005848}
5849
Richard Smith290d8012016-04-06 17:06:00 +00005850// FIXME: Move this out of the main ASTRecordWriter interface.
Richard Smith645d2cf2016-04-14 00:29:55 +00005851void ASTRecordWriter::AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases) {
5852 AddOffset(EmitCXXBaseSpecifiers(*Writer, Bases));
5853}
5854
Richard Smithaa165cf2016-04-13 21:57:08 +00005855static uint64_t
5856EmitCXXCtorInitializers(ASTWriter &W,
5857 ArrayRef<CXXCtorInitializer *> CtorInits) {
5858 ASTWriter::RecordData Record;
5859 ASTRecordWriter Writer(W, Record);
5860 Writer.push_back(CtorInits.size());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005861
Richard Smithaa165cf2016-04-13 21:57:08 +00005862 for (auto *Init : CtorInits) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005863 if (Init->isBaseInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005864 Writer.push_back(CTOR_INITIALIZER_BASE);
5865 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
5866 Writer.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005867 } else if (Init->isDelegatingInitializer()) {
Richard Smithaa165cf2016-04-13 21:57:08 +00005868 Writer.push_back(CTOR_INITIALIZER_DELEGATING);
5869 Writer.AddTypeSourceInfo(Init->getTypeSourceInfo());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005870 } else if (Init->isMemberInitializer()){
Richard Smithaa165cf2016-04-13 21:57:08 +00005871 Writer.push_back(CTOR_INITIALIZER_MEMBER);
5872 Writer.AddDeclRef(Init->getMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005873 } else {
Richard Smithaa165cf2016-04-13 21:57:08 +00005874 Writer.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5875 Writer.AddDeclRef(Init->getIndirectMember());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005876 }
Francois Pichetd583da02010-12-04 09:14:42 +00005877
Richard Smithaa165cf2016-04-13 21:57:08 +00005878 Writer.AddSourceLocation(Init->getMemberLocation());
5879 Writer.AddStmt(Init->getInit());
5880 Writer.AddSourceLocation(Init->getLParenLoc());
5881 Writer.AddSourceLocation(Init->getRParenLoc());
5882 Writer.push_back(Init->isWritten());
Richard Smith30e304e2016-12-14 00:03:17 +00005883 if (Init->isWritten())
Richard Smithaa165cf2016-04-13 21:57:08 +00005884 Writer.push_back(Init->getSourceOrder());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005885 }
Richard Smithaa165cf2016-04-13 21:57:08 +00005886
5887 return Writer.Emit(serialization::DECL_CXX_CTOR_INITIALIZERS);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005888}
5889
Richard Smithaa165cf2016-04-13 21:57:08 +00005890// FIXME: Move this out of the main ASTRecordWriter interface.
5891void ASTRecordWriter::AddCXXCtorInitializers(
5892 ArrayRef<CXXCtorInitializer *> CtorInits) {
5893 AddOffset(EmitCXXCtorInitializers(*Writer, CtorInits));
Richard Smithc2bb8182015-03-24 06:36:48 +00005894}
5895
Richard Smith290d8012016-04-06 17:06:00 +00005896void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
Richard Smith053f6c62014-05-16 23:01:30 +00005897 auto &Data = D->data();
Richard Smith290d8012016-04-06 17:06:00 +00005898 Record->push_back(Data.IsLambda);
5899 Record->push_back(Data.UserDeclaredConstructor);
5900 Record->push_back(Data.UserDeclaredSpecialMembers);
5901 Record->push_back(Data.Aggregate);
5902 Record->push_back(Data.PlainOldData);
5903 Record->push_back(Data.Empty);
5904 Record->push_back(Data.Polymorphic);
5905 Record->push_back(Data.Abstract);
5906 Record->push_back(Data.IsStandardLayout);
5907 Record->push_back(Data.HasNoNonEmptyBases);
5908 Record->push_back(Data.HasPrivateFields);
5909 Record->push_back(Data.HasProtectedFields);
5910 Record->push_back(Data.HasPublicFields);
5911 Record->push_back(Data.HasMutableFields);
5912 Record->push_back(Data.HasVariantMembers);
5913 Record->push_back(Data.HasOnlyCMembers);
5914 Record->push_back(Data.HasInClassInitializer);
5915 Record->push_back(Data.HasUninitializedReferenceMember);
5916 Record->push_back(Data.HasUninitializedFields);
Richard Smith12e79312016-05-13 06:47:56 +00005917 Record->push_back(Data.HasInheritedConstructor);
5918 Record->push_back(Data.HasInheritedAssignment);
Richard Smith96cd6712017-08-16 01:49:53 +00005919 Record->push_back(Data.NeedOverloadResolutionForCopyConstructor);
Richard Smith290d8012016-04-06 17:06:00 +00005920 Record->push_back(Data.NeedOverloadResolutionForMoveConstructor);
5921 Record->push_back(Data.NeedOverloadResolutionForMoveAssignment);
5922 Record->push_back(Data.NeedOverloadResolutionForDestructor);
Richard Smith96cd6712017-08-16 01:49:53 +00005923 Record->push_back(Data.DefaultedCopyConstructorIsDeleted);
Richard Smith290d8012016-04-06 17:06:00 +00005924 Record->push_back(Data.DefaultedMoveConstructorIsDeleted);
5925 Record->push_back(Data.DefaultedMoveAssignmentIsDeleted);
5926 Record->push_back(Data.DefaultedDestructorIsDeleted);
5927 Record->push_back(Data.HasTrivialSpecialMembers);
5928 Record->push_back(Data.DeclaredNonTrivialSpecialMembers);
5929 Record->push_back(Data.HasIrrelevantDestructor);
5930 Record->push_back(Data.HasConstexprNonCopyMoveConstructor);
5931 Record->push_back(Data.HasDefaultedDefaultConstructor);
Richard Smith96cd6712017-08-16 01:49:53 +00005932 Record->push_back(Data.CanPassInRegisters);
Richard Smith290d8012016-04-06 17:06:00 +00005933 Record->push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5934 Record->push_back(Data.HasConstexprDefaultConstructor);
5935 Record->push_back(Data.HasNonLiteralTypeFieldsOrBases);
5936 Record->push_back(Data.ComputedVisibleConversions);
5937 Record->push_back(Data.UserProvidedDefaultConstructor);
5938 Record->push_back(Data.DeclaredSpecialMembers);
Richard Smithdf054d32017-02-25 23:53:05 +00005939 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForVBase);
5940 Record->push_back(Data.ImplicitCopyConstructorCanHaveConstParamForNonVBase);
Richard Smith290d8012016-04-06 17:06:00 +00005941 Record->push_back(Data.ImplicitCopyAssignmentHasConstParam);
5942 Record->push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5943 Record->push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Trieufd1acbb2017-04-11 21:31:00 +00005944
5945 // getODRHash will compute the ODRHash if it has not been previously computed.
5946 Record->push_back(D->getODRHash());
David Blaikief63556d2017-04-12 20:58:33 +00005947 bool ModulesDebugInfo = Writer->Context->getLangOpts().ModulesDebugInfo &&
5948 Writer->WritingModule && !D->isDependentType();
5949 Record->push_back(ModulesDebugInfo);
5950 if (ModulesDebugInfo)
David Blaikie1ac9c982017-04-11 21:13:37 +00005951 Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(D));
5952
Richard Smith561fb152012-02-25 07:33:38 +00005953 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005954
Richard Smith290d8012016-04-06 17:06:00 +00005955 Record->push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005956 if (Data.NumBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005957 AddCXXBaseSpecifiers(Data.bases());
5958
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005959 // FIXME: Make VBases lazily computed when needed to avoid storing them.
Richard Smith290d8012016-04-06 17:06:00 +00005960 Record->push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005961 if (Data.NumVBases > 0)
Richard Smith645d2cf2016-04-14 00:29:55 +00005962 AddCXXBaseSpecifiers(Data.vbases());
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005963
Richard Smith290d8012016-04-06 17:06:00 +00005964 AddUnresolvedSet(Data.Conversions.get(*Writer->Context));
5965 AddUnresolvedSet(Data.VisibleConversions.get(*Writer->Context));
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005966 // Data.Definition is the owning decl, no need to write it.
Richard Smith290d8012016-04-06 17:06:00 +00005967 AddDeclRef(D->getFirstFriend());
Douglas Gregor99ae8062012-02-14 17:54:36 +00005968
5969 // Add lambda-specific data.
5970 if (Data.IsLambda) {
Richard Smith053f6c62014-05-16 23:01:30 +00005971 auto &Lambda = D->getLambdaData();
Richard Smith290d8012016-04-06 17:06:00 +00005972 Record->push_back(Lambda.Dependent);
5973 Record->push_back(Lambda.IsGenericLambda);
5974 Record->push_back(Lambda.CaptureDefault);
5975 Record->push_back(Lambda.NumCaptures);
5976 Record->push_back(Lambda.NumExplicitCaptures);
5977 Record->push_back(Lambda.ManglingNumber);
Richard Smith0bae6242016-08-25 00:34:00 +00005978 AddDeclRef(D->getLambdaContextDecl());
Richard Smith290d8012016-04-06 17:06:00 +00005979 AddTypeSourceInfo(Lambda.MethodTyInfo);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005980 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00005981 const LambdaCapture &Capture = Lambda.Captures[I];
Richard Smith290d8012016-04-06 17:06:00 +00005982 AddSourceLocation(Capture.getLocation());
5983 Record->push_back(Capture.isImplicit());
5984 Record->push_back(Capture.getCaptureKind());
Richard Smithba71c082013-05-16 06:20:58 +00005985 switch (Capture.getCaptureKind()) {
Faisal Validc6b5962016-03-21 09:25:37 +00005986 case LCK_StarThis:
Richard Smithba71c082013-05-16 06:20:58 +00005987 case LCK_This:
Alexey Bataev39c81e22014-08-28 04:28:19 +00005988 case LCK_VLAType:
Richard Smithba71c082013-05-16 06:20:58 +00005989 break;
5990 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005991 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005992 VarDecl *Var =
Craig Toppera13603a2014-05-22 05:54:18 +00005993 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith290d8012016-04-06 17:06:00 +00005994 AddDeclRef(Var);
Richard Smithba71c082013-05-16 06:20:58 +00005995 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
Richard Smith290d8012016-04-06 17:06:00 +00005996 : SourceLocation());
Richard Smithba71c082013-05-16 06:20:58 +00005997 break;
5998 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005999 }
6000 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00006001}
6002
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006003void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00006004 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00006005 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00006006 assert(FirstDeclID == NextDeclID &&
6007 FirstTypeID == NextTypeID &&
6008 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006009 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00006010 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00006011 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00006012 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00006013
Sebastian Redl07a89a82010-07-30 00:29:29 +00006014 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006015
Richard Smith7f330cd2015-03-18 01:42:29 +00006016 // Note, this will get called multiple times, once one the reader starts up
6017 // and again each time it's done reading a PCH or module.
Douglas Gregordf0c1512011-08-18 04:12:04 +00006018 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
6019 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
6020 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006021 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00006022 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00006023 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006024 NextDeclID = FirstDeclID;
6025 NextTypeID = FirstTypeID;
6026 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006027 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00006028 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00006029 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00006030}
6031
Sebastian Redl539c5062010-08-18 23:57:32 +00006032void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006033 // Always keep the highest ID. See \p TypeRead() for more information.
6034 IdentID &StoredID = IdentifierIDs[II];
6035 if (ID > StoredID)
6036 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00006037}
6038
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006039void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006040 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00006041 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006042 if (ID > StoredID)
6043 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006044}
6045
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00006046void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006047 // Always take the highest-numbered type index. This copes with an interesting
6048 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00006049 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00006050 // keep the higher-numbered entry so that we can properly write it out to
6051 // the AST file.
6052 TypeIdx &StoredIdx = TypeIdxs[T];
6053 if (Idx.getIndex() >= StoredIdx.getIndex())
6054 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00006055}
6056
Sebastian Redl539c5062010-08-18 23:57:32 +00006057void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00006058 // Always keep the highest ID. See \p TypeRead() for more information.
6059 SelectorID &StoredID = SelectorIDs[S];
6060 if (ID > StoredID)
6061 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00006062}
Douglas Gregor91096292010-10-02 19:29:26 +00006063
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006064void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Richard Smith66a81862015-05-04 02:25:31 +00006065 MacroDefinitionRecord *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00006066 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00006067 MacroDefinitions[MD] = ID;
6068}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006069
Douglas Gregore37a85a2011-12-02 17:30:13 +00006070void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
6071 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
6072 SubmoduleIDs[Mod] = ID;
6073}
6074
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006075void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006076 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCallf937c022011-10-07 06:10:15 +00006077 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006078 assert(!WritingAST && "Already writing the AST!");
Eugene Zelenkoddaa4b42015-12-08 18:00:11 +00006079 if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006080 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00006081 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006082 // A forward reference was mutated into a definition. Rewrite it.
6083 // FIXME: This happens during template instantiation, should we
6084 // have created a new definition decl instead ?
Richard Smithcd45dbc2014-04-19 03:48:30 +00006085 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
6086 "completed a tag from another module but not by instantiation?");
6087 DeclUpdates[RD].push_back(
6088 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006089 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00006090 }
6091}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00006092
Richard Smithf983f7f2015-10-13 00:23:25 +00006093static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
6094 if (D->isFromASTFile())
6095 return true;
6096
Richard Smithf983f7f2015-10-13 00:23:25 +00006097 // The predefined __va_list_tag struct is imported if we imported any decls.
6098 // FIXME: This is a gross hack.
6099 return D == D->getASTContext().getVaListTagDecl();
6100}
6101
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006102void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006103 if (Chain && Chain->isProcessingUpdateRecords()) return;
6104 assert(DC->isLookupContext() &&
Vassil Vassilev71eafde2016-04-06 20:56:03 +00006105 "Should not add lookup results to non-lookup contexts!");
6106
Vassil Vassilev632eac32016-03-16 11:17:04 +00006107 // TU is handled elsewhere.
6108 if (isa<TranslationUnitDecl>(DC))
6109 return;
6110
6111 // Namespaces are handled elsewhere, except for template instantiations of
6112 // FunctionTemplateDecls in namespaces. We are interested in cases where the
6113 // local instantiations are added to an imported context. Only happens when
6114 // adding ADL lookup candidates, for example templated friends.
6115 if (isa<NamespaceDecl>(DC) && D->getFriendObjectKind() == Decl::FOK_None &&
6116 !isa<FunctionTemplateDecl>(D))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006117 return;
6118
Richard Smithf983f7f2015-10-13 00:23:25 +00006119 // We're only interested in cases where a local declaration is added to an
6120 // imported context.
6121 if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
6122 return;
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006123
Richard Smith0f4e2c42015-08-06 04:23:48 +00006124 assert(DC == DC->getPrimaryContext() && "added to non-primary context");
Douglas Gregor9f782892013-01-21 15:25:38 +00006125 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Richard Smithe9a8bc32014-09-30 00:45:29 +00006126 assert(!WritingAST && "Already writing the AST!");
Richard Smithf983f7f2015-10-13 00:23:25 +00006127 if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
6128 // We're adding a visible declaration to a predefined decl context. Ensure
6129 // that we write out all of its lookup results so we don't get a nasty
6130 // surprise when we try to emit its lookup table.
6131 for (auto *Child : DC->decls())
Richard Smithc26d9742016-10-06 20:30:51 +00006132 DeclsToEmitEvenIfUnreferenced.push_back(Child);
Richard Smithf983f7f2015-10-13 00:23:25 +00006133 }
Richard Smithc26d9742016-10-06 20:30:51 +00006134 DeclsToEmitEvenIfUnreferenced.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00006135}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006136
6137void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006138 if (Chain && Chain->isProcessingUpdateRecords()) return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006139 assert(D->isImplicit());
Richard Smithf983f7f2015-10-13 00:23:25 +00006140
6141 // We're only interested in cases where a local declaration is added to an
6142 // imported context.
6143 if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
6144 return;
6145
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006146 if (!isa<CXXMethodDecl>(D))
Richard Smithf983f7f2015-10-13 00:23:25 +00006147 return;
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006148
6149 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00006150 assert(RD->isCompleteDefinition());
Richard Smithe9a8bc32014-09-30 00:45:29 +00006151 assert(!WritingAST && "Already writing the AST!");
Aaron Ballman4f45b712014-03-21 15:22:56 +00006152 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00006153}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00006154
Richard Smith564417a2014-03-20 21:47:22 +00006155void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006156 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith9e2341d2015-03-23 03:25:59 +00006157 assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
6158 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006159 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006160 // If we don't already know the exception specification for this redecl
6161 // chain, add an update record for it.
6162 if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
6163 ->getType()
6164 ->castAs<FunctionProtoType>()
6165 ->getExceptionSpecType()))
6166 DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
6167 });
Richard Smith564417a2014-03-20 21:47:22 +00006168}
6169
Richard Smith1fa5d642013-05-11 05:45:24 +00006170void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006171 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith1fa5d642013-05-11 05:45:24 +00006172 assert(!WritingAST && "Already writing the AST!");
Richard Smith9e2341d2015-03-23 03:25:59 +00006173 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006174 Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006175 DeclUpdates[D].push_back(
6176 DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
6177 });
Richard Smith1fa5d642013-05-11 05:45:24 +00006178}
6179
Richard Smithf8134002015-03-10 01:41:22 +00006180void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
6181 const FunctionDecl *Delete) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006182 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithf8134002015-03-10 01:41:22 +00006183 assert(!WritingAST && "Already writing the AST!");
6184 assert(Delete && "Not given an operator delete");
Richard Smith9e2341d2015-03-23 03:25:59 +00006185 if (!Chain) return;
Richard Smith5fc18a92015-07-12 23:43:21 +00006186 Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
Richard Smith9e2341d2015-03-23 03:25:59 +00006187 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
6188 });
Richard Smithf8134002015-03-10 01:41:22 +00006189}
6190
Sebastian Redlab238a72011-04-24 16:28:06 +00006191void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006192 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006193 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006194 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00006195 return; // Declaration not imported from PCH.
6196
Richard Smith4d235792014-08-07 18:53:08 +00006197 // Implicit function decl from a PCH was defined.
6198 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redlab238a72011-04-24 16:28:06 +00006199}
6200
Richard Smithd28ac5b2014-03-22 23:33:22 +00006201void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006202 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smithd28ac5b2014-03-22 23:33:22 +00006203 assert(!WritingAST && "Already writing the AST!");
6204 if (!D->isFromASTFile())
6205 return;
6206
Richard Smith9e2341d2015-03-23 03:25:59 +00006207 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Richard Smithd28ac5b2014-03-22 23:33:22 +00006208}
6209
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006210void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006211 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006212 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006213 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006214 return;
6215
6216 // Since the actual instantiation is delayed, this really means that we need
6217 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00006218 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00006219 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
6220 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00006221}
6222
John McCall32791cc2016-01-06 22:34:54 +00006223void ASTWriter::DefaultArgumentInstantiated(const ParmVarDecl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006224 if (Chain && Chain->isProcessingUpdateRecords()) return;
John McCall32791cc2016-01-06 22:34:54 +00006225 assert(!WritingAST && "Already writing the AST!");
6226 if (!D->isFromASTFile())
6227 return;
6228
6229 DeclUpdates[D].push_back(
6230 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
6231}
6232
Richard Smith4b054b22016-08-24 21:25:37 +00006233void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
6234 assert(!WritingAST && "Already writing the AST!");
6235 if (!D->isFromASTFile())
6236 return;
6237
6238 DeclUpdates[D].push_back(
6239 DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
6240}
6241
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006242void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
6243 const ObjCInterfaceDecl *IFD) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006244 if (Chain && Chain->isProcessingUpdateRecords()) return;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00006245 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00006246 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006247 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00006248
6249 assert(IFD->getDefinition() && "Category on a class without a definition?");
6250 ObjCClassesWithCategories.insert(
6251 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00006252}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00006253
Eli Friedman276dd182013-09-05 00:02:25 +00006254void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006255 if (Chain && Chain->isProcessingUpdateRecords()) return;
Eli Friedman276dd182013-09-05 00:02:25 +00006256 assert(!WritingAST && "Already writing the AST!");
Vassil Vassilev928c8252016-04-28 14:13:28 +00006257
6258 // If there is *any* declaration of the entity that's not from an AST file,
6259 // we can skip writing the update record. We make sure that isUsed() triggers
6260 // completion of the redeclaration chain of the entity.
6261 for (auto Prev = D->getMostRecentDecl(); Prev; Prev = Prev->getPreviousDecl())
6262 if (IsLocalDecl(Prev))
6263 return;
Eli Friedman276dd182013-09-05 00:02:25 +00006264
Aaron Ballman4f45b712014-03-21 15:22:56 +00006265 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00006266}
Alexey Bataev97720002014-11-11 04:05:39 +00006267
6268void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006269 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alexey Bataev97720002014-11-11 04:05:39 +00006270 assert(!WritingAST && "Already writing the AST!");
6271 if (!D->isFromASTFile())
6272 return;
6273
6274 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
6275}
Richard Smith65ebb4a2015-03-26 04:09:53 +00006276
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006277void ASTWriter::DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
6278 const Attr *Attr) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006279 if (Chain && Chain->isProcessingUpdateRecords()) return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006280 assert(!WritingAST && "Already writing the AST!");
6281 if (!D->isFromASTFile())
6282 return;
6283
Dmitry Polukhind69b5052016-05-09 14:59:13 +00006284 DeclUpdates[D].push_back(
6285 DeclUpdate(UPD_DECL_MARKED_OPENMP_DECLARETARGET, Attr));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00006286}
6287
Richard Smith4caa4492015-05-15 02:34:32 +00006288void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006289 if (Chain && Chain->isProcessingUpdateRecords()) return;
Richard Smith65ebb4a2015-03-26 04:09:53 +00006290 assert(!WritingAST && "Already writing the AST!");
6291 assert(D->isHidden() && "expected a hidden declaration");
Richard Smith4caa4492015-05-15 02:34:32 +00006292 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
Richard Smith65ebb4a2015-03-26 04:09:53 +00006293}
Alex Denisovfde64952015-06-26 05:28:36 +00006294
6295void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
6296 const RecordDecl *Record) {
Vassil Vassilev19765fb2016-07-22 21:08:24 +00006297 if (Chain && Chain->isProcessingUpdateRecords()) return;
Alex Denisovfde64952015-06-26 05:28:36 +00006298 assert(!WritingAST && "Already writing the AST!");
6299 if (!Record->isFromASTFile())
6300 return;
6301 DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
6302}
Richard Smithc26d9742016-10-06 20:30:51 +00006303
6304void ASTWriter::AddedCXXTemplateSpecialization(
6305 const ClassTemplateDecl *TD, const ClassTemplateSpecializationDecl *D) {
6306 assert(!WritingAST && "Already writing the AST!");
6307
6308 if (!TD->getFirstDecl()->isFromASTFile())
6309 return;
6310 if (Chain && Chain->isProcessingUpdateRecords())
6311 return;
6312
6313 DeclsToEmitEvenIfUnreferenced.push_back(D);
6314}
6315
6316void ASTWriter::AddedCXXTemplateSpecialization(
6317 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
6318 assert(!WritingAST && "Already writing the AST!");
6319
6320 if (!TD->getFirstDecl()->isFromASTFile())
6321 return;
6322 if (Chain && Chain->isProcessingUpdateRecords())
6323 return;
6324
6325 DeclsToEmitEvenIfUnreferenced.push_back(D);
6326}
6327
6328void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
6329 const FunctionDecl *D) {
6330 assert(!WritingAST && "Already writing the AST!");
6331
6332 if (!TD->getFirstDecl()->isFromASTFile())
6333 return;
6334 if (Chain && Chain->isProcessingUpdateRecords())
6335 return;
6336
6337 DeclsToEmitEvenIfUnreferenced.push_back(D);
6338}