blob: 3af2a4003ba3857e3415a145bbcc302b74e32612 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070020#include "clang/AST/DeclLookups.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070026#include "clang/Basic/DiagnosticOptions.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000027#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000028#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070049#include "llvm/Support/EndianStream.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000050#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000051#include "llvm/Support/MemoryBuffer.h"
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052#include "llvm/Support/OnDiskHashTable.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000053#include "llvm/Support/Path.h"
Stephen Hines176edba2014-12-01 14:53:08 -080054#include "llvm/Support/Process.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000055#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000056#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000057#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000058#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000059using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000060using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000061
Sebastian Redlade50002010-07-30 17:03:48 +000062template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static StringRef data(const std::vector<T, Allocator> &v) {
64 if (v.empty()) return StringRef();
65 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000066 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000067}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068
69template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000070static StringRef data(const SmallVectorImpl<T> &v) {
71 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000072 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000073}
74
Douglas Gregor2cf26342009-04-09 22:27:44 +000075//===----------------------------------------------------------------------===//
76// Type serialization
77//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000078
Douglas Gregor2cf26342009-04-09 22:27:44 +000079namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000080 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000081 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000082 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
84 public:
85 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000086 TypeCode Code;
Stephen Hines176edba2014-12-01 14:53:08 -080087 /// \brief Abbreviation to use for the record, if any.
88 unsigned AbbrevToUse;
Douglas Gregor2cf26342009-04-09 22:27:44 +000089
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000090 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000091 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000092
93 void VisitArrayType(const ArrayType *T);
94 void VisitFunctionType(const FunctionType *T);
95 void VisitTagType(const TagType *T);
96
97#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
98#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000099#include "clang/AST/TypeNodes.def"
100 };
101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000104 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000114 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000115}
116
Reid Kleckner12df2462013-06-24 17:51:48 +0000117void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
118 Writer.AddTypeRef(T->getOriginalType(), Record);
119 Code = TYPE_DECAYED;
120}
121
Stephen Hines651f13c2014-04-23 16:59:28 -0700122void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
123 Writer.AddTypeRef(T->getOriginalType(), Record);
124 Writer.AddTypeRef(T->getAdjustedType(), Record);
125 Code = TYPE_ADJUSTED;
126}
127
Sebastian Redl3397c552010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
135 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000136 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000140 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000141 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142}
143
Sebastian Redl3397c552010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000145 Writer.AddTypeRef(T->getPointeeType(), Record);
146 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 Writer.AddTypeRef(T->getElementType(), Record);
152 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000153 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154}
155
Sebastian Redl3397c552010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157 VisitArrayType(T);
158 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000169 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
170 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000171 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000172 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173}
174
Sebastian Redl3397c552010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176 Writer.AddTypeRef(T->getElementType(), Record);
177 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000178 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000184 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185}
186
Sebastian Redl3397c552010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700188 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000189 FunctionType::ExtInfo C = T->getExtInfo();
190 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000191 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000192 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000193 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000194 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000195 Record.push_back(C.getProducesResult());
Stephen Hines176edba2014-12-01 14:53:08 -0800196
197 if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
198 AbbrevToUse = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000202 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000204}
205
Stephen Hines651f13c2014-04-23 16:59:28 -0700206static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
207 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl60618fa2011-03-12 11:50:43 +0000208 Record.push_back(T->getExceptionSpecType());
209 if (T->getExceptionSpecType() == EST_Dynamic) {
210 Record.push_back(T->getNumExceptions());
211 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
212 Writer.AddTypeRef(T->getExceptionType(I), Record);
213 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
214 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000215 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
216 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
217 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000218 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
219 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000220 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700221}
222
223void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
224 VisitFunctionType(T);
Stephen Hines176edba2014-12-01 14:53:08 -0800225
Stephen Hines651f13c2014-04-23 16:59:28 -0700226 Record.push_back(T->isVariadic());
227 Record.push_back(T->hasTrailingReturn());
228 Record.push_back(T->getTypeQuals());
229 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
230 addExceptionSpec(Writer, T, Record);
Stephen Hines176edba2014-12-01 14:53:08 -0800231
232 Record.push_back(T->getNumParams());
233 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
234 Writer.AddTypeRef(T->getParamType(I), Record);
235
236 if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
237 T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
238 AbbrevToUse = 0;
239
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000240 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241}
242
Sebastian Redl3397c552010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000244 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000245 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000246}
John McCalled976492009-12-04 22:46:56 +0000247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000250 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
251 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000252 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000253}
254
Sebastian Redl3397c552010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000256 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000257 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258}
259
Sebastian Redl3397c552010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000261 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000262 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263}
264
Sebastian Redl3397c552010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000266 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000267 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000268 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000269}
270
Sean Huntca63c202011-05-24 22:41:36 +0000271void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
272 Writer.AddTypeRef(T->getBaseType(), Record);
273 Writer.AddTypeRef(T->getUnderlyingType(), Record);
274 Record.push_back(T->getUTTKind());
275 Code = TYPE_UNARY_TRANSFORM;
276}
277
Richard Smith34b41d92011-02-20 03:19:35 +0000278void ASTTypeWriter::VisitAutoType(const AutoType *T) {
279 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smitha2c36462013-04-26 16:15:35 +0000280 Record.push_back(T->isDecltypeAuto());
Richard Smithdc7a4f52013-04-30 13:56:41 +0000281 if (T->getDeducedType().isNull())
282 Record.push_back(T->isDependentType());
Richard Smith34b41d92011-02-20 03:19:35 +0000283 Code = TYPE_AUTO;
284}
285
Sebastian Redl3397c552010-08-18 23:56:27 +0000286void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000287 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000288 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000289 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000290 "Cannot serialize in the middle of a type definition");
291}
292
Sebastian Redl3397c552010-08-18 23:56:27 +0000293void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000294 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000295 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000296}
297
Sebastian Redl3397c552010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000299 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000300 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000301}
302
John McCall9d156a72011-01-06 01:58:22 +0000303void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
304 Writer.AddTypeRef(T->getModifiedType(), Record);
305 Writer.AddTypeRef(T->getEquivalentType(), Record);
306 Record.push_back(T->getAttrKind());
307 Code = TYPE_ATTRIBUTED;
308}
309
Mike Stump1eb44332009-09-09 15:08:12 +0000310void
Sebastian Redl3397c552010-08-18 23:56:27 +0000311ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000312 const SubstTemplateTypeParmType *T) {
313 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
314 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000315 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000316}
317
318void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000319ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
320 const SubstTemplateTypeParmPackType *T) {
321 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
322 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
323 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
324}
325
326void
Sebastian Redl3397c552010-08-18 23:56:27 +0000327ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000328 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000329 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000330 Writer.AddTemplateName(T->getTemplateName(), Record);
331 Record.push_back(T->getNumArgs());
332 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
333 ArgI != ArgE; ++ArgI)
334 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000335 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
336 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000337 : T->getCanonicalTypeInternal(),
338 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000339 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl3397c552010-08-18 23:56:27 +0000343ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000344 VisitArrayType(T);
345 Writer.AddStmt(T->getSizeExpr());
346 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000347 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000348}
349
350void
Sebastian Redl3397c552010-08-18 23:56:27 +0000351ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000352 const DependentSizedExtVectorType *T) {
353 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000354 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000355}
356
357void
Sebastian Redl3397c552010-08-18 23:56:27 +0000358ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000359 Record.push_back(T->getDepth());
360 Record.push_back(T->getIndex());
361 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000362 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000364}
365
366void
Sebastian Redl3397c552010-08-18 23:56:27 +0000367ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000368 Record.push_back(T->getKeyword());
369 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
370 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000371 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
372 : T->getCanonicalTypeInternal(),
373 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000375}
376
377void
Sebastian Redl3397c552010-08-18 23:56:27 +0000378ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000379 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000380 Record.push_back(T->getKeyword());
381 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
382 Writer.AddIdentifierRef(T->getIdentifier(), Record);
383 Record.push_back(T->getNumArgs());
384 for (DependentTemplateSpecializationType::iterator
385 I = T->begin(), E = T->end(); I != E; ++I)
386 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000387 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000388}
389
Douglas Gregor7536dd52010-12-20 02:24:11 +0000390void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
391 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000392 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000393 Record.push_back(*NumExpansions + 1);
394 else
395 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000396 Code = TYPE_PACK_EXPANSION;
397}
398
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000399void ASTTypeWriter::VisitParenType(const ParenType *T) {
400 Writer.AddTypeRef(T->getInnerType(), Record);
401 Code = TYPE_PAREN;
402}
403
Sebastian Redl3397c552010-08-18 23:56:27 +0000404void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000405 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000406 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
407 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000408 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000409}
410
Sebastian Redl3397c552010-08-18 23:56:27 +0000411void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000412 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000413 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000414 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000415}
416
Sebastian Redl3397c552010-08-18 23:56:27 +0000417void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000418 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000419 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000420}
421
Sebastian Redl3397c552010-08-18 23:56:27 +0000422void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000423 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000424 Record.push_back(T->getNumProtocols());
Stephen Hines651f13c2014-04-23 16:59:28 -0700425 for (const auto *I : T->quals())
426 Writer.AddDeclRef(I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000427 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000428}
429
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000430void
Sebastian Redl3397c552010-08-18 23:56:27 +0000431ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000432 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000433 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000434}
435
Eli Friedmanb001de72011-10-06 23:00:33 +0000436void
437ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
438 Writer.AddTypeRef(T->getValueType(), Record);
439 Code = TYPE_ATOMIC;
440}
441
John McCalla1ee0c52009-10-16 21:56:05 +0000442namespace {
443
444class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000445 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000446 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000447
448public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000449 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000450 : Writer(Writer), Record(Record) { }
451
John McCall51bd8032009-10-18 01:05:36 +0000452#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000453#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000454 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000455#include "clang/AST/TypeLocNodes.def"
456
John McCall51bd8032009-10-18 01:05:36 +0000457 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
458 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000459};
460
461}
462
John McCall51bd8032009-10-18 01:05:36 +0000463void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
464 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000465}
John McCall51bd8032009-10-18 01:05:36 +0000466void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000467 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
468 if (TL.needsExtraLocalData()) {
469 Record.push_back(TL.getWrittenTypeSpec());
470 Record.push_back(TL.getWrittenSignSpec());
471 Record.push_back(TL.getWrittenWidthSpec());
472 Record.push_back(TL.hasModeAttr());
473 }
John McCalla1ee0c52009-10-16 21:56:05 +0000474}
John McCall51bd8032009-10-18 01:05:36 +0000475void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000477}
John McCall51bd8032009-10-18 01:05:36 +0000478void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000480}
Reid Kleckner12df2462013-06-24 17:51:48 +0000481void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
482 // nothing to do
483}
Stephen Hines651f13c2014-04-23 16:59:28 -0700484void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
485 // nothing to do
486}
John McCall51bd8032009-10-18 01:05:36 +0000487void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000489}
John McCall51bd8032009-10-18 01:05:36 +0000490void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000492}
John McCall51bd8032009-10-18 01:05:36 +0000493void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000495}
John McCall51bd8032009-10-18 01:05:36 +0000496void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000498 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000499}
John McCall51bd8032009-10-18 01:05:36 +0000500void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
502 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
503 Record.push_back(TL.getSizeExpr() ? 1 : 0);
504 if (TL.getSizeExpr())
505 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000506}
John McCall51bd8032009-10-18 01:05:36 +0000507void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
508 VisitArrayTypeLoc(TL);
509}
510void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
511 VisitArrayTypeLoc(TL);
512}
513void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
514 VisitArrayTypeLoc(TL);
515}
516void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
517 DependentSizedArrayTypeLoc TL) {
518 VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
521 DependentSizedExtVectorTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
524void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
527void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000531 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000534 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Stephen Hines651f13c2014-04-23 16:59:28 -0700535 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
536 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000537}
538void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
539 VisitFunctionTypeLoc(TL);
540}
541void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
542 VisitFunctionTypeLoc(TL);
543}
John McCalled976492009-12-04 22:46:56 +0000544void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCall51bd8032009-10-18 01:05:36 +0000547void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
550void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000551 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
552 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000554}
555void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000556 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
557 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
558 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
559 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000560}
561void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
Sean Huntca63c202011-05-24 22:41:36 +0000564void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getKWLoc(), Record);
566 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
569}
Richard Smith34b41d92011-02-20 03:19:35 +0000570void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall51bd8032009-10-18 01:05:36 +0000573void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
576void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
John McCall9d156a72011-01-06 01:58:22 +0000579void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
581 if (TL.hasAttrOperand()) {
582 SourceRange range = TL.getAttrOperandParensRange();
583 Writer.AddSourceLocation(range.getBegin(), Record);
584 Writer.AddSourceLocation(range.getEnd(), Record);
585 }
586 if (TL.hasAttrExprOperand()) {
587 Expr *operand = TL.getAttrExprOperand();
588 Record.push_back(operand ? 1 : 0);
589 if (operand) Writer.AddStmt(operand);
590 } else if (TL.hasAttrEnumOperand()) {
591 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
592 }
593}
John McCall51bd8032009-10-18 01:05:36 +0000594void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getNameLoc(), Record);
596}
John McCall49a832b2009-10-18 09:09:24 +0000597void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
598 SubstTemplateTypeParmTypeLoc TL) {
599 Writer.AddSourceLocation(TL.getNameLoc(), Record);
600}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000601void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
602 SubstTemplateTypeParmPackTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
604}
John McCall51bd8032009-10-18 01:05:36 +0000605void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
606 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000607 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000608 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
609 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000612 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
613 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000614}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000615void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
617 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
618}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000619void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000620 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000621 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000622}
John McCall3cb0ebd2010-03-10 03:28:59 +0000623void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getNameLoc(), Record);
625}
Douglas Gregor4714c122010-03-31 17:34:00 +0000626void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000627 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000628 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000629 Writer.AddSourceLocation(TL.getNameLoc(), Record);
630}
John McCall33500952010-06-11 00:33:02 +0000631void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
632 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000633 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000634 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000635 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000636 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000637 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000640 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
641 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000642}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000643void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
645}
John McCall51bd8032009-10-18 01:05:36 +0000646void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
647 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000648}
649void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
650 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000651 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
652 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
653 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
654 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000655}
John McCall54e14c42009-10-22 22:37:11 +0000656void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
657 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000658}
Eli Friedmanb001de72011-10-06 23:00:33 +0000659void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
660 Writer.AddSourceLocation(TL.getKWLoc(), Record);
661 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
662 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
663}
John McCalla1ee0c52009-10-16 21:56:05 +0000664
Stephen Hines176edba2014-12-01 14:53:08 -0800665void ASTWriter::WriteTypeAbbrevs() {
666 using namespace llvm;
667
668 BitCodeAbbrev *Abv;
669
670 // Abbreviation for TYPE_EXT_QUAL
671 Abv = new BitCodeAbbrev();
672 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
673 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
674 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Quals
675 TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
676
677 // Abbreviation for TYPE_FUNCTION_PROTO
678 Abv = new BitCodeAbbrev();
679 Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
680 // FunctionType
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ReturnType
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
683 Abv->Add(BitCodeAbbrevOp(0)); // HasRegParm
684 Abv->Add(BitCodeAbbrevOp(0)); // RegParm
685 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
686 Abv->Add(BitCodeAbbrevOp(0)); // ProducesResult
687 // FunctionProtoType
688 Abv->Add(BitCodeAbbrevOp(0)); // IsVariadic
689 Abv->Add(BitCodeAbbrevOp(0)); // HasTrailingReturn
690 Abv->Add(BitCodeAbbrevOp(0)); // TypeQuals
691 Abv->Add(BitCodeAbbrevOp(0)); // RefQualifier
692 Abv->Add(BitCodeAbbrevOp(EST_None)); // ExceptionSpec
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumParams
694 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
695 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Params
696 TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
697}
698
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000699//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000700// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000701//===----------------------------------------------------------------------===//
702
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000703static void EmitBlockID(unsigned ID, const char *Name,
704 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000705 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000706 Record.clear();
707 Record.push_back(ID);
708 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
709
710 // Emit the block name if present.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700711 if (!Name || Name[0] == 0)
712 return;
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000713 Record.clear();
714 while (*Name)
715 Record.push_back(*Name++);
716 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
717}
718
719static void EmitRecordID(unsigned ID, const char *Name,
720 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000721 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000722 Record.clear();
723 Record.push_back(ID);
724 while (*Name)
725 Record.push_back(*Name++);
726 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000727}
728
729static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000730 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000731#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000732 RECORD(STMT_STOP);
733 RECORD(STMT_NULL_PTR);
Stephen Hines176edba2014-12-01 14:53:08 -0800734 RECORD(STMT_REF_PTR);
Chris Lattner0558df22009-04-27 00:49:53 +0000735 RECORD(STMT_NULL);
736 RECORD(STMT_COMPOUND);
737 RECORD(STMT_CASE);
738 RECORD(STMT_DEFAULT);
739 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000740 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000741 RECORD(STMT_IF);
742 RECORD(STMT_SWITCH);
743 RECORD(STMT_WHILE);
744 RECORD(STMT_DO);
745 RECORD(STMT_FOR);
746 RECORD(STMT_GOTO);
747 RECORD(STMT_INDIRECT_GOTO);
748 RECORD(STMT_CONTINUE);
749 RECORD(STMT_BREAK);
750 RECORD(STMT_RETURN);
751 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000752 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000753 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000754 RECORD(EXPR_PREDEFINED);
755 RECORD(EXPR_DECL_REF);
756 RECORD(EXPR_INTEGER_LITERAL);
757 RECORD(EXPR_FLOATING_LITERAL);
758 RECORD(EXPR_IMAGINARY_LITERAL);
759 RECORD(EXPR_STRING_LITERAL);
760 RECORD(EXPR_CHARACTER_LITERAL);
761 RECORD(EXPR_PAREN);
Stephen Hines176edba2014-12-01 14:53:08 -0800762 RECORD(EXPR_PAREN_LIST);
Chris Lattner0558df22009-04-27 00:49:53 +0000763 RECORD(EXPR_UNARY_OPERATOR);
764 RECORD(EXPR_SIZEOF_ALIGN_OF);
765 RECORD(EXPR_ARRAY_SUBSCRIPT);
766 RECORD(EXPR_CALL);
767 RECORD(EXPR_MEMBER);
768 RECORD(EXPR_BINARY_OPERATOR);
769 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
770 RECORD(EXPR_CONDITIONAL_OPERATOR);
771 RECORD(EXPR_IMPLICIT_CAST);
772 RECORD(EXPR_CSTYLE_CAST);
773 RECORD(EXPR_COMPOUND_LITERAL);
774 RECORD(EXPR_EXT_VECTOR_ELEMENT);
775 RECORD(EXPR_INIT_LIST);
776 RECORD(EXPR_DESIGNATED_INIT);
777 RECORD(EXPR_IMPLICIT_VALUE_INIT);
778 RECORD(EXPR_VA_ARG);
779 RECORD(EXPR_ADDR_LABEL);
780 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000781 RECORD(EXPR_CHOOSE);
782 RECORD(EXPR_GNU_NULL);
783 RECORD(EXPR_SHUFFLE_VECTOR);
784 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000785 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000786 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000787 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000788 RECORD(EXPR_OBJC_ARRAY_LITERAL);
789 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000790 RECORD(EXPR_OBJC_ENCODE);
791 RECORD(EXPR_OBJC_SELECTOR_EXPR);
792 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
793 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
794 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
795 RECORD(EXPR_OBJC_KVC_REF_EXPR);
796 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000797 RECORD(STMT_OBJC_FOR_COLLECTION);
798 RECORD(STMT_OBJC_CATCH);
799 RECORD(STMT_OBJC_FINALLY);
800 RECORD(STMT_OBJC_AT_TRY);
801 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
802 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000803 RECORD(EXPR_OBJC_BOOL_LITERAL);
Stephen Hines176edba2014-12-01 14:53:08 -0800804 RECORD(STMT_CXX_CATCH);
805 RECORD(STMT_CXX_TRY);
806 RECORD(STMT_CXX_FOR_RANGE);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000807 RECORD(EXPR_CXX_OPERATOR_CALL);
Stephen Hines176edba2014-12-01 14:53:08 -0800808 RECORD(EXPR_CXX_MEMBER_CALL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000809 RECORD(EXPR_CXX_CONSTRUCT);
Stephen Hines176edba2014-12-01 14:53:08 -0800810 RECORD(EXPR_CXX_TEMPORARY_OBJECT);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000811 RECORD(EXPR_CXX_STATIC_CAST);
812 RECORD(EXPR_CXX_DYNAMIC_CAST);
813 RECORD(EXPR_CXX_REINTERPRET_CAST);
814 RECORD(EXPR_CXX_CONST_CAST);
815 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000816 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smith7c3e6152013-06-12 22:31:48 +0000817 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000818 RECORD(EXPR_CXX_BOOL_LITERAL);
819 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000820 RECORD(EXPR_CXX_TYPEID_EXPR);
821 RECORD(EXPR_CXX_TYPEID_TYPE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000822 RECORD(EXPR_CXX_THIS);
823 RECORD(EXPR_CXX_THROW);
824 RECORD(EXPR_CXX_DEFAULT_ARG);
Stephen Hines176edba2014-12-01 14:53:08 -0800825 RECORD(EXPR_CXX_DEFAULT_INIT);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000826 RECORD(EXPR_CXX_BIND_TEMPORARY);
827 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
828 RECORD(EXPR_CXX_NEW);
829 RECORD(EXPR_CXX_DELETE);
830 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
831 RECORD(EXPR_EXPR_WITH_CLEANUPS);
832 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
833 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
834 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
835 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
836 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Stephen Hines176edba2014-12-01 14:53:08 -0800837 RECORD(EXPR_CXX_EXPRESSION_TRAIT);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000838 RECORD(EXPR_CXX_NOEXCEPT);
839 RECORD(EXPR_OPAQUE_VALUE);
Stephen Hines176edba2014-12-01 14:53:08 -0800840 RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
841 RECORD(EXPR_TYPE_TRAIT);
842 RECORD(EXPR_ARRAY_TYPE_TRAIT);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000843 RECORD(EXPR_PACK_EXPANSION);
844 RECORD(EXPR_SIZEOF_PACK);
Stephen Hines176edba2014-12-01 14:53:08 -0800845 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000846 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Stephen Hines176edba2014-12-01 14:53:08 -0800847 RECORD(EXPR_FUNCTION_PARM_PACK);
848 RECORD(EXPR_MATERIALIZE_TEMPORARY);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000849 RECORD(EXPR_CUDA_KERNEL_CALL);
Stephen Hines176edba2014-12-01 14:53:08 -0800850 RECORD(EXPR_CXX_UUIDOF_EXPR);
851 RECORD(EXPR_CXX_UUIDOF_TYPE);
852 RECORD(EXPR_LAMBDA);
Chris Lattner0558df22009-04-27 00:49:53 +0000853#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000854}
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Sebastian Redla4232eb2010-08-18 23:56:21 +0000856void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000857 RecordData Record;
858 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000860#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
861#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000863 // Control Block.
864 BLOCK(CONTROL_BLOCK);
865 RECORD(METADATA);
Stephen Hines176edba2014-12-01 14:53:08 -0800866 RECORD(SIGNATURE);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700867 RECORD(MODULE_NAME);
868 RECORD(MODULE_MAP_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000869 RECORD(IMPORTS);
870 RECORD(LANGUAGE_OPTIONS);
871 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000872 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000873 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000874 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000875 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000876 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000877 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000878 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000879 RECORD(PREPROCESSOR_OPTIONS);
880
Douglas Gregorc337fef2012-10-19 00:45:00 +0000881 BLOCK(INPUT_FILES_BLOCK);
882 RECORD(INPUT_FILE);
883
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000884 // AST Top-Level Block.
885 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000886 RECORD(TYPE_OFFSET);
887 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000888 RECORD(IDENTIFIER_OFFSET);
889 RECORD(IDENTIFIER_TABLE);
Stephen Hines651f13c2014-04-23 16:59:28 -0700890 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000891 RECORD(SPECIAL_TYPES);
892 RECORD(STATISTICS);
893 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000894 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000895 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000896 RECORD(SELECTOR_OFFSETS);
897 RECORD(METHOD_POOL);
898 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000899 RECORD(SOURCE_LOCATION_OFFSETS);
900 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000901 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000902 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000903 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000904 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000905 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000906 RECORD(SEMA_DECL_REFS);
907 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
908 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
909 RECORD(DECL_REPLACEMENTS);
910 RECORD(UPDATE_VISIBLE);
911 RECORD(DECL_UPDATE_OFFSETS);
912 RECORD(DECL_UPDATES);
913 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
914 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000915 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000916 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000917 RECORD(FP_PRAGMA_OPTIONS);
918 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000919 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000920 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000921 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000922 RECORD(MODULE_OFFSET_MAP);
923 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000924 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000925 RECORD(FILE_SORTED_DECLS);
926 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000927 RECORD(MERGED_DECLARATIONS);
928 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000929 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000930 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000931 RECORD(MACRO_TABLE);
Richard Smithac32d902013-08-07 21:41:30 +0000932 RECORD(LATE_PARSED_TEMPLATE);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700933 RECORD(OPTIMIZE_PRAGMA_OPTIONS);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000934
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000935 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000936 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000937 RECORD(SM_SLOC_FILE_ENTRY);
938 RECORD(SM_SLOC_BUFFER_ENTRY);
939 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000940 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000942 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000943 BLOCK(PREPROCESSOR_BLOCK);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700944 RECORD(PP_MACRO_DIRECTIVE_HISTORY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000945 RECORD(PP_MACRO_OBJECT_LIKE);
946 RECORD(PP_MACRO_FUNCTION_LIKE);
947 RECORD(PP_TOKEN);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700948
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000949 // Decls and Types block.
950 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000951 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000952 RECORD(TYPE_COMPLEX);
953 RECORD(TYPE_POINTER);
954 RECORD(TYPE_BLOCK_POINTER);
955 RECORD(TYPE_LVALUE_REFERENCE);
956 RECORD(TYPE_RVALUE_REFERENCE);
957 RECORD(TYPE_MEMBER_POINTER);
958 RECORD(TYPE_CONSTANT_ARRAY);
959 RECORD(TYPE_INCOMPLETE_ARRAY);
960 RECORD(TYPE_VARIABLE_ARRAY);
961 RECORD(TYPE_VECTOR);
962 RECORD(TYPE_EXT_VECTOR);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000963 RECORD(TYPE_FUNCTION_NO_PROTO);
Stephen Hines176edba2014-12-01 14:53:08 -0800964 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000965 RECORD(TYPE_TYPEDEF);
966 RECORD(TYPE_TYPEOF_EXPR);
967 RECORD(TYPE_TYPEOF);
968 RECORD(TYPE_RECORD);
969 RECORD(TYPE_ENUM);
970 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000971 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000972 RECORD(TYPE_DECLTYPE);
973 RECORD(TYPE_ELABORATED);
974 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
975 RECORD(TYPE_UNRESOLVED_USING);
976 RECORD(TYPE_INJECTED_CLASS_NAME);
977 RECORD(TYPE_OBJC_OBJECT);
978 RECORD(TYPE_TEMPLATE_TYPE_PARM);
979 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
980 RECORD(TYPE_DEPENDENT_NAME);
981 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
982 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
983 RECORD(TYPE_PAREN);
984 RECORD(TYPE_PACK_EXPANSION);
985 RECORD(TYPE_ATTRIBUTED);
986 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Stephen Hines176edba2014-12-01 14:53:08 -0800987 RECORD(TYPE_AUTO);
988 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedmanb001de72011-10-06 23:00:33 +0000989 RECORD(TYPE_ATOMIC);
Stephen Hines176edba2014-12-01 14:53:08 -0800990 RECORD(TYPE_DECAYED);
991 RECORD(TYPE_ADJUSTED);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000992 RECORD(DECL_TYPEDEF);
Stephen Hines176edba2014-12-01 14:53:08 -0800993 RECORD(DECL_TYPEALIAS);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000994 RECORD(DECL_ENUM);
995 RECORD(DECL_RECORD);
996 RECORD(DECL_ENUM_CONSTANT);
997 RECORD(DECL_FUNCTION);
998 RECORD(DECL_OBJC_METHOD);
999 RECORD(DECL_OBJC_INTERFACE);
1000 RECORD(DECL_OBJC_PROTOCOL);
1001 RECORD(DECL_OBJC_IVAR);
1002 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +00001003 RECORD(DECL_OBJC_CATEGORY);
1004 RECORD(DECL_OBJC_CATEGORY_IMPL);
1005 RECORD(DECL_OBJC_IMPLEMENTATION);
1006 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1007 RECORD(DECL_OBJC_PROPERTY);
1008 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001009 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +00001010 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001011 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +00001012 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001013 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +00001014 RECORD(DECL_FILE_SCOPE_ASM);
1015 RECORD(DECL_BLOCK);
1016 RECORD(DECL_CONTEXT_LEXICAL);
1017 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +00001018 RECORD(DECL_NAMESPACE);
1019 RECORD(DECL_NAMESPACE_ALIAS);
1020 RECORD(DECL_USING);
1021 RECORD(DECL_USING_SHADOW);
1022 RECORD(DECL_USING_DIRECTIVE);
1023 RECORD(DECL_UNRESOLVED_USING_VALUE);
1024 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1025 RECORD(DECL_LINKAGE_SPEC);
1026 RECORD(DECL_CXX_RECORD);
1027 RECORD(DECL_CXX_METHOD);
1028 RECORD(DECL_CXX_CONSTRUCTOR);
1029 RECORD(DECL_CXX_DESTRUCTOR);
1030 RECORD(DECL_CXX_CONVERSION);
1031 RECORD(DECL_ACCESS_SPEC);
1032 RECORD(DECL_FRIEND);
1033 RECORD(DECL_FRIEND_TEMPLATE);
1034 RECORD(DECL_CLASS_TEMPLATE);
1035 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1036 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufoef4579c2013-08-06 01:03:05 +00001037 RECORD(DECL_VAR_TEMPLATE);
1038 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1039 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +00001040 RECORD(DECL_FUNCTION_TEMPLATE);
1041 RECORD(DECL_TEMPLATE_TYPE_PARM);
1042 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1043 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1044 RECORD(DECL_STATIC_ASSERT);
1045 RECORD(DECL_CXX_BASE_SPECIFIERS);
1046 RECORD(DECL_INDIRECTFIELD);
1047 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1048
Douglas Gregora72d8c42011-06-03 02:27:19 +00001049 // Statements and Exprs can occur in the Decls and Types block.
1050 AddStmtsExprs(Stream, Record);
1051
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001052 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001053 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001054 RECORD(PPD_MACRO_DEFINITION);
1055 RECORD(PPD_INCLUSION_DIRECTIVE);
1056
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001057#undef RECORD
1058#undef BLOCK
1059 Stream.ExitBlock();
1060}
1061
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001062/// \brief Prepares a path for being written to an AST file by converting it
1063/// to an absolute path and removing nested './'s.
1064///
1065/// \return \c true if the path was changed.
1066static bool cleanPathForOutput(FileManager &FileMgr,
1067 SmallVectorImpl<char> &Path) {
1068 bool Changed = false;
1069
1070 if (!llvm::sys::path::is_absolute(StringRef(Path.data(), Path.size()))) {
1071 llvm::sys::fs::make_absolute(Path);
1072 Changed = true;
1073 }
1074
1075 return Changed | FileMgr.removeDotPaths(Path);
1076}
1077
Douglas Gregore650c8c2009-07-07 00:12:59 +00001078/// \brief Adjusts the given filename to only write out the portion of the
1079/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +00001080///
Douglas Gregore650c8c2009-07-07 00:12:59 +00001081/// \param Filename the file name to adjust.
1082///
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001083/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1084/// the returned filename will be adjusted by this root directory.
Douglas Gregore650c8c2009-07-07 00:12:59 +00001085///
1086/// \returns either the original filename (if it needs no adjustment) or the
1087/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +00001088static const char *
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001089adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001090 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001092 if (BaseDir.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001093 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Douglas Gregore650c8c2009-07-07 00:12:59 +00001095 // Verify that the filename and the system root have the same prefix.
1096 unsigned Pos = 0;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001097 for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1098 if (Filename[Pos] != BaseDir[Pos])
Douglas Gregore650c8c2009-07-07 00:12:59 +00001099 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregore650c8c2009-07-07 00:12:59 +00001101 // We hit the end of the filename before we hit the end of the system root.
1102 if (!Filename[Pos])
1103 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001105 // If there's not a path separator at the end of the base directory nor
1106 // immediately after it, then this isn't within the base directory.
1107 if (!llvm::sys::path::is_separator(Filename[Pos])) {
1108 if (!llvm::sys::path::is_separator(BaseDir.back()))
1109 return Filename;
1110 } else {
1111 // If the file name has a '/' at the current position, skip over the '/'.
1112 // We distinguish relative paths from absolute paths by the
1113 // absence of '/' at the beginning of relative paths.
1114 //
1115 // FIXME: This is wrong. We distinguish them by asking if the path is
1116 // absolute, which isn't the same thing. And there might be multiple '/'s
1117 // in a row. Use a better mechanism to indicate whether we have emitted an
1118 // absolute or relative path.
Douglas Gregore650c8c2009-07-07 00:12:59 +00001119 ++Pos;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregore650c8c2009-07-07 00:12:59 +00001122 return Filename + Pos;
1123}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001124
Stephen Hines176edba2014-12-01 14:53:08 -08001125static ASTFileSignature getSignature() {
1126 while (1) {
1127 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1128 return S;
1129 // Rely on GetRandomNumber to eventually return non-zero...
1130 }
1131}
1132
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001133/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001134void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1135 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001136 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001137 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001138 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1139 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001140
Douglas Gregore650c8c2009-07-07 00:12:59 +00001141 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001142 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1143 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1144 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1145 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1146 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1147 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1148 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1149 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1150 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1151 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1152 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001153 Record.push_back(VERSION_MAJOR);
1154 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001155 Record.push_back(CLANG_VERSION_MAJOR);
1156 Record.push_back(CLANG_VERSION_MINOR);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001157 assert((!WritingModule || isysroot.empty()) &&
1158 "writing module as a relocatable PCH?");
Douglas Gregor832d6202011-07-22 16:35:34 +00001159 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001160 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001161 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1162 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001163
Stephen Hines176edba2014-12-01 14:53:08 -08001164 // Signature
1165 Record.clear();
1166 Record.push_back(getSignature());
1167 Stream.EmitRecord(SIGNATURE, Record);
1168
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001169 if (WritingModule) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001170 // Module name
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001171 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1172 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1174 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1175 RecordData Record;
1176 Record.push_back(MODULE_NAME);
1177 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1178 }
1179
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001180 if (WritingModule && WritingModule->Directory) {
1181 // Module directory.
1182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1183 Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1185 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1186 RecordData Record;
1187 Record.push_back(MODULE_DIRECTORY);
1188
1189 SmallString<128> BaseDir(WritingModule->Directory->getName());
1190 cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1191 Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1192
1193 // Write out all other paths relative to the base directory if possible.
1194 BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1195 } else if (!isysroot.empty()) {
1196 // Write out paths relative to the sysroot if possible.
1197 BaseDirectory = isysroot;
1198 }
1199
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001200 // Module map file
1201 if (WritingModule) {
Stephen Hines176edba2014-12-01 14:53:08 -08001202 Record.clear();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001203
Stephen Hines176edba2014-12-01 14:53:08 -08001204 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1205
1206 // Primary module map file.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001207 AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
Stephen Hines176edba2014-12-01 14:53:08 -08001208
1209 // Additional module map files.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001210 if (auto *AdditionalModMaps =
1211 Map.getAdditionalModuleMapFiles(WritingModule)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001212 Record.push_back(AdditionalModMaps->size());
1213 for (const FileEntry *F : *AdditionalModMaps)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001214 AddPath(F->getName(), Record);
Stephen Hines176edba2014-12-01 14:53:08 -08001215 } else {
1216 Record.push_back(0);
1217 }
1218
1219 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001220 }
1221
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001222 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001223 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001224 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregore95b9192011-08-17 21:07:30 +00001225 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001226
1227 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1228 M != MEnd; ++M) {
1229 // Skip modules that weren't directly imported.
1230 if (!(*M)->isDirectlyImported())
1231 continue;
1232
1233 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001234 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001235 Record.push_back((*M)->File->getSize());
1236 Record.push_back((*M)->File->getModificationTime());
Stephen Hines176edba2014-12-01 14:53:08 -08001237 Record.push_back((*M)->Signature);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001238 AddPath((*M)->FileName, Record);
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001239 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001240 Stream.EmitRecord(IMPORTS, Record);
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001243 // Language options.
1244 Record.clear();
1245 const LangOptions &LangOpts = Context.getLangOpts();
1246#define LANGOPT(Name, Bits, Default, Description) \
1247 Record.push_back(LangOpts.Name);
1248#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1249 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Stephen Hines176edba2014-12-01 14:53:08 -08001250#include "clang/Basic/LangOptions.def"
1251#define SANITIZER(NAME, ID) \
1252 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietz4f45bc02013-01-18 11:30:38 +00001253#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001254
1255 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1256 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1257
1258 Record.push_back(LangOpts.CurrentModule.size());
1259 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001260
1261 // Comment options.
1262 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1263 for (CommentOptions::BlockCommandNamesTy::const_iterator
1264 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1265 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1266 I != IEnd; ++I) {
1267 AddString(*I, Record);
1268 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001269 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001270
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001271 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1272
Douglas Gregoree097c12012-10-18 17:58:09 +00001273 // Target options.
1274 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001275 const TargetInfo &Target = Context.getTargetInfo();
1276 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001277 AddString(TargetOpts.Triple, Record);
1278 AddString(TargetOpts.CPU, Record);
1279 AddString(TargetOpts.ABI, Record);
Douglas Gregoree097c12012-10-18 17:58:09 +00001280 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1281 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1282 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1283 }
1284 Record.push_back(TargetOpts.Features.size());
1285 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1286 AddString(TargetOpts.Features[I], Record);
1287 }
1288 Stream.EmitRecord(TARGET_OPTIONS, Record);
1289
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001290 // Diagnostic options.
1291 Record.clear();
1292 const DiagnosticOptions &DiagOpts
1293 = Context.getDiagnostics().getDiagnosticOptions();
1294#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1295#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1296 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1297#include "clang/Basic/DiagnosticOptions.def"
1298 Record.push_back(DiagOpts.Warnings.size());
1299 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1300 AddString(DiagOpts.Warnings[I], Record);
Stephen Hines176edba2014-12-01 14:53:08 -08001301 Record.push_back(DiagOpts.Remarks.size());
1302 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1303 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001304 // Note: we don't serialize the log or serialization file names, because they
1305 // are generally transient files and will almost always be overridden.
1306 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1307
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001308 // File system options.
1309 Record.clear();
1310 const FileSystemOptions &FSOpts
1311 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1312 AddString(FSOpts.WorkingDir, Record);
1313 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1314
Douglas Gregorbbf38312012-10-24 16:50:34 +00001315 // Header search options.
1316 Record.clear();
1317 const HeaderSearchOptions &HSOpts
1318 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1319 AddString(HSOpts.Sysroot, Record);
1320
1321 // Include entries.
1322 Record.push_back(HSOpts.UserEntries.size());
1323 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1324 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1325 AddString(Entry.Path, Record);
1326 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001327 Record.push_back(Entry.IsFramework);
1328 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001329 }
1330
1331 // System header prefixes.
1332 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1333 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1334 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1335 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1336 }
1337
1338 AddString(HSOpts.ResourceDir, Record);
1339 AddString(HSOpts.ModuleCachePath, Record);
Stephen Hines651f13c2014-04-23 16:59:28 -07001340 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001341 Record.push_back(HSOpts.DisableModuleHash);
1342 Record.push_back(HSOpts.UseBuiltinIncludes);
1343 Record.push_back(HSOpts.UseStandardSystemIncludes);
1344 Record.push_back(HSOpts.UseStandardCXXIncludes);
1345 Record.push_back(HSOpts.UseLibcxx);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001346 // Write out the specific module cache path that contains the module files.
1347 AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001348 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1349
Douglas Gregora71a7d82012-10-24 20:05:57 +00001350 // Preprocessor options.
1351 Record.clear();
1352 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1353
1354 // Macro definitions.
1355 Record.push_back(PPOpts.Macros.size());
1356 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1357 AddString(PPOpts.Macros[I].first, Record);
1358 Record.push_back(PPOpts.Macros[I].second);
1359 }
1360
1361 // Includes
1362 Record.push_back(PPOpts.Includes.size());
1363 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1364 AddString(PPOpts.Includes[I], Record);
1365
1366 // Macro includes
1367 Record.push_back(PPOpts.MacroIncludes.size());
1368 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1369 AddString(PPOpts.MacroIncludes[I], Record);
1370
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001371 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001372 // Detailed record is important since it is used for the module cache hash.
1373 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001374 AddString(PPOpts.ImplicitPCHInclude, Record);
1375 AddString(PPOpts.ImplicitPTHInclude, Record);
1376 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1377 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1378
Douglas Gregor31d375f2011-05-06 21:43:30 +00001379 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001380 SourceManager &SM = Context.getSourceManager();
1381 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1382 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001383 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1384 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001385 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1386 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1387
Douglas Gregora71a7d82012-10-24 20:05:57 +00001388 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001389 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001390 Record.push_back(SM.getMainFileID().getOpaqueValue());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001391 EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
Douglas Gregorb64c1932009-05-12 01:31:05 +00001392 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001393
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001394 Record.clear();
1395 Record.push_back(SM.getMainFileID().getOpaqueValue());
1396 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1397
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001398 // Original PCH directory
1399 if (!OutputFile.empty() && OutputFile != "-") {
1400 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1401 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1403 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1404
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001405 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001406
1407 llvm::sys::fs::make_absolute(OutputPath);
1408 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1409
1410 RecordData Record;
1411 Record.push_back(ORIGINAL_PCH_DIR);
1412 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1413 }
1414
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001415 WriteInputFiles(Context.SourceMgr,
1416 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregorb22d1942013-07-22 20:48:33 +00001417 PP.getLangOpts().Modules);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001418 Stream.ExitBlock();
1419}
1420
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001421namespace {
1422 /// \brief An input file.
1423 struct InputFileEntry {
1424 const FileEntry *File;
1425 bool IsSystemFile;
1426 bool BufferOverridden;
1427 };
1428}
1429
1430void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1431 HeaderSearchOptions &HSOpts,
Douglas Gregorb22d1942013-07-22 20:48:33 +00001432 bool Modules) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001433 using namespace llvm;
1434 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1435 RecordData Record;
1436
1437 // Create input-file abbreviation.
1438 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1439 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001440 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001441 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1442 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001443 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001444 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1445 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1446
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001447 // Get all ContentCache objects for files, sorted by whether the file is a
1448 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001449 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001450 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1451 // Get this source location entry.
1452 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001453 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001454
1455 // We only care about file entries that were not overridden.
1456 if (!SLoc->isFile())
1457 continue;
1458 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001459 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001460 continue;
1461
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001462 InputFileEntry Entry;
1463 Entry.File = Cache->OrigEntry;
1464 Entry.IsSystemFile = Cache->IsSystemFile;
1465 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001466 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001467 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001468 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001469 SortedFiles.push_front(Entry);
1470 }
1471
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001472 unsigned UserFilesNum = 0;
1473 // Write out all of the input files.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001474 std::vector<uint64_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001475 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001476 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001477 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001478
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001479 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001480 if (InputFileID != 0)
1481 continue; // already recorded this file.
1482
Douglas Gregora930dc92012-10-22 18:42:04 +00001483 // Record this entry's offset.
1484 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001485
1486 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001487
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001488 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001489 ++UserFilesNum;
1490
Douglas Gregor745e6f12012-10-19 00:38:02 +00001491 Record.clear();
1492 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001493 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001494
1495 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001496 Record.push_back(Entry.File->getSize());
1497 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001498
Douglas Gregora930dc92012-10-22 18:42:04 +00001499 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001500 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001501
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001502 EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1503 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001504
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001505 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001506
1507 // Create input file offsets abbreviation.
1508 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1509 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1510 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001511 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1512 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001513 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1514 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1515
1516 // Write input file offsets.
1517 Record.clear();
1518 Record.push_back(INPUT_FILE_OFFSETS);
1519 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001520 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001521 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001522}
1523
Douglas Gregor14f79002009-04-10 03:52:48 +00001524//===----------------------------------------------------------------------===//
1525// Source Manager Serialization
1526//===----------------------------------------------------------------------===//
1527
1528/// \brief Create an abbreviation for the SLocEntry that refers to a
1529/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001530static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001531 using namespace llvm;
1532 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001533 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001538 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001543 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001544}
1545
1546/// \brief Create an abbreviation for the SLocEntry that refers to a
1547/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001548static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001549 using namespace llvm;
1550 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001551 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001557 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001558}
1559
1560/// \brief Create an abbreviation for the SLocEntry that refers to a
1561/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001562static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001563 using namespace llvm;
1564 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001565 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001567 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001568}
1569
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001570/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1571/// expansion.
1572static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001573 using namespace llvm;
1574 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001575 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001581 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001582}
1583
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001584namespace {
1585 // Trait used for the on-disk hash table of header search information.
1586 class HeaderFileInfoTrait {
1587 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001588 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001589
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001590 // Keep track of the framework names we've used during serialization.
1591 SmallVector<char, 128> FrameworkStringData;
1592 llvm::StringMap<unsigned> FrameworkNameOffset;
1593
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001594 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001595 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1596 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001597
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001598 struct key_type {
1599 const FileEntry *FE;
1600 const char *Filename;
1601 };
1602 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001603
1604 typedef HeaderFileInfo data_type;
1605 typedef const data_type &data_type_ref;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001606 typedef unsigned hash_value_type;
1607 typedef unsigned offset_type;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001608
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001609 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001610 // The hash is based only on size/time of the file, so that the reader can
1611 // match even when symlinking or excess path elements ("foo/../", "../")
1612 // change the form of the name. However, complete path is still the key.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001613 //
1614 // FIXME: Using the mtime here will cause problems for explicit module
1615 // imports.
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001616 return llvm::hash_combine(key.FE->getSize(),
1617 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001618 }
1619
1620 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001621 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001622 using namespace llvm::support;
1623 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001624 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001625 Writer.write<uint16_t>(KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001626 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001627 if (Data.isModuleHeader)
1628 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07001629 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001630 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001631 }
1632
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001633 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001634 using namespace llvm::support;
1635 endian::Writer<little> LE(Out);
1636 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001637 KeyLen -= 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001638 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001639 KeyLen -= 8;
1640 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001641 }
1642
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001643 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001644 data_type_ref Data, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001645 using namespace llvm::support;
1646 endian::Writer<little> LE(Out);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001647 uint64_t Start = Out.tell(); (void)Start;
1648
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001649 unsigned char Flags = (Data.HeaderRole << 6)
1650 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001651 | (Data.isPragmaOnce << 4)
1652 | (Data.DirInfo << 2)
1653 | (Data.Resolved << 1)
1654 | Data.IndexHeaderMapHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -07001655 LE.write<uint8_t>(Flags);
1656 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001657
1658 if (!Data.ControllingMacro)
Stephen Hines651f13c2014-04-23 16:59:28 -07001659 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001660 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001661 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001662
1663 unsigned Offset = 0;
1664 if (!Data.Framework.empty()) {
1665 // If this header refers into a framework, save the framework name.
1666 llvm::StringMap<unsigned>::iterator Pos
1667 = FrameworkNameOffset.find(Data.Framework);
1668 if (Pos == FrameworkNameOffset.end()) {
1669 Offset = FrameworkStringData.size() + 1;
1670 FrameworkStringData.append(Data.Framework.begin(),
1671 Data.Framework.end());
1672 FrameworkStringData.push_back(0);
1673
1674 FrameworkNameOffset[Data.Framework] = Offset;
1675 } else
1676 Offset = Pos->second;
1677 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001678 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001679
1680 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001681 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Stephen Hines651f13c2014-04-23 16:59:28 -07001682 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001683 }
1684
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001685 assert(Out.tell() - Start == DataLen && "Wrong data length");
1686 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001687
1688 const char *strings_begin() const { return FrameworkStringData.begin(); }
1689 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001690 };
1691} // end anonymous namespace
1692
1693/// \brief Write the header search block for the list of files that
1694///
1695/// \param HS The header search structure to save.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001696void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001697 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001698 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1699
1700 if (FilesByUID.size() > HS.header_file_size())
1701 FilesByUID.resize(HS.header_file_size());
1702
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001703 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001704 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001705 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001706 unsigned NumHeaderSearchEntries = 0;
1707 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1708 const FileEntry *File = FilesByUID[UID];
1709 if (!File)
1710 continue;
1711
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001712 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1713 // from the external source if it was not provided already.
Stephen Hines651f13c2014-04-23 16:59:28 -07001714 HeaderFileInfo HFI;
1715 if (!HS.tryGetFileInfo(File, HFI) ||
1716 (HFI.External && Chain) ||
1717 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001718 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001719
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001720 // Massage the file path into an appropriate form.
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001721 const char *Filename = File->getName();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001722 SmallString<128> FilenameTmp(Filename);
1723 if (PreparePathForOutput(FilenameTmp)) {
1724 // If we performed any translation on the file name at all, we need to
1725 // save this string, since the generator will refer to it later.
1726 Filename = strdup(FilenameTmp.c_str());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001727 SavedStrings.push_back(Filename);
1728 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001729
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001730 HeaderFileInfoTrait::key_type key = { File, Filename };
1731 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001732 ++NumHeaderSearchEntries;
1733 }
1734
1735 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001736 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001737 uint32_t BucketOffset;
1738 {
Stephen Hines651f13c2014-04-23 16:59:28 -07001739 using namespace llvm::support;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001740 llvm::raw_svector_ostream Out(TableData);
1741 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07001742 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001743 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1744 }
1745
1746 // Create a blob abbreviation
1747 using namespace llvm;
1748 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1749 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1754 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1755
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001756 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001757 RecordData Record;
1758 Record.push_back(HEADER_SEARCH_TABLE);
1759 Record.push_back(BucketOffset);
1760 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001761 Record.push_back(TableData.size());
1762 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001763 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1764
1765 // Free all of the strings we had to duplicate.
1766 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001767 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001768}
1769
Douglas Gregor14f79002009-04-10 03:52:48 +00001770/// \brief Writes the block containing the serialized form of the
1771/// source manager.
1772///
1773/// TODO: We should probably use an on-disk hash table (stored in a
1774/// blob), indexed based on the file name, so that we only create
1775/// entries for files that we actually need. In the common case (no
1776/// errors), we probably won't have to create file entries for any of
1777/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001778void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001779 const Preprocessor &PP) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001780 RecordData Record;
1781
Chris Lattnerf04ad692009-04-10 17:16:57 +00001782 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001783 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001784
1785 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001786 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1787 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1788 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001789 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001790
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001791 // Write out the source location entry table. We skip the first
1792 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001793 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001794 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001795 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1796 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001797 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001798 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001799 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001800 FileID FID = FileID::get(I);
1801 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001802
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001803 // Record the offset of this source-location entry.
1804 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1805
1806 // Figure out which record code to use.
1807 unsigned Code;
1808 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001809 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1810 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001811 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001812 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001813 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001814 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001815 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001816 Record.clear();
1817 Record.push_back(Code);
1818
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001819 // Starting offset of this entry within this module, so skip the dummy.
1820 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001821 if (SLoc->isFile()) {
1822 const SrcMgr::FileInfo &File = SLoc->getFile();
1823 Record.push_back(File.getIncludeLoc().getRawEncoding());
1824 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1825 Record.push_back(File.hasLineDirectives());
1826
1827 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001828 if (Content->OrigEntry) {
1829 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001830 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001831
Douglas Gregora930dc92012-10-22 18:42:04 +00001832 // The source location entry is a file. Emit input file ID.
1833 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1834 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001836 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001837
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001838 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001839 if (FDI != FileDeclIDs.end()) {
1840 Record.push_back(FDI->second->FirstDeclIndex);
1841 Record.push_back(FDI->second->DeclIDs.size());
1842 } else {
1843 Record.push_back(0);
1844 Record.push_back(0);
1845 }
Douglas Gregora081da52011-11-16 20:05:18 +00001846
Douglas Gregora930dc92012-10-22 18:42:04 +00001847 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001848
1849 if (Content->BufferOverridden) {
1850 Record.clear();
1851 Record.push_back(SM_SLOC_BUFFER_BLOB);
1852 const llvm::MemoryBuffer *Buffer
1853 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1854 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1855 StringRef(Buffer->getBufferStart(),
1856 Buffer->getBufferSize() + 1));
1857 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001858 } else {
1859 // The source location entry is a buffer. The blob associated
1860 // with this entry contains the contents of the buffer.
1861
1862 // We add one to the size so that we capture the trailing NULL
1863 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1864 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001865 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001866 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001867 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001868 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001869 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001870 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001871 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001872 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001873 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001874 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001875
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001876 if (strcmp(Name, "<built-in>") == 0) {
1877 PreloadSLocs.push_back(SLocEntryOffsets.size());
1878 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001879 }
1880 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001881 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001882 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001883 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1884 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001885 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1886 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001887
1888 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001889 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001890 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001891 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001892 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001893 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001894 }
1895 }
1896
Douglas Gregorc9490c02009-04-16 22:23:12 +00001897 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001898
1899 if (SLocEntryOffsets.empty())
1900 return;
1901
Sebastian Redl3397c552010-08-18 23:56:27 +00001902 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001903 // table is used for lazily loading source-location information.
1904 using namespace llvm;
1905 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001906 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1910 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001912 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001913 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001914 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001915 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001916 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001917
Sebastian Redl3397c552010-08-18 23:56:27 +00001918 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001919 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001920 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001921
1922 // Write the line table. It depends on remapping working, so it must come
1923 // after the source location offsets.
1924 if (SourceMgr.hasLineTable()) {
1925 LineTableInfo &LineTable = SourceMgr.getLineTable();
1926
1927 Record.clear();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001928 // Emit the file names.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001929 Record.push_back(LineTable.getNumFilenames());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001930 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I)
1931 AddPath(LineTable.getFilename(I), Record);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001932
1933 // Emit the line entries
1934 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1935 L != LEnd; ++L) {
1936 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001937 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001938 continue;
1939
1940 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001941 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001942
1943 // Emit the line entries
1944 Record.push_back(L->second.size());
1945 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1946 LEEnd = L->second.end();
1947 LE != LEEnd; ++LE) {
1948 Record.push_back(LE->FileOffset);
1949 Record.push_back(LE->LineNo);
1950 Record.push_back(LE->FilenameID);
1951 Record.push_back((unsigned)LE->FileKind);
1952 Record.push_back(LE->IncludeOffset);
1953 }
1954 }
1955 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1956 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001957}
1958
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001959//===----------------------------------------------------------------------===//
1960// Preprocessor Serialization
1961//===----------------------------------------------------------------------===//
1962
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001963namespace {
1964class ASTMacroTableTrait {
1965public:
1966 typedef IdentID key_type;
1967 typedef key_type key_type_ref;
1968
1969 struct Data {
1970 uint32_t MacroDirectivesOffset;
1971 };
1972
1973 typedef Data data_type;
1974 typedef const data_type &data_type_ref;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001975 typedef unsigned hash_value_type;
1976 typedef unsigned offset_type;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001977
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001978 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001979 return llvm::hash_value(IdID);
1980 }
1981
1982 std::pair<unsigned,unsigned>
1983 static EmitKeyDataLength(raw_ostream& Out,
1984 key_type_ref Key, data_type_ref Data) {
1985 unsigned KeyLen = 4; // IdentID.
1986 unsigned DataLen = 4; // MacroDirectivesOffset.
1987 return std::make_pair(KeyLen, DataLen);
1988 }
1989
1990 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001991 using namespace llvm::support;
1992 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001993 }
1994
1995 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1996 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001997 using namespace llvm::support;
1998 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001999 }
2000};
2001} // end anonymous namespace
2002
Benjamin Kramer767b3d22013-09-22 14:10:29 +00002003static int compareMacroDirectives(
2004 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
2005 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
2006 return X->first->getName().compare(Y->first->getName());
Douglas Gregor9c736102011-02-10 18:20:09 +00002007}
2008
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002009static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2010 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002011 if (MacroInfo *MI = MD->getMacroInfo())
2012 if (MI->isBuiltinMacro())
2013 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002014
2015 if (IsModule) {
Stephen Hines176edba2014-12-01 14:53:08 -08002016 // Re-export any imported directives.
2017 if (MD->isImported())
2018 return false;
2019
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002020 SourceLocation Loc = MD->getLocation();
2021 if (Loc.isInvalid())
2022 return true;
2023 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2024 return true;
2025 }
2026
2027 return false;
2028}
2029
Chris Lattner0b1fb982009-04-10 17:15:23 +00002030/// \brief Writes the block containing the serialized form of the
2031/// preprocessor.
2032///
Douglas Gregor7143aab2011-09-01 17:04:32 +00002033void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002034 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2035 if (PPRec)
2036 WritePreprocessorDetail(*PPRec);
2037
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002038 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00002039
Chris Lattnerc1f9d822009-04-13 01:29:17 +00002040 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2041 if (PP.getCounterValue() != 0) {
2042 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002043 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00002044 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002045 }
2046
2047 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00002048 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Sebastian Redl3397c552010-08-18 23:56:27 +00002050 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002051 // FIXME: use diagnostics subsystem for localization etc.
2052 if (PP.SawDateOrTime())
2053 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Douglas Gregorecdcb882010-10-20 22:00:55 +00002055
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002056 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002057 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00002058
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002059 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002060 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002061 MacroDirectives;
2062 for (Preprocessor::macro_iterator
2063 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2064 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002065 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002066 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00002067 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002068
Douglas Gregor9c736102011-02-10 18:20:09 +00002069 // Sort the set of macro definitions that need to be serialized by the
2070 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002071 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2072 &compareMacroDirectives);
2073
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002074 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002075
2076 // Emit the macro directives as a list and associate the offset with the
2077 // identifier they belong to.
2078 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2079 const IdentifierInfo *Name = MacroDirectives[I].first;
2080 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2081 MacroDirective *MD = MacroDirectives[I].second;
2082
2083 // If the macro or identifier need no updates, don't write the macro history
2084 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002085 // FIXME: Chain the macro history instead of re-writing it.
2086 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002087 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2088 continue;
2089
2090 // Emit the macro directives in reverse source order.
2091 for (; MD; MD = MD->getPrevious()) {
2092 if (shouldIgnoreMacro(MD, IsModule, PP))
2093 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002094
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002095 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002096 Record.push_back(MD->getKind());
Stephen Hines176edba2014-12-01 14:53:08 -08002097 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002098 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2099 Record.push_back(InfoID);
Stephen Hines176edba2014-12-01 14:53:08 -08002100 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002101 Record.push_back(DefMD->isAmbiguous());
Stephen Hines176edba2014-12-01 14:53:08 -08002102 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2103 Record.push_back(UndefMD->getOwningModuleID());
2104 } else {
2105 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002106 Record.push_back(VisMD->isPublic());
2107 }
Stephen Hines176edba2014-12-01 14:53:08 -08002108
2109 if (MD->isImported()) {
2110 auto Overrides = MD->getOverriddenModules();
2111 Record.push_back(Overrides.size());
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002112 Record.append(Overrides.begin(), Overrides.end());
Stephen Hines176edba2014-12-01 14:53:08 -08002113 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002114 }
2115 if (Record.empty())
2116 continue;
2117
2118 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2119 Record.clear();
2120
2121 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2122
2123 IdentID NameID = getIdentifierRef(Name);
2124 ASTMacroTableTrait::Data data;
2125 data.MacroDirectivesOffset = MacroDirectiveOffset;
2126 Generator.insert(NameID, data);
2127 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002128
Douglas Gregora8235d62012-10-09 23:05:51 +00002129 /// \brief Offsets of each of the macros into the bitstream, indexed by
2130 /// the local macro ID
2131 ///
2132 /// For each identifier that is associated with a macro, this map
2133 /// provides the offset into the bitstream where that macro is
2134 /// defined.
2135 std::vector<uint32_t> MacroOffsets;
2136
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002137 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2138 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2139 MacroInfo *MI = MacroInfosToEmit[I].MI;
2140 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00002141
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002142 if (ID < FirstMacroID) {
2143 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2144 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00002145 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002146
2147 // Record the local offset of this macro.
2148 unsigned Index = ID - FirstMacroID;
2149 if (Index == MacroOffsets.size())
2150 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2151 else {
2152 if (Index > MacroOffsets.size())
2153 MacroOffsets.resize(Index + 1);
2154
2155 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2156 }
2157
2158 AddIdentifierRef(Name, Record);
2159 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2160 AddSourceLocation(MI->getDefinitionLoc(), Record);
2161 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2162 Record.push_back(MI->isUsed());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002163 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002164 unsigned Code;
2165 if (MI->isObjectLike()) {
2166 Code = PP_MACRO_OBJECT_LIKE;
2167 } else {
2168 Code = PP_MACRO_FUNCTION_LIKE;
2169
2170 Record.push_back(MI->isC99Varargs());
2171 Record.push_back(MI->isGNUVarargs());
2172 Record.push_back(MI->hasCommaPasting());
2173 Record.push_back(MI->getNumArgs());
2174 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2175 I != E; ++I)
2176 AddIdentifierRef(*I, Record);
2177 }
2178
2179 // If we have a detailed preprocessing record, record the macro definition
2180 // ID that corresponds to this macro.
2181 if (PPRec)
2182 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2183
2184 Stream.EmitRecord(Code, Record);
2185 Record.clear();
2186
2187 // Emit the tokens array.
2188 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2189 // Note that we know that the preprocessor does not have any annotation
2190 // tokens in it because they are created by the parser, and thus can't
2191 // be in a macro definition.
2192 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002193 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002194 Stream.EmitRecord(PP_TOKEN, Record);
2195 Record.clear();
2196 }
2197 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002198 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002199
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002200 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002201
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002202 // Create the on-disk hash table in a buffer.
2203 SmallString<4096> MacroTable;
2204 uint32_t BucketOffset;
2205 {
Stephen Hines651f13c2014-04-23 16:59:28 -07002206 using namespace llvm::support;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002207 llvm::raw_svector_ostream Out(MacroTable);
2208 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07002209 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002210 BucketOffset = Generator.Emit(Out);
2211 }
2212
2213 // Write the macro table
2214 using namespace llvm;
2215 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2216 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2219 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2220
2221 Record.push_back(MACRO_TABLE);
2222 Record.push_back(BucketOffset);
2223 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2224 Record.clear();
2225
Douglas Gregora8235d62012-10-09 23:05:51 +00002226 // Write the offsets table for macro IDs.
2227 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002228 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002229 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2233
2234 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2235 Record.clear();
2236 Record.push_back(MACRO_OFFSET);
2237 Record.push_back(MacroOffsets.size());
2238 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2239 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2240 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002241}
2242
2243void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002244 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002245 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002246
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002247 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002248
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002249 // Enter the preprocessor block.
2250 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002251
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002252 // If the preprocessor has a preprocessing record, emit it.
2253 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002254 using namespace llvm;
2255
2256 // Set up the abbreviation for
2257 unsigned InclusionAbbrev = 0;
2258 {
2259 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2260 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2266 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2267 }
2268
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002269 unsigned FirstPreprocessorEntityID
2270 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2271 + NUM_PREDEF_PP_ENTITY_IDS;
2272 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002273 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002274 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2275 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002276 E != EEnd;
2277 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002278 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002279
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002280 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2281 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002282
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002283 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002284 // Record this macro definition's ID.
2285 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002286
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002287 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002288 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2289 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002290 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002291
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002292 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002293 Record.push_back(ME->isBuiltinMacro());
2294 if (ME->isBuiltinMacro())
2295 AddIdentifierRef(ME->getName(), Record);
2296 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002297 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002298 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002299 continue;
2300 }
2301
2302 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2303 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002304 Record.push_back(ID->getFileName().size());
2305 Record.push_back(ID->wasInQuotes());
2306 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002307 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002308 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002309 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002310 // Check that the FileEntry is not null because it was not resolved and
2311 // we create a PCH even with compiler errors.
2312 if (ID->getFile())
2313 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002314 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2315 continue;
2316 }
2317
2318 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2319 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002320 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002321
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002322 // Write the offsets table for the preprocessing record.
2323 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002324 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2325
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002326 // Write the offsets table for identifier IDs.
2327 using namespace llvm;
2328 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002329 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002332 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002333
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002334 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002335 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002336 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002337 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2338 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002339 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002340}
2341
Douglas Gregore209e502011-12-06 01:10:29 +00002342unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2343 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2344 if (Known != SubmoduleIDs.end())
2345 return Known->second;
2346
2347 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2348}
2349
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002350unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2351 if (!Mod)
2352 return 0;
2353
2354 llvm::DenseMap<Module *, unsigned>::const_iterator
2355 Known = SubmoduleIDs.find(Mod);
2356 if (Known != SubmoduleIDs.end())
2357 return Known->second;
2358
2359 return 0;
2360}
2361
Douglas Gregor26ced122011-12-01 00:59:36 +00002362/// \brief Compute the number of modules within the given tree (including the
2363/// given module).
2364static unsigned getNumberOfModules(Module *Mod) {
2365 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002366 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2367 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002368 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002369 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002370
2371 return ChildModules + 1;
2372}
2373
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002374void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002375 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002376 // FIXME: This feels like it belongs somewhere else, but there are no
2377 // other consumers of this information.
2378 SourceManager &SrcMgr = PP->getSourceManager();
2379 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Stephen Hines651f13c2014-04-23 16:59:28 -07002380 for (const auto *I : Context->local_imports()) {
Douglas Gregor55988682011-12-05 16:33:54 +00002381 if (Module *ImportedFrom
2382 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2383 SrcMgr))) {
2384 ImportedFrom->Imports.push_back(I->getImportedModule());
2385 }
2386 }
2387
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002388 // Enter the submodule description block.
Stephen Hines176edba2014-12-01 14:53:08 -08002389 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002390
2391 // Write the abbreviations needed for the submodules block.
2392 using namespace llvm;
2393 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2394 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Stephen Hines651f13c2014-04-23 16:59:28 -07002399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2406 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2407
2408 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002409 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2411 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2412
2413 Abbrev = new BitCodeAbbrev();
2414 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2416 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002417
2418 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002419 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2421 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2422
2423 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002424 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2426 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2427
Douglas Gregor51f564f2011-12-31 04:05:44 +00002428 Abbrev = new BitCodeAbbrev();
2429 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smith5794b532013-10-28 22:18:19 +00002430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor51f564f2011-12-31 04:05:44 +00002432 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2433
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002434 Abbrev = new BitCodeAbbrev();
2435 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2437 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2438
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002439 Abbrev = new BitCodeAbbrev();
Stephen Hines176edba2014-12-01 14:53:08 -08002440 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2442 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2443
2444 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002445 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2447 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2448
2449 Abbrev = new BitCodeAbbrev();
Stephen Hines176edba2014-12-01 14:53:08 -08002450 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2452 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2453
2454 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002455 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2458 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2459
Douglas Gregor63a72682013-03-20 00:22:05 +00002460 Abbrev = new BitCodeAbbrev();
2461 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2462 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2463 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2464
Douglas Gregor906d66a2013-03-20 21:10:35 +00002465 Abbrev = new BitCodeAbbrev();
2466 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2467 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2468 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2469 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2470
Douglas Gregor26ced122011-12-01 00:59:36 +00002471 // Write the submodule metadata block.
2472 RecordData Record;
2473 Record.push_back(getNumberOfModules(WritingModule));
2474 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2475 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2476
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002477 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002478 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002479 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002480 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002481 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002482 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002483 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002484
2485 // Emit the definition of the block.
2486 Record.clear();
2487 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002488 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002489 if (Mod->Parent) {
2490 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2491 Record.push_back(SubmoduleIDs[Mod->Parent]);
2492 } else {
2493 Record.push_back(0);
2494 }
2495 Record.push_back(Mod->IsFramework);
2496 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002497 Record.push_back(Mod->IsSystem);
Stephen Hines651f13c2014-04-23 16:59:28 -07002498 Record.push_back(Mod->IsExternC);
Douglas Gregor1e123682011-12-05 22:27:44 +00002499 Record.push_back(Mod->InferSubmodules);
2500 Record.push_back(Mod->InferExplicitSubmodules);
2501 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002502 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002503 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2504
Douglas Gregor51f564f2011-12-31 04:05:44 +00002505 // Emit the requirements.
Richard Smith5794b532013-10-28 22:18:19 +00002506 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +00002507 Record.clear();
2508 Record.push_back(SUBMODULE_REQUIRES);
Richard Smith5794b532013-10-28 22:18:19 +00002509 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002510 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smith5794b532013-10-28 22:18:19 +00002511 Mod->Requirements[I].first);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002512 }
2513
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002514 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002515 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002516 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002517 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002518 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002519 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002520 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2521 Record.clear();
2522 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2523 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2524 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002525 }
Stephen Hines176edba2014-12-01 14:53:08 -08002526
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002527 // Emit the headers.
Stephen Hines176edba2014-12-01 14:53:08 -08002528 struct {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002529 unsigned RecordKind;
Stephen Hines176edba2014-12-01 14:53:08 -08002530 unsigned Abbrev;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002531 Module::HeaderKind HeaderKind;
Stephen Hines176edba2014-12-01 14:53:08 -08002532 } HeaderLists[] = {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002533 {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2534 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2535 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
Stephen Hines176edba2014-12-01 14:53:08 -08002536 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002537 Module::HK_PrivateTextual},
2538 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
Stephen Hines176edba2014-12-01 14:53:08 -08002539 };
2540 for (auto &HL : HeaderLists) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002541 Record.clear();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002542 Record.push_back(HL.RecordKind);
2543 for (auto &H : Mod->Headers[HL.HeaderKind])
2544 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2545 }
2546
2547 // Emit the top headers.
2548 {
2549 auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2550 Record.clear();
2551 Record.push_back(SUBMODULE_TOPHEADER);
2552 for (auto *H : TopHeaders)
2553 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002554 }
Douglas Gregor55988682011-12-05 16:33:54 +00002555
2556 // Emit the imports.
2557 if (!Mod->Imports.empty()) {
2558 Record.clear();
2559 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002560 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002561 assert(ImportedID && "Unknown submodule!");
Douglas Gregor55988682011-12-05 16:33:54 +00002562 Record.push_back(ImportedID);
2563 }
2564 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2565 }
2566
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002567 // Emit the exports.
2568 if (!Mod->Exports.empty()) {
2569 Record.clear();
2570 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002571 if (Module *Exported = Mod->Exports[I].getPointer()) {
2572 unsigned ExportedID = SubmoduleIDs[Exported];
2573 assert(ExportedID > 0 && "Unknown submodule ID?");
2574 Record.push_back(ExportedID);
2575 } else {
2576 Record.push_back(0);
2577 }
2578
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002579 Record.push_back(Mod->Exports[I].getInt());
2580 }
2581 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2582 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002583
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00002584 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2585 // Might be unnecessary as use declarations are only used to build the
2586 // module itself.
2587
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002588 // Emit the link libraries.
2589 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2590 Record.clear();
2591 Record.push_back(SUBMODULE_LINK_LIBRARY);
2592 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2593 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2594 Mod->LinkLibraries[I].Library);
2595 }
2596
Douglas Gregor906d66a2013-03-20 21:10:35 +00002597 // Emit the conflicts.
2598 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2599 Record.clear();
2600 Record.push_back(SUBMODULE_CONFLICT);
2601 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2602 assert(OtherID && "Unknown submodule!");
2603 Record.push_back(OtherID);
2604 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2605 Mod->Conflicts[I].Message);
2606 }
2607
Douglas Gregor63a72682013-03-20 00:22:05 +00002608 // Emit the configuration macros.
2609 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2610 Record.clear();
2611 Record.push_back(SUBMODULE_CONFIG_MACRO);
2612 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2613 Mod->ConfigMacros[I]);
2614 }
2615
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002616 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002617 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2618 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002619 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002620 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002621 }
2622
2623 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002624
2625 assert((NextSubmoduleID - FirstSubmoduleID
2626 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002627}
2628
Douglas Gregor185dbd72011-12-01 02:07:58 +00002629serialization::SubmoduleID
2630ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002631 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002632 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002633
2634 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002635 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002636 Module *OwningMod
2637 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002638 if (!OwningMod)
2639 return 0;
2640
Douglas Gregore209e502011-12-06 01:10:29 +00002641 // Check whether this submodule is part of our own module.
2642 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002643 return 0;
2644
Douglas Gregore209e502011-12-06 01:10:29 +00002645 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002646}
2647
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002648void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2649 bool isModule) {
2650 // Make sure set diagnostic pragmas don't affect the translation unit that
2651 // imports the module.
2652 // FIXME: Make diagnostic pragma sections work properly with modules.
2653 if (isModule)
2654 return;
2655
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002656 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2657 DiagStateIDMap;
2658 unsigned CurrID = 0;
2659 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002660 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002661 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002662 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2663 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002664 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002665 if (point.Loc.isInvalid())
2666 continue;
2667
2668 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002669 unsigned &DiagStateID = DiagStateIDMap[point.State];
2670 Record.push_back(DiagStateID);
2671
2672 if (DiagStateID == 0) {
2673 DiagStateID = ++CurrID;
2674 for (DiagnosticsEngine::DiagState::const_iterator
2675 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2676 if (I->second.isPragma()) {
2677 Record.push_back(I->first);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002678 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002679 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002680 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002681 Record.push_back(-1); // mark the end of the diag/map pairs for this
2682 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002683 }
2684 }
2685
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002686 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002687 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002688}
2689
Anders Carlssonc8505782011-03-06 18:41:18 +00002690void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2691 if (CXXBaseSpecifiersOffsets.empty())
2692 return;
2693
2694 RecordData Record;
2695
2696 // Create a blob abbreviation for the C++ base specifiers offsets.
2697 using namespace llvm;
2698
2699 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2700 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2702 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2703 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2704
Douglas Gregore92b8a12011-08-04 00:01:48 +00002705 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002706 Record.clear();
2707 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2708 Record.push_back(CXXBaseSpecifiersOffsets.size());
2709 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002710 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002711}
2712
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002713//===----------------------------------------------------------------------===//
2714// Type Serialization
2715//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002716
Sebastian Redl3397c552010-08-18 23:56:27 +00002717/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002718void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002719 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002720 if (Idx.getIndex() == 0) // we haven't seen this type before.
2721 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregor97475832010-10-05 18:37:06 +00002723 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002724
Douglas Gregor2cf26342009-04-09 22:27:44 +00002725 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002726 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002727 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002728 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002729 else if (TypeOffsets.size() < Index) {
2730 TypeOffsets.resize(Index + 1);
2731 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002732 }
2733
2734 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Douglas Gregor2cf26342009-04-09 22:27:44 +00002736 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002737 ASTTypeWriter W(*this, Record);
Stephen Hines176edba2014-12-01 14:53:08 -08002738 W.AbbrevToUse = 0;
John McCall0953e762009-09-24 19:53:00 +00002739
Douglas Gregora4923eb2009-11-16 21:35:15 +00002740 if (T.hasLocalNonFastQualifiers()) {
2741 Qualifiers Qs = T.getLocalQualifiers();
2742 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002743 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002744 W.Code = TYPE_EXT_QUAL;
Stephen Hines176edba2014-12-01 14:53:08 -08002745 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall0953e762009-09-24 19:53:00 +00002746 } else {
2747 switch (T->getTypeClass()) {
2748 // For all of the concrete, non-dependent types, call the
2749 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002750#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002751 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002752#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002753#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002754 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002755 }
2756
2757 // Emit the serialized record.
Stephen Hines176edba2014-12-01 14:53:08 -08002758 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregor0b748912009-04-14 21:18:50 +00002759
2760 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002761 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002762}
2763
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002764//===----------------------------------------------------------------------===//
2765// Declaration Serialization
2766//===----------------------------------------------------------------------===//
2767
Douglas Gregor2cf26342009-04-09 22:27:44 +00002768/// \brief Write the block containing all of the declaration IDs
2769/// lexically declared within the given DeclContext.
2770///
2771/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2772/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002773uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002774 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002775 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002776 return 0;
2777
Douglas Gregorc9490c02009-04-16 22:23:12 +00002778 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002779 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002780 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002781 SmallVector<KindDeclIDPair, 64> Decls;
Stephen Hines651f13c2014-04-23 16:59:28 -07002782 for (const auto *D : DC->decls())
2783 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002784
Douglas Gregor25123082009-04-22 22:34:57 +00002785 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002786 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002787 return Offset;
2788}
2789
Sebastian Redla4232eb2010-08-18 23:56:21 +00002790void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002791 using namespace llvm;
2792 RecordData Record;
2793
2794 // Write the type offsets array
2795 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002796 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2800 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2801 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002802 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002803 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002804 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002805 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002806
2807 // Write the declaration offsets array
2808 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002809 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2813 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2814 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002815 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002816 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002817 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002818 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002819}
2820
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002821void ASTWriter::WriteFileDeclIDsMap() {
2822 using namespace llvm;
2823 RecordData Record;
2824
2825 // Join the vectors of DeclIDs from all files.
2826 SmallVector<DeclID, 256> FileSortedIDs;
2827 for (FileDeclIDsTy::iterator
2828 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2829 DeclIDInFileInfo &Info = *FI->second;
2830 Info.FirstDeclIndex = FileSortedIDs.size();
2831 for (LocDeclIDsTy::iterator
2832 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2833 FileSortedIDs.push_back(DI->second);
2834 }
2835
2836 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2837 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002838 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002839 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2840 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2841 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002842 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002843 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2844}
2845
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002846void ASTWriter::WriteComments() {
2847 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002848 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002849 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002850 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2851 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002852 I != E; ++I) {
2853 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002854 AddSourceRange((*I)->getSourceRange(), Record);
2855 Record.push_back((*I)->getKind());
2856 Record.push_back((*I)->isTrailingComment());
2857 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002858 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2859 }
2860 Stream.ExitBlock();
2861}
2862
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002863//===----------------------------------------------------------------------===//
2864// Global Method Pool and Selector Serialization
2865//===----------------------------------------------------------------------===//
2866
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002867namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002868// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002869class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002870 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002871
2872public:
2873 typedef Selector key_type;
2874 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Sebastian Redl5d050072010-08-04 17:20:04 +00002876 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002877 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002878 ObjCMethodList Instance, Factory;
2879 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002880 typedef const data_type& data_type_ref;
2881
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002882 typedef unsigned hash_value_type;
2883 typedef unsigned offset_type;
2884
Sebastian Redl3397c552010-08-18 23:56:27 +00002885 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002886
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002887 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002888 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002889 }
Mike Stump1eb44332009-09-09 15:08:12 +00002890
2891 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002892 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002893 data_type_ref Methods) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002894 using namespace llvm::support;
2895 endian::Writer<little> LE(Out);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002896 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Stephen Hines651f13c2014-04-23 16:59:28 -07002897 LE.write<uint16_t>(KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002898 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2899 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002900 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002901 if (Method->getMethod())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002902 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002903 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002904 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002905 if (Method->getMethod())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002906 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07002907 LE.write<uint16_t>(DataLen);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002908 return std::make_pair(KeyLen, DataLen);
2909 }
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Chris Lattner5f9e2722011-07-23 10:55:15 +00002911 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002912 using namespace llvm::support;
2913 endian::Writer<little> LE(Out);
Mike Stump1eb44332009-09-09 15:08:12 +00002914 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002915 assert((Start >> 32) == 0 && "Selector key offset too large");
2916 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002917 unsigned N = Sel.getNumArgs();
Stephen Hines651f13c2014-04-23 16:59:28 -07002918 LE.write<uint16_t>(N);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002919 if (N == 0)
2920 N = 1;
2921 for (unsigned I = 0; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07002922 LE.write<uint32_t>(
2923 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002924 }
Mike Stump1eb44332009-09-09 15:08:12 +00002925
Chris Lattner5f9e2722011-07-23 10:55:15 +00002926 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002927 data_type_ref Methods, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002928 using namespace llvm::support;
2929 endian::Writer<little> LE(Out);
Douglas Gregora67e58c2009-04-24 21:49:02 +00002930 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07002931 LE.write<uint32_t>(Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002932 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002933 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002934 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002935 if (Method->getMethod())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002936 ++NumInstanceMethods;
2937
2938 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002939 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002940 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002941 if (Method->getMethod())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002942 ++NumFactoryMethods;
2943
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002944 unsigned InstanceBits = Methods.Instance.getBits();
2945 assert(InstanceBits < 4);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002946 unsigned InstanceHasMoreThanOneDeclBit =
2947 Methods.Instance.hasMoreThanOneDecl();
2948 unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2949 (InstanceHasMoreThanOneDeclBit << 2) |
2950 InstanceBits;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002951 unsigned FactoryBits = Methods.Factory.getBits();
2952 assert(FactoryBits < 4);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002953 unsigned FactoryHasMoreThanOneDeclBit =
2954 Methods.Factory.hasMoreThanOneDecl();
2955 unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2956 (FactoryHasMoreThanOneDeclBit << 2) |
2957 FactoryBits;
2958 LE.write<uint16_t>(FullInstanceBits);
2959 LE.write<uint16_t>(FullFactoryBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002960 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002961 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002962 if (Method->getMethod())
2963 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Sebastian Redl5d050072010-08-04 17:20:04 +00002964 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002965 Method = Method->getNext())
Stephen Hines0e2c34f2015-03-23 12:09:02 -07002966 if (Method->getMethod())
2967 LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002968
2969 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002970 }
2971};
2972} // end anonymous namespace
2973
Sebastian Redl059612d2010-08-03 21:58:15 +00002974/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002975///
2976/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002977/// in an on-disk hash table indexed by the selector. The hash table also
2978/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002979void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002980 using namespace llvm;
2981
Sebastian Redl059612d2010-08-03 21:58:15 +00002982 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002983 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002984 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002985 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002986 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002987 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002988 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002989 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Sebastian Redl059612d2010-08-03 21:58:15 +00002991 // Create the on-disk hash table representation. We walk through every
2992 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002993 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002994 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002995 I = SelectorIDs.begin(), E = SelectorIDs.end();
2996 I != E; ++I) {
2997 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002998 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002999 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00003000 I->second,
3001 ObjCMethodList(),
3002 ObjCMethodList()
3003 };
3004 if (F != SemaRef.MethodPool.end()) {
3005 Data.Instance = F->second.first;
3006 Data.Factory = F->second.second;
3007 }
Sebastian Redl3397c552010-08-18 23:56:27 +00003008 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00003009 // changed.
3010 if (Chain && I->second < FirstSelectorID) {
3011 // Selector already exists. Did it change?
3012 bool changed = false;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003013 for (ObjCMethodList *M = &Data.Instance;
3014 !changed && M && M->getMethod(); M = M->getNext()) {
3015 if (!M->getMethod()->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00003016 changed = true;
3017 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003018 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00003019 M = M->getNext()) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003020 if (!M->getMethod()->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00003021 changed = true;
3022 }
3023 if (!changed)
3024 continue;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003025 } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
Sebastian Redlfa78dec2010-08-04 21:22:45 +00003026 // A new method pool entry.
3027 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00003028 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003029 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003030 }
3031
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003032 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003033 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003034 uint32_t BucketOffset;
3035 {
Stephen Hines651f13c2014-04-23 16:59:28 -07003036 using namespace llvm::support;
Sebastian Redl3397c552010-08-18 23:56:27 +00003037 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003038 llvm::raw_svector_ostream Out(MethodPool);
3039 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07003040 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003041 BucketOffset = Generator.Emit(Out, Trait);
3042 }
3043
3044 // Create a blob abbreviation
3045 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003046 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00003048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3050 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3051
Douglas Gregor83941df2009-04-25 17:48:32 +00003052 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003053 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003054 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003055 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00003056 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003057 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00003058
3059 // Create a blob abbreviation for the selector table offsets.
3060 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003061 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00003062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00003064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3065 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3066
3067 // Write the selector offsets table.
3068 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003069 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00003070 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003071 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00003072 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003073 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003074 }
3075}
3076
Sebastian Redl3397c552010-08-18 23:56:27 +00003077/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003078void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00003079 using namespace llvm;
3080 if (SemaRef.ReferencedSelectors.empty())
3081 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00003082
Fariborz Jahanian32019832010-07-23 19:11:11 +00003083 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00003084
Sebastian Redl3397c552010-08-18 23:56:27 +00003085 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00003086 // very tricky to fix, and given that @selector shouldn't really appear in
3087 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00003088 for (DenseMap<Selector, SourceLocation>::iterator S =
3089 SemaRef.ReferencedSelectors.begin(),
3090 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3091 Selector Sel = (*S).first;
3092 SourceLocation Loc = (*S).second;
3093 AddSelectorRef(Sel, Record);
3094 AddSourceLocation(Loc, Record);
3095 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003096 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003097}
3098
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003099//===----------------------------------------------------------------------===//
3100// Identifier Table Serialization
3101//===----------------------------------------------------------------------===//
3102
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003103/// Determine the declaration that should be put into the name lookup table to
3104/// represent the given declaration in this module. This is usually D itself,
3105/// but if D was imported and merged into a local declaration, we want the most
3106/// recent local declaration instead. The chosen declaration will be the most
3107/// recent declaration in any module that imports this one.
3108static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3109 NamedDecl *D) {
3110 if (!LangOpts.Modules || !D->isFromASTFile())
3111 return D;
3112
3113 if (Decl *Redecl = D->getPreviousDecl()) {
3114 // For Redeclarable decls, a prior declaration might be local.
3115 for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3116 if (!Redecl->isFromASTFile())
3117 return cast<NamedDecl>(Redecl);
3118 // If we come up a decl from a (chained-)PCH stop since we won't find a
3119 // local one.
3120 if (D->getOwningModuleID() == 0)
3121 break;
3122 }
3123 } else if (Decl *First = D->getCanonicalDecl()) {
3124 // For Mergeable decls, the first decl might be local.
3125 if (!First->isFromASTFile())
3126 return cast<NamedDecl>(First);
3127 }
3128
3129 // All declarations are imported. Our most recent declaration will also be
3130 // the most recent one in anyone who imports us.
3131 return D;
3132}
3133
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003134namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00003135class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00003136 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00003137 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003138 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003139 bool IsModule;
3140
Douglas Gregora92193e2009-04-28 21:18:29 +00003141 /// \brief Determines whether this is an "interesting" identifier
3142 /// that needs a full IdentifierInfo structure written into the hash
3143 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003144 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00003145 if (II->isPoisoned() ||
3146 II->isExtensionToken() ||
3147 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00003148 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00003149 II->getFETokenInfo<void>())
3150 return true;
3151
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003152 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00003153 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003154
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003155 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003156 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00003157 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003158
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003159 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3160 if (!IsModule)
3161 return !shouldIgnoreMacro(Macro, IsModule, PP);
Stephen Hines176edba2014-12-01 14:53:08 -08003162
3163 MacroState State;
3164 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003165 return true;
3166 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003167
3168 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00003169 }
3170
Stephen Hines176edba2014-12-01 14:53:08 -08003171 enum class SubmoduleMacroState {
3172 /// We've seen nothing about this macro.
3173 None,
3174 /// We've seen a public visibility directive.
3175 Public,
3176 /// We've either exported a macro for this module or found that the
3177 /// module's definition of this macro is private.
3178 Done
3179 };
3180 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Stephen Hines651f13c2014-04-23 16:59:28 -07003181
3182 MacroDirective *
Stephen Hines176edba2014-12-01 14:53:08 -08003183 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3184 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3185 return NextMD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003186 return nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003187 }
3188
Stephen Hines651f13c2014-04-23 16:59:28 -07003189 MacroDirective *
Stephen Hines176edba2014-12-01 14:53:08 -08003190 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003191 if (MacroDirective *NextMD =
Stephen Hines176edba2014-12-01 14:53:08 -08003192 getPublicSubmoduleMacro(MD->getPrevious(), State))
3193 return NextMD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003194 return nullptr;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003195 }
3196
Stephen Hines176edba2014-12-01 14:53:08 -08003197 /// \brief Traverses the macro directives history and returns the next
3198 /// public macro definition or undefinition that has not been found so far.
3199 ///
Stephen Hines651f13c2014-04-23 16:59:28 -07003200 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003201 /// will still be considered as defined/exported from submodule A.
Stephen Hines651f13c2014-04-23 16:59:28 -07003202 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Stephen Hines176edba2014-12-01 14:53:08 -08003203 MacroState &State) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003204 if (!MD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003205 return nullptr;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003206
Stephen Hines651f13c2014-04-23 16:59:28 -07003207 Optional<bool> IsPublic;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003208 for (; MD; MD = MD->getPrevious()) {
Stephen Hines176edba2014-12-01 14:53:08 -08003209 // Once we hit an ignored macro, we're done: the rest of the chain
3210 // will all be ignored macros.
3211 if (shouldIgnoreMacro(MD, IsModule, PP))
3212 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07003213
Stephen Hines176edba2014-12-01 14:53:08 -08003214 // If this macro was imported, re-export it.
3215 if (MD->isImported())
3216 return MD;
Stephen Hines651f13c2014-04-23 16:59:28 -07003217
Stephen Hines176edba2014-12-01 14:53:08 -08003218 SubmoduleID ModID = getSubmoduleID(MD);
3219 auto &S = State[ModID];
3220 assert(ModID && "found macro in no submodule");
3221
3222 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003223 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003224
Stephen Hines176edba2014-12-01 14:53:08 -08003225 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3226 // The latest visibility directive for a name in a submodule affects all
3227 // the directives that come before it.
3228 if (S == SubmoduleMacroState::None)
3229 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3230 : SubmoduleMacroState::Done;
3231 } else {
3232 S = SubmoduleMacroState::Done;
Stephen Hines651f13c2014-04-23 16:59:28 -07003233 return MD;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003234 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003235 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003236
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003237 return nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003238 }
3239
Stephen Hines176edba2014-12-01 14:53:08 -08003240 ArrayRef<SubmoduleID>
3241 getOverriddenSubmodules(MacroDirective *MD,
3242 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3243 assert(!isa<VisibilityMacroDirective>(MD) &&
3244 "only #define and #undef can override");
3245 if (MD->isImported())
3246 return MD->getOverriddenModules();
3247
3248 ScratchSpace.clear();
3249 SubmoduleID ModID = getSubmoduleID(MD);
3250 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3251 if (shouldIgnoreMacro(MD, IsModule, PP))
3252 break;
3253
3254 // If this is a definition from a submodule import, that submodule's
3255 // definition is overridden by the definition or undefinition that we
3256 // started with.
3257 if (MD->isImported()) {
3258 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3259 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3260 assert(DefModuleID && "imported macro has no owning module");
3261 ScratchSpace.push_back(DefModuleID);
3262 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3263 // If we override a #undef, we override anything that #undef overrides.
3264 // We don't need to override it, since an active #undef doesn't affect
3265 // the meaning of a macro.
3266 auto Overrides = UndefMD->getOverriddenModules();
3267 ScratchSpace.insert(ScratchSpace.end(),
3268 Overrides.begin(), Overrides.end());
3269 }
3270 }
3271
3272 // Stop once we leave the original macro's submodule.
3273 //
3274 // Either this submodule #included another submodule of the same
3275 // module or it just happened to be built after the other module.
3276 // In the former case, we override the submodule's macro.
3277 //
3278 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3279 // these cases apart.
3280 //
3281 // FIXME: We can leave this submodule and re-enter it if it #includes a
3282 // header within a different submodule of the same module. In such cases
3283 // the overrides list will be incomplete.
3284 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3285 if (DirectiveModuleID != ModID) {
3286 if (DirectiveModuleID && !MD->isImported())
3287 ScratchSpace.push_back(DirectiveModuleID);
3288 break;
3289 }
3290 }
3291
3292 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3293 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3294 ScratchSpace.end());
3295 return ScratchSpace;
3296 }
3297
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003298 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003299 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003300 }
3301
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003302public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003303 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003304 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003305
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003306 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003307 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003308
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003309 typedef unsigned hash_value_type;
3310 typedef unsigned offset_type;
3311
Douglas Gregoreee242f2011-10-27 09:33:13 +00003312 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3313 IdentifierResolver &IdResolver, bool IsModule)
3314 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003315
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003316 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003317 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003318 }
Mike Stump1eb44332009-09-09 15:08:12 +00003319
3320 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003321 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003322 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003323 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003324 MacroDirective *Macro = nullptr;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003325 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003326 DataLen += 2; // 2 bytes for builtin ID
3327 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003328 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003329 DataLen += 4; // MacroDirectives offset.
3330 if (IsModule) {
Stephen Hines176edba2014-12-01 14:53:08 -08003331 MacroState State;
3332 SmallVector<SubmoduleID, 16> Scratch;
3333 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3334 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003335 DataLen += 4; // MacroInfo ID or ModuleID.
Stephen Hines176edba2014-12-01 14:53:08 -08003336 if (unsigned NumOverrides =
3337 getOverriddenSubmodules(MD, Scratch).size())
3338 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003339 }
Stephen Hines176edba2014-12-01 14:53:08 -08003340 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003341 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003342 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003343
Douglas Gregoreee242f2011-10-27 09:33:13 +00003344 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3345 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003346 D != DEnd; ++D)
Stephen Hines176edba2014-12-01 14:53:08 -08003347 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00003348 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003349 using namespace llvm::support;
3350 endian::Writer<little> LE(Out);
3351
3352 LE.write<uint16_t>(DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003353 // We emit the key length after the data length so that every
3354 // string is preceded by a 16-bit length. This matches the PTH
3355 // format for storing identifiers.
Stephen Hines651f13c2014-04-23 16:59:28 -07003356 LE.write<uint16_t>(KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003357 return std::make_pair(KeyLen, DataLen);
3358 }
Mike Stump1eb44332009-09-09 15:08:12 +00003359
Chris Lattner5f9e2722011-07-23 10:55:15 +00003360 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003361 unsigned KeyLen) {
3362 // Record the location of the key data. This is used when generating
3363 // the mapping from persistent IDs to strings.
3364 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003365 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003366 }
Mike Stump1eb44332009-09-09 15:08:12 +00003367
Stephen Hines651f13c2014-04-23 16:59:28 -07003368 static void emitMacroOverrides(raw_ostream &Out,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003369 ArrayRef<SubmoduleID> Overridden) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003370 if (!Overridden.empty()) {
3371 using namespace llvm::support;
3372 endian::Writer<little> LE(Out);
3373 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Stephen Hines176edba2014-12-01 14:53:08 -08003374 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3375 assert(Overridden[I] && "zero module ID for override");
Stephen Hines651f13c2014-04-23 16:59:28 -07003376 LE.write<uint32_t>(Overridden[I]);
Stephen Hines176edba2014-12-01 14:53:08 -08003377 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003378 }
3379 }
3380
Douglas Gregor7143aab2011-09-01 17:04:32 +00003381 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003382 IdentID ID, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003383 using namespace llvm::support;
3384 endian::Writer<little> LE(Out);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003385 MacroDirective *Macro = nullptr;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003386 if (!isInterestingIdentifier(II, Macro)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003387 LE.write<uint32_t>(ID << 1);
Douglas Gregora92193e2009-04-28 21:18:29 +00003388 return;
3389 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003390
Stephen Hines651f13c2014-04-23 16:59:28 -07003391 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003392 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3393 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Stephen Hines651f13c2014-04-23 16:59:28 -07003394 LE.write<uint16_t>(Bits);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003395 Bits = 0;
3396 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003397 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003398 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003399 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3400 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003401 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003402 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Stephen Hines651f13c2014-04-23 16:59:28 -07003403 LE.write<uint16_t>(Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003404
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003405 if (HadMacroDefinition) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003406 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003407 if (IsModule) {
3408 // Write the IDs of macros coming from different submodules.
Stephen Hines176edba2014-12-01 14:53:08 -08003409 MacroState State;
3410 SmallVector<SubmoduleID, 16> Scratch;
3411 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3412 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003413 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003414 // FIXME: If this macro directive was created by #pragma pop_macros,
3415 // or if it was created implicitly by resolving conflicting macros,
3416 // it may be for a different submodule from the one in the MacroInfo
3417 // object. If so, we should write out its owning ModuleID.
3418 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Stephen Hines651f13c2014-04-23 16:59:28 -07003419 assert(InfoID);
3420 LE.write<uint32_t>(InfoID << 1);
3421 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08003422 auto *UndefMD = cast<UndefMacroDirective>(MD);
3423 SubmoduleID Mod = UndefMD->isImported()
3424 ? UndefMD->getOwningModuleID()
3425 : getSubmoduleID(UndefMD);
3426 LE.write<uint32_t>((Mod << 1) | 1);
Stephen Hines651f13c2014-04-23 16:59:28 -07003427 }
Stephen Hines176edba2014-12-01 14:53:08 -08003428 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003429 }
Stephen Hines176edba2014-12-01 14:53:08 -08003430 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003431 }
Douglas Gregor13292642011-12-02 15:45:10 +00003432 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003433
Douglas Gregor668c1a42009-04-21 22:25:48 +00003434 // Emit the declaration IDs in reverse order, because the
3435 // IdentifierResolver provides the declarations as they would be
3436 // visible (e.g., the function "stat" would come before the struct
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003437 // "stat"), but the ASTReader adds declarations to the end of the list
3438 // (so we need to see the struct "stat" before the function "stat").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003439 // Only emit declarations that aren't from a chained PCH, though.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003440 SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II), IdResolver.end());
3441 for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3442 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003443 D != DEnd; ++D)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003444 LE.write<uint32_t>(
3445 Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003446 }
3447};
3448} // end anonymous namespace
3449
Sebastian Redl3397c552010-08-18 23:56:27 +00003450/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003451///
3452/// The identifier table consists of a blob containing string data
3453/// (the actual identifiers themselves) and a separate "offsets" index
3454/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003455void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3456 IdentifierResolver &IdResolver,
3457 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003458 using namespace llvm;
3459
3460 // Create and write out the blob that contains the identifier
3461 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003462 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003463 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003464 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003465
Douglas Gregor92b059e2009-04-28 20:33:11 +00003466 // Look for any identifiers that were named while processing the
3467 // headers, but are otherwise not needed. We add these to the hash
3468 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003469 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003470 // file.
3471 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3472 IDEnd = PP.getIdentifierTable().end();
3473 ID != IDEnd; ++ID)
3474 getIdentifierRef(ID->second);
3475
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003476 // Create the on-disk hash table representation. We only store offsets
3477 // for identifiers that appear here for the first time.
3478 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003479 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003480 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3481 ID != IDEnd; ++ID) {
3482 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003483 if (!Chain || !ID->first->isFromAST() ||
3484 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003485 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003486 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003487 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003488
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003489 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003490 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003491 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003492 {
Stephen Hines651f13c2014-04-23 16:59:28 -07003493 using namespace llvm::support;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003494 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003495 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003496 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07003497 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003498 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003499 }
3500
3501 // Create a blob abbreviation
3502 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003503 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003504 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003506 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003507
3508 // Write the identifier table
3509 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003510 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003511 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003512 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003513 }
3514
3515 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003516 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003517 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003518 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3521 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3522
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003523#ifndef NDEBUG
3524 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3525 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3526#endif
3527
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003528 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003529 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003530 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003531 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003532 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003533 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003534}
3535
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003536//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003537// DeclContext's Name Lookup Table Serialization
3538//===----------------------------------------------------------------------===//
3539
3540namespace {
3541// Trait used for the on-disk hash table used in the method pool.
3542class ASTDeclContextNameLookupTrait {
3543 ASTWriter &Writer;
3544
3545public:
3546 typedef DeclarationName key_type;
3547 typedef key_type key_type_ref;
3548
3549 typedef DeclContext::lookup_result data_type;
3550 typedef const data_type& data_type_ref;
3551
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003552 typedef unsigned hash_value_type;
3553 typedef unsigned offset_type;
3554
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003555 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3556
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003557 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003558 llvm::FoldingSetNodeID ID;
3559 ID.AddInteger(Name.getNameKind());
3560
3561 switch (Name.getNameKind()) {
3562 case DeclarationName::Identifier:
3563 ID.AddString(Name.getAsIdentifierInfo()->getName());
3564 break;
3565 case DeclarationName::ObjCZeroArgSelector:
3566 case DeclarationName::ObjCOneArgSelector:
3567 case DeclarationName::ObjCMultiArgSelector:
3568 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3569 break;
3570 case DeclarationName::CXXConstructorName:
3571 case DeclarationName::CXXDestructorName:
3572 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003573 break;
3574 case DeclarationName::CXXOperatorName:
3575 ID.AddInteger(Name.getCXXOverloadedOperator());
3576 break;
3577 case DeclarationName::CXXLiteralOperatorName:
3578 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3579 case DeclarationName::CXXUsingDirective:
3580 break;
3581 }
3582
3583 return ID.ComputeHash();
3584 }
3585
3586 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003587 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003588 data_type_ref Lookup) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003589 using namespace llvm::support;
3590 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003591 unsigned KeyLen = 1;
3592 switch (Name.getNameKind()) {
3593 case DeclarationName::Identifier:
3594 case DeclarationName::ObjCZeroArgSelector:
3595 case DeclarationName::ObjCOneArgSelector:
3596 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003597 case DeclarationName::CXXLiteralOperatorName:
3598 KeyLen += 4;
3599 break;
3600 case DeclarationName::CXXOperatorName:
3601 KeyLen += 1;
3602 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003603 case DeclarationName::CXXConstructorName:
3604 case DeclarationName::CXXDestructorName:
3605 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003606 case DeclarationName::CXXUsingDirective:
3607 break;
3608 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003609 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003610
3611 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003612 unsigned DataLen = 2 + 4 * Lookup.size();
Stephen Hines651f13c2014-04-23 16:59:28 -07003613 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003614
3615 return std::make_pair(KeyLen, DataLen);
3616 }
3617
Chris Lattner5f9e2722011-07-23 10:55:15 +00003618 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003619 using namespace llvm::support;
3620 endian::Writer<little> LE(Out);
3621 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003622 switch (Name.getNameKind()) {
3623 case DeclarationName::Identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -07003624 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003625 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003626 case DeclarationName::ObjCZeroArgSelector:
3627 case DeclarationName::ObjCOneArgSelector:
3628 case DeclarationName::ObjCMultiArgSelector:
Stephen Hines651f13c2014-04-23 16:59:28 -07003629 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003630 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003631 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003632 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3633 "Invalid operator?");
Stephen Hines651f13c2014-04-23 16:59:28 -07003634 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003635 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003636 case DeclarationName::CXXLiteralOperatorName:
Stephen Hines651f13c2014-04-23 16:59:28 -07003637 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003638 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003639 case DeclarationName::CXXConstructorName:
3640 case DeclarationName::CXXDestructorName:
3641 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003642 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003643 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003644 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003645
3646 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003647 }
3648
Chris Lattner5f9e2722011-07-23 10:55:15 +00003649 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003650 data_type Lookup, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003651 using namespace llvm::support;
3652 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003653 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07003654 LE.write<uint16_t>(Lookup.size());
David Blaikie3bc93e32012-12-19 00:45:41 +00003655 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3656 I != E; ++I)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003657 LE.write<uint32_t>(
3658 Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), *I)));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003659
3660 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3661 }
3662};
3663} // end anonymous namespace
3664
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003665template<typename Visitor>
3666static void visitLocalLookupResults(const DeclContext *ConstDC,
3667 bool NeedToReconcileExternalVisibleStorage,
3668 Visitor AddLookupResult) {
3669 // FIXME: We need to build the lookups table, which is logically const.
3670 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Stephen Hines651f13c2014-04-23 16:59:28 -07003671 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3672
Stephen Hines651f13c2014-04-23 16:59:28 -07003673 SmallVector<DeclarationName, 16> ExternalNames;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003674 for (auto &Lookup : *DC->buildLookup()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003675 if (Lookup.second.hasExternalDecls() ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003676 NeedToReconcileExternalVisibleStorage) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003677 // We don't know for sure what declarations are found by this name,
3678 // because the external source might have a different set from the set
3679 // that are in the lookup map, and we can't update it now without
3680 // risking invalidating our lookup iterator. So add it to a queue to
3681 // deal with later.
3682 ExternalNames.push_back(Lookup.first);
3683 continue;
3684 }
3685
3686 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3687 }
3688
3689 // Add the names we needed to defer. Note, this shouldn't add any new decls
3690 // to the list we need to serialize: any new declarations we find here should
3691 // be imported from an external source.
3692 // FIXME: What if the external source isn't an ASTReader?
3693 for (const auto &Name : ExternalNames)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003694 AddLookupResult(Name, DC->lookup(Name));
3695}
3696
3697void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
Stephen Hines176edba2014-12-01 14:53:08 -08003698 if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003699 // Ensure we emit all the visible declarations.
3700 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3701 [&](DeclarationName Name,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003702 DeclContext::lookup_result Result) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003703 for (auto *Decl : Result)
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003704 GetDeclRef(getDeclForLocalLookup(getLangOpts(), Decl));
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003705 });
3706 }
3707}
3708
3709uint32_t
3710ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3711 llvm::SmallVectorImpl<char> &LookupTable) {
3712 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3713
3714 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3715 Generator;
3716 ASTDeclContextNameLookupTrait Trait(*this);
3717
3718 // Create the on-disk hash table representation.
3719 DeclarationName ConstructorName;
3720 DeclarationName ConversionName;
3721 SmallVector<NamedDecl *, 8> ConstructorDecls;
3722 SmallVector<NamedDecl *, 4> ConversionDecls;
3723
3724 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3725 [&](DeclarationName Name,
3726 DeclContext::lookup_result Result) {
3727 if (Result.empty())
3728 return;
3729
3730 // Different DeclarationName values of certain kinds are mapped to
3731 // identical serialized keys, because we don't want to use type
3732 // identifiers in the keys (since type ids are local to the module).
3733 switch (Name.getNameKind()) {
3734 case DeclarationName::CXXConstructorName:
3735 // There may be different CXXConstructorName DeclarationName values
3736 // in a DeclContext because a UsingDecl that inherits constructors
3737 // has the DeclarationName of the inherited constructors.
3738 if (!ConstructorName)
3739 ConstructorName = Name;
3740 ConstructorDecls.append(Result.begin(), Result.end());
3741 return;
3742
3743 case DeclarationName::CXXConversionFunctionName:
3744 if (!ConversionName)
3745 ConversionName = Name;
3746 ConversionDecls.append(Result.begin(), Result.end());
3747 return;
3748
3749 default:
3750 break;
3751 }
3752
3753 Generator.insert(Name, Result, Trait);
3754 });
Stephen Hines651f13c2014-04-23 16:59:28 -07003755
3756 // Add the constructors.
3757 if (!ConstructorDecls.empty()) {
3758 Generator.insert(ConstructorName,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003759 DeclContext::lookup_result(ConstructorDecls),
Stephen Hines651f13c2014-04-23 16:59:28 -07003760 Trait);
3761 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003762
Stephen Hines651f13c2014-04-23 16:59:28 -07003763 // Add the conversion functions.
3764 if (!ConversionDecls.empty()) {
3765 Generator.insert(ConversionName,
Stephen Hines0e2c34f2015-03-23 12:09:02 -07003766 DeclContext::lookup_result(ConversionDecls),
Stephen Hines651f13c2014-04-23 16:59:28 -07003767 Trait);
3768 }
3769
3770 // Create the on-disk hash table in a buffer.
3771 llvm::raw_svector_ostream Out(LookupTable);
3772 // Make sure that no bucket is at offset 0
3773 using namespace llvm::support;
3774 endian::Writer<little>(Out).write<uint32_t>(0);
3775 return Generator.Emit(Out, Trait);
3776}
3777
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003778/// \brief Write the block containing all of the declaration IDs
3779/// visible from the given DeclContext.
3780///
3781/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003782/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003783uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3784 DeclContext *DC) {
3785 if (DC->getPrimaryContext() != DC)
3786 return 0;
3787
3788 // Since there is no name lookup into functions or methods, don't bother to
3789 // build a visible-declarations table for these entities.
3790 if (DC->isFunctionOrMethod())
3791 return 0;
3792
3793 // If not in C++, we perform name lookup for the translation unit via the
3794 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003795 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003796 return 0;
3797
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003798 // Serialize the contents of the mapping used for lookup. Note that,
3799 // although we have two very different code paths, the serialized
3800 // representation is the same for both cases: a declaration name,
3801 // followed by a size, followed by references to the visible
3802 // declarations that have that name.
3803 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003804 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003805 if (!Map || Map->empty())
3806 return 0;
3807
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003808 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003809 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003810 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003811
3812 // Write the lookup table
3813 RecordData Record;
3814 Record.push_back(DECL_CONTEXT_VISIBLE);
3815 Record.push_back(BucketOffset);
3816 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3817 LookupTable.str());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003818 ++NumVisibleDeclContexts;
3819 return Offset;
3820}
3821
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003822/// \brief Write an UPDATE_VISIBLE block for the given context.
3823///
3824/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3825/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003826/// (in C++), for namespaces, and for classes with forward-declared unscoped
3827/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003828void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003829 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003830 if (!Map || Map->empty())
3831 return;
3832
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003833 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003834 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003835 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003836
3837 // Write the lookup table
3838 RecordData Record;
3839 Record.push_back(UPDATE_VISIBLE);
3840 Record.push_back(getDeclID(cast<Decl>(DC)));
3841 Record.push_back(BucketOffset);
3842 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3843}
3844
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003845/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3846void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3847 RecordData Record;
3848 Record.push_back(Opts.fp_contract);
3849 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3850}
3851
3852/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3853void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003854 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003855 return;
3856
3857 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3858 RecordData Record;
3859#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3860#include "clang/Basic/OpenCLExtensions.def"
3861 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3862}
3863
Douglas Gregor2171bf12012-01-15 16:58:34 +00003864void ASTWriter::WriteRedeclarations() {
3865 RecordData LocalRedeclChains;
3866 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3867
3868 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3869 Decl *First = Redeclarations[I];
Rafael Espindola7693b322013-10-19 02:13:21 +00003870 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor2171bf12012-01-15 16:58:34 +00003871
3872 Decl *MostRecent = First->getMostRecentDecl();
3873
3874 // If we only have a single declaration, there is no point in storing
3875 // a redeclaration chain.
3876 if (First == MostRecent)
3877 continue;
3878
3879 unsigned Offset = LocalRedeclChains.size();
3880 unsigned Size = 0;
3881 LocalRedeclChains.push_back(0); // Placeholder for the size.
3882
3883 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003884 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003885 Prev = Prev->getPreviousDecl()) {
3886 if (!Prev->isFromASTFile()) {
3887 AddDeclRef(Prev, LocalRedeclChains);
3888 ++Size;
3889 }
3890 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003891
3892 if (!First->isFromASTFile() && Chain) {
3893 Decl *FirstFromAST = MostRecent;
3894 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3895 if (Prev->isFromASTFile())
3896 FirstFromAST = Prev;
3897 }
3898
Stephen Hines176edba2014-12-01 14:53:08 -08003899 // FIXME: Do we need to do this for the first declaration from each
3900 // redeclaration chain that was merged into this one?
Douglas Gregoraa945902013-02-18 15:53:43 +00003901 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3902 }
3903
Douglas Gregor2171bf12012-01-15 16:58:34 +00003904 LocalRedeclChains[Offset] = Size;
3905
3906 // Reverse the set of local redeclarations, so that we store them in
3907 // order (since we found them in reverse order).
3908 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3909
Douglas Gregoraa945902013-02-18 15:53:43 +00003910 // Add the mapping from the first ID from the AST to the set of local
3911 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003912 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3913 LocalRedeclsMap.push_back(Info);
3914
3915 assert(N == Redeclarations.size() &&
3916 "Deserialized a declaration we shouldn't have");
3917 }
3918
3919 if (LocalRedeclChains.empty())
3920 return;
3921
3922 // Sort the local redeclarations map by the first declaration ID,
3923 // since the reader will be performing binary searches on this information.
3924 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3925
3926 // Emit the local redeclarations map.
3927 using namespace llvm;
3928 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3929 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3931 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3932 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3933
3934 RecordData Record;
3935 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3936 Record.push_back(LocalRedeclsMap.size());
3937 Stream.EmitRecordWithBlob(AbbrevID, Record,
3938 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3939 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3940
3941 // Emit the redeclaration chains.
3942 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3943}
3944
Douglas Gregorcff9f262012-01-27 01:47:08 +00003945void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003946 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003947 RecordData Categories;
3948
3949 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3950 unsigned Size = 0;
3951 unsigned StartIndex = Categories.size();
3952
3953 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3954
3955 // Allocate space for the size.
3956 Categories.push_back(0);
3957
3958 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003959 for (ObjCInterfaceDecl::known_categories_iterator
3960 Cat = Class->known_categories_begin(),
3961 CatEnd = Class->known_categories_end();
3962 Cat != CatEnd; ++Cat, ++Size) {
3963 assert(getDeclID(*Cat) != 0 && "Bogus category");
3964 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003965 }
3966
3967 // Update the size.
3968 Categories[StartIndex] = Size;
3969
3970 // Record this interface -> category map.
3971 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3972 CategoriesMap.push_back(CatInfo);
3973 }
3974
3975 // Sort the categories map by the definition ID, since the reader will be
3976 // performing binary searches on this information.
3977 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3978
3979 // Emit the categories map.
3980 using namespace llvm;
3981 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3982 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3984 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3985 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3986
3987 RecordData Record;
3988 Record.push_back(OBJC_CATEGORIES_MAP);
3989 Record.push_back(CategoriesMap.size());
3990 Stream.EmitRecordWithBlob(AbbrevID, Record,
3991 reinterpret_cast<char*>(CategoriesMap.data()),
3992 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3993
3994 // Emit the category lists.
3995 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3996}
3997
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003998void ASTWriter::WriteMergedDecls() {
3999 if (!Chain || Chain->MergedDecls.empty())
4000 return;
4001
4002 RecordData Record;
4003 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
4004 IEnd = Chain->MergedDecls.end();
4005 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00004006 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004007 : GetDeclRef(I->first);
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00004008 assert(CanonID && "Merged declaration not known?");
4009
4010 Record.push_back(CanonID);
4011 Record.push_back(I->second.size());
4012 Record.append(I->second.begin(), I->second.end());
4013 }
4014 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
4015}
4016
Richard Smithac32d902013-08-07 21:41:30 +00004017void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4018 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4019
4020 if (LPTMap.empty())
4021 return;
4022
4023 RecordData Record;
4024 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4025 ItEnd = LPTMap.end();
4026 It != ItEnd; ++It) {
4027 LateParsedTemplate *LPT = It->second;
4028 AddDeclRef(It->first, Record);
4029 AddDeclRef(LPT->D, Record);
4030 Record.push_back(LPT->Toks.size());
4031
4032 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4033 TokEnd = LPT->Toks.end();
4034 TokIt != TokEnd; ++TokIt) {
4035 AddToken(*TokIt, Record);
4036 }
4037 }
4038 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4039}
4040
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004041/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4042void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4043 RecordData Record;
4044 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4045 AddSourceLocation(PragmaLoc, Record);
4046 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4047}
4048
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004049//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004050// General Serialization Routines
4051//===----------------------------------------------------------------------===//
4052
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004053/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00004054void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4055 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00004056 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00004057 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4058 e = Attrs.end(); i != e; ++i){
4059 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00004060 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004061 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004062
Sean Huntcf807c42010-08-18 23:23:40 +00004063#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00004064
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004065 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004066}
4067
John McCallaeeacf72013-05-03 00:10:13 +00004068void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4069 AddSourceLocation(Tok.getLocation(), Record);
4070 Record.push_back(Tok.getLength());
4071
4072 // FIXME: When reading literal tokens, reconstruct the literal pointer
4073 // if it is needed.
4074 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4075 // FIXME: Should translate token kind to a stable encoding.
4076 Record.push_back(Tok.getKind());
4077 // FIXME: Should translate token flags to a stable encoding.
4078 Record.push_back(Tok.getFlags());
4079}
4080
Chris Lattner5f9e2722011-07-23 10:55:15 +00004081void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004082 Record.push_back(Str.size());
4083 Record.insert(Record.end(), Str.begin(), Str.end());
4084}
4085
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004086bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
4087 assert(Context && "should have context when outputting path");
4088
4089 bool Changed =
4090 cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
4091
4092 // Remove a prefix to make the path relative, if relevant.
4093 const char *PathBegin = Path.data();
4094 const char *PathPtr =
4095 adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
4096 if (PathPtr != PathBegin) {
4097 Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
4098 Changed = true;
4099 }
4100
4101 return Changed;
4102}
4103
4104void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
4105 SmallString<128> FilePath(Path);
4106 PreparePathForOutput(FilePath);
4107 AddString(FilePath, Record);
4108}
4109
4110void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
4111 StringRef Path) {
4112 SmallString<128> FilePath(Path);
4113 PreparePathForOutput(FilePath);
4114 Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
4115}
4116
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004117void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4118 RecordDataImpl &Record) {
4119 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00004120 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004121 Record.push_back(*Minor + 1);
4122 else
4123 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00004124 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004125 Record.push_back(*Subminor + 1);
4126 else
4127 Record.push_back(0);
4128}
4129
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004130/// \brief Note that the identifier II occurs at the given offset
4131/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00004132void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004133 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00004134 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004135 // up earlier in the chain and thus don't need an offset.
4136 if (ID >= FirstIdentID)
4137 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004138}
4139
Douglas Gregor83941df2009-04-25 17:48:32 +00004140/// \brief Note that the selector Sel occurs at the given offset
4141/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00004142void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004143 unsigned ID = SelectorIDs[Sel];
4144 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00004145 // Don't record offsets for selectors that are also available in a different
4146 // file.
4147 if (ID < FirstSelectorID)
4148 return;
4149 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00004150}
4151
Sebastian Redla4232eb2010-08-18 23:56:21 +00004152ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Stephen Hines176edba2014-12-01 14:53:08 -08004153 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4154 WritingModule(nullptr), WritingAST(false),
4155 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4156 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4157 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4158 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4159 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4160 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4161 NextSubmoduleID(FirstSubmoduleID),
4162 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4163 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4164 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4165 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4166 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4167 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
4168 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
4169 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
4170 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4171 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4172 ExprImplicitCastAbbrev(0) {}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004173
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004174ASTWriter::~ASTWriter() {
Stephen Hines651f13c2014-04-23 16:59:28 -07004175 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004176}
4177
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004178const LangOptions &ASTWriter::getLangOpts() const {
4179 assert(WritingAST && "can't determine lang opts when not writing AST");
4180 return Context->getLangOpts();
4181}
4182
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00004183void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00004184 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00004185 Module *WritingModule, StringRef isysroot,
4186 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004187 WritingAST = true;
4188
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00004189 ASTHasCompilerErrors = hasErrors;
4190
Douglas Gregor2cf26342009-04-09 22:27:44 +00004191 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00004192 Stream.Emit((unsigned)'C', 8);
4193 Stream.Emit((unsigned)'P', 8);
4194 Stream.Emit((unsigned)'C', 8);
4195 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00004196
Chris Lattnerb145b1e2009-04-26 22:26:21 +00004197 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004198
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004199 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00004200 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00004201 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00004202 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004203 Context = nullptr;
4204 PP = nullptr;
4205 this->WritingModule = nullptr;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004206 this->BaseDirectory.clear();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004207
Douglas Gregor61c5e342011-09-17 00:05:03 +00004208 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004209}
4210
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004211template<typename Vector>
4212static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4213 ASTWriter::RecordData &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004214 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4215 I != E; ++I) {
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004216 Writer.AddDeclRef(*I, Record);
4217 }
4218}
4219
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00004220void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00004221 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00004222 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004223 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004224 using namespace llvm;
4225
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004226 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004227
Douglas Gregorecc2c092011-12-01 22:20:10 +00004228 // Make sure that the AST reader knows to finalize itself.
4229 if (Chain)
4230 Chain->finalizeForWriting();
4231
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004232 ASTContext &Context = SemaRef.Context;
4233 Preprocessor &PP = SemaRef.PP;
4234
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004235 // Set up predefined declaration IDs.
4236 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004237 if (Context.ObjCIdDecl)
4238 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004239 if (Context.ObjCSelDecl)
4240 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00004241 if (Context.ObjCClassDecl)
4242 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004243 if (Context.ObjCProtocolClassDecl)
4244 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00004245 if (Context.Int128Decl)
4246 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4247 if (Context.UInt128Decl)
4248 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00004249 if (Context.ObjCInstanceTypeDecl)
4250 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00004251 if (Context.BuiltinVaListDecl)
4252 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4253
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004254 if (!Chain) {
4255 // Make sure that we emit IdentifierInfos (and any attached
4256 // declarations) for builtins. We don't need to do this when we're
4257 // emitting chained PCH files, because all of the builtins will be
4258 // in the original PCH file.
4259 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00004260 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00004261 SmallVector<const char *, 32> BuiltinNames;
Eli Bendersky97a03cf2013-07-11 16:53:04 +00004262 if (!Context.getLangOpts().NoBuiltin) {
4263 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4264 }
Douglas Gregor2deaea32009-04-22 18:49:13 +00004265 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4266 getIdentifierRef(&Table.get(BuiltinNames[I]));
4267 }
4268
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004269 // If we saw any DeclContext updates before we started writing the AST file,
4270 // make sure all visible decls in those DeclContexts are written out.
4271 if (!UpdatedDeclContexts.empty()) {
4272 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4273 UpdatedDeclContexts.clear();
4274 for (auto *DC : OldUpdatedDeclContexts)
4275 AddUpdatedDeclContext(DC);
4276 }
4277
Chris Lattner63d65f82009-09-08 18:19:27 +00004278 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00004279 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00004280 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004281 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004282 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00004283
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004284 // Build a record containing all of the file scoped decls in this file.
4285 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00004286 if (!isModule)
4287 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4288 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00004289
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004290 // Build a record containing all of the delegating constructors we still need
4291 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00004292 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004293 if (!isModule)
4294 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004295
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004296 // Write the set of weak, undeclared identifiers. We always write the
4297 // entire table, since later PCH files in a PCH chain are only interested in
4298 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004299 RecordData WeakUndeclaredIdentifiers;
4300 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00004301 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004302 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4303 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4304 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4305 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4306 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4307 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4308 }
4309 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004310
Richard Smith5ea6ef42013-01-10 23:43:47 +00004311 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00004312 // declarations in this header file. Generally, this record will be
4313 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00004314 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00004315 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00004316 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00004317 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00004318 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4319 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00004320 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00004321 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00004322 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00004323 }
4324
Douglas Gregorb81c1702009-04-27 20:06:05 +00004325 // Build a record containing all of the ext_vector declarations.
4326 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00004327 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004328
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004329 // Build a record containing all of the VTable uses information.
4330 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004331 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004332 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4333 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4334 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4335 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4336 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004337 }
4338
Stephen Hines176edba2014-12-01 14:53:08 -08004339 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4340 RecordData UnusedLocalTypedefNameCandidates;
4341 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4342 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4343
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004344 // Build a record containing all of dynamic classes declarations.
4345 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00004346 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004347
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004348 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004349 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004350 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00004351 I = SemaRef.PendingInstantiations.begin(),
4352 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4353 AddDeclRef(I->first, PendingInstantiations);
4354 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004355 }
4356 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4357 "There are local ones at end of translation unit!");
4358
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004359 // Build a record containing some declaration references.
4360 RecordData SemaDeclRefs;
4361 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4362 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4363 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4364 }
4365
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004366 RecordData CUDASpecialDeclRefs;
4367 if (Context.getcudaConfigureCallDecl()) {
4368 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4369 }
4370
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004371 // Build a record containing all of the known namespaces.
4372 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00004373 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004374 I = SemaRef.KnownNamespaces.begin(),
4375 IEnd = SemaRef.KnownNamespaces.end();
4376 I != IEnd; ++I) {
4377 if (!I->second)
4378 AddDeclRef(I->first, KnownNamespaces);
4379 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004380
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004381 // Build a record of all used, undefined objects that require definitions.
4382 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00004383
4384 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004385 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00004386 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4387 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004388 AddDeclRef(I->first, UndefinedButUsed);
4389 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00004390 }
4391
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004392 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00004393 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004394
Sebastian Redl3397c552010-08-18 23:56:27 +00004395 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00004396 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004397 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004398
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00004399 // This is so that older clang versions, before the introduction
4400 // of the control block, can read and reject the newer PCH format.
4401 Record.clear();
4402 Record.push_back(VERSION_MAJOR);
4403 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4404
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004405 // Create a lexical update block containing all of the declarations in the
4406 // translation unit that do not come from other AST files.
4407 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4408 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Stephen Hines651f13c2014-04-23 16:59:28 -07004409 for (const auto *I : TU->noload_decls()) {
4410 if (!I->isFromASTFile())
4411 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004412 }
4413
4414 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4415 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4416 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4417 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4418 Record.clear();
4419 Record.push_back(TU_UPDATE_LEXICAL);
4420 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4421 data(NewGlobalDecls));
4422
4423 // And a visible updates block for the translation unit.
4424 Abv = new llvm::BitCodeAbbrev();
4425 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4426 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4427 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4428 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4429 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4430 WriteDeclContextVisibleUpdate(TU);
4431
4432 // If the translation unit has an anonymous namespace, and we don't already
4433 // have an update block for it, write it as an update block.
Stephen Hines651f13c2014-04-23 16:59:28 -07004434 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004435 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4436 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Stephen Hines651f13c2014-04-23 16:59:28 -07004437 if (Record.empty())
4438 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004439 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004440
Stephen Hines651f13c2014-04-23 16:59:28 -07004441 // Add update records for all mangling numbers and static local numbers.
4442 // These aren't really update records, but this is a convenient way of
4443 // tagging this rare extra data onto the declarations.
4444 for (const auto &Number : Context.MangleNumbers)
4445 if (!Number.first->isFromASTFile())
4446 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4447 Number.second));
4448 for (const auto &Number : Context.StaticLocalNumbers)
4449 if (!Number.first->isFromASTFile())
4450 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4451 Number.second));
4452
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004453 // Make sure visible decls, added to DeclContexts previously loaded from
4454 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004455 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004456 I = UpdatingVisibleDecls.begin(),
4457 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4458 GetDeclRef(*I);
4459 }
4460
Argyrios Kyrtzidis51e75ae2013-08-07 21:17:33 +00004461 // Make sure all decls associated with an identifier are registered for
4462 // serialization.
4463 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4464 IDEnd = PP.getIdentifierTable().end();
4465 ID != IDEnd; ++ID) {
4466 const IdentifierInfo *II = ID->second;
4467 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4468 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4469 DEnd = SemaRef.IdResolver.end();
4470 D != DEnd; ++D) {
4471 GetDeclRef(*D);
4472 }
4473 }
4474 }
4475
Douglas Gregora119da02011-08-02 16:26:37 +00004476 // Form the record of special types.
4477 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004478 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004479 AddTypeRef(Context.getFILEType(), SpecialTypes);
4480 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4481 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4482 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4483 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004484 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004485 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004486
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004487 if (Chain) {
4488 // Write the mapping information describing our module dependencies and how
4489 // each of those modules were mapped into our own offset/ID space, so that
4490 // the reader can build the appropriate mapping to its own offset/ID space.
4491 // The map consists solely of a blob with the following format:
4492 // *(module-name-len:i16 module-name:len*i8
4493 // source-location-offset:i32
4494 // identifier-id:i32
4495 // preprocessed-entity-id:i32
4496 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004497 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004498 // selector-id:i32
4499 // declaration-id:i32
4500 // c++-base-specifiers-id:i32
4501 // type-id:i32)
4502 //
4503 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4504 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4505 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4506 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004507 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004508 {
4509 llvm::raw_svector_ostream Out(Buffer);
Stephen Hines176edba2014-12-01 14:53:08 -08004510 for (ModuleFile *M : Chain->ModuleMgr) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004511 using namespace llvm::support;
4512 endian::Writer<little> LE(Out);
Stephen Hines176edba2014-12-01 14:53:08 -08004513 StringRef FileName = M->FileName;
Stephen Hines651f13c2014-04-23 16:59:28 -07004514 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004515 Out.write(FileName.data(), FileName.size());
Stephen Hines176edba2014-12-01 14:53:08 -08004516
4517 // Note: if a base ID was uint max, it would not be possible to load
4518 // another module after it or have more than one entity inside it.
4519 uint32_t None = std::numeric_limits<uint32_t>::max();
4520
4521 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4522 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4523 if (ShouldWrite)
4524 LE.write<uint32_t>(BaseID);
4525 else
4526 LE.write<uint32_t>(None);
4527 };
4528
4529 // These values should be unique within a chain, since they will be read
4530 // as keys into ContinuousRangeMaps.
4531 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4532 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4533 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4534 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4535 M->NumPreprocessedEntities);
4536 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4537 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4538 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4539 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004540 }
4541 }
4542 Record.clear();
4543 Record.push_back(MODULE_OFFSET_MAP);
4544 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4545 Buffer.data(), Buffer.size());
4546 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004547
4548 RecordData DeclUpdatesOffsetsRecord;
4549
4550 // Keep writing types, declarations, and declaration update records
4551 // until we've emitted all of them.
Stephen Hines176edba2014-12-01 14:53:08 -08004552 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4553 WriteTypeAbbrevs();
4554 WriteDeclAbbrevs();
Stephen Hines651f13c2014-04-23 16:59:28 -07004555 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4556 E = DeclsToRewrite.end();
4557 I != E; ++I)
4558 DeclTypesToEmit.push(const_cast<Decl*>(*I));
4559 do {
4560 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4561 while (!DeclTypesToEmit.empty()) {
4562 DeclOrType DOT = DeclTypesToEmit.front();
4563 DeclTypesToEmit.pop();
4564 if (DOT.isType())
4565 WriteType(DOT.getType());
4566 else
4567 WriteDecl(Context, DOT.getDecl());
4568 }
4569 } while (!DeclUpdates.empty());
4570 Stream.ExitBlock();
4571
4572 DoneWritingDeclsAndTypes = true;
4573
4574 // These things can only be done once we've written out decls and types.
4575 WriteTypeDeclOffsets();
4576 if (!DeclUpdatesOffsetsRecord.empty())
4577 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4578 WriteCXXBaseSpecifiersOffsets();
4579 WriteFileDeclIDsMap();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004580 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Stephen Hines651f13c2014-04-23 16:59:28 -07004581
4582 WriteComments();
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004583 WritePreprocessor(PP, isModule);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07004584 WriteHeaderSearch(PP.getHeaderSearchInfo());
Sebastian Redl059612d2010-08-03 21:58:15 +00004585 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004586 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004587 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004588 WriteFPPragmaOptions(SemaRef.getFPOptions());
4589 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004590 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004591
Douglas Gregore209e502011-12-06 01:10:29 +00004592 // If we're emitting a module, write out the submodule information.
4593 if (WritingModule)
4594 WriteSubmodules(WritingModule);
4595
Douglas Gregora119da02011-08-02 16:26:37 +00004596 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4597
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004598 // Write the record containing external, unnamed definitions.
Stephen Hines651f13c2014-04-23 16:59:28 -07004599 if (!EagerlyDeserializedDecls.empty())
4600 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004601
4602 // Write the record containing tentative definitions.
4603 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004604 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004605
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004606 // Write the record containing unused file scoped decls.
4607 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004608 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004609
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004610 // Write the record containing weak undeclared identifiers.
4611 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004612 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004613 WeakUndeclaredIdentifiers);
4614
Richard Smith5ea6ef42013-01-10 23:43:47 +00004615 // Write the record containing locally-scoped extern "C" definitions.
4616 if (!LocallyScopedExternCDecls.empty())
4617 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4618 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004619
4620 // Write the record containing ext_vector type names.
4621 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004622 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004623
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004624 // Write the record containing VTable uses information.
4625 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004626 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004627
4628 // Write the record containing dynamic classes declarations.
4629 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004630 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004631
Stephen Hines176edba2014-12-01 14:53:08 -08004632 // Write the record containing potentially unused local typedefs.
4633 if (!UnusedLocalTypedefNameCandidates.empty())
4634 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4635 UnusedLocalTypedefNameCandidates);
4636
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004637 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004638 if (!PendingInstantiations.empty())
4639 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004640
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004641 // Write the record containing declaration references of Sema.
4642 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004643 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004644
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004645 // Write the record containing CUDA-specific declaration references.
4646 if (!CUDASpecialDeclRefs.empty())
4647 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004648
4649 // Write the delegating constructors.
4650 if (!DelegatingCtorDecls.empty())
4651 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004652
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004653 // Write the known namespaces.
4654 if (!KnownNamespaces.empty())
4655 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004656
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004657 // Write the undefined internal functions and variables, and inline functions.
4658 if (!UndefinedButUsed.empty())
4659 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004660
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004661 // Write the visible updates to DeclContexts.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004662 for (auto *DC : UpdatedDeclContexts)
4663 WriteDeclContextVisibleUpdate(DC);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004664
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004665 if (!WritingModule) {
4666 // Write the submodules that were imported, if any.
Stephen Hines651f13c2014-04-23 16:59:28 -07004667 struct ModuleInfo {
4668 uint64_t ID;
4669 Module *M;
4670 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4671 };
4672 llvm::SmallVector<ModuleInfo, 64> Imports;
4673 for (const auto *I : Context.local_imports()) {
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004674 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Stephen Hines651f13c2014-04-23 16:59:28 -07004675 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4676 I->getImportedModule()));
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004677 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004678
4679 if (!Imports.empty()) {
4680 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4681 return A.ID < B.ID;
4682 };
Stephen Hines176edba2014-12-01 14:53:08 -08004683 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4684 return A.ID == B.ID;
4685 };
Stephen Hines651f13c2014-04-23 16:59:28 -07004686
4687 // Sort and deduplicate module IDs.
4688 std::sort(Imports.begin(), Imports.end(), Cmp);
Stephen Hines176edba2014-12-01 14:53:08 -08004689 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Stephen Hines651f13c2014-04-23 16:59:28 -07004690 Imports.end());
4691
4692 RecordData ImportedModules;
4693 for (const auto &Import : Imports) {
4694 ImportedModules.push_back(Import.ID);
4695 // FIXME: If the module has macros imported then later has declarations
4696 // imported, this location won't be the right one as a location for the
4697 // declaration imports.
4698 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4699 }
4700
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004701 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4702 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004703 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004704
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004705 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004706 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004707 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004708 WriteObjCCategories();
Richard Smithac32d902013-08-07 21:41:30 +00004709 WriteLateParsedTemplates(SemaRef);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004710 if(!WritingModule)
4711 WriteOptimizePragmaOptions(SemaRef);
Richard Smithac32d902013-08-07 21:41:30 +00004712
Douglas Gregor3e1af842009-04-17 22:13:46 +00004713 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004714 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004715 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004716 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004717 Record.push_back(NumLexicalDeclContexts);
4718 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004719 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004720 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004721}
4722
Stephen Hines651f13c2014-04-23 16:59:28 -07004723void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004724 if (DeclUpdates.empty())
4725 return;
4726
Stephen Hines651f13c2014-04-23 16:59:28 -07004727 DeclUpdateMap LocalUpdates;
4728 LocalUpdates.swap(DeclUpdates);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004729
Stephen Hines651f13c2014-04-23 16:59:28 -07004730 for (auto &DeclUpdate : LocalUpdates) {
4731 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004732 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004733 continue; // The decl will be written completely,no need to store updates.
4734
Stephen Hines651f13c2014-04-23 16:59:28 -07004735 bool HasUpdatedBody = false;
4736 RecordData Record;
4737 for (auto &Update : DeclUpdate.second) {
4738 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4739
4740 Record.push_back(Kind);
4741 switch (Kind) {
4742 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4743 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4744 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004745 assert(Update.getDecl() && "no decl to add?");
Stephen Hines651f13c2014-04-23 16:59:28 -07004746 Record.push_back(GetDeclRef(Update.getDecl()));
4747 break;
4748
Stephen Hines176edba2014-12-01 14:53:08 -08004749 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Stephen Hines651f13c2014-04-23 16:59:28 -07004750 // An updated body is emitted last, so that the reader doesn't need
4751 // to skip over the lazy body to reach statements for other records.
4752 Record.pop_back();
4753 HasUpdatedBody = true;
4754 break;
4755
Stephen Hines176edba2014-12-01 14:53:08 -08004756 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4757 AddSourceLocation(Update.getLoc(), Record);
4758 break;
4759
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004760 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4761 auto *RD = cast<CXXRecordDecl>(D);
4762 AddUpdatedDeclContext(RD->getPrimaryContext());
4763 AddCXXDefinitionData(RD, Record);
4764 Record.push_back(WriteDeclContextLexicalBlock(
4765 *Context, const_cast<CXXRecordDecl *>(RD)));
4766
4767 // This state is sometimes updated by template instantiation, when we
4768 // switch from the specialization referring to the template declaration
4769 // to it referring to the template definition.
4770 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4771 Record.push_back(MSInfo->getTemplateSpecializationKind());
4772 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4773 } else {
4774 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4775 Record.push_back(Spec->getTemplateSpecializationKind());
4776 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
4777
4778 // The instantiation might have been resolved to a partial
4779 // specialization. If so, record which one.
4780 auto From = Spec->getInstantiatedFrom();
4781 if (auto PartialSpec =
4782 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4783 Record.push_back(true);
4784 AddDeclRef(PartialSpec, Record);
4785 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4786 Record);
4787 } else {
4788 Record.push_back(false);
4789 }
4790 }
4791 Record.push_back(RD->getTagKind());
4792 AddSourceLocation(RD->getLocation(), Record);
4793 AddSourceLocation(RD->getLocStart(), Record);
4794 AddSourceLocation(RD->getRBraceLoc(), Record);
4795
4796 // Instantiation may change attributes; write them all out afresh.
4797 Record.push_back(D->hasAttrs());
4798 if (Record.back())
Stephen Hines176edba2014-12-01 14:53:08 -08004799 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4800 D->getAttrs().size()), Record);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004801
4802 // FIXME: Ensure we don't get here for explicit instantiations.
4803 break;
4804 }
4805
Stephen Hines651f13c2014-04-23 16:59:28 -07004806 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4807 addExceptionSpec(
4808 *this,
4809 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4810 Record);
4811 break;
4812
4813 case UPD_CXX_DEDUCED_RETURN_TYPE:
4814 Record.push_back(GetOrCreateTypeID(Update.getType()));
4815 break;
4816
4817 case UPD_DECL_MARKED_USED:
4818 break;
4819
4820 case UPD_MANGLING_NUMBER:
4821 case UPD_STATIC_LOCAL_NUMBER:
4822 Record.push_back(Update.getNumber());
4823 break;
Stephen Hines176edba2014-12-01 14:53:08 -08004824 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4825 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4826 Record);
4827 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07004828 }
4829 }
4830
4831 if (HasUpdatedBody) {
4832 const FunctionDecl *Def = cast<FunctionDecl>(D);
Stephen Hines176edba2014-12-01 14:53:08 -08004833 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Stephen Hines651f13c2014-04-23 16:59:28 -07004834 Record.push_back(Def->isInlined());
4835 AddSourceLocation(Def->getInnerLocStart(), Record);
4836 AddFunctionDefinition(Def, Record);
Stephen Hines176edba2014-12-01 14:53:08 -08004837 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4838 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Stephen Hines651f13c2014-04-23 16:59:28 -07004839 }
4840
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004841 OffsetsRecord.push_back(GetDeclRef(D));
4842 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4843
Stephen Hines651f13c2014-04-23 16:59:28 -07004844 Stream.EmitRecord(DECL_UPDATES, Record);
4845
4846 // Flush any statements that were written as part of this update record.
4847 FlushStmts();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004848
4849 // Flush C++ base specifiers, if there are any.
4850 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004851 }
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004852}
4853
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004854void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004855 if (ReplacedDecls.empty())
4856 return;
4857
4858 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004859 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4860 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004861 Record.push_back(I->ID);
4862 Record.push_back(I->Offset);
4863 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004864 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004865 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004866}
4867
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004868void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004869 Record.push_back(Loc.getRawEncoding());
4870}
4871
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004872void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004873 AddSourceLocation(Range.getBegin(), Record);
4874 AddSourceLocation(Range.getEnd(), Record);
4875}
4876
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004877void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004878 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004879 const uint64_t *Words = Value.getRawData();
4880 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004881}
4882
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004883void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004884 Record.push_back(Value.isUnsigned());
4885 AddAPInt(Value, Record);
4886}
4887
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004888void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004889 AddAPInt(Value.bitcastToAPInt(), Record);
4890}
4891
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004892void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004893 Record.push_back(getIdentifierRef(II));
4894}
4895
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004896IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004897 if (!II)
Douglas Gregor2deaea32009-04-22 18:49:13 +00004898 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004899
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004900 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004901 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004902 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004903 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004904}
4905
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004906MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004907 // Don't emit builtin macros like __LINE__ to the AST file unless they
4908 // have been redefined by the header (in which case they are not
4909 // isBuiltinMacro).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004910 if (!MI || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004911 return 0;
4912
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004913 MacroID &ID = MacroIDs[MI];
4914 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004915 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004916 MacroInfoToEmitData Info = { Name, MI, ID };
4917 MacroInfosToEmit.push_back(Info);
4918 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004919 return ID;
4920}
4921
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004922MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004923 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004924 return 0;
4925
4926 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4927 return MacroIDs[MI];
4928}
4929
4930uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4931 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4932 return IdentMacroDirectivesOffsetMap[Name];
4933}
4934
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004935void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004936 Record.push_back(getSelectorRef(SelRef));
4937}
4938
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004939SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004940 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004941 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004942 }
4943
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004944 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004945 if (SID == 0 && Chain) {
4946 // This might trigger a ReadSelector callback, which will set the ID for
4947 // this selector.
4948 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004949 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004950 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004951 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004952 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004953 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004954 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004955 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004956}
4957
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004958void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004959 AddDeclRef(Temp->getDestructor(), Record);
4960}
4961
Douglas Gregor7c789c12010-10-29 22:39:52 +00004962void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4963 CXXBaseSpecifier const *BasesEnd,
4964 RecordDataImpl &Record) {
4965 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4966 CXXBaseSpecifiersToWrite.push_back(
4967 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4968 Bases, BasesEnd));
4969 Record.push_back(NextCXXBaseSpecifiersID++);
4970}
4971
Sebastian Redla4232eb2010-08-18 23:56:21 +00004972void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004973 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004974 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004975 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004976 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004977 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004978 break;
4979 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004980 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004981 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004982 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004983 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004984 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004985 break;
4986 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004987 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004988 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004989 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004990 break;
John McCall833ca992009-10-29 08:12:44 +00004991 case TemplateArgument::Null:
4992 case TemplateArgument::Integral:
4993 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004994 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004995 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004996 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004997 break;
4998 }
4999}
5000
Sebastian Redla4232eb2010-08-18 23:56:21 +00005001void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005002 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00005003 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00005004
5005 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
5006 bool InfoHasSameExpr
5007 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
5008 Record.push_back(InfoHasSameExpr);
5009 if (InfoHasSameExpr)
5010 return; // Avoid storing the same expr twice.
5011 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00005012 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
5013 Record);
5014}
5015
Douglas Gregordc355712011-02-25 00:36:19 +00005016void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
5017 RecordDataImpl &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005018 if (!TInfo) {
John McCalla1ee0c52009-10-16 21:56:05 +00005019 AddTypeRef(QualType(), Record);
5020 return;
5021 }
5022
Douglas Gregordc355712011-02-25 00:36:19 +00005023 AddTypeLoc(TInfo->getTypeLoc(), Record);
5024}
5025
5026void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
5027 AddTypeRef(TL.getType(), Record);
5028
John McCalla1ee0c52009-10-16 21:56:05 +00005029 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00005030 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00005031 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00005032}
5033
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005034void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00005035 Record.push_back(GetOrCreateTypeID(T));
5036}
5037
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005038TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00005039 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005040 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00005041 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
5042}
Douglas Gregor2cf26342009-04-09 22:27:44 +00005043
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00005044TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00005045 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005046 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00005047 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00005048}
5049
5050TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5051 if (T.isNull())
5052 return TypeIdx();
5053 assert(!T.getLocalFastQualifiers());
5054
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00005055 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005056 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005057 if (DoneWritingDeclsAndTypes) {
5058 assert(0 && "New type seen after serializing all the types to emit!");
5059 return TypeIdx();
5060 }
5061
Douglas Gregor366809a2009-04-26 03:49:13 +00005062 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00005063 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005064 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00005065 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00005066 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00005067 return Idx;
5068}
Douglas Gregor2cf26342009-04-09 22:27:44 +00005069
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00005070TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00005071 if (T.isNull())
5072 return TypeIdx();
5073 assert(!T.getLocalFastQualifiers());
5074
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00005075 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5076 assert(I != TypeIdxs.end() && "Type not emitted!");
5077 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005078}
5079
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005080void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00005081 Record.push_back(GetDeclRef(D));
5082}
5083
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005084DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005085 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005086
5087 if (!D) {
Sebastian Redl681d7232010-07-27 00:17:23 +00005088 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005089 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00005090
5091 // If D comes from an AST file, its declaration ID is already known and
5092 // fixed.
5093 if (D->isFromASTFile())
5094 return D->getGlobalID();
5095
Douglas Gregor97475832010-10-05 18:37:06 +00005096 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005097 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00005098 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005099 if (DoneWritingDeclsAndTypes) {
5100 assert(0 && "New decl seen after serializing all the decls to emit!");
5101 return 0;
5102 }
5103
Douglas Gregor2cf26342009-04-09 22:27:44 +00005104 // We haven't seen this declaration before. Give it a new ID and
5105 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005106 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00005107 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005108 }
5109
Sebastian Redl681d7232010-07-27 00:17:23 +00005110 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005111}
5112
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005113DeclID ASTWriter::getDeclID(const Decl *D) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005114 if (!D)
Douglas Gregor3251ceb2009-04-20 20:36:09 +00005115 return 0;
5116
Douglas Gregor1c7946a2012-01-05 22:33:30 +00005117 // If D comes from an AST file, its declaration ID is already known and
5118 // fixed.
5119 if (D->isFromASTFile())
5120 return D->getGlobalID();
5121
Douglas Gregor3251ceb2009-04-20 20:36:09 +00005122 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5123 return DeclIDs[D];
5124}
5125
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00005126void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005127 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00005128 assert(D);
5129
5130 SourceLocation Loc = D->getLocation();
5131 if (Loc.isInvalid())
5132 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005133
5134 // We only keep track of the file-level declarations of each file.
5135 if (!D->getLexicalDeclContext()->isFileContext())
5136 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00005137 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5138 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00005139 if (isa<ParmVarDecl>(D))
5140 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005141
5142 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00005143 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005144 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00005145 FileID FID;
5146 unsigned Offset;
Stephen Hines651f13c2014-04-23 16:59:28 -07005147 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005148 if (FID.isInvalid())
5149 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00005150 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005151
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00005152 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005153 if (!Info)
5154 Info = new DeclIDInFileInfo();
5155
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00005156 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005157 LocDeclIDsTy &Decls = Info->DeclIDs;
5158
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00005159 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005160 Decls.push_back(LocDecl);
5161 return;
5162 }
5163
Benjamin Kramer809d2542013-08-24 13:22:59 +00005164 LocDeclIDsTy::iterator I =
5165 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005166
5167 Decls.insert(I, LocDecl);
5168}
5169
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005170void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00005171 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00005172 Record.push_back(Name.getNameKind());
5173 switch (Name.getNameKind()) {
5174 case DeclarationName::Identifier:
5175 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5176 break;
5177
5178 case DeclarationName::ObjCZeroArgSelector:
5179 case DeclarationName::ObjCOneArgSelector:
5180 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005181 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005182 break;
5183
5184 case DeclarationName::CXXConstructorName:
5185 case DeclarationName::CXXDestructorName:
5186 case DeclarationName::CXXConversionFunctionName:
5187 AddTypeRef(Name.getCXXNameType(), Record);
5188 break;
5189
5190 case DeclarationName::CXXOperatorName:
5191 Record.push_back(Name.getCXXOverloadedOperator());
5192 break;
5193
Sean Hunt3e518bd2009-11-29 07:34:05 +00005194 case DeclarationName::CXXLiteralOperatorName:
5195 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5196 break;
5197
Douglas Gregor2cf26342009-04-09 22:27:44 +00005198 case DeclarationName::CXXUsingDirective:
5199 // No extra data to emit
5200 break;
5201 }
5202}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005203
Stephen Hines176edba2014-12-01 14:53:08 -08005204unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5205 assert(needsAnonymousDeclarationNumber(D) &&
5206 "expected an anonymous declaration");
5207
5208 // Number the anonymous declarations within this context, if we've not
5209 // already done so.
5210 auto It = AnonymousDeclarationNumbers.find(D);
5211 if (It == AnonymousDeclarationNumbers.end()) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07005212 auto *DC = D->getLexicalDeclContext();
5213 numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5214 AnonymousDeclarationNumbers[ND] = Number;
5215 });
Stephen Hines176edba2014-12-01 14:53:08 -08005216
5217 It = AnonymousDeclarationNumbers.find(D);
5218 assert(It != AnonymousDeclarationNumbers.end() &&
5219 "declaration not found within its lexical context");
5220 }
5221
5222 return It->second;
5223}
5224
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005225void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005226 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005227 switch (Name.getNameKind()) {
5228 case DeclarationName::CXXConstructorName:
5229 case DeclarationName::CXXDestructorName:
5230 case DeclarationName::CXXConversionFunctionName:
5231 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5232 break;
5233
5234 case DeclarationName::CXXOperatorName:
5235 AddSourceLocation(
5236 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5237 Record);
5238 AddSourceLocation(
5239 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5240 Record);
5241 break;
5242
5243 case DeclarationName::CXXLiteralOperatorName:
5244 AddSourceLocation(
5245 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5246 Record);
5247 break;
5248
5249 case DeclarationName::Identifier:
5250 case DeclarationName::ObjCZeroArgSelector:
5251 case DeclarationName::ObjCOneArgSelector:
5252 case DeclarationName::ObjCMultiArgSelector:
5253 case DeclarationName::CXXUsingDirective:
5254 break;
5255 }
5256}
5257
5258void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005259 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005260 AddDeclarationName(NameInfo.getName(), Record);
5261 AddSourceLocation(NameInfo.getLoc(), Record);
5262 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5263}
5264
5265void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005266 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005267 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005268 Record.push_back(Info.NumTemplParamLists);
5269 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5270 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5271}
5272
Sebastian Redla4232eb2010-08-18 23:56:21 +00005273void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005274 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005275 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00005276 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005277 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005278
5279 // Push each of the NNS's onto a stack for serialization in reverse order.
5280 while (NNS) {
5281 NestedNames.push_back(NNS);
5282 NNS = NNS->getPrefix();
5283 }
5284
5285 Record.push_back(NestedNames.size());
5286 while(!NestedNames.empty()) {
5287 NNS = NestedNames.pop_back_val();
5288 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5289 Record.push_back(Kind);
5290 switch (Kind) {
5291 case NestedNameSpecifier::Identifier:
5292 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5293 break;
5294
5295 case NestedNameSpecifier::Namespace:
5296 AddDeclRef(NNS->getAsNamespace(), Record);
5297 break;
5298
Douglas Gregor14aba762011-02-24 02:36:08 +00005299 case NestedNameSpecifier::NamespaceAlias:
5300 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5301 break;
5302
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005303 case NestedNameSpecifier::TypeSpec:
5304 case NestedNameSpecifier::TypeSpecWithTemplate:
5305 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5306 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5307 break;
5308
5309 case NestedNameSpecifier::Global:
5310 // Don't need to write an associated value.
5311 break;
Stephen Hines176edba2014-12-01 14:53:08 -08005312
5313 case NestedNameSpecifier::Super:
5314 AddDeclRef(NNS->getAsRecordDecl(), Record);
5315 break;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005316 }
5317 }
5318}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005319
Douglas Gregordc355712011-02-25 00:36:19 +00005320void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5321 RecordDataImpl &Record) {
5322 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00005323 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005324 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00005325
5326 // Push each of the nested-name-specifiers's onto a stack for
5327 // serialization in reverse order.
5328 while (NNS) {
5329 NestedNames.push_back(NNS);
5330 NNS = NNS.getPrefix();
5331 }
5332
5333 Record.push_back(NestedNames.size());
5334 while(!NestedNames.empty()) {
5335 NNS = NestedNames.pop_back_val();
5336 NestedNameSpecifier::SpecifierKind Kind
5337 = NNS.getNestedNameSpecifier()->getKind();
5338 Record.push_back(Kind);
5339 switch (Kind) {
5340 case NestedNameSpecifier::Identifier:
5341 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5342 AddSourceRange(NNS.getLocalSourceRange(), Record);
5343 break;
5344
5345 case NestedNameSpecifier::Namespace:
5346 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5347 AddSourceRange(NNS.getLocalSourceRange(), Record);
5348 break;
5349
5350 case NestedNameSpecifier::NamespaceAlias:
5351 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5352 AddSourceRange(NNS.getLocalSourceRange(), Record);
5353 break;
5354
5355 case NestedNameSpecifier::TypeSpec:
5356 case NestedNameSpecifier::TypeSpecWithTemplate:
5357 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5358 AddTypeLoc(NNS.getTypeLoc(), Record);
5359 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5360 break;
5361
5362 case NestedNameSpecifier::Global:
5363 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5364 break;
Stephen Hines176edba2014-12-01 14:53:08 -08005365
5366 case NestedNameSpecifier::Super:
5367 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5368 AddSourceRange(NNS.getLocalSourceRange(), Record);
5369 break;
Douglas Gregordc355712011-02-25 00:36:19 +00005370 }
5371 }
5372}
5373
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005374void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005375 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005376 Record.push_back(Kind);
5377 switch (Kind) {
5378 case TemplateName::Template:
5379 AddDeclRef(Name.getAsTemplateDecl(), Record);
5380 break;
5381
5382 case TemplateName::OverloadedTemplate: {
5383 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5384 Record.push_back(OvT->size());
5385 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5386 I != E; ++I)
5387 AddDeclRef(*I, Record);
5388 break;
5389 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005390
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005391 case TemplateName::QualifiedTemplate: {
5392 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5393 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5394 Record.push_back(QualT->hasTemplateKeyword());
5395 AddDeclRef(QualT->getTemplateDecl(), Record);
5396 break;
5397 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005398
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005399 case TemplateName::DependentTemplate: {
5400 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5401 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5402 Record.push_back(DepT->isIdentifier());
5403 if (DepT->isIdentifier())
5404 AddIdentifierRef(DepT->getIdentifier(), Record);
5405 else
5406 Record.push_back(DepT->getOperator());
5407 break;
5408 }
John McCall14606042011-06-30 08:33:18 +00005409
5410 case TemplateName::SubstTemplateTemplateParm: {
5411 SubstTemplateTemplateParmStorage *subst
5412 = Name.getAsSubstTemplateTemplateParm();
5413 AddDeclRef(subst->getParameter(), Record);
5414 AddTemplateName(subst->getReplacement(), Record);
5415 break;
5416 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005417
5418 case TemplateName::SubstTemplateTemplateParmPack: {
5419 SubstTemplateTemplateParmPackStorage *SubstPack
5420 = Name.getAsSubstTemplateTemplateParmPack();
5421 AddDeclRef(SubstPack->getParameterPack(), Record);
5422 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5423 break;
5424 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005425 }
5426}
5427
Michael J. Spencer20249a12010-10-21 03:16:25 +00005428void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005429 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005430 Record.push_back(Arg.getKind());
5431 switch (Arg.getKind()) {
5432 case TemplateArgument::Null:
5433 break;
5434 case TemplateArgument::Type:
5435 AddTypeRef(Arg.getAsType(), Record);
5436 break;
5437 case TemplateArgument::Declaration:
5438 AddDeclRef(Arg.getAsDecl(), Record);
Stephen Hines176edba2014-12-01 14:53:08 -08005439 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00005440 break;
5441 case TemplateArgument::NullPtr:
5442 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005443 break;
5444 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00005445 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005446 AddTypeRef(Arg.getIntegralType(), Record);
5447 break;
5448 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00005449 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5450 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00005451 case TemplateArgument::TemplateExpansion:
5452 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00005453 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00005454 Record.push_back(*NumExpansions + 1);
5455 else
5456 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005457 break;
5458 case TemplateArgument::Expression:
5459 AddStmt(Arg.getAsExpr());
5460 break;
5461 case TemplateArgument::Pack:
5462 Record.push_back(Arg.pack_size());
Stephen Hines176edba2014-12-01 14:53:08 -08005463 for (const auto &P : Arg.pack_elements())
5464 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005465 break;
5466 }
5467}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005468
5469void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005470ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005471 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005472 assert(TemplateParams && "No TemplateParams!");
5473 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5474 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5475 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5476 Record.push_back(TemplateParams->size());
5477 for (TemplateParameterList::const_iterator
5478 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5479 P != PEnd; ++P)
5480 AddDeclRef(*P, Record);
5481}
5482
5483/// \brief Emit a template argument list.
5484void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005485ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005486 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005487 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00005488 Record.push_back(TemplateArgs->size());
5489 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005490 AddTemplateArgument(TemplateArgs->get(i), Record);
5491}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005492
Enea Zaffanellac1cef082013-08-10 07:24:53 +00005493void
5494ASTWriter::AddASTTemplateArgumentListInfo
5495(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5496 assert(ASTTemplArgList && "No ASTTemplArgList!");
5497 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5498 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5499 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5500 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5501 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5502 AddTemplateArgumentLoc(TemplArgs[i], Record);
5503}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005504
5505void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005506ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005507 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005508 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005509 I = Set.begin(), E = Set.end(); I != E; ++I) {
5510 AddDeclRef(I.getDecl(), Record);
5511 Record.push_back(I.getAccess());
5512 }
5513}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005514
Sebastian Redla4232eb2010-08-18 23:56:21 +00005515void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005516 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005517 Record.push_back(Base.isVirtual());
5518 Record.push_back(Base.isBaseOfClass());
5519 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00005520 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00005521 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005522 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005523 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5524 : SourceLocation(),
5525 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005526}
Sebastian Redl30c514c2010-07-14 23:45:08 +00005527
Douglas Gregor7c789c12010-10-29 22:39:52 +00005528void ASTWriter::FlushCXXBaseSpecifiers() {
5529 RecordData Record;
5530 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5531 Record.clear();
5532
5533 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00005534 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005535 if (Index == CXXBaseSpecifiersOffsets.size())
5536 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5537 else {
5538 if (Index > CXXBaseSpecifiersOffsets.size())
5539 CXXBaseSpecifiersOffsets.resize(Index + 1);
5540 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5541 }
5542
5543 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5544 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5545 Record.push_back(BEnd - B);
5546 for (; B != BEnd; ++B)
5547 AddCXXBaseSpecifier(*B, Record);
5548 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00005549
5550 // Flush any expressions that were written as part of the base specifiers.
5551 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00005552 }
5553
5554 CXXBaseSpecifiersToWrite.clear();
5555}
5556
Sean Huntcbb67482011-01-08 20:30:50 +00005557void ASTWriter::AddCXXCtorInitializers(
5558 const CXXCtorInitializer * const *CtorInitializers,
5559 unsigned NumCtorInitializers,
5560 RecordDataImpl &Record) {
5561 Record.push_back(NumCtorInitializers);
5562 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5563 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005564
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005565 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00005566 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00005567 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005568 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00005569 } else if (Init->isDelegatingInitializer()) {
5570 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00005571 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00005572 } else if (Init->isMemberInitializer()){
5573 Record.push_back(CTOR_INITIALIZER_MEMBER);
5574 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005575 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00005576 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5577 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005578 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00005579
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005580 AddSourceLocation(Init->getMemberLocation(), Record);
5581 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005582 AddSourceLocation(Init->getLParenLoc(), Record);
5583 AddSourceLocation(Init->getRParenLoc(), Record);
5584 Record.push_back(Init->isWritten());
5585 if (Init->isWritten()) {
5586 Record.push_back(Init->getSourceOrder());
5587 } else {
5588 Record.push_back(Init->getNumArrayIndices());
5589 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5590 AddDeclRef(Init->getArrayIndex(i), Record);
5591 }
5592 }
5593}
5594
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005595void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005596 auto &Data = D->data();
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005597 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005598 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005599 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005600 Record.push_back(Data.Aggregate);
5601 Record.push_back(Data.PlainOldData);
5602 Record.push_back(Data.Empty);
5603 Record.push_back(Data.Polymorphic);
5604 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005605 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005606 Record.push_back(Data.HasNoNonEmptyBases);
5607 Record.push_back(Data.HasPrivateFields);
5608 Record.push_back(Data.HasProtectedFields);
5609 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005610 Record.push_back(Data.HasMutableFields);
Stephen Hines651f13c2014-04-23 16:59:28 -07005611 Record.push_back(Data.HasVariantMembers);
Richard Smithdfefb842012-02-25 07:33:38 +00005612 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005613 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005614 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005615 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5616 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5617 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5618 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5619 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5620 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005621 Record.push_back(Data.HasTrivialSpecialMembers);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005622 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005623 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005624 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005625 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005626 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005627 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005628 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005629 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005630 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005631 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5632 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5633 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5634 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smithdfefb842012-02-25 07:33:38 +00005635 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005636
5637 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005638 if (Data.NumBases > 0)
5639 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5640 Record);
5641
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005642 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5643 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005644 if (Data.NumVBases > 0)
5645 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5646 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005647
Richard Smithc2d77572013-08-30 04:46:40 +00005648 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5649 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005650 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005651 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005652
5653 // Add lambda-specific data.
5654 if (Data.IsLambda) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005655 auto &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005656 Record.push_back(Lambda.Dependent);
Faisal Valibef582b2013-10-23 16:10:50 +00005657 Record.push_back(Lambda.IsGenericLambda);
5658 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005659 Record.push_back(Lambda.NumCaptures);
5660 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005661 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005662 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005663 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005664 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005665 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005666 AddSourceLocation(Capture.getLocation(), Record);
5667 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005668 Record.push_back(Capture.getCaptureKind());
5669 switch (Capture.getCaptureKind()) {
5670 case LCK_This:
Stephen Hines176edba2014-12-01 14:53:08 -08005671 case LCK_VLAType:
Richard Smith0d8e9642013-05-16 06:20:58 +00005672 break;
5673 case LCK_ByCopy:
Richard Smith04fa7a32013-09-28 04:02:39 +00005674 case LCK_ByRef:
Richard Smith0d8e9642013-05-16 06:20:58 +00005675 VarDecl *Var =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005676 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith0d8e9642013-05-16 06:20:58 +00005677 AddDeclRef(Var, Record);
5678 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5679 : SourceLocation(),
5680 Record);
5681 break;
5682 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005683 }
5684 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005685}
5686
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005687void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005688 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005689 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005690 assert(FirstDeclID == NextDeclID &&
5691 FirstTypeID == NextTypeID &&
5692 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005693 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005694 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005695 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005696 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005697
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005698 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005699
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005700 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5701 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5702 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005703 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005704 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005705 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005706 NextDeclID = FirstDeclID;
5707 NextTypeID = FirstTypeID;
5708 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005709 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005710 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005711 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005712}
5713
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005714void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005715 // Always keep the highest ID. See \p TypeRead() for more information.
5716 IdentID &StoredID = IdentifierIDs[II];
5717 if (ID > StoredID)
5718 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005719}
5720
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005721void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005722 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005723 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005724 if (ID > StoredID)
5725 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005726}
5727
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005728void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005729 // Always take the highest-numbered type index. This copes with an interesting
5730 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005731 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005732 // keep the higher-numbered entry so that we can properly write it out to
5733 // the AST file.
5734 TypeIdx &StoredIdx = TypeIdxs[T];
5735 if (Idx.getIndex() >= StoredIdx.getIndex())
5736 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005737}
5738
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005739void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005740 // Always keep the highest ID. See \p TypeRead() for more information.
5741 SelectorID &StoredID = SelectorIDs[S];
5742 if (ID > StoredID)
5743 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005744}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005745
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005746void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005747 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005748 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005749 MacroDefinitions[MD] = ID;
5750}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005751
Douglas Gregora015cab2011-12-02 17:30:13 +00005752void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5753 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5754 SubmoduleIDs[Mod] = ID;
5755}
5756
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005757void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005758 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005759 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005760 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5761 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005762 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005763 // A forward reference was mutated into a definition. Rewrite it.
5764 // FIXME: This happens during template instantiation, should we
5765 // have created a new definition decl instead ?
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005766 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5767 "completed a tag from another module but not by instantiation?");
5768 DeclUpdates[RD].push_back(
5769 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005770 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005771 }
5772}
Douglas Gregora8235d62012-10-09 23:05:51 +00005773
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005774void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5775 // TU and namespaces are handled elsewhere.
5776 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5777 return;
5778
Douglas Gregor919814d2011-09-09 23:01:35 +00005779 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005780 return; // Not a source decl added to a DeclContext from PCH.
5781
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005782 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Stephen Hines176edba2014-12-01 14:53:08 -08005783 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005784 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005785 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005786}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005787
5788void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5789 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005790 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005791 return; // Not a source member added to a class from PCH.
5792 if (!isa<CXXMethodDecl>(D))
5793 return; // We are interested in lazily declared implicit methods.
5794
5795 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005796 assert(RD->isCompleteDefinition());
Stephen Hines176edba2014-12-01 14:53:08 -08005797 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005798 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005799}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005800
5801void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5802 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005803 // The specializations set is kept in the canonical template.
5804 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005805 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005806 return; // Not a source specialization added to a template from PCH.
5807
Stephen Hines176edba2014-12-01 14:53:08 -08005808 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005809 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5810 D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005811}
Douglas Gregor89d99802010-11-30 06:16:57 +00005812
Larisse Voufoef4579c2013-08-06 01:03:05 +00005813void ASTWriter::AddedCXXTemplateSpecialization(
5814 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5815 // The specializations set is kept in the canonical template.
Larisse Voufoef4579c2013-08-06 01:03:05 +00005816 TD = TD->getCanonicalDecl();
5817 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5818 return; // Not a source specialization added to a template from PCH.
5819
Stephen Hines176edba2014-12-01 14:53:08 -08005820 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005821 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5822 D));
Larisse Voufoef4579c2013-08-06 01:03:05 +00005823}
5824
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005825void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5826 const FunctionDecl *D) {
5827 // The specializations set is kept in the canonical template.
5828 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005829 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005830 return; // Not a source specialization added to a template from PCH.
5831
Stephen Hines176edba2014-12-01 14:53:08 -08005832 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005833 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5834 D));
5835}
5836
5837void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5838 assert(!WritingAST && "Already writing the AST!");
5839 FD = FD->getCanonicalDecl();
5840 if (!FD->isFromASTFile())
5841 return; // Not a function declared in PCH and defined outside.
5842
5843 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005844}
5845
Richard Smith9dadfab2013-05-11 05:45:24 +00005846void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5847 assert(!WritingAST && "Already writing the AST!");
5848 FD = FD->getCanonicalDecl();
5849 if (!FD->isFromASTFile())
5850 return; // Not a function declared in PCH and defined outside.
5851
Stephen Hines651f13c2014-04-23 16:59:28 -07005852 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith9dadfab2013-05-11 05:45:24 +00005853}
5854
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005855void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005856 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005857 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005858 return; // Declaration not imported from PCH.
5859
Stephen Hines176edba2014-12-01 14:53:08 -08005860 // Implicit function decl from a PCH was defined.
5861 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005862}
5863
Stephen Hines651f13c2014-04-23 16:59:28 -07005864void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5865 assert(!WritingAST && "Already writing the AST!");
5866 if (!D->isFromASTFile())
5867 return;
5868
Stephen Hines651f13c2014-04-23 16:59:28 -07005869 DeclUpdates[D].push_back(
Stephen Hines176edba2014-12-01 14:53:08 -08005870 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Stephen Hines651f13c2014-04-23 16:59:28 -07005871}
5872
Sebastian Redlf79a7192011-04-29 08:19:30 +00005873void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005874 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005875 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005876 return;
5877
5878 // Since the actual instantiation is delayed, this really means that we need
5879 // to update the instantiation location.
Stephen Hines651f13c2014-04-23 16:59:28 -07005880 DeclUpdates[D].push_back(
5881 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5882 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redlf79a7192011-04-29 08:19:30 +00005883}
5884
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005885void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5886 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005887 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005888 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005889 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005890
5891 assert(IFD->getDefinition() && "Category on a class without a definition?");
5892 ObjCClassesWithCategories.insert(
5893 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005894}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005895
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005896
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005897void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5898 const ObjCPropertyDecl *OrigProp,
5899 const ObjCCategoryDecl *ClassExt) {
5900 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5901 if (!D)
5902 return;
5903
5904 assert(!WritingAST && "Already writing the AST!");
5905 if (!D->isFromASTFile())
5906 return; // Declaration not imported from PCH.
5907
5908 RewriteDecl(D);
5909}
Eli Friedman86164e82013-09-05 00:02:25 +00005910
5911void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5912 assert(!WritingAST && "Already writing the AST!");
5913 if (!D->isFromASTFile())
5914 return;
5915
Stephen Hines651f13c2014-04-23 16:59:28 -07005916 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman86164e82013-09-05 00:02:25 +00005917}
Stephen Hines176edba2014-12-01 14:53:08 -08005918
5919void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5920 assert(!WritingAST && "Already writing the AST!");
5921 if (!D->isFromASTFile())
5922 return;
5923
5924 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5925}