blob: b9be775408b2dac812efc150f114beb6c72c6dbc [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);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000944 RECORD(PP_MACRO_OBJECT_LIKE);
945 RECORD(PP_MACRO_FUNCTION_LIKE);
946 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000947
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000948 // Decls and Types block.
949 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000950 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000951 RECORD(TYPE_COMPLEX);
952 RECORD(TYPE_POINTER);
953 RECORD(TYPE_BLOCK_POINTER);
954 RECORD(TYPE_LVALUE_REFERENCE);
955 RECORD(TYPE_RVALUE_REFERENCE);
956 RECORD(TYPE_MEMBER_POINTER);
957 RECORD(TYPE_CONSTANT_ARRAY);
958 RECORD(TYPE_INCOMPLETE_ARRAY);
959 RECORD(TYPE_VARIABLE_ARRAY);
960 RECORD(TYPE_VECTOR);
961 RECORD(TYPE_EXT_VECTOR);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000962 RECORD(TYPE_FUNCTION_NO_PROTO);
Stephen Hines176edba2014-12-01 14:53:08 -0800963 RECORD(TYPE_FUNCTION_PROTO);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000964 RECORD(TYPE_TYPEDEF);
965 RECORD(TYPE_TYPEOF_EXPR);
966 RECORD(TYPE_TYPEOF);
967 RECORD(TYPE_RECORD);
968 RECORD(TYPE_ENUM);
969 RECORD(TYPE_OBJC_INTERFACE);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000970 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000971 RECORD(TYPE_DECLTYPE);
972 RECORD(TYPE_ELABORATED);
973 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
974 RECORD(TYPE_UNRESOLVED_USING);
975 RECORD(TYPE_INJECTED_CLASS_NAME);
976 RECORD(TYPE_OBJC_OBJECT);
977 RECORD(TYPE_TEMPLATE_TYPE_PARM);
978 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
979 RECORD(TYPE_DEPENDENT_NAME);
980 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
981 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
982 RECORD(TYPE_PAREN);
983 RECORD(TYPE_PACK_EXPANSION);
984 RECORD(TYPE_ATTRIBUTED);
985 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Stephen Hines176edba2014-12-01 14:53:08 -0800986 RECORD(TYPE_AUTO);
987 RECORD(TYPE_UNARY_TRANSFORM);
Eli Friedmanb001de72011-10-06 23:00:33 +0000988 RECORD(TYPE_ATOMIC);
Stephen Hines176edba2014-12-01 14:53:08 -0800989 RECORD(TYPE_DECAYED);
990 RECORD(TYPE_ADJUSTED);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000991 RECORD(DECL_TYPEDEF);
Stephen Hines176edba2014-12-01 14:53:08 -0800992 RECORD(DECL_TYPEALIAS);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000993 RECORD(DECL_ENUM);
994 RECORD(DECL_RECORD);
995 RECORD(DECL_ENUM_CONSTANT);
996 RECORD(DECL_FUNCTION);
997 RECORD(DECL_OBJC_METHOD);
998 RECORD(DECL_OBJC_INTERFACE);
999 RECORD(DECL_OBJC_PROTOCOL);
1000 RECORD(DECL_OBJC_IVAR);
1001 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +00001002 RECORD(DECL_OBJC_CATEGORY);
1003 RECORD(DECL_OBJC_CATEGORY_IMPL);
1004 RECORD(DECL_OBJC_IMPLEMENTATION);
1005 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1006 RECORD(DECL_OBJC_PROPERTY);
1007 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001008 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +00001009 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001010 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +00001011 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001012 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +00001013 RECORD(DECL_FILE_SCOPE_ASM);
1014 RECORD(DECL_BLOCK);
1015 RECORD(DECL_CONTEXT_LEXICAL);
1016 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +00001017 RECORD(DECL_NAMESPACE);
1018 RECORD(DECL_NAMESPACE_ALIAS);
1019 RECORD(DECL_USING);
1020 RECORD(DECL_USING_SHADOW);
1021 RECORD(DECL_USING_DIRECTIVE);
1022 RECORD(DECL_UNRESOLVED_USING_VALUE);
1023 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1024 RECORD(DECL_LINKAGE_SPEC);
1025 RECORD(DECL_CXX_RECORD);
1026 RECORD(DECL_CXX_METHOD);
1027 RECORD(DECL_CXX_CONSTRUCTOR);
1028 RECORD(DECL_CXX_DESTRUCTOR);
1029 RECORD(DECL_CXX_CONVERSION);
1030 RECORD(DECL_ACCESS_SPEC);
1031 RECORD(DECL_FRIEND);
1032 RECORD(DECL_FRIEND_TEMPLATE);
1033 RECORD(DECL_CLASS_TEMPLATE);
1034 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1035 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufoef4579c2013-08-06 01:03:05 +00001036 RECORD(DECL_VAR_TEMPLATE);
1037 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1038 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +00001039 RECORD(DECL_FUNCTION_TEMPLATE);
1040 RECORD(DECL_TEMPLATE_TYPE_PARM);
1041 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1042 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1043 RECORD(DECL_STATIC_ASSERT);
1044 RECORD(DECL_CXX_BASE_SPECIFIERS);
1045 RECORD(DECL_INDIRECTFIELD);
1046 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1047
Douglas Gregora72d8c42011-06-03 02:27:19 +00001048 // Statements and Exprs can occur in the Decls and Types block.
1049 AddStmtsExprs(Stream, Record);
1050
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001051 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001052 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001053 RECORD(PPD_MACRO_DEFINITION);
1054 RECORD(PPD_INCLUSION_DIRECTIVE);
1055
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001056#undef RECORD
1057#undef BLOCK
1058 Stream.ExitBlock();
1059}
1060
Douglas Gregore650c8c2009-07-07 00:12:59 +00001061/// \brief Adjusts the given filename to only write out the portion of the
1062/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +00001063///
Douglas Gregore650c8c2009-07-07 00:12:59 +00001064/// \param Filename the file name to adjust.
1065///
1066/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
1067/// the returned filename will be adjusted by this system root.
1068///
1069/// \returns either the original filename (if it needs no adjustment) or the
1070/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +00001071static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +00001072adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001073 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Douglas Gregor832d6202011-07-22 16:35:34 +00001075 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001076 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Douglas Gregore650c8c2009-07-07 00:12:59 +00001078 // Verify that the filename and the system root have the same prefix.
1079 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +00001080 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +00001081 if (Filename[Pos] != isysroot[Pos])
1082 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Douglas Gregore650c8c2009-07-07 00:12:59 +00001084 // We hit the end of the filename before we hit the end of the system root.
1085 if (!Filename[Pos])
1086 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Douglas Gregore650c8c2009-07-07 00:12:59 +00001088 // If the file name has a '/' at the current position, skip over the '/'.
1089 // We distinguish sysroot-based includes from absolute includes by the
1090 // absence of '/' at the beginning of sysroot-based includes.
1091 if (Filename[Pos] == '/')
1092 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Douglas Gregore650c8c2009-07-07 00:12:59 +00001094 return Filename + Pos;
1095}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001096
Stephen Hines176edba2014-12-01 14:53:08 -08001097static ASTFileSignature getSignature() {
1098 while (1) {
1099 if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1100 return S;
1101 // Rely on GetRandomNumber to eventually return non-zero...
1102 }
1103}
1104
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001105/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001106void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1107 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001108 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001109 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001110 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1111 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001112
Douglas Gregore650c8c2009-07-07 00:12:59 +00001113 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001114 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1115 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1116 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1117 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1118 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1119 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1120 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1121 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1122 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1123 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1124 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001125 Record.push_back(VERSION_MAJOR);
1126 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001127 Record.push_back(CLANG_VERSION_MAJOR);
1128 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001129 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001130 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001131 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1132 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001133
Stephen Hines176edba2014-12-01 14:53:08 -08001134 // Signature
1135 Record.clear();
1136 Record.push_back(getSignature());
1137 Stream.EmitRecord(SIGNATURE, Record);
1138
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001139 // Module name
1140 if (WritingModule) {
1141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1142 Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1144 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1145 RecordData Record;
1146 Record.push_back(MODULE_NAME);
1147 Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1148 }
1149
1150 // Module map file
1151 if (WritingModule) {
Stephen Hines176edba2014-12-01 14:53:08 -08001152 Record.clear();
1153 auto addModMap = [&](const FileEntry *F) {
1154 SmallString<128> ModuleMap(F->getName());
1155 llvm::sys::fs::make_absolute(ModuleMap);
1156 AddString(ModuleMap.str(), Record);
1157 };
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001158
Stephen Hines176edba2014-12-01 14:53:08 -08001159 auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1160
1161 // Primary module map file.
1162 addModMap(Map.getModuleMapFileForUniquing(WritingModule));
1163
1164 // Additional module map files.
1165 if (auto *AdditionalModMaps = Map.getAdditionalModuleMapFiles(WritingModule)) {
1166 Record.push_back(AdditionalModMaps->size());
1167 for (const FileEntry *F : *AdditionalModMaps)
1168 addModMap(F);
1169 } else {
1170 Record.push_back(0);
1171 }
1172
1173 Stream.EmitRecord(MODULE_MAP_FILE, Record);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001174 }
1175
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001176 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001177 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001178 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregore95b9192011-08-17 21:07:30 +00001179 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001180
1181 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1182 M != MEnd; ++M) {
1183 // Skip modules that weren't directly imported.
1184 if (!(*M)->isDirectlyImported())
1185 continue;
1186
1187 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001188 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001189 Record.push_back((*M)->File->getSize());
1190 Record.push_back((*M)->File->getModificationTime());
Stephen Hines176edba2014-12-01 14:53:08 -08001191 Record.push_back((*M)->Signature);
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001192 const std::string &FileName = (*M)->FileName;
1193 Record.push_back(FileName.size());
1194 Record.append(FileName.begin(), FileName.end());
1195 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001196 Stream.EmitRecord(IMPORTS, Record);
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001199 // Language options.
1200 Record.clear();
1201 const LangOptions &LangOpts = Context.getLangOpts();
1202#define LANGOPT(Name, Bits, Default, Description) \
1203 Record.push_back(LangOpts.Name);
1204#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1205 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
Stephen Hines176edba2014-12-01 14:53:08 -08001206#include "clang/Basic/LangOptions.def"
1207#define SANITIZER(NAME, ID) \
1208 Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
Will Dietz4f45bc02013-01-18 11:30:38 +00001209#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001210
1211 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1212 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1213
1214 Record.push_back(LangOpts.CurrentModule.size());
1215 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001216
1217 // Comment options.
1218 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1219 for (CommentOptions::BlockCommandNamesTy::const_iterator
1220 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1221 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1222 I != IEnd; ++I) {
1223 AddString(*I, Record);
1224 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001225 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001226
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001227 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1228
Douglas Gregoree097c12012-10-18 17:58:09 +00001229 // Target options.
1230 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001231 const TargetInfo &Target = Context.getTargetInfo();
1232 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001233 AddString(TargetOpts.Triple, Record);
1234 AddString(TargetOpts.CPU, Record);
1235 AddString(TargetOpts.ABI, Record);
Douglas Gregoree097c12012-10-18 17:58:09 +00001236 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1237 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1238 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1239 }
1240 Record.push_back(TargetOpts.Features.size());
1241 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1242 AddString(TargetOpts.Features[I], Record);
1243 }
1244 Stream.EmitRecord(TARGET_OPTIONS, Record);
1245
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001246 // Diagnostic options.
1247 Record.clear();
1248 const DiagnosticOptions &DiagOpts
1249 = Context.getDiagnostics().getDiagnosticOptions();
1250#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1251#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1252 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1253#include "clang/Basic/DiagnosticOptions.def"
1254 Record.push_back(DiagOpts.Warnings.size());
1255 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1256 AddString(DiagOpts.Warnings[I], Record);
Stephen Hines176edba2014-12-01 14:53:08 -08001257 Record.push_back(DiagOpts.Remarks.size());
1258 for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1259 AddString(DiagOpts.Remarks[I], Record);
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001260 // Note: we don't serialize the log or serialization file names, because they
1261 // are generally transient files and will almost always be overridden.
1262 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1263
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001264 // File system options.
1265 Record.clear();
1266 const FileSystemOptions &FSOpts
1267 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1268 AddString(FSOpts.WorkingDir, Record);
1269 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1270
Douglas Gregorbbf38312012-10-24 16:50:34 +00001271 // Header search options.
1272 Record.clear();
1273 const HeaderSearchOptions &HSOpts
1274 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1275 AddString(HSOpts.Sysroot, Record);
1276
1277 // Include entries.
1278 Record.push_back(HSOpts.UserEntries.size());
1279 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1280 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1281 AddString(Entry.Path, Record);
1282 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001283 Record.push_back(Entry.IsFramework);
1284 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001285 }
1286
1287 // System header prefixes.
1288 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1289 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1290 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1291 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1292 }
1293
1294 AddString(HSOpts.ResourceDir, Record);
1295 AddString(HSOpts.ModuleCachePath, Record);
Stephen Hines651f13c2014-04-23 16:59:28 -07001296 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001297 Record.push_back(HSOpts.DisableModuleHash);
1298 Record.push_back(HSOpts.UseBuiltinIncludes);
1299 Record.push_back(HSOpts.UseStandardSystemIncludes);
1300 Record.push_back(HSOpts.UseStandardCXXIncludes);
1301 Record.push_back(HSOpts.UseLibcxx);
1302 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1303
Douglas Gregora71a7d82012-10-24 20:05:57 +00001304 // Preprocessor options.
1305 Record.clear();
1306 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1307
1308 // Macro definitions.
1309 Record.push_back(PPOpts.Macros.size());
1310 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1311 AddString(PPOpts.Macros[I].first, Record);
1312 Record.push_back(PPOpts.Macros[I].second);
1313 }
1314
1315 // Includes
1316 Record.push_back(PPOpts.Includes.size());
1317 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1318 AddString(PPOpts.Includes[I], Record);
1319
1320 // Macro includes
1321 Record.push_back(PPOpts.MacroIncludes.size());
1322 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1323 AddString(PPOpts.MacroIncludes[I], Record);
1324
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001325 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001326 // Detailed record is important since it is used for the module cache hash.
1327 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001328 AddString(PPOpts.ImplicitPCHInclude, Record);
1329 AddString(PPOpts.ImplicitPTHInclude, Record);
1330 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1331 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1332
Douglas Gregor31d375f2011-05-06 21:43:30 +00001333 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001334 SourceManager &SM = Context.getSourceManager();
1335 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1336 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001337 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1338 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001339 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1340 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1341
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001342 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001344 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001345
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001346 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001347 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001348 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001349 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001350 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001351 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001352 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001353 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001354
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001355 Record.clear();
1356 Record.push_back(SM.getMainFileID().getOpaqueValue());
1357 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1358
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001359 // Original PCH directory
1360 if (!OutputFile.empty() && OutputFile != "-") {
1361 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1362 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1364 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1365
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001366 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001367
1368 llvm::sys::fs::make_absolute(OutputPath);
1369 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1370
1371 RecordData Record;
1372 Record.push_back(ORIGINAL_PCH_DIR);
1373 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1374 }
1375
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001376 WriteInputFiles(Context.SourceMgr,
1377 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregorb22d1942013-07-22 20:48:33 +00001378 isysroot,
1379 PP.getLangOpts().Modules);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001380 Stream.ExitBlock();
1381}
1382
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001383namespace {
1384 /// \brief An input file.
1385 struct InputFileEntry {
1386 const FileEntry *File;
1387 bool IsSystemFile;
1388 bool BufferOverridden;
1389 };
1390}
1391
1392void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1393 HeaderSearchOptions &HSOpts,
Douglas Gregorb22d1942013-07-22 20:48:33 +00001394 StringRef isysroot,
1395 bool Modules) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001396 using namespace llvm;
1397 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1398 RecordData Record;
1399
1400 // Create input-file abbreviation.
1401 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1402 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001403 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001404 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1405 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001406 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001407 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1408 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1409
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001410 // Get all ContentCache objects for files, sorted by whether the file is a
1411 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001412 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001413 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1414 // Get this source location entry.
1415 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001416 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001417
1418 // We only care about file entries that were not overridden.
1419 if (!SLoc->isFile())
1420 continue;
1421 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001422 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001423 continue;
1424
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001425 InputFileEntry Entry;
1426 Entry.File = Cache->OrigEntry;
1427 Entry.IsSystemFile = Cache->IsSystemFile;
1428 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001429 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001430 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001431 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001432 SortedFiles.push_front(Entry);
1433 }
1434
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001435 unsigned UserFilesNum = 0;
1436 // Write out all of the input files.
1437 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001438 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001439 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001440 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001441
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001442 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001443 if (InputFileID != 0)
1444 continue; // already recorded this file.
1445
Douglas Gregora930dc92012-10-22 18:42:04 +00001446 // Record this entry's offset.
1447 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001448
1449 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001450
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001451 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001452 ++UserFilesNum;
1453
Douglas Gregor745e6f12012-10-19 00:38:02 +00001454 Record.clear();
1455 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001456 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001457
1458 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001459 Record.push_back(Entry.File->getSize());
1460 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001461
Douglas Gregora930dc92012-10-22 18:42:04 +00001462 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001463 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001464
Douglas Gregor745e6f12012-10-19 00:38:02 +00001465 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001466 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001467 SmallString<128> FilePath(Filename);
1468
1469 // Ask the file manager to fixup the relative path for us. This will
1470 // honor the working directory.
Stephen Hines651f13c2014-04-23 16:59:28 -07001471 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001472
1473 // FIXME: This call to make_absolute shouldn't be necessary, the
1474 // call to FixupRelativePath should always return an absolute path.
1475 llvm::sys::fs::make_absolute(FilePath);
1476 Filename = FilePath.c_str();
1477
1478 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1479
1480 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1481 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001482
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001483 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001484
1485 // Create input file offsets abbreviation.
1486 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1487 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1488 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001489 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1490 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001491 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1492 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1493
1494 // Write input file offsets.
1495 Record.clear();
1496 Record.push_back(INPUT_FILE_OFFSETS);
1497 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001498 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001499 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001500}
1501
Douglas Gregor14f79002009-04-10 03:52:48 +00001502//===----------------------------------------------------------------------===//
1503// Source Manager Serialization
1504//===----------------------------------------------------------------------===//
1505
1506/// \brief Create an abbreviation for the SLocEntry that refers to a
1507/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001508static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001509 using namespace llvm;
1510 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001511 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1514 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1515 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001516 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001517 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001518 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001521 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001522}
1523
1524/// \brief Create an abbreviation for the SLocEntry that refers to a
1525/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001526static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001527 using namespace llvm;
1528 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001529 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1532 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1533 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001535 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001536}
1537
1538/// \brief Create an abbreviation for the SLocEntry that refers to a
1539/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001540static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001541 using namespace llvm;
1542 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001544 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001545 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001546}
1547
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001548/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1549/// expansion.
1550static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001551 using namespace llvm;
1552 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001553 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001559 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001560}
1561
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001562namespace {
1563 // Trait used for the on-disk hash table of header search information.
1564 class HeaderFileInfoTrait {
1565 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001566 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001567
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001568 // Keep track of the framework names we've used during serialization.
1569 SmallVector<char, 128> FrameworkStringData;
1570 llvm::StringMap<unsigned> FrameworkNameOffset;
1571
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001572 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001573 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1574 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001575
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001576 struct key_type {
1577 const FileEntry *FE;
1578 const char *Filename;
1579 };
1580 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001581
1582 typedef HeaderFileInfo data_type;
1583 typedef const data_type &data_type_ref;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001584 typedef unsigned hash_value_type;
1585 typedef unsigned offset_type;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001586
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001587 static hash_value_type ComputeHash(key_type_ref key) {
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001588 // The hash is based only on size/time of the file, so that the reader can
1589 // match even when symlinking or excess path elements ("foo/../", "../")
1590 // change the form of the name. However, complete path is still the key.
1591 return llvm::hash_combine(key.FE->getSize(),
1592 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001593 }
1594
1595 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001596 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001597 using namespace llvm::support;
1598 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001599 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001600 Writer.write<uint16_t>(KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001601 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001602 if (Data.isModuleHeader)
1603 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07001604 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001605 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001606 }
1607
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001608 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001609 using namespace llvm::support;
1610 endian::Writer<little> LE(Out);
1611 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001612 KeyLen -= 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001613 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001614 KeyLen -= 8;
1615 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001616 }
1617
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001618 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001619 data_type_ref Data, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001620 using namespace llvm::support;
1621 endian::Writer<little> LE(Out);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001622 uint64_t Start = Out.tell(); (void)Start;
1623
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001624 unsigned char Flags = (Data.HeaderRole << 6)
1625 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001626 | (Data.isPragmaOnce << 4)
1627 | (Data.DirInfo << 2)
1628 | (Data.Resolved << 1)
1629 | Data.IndexHeaderMapHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -07001630 LE.write<uint8_t>(Flags);
1631 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001632
1633 if (!Data.ControllingMacro)
Stephen Hines651f13c2014-04-23 16:59:28 -07001634 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001635 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001636 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001637
1638 unsigned Offset = 0;
1639 if (!Data.Framework.empty()) {
1640 // If this header refers into a framework, save the framework name.
1641 llvm::StringMap<unsigned>::iterator Pos
1642 = FrameworkNameOffset.find(Data.Framework);
1643 if (Pos == FrameworkNameOffset.end()) {
1644 Offset = FrameworkStringData.size() + 1;
1645 FrameworkStringData.append(Data.Framework.begin(),
1646 Data.Framework.end());
1647 FrameworkStringData.push_back(0);
1648
1649 FrameworkNameOffset[Data.Framework] = Offset;
1650 } else
1651 Offset = Pos->second;
1652 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001653 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001654
1655 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001656 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Stephen Hines651f13c2014-04-23 16:59:28 -07001657 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001658 }
1659
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001660 assert(Out.tell() - Start == DataLen && "Wrong data length");
1661 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001662
1663 const char *strings_begin() const { return FrameworkStringData.begin(); }
1664 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001665 };
1666} // end anonymous namespace
1667
1668/// \brief Write the header search block for the list of files that
1669///
1670/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001671void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001672 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001673 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1674
1675 if (FilesByUID.size() > HS.header_file_size())
1676 FilesByUID.resize(HS.header_file_size());
1677
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001678 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001679 llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001680 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001681 unsigned NumHeaderSearchEntries = 0;
1682 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1683 const FileEntry *File = FilesByUID[UID];
1684 if (!File)
1685 continue;
1686
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001687 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1688 // from the external source if it was not provided already.
Stephen Hines651f13c2014-04-23 16:59:28 -07001689 HeaderFileInfo HFI;
1690 if (!HS.tryGetFileInfo(File, HFI) ||
1691 (HFI.External && Chain) ||
1692 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001693 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001694
1695 // Turn the file name into an absolute path, if it isn't already.
1696 const char *Filename = File->getName();
1697 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1698
1699 // If we performed any translation on the file name at all, we need to
1700 // save this string, since the generator will refer to it later.
1701 if (Filename != File->getName()) {
1702 Filename = strdup(Filename);
1703 SavedStrings.push_back(Filename);
1704 }
1705
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001706 HeaderFileInfoTrait::key_type key = { File, Filename };
1707 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001708 ++NumHeaderSearchEntries;
1709 }
1710
1711 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001712 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001713 uint32_t BucketOffset;
1714 {
Stephen Hines651f13c2014-04-23 16:59:28 -07001715 using namespace llvm::support;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001716 llvm::raw_svector_ostream Out(TableData);
1717 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07001718 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001719 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1720 }
1721
1722 // Create a blob abbreviation
1723 using namespace llvm;
1724 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1725 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1730 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1731
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001732 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001733 RecordData Record;
1734 Record.push_back(HEADER_SEARCH_TABLE);
1735 Record.push_back(BucketOffset);
1736 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001737 Record.push_back(TableData.size());
1738 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001739 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1740
1741 // Free all of the strings we had to duplicate.
1742 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001743 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001744}
1745
Douglas Gregor14f79002009-04-10 03:52:48 +00001746/// \brief Writes the block containing the serialized form of the
1747/// source manager.
1748///
1749/// TODO: We should probably use an on-disk hash table (stored in a
1750/// blob), indexed based on the file name, so that we only create
1751/// entries for files that we actually need. In the common case (no
1752/// errors), we probably won't have to create file entries for any of
1753/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001754void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001755 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001756 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001757 RecordData Record;
1758
Chris Lattnerf04ad692009-04-10 17:16:57 +00001759 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001760 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001761
1762 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001763 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1764 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1765 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001766 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001767
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001768 // Write out the source location entry table. We skip the first
1769 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001770 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001771 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001772 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1773 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001774 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001775 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001776 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001777 FileID FID = FileID::get(I);
1778 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001779
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001780 // Record the offset of this source-location entry.
1781 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1782
1783 // Figure out which record code to use.
1784 unsigned Code;
1785 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001786 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1787 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001788 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001789 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001790 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001791 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001792 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001793 Record.clear();
1794 Record.push_back(Code);
1795
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001796 // Starting offset of this entry within this module, so skip the dummy.
1797 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001798 if (SLoc->isFile()) {
1799 const SrcMgr::FileInfo &File = SLoc->getFile();
1800 Record.push_back(File.getIncludeLoc().getRawEncoding());
1801 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1802 Record.push_back(File.hasLineDirectives());
1803
1804 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001805 if (Content->OrigEntry) {
1806 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001807 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001808
Douglas Gregora930dc92012-10-22 18:42:04 +00001809 // The source location entry is a file. Emit input file ID.
1810 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1811 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001813 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001814
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001815 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001816 if (FDI != FileDeclIDs.end()) {
1817 Record.push_back(FDI->second->FirstDeclIndex);
1818 Record.push_back(FDI->second->DeclIDs.size());
1819 } else {
1820 Record.push_back(0);
1821 Record.push_back(0);
1822 }
Douglas Gregora081da52011-11-16 20:05:18 +00001823
Douglas Gregora930dc92012-10-22 18:42:04 +00001824 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001825
1826 if (Content->BufferOverridden) {
1827 Record.clear();
1828 Record.push_back(SM_SLOC_BUFFER_BLOB);
1829 const llvm::MemoryBuffer *Buffer
1830 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1831 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1832 StringRef(Buffer->getBufferStart(),
1833 Buffer->getBufferSize() + 1));
1834 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001835 } else {
1836 // The source location entry is a buffer. The blob associated
1837 // with this entry contains the contents of the buffer.
1838
1839 // We add one to the size so that we capture the trailing NULL
1840 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1841 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001842 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001843 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001844 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001845 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001846 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001847 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001848 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001849 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001850 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001851 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001852
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001853 if (strcmp(Name, "<built-in>") == 0) {
1854 PreloadSLocs.push_back(SLocEntryOffsets.size());
1855 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001856 }
1857 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001858 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001859 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001860 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1861 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001862 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1863 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001864
1865 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001866 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001867 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001868 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001869 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001870 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001871 }
1872 }
1873
Douglas Gregorc9490c02009-04-16 22:23:12 +00001874 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001875
1876 if (SLocEntryOffsets.empty())
1877 return;
1878
Sebastian Redl3397c552010-08-18 23:56:27 +00001879 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001880 // table is used for lazily loading source-location information.
1881 using namespace llvm;
1882 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001883 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1887 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001889 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001890 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001891 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001892 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001893 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001894
Sebastian Redl3397c552010-08-18 23:56:27 +00001895 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001896 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001897 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001898
1899 // Write the line table. It depends on remapping working, so it must come
1900 // after the source location offsets.
1901 if (SourceMgr.hasLineTable()) {
1902 LineTableInfo &LineTable = SourceMgr.getLineTable();
1903
1904 Record.clear();
1905 // Emit the file names
1906 Record.push_back(LineTable.getNumFilenames());
1907 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1908 // Emit the file name
1909 const char *Filename = LineTable.getFilename(I);
1910 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1911 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1912 Record.push_back(FilenameLen);
1913 if (FilenameLen)
1914 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1915 }
1916
1917 // Emit the line entries
1918 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1919 L != LEnd; ++L) {
1920 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001921 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001922 continue;
1923
1924 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001925 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001926
1927 // Emit the line entries
1928 Record.push_back(L->second.size());
1929 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1930 LEEnd = L->second.end();
1931 LE != LEEnd; ++LE) {
1932 Record.push_back(LE->FileOffset);
1933 Record.push_back(LE->LineNo);
1934 Record.push_back(LE->FilenameID);
1935 Record.push_back((unsigned)LE->FileKind);
1936 Record.push_back(LE->IncludeOffset);
1937 }
1938 }
1939 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1940 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001941}
1942
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001943//===----------------------------------------------------------------------===//
1944// Preprocessor Serialization
1945//===----------------------------------------------------------------------===//
1946
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001947namespace {
1948class ASTMacroTableTrait {
1949public:
1950 typedef IdentID key_type;
1951 typedef key_type key_type_ref;
1952
1953 struct Data {
1954 uint32_t MacroDirectivesOffset;
1955 };
1956
1957 typedef Data data_type;
1958 typedef const data_type &data_type_ref;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001959 typedef unsigned hash_value_type;
1960 typedef unsigned offset_type;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001961
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001962 static hash_value_type ComputeHash(IdentID IdID) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001963 return llvm::hash_value(IdID);
1964 }
1965
1966 std::pair<unsigned,unsigned>
1967 static EmitKeyDataLength(raw_ostream& Out,
1968 key_type_ref Key, data_type_ref Data) {
1969 unsigned KeyLen = 4; // IdentID.
1970 unsigned DataLen = 4; // MacroDirectivesOffset.
1971 return std::make_pair(KeyLen, DataLen);
1972 }
1973
1974 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001975 using namespace llvm::support;
1976 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001977 }
1978
1979 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1980 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001981 using namespace llvm::support;
1982 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001983 }
1984};
1985} // end anonymous namespace
1986
Benjamin Kramer767b3d22013-09-22 14:10:29 +00001987static int compareMacroDirectives(
1988 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1989 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1990 return X->first->getName().compare(Y->first->getName());
Douglas Gregor9c736102011-02-10 18:20:09 +00001991}
1992
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001993static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1994 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001995 if (MacroInfo *MI = MD->getMacroInfo())
1996 if (MI->isBuiltinMacro())
1997 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001998
1999 if (IsModule) {
Stephen Hines176edba2014-12-01 14:53:08 -08002000 // Re-export any imported directives.
2001 if (MD->isImported())
2002 return false;
2003
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00002004 SourceLocation Loc = MD->getLocation();
2005 if (Loc.isInvalid())
2006 return true;
2007 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2008 return true;
2009 }
2010
2011 return false;
2012}
2013
Chris Lattner0b1fb982009-04-10 17:15:23 +00002014/// \brief Writes the block containing the serialized form of the
2015/// preprocessor.
2016///
Douglas Gregor7143aab2011-09-01 17:04:32 +00002017void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002018 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2019 if (PPRec)
2020 WritePreprocessorDetail(*PPRec);
2021
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002022 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00002023
Chris Lattnerc1f9d822009-04-13 01:29:17 +00002024 // If the preprocessor __COUNTER__ value has been bumped, remember it.
2025 if (PP.getCounterValue() != 0) {
2026 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002027 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00002028 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002029 }
2030
2031 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00002032 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Sebastian Redl3397c552010-08-18 23:56:27 +00002034 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00002035 // FIXME: use diagnostics subsystem for localization etc.
2036 if (PP.SawDateOrTime())
2037 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Douglas Gregorecdcb882010-10-20 22:00:55 +00002039
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002040 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002041 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00002042
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002043 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002044 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002045 MacroDirectives;
2046 for (Preprocessor::macro_iterator
2047 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
2048 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002049 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002050 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00002051 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002052
Douglas Gregor9c736102011-02-10 18:20:09 +00002053 // Sort the set of macro definitions that need to be serialized by the
2054 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002055 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
2056 &compareMacroDirectives);
2057
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002058 llvm::OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002059
2060 // Emit the macro directives as a list and associate the offset with the
2061 // identifier they belong to.
2062 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
2063 const IdentifierInfo *Name = MacroDirectives[I].first;
2064 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
2065 MacroDirective *MD = MacroDirectives[I].second;
2066
2067 // If the macro or identifier need no updates, don't write the macro history
2068 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002069 // FIXME: Chain the macro history instead of re-writing it.
2070 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002071 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
2072 continue;
2073
2074 // Emit the macro directives in reverse source order.
2075 for (; MD; MD = MD->getPrevious()) {
2076 if (shouldIgnoreMacro(MD, IsModule, PP))
2077 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002078
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002079 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002080 Record.push_back(MD->getKind());
Stephen Hines176edba2014-12-01 14:53:08 -08002081 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002082 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
2083 Record.push_back(InfoID);
Stephen Hines176edba2014-12-01 14:53:08 -08002084 Record.push_back(DefMD->getOwningModuleID());
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002085 Record.push_back(DefMD->isAmbiguous());
Stephen Hines176edba2014-12-01 14:53:08 -08002086 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
2087 Record.push_back(UndefMD->getOwningModuleID());
2088 } else {
2089 auto *VisMD = cast<VisibilityMacroDirective>(MD);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002090 Record.push_back(VisMD->isPublic());
2091 }
Stephen Hines176edba2014-12-01 14:53:08 -08002092
2093 if (MD->isImported()) {
2094 auto Overrides = MD->getOverriddenModules();
2095 Record.push_back(Overrides.size());
2096 for (auto Override : Overrides)
2097 Record.push_back(Override);
2098 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002099 }
2100 if (Record.empty())
2101 continue;
2102
2103 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2104 Record.clear();
2105
2106 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
2107
2108 IdentID NameID = getIdentifierRef(Name);
2109 ASTMacroTableTrait::Data data;
2110 data.MacroDirectivesOffset = MacroDirectiveOffset;
2111 Generator.insert(NameID, data);
2112 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002113
Douglas Gregora8235d62012-10-09 23:05:51 +00002114 /// \brief Offsets of each of the macros into the bitstream, indexed by
2115 /// the local macro ID
2116 ///
2117 /// For each identifier that is associated with a macro, this map
2118 /// provides the offset into the bitstream where that macro is
2119 /// defined.
2120 std::vector<uint32_t> MacroOffsets;
2121
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002122 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2123 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2124 MacroInfo *MI = MacroInfosToEmit[I].MI;
2125 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00002126
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002127 if (ID < FirstMacroID) {
2128 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2129 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00002130 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002131
2132 // Record the local offset of this macro.
2133 unsigned Index = ID - FirstMacroID;
2134 if (Index == MacroOffsets.size())
2135 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2136 else {
2137 if (Index > MacroOffsets.size())
2138 MacroOffsets.resize(Index + 1);
2139
2140 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2141 }
2142
2143 AddIdentifierRef(Name, Record);
2144 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2145 AddSourceLocation(MI->getDefinitionLoc(), Record);
2146 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2147 Record.push_back(MI->isUsed());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002148 Record.push_back(MI->isUsedForHeaderGuard());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002149 unsigned Code;
2150 if (MI->isObjectLike()) {
2151 Code = PP_MACRO_OBJECT_LIKE;
2152 } else {
2153 Code = PP_MACRO_FUNCTION_LIKE;
2154
2155 Record.push_back(MI->isC99Varargs());
2156 Record.push_back(MI->isGNUVarargs());
2157 Record.push_back(MI->hasCommaPasting());
2158 Record.push_back(MI->getNumArgs());
2159 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2160 I != E; ++I)
2161 AddIdentifierRef(*I, Record);
2162 }
2163
2164 // If we have a detailed preprocessing record, record the macro definition
2165 // ID that corresponds to this macro.
2166 if (PPRec)
2167 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2168
2169 Stream.EmitRecord(Code, Record);
2170 Record.clear();
2171
2172 // Emit the tokens array.
2173 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2174 // Note that we know that the preprocessor does not have any annotation
2175 // tokens in it because they are created by the parser, and thus can't
2176 // be in a macro definition.
2177 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002178 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002179 Stream.EmitRecord(PP_TOKEN, Record);
2180 Record.clear();
2181 }
2182 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002183 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002184
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002185 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002186
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002187 // Create the on-disk hash table in a buffer.
2188 SmallString<4096> MacroTable;
2189 uint32_t BucketOffset;
2190 {
Stephen Hines651f13c2014-04-23 16:59:28 -07002191 using namespace llvm::support;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002192 llvm::raw_svector_ostream Out(MacroTable);
2193 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07002194 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002195 BucketOffset = Generator.Emit(Out);
2196 }
2197
2198 // Write the macro table
2199 using namespace llvm;
2200 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2201 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2204 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2205
2206 Record.push_back(MACRO_TABLE);
2207 Record.push_back(BucketOffset);
2208 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2209 Record.clear();
2210
Douglas Gregora8235d62012-10-09 23:05:51 +00002211 // Write the offsets table for macro IDs.
2212 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002213 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002214 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2218
2219 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2220 Record.clear();
2221 Record.push_back(MACRO_OFFSET);
2222 Record.push_back(MacroOffsets.size());
2223 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2224 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2225 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002226}
2227
2228void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002229 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002230 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002231
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002232 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002233
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002234 // Enter the preprocessor block.
2235 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002236
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002237 // If the preprocessor has a preprocessing record, emit it.
2238 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002239 using namespace llvm;
2240
2241 // Set up the abbreviation for
2242 unsigned InclusionAbbrev = 0;
2243 {
2244 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2245 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2251 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2252 }
2253
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002254 unsigned FirstPreprocessorEntityID
2255 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2256 + NUM_PREDEF_PP_ENTITY_IDS;
2257 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002258 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002259 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2260 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002261 E != EEnd;
2262 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002263 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002264
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002265 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2266 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002267
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002268 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002269 // Record this macro definition's ID.
2270 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002271
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002272 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002273 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2274 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002275 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002276
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002277 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002278 Record.push_back(ME->isBuiltinMacro());
2279 if (ME->isBuiltinMacro())
2280 AddIdentifierRef(ME->getName(), Record);
2281 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002282 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002283 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002284 continue;
2285 }
2286
2287 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2288 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002289 Record.push_back(ID->getFileName().size());
2290 Record.push_back(ID->wasInQuotes());
2291 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002292 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002293 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002294 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002295 // Check that the FileEntry is not null because it was not resolved and
2296 // we create a PCH even with compiler errors.
2297 if (ID->getFile())
2298 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002299 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2300 continue;
2301 }
2302
2303 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2304 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002305 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002306
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002307 // Write the offsets table for the preprocessing record.
2308 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002309 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2310
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002311 // Write the offsets table for identifier IDs.
2312 using namespace llvm;
2313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002314 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002317 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002318
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002319 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002320 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002321 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002322 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2323 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002324 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002325}
2326
Douglas Gregore209e502011-12-06 01:10:29 +00002327unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2328 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2329 if (Known != SubmoduleIDs.end())
2330 return Known->second;
2331
2332 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2333}
2334
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002335unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2336 if (!Mod)
2337 return 0;
2338
2339 llvm::DenseMap<Module *, unsigned>::const_iterator
2340 Known = SubmoduleIDs.find(Mod);
2341 if (Known != SubmoduleIDs.end())
2342 return Known->second;
2343
2344 return 0;
2345}
2346
Douglas Gregor26ced122011-12-01 00:59:36 +00002347/// \brief Compute the number of modules within the given tree (including the
2348/// given module).
2349static unsigned getNumberOfModules(Module *Mod) {
2350 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002351 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2352 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002353 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002354 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002355
2356 return ChildModules + 1;
2357}
2358
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002359void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002360 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002361 // FIXME: This feels like it belongs somewhere else, but there are no
2362 // other consumers of this information.
2363 SourceManager &SrcMgr = PP->getSourceManager();
2364 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Stephen Hines651f13c2014-04-23 16:59:28 -07002365 for (const auto *I : Context->local_imports()) {
Douglas Gregor55988682011-12-05 16:33:54 +00002366 if (Module *ImportedFrom
2367 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2368 SrcMgr))) {
2369 ImportedFrom->Imports.push_back(I->getImportedModule());
2370 }
2371 }
2372
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002373 // Enter the submodule description block.
Stephen Hines176edba2014-12-01 14:53:08 -08002374 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002375
2376 // Write the abbreviations needed for the submodules block.
2377 using namespace llvm;
2378 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2379 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Stephen Hines651f13c2014-04-23 16:59:28 -07002384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2391 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2392
2393 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002394 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2396 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2397
2398 Abbrev = new BitCodeAbbrev();
2399 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2401 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002402
2403 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002404 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2406 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2407
2408 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002409 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2411 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2412
Douglas Gregor51f564f2011-12-31 04:05:44 +00002413 Abbrev = new BitCodeAbbrev();
2414 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smith5794b532013-10-28 22:18:19 +00002415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor51f564f2011-12-31 04:05:44 +00002417 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2418
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002419 Abbrev = new BitCodeAbbrev();
2420 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2422 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2423
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002424 Abbrev = new BitCodeAbbrev();
Stephen Hines176edba2014-12-01 14:53:08 -08002425 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2427 unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2428
2429 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002430 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2432 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2433
2434 Abbrev = new BitCodeAbbrev();
Stephen Hines176edba2014-12-01 14:53:08 -08002435 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2437 unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2438
2439 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002440 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2441 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2443 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2444
Douglas Gregor63a72682013-03-20 00:22:05 +00002445 Abbrev = new BitCodeAbbrev();
2446 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2448 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2449
Douglas Gregor906d66a2013-03-20 21:10:35 +00002450 Abbrev = new BitCodeAbbrev();
2451 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2454 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2455
Douglas Gregor26ced122011-12-01 00:59:36 +00002456 // Write the submodule metadata block.
2457 RecordData Record;
2458 Record.push_back(getNumberOfModules(WritingModule));
2459 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2460 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2461
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002462 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002463 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002464 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002465 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002466 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002467 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002468 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002469
2470 // Emit the definition of the block.
2471 Record.clear();
2472 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002473 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002474 if (Mod->Parent) {
2475 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2476 Record.push_back(SubmoduleIDs[Mod->Parent]);
2477 } else {
2478 Record.push_back(0);
2479 }
2480 Record.push_back(Mod->IsFramework);
2481 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002482 Record.push_back(Mod->IsSystem);
Stephen Hines651f13c2014-04-23 16:59:28 -07002483 Record.push_back(Mod->IsExternC);
Douglas Gregor1e123682011-12-05 22:27:44 +00002484 Record.push_back(Mod->InferSubmodules);
2485 Record.push_back(Mod->InferExplicitSubmodules);
2486 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002487 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002488 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2489
Douglas Gregor51f564f2011-12-31 04:05:44 +00002490 // Emit the requirements.
Richard Smith5794b532013-10-28 22:18:19 +00002491 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +00002492 Record.clear();
2493 Record.push_back(SUBMODULE_REQUIRES);
Richard Smith5794b532013-10-28 22:18:19 +00002494 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002495 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smith5794b532013-10-28 22:18:19 +00002496 Mod->Requirements[I].first);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002497 }
2498
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002499 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002500 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002501 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002502 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002503 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002504 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002505 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2506 Record.clear();
2507 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2508 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2509 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002510 }
Stephen Hines176edba2014-12-01 14:53:08 -08002511
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002512 // Emit the headers.
Stephen Hines176edba2014-12-01 14:53:08 -08002513 struct {
2514 unsigned Kind;
2515 unsigned Abbrev;
2516 ArrayRef<const FileEntry*> Headers;
2517 } HeaderLists[] = {
2518 {SUBMODULE_HEADER, HeaderAbbrev, Mod->NormalHeaders},
2519 {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Mod->TextualHeaders},
2520 {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Mod->PrivateHeaders},
2521 {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2522 Mod->PrivateTextualHeaders},
2523 {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Mod->ExcludedHeaders},
2524 {SUBMODULE_TOPHEADER, TopHeaderAbbrev,
2525 Mod->getTopHeaders(PP->getFileManager())}
2526 };
2527 for (auto &HL : HeaderLists) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002528 Record.clear();
Stephen Hines176edba2014-12-01 14:53:08 -08002529 Record.push_back(HL.Kind);
2530 for (auto *H : HL.Headers)
2531 Stream.EmitRecordWithBlob(HL.Abbrev, Record, H->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002532 }
Douglas Gregor55988682011-12-05 16:33:54 +00002533
2534 // Emit the imports.
2535 if (!Mod->Imports.empty()) {
2536 Record.clear();
2537 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002538 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002539 assert(ImportedID && "Unknown submodule!");
2540 Record.push_back(ImportedID);
2541 }
2542 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2543 }
2544
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002545 // Emit the exports.
2546 if (!Mod->Exports.empty()) {
2547 Record.clear();
2548 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002549 if (Module *Exported = Mod->Exports[I].getPointer()) {
2550 unsigned ExportedID = SubmoduleIDs[Exported];
2551 assert(ExportedID > 0 && "Unknown submodule ID?");
2552 Record.push_back(ExportedID);
2553 } else {
2554 Record.push_back(0);
2555 }
2556
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002557 Record.push_back(Mod->Exports[I].getInt());
2558 }
2559 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2560 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002561
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00002562 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2563 // Might be unnecessary as use declarations are only used to build the
2564 // module itself.
2565
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002566 // Emit the link libraries.
2567 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2568 Record.clear();
2569 Record.push_back(SUBMODULE_LINK_LIBRARY);
2570 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2571 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2572 Mod->LinkLibraries[I].Library);
2573 }
2574
Douglas Gregor906d66a2013-03-20 21:10:35 +00002575 // Emit the conflicts.
2576 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2577 Record.clear();
2578 Record.push_back(SUBMODULE_CONFLICT);
2579 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2580 assert(OtherID && "Unknown submodule!");
2581 Record.push_back(OtherID);
2582 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2583 Mod->Conflicts[I].Message);
2584 }
2585
Douglas Gregor63a72682013-03-20 00:22:05 +00002586 // Emit the configuration macros.
2587 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2588 Record.clear();
2589 Record.push_back(SUBMODULE_CONFIG_MACRO);
2590 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2591 Mod->ConfigMacros[I]);
2592 }
2593
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002594 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002595 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2596 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002597 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002598 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002599 }
2600
2601 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002602
2603 assert((NextSubmoduleID - FirstSubmoduleID
2604 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002605}
2606
Douglas Gregor185dbd72011-12-01 02:07:58 +00002607serialization::SubmoduleID
2608ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002609 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002610 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002611
2612 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002613 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002614 Module *OwningMod
2615 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002616 if (!OwningMod)
2617 return 0;
2618
Douglas Gregore209e502011-12-06 01:10:29 +00002619 // Check whether this submodule is part of our own module.
2620 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002621 return 0;
2622
Douglas Gregore209e502011-12-06 01:10:29 +00002623 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002624}
2625
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002626void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2627 bool isModule) {
2628 // Make sure set diagnostic pragmas don't affect the translation unit that
2629 // imports the module.
2630 // FIXME: Make diagnostic pragma sections work properly with modules.
2631 if (isModule)
2632 return;
2633
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002634 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2635 DiagStateIDMap;
2636 unsigned CurrID = 0;
2637 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002638 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002639 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002640 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2641 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002642 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002643 if (point.Loc.isInvalid())
2644 continue;
2645
2646 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002647 unsigned &DiagStateID = DiagStateIDMap[point.State];
2648 Record.push_back(DiagStateID);
2649
2650 if (DiagStateID == 0) {
2651 DiagStateID = ++CurrID;
2652 for (DiagnosticsEngine::DiagState::const_iterator
2653 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2654 if (I->second.isPragma()) {
2655 Record.push_back(I->first);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002656 Record.push_back((unsigned)I->second.getSeverity());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002657 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002658 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002659 Record.push_back(-1); // mark the end of the diag/map pairs for this
2660 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002661 }
2662 }
2663
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002664 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002665 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002666}
2667
Anders Carlssonc8505782011-03-06 18:41:18 +00002668void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2669 if (CXXBaseSpecifiersOffsets.empty())
2670 return;
2671
2672 RecordData Record;
2673
2674 // Create a blob abbreviation for the C++ base specifiers offsets.
2675 using namespace llvm;
2676
2677 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2678 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2679 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2681 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2682
Douglas Gregore92b8a12011-08-04 00:01:48 +00002683 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002684 Record.clear();
2685 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2686 Record.push_back(CXXBaseSpecifiersOffsets.size());
2687 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002688 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002689}
2690
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002691//===----------------------------------------------------------------------===//
2692// Type Serialization
2693//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002694
Sebastian Redl3397c552010-08-18 23:56:27 +00002695/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002696void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002697 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002698 if (Idx.getIndex() == 0) // we haven't seen this type before.
2699 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002700
Douglas Gregor97475832010-10-05 18:37:06 +00002701 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002702
Douglas Gregor2cf26342009-04-09 22:27:44 +00002703 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002704 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002705 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002706 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002707 else if (TypeOffsets.size() < Index) {
2708 TypeOffsets.resize(Index + 1);
2709 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002710 }
2711
2712 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Douglas Gregor2cf26342009-04-09 22:27:44 +00002714 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002715 ASTTypeWriter W(*this, Record);
Stephen Hines176edba2014-12-01 14:53:08 -08002716 W.AbbrevToUse = 0;
John McCall0953e762009-09-24 19:53:00 +00002717
Douglas Gregora4923eb2009-11-16 21:35:15 +00002718 if (T.hasLocalNonFastQualifiers()) {
2719 Qualifiers Qs = T.getLocalQualifiers();
2720 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002721 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 W.Code = TYPE_EXT_QUAL;
Stephen Hines176edba2014-12-01 14:53:08 -08002723 W.AbbrevToUse = TypeExtQualAbbrev;
John McCall0953e762009-09-24 19:53:00 +00002724 } else {
2725 switch (T->getTypeClass()) {
2726 // For all of the concrete, non-dependent types, call the
2727 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002728#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002729 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002730#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002731#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002732 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002733 }
2734
2735 // Emit the serialized record.
Stephen Hines176edba2014-12-01 14:53:08 -08002736 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Douglas Gregor0b748912009-04-14 21:18:50 +00002737
2738 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002739 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002740}
2741
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002742//===----------------------------------------------------------------------===//
2743// Declaration Serialization
2744//===----------------------------------------------------------------------===//
2745
Douglas Gregor2cf26342009-04-09 22:27:44 +00002746/// \brief Write the block containing all of the declaration IDs
2747/// lexically declared within the given DeclContext.
2748///
2749/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2750/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002751uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002752 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002753 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002754 return 0;
2755
Douglas Gregorc9490c02009-04-16 22:23:12 +00002756 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002757 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002758 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002759 SmallVector<KindDeclIDPair, 64> Decls;
Stephen Hines651f13c2014-04-23 16:59:28 -07002760 for (const auto *D : DC->decls())
2761 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002762
Douglas Gregor25123082009-04-22 22:34:57 +00002763 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002764 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002765 return Offset;
2766}
2767
Sebastian Redla4232eb2010-08-18 23:56:21 +00002768void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002769 using namespace llvm;
2770 RecordData Record;
2771
2772 // Write the type offsets array
2773 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002774 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2778 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2779 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002780 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002781 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002782 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002783 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002784
2785 // Write the declaration offsets array
2786 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002787 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2791 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2792 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002793 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002794 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002795 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002796 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002797}
2798
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002799void ASTWriter::WriteFileDeclIDsMap() {
2800 using namespace llvm;
2801 RecordData Record;
2802
2803 // Join the vectors of DeclIDs from all files.
2804 SmallVector<DeclID, 256> FileSortedIDs;
2805 for (FileDeclIDsTy::iterator
2806 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2807 DeclIDInFileInfo &Info = *FI->second;
2808 Info.FirstDeclIndex = FileSortedIDs.size();
2809 for (LocDeclIDsTy::iterator
2810 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2811 FileSortedIDs.push_back(DI->second);
2812 }
2813
2814 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2815 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2818 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2819 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002820 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002821 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2822}
2823
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002824void ASTWriter::WriteComments() {
2825 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002826 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002827 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002828 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2829 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002830 I != E; ++I) {
2831 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002832 AddSourceRange((*I)->getSourceRange(), Record);
2833 Record.push_back((*I)->getKind());
2834 Record.push_back((*I)->isTrailingComment());
2835 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002836 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2837 }
2838 Stream.ExitBlock();
2839}
2840
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002841//===----------------------------------------------------------------------===//
2842// Global Method Pool and Selector Serialization
2843//===----------------------------------------------------------------------===//
2844
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002845namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002846// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002847class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002848 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002849
2850public:
2851 typedef Selector key_type;
2852 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002853
Sebastian Redl5d050072010-08-04 17:20:04 +00002854 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002855 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002856 ObjCMethodList Instance, Factory;
2857 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002858 typedef const data_type& data_type_ref;
2859
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002860 typedef unsigned hash_value_type;
2861 typedef unsigned offset_type;
2862
Sebastian Redl3397c552010-08-18 23:56:27 +00002863 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002864
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002865 static hash_value_type ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002866 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002867 }
Mike Stump1eb44332009-09-09 15:08:12 +00002868
2869 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002870 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002871 data_type_ref Methods) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002872 using namespace llvm::support;
2873 endian::Writer<little> LE(Out);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002874 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Stephen Hines651f13c2014-04-23 16:59:28 -07002875 LE.write<uint16_t>(KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002876 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2877 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002878 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002879 if (Method->Method)
2880 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002881 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002882 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002883 if (Method->Method)
2884 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07002885 LE.write<uint16_t>(DataLen);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002886 return std::make_pair(KeyLen, DataLen);
2887 }
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Chris Lattner5f9e2722011-07-23 10:55:15 +00002889 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002890 using namespace llvm::support;
2891 endian::Writer<little> LE(Out);
Mike Stump1eb44332009-09-09 15:08:12 +00002892 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002893 assert((Start >> 32) == 0 && "Selector key offset too large");
2894 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002895 unsigned N = Sel.getNumArgs();
Stephen Hines651f13c2014-04-23 16:59:28 -07002896 LE.write<uint16_t>(N);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002897 if (N == 0)
2898 N = 1;
2899 for (unsigned I = 0; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07002900 LE.write<uint32_t>(
2901 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002902 }
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Chris Lattner5f9e2722011-07-23 10:55:15 +00002904 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002905 data_type_ref Methods, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002906 using namespace llvm::support;
2907 endian::Writer<little> LE(Out);
Douglas Gregora67e58c2009-04-24 21:49:02 +00002908 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07002909 LE.write<uint32_t>(Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002910 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002911 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002912 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002913 if (Method->Method)
2914 ++NumInstanceMethods;
2915
2916 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002917 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002918 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002919 if (Method->Method)
2920 ++NumFactoryMethods;
2921
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002922 unsigned InstanceBits = Methods.Instance.getBits();
2923 assert(InstanceBits < 4);
2924 unsigned NumInstanceMethodsAndBits =
2925 (NumInstanceMethods << 2) | InstanceBits;
2926 unsigned FactoryBits = Methods.Factory.getBits();
2927 assert(FactoryBits < 4);
2928 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Stephen Hines651f13c2014-04-23 16:59:28 -07002929 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2930 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002931 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002932 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002933 if (Method->Method)
Stephen Hines651f13c2014-04-23 16:59:28 -07002934 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002935 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002936 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002937 if (Method->Method)
Stephen Hines651f13c2014-04-23 16:59:28 -07002938 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002939
2940 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002941 }
2942};
2943} // end anonymous namespace
2944
Sebastian Redl059612d2010-08-03 21:58:15 +00002945/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002946///
2947/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002948/// in an on-disk hash table indexed by the selector. The hash table also
2949/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002950void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002951 using namespace llvm;
2952
Sebastian Redl059612d2010-08-03 21:58:15 +00002953 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002954 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002955 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002956 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002957 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002958 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002959 llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002960 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002961
Sebastian Redl059612d2010-08-03 21:58:15 +00002962 // Create the on-disk hash table representation. We walk through every
2963 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002964 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002965 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002966 I = SelectorIDs.begin(), E = SelectorIDs.end();
2967 I != E; ++I) {
2968 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002969 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002970 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002971 I->second,
2972 ObjCMethodList(),
2973 ObjCMethodList()
2974 };
2975 if (F != SemaRef.MethodPool.end()) {
2976 Data.Instance = F->second.first;
2977 Data.Factory = F->second.second;
2978 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002979 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002980 // changed.
2981 if (Chain && I->second < FirstSelectorID) {
2982 // Selector already exists. Did it change?
2983 bool changed = false;
2984 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002985 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002986 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002987 changed = true;
2988 }
2989 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002990 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002991 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002992 changed = true;
2993 }
2994 if (!changed)
2995 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002996 } else if (Data.Instance.Method || Data.Factory.Method) {
2997 // A new method pool entry.
2998 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002999 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003000 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003001 }
3002
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003003 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003004 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003005 uint32_t BucketOffset;
3006 {
Stephen Hines651f13c2014-04-23 16:59:28 -07003007 using namespace llvm::support;
Sebastian Redl3397c552010-08-18 23:56:27 +00003008 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003009 llvm::raw_svector_ostream Out(MethodPool);
3010 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07003011 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003012 BucketOffset = Generator.Emit(Out, Trait);
3013 }
3014
3015 // Create a blob abbreviation
3016 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003017 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00003019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3021 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3022
Douglas Gregor83941df2009-04-25 17:48:32 +00003023 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003024 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003025 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003026 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00003027 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003028 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00003029
3030 // Create a blob abbreviation for the selector table offsets.
3031 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003032 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00003033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00003035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3036 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3037
3038 // Write the selector offsets table.
3039 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003040 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00003041 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003042 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00003043 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003044 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003045 }
3046}
3047
Sebastian Redl3397c552010-08-18 23:56:27 +00003048/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003049void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00003050 using namespace llvm;
3051 if (SemaRef.ReferencedSelectors.empty())
3052 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00003053
Fariborz Jahanian32019832010-07-23 19:11:11 +00003054 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00003055
Sebastian Redl3397c552010-08-18 23:56:27 +00003056 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00003057 // very tricky to fix, and given that @selector shouldn't really appear in
3058 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00003059 for (DenseMap<Selector, SourceLocation>::iterator S =
3060 SemaRef.ReferencedSelectors.begin(),
3061 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
3062 Selector Sel = (*S).first;
3063 SourceLocation Loc = (*S).second;
3064 AddSelectorRef(Sel, Record);
3065 AddSourceLocation(Loc, Record);
3066 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003067 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003068}
3069
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003070//===----------------------------------------------------------------------===//
3071// Identifier Table Serialization
3072//===----------------------------------------------------------------------===//
3073
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003074namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00003075class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00003076 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00003077 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003078 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003079 bool IsModule;
3080
Douglas Gregora92193e2009-04-28 21:18:29 +00003081 /// \brief Determines whether this is an "interesting" identifier
3082 /// that needs a full IdentifierInfo structure written into the hash
3083 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003084 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00003085 if (II->isPoisoned() ||
3086 II->isExtensionToken() ||
3087 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00003088 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00003089 II->getFETokenInfo<void>())
3090 return true;
3091
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003092 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00003093 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003094
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003095 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003096 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00003097 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003098
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003099 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
3100 if (!IsModule)
3101 return !shouldIgnoreMacro(Macro, IsModule, PP);
Stephen Hines176edba2014-12-01 14:53:08 -08003102
3103 MacroState State;
3104 if (getFirstPublicSubmoduleMacro(Macro, State))
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003105 return true;
3106 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003107
3108 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00003109 }
3110
Stephen Hines176edba2014-12-01 14:53:08 -08003111 enum class SubmoduleMacroState {
3112 /// We've seen nothing about this macro.
3113 None,
3114 /// We've seen a public visibility directive.
3115 Public,
3116 /// We've either exported a macro for this module or found that the
3117 /// module's definition of this macro is private.
3118 Done
3119 };
3120 typedef llvm::DenseMap<SubmoduleID, SubmoduleMacroState> MacroState;
Stephen Hines651f13c2014-04-23 16:59:28 -07003121
3122 MacroDirective *
Stephen Hines176edba2014-12-01 14:53:08 -08003123 getFirstPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
3124 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, State))
3125 return NextMD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003126 return nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003127 }
3128
Stephen Hines651f13c2014-04-23 16:59:28 -07003129 MacroDirective *
Stephen Hines176edba2014-12-01 14:53:08 -08003130 getNextPublicSubmoduleMacro(MacroDirective *MD, MacroState &State) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003131 if (MacroDirective *NextMD =
Stephen Hines176edba2014-12-01 14:53:08 -08003132 getPublicSubmoduleMacro(MD->getPrevious(), State))
3133 return NextMD;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003134 return nullptr;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003135 }
3136
Stephen Hines176edba2014-12-01 14:53:08 -08003137 /// \brief Traverses the macro directives history and returns the next
3138 /// public macro definition or undefinition that has not been found so far.
3139 ///
Stephen Hines651f13c2014-04-23 16:59:28 -07003140 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003141 /// will still be considered as defined/exported from submodule A.
Stephen Hines651f13c2014-04-23 16:59:28 -07003142 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
Stephen Hines176edba2014-12-01 14:53:08 -08003143 MacroState &State) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003144 if (!MD)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003145 return nullptr;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003146
Stephen Hines651f13c2014-04-23 16:59:28 -07003147 Optional<bool> IsPublic;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003148 for (; MD; MD = MD->getPrevious()) {
Stephen Hines176edba2014-12-01 14:53:08 -08003149 // Once we hit an ignored macro, we're done: the rest of the chain
3150 // will all be ignored macros.
3151 if (shouldIgnoreMacro(MD, IsModule, PP))
3152 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07003153
Stephen Hines176edba2014-12-01 14:53:08 -08003154 // If this macro was imported, re-export it.
3155 if (MD->isImported())
3156 return MD;
Stephen Hines651f13c2014-04-23 16:59:28 -07003157
Stephen Hines176edba2014-12-01 14:53:08 -08003158 SubmoduleID ModID = getSubmoduleID(MD);
3159 auto &S = State[ModID];
3160 assert(ModID && "found macro in no submodule");
3161
3162 if (S == SubmoduleMacroState::Done)
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003163 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003164
Stephen Hines176edba2014-12-01 14:53:08 -08003165 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
3166 // The latest visibility directive for a name in a submodule affects all
3167 // the directives that come before it.
3168 if (S == SubmoduleMacroState::None)
3169 S = VisMD->isPublic() ? SubmoduleMacroState::Public
3170 : SubmoduleMacroState::Done;
3171 } else {
3172 S = SubmoduleMacroState::Done;
Stephen Hines651f13c2014-04-23 16:59:28 -07003173 return MD;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003174 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003175 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003176
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003177 return nullptr;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003178 }
3179
Stephen Hines176edba2014-12-01 14:53:08 -08003180 ArrayRef<SubmoduleID>
3181 getOverriddenSubmodules(MacroDirective *MD,
3182 SmallVectorImpl<SubmoduleID> &ScratchSpace) {
3183 assert(!isa<VisibilityMacroDirective>(MD) &&
3184 "only #define and #undef can override");
3185 if (MD->isImported())
3186 return MD->getOverriddenModules();
3187
3188 ScratchSpace.clear();
3189 SubmoduleID ModID = getSubmoduleID(MD);
3190 for (MD = MD->getPrevious(); MD; MD = MD->getPrevious()) {
3191 if (shouldIgnoreMacro(MD, IsModule, PP))
3192 break;
3193
3194 // If this is a definition from a submodule import, that submodule's
3195 // definition is overridden by the definition or undefinition that we
3196 // started with.
3197 if (MD->isImported()) {
3198 if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3199 SubmoduleID DefModuleID = DefMD->getInfo()->getOwningModuleID();
3200 assert(DefModuleID && "imported macro has no owning module");
3201 ScratchSpace.push_back(DefModuleID);
3202 } else if (auto *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
3203 // If we override a #undef, we override anything that #undef overrides.
3204 // We don't need to override it, since an active #undef doesn't affect
3205 // the meaning of a macro.
3206 auto Overrides = UndefMD->getOverriddenModules();
3207 ScratchSpace.insert(ScratchSpace.end(),
3208 Overrides.begin(), Overrides.end());
3209 }
3210 }
3211
3212 // Stop once we leave the original macro's submodule.
3213 //
3214 // Either this submodule #included another submodule of the same
3215 // module or it just happened to be built after the other module.
3216 // In the former case, we override the submodule's macro.
3217 //
3218 // FIXME: In the latter case, we shouldn't do so, but we can't tell
3219 // these cases apart.
3220 //
3221 // FIXME: We can leave this submodule and re-enter it if it #includes a
3222 // header within a different submodule of the same module. In such cases
3223 // the overrides list will be incomplete.
3224 SubmoduleID DirectiveModuleID = getSubmoduleID(MD);
3225 if (DirectiveModuleID != ModID) {
3226 if (DirectiveModuleID && !MD->isImported())
3227 ScratchSpace.push_back(DirectiveModuleID);
3228 break;
3229 }
3230 }
3231
3232 std::sort(ScratchSpace.begin(), ScratchSpace.end());
3233 ScratchSpace.erase(std::unique(ScratchSpace.begin(), ScratchSpace.end()),
3234 ScratchSpace.end());
3235 return ScratchSpace;
3236 }
3237
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003238 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003239 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003240 }
3241
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003242public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003243 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003244 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003246 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003247 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003249 typedef unsigned hash_value_type;
3250 typedef unsigned offset_type;
3251
Douglas Gregoreee242f2011-10-27 09:33:13 +00003252 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3253 IdentifierResolver &IdResolver, bool IsModule)
3254 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003255
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003256 static hash_value_type ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003257 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003258 }
Mike Stump1eb44332009-09-09 15:08:12 +00003259
3260 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003261 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003262 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003263 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003264 MacroDirective *Macro = nullptr;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003265 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003266 DataLen += 2; // 2 bytes for builtin ID
3267 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003268 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003269 DataLen += 4; // MacroDirectives offset.
3270 if (IsModule) {
Stephen Hines176edba2014-12-01 14:53:08 -08003271 MacroState State;
3272 SmallVector<SubmoduleID, 16> Scratch;
3273 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3274 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003275 DataLen += 4; // MacroInfo ID or ModuleID.
Stephen Hines176edba2014-12-01 14:53:08 -08003276 if (unsigned NumOverrides =
3277 getOverriddenSubmodules(MD, Scratch).size())
3278 DataLen += 4 * (1 + NumOverrides);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003279 }
Stephen Hines176edba2014-12-01 14:53:08 -08003280 DataLen += 4; // 0 terminator.
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003281 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003282 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003283
Douglas Gregoreee242f2011-10-27 09:33:13 +00003284 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3285 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003286 D != DEnd; ++D)
Stephen Hines176edba2014-12-01 14:53:08 -08003287 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00003288 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003289 using namespace llvm::support;
3290 endian::Writer<little> LE(Out);
3291
3292 LE.write<uint16_t>(DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003293 // We emit the key length after the data length so that every
3294 // string is preceded by a 16-bit length. This matches the PTH
3295 // format for storing identifiers.
Stephen Hines651f13c2014-04-23 16:59:28 -07003296 LE.write<uint16_t>(KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003297 return std::make_pair(KeyLen, DataLen);
3298 }
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Chris Lattner5f9e2722011-07-23 10:55:15 +00003300 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003301 unsigned KeyLen) {
3302 // Record the location of the key data. This is used when generating
3303 // the mapping from persistent IDs to strings.
3304 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003305 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003306 }
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Stephen Hines651f13c2014-04-23 16:59:28 -07003308 static void emitMacroOverrides(raw_ostream &Out,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07003309 ArrayRef<SubmoduleID> Overridden) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003310 if (!Overridden.empty()) {
3311 using namespace llvm::support;
3312 endian::Writer<little> LE(Out);
3313 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
Stephen Hines176edba2014-12-01 14:53:08 -08003314 for (unsigned I = 0, N = Overridden.size(); I != N; ++I) {
3315 assert(Overridden[I] && "zero module ID for override");
Stephen Hines651f13c2014-04-23 16:59:28 -07003316 LE.write<uint32_t>(Overridden[I]);
Stephen Hines176edba2014-12-01 14:53:08 -08003317 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003318 }
3319 }
3320
Douglas Gregor7143aab2011-09-01 17:04:32 +00003321 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003322 IdentID ID, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003323 using namespace llvm::support;
3324 endian::Writer<little> LE(Out);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003325 MacroDirective *Macro = nullptr;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003326 if (!isInterestingIdentifier(II, Macro)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003327 LE.write<uint32_t>(ID << 1);
Douglas Gregora92193e2009-04-28 21:18:29 +00003328 return;
3329 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003330
Stephen Hines651f13c2014-04-23 16:59:28 -07003331 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003332 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3333 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Stephen Hines651f13c2014-04-23 16:59:28 -07003334 LE.write<uint16_t>(Bits);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003335 Bits = 0;
3336 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003337 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003338 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003339 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3340 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003341 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003342 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Stephen Hines651f13c2014-04-23 16:59:28 -07003343 LE.write<uint16_t>(Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003344
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003345 if (HadMacroDefinition) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003346 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003347 if (IsModule) {
3348 // Write the IDs of macros coming from different submodules.
Stephen Hines176edba2014-12-01 14:53:08 -08003349 MacroState State;
3350 SmallVector<SubmoduleID, 16> Scratch;
3351 for (MacroDirective *MD = getFirstPublicSubmoduleMacro(Macro, State);
3352 MD; MD = getNextPublicSubmoduleMacro(MD, State)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003353 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
Stephen Hines176edba2014-12-01 14:53:08 -08003354 // FIXME: If this macro directive was created by #pragma pop_macros,
3355 // or if it was created implicitly by resolving conflicting macros,
3356 // it may be for a different submodule from the one in the MacroInfo
3357 // object. If so, we should write out its owning ModuleID.
3358 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Stephen Hines651f13c2014-04-23 16:59:28 -07003359 assert(InfoID);
3360 LE.write<uint32_t>(InfoID << 1);
3361 } else {
Stephen Hines176edba2014-12-01 14:53:08 -08003362 auto *UndefMD = cast<UndefMacroDirective>(MD);
3363 SubmoduleID Mod = UndefMD->isImported()
3364 ? UndefMD->getOwningModuleID()
3365 : getSubmoduleID(UndefMD);
3366 LE.write<uint32_t>((Mod << 1) | 1);
Stephen Hines651f13c2014-04-23 16:59:28 -07003367 }
Stephen Hines176edba2014-12-01 14:53:08 -08003368 emitMacroOverrides(Out, getOverriddenSubmodules(MD, Scratch));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003369 }
Stephen Hines176edba2014-12-01 14:53:08 -08003370 LE.write<uint32_t>(0xdeadbeef);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003371 }
Douglas Gregor13292642011-12-02 15:45:10 +00003372 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003373
Douglas Gregor668c1a42009-04-21 22:25:48 +00003374 // Emit the declaration IDs in reverse order, because the
3375 // IdentifierResolver provides the declarations as they would be
3376 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003377 // "stat"), but the ASTReader adds declarations to the end of the list
3378 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003379 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003380 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3381 IdResolver.end());
Craig Topper09d19ef2013-07-04 03:08:24 +00003382 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003383 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003384 D != DEnd; ++D)
Stephen Hines651f13c2014-04-23 16:59:28 -07003385 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003386 }
3387
3388 /// \brief Returns the most recent local decl or the given decl if there are
3389 /// no local ones. The given decl is assumed to be the most recent one.
3390 Decl *getMostRecentLocalDecl(Decl *Orig) {
3391 // The only way a "from AST file" decl would be more recent from a local one
3392 // is if it came from a module.
3393 if (!PP.getLangOpts().Modules)
3394 return Orig;
3395
3396 // Look for a local in the decl chain.
3397 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3398 if (!D->isFromASTFile())
3399 return D;
3400 // If we come up a decl from a (chained-)PCH stop since we won't find a
3401 // local one.
3402 if (D->getOwningModuleID() == 0)
3403 break;
3404 }
3405
3406 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003407 }
3408};
3409} // end anonymous namespace
3410
Sebastian Redl3397c552010-08-18 23:56:27 +00003411/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003412///
3413/// The identifier table consists of a blob containing string data
3414/// (the actual identifiers themselves) and a separate "offsets" index
3415/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003416void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3417 IdentifierResolver &IdResolver,
3418 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003419 using namespace llvm;
3420
3421 // Create and write out the blob that contains the identifier
3422 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003423 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003424 llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003425 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003426
Douglas Gregor92b059e2009-04-28 20:33:11 +00003427 // Look for any identifiers that were named while processing the
3428 // headers, but are otherwise not needed. We add these to the hash
3429 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003430 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003431 // file.
3432 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3433 IDEnd = PP.getIdentifierTable().end();
3434 ID != IDEnd; ++ID)
3435 getIdentifierRef(ID->second);
3436
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003437 // Create the on-disk hash table representation. We only store offsets
3438 // for identifiers that appear here for the first time.
3439 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003440 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003441 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3442 ID != IDEnd; ++ID) {
3443 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003444 if (!Chain || !ID->first->isFromAST() ||
3445 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003446 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003447 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003448 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003449
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003450 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003451 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003452 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003453 {
Stephen Hines651f13c2014-04-23 16:59:28 -07003454 using namespace llvm::support;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003455 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003456 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003457 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07003458 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003459 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003460 }
3461
3462 // Create a blob abbreviation
3463 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003464 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003465 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003466 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003467 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003468
3469 // Write the identifier table
3470 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003471 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003472 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003473 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003474 }
3475
3476 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003477 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003478 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003481 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3482 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3483
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003484#ifndef NDEBUG
3485 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3486 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3487#endif
3488
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003489 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003490 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003491 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003492 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003493 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003494 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003495}
3496
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003497//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003498// DeclContext's Name Lookup Table Serialization
3499//===----------------------------------------------------------------------===//
3500
Stephen Hines176edba2014-12-01 14:53:08 -08003501/// Determine the declaration that should be put into the name lookup table to
3502/// represent the given declaration in this module. This is usually D itself,
3503/// but if D was imported and merged into a local declaration, we want the most
3504/// recent local declaration instead. The chosen declaration will be the most
3505/// recent declaration in any module that imports this one.
3506static NamedDecl *getDeclForLocalLookup(NamedDecl *D) {
3507 if (!D->isFromASTFile())
3508 return D;
3509
3510 if (Decl *Redecl = D->getPreviousDecl()) {
3511 // For Redeclarable decls, a prior declaration might be local.
3512 for (; Redecl; Redecl = Redecl->getPreviousDecl())
3513 if (!Redecl->isFromASTFile())
3514 return cast<NamedDecl>(Redecl);
3515 } else if (Decl *First = D->getCanonicalDecl()) {
3516 // For Mergeable decls, the first decl might be local.
3517 if (!First->isFromASTFile())
3518 return cast<NamedDecl>(First);
3519 }
3520
3521 // All declarations are imported. Our most recent declaration will also be
3522 // the most recent one in anyone who imports us.
3523 return D;
3524}
3525
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003526namespace {
3527// Trait used for the on-disk hash table used in the method pool.
3528class ASTDeclContextNameLookupTrait {
3529 ASTWriter &Writer;
3530
3531public:
3532 typedef DeclarationName key_type;
3533 typedef key_type key_type_ref;
3534
3535 typedef DeclContext::lookup_result data_type;
3536 typedef const data_type& data_type_ref;
3537
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003538 typedef unsigned hash_value_type;
3539 typedef unsigned offset_type;
3540
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003541 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3542
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003543 hash_value_type ComputeHash(DeclarationName Name) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003544 llvm::FoldingSetNodeID ID;
3545 ID.AddInteger(Name.getNameKind());
3546
3547 switch (Name.getNameKind()) {
3548 case DeclarationName::Identifier:
3549 ID.AddString(Name.getAsIdentifierInfo()->getName());
3550 break;
3551 case DeclarationName::ObjCZeroArgSelector:
3552 case DeclarationName::ObjCOneArgSelector:
3553 case DeclarationName::ObjCMultiArgSelector:
3554 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3555 break;
3556 case DeclarationName::CXXConstructorName:
3557 case DeclarationName::CXXDestructorName:
3558 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003559 break;
3560 case DeclarationName::CXXOperatorName:
3561 ID.AddInteger(Name.getCXXOverloadedOperator());
3562 break;
3563 case DeclarationName::CXXLiteralOperatorName:
3564 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3565 case DeclarationName::CXXUsingDirective:
3566 break;
3567 }
3568
3569 return ID.ComputeHash();
3570 }
3571
3572 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003573 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003574 data_type_ref Lookup) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003575 using namespace llvm::support;
3576 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003577 unsigned KeyLen = 1;
3578 switch (Name.getNameKind()) {
3579 case DeclarationName::Identifier:
3580 case DeclarationName::ObjCZeroArgSelector:
3581 case DeclarationName::ObjCOneArgSelector:
3582 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003583 case DeclarationName::CXXLiteralOperatorName:
3584 KeyLen += 4;
3585 break;
3586 case DeclarationName::CXXOperatorName:
3587 KeyLen += 1;
3588 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003589 case DeclarationName::CXXConstructorName:
3590 case DeclarationName::CXXDestructorName:
3591 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003592 case DeclarationName::CXXUsingDirective:
3593 break;
3594 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003595 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003596
3597 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003598 unsigned DataLen = 2 + 4 * Lookup.size();
Stephen Hines651f13c2014-04-23 16:59:28 -07003599 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003600
3601 return std::make_pair(KeyLen, DataLen);
3602 }
3603
Chris Lattner5f9e2722011-07-23 10:55:15 +00003604 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003605 using namespace llvm::support;
3606 endian::Writer<little> LE(Out);
3607 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003608 switch (Name.getNameKind()) {
3609 case DeclarationName::Identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -07003610 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003611 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003612 case DeclarationName::ObjCZeroArgSelector:
3613 case DeclarationName::ObjCOneArgSelector:
3614 case DeclarationName::ObjCMultiArgSelector:
Stephen Hines651f13c2014-04-23 16:59:28 -07003615 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003616 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003617 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003618 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3619 "Invalid operator?");
Stephen Hines651f13c2014-04-23 16:59:28 -07003620 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003621 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003622 case DeclarationName::CXXLiteralOperatorName:
Stephen Hines651f13c2014-04-23 16:59:28 -07003623 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003624 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003625 case DeclarationName::CXXConstructorName:
3626 case DeclarationName::CXXDestructorName:
3627 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003628 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003629 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003630 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003631
3632 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003633 }
3634
Chris Lattner5f9e2722011-07-23 10:55:15 +00003635 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003636 data_type Lookup, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003637 using namespace llvm::support;
3638 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003639 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07003640 LE.write<uint16_t>(Lookup.size());
David Blaikie3bc93e32012-12-19 00:45:41 +00003641 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3642 I != E; ++I)
Stephen Hines176edba2014-12-01 14:53:08 -08003643 LE.write<uint32_t>(Writer.GetDeclRef(getDeclForLocalLookup(*I)));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003644
3645 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3646 }
3647};
3648} // end anonymous namespace
3649
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003650template<typename Visitor>
3651static void visitLocalLookupResults(const DeclContext *ConstDC,
3652 bool NeedToReconcileExternalVisibleStorage,
3653 Visitor AddLookupResult) {
3654 // FIXME: We need to build the lookups table, which is logically const.
3655 DeclContext *DC = const_cast<DeclContext*>(ConstDC);
Stephen Hines651f13c2014-04-23 16:59:28 -07003656 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3657
Stephen Hines651f13c2014-04-23 16:59:28 -07003658 SmallVector<DeclarationName, 16> ExternalNames;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003659 for (auto &Lookup : *DC->buildLookup()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003660 if (Lookup.second.hasExternalDecls() ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003661 NeedToReconcileExternalVisibleStorage) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003662 // We don't know for sure what declarations are found by this name,
3663 // because the external source might have a different set from the set
3664 // that are in the lookup map, and we can't update it now without
3665 // risking invalidating our lookup iterator. So add it to a queue to
3666 // deal with later.
3667 ExternalNames.push_back(Lookup.first);
3668 continue;
3669 }
3670
3671 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3672 }
3673
3674 // Add the names we needed to defer. Note, this shouldn't add any new decls
3675 // to the list we need to serialize: any new declarations we find here should
3676 // be imported from an external source.
3677 // FIXME: What if the external source isn't an ASTReader?
3678 for (const auto &Name : ExternalNames)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003679 AddLookupResult(Name, DC->lookup(Name));
3680}
3681
3682void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) {
Stephen Hines176edba2014-12-01 14:53:08 -08003683 if (UpdatedDeclContexts.insert(DC).second && WritingAST) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003684 // Ensure we emit all the visible declarations.
3685 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3686 [&](DeclarationName Name,
3687 DeclContext::lookup_const_result Result) {
3688 for (auto *Decl : Result)
Stephen Hines176edba2014-12-01 14:53:08 -08003689 GetDeclRef(getDeclForLocalLookup(Decl));
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003690 });
3691 }
3692}
3693
3694uint32_t
3695ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3696 llvm::SmallVectorImpl<char> &LookupTable) {
3697 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3698
3699 llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait>
3700 Generator;
3701 ASTDeclContextNameLookupTrait Trait(*this);
3702
3703 // Create the on-disk hash table representation.
3704 DeclarationName ConstructorName;
3705 DeclarationName ConversionName;
3706 SmallVector<NamedDecl *, 8> ConstructorDecls;
3707 SmallVector<NamedDecl *, 4> ConversionDecls;
3708
3709 visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage,
3710 [&](DeclarationName Name,
3711 DeclContext::lookup_result Result) {
3712 if (Result.empty())
3713 return;
3714
3715 // Different DeclarationName values of certain kinds are mapped to
3716 // identical serialized keys, because we don't want to use type
3717 // identifiers in the keys (since type ids are local to the module).
3718 switch (Name.getNameKind()) {
3719 case DeclarationName::CXXConstructorName:
3720 // There may be different CXXConstructorName DeclarationName values
3721 // in a DeclContext because a UsingDecl that inherits constructors
3722 // has the DeclarationName of the inherited constructors.
3723 if (!ConstructorName)
3724 ConstructorName = Name;
3725 ConstructorDecls.append(Result.begin(), Result.end());
3726 return;
3727
3728 case DeclarationName::CXXConversionFunctionName:
3729 if (!ConversionName)
3730 ConversionName = Name;
3731 ConversionDecls.append(Result.begin(), Result.end());
3732 return;
3733
3734 default:
3735 break;
3736 }
3737
3738 Generator.insert(Name, Result, Trait);
3739 });
Stephen Hines651f13c2014-04-23 16:59:28 -07003740
3741 // Add the constructors.
3742 if (!ConstructorDecls.empty()) {
3743 Generator.insert(ConstructorName,
3744 DeclContext::lookup_result(ConstructorDecls.begin(),
3745 ConstructorDecls.end()),
3746 Trait);
3747 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003748
Stephen Hines651f13c2014-04-23 16:59:28 -07003749 // Add the conversion functions.
3750 if (!ConversionDecls.empty()) {
3751 Generator.insert(ConversionName,
3752 DeclContext::lookup_result(ConversionDecls.begin(),
3753 ConversionDecls.end()),
3754 Trait);
3755 }
3756
3757 // Create the on-disk hash table in a buffer.
3758 llvm::raw_svector_ostream Out(LookupTable);
3759 // Make sure that no bucket is at offset 0
3760 using namespace llvm::support;
3761 endian::Writer<little>(Out).write<uint32_t>(0);
3762 return Generator.Emit(Out, Trait);
3763}
3764
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003765/// \brief Write the block containing all of the declaration IDs
3766/// visible from the given DeclContext.
3767///
3768/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003769/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003770uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3771 DeclContext *DC) {
3772 if (DC->getPrimaryContext() != DC)
3773 return 0;
3774
3775 // Since there is no name lookup into functions or methods, don't bother to
3776 // build a visible-declarations table for these entities.
3777 if (DC->isFunctionOrMethod())
3778 return 0;
3779
3780 // If not in C++, we perform name lookup for the translation unit via the
3781 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003782 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003783 return 0;
3784
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003785 // Serialize the contents of the mapping used for lookup. Note that,
3786 // although we have two very different code paths, the serialized
3787 // representation is the same for both cases: a declaration name,
3788 // followed by a size, followed by references to the visible
3789 // declarations that have that name.
3790 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003791 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003792 if (!Map || Map->empty())
3793 return 0;
3794
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003795 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003796 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003797 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003798
3799 // Write the lookup table
3800 RecordData Record;
3801 Record.push_back(DECL_CONTEXT_VISIBLE);
3802 Record.push_back(BucketOffset);
3803 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3804 LookupTable.str());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003805 ++NumVisibleDeclContexts;
3806 return Offset;
3807}
3808
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003809/// \brief Write an UPDATE_VISIBLE block for the given context.
3810///
3811/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3812/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003813/// (in C++), for namespaces, and for classes with forward-declared unscoped
3814/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003815void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003816 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003817 if (!Map || Map->empty())
3818 return;
3819
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003820 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003821 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003822 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003823
3824 // Write the lookup table
3825 RecordData Record;
3826 Record.push_back(UPDATE_VISIBLE);
3827 Record.push_back(getDeclID(cast<Decl>(DC)));
3828 Record.push_back(BucketOffset);
3829 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3830}
3831
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003832/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3833void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3834 RecordData Record;
3835 Record.push_back(Opts.fp_contract);
3836 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3837}
3838
3839/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3840void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003841 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003842 return;
3843
3844 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3845 RecordData Record;
3846#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3847#include "clang/Basic/OpenCLExtensions.def"
3848 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3849}
3850
Douglas Gregor2171bf12012-01-15 16:58:34 +00003851void ASTWriter::WriteRedeclarations() {
3852 RecordData LocalRedeclChains;
3853 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3854
3855 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3856 Decl *First = Redeclarations[I];
Rafael Espindola7693b322013-10-19 02:13:21 +00003857 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor2171bf12012-01-15 16:58:34 +00003858
3859 Decl *MostRecent = First->getMostRecentDecl();
3860
3861 // If we only have a single declaration, there is no point in storing
3862 // a redeclaration chain.
3863 if (First == MostRecent)
3864 continue;
3865
3866 unsigned Offset = LocalRedeclChains.size();
3867 unsigned Size = 0;
3868 LocalRedeclChains.push_back(0); // Placeholder for the size.
3869
3870 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003871 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003872 Prev = Prev->getPreviousDecl()) {
3873 if (!Prev->isFromASTFile()) {
3874 AddDeclRef(Prev, LocalRedeclChains);
3875 ++Size;
3876 }
3877 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003878
3879 if (!First->isFromASTFile() && Chain) {
3880 Decl *FirstFromAST = MostRecent;
3881 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3882 if (Prev->isFromASTFile())
3883 FirstFromAST = Prev;
3884 }
3885
Stephen Hines176edba2014-12-01 14:53:08 -08003886 // FIXME: Do we need to do this for the first declaration from each
3887 // redeclaration chain that was merged into this one?
Douglas Gregoraa945902013-02-18 15:53:43 +00003888 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3889 }
3890
Douglas Gregor2171bf12012-01-15 16:58:34 +00003891 LocalRedeclChains[Offset] = Size;
3892
3893 // Reverse the set of local redeclarations, so that we store them in
3894 // order (since we found them in reverse order).
3895 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3896
Douglas Gregoraa945902013-02-18 15:53:43 +00003897 // Add the mapping from the first ID from the AST to the set of local
3898 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003899 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3900 LocalRedeclsMap.push_back(Info);
3901
3902 assert(N == Redeclarations.size() &&
3903 "Deserialized a declaration we shouldn't have");
3904 }
3905
3906 if (LocalRedeclChains.empty())
3907 return;
3908
3909 // Sort the local redeclarations map by the first declaration ID,
3910 // since the reader will be performing binary searches on this information.
3911 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3912
3913 // Emit the local redeclarations map.
3914 using namespace llvm;
3915 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3916 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3919 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3920
3921 RecordData Record;
3922 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3923 Record.push_back(LocalRedeclsMap.size());
3924 Stream.EmitRecordWithBlob(AbbrevID, Record,
3925 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3926 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3927
3928 // Emit the redeclaration chains.
3929 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3930}
3931
Douglas Gregorcff9f262012-01-27 01:47:08 +00003932void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003933 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003934 RecordData Categories;
3935
3936 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3937 unsigned Size = 0;
3938 unsigned StartIndex = Categories.size();
3939
3940 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3941
3942 // Allocate space for the size.
3943 Categories.push_back(0);
3944
3945 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003946 for (ObjCInterfaceDecl::known_categories_iterator
3947 Cat = Class->known_categories_begin(),
3948 CatEnd = Class->known_categories_end();
3949 Cat != CatEnd; ++Cat, ++Size) {
3950 assert(getDeclID(*Cat) != 0 && "Bogus category");
3951 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003952 }
3953
3954 // Update the size.
3955 Categories[StartIndex] = Size;
3956
3957 // Record this interface -> category map.
3958 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3959 CategoriesMap.push_back(CatInfo);
3960 }
3961
3962 // Sort the categories map by the definition ID, since the reader will be
3963 // performing binary searches on this information.
3964 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3965
3966 // Emit the categories map.
3967 using namespace llvm;
3968 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3969 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3972 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3973
3974 RecordData Record;
3975 Record.push_back(OBJC_CATEGORIES_MAP);
3976 Record.push_back(CategoriesMap.size());
3977 Stream.EmitRecordWithBlob(AbbrevID, Record,
3978 reinterpret_cast<char*>(CategoriesMap.data()),
3979 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3980
3981 // Emit the category lists.
3982 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3983}
3984
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003985void ASTWriter::WriteMergedDecls() {
3986 if (!Chain || Chain->MergedDecls.empty())
3987 return;
3988
3989 RecordData Record;
3990 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3991 IEnd = Chain->MergedDecls.end();
3992 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003993 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Stephen Hines6bcf27b2014-05-29 04:14:42 -07003994 : GetDeclRef(I->first);
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003995 assert(CanonID && "Merged declaration not known?");
3996
3997 Record.push_back(CanonID);
3998 Record.push_back(I->second.size());
3999 Record.append(I->second.begin(), I->second.end());
4000 }
4001 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
4002}
4003
Richard Smithac32d902013-08-07 21:41:30 +00004004void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
4005 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
4006
4007 if (LPTMap.empty())
4008 return;
4009
4010 RecordData Record;
4011 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
4012 ItEnd = LPTMap.end();
4013 It != ItEnd; ++It) {
4014 LateParsedTemplate *LPT = It->second;
4015 AddDeclRef(It->first, Record);
4016 AddDeclRef(LPT->D, Record);
4017 Record.push_back(LPT->Toks.size());
4018
4019 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
4020 TokEnd = LPT->Toks.end();
4021 TokIt != TokEnd; ++TokIt) {
4022 AddToken(*TokIt, Record);
4023 }
4024 }
4025 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
4026}
4027
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004028/// \brief Write the state of 'pragma clang optimize' at the end of the module.
4029void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
4030 RecordData Record;
4031 SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
4032 AddSourceLocation(PragmaLoc, Record);
4033 Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
4034}
4035
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004036//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00004037// General Serialization Routines
4038//===----------------------------------------------------------------------===//
4039
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004040/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00004041void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
4042 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00004043 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00004044 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
4045 e = Attrs.end(); i != e; ++i){
4046 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00004047 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00004048 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004049
Sean Huntcf807c42010-08-18 23:23:40 +00004050#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00004051
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004052 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004053}
4054
John McCallaeeacf72013-05-03 00:10:13 +00004055void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
4056 AddSourceLocation(Tok.getLocation(), Record);
4057 Record.push_back(Tok.getLength());
4058
4059 // FIXME: When reading literal tokens, reconstruct the literal pointer
4060 // if it is needed.
4061 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
4062 // FIXME: Should translate token kind to a stable encoding.
4063 Record.push_back(Tok.getKind());
4064 // FIXME: Should translate token flags to a stable encoding.
4065 Record.push_back(Tok.getFlags());
4066}
4067
Chris Lattner5f9e2722011-07-23 10:55:15 +00004068void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00004069 Record.push_back(Str.size());
4070 Record.insert(Record.end(), Str.begin(), Str.end());
4071}
4072
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004073void ASTWriter::AddVersionTuple(const VersionTuple &Version,
4074 RecordDataImpl &Record) {
4075 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00004076 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004077 Record.push_back(*Minor + 1);
4078 else
4079 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00004080 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004081 Record.push_back(*Subminor + 1);
4082 else
4083 Record.push_back(0);
4084}
4085
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004086/// \brief Note that the identifier II occurs at the given offset
4087/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00004088void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004089 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00004090 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004091 // up earlier in the chain and thus don't need an offset.
4092 if (ID >= FirstIdentID)
4093 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004094}
4095
Douglas Gregor83941df2009-04-25 17:48:32 +00004096/// \brief Note that the selector Sel occurs at the given offset
4097/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00004098void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00004099 unsigned ID = SelectorIDs[Sel];
4100 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00004101 // Don't record offsets for selectors that are also available in a different
4102 // file.
4103 if (ID < FirstSelectorID)
4104 return;
4105 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00004106}
4107
Sebastian Redla4232eb2010-08-18 23:56:21 +00004108ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Stephen Hines176edba2014-12-01 14:53:08 -08004109 : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4110 WritingModule(nullptr), WritingAST(false),
4111 DoneWritingDeclsAndTypes(false), ASTHasCompilerErrors(false),
4112 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
4113 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
4114 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
4115 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
4116 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4117 NextSubmoduleID(FirstSubmoduleID),
4118 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4119 CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4120 NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4121 NextCXXBaseSpecifiersID(1), TypeExtQualAbbrev(0),
4122 TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4123 DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
4124 UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
4125 DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
4126 DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4127 CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4128 ExprImplicitCastAbbrev(0) {}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004129
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004130ASTWriter::~ASTWriter() {
Stephen Hines651f13c2014-04-23 16:59:28 -07004131 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004132}
4133
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00004134void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00004135 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00004136 Module *WritingModule, StringRef isysroot,
4137 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004138 WritingAST = true;
4139
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00004140 ASTHasCompilerErrors = hasErrors;
4141
Douglas Gregor2cf26342009-04-09 22:27:44 +00004142 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00004143 Stream.Emit((unsigned)'C', 8);
4144 Stream.Emit((unsigned)'P', 8);
4145 Stream.Emit((unsigned)'C', 8);
4146 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00004147
Chris Lattnerb145b1e2009-04-26 22:26:21 +00004148 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004149
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004150 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00004151 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00004152 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00004153 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004154 Context = nullptr;
4155 PP = nullptr;
4156 this->WritingModule = nullptr;
4157
Douglas Gregor61c5e342011-09-17 00:05:03 +00004158 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004159}
4160
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004161template<typename Vector>
4162static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4163 ASTWriter::RecordData &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004164 for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4165 I != E; ++I) {
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004166 Writer.AddDeclRef(*I, Record);
4167 }
4168}
4169
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00004170void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00004171 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00004172 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00004173 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004174 using namespace llvm;
4175
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004176 bool isModule = WritingModule != nullptr;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004177
Douglas Gregorecc2c092011-12-01 22:20:10 +00004178 // Make sure that the AST reader knows to finalize itself.
4179 if (Chain)
4180 Chain->finalizeForWriting();
4181
Sebastian Redl1dc13a12010-07-12 22:02:52 +00004182 ASTContext &Context = SemaRef.Context;
4183 Preprocessor &PP = SemaRef.PP;
4184
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004185 // Set up predefined declaration IDs.
4186 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00004187 if (Context.ObjCIdDecl)
4188 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00004189 if (Context.ObjCSelDecl)
4190 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00004191 if (Context.ObjCClassDecl)
4192 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00004193 if (Context.ObjCProtocolClassDecl)
4194 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00004195 if (Context.Int128Decl)
4196 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
4197 if (Context.UInt128Decl)
4198 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00004199 if (Context.ObjCInstanceTypeDecl)
4200 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00004201 if (Context.BuiltinVaListDecl)
4202 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
4203
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004204 if (!Chain) {
4205 // Make sure that we emit IdentifierInfos (and any attached
4206 // declarations) for builtins. We don't need to do this when we're
4207 // emitting chained PCH files, because all of the builtins will be
4208 // in the original PCH file.
4209 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00004210 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00004211 SmallVector<const char *, 32> BuiltinNames;
Eli Bendersky97a03cf2013-07-11 16:53:04 +00004212 if (!Context.getLangOpts().NoBuiltin) {
4213 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
4214 }
Douglas Gregor2deaea32009-04-22 18:49:13 +00004215 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
4216 getIdentifierRef(&Table.get(BuiltinNames[I]));
4217 }
4218
Douglas Gregoreee242f2011-10-27 09:33:13 +00004219 // If there are any out-of-date identifiers, bring them up to date.
4220 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00004221 // Find out-of-date identifiers.
4222 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00004223 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4224 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00004225 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00004226 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00004227 OutOfDate.push_back(ID->second);
4228 }
4229
4230 // Update the out-of-date identifiers.
4231 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
4232 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
4233 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00004234 }
4235
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004236 // If we saw any DeclContext updates before we started writing the AST file,
4237 // make sure all visible decls in those DeclContexts are written out.
4238 if (!UpdatedDeclContexts.empty()) {
4239 auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts);
4240 UpdatedDeclContexts.clear();
4241 for (auto *DC : OldUpdatedDeclContexts)
4242 AddUpdatedDeclContext(DC);
4243 }
4244
Chris Lattner63d65f82009-09-08 18:19:27 +00004245 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00004246 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00004247 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004248 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00004249 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00004250
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004251 // Build a record containing all of the file scoped decls in this file.
4252 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00004253 if (!isModule)
4254 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4255 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00004256
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004257 // Build a record containing all of the delegating constructors we still need
4258 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00004259 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004260 if (!isModule)
4261 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004262
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004263 // Write the set of weak, undeclared identifiers. We always write the
4264 // entire table, since later PCH files in a PCH chain are only interested in
4265 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004266 RecordData WeakUndeclaredIdentifiers;
4267 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00004268 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004269 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4270 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4271 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4272 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4273 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4274 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4275 }
4276 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004277
Richard Smith5ea6ef42013-01-10 23:43:47 +00004278 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00004279 // declarations in this header file. Generally, this record will be
4280 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00004281 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00004282 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00004283 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00004284 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00004285 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4286 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00004287 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00004288 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00004289 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00004290 }
4291
Douglas Gregorb81c1702009-04-27 20:06:05 +00004292 // Build a record containing all of the ext_vector declarations.
4293 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00004294 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004295
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004296 // Build a record containing all of the VTable uses information.
4297 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004298 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004299 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4300 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4301 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4302 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4303 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004304 }
4305
Stephen Hines176edba2014-12-01 14:53:08 -08004306 // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4307 RecordData UnusedLocalTypedefNameCandidates;
4308 for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4309 AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4310
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004311 // Build a record containing all of dynamic classes declarations.
4312 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00004313 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004314
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004315 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004316 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004317 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00004318 I = SemaRef.PendingInstantiations.begin(),
4319 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4320 AddDeclRef(I->first, PendingInstantiations);
4321 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004322 }
4323 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4324 "There are local ones at end of translation unit!");
4325
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004326 // Build a record containing some declaration references.
4327 RecordData SemaDeclRefs;
4328 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4329 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4330 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4331 }
4332
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004333 RecordData CUDASpecialDeclRefs;
4334 if (Context.getcudaConfigureCallDecl()) {
4335 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4336 }
4337
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004338 // Build a record containing all of the known namespaces.
4339 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00004340 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004341 I = SemaRef.KnownNamespaces.begin(),
4342 IEnd = SemaRef.KnownNamespaces.end();
4343 I != IEnd; ++I) {
4344 if (!I->second)
4345 AddDeclRef(I->first, KnownNamespaces);
4346 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004347
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004348 // Build a record of all used, undefined objects that require definitions.
4349 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00004350
4351 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004352 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00004353 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4354 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004355 AddDeclRef(I->first, UndefinedButUsed);
4356 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00004357 }
4358
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004359 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00004360 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004361
Sebastian Redl3397c552010-08-18 23:56:27 +00004362 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00004363 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004364 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004365
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00004366 // This is so that older clang versions, before the introduction
4367 // of the control block, can read and reject the newer PCH format.
4368 Record.clear();
4369 Record.push_back(VERSION_MAJOR);
4370 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4371
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004372 // Create a lexical update block containing all of the declarations in the
4373 // translation unit that do not come from other AST files.
4374 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4375 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Stephen Hines651f13c2014-04-23 16:59:28 -07004376 for (const auto *I : TU->noload_decls()) {
4377 if (!I->isFromASTFile())
4378 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004379 }
4380
4381 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4382 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4383 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4384 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4385 Record.clear();
4386 Record.push_back(TU_UPDATE_LEXICAL);
4387 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4388 data(NewGlobalDecls));
4389
4390 // And a visible updates block for the translation unit.
4391 Abv = new llvm::BitCodeAbbrev();
4392 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4393 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4394 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4395 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4396 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4397 WriteDeclContextVisibleUpdate(TU);
4398
4399 // If the translation unit has an anonymous namespace, and we don't already
4400 // have an update block for it, write it as an update block.
Stephen Hines651f13c2014-04-23 16:59:28 -07004401 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004402 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4403 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Stephen Hines651f13c2014-04-23 16:59:28 -07004404 if (Record.empty())
4405 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004406 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004407
Stephen Hines651f13c2014-04-23 16:59:28 -07004408 // Add update records for all mangling numbers and static local numbers.
4409 // These aren't really update records, but this is a convenient way of
4410 // tagging this rare extra data onto the declarations.
4411 for (const auto &Number : Context.MangleNumbers)
4412 if (!Number.first->isFromASTFile())
4413 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4414 Number.second));
4415 for (const auto &Number : Context.StaticLocalNumbers)
4416 if (!Number.first->isFromASTFile())
4417 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4418 Number.second));
4419
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004420 // Make sure visible decls, added to DeclContexts previously loaded from
4421 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004422 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004423 I = UpdatingVisibleDecls.begin(),
4424 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4425 GetDeclRef(*I);
4426 }
4427
Argyrios Kyrtzidis51e75ae2013-08-07 21:17:33 +00004428 // Make sure all decls associated with an identifier are registered for
4429 // serialization.
4430 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4431 IDEnd = PP.getIdentifierTable().end();
4432 ID != IDEnd; ++ID) {
4433 const IdentifierInfo *II = ID->second;
4434 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4435 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4436 DEnd = SemaRef.IdResolver.end();
4437 D != DEnd; ++D) {
4438 GetDeclRef(*D);
4439 }
4440 }
4441 }
4442
Douglas Gregora119da02011-08-02 16:26:37 +00004443 // Form the record of special types.
4444 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004445 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004446 AddTypeRef(Context.getFILEType(), SpecialTypes);
4447 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4448 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4449 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4450 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004451 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004452 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004453
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004454 if (Chain) {
4455 // Write the mapping information describing our module dependencies and how
4456 // each of those modules were mapped into our own offset/ID space, so that
4457 // the reader can build the appropriate mapping to its own offset/ID space.
4458 // The map consists solely of a blob with the following format:
4459 // *(module-name-len:i16 module-name:len*i8
4460 // source-location-offset:i32
4461 // identifier-id:i32
4462 // preprocessed-entity-id:i32
4463 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004464 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004465 // selector-id:i32
4466 // declaration-id:i32
4467 // c++-base-specifiers-id:i32
4468 // type-id:i32)
4469 //
4470 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4471 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4473 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004474 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004475 {
4476 llvm::raw_svector_ostream Out(Buffer);
Stephen Hines176edba2014-12-01 14:53:08 -08004477 for (ModuleFile *M : Chain->ModuleMgr) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004478 using namespace llvm::support;
4479 endian::Writer<little> LE(Out);
Stephen Hines176edba2014-12-01 14:53:08 -08004480 StringRef FileName = M->FileName;
Stephen Hines651f13c2014-04-23 16:59:28 -07004481 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004482 Out.write(FileName.data(), FileName.size());
Stephen Hines176edba2014-12-01 14:53:08 -08004483
4484 // Note: if a base ID was uint max, it would not be possible to load
4485 // another module after it or have more than one entity inside it.
4486 uint32_t None = std::numeric_limits<uint32_t>::max();
4487
4488 auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4489 assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4490 if (ShouldWrite)
4491 LE.write<uint32_t>(BaseID);
4492 else
4493 LE.write<uint32_t>(None);
4494 };
4495
4496 // These values should be unique within a chain, since they will be read
4497 // as keys into ContinuousRangeMaps.
4498 writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4499 writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4500 writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4501 writeBaseIDOrNone(M->BasePreprocessedEntityID,
4502 M->NumPreprocessedEntities);
4503 writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4504 writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4505 writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4506 writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004507 }
4508 }
4509 Record.clear();
4510 Record.push_back(MODULE_OFFSET_MAP);
4511 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4512 Buffer.data(), Buffer.size());
4513 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004514
4515 RecordData DeclUpdatesOffsetsRecord;
4516
4517 // Keep writing types, declarations, and declaration update records
4518 // until we've emitted all of them.
Stephen Hines176edba2014-12-01 14:53:08 -08004519 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4520 WriteTypeAbbrevs();
4521 WriteDeclAbbrevs();
Stephen Hines651f13c2014-04-23 16:59:28 -07004522 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4523 E = DeclsToRewrite.end();
4524 I != E; ++I)
4525 DeclTypesToEmit.push(const_cast<Decl*>(*I));
4526 do {
4527 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4528 while (!DeclTypesToEmit.empty()) {
4529 DeclOrType DOT = DeclTypesToEmit.front();
4530 DeclTypesToEmit.pop();
4531 if (DOT.isType())
4532 WriteType(DOT.getType());
4533 else
4534 WriteDecl(Context, DOT.getDecl());
4535 }
4536 } while (!DeclUpdates.empty());
4537 Stream.ExitBlock();
4538
4539 DoneWritingDeclsAndTypes = true;
4540
4541 // These things can only be done once we've written out decls and types.
4542 WriteTypeDeclOffsets();
4543 if (!DeclUpdatesOffsetsRecord.empty())
4544 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4545 WriteCXXBaseSpecifiersOffsets();
4546 WriteFileDeclIDsMap();
4547 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4548
4549 WriteComments();
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004550 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004551 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004552 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004553 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004554 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004555 WriteFPPragmaOptions(SemaRef.getFPOptions());
4556 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004557 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004558
Douglas Gregore209e502011-12-06 01:10:29 +00004559 // If we're emitting a module, write out the submodule information.
4560 if (WritingModule)
4561 WriteSubmodules(WritingModule);
4562
Douglas Gregora119da02011-08-02 16:26:37 +00004563 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4564
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004565 // Write the record containing external, unnamed definitions.
Stephen Hines651f13c2014-04-23 16:59:28 -07004566 if (!EagerlyDeserializedDecls.empty())
4567 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004568
4569 // Write the record containing tentative definitions.
4570 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004571 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004572
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004573 // Write the record containing unused file scoped decls.
4574 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004575 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004576
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004577 // Write the record containing weak undeclared identifiers.
4578 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004579 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004580 WeakUndeclaredIdentifiers);
4581
Richard Smith5ea6ef42013-01-10 23:43:47 +00004582 // Write the record containing locally-scoped extern "C" definitions.
4583 if (!LocallyScopedExternCDecls.empty())
4584 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4585 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004586
4587 // Write the record containing ext_vector type names.
4588 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004589 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004590
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004591 // Write the record containing VTable uses information.
4592 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004593 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004594
4595 // Write the record containing dynamic classes declarations.
4596 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004597 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004598
Stephen Hines176edba2014-12-01 14:53:08 -08004599 // Write the record containing potentially unused local typedefs.
4600 if (!UnusedLocalTypedefNameCandidates.empty())
4601 Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4602 UnusedLocalTypedefNameCandidates);
4603
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004604 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004605 if (!PendingInstantiations.empty())
4606 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004607
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004608 // Write the record containing declaration references of Sema.
4609 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004610 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004611
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004612 // Write the record containing CUDA-specific declaration references.
4613 if (!CUDASpecialDeclRefs.empty())
4614 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004615
4616 // Write the delegating constructors.
4617 if (!DelegatingCtorDecls.empty())
4618 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004619
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004620 // Write the known namespaces.
4621 if (!KnownNamespaces.empty())
4622 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004623
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004624 // Write the undefined internal functions and variables, and inline functions.
4625 if (!UndefinedButUsed.empty())
4626 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004627
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004628 // Write the visible updates to DeclContexts.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004629 for (auto *DC : UpdatedDeclContexts)
4630 WriteDeclContextVisibleUpdate(DC);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004631
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004632 if (!WritingModule) {
4633 // Write the submodules that were imported, if any.
Stephen Hines651f13c2014-04-23 16:59:28 -07004634 struct ModuleInfo {
4635 uint64_t ID;
4636 Module *M;
4637 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4638 };
4639 llvm::SmallVector<ModuleInfo, 64> Imports;
4640 for (const auto *I : Context.local_imports()) {
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004641 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Stephen Hines651f13c2014-04-23 16:59:28 -07004642 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4643 I->getImportedModule()));
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004644 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004645
4646 if (!Imports.empty()) {
4647 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4648 return A.ID < B.ID;
4649 };
Stephen Hines176edba2014-12-01 14:53:08 -08004650 auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4651 return A.ID == B.ID;
4652 };
Stephen Hines651f13c2014-04-23 16:59:28 -07004653
4654 // Sort and deduplicate module IDs.
4655 std::sort(Imports.begin(), Imports.end(), Cmp);
Stephen Hines176edba2014-12-01 14:53:08 -08004656 Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
Stephen Hines651f13c2014-04-23 16:59:28 -07004657 Imports.end());
4658
4659 RecordData ImportedModules;
4660 for (const auto &Import : Imports) {
4661 ImportedModules.push_back(Import.ID);
4662 // FIXME: If the module has macros imported then later has declarations
4663 // imported, this location won't be the right one as a location for the
4664 // declaration imports.
4665 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4666 }
4667
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004668 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4669 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004670 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004671
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004672 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004673 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004674 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004675 WriteObjCCategories();
Richard Smithac32d902013-08-07 21:41:30 +00004676 WriteLateParsedTemplates(SemaRef);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004677 if(!WritingModule)
4678 WriteOptimizePragmaOptions(SemaRef);
Richard Smithac32d902013-08-07 21:41:30 +00004679
Douglas Gregor3e1af842009-04-17 22:13:46 +00004680 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004681 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004682 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004683 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004684 Record.push_back(NumLexicalDeclContexts);
4685 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004686 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004687 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004688}
4689
Stephen Hines651f13c2014-04-23 16:59:28 -07004690void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004691 if (DeclUpdates.empty())
4692 return;
4693
Stephen Hines651f13c2014-04-23 16:59:28 -07004694 DeclUpdateMap LocalUpdates;
4695 LocalUpdates.swap(DeclUpdates);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004696
Stephen Hines651f13c2014-04-23 16:59:28 -07004697 for (auto &DeclUpdate : LocalUpdates) {
4698 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004699 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004700 continue; // The decl will be written completely,no need to store updates.
4701
Stephen Hines651f13c2014-04-23 16:59:28 -07004702 bool HasUpdatedBody = false;
4703 RecordData Record;
4704 for (auto &Update : DeclUpdate.second) {
4705 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4706
4707 Record.push_back(Kind);
4708 switch (Kind) {
4709 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4710 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4711 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004712 assert(Update.getDecl() && "no decl to add?");
Stephen Hines651f13c2014-04-23 16:59:28 -07004713 Record.push_back(GetDeclRef(Update.getDecl()));
4714 break;
4715
Stephen Hines176edba2014-12-01 14:53:08 -08004716 case UPD_CXX_ADDED_FUNCTION_DEFINITION:
Stephen Hines651f13c2014-04-23 16:59:28 -07004717 // An updated body is emitted last, so that the reader doesn't need
4718 // to skip over the lazy body to reach statements for other records.
4719 Record.pop_back();
4720 HasUpdatedBody = true;
4721 break;
4722
Stephen Hines176edba2014-12-01 14:53:08 -08004723 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4724 AddSourceLocation(Update.getLoc(), Record);
4725 break;
4726
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004727 case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4728 auto *RD = cast<CXXRecordDecl>(D);
4729 AddUpdatedDeclContext(RD->getPrimaryContext());
4730 AddCXXDefinitionData(RD, Record);
4731 Record.push_back(WriteDeclContextLexicalBlock(
4732 *Context, const_cast<CXXRecordDecl *>(RD)));
4733
4734 // This state is sometimes updated by template instantiation, when we
4735 // switch from the specialization referring to the template declaration
4736 // to it referring to the template definition.
4737 if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4738 Record.push_back(MSInfo->getTemplateSpecializationKind());
4739 AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4740 } else {
4741 auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4742 Record.push_back(Spec->getTemplateSpecializationKind());
4743 AddSourceLocation(Spec->getPointOfInstantiation(), Record);
4744
4745 // The instantiation might have been resolved to a partial
4746 // specialization. If so, record which one.
4747 auto From = Spec->getInstantiatedFrom();
4748 if (auto PartialSpec =
4749 From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4750 Record.push_back(true);
4751 AddDeclRef(PartialSpec, Record);
4752 AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4753 Record);
4754 } else {
4755 Record.push_back(false);
4756 }
4757 }
4758 Record.push_back(RD->getTagKind());
4759 AddSourceLocation(RD->getLocation(), Record);
4760 AddSourceLocation(RD->getLocStart(), Record);
4761 AddSourceLocation(RD->getRBraceLoc(), Record);
4762
4763 // Instantiation may change attributes; write them all out afresh.
4764 Record.push_back(D->hasAttrs());
4765 if (Record.back())
Stephen Hines176edba2014-12-01 14:53:08 -08004766 WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4767 D->getAttrs().size()), Record);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004768
4769 // FIXME: Ensure we don't get here for explicit instantiations.
4770 break;
4771 }
4772
Stephen Hines651f13c2014-04-23 16:59:28 -07004773 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4774 addExceptionSpec(
4775 *this,
4776 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4777 Record);
4778 break;
4779
4780 case UPD_CXX_DEDUCED_RETURN_TYPE:
4781 Record.push_back(GetOrCreateTypeID(Update.getType()));
4782 break;
4783
4784 case UPD_DECL_MARKED_USED:
4785 break;
4786
4787 case UPD_MANGLING_NUMBER:
4788 case UPD_STATIC_LOCAL_NUMBER:
4789 Record.push_back(Update.getNumber());
4790 break;
Stephen Hines176edba2014-12-01 14:53:08 -08004791 case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4792 AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4793 Record);
4794 break;
Stephen Hines651f13c2014-04-23 16:59:28 -07004795 }
4796 }
4797
4798 if (HasUpdatedBody) {
4799 const FunctionDecl *Def = cast<FunctionDecl>(D);
Stephen Hines176edba2014-12-01 14:53:08 -08004800 Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
Stephen Hines651f13c2014-04-23 16:59:28 -07004801 Record.push_back(Def->isInlined());
4802 AddSourceLocation(Def->getInnerLocStart(), Record);
4803 AddFunctionDefinition(Def, Record);
Stephen Hines176edba2014-12-01 14:53:08 -08004804 if (auto *DD = dyn_cast<CXXDestructorDecl>(Def))
4805 Record.push_back(GetDeclRef(DD->getOperatorDelete()));
Stephen Hines651f13c2014-04-23 16:59:28 -07004806 }
4807
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004808 OffsetsRecord.push_back(GetDeclRef(D));
4809 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4810
Stephen Hines651f13c2014-04-23 16:59:28 -07004811 Stream.EmitRecord(DECL_UPDATES, Record);
4812
4813 // Flush any statements that were written as part of this update record.
4814 FlushStmts();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004815
4816 // Flush C++ base specifiers, if there are any.
4817 FlushCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004818 }
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004819}
4820
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004821void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004822 if (ReplacedDecls.empty())
4823 return;
4824
4825 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004826 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4827 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004828 Record.push_back(I->ID);
4829 Record.push_back(I->Offset);
4830 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004831 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004832 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004833}
4834
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004835void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004836 Record.push_back(Loc.getRawEncoding());
4837}
4838
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004839void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004840 AddSourceLocation(Range.getBegin(), Record);
4841 AddSourceLocation(Range.getEnd(), Record);
4842}
4843
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004844void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004845 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004846 const uint64_t *Words = Value.getRawData();
4847 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004848}
4849
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004850void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004851 Record.push_back(Value.isUnsigned());
4852 AddAPInt(Value, Record);
4853}
4854
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004855void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004856 AddAPInt(Value.bitcastToAPInt(), Record);
4857}
4858
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004859void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004860 Record.push_back(getIdentifierRef(II));
4861}
4862
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004863IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004864 if (!II)
Douglas Gregor2deaea32009-04-22 18:49:13 +00004865 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004866
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004867 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004868 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004869 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004870 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004871}
4872
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004873MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004874 // Don't emit builtin macros like __LINE__ to the AST file unless they
4875 // have been redefined by the header (in which case they are not
4876 // isBuiltinMacro).
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004877 if (!MI || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004878 return 0;
4879
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004880 MacroID &ID = MacroIDs[MI];
4881 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004882 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004883 MacroInfoToEmitData Info = { Name, MI, ID };
4884 MacroInfosToEmit.push_back(Info);
4885 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004886 return ID;
4887}
4888
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004889MacroID ASTWriter::getMacroID(MacroInfo *MI) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004890 if (!MI || MI->isBuiltinMacro())
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004891 return 0;
4892
4893 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4894 return MacroIDs[MI];
4895}
4896
4897uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4898 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4899 return IdentMacroDirectivesOffsetMap[Name];
4900}
4901
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004902void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004903 Record.push_back(getSelectorRef(SelRef));
4904}
4905
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004906SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004907 if (Sel.getAsOpaquePtr() == nullptr) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004908 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004909 }
4910
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004911 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004912 if (SID == 0 && Chain) {
4913 // This might trigger a ReadSelector callback, which will set the ID for
4914 // this selector.
4915 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004916 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004917 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004918 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004919 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004920 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004921 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004922 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004923}
4924
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004925void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004926 AddDeclRef(Temp->getDestructor(), Record);
4927}
4928
Douglas Gregor7c789c12010-10-29 22:39:52 +00004929void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4930 CXXBaseSpecifier const *BasesEnd,
4931 RecordDataImpl &Record) {
4932 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4933 CXXBaseSpecifiersToWrite.push_back(
4934 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4935 Bases, BasesEnd));
4936 Record.push_back(NextCXXBaseSpecifiersID++);
4937}
4938
Sebastian Redla4232eb2010-08-18 23:56:21 +00004939void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004940 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004941 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004942 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004943 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004944 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004945 break;
4946 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004947 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004948 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004949 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004950 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004951 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004952 break;
4953 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004954 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004955 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004956 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004957 break;
John McCall833ca992009-10-29 08:12:44 +00004958 case TemplateArgument::Null:
4959 case TemplateArgument::Integral:
4960 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004961 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004962 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004963 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004964 break;
4965 }
4966}
4967
Sebastian Redla4232eb2010-08-18 23:56:21 +00004968void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004969 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004970 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004971
4972 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4973 bool InfoHasSameExpr
4974 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4975 Record.push_back(InfoHasSameExpr);
4976 if (InfoHasSameExpr)
4977 return; // Avoid storing the same expr twice.
4978 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004979 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4980 Record);
4981}
4982
Douglas Gregordc355712011-02-25 00:36:19 +00004983void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4984 RecordDataImpl &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07004985 if (!TInfo) {
John McCalla1ee0c52009-10-16 21:56:05 +00004986 AddTypeRef(QualType(), Record);
4987 return;
4988 }
4989
Douglas Gregordc355712011-02-25 00:36:19 +00004990 AddTypeLoc(TInfo->getTypeLoc(), Record);
4991}
4992
4993void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4994 AddTypeRef(TL.getType(), Record);
4995
John McCalla1ee0c52009-10-16 21:56:05 +00004996 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004997 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004998 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004999}
5000
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005001void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00005002 Record.push_back(GetOrCreateTypeID(T));
5003}
5004
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005005TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00005006 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005007 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00005008 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
5009}
Douglas Gregor2cf26342009-04-09 22:27:44 +00005010
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00005011TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00005012 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00005013 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00005014 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00005015}
5016
5017TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
5018 if (T.isNull())
5019 return TypeIdx();
5020 assert(!T.getLocalFastQualifiers());
5021
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00005022 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005023 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005024 if (DoneWritingDeclsAndTypes) {
5025 assert(0 && "New type seen after serializing all the types to emit!");
5026 return TypeIdx();
5027 }
5028
Douglas Gregor366809a2009-04-26 03:49:13 +00005029 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00005030 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005031 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00005032 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00005033 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00005034 return Idx;
5035}
Douglas Gregor2cf26342009-04-09 22:27:44 +00005036
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00005037TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00005038 if (T.isNull())
5039 return TypeIdx();
5040 assert(!T.getLocalFastQualifiers());
5041
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00005042 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
5043 assert(I != TypeIdxs.end() && "Type not emitted!");
5044 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005045}
5046
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005047void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00005048 Record.push_back(GetDeclRef(D));
5049}
5050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005051DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005052 assert(WritingAST && "Cannot request a declaration ID before AST writing");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005053
5054 if (!D) {
Sebastian Redl681d7232010-07-27 00:17:23 +00005055 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005056 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00005057
5058 // If D comes from an AST file, its declaration ID is already known and
5059 // fixed.
5060 if (D->isFromASTFile())
5061 return D->getGlobalID();
5062
Douglas Gregor97475832010-10-05 18:37:06 +00005063 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005064 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00005065 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005066 if (DoneWritingDeclsAndTypes) {
5067 assert(0 && "New decl seen after serializing all the decls to emit!");
5068 return 0;
5069 }
5070
Douglas Gregor2cf26342009-04-09 22:27:44 +00005071 // We haven't seen this declaration before. Give it a new ID and
5072 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005073 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00005074 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00005075 }
5076
Sebastian Redl681d7232010-07-27 00:17:23 +00005077 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00005078}
5079
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005080DeclID ASTWriter::getDeclID(const Decl *D) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005081 if (!D)
Douglas Gregor3251ceb2009-04-20 20:36:09 +00005082 return 0;
5083
Douglas Gregor1c7946a2012-01-05 22:33:30 +00005084 // If D comes from an AST file, its declaration ID is already known and
5085 // fixed.
5086 if (D->isFromASTFile())
5087 return D->getGlobalID();
5088
Douglas Gregor3251ceb2009-04-20 20:36:09 +00005089 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
5090 return DeclIDs[D];
5091}
5092
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00005093void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005094 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00005095 assert(D);
5096
5097 SourceLocation Loc = D->getLocation();
5098 if (Loc.isInvalid())
5099 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005100
5101 // We only keep track of the file-level declarations of each file.
5102 if (!D->getLexicalDeclContext()->isFileContext())
5103 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00005104 // FIXME: ParmVarDecls that are part of a function type of a parameter of
5105 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00005106 if (isa<ParmVarDecl>(D))
5107 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005108
5109 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00005110 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005111 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00005112 FileID FID;
5113 unsigned Offset;
Stephen Hines651f13c2014-04-23 16:59:28 -07005114 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005115 if (FID.isInvalid())
5116 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00005117 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005118
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00005119 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005120 if (!Info)
5121 Info = new DeclIDInFileInfo();
5122
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00005123 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005124 LocDeclIDsTy &Decls = Info->DeclIDs;
5125
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00005126 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005127 Decls.push_back(LocDecl);
5128 return;
5129 }
5130
Benjamin Kramer809d2542013-08-24 13:22:59 +00005131 LocDeclIDsTy::iterator I =
5132 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00005133
5134 Decls.insert(I, LocDecl);
5135}
5136
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005137void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00005138 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00005139 Record.push_back(Name.getNameKind());
5140 switch (Name.getNameKind()) {
5141 case DeclarationName::Identifier:
5142 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5143 break;
5144
5145 case DeclarationName::ObjCZeroArgSelector:
5146 case DeclarationName::ObjCOneArgSelector:
5147 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00005148 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00005149 break;
5150
5151 case DeclarationName::CXXConstructorName:
5152 case DeclarationName::CXXDestructorName:
5153 case DeclarationName::CXXConversionFunctionName:
5154 AddTypeRef(Name.getCXXNameType(), Record);
5155 break;
5156
5157 case DeclarationName::CXXOperatorName:
5158 Record.push_back(Name.getCXXOverloadedOperator());
5159 break;
5160
Sean Hunt3e518bd2009-11-29 07:34:05 +00005161 case DeclarationName::CXXLiteralOperatorName:
5162 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5163 break;
5164
Douglas Gregor2cf26342009-04-09 22:27:44 +00005165 case DeclarationName::CXXUsingDirective:
5166 // No extra data to emit
5167 break;
5168 }
5169}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005170
Stephen Hines176edba2014-12-01 14:53:08 -08005171unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5172 assert(needsAnonymousDeclarationNumber(D) &&
5173 "expected an anonymous declaration");
5174
5175 // Number the anonymous declarations within this context, if we've not
5176 // already done so.
5177 auto It = AnonymousDeclarationNumbers.find(D);
5178 if (It == AnonymousDeclarationNumbers.end()) {
5179 unsigned Index = 0;
5180 for (Decl *LexicalD : D->getLexicalDeclContext()->decls()) {
5181 auto *ND = dyn_cast<NamedDecl>(LexicalD);
5182 if (!ND || !needsAnonymousDeclarationNumber(ND))
5183 continue;
5184 AnonymousDeclarationNumbers[ND] = Index++;
5185 }
5186
5187 It = AnonymousDeclarationNumbers.find(D);
5188 assert(It != AnonymousDeclarationNumbers.end() &&
5189 "declaration not found within its lexical context");
5190 }
5191
5192 return It->second;
5193}
5194
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005195void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005196 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005197 switch (Name.getNameKind()) {
5198 case DeclarationName::CXXConstructorName:
5199 case DeclarationName::CXXDestructorName:
5200 case DeclarationName::CXXConversionFunctionName:
5201 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5202 break;
5203
5204 case DeclarationName::CXXOperatorName:
5205 AddSourceLocation(
5206 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5207 Record);
5208 AddSourceLocation(
5209 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5210 Record);
5211 break;
5212
5213 case DeclarationName::CXXLiteralOperatorName:
5214 AddSourceLocation(
5215 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5216 Record);
5217 break;
5218
5219 case DeclarationName::Identifier:
5220 case DeclarationName::ObjCZeroArgSelector:
5221 case DeclarationName::ObjCOneArgSelector:
5222 case DeclarationName::ObjCMultiArgSelector:
5223 case DeclarationName::CXXUsingDirective:
5224 break;
5225 }
5226}
5227
5228void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005229 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005230 AddDeclarationName(NameInfo.getName(), Record);
5231 AddSourceLocation(NameInfo.getLoc(), Record);
5232 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5233}
5234
5235void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005236 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00005237 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00005238 Record.push_back(Info.NumTemplParamLists);
5239 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5240 AddTemplateParameterList(Info.TemplParamLists[i], Record);
5241}
5242
Sebastian Redla4232eb2010-08-18 23:56:21 +00005243void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005244 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005245 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00005246 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005247 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005248
5249 // Push each of the NNS's onto a stack for serialization in reverse order.
5250 while (NNS) {
5251 NestedNames.push_back(NNS);
5252 NNS = NNS->getPrefix();
5253 }
5254
5255 Record.push_back(NestedNames.size());
5256 while(!NestedNames.empty()) {
5257 NNS = NestedNames.pop_back_val();
5258 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5259 Record.push_back(Kind);
5260 switch (Kind) {
5261 case NestedNameSpecifier::Identifier:
5262 AddIdentifierRef(NNS->getAsIdentifier(), Record);
5263 break;
5264
5265 case NestedNameSpecifier::Namespace:
5266 AddDeclRef(NNS->getAsNamespace(), Record);
5267 break;
5268
Douglas Gregor14aba762011-02-24 02:36:08 +00005269 case NestedNameSpecifier::NamespaceAlias:
5270 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5271 break;
5272
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005273 case NestedNameSpecifier::TypeSpec:
5274 case NestedNameSpecifier::TypeSpecWithTemplate:
5275 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5276 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5277 break;
5278
5279 case NestedNameSpecifier::Global:
5280 // Don't need to write an associated value.
5281 break;
Stephen Hines176edba2014-12-01 14:53:08 -08005282
5283 case NestedNameSpecifier::Super:
5284 AddDeclRef(NNS->getAsRecordDecl(), Record);
5285 break;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00005286 }
5287 }
5288}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005289
Douglas Gregordc355712011-02-25 00:36:19 +00005290void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5291 RecordDataImpl &Record) {
5292 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00005293 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005294 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00005295
5296 // Push each of the nested-name-specifiers's onto a stack for
5297 // serialization in reverse order.
5298 while (NNS) {
5299 NestedNames.push_back(NNS);
5300 NNS = NNS.getPrefix();
5301 }
5302
5303 Record.push_back(NestedNames.size());
5304 while(!NestedNames.empty()) {
5305 NNS = NestedNames.pop_back_val();
5306 NestedNameSpecifier::SpecifierKind Kind
5307 = NNS.getNestedNameSpecifier()->getKind();
5308 Record.push_back(Kind);
5309 switch (Kind) {
5310 case NestedNameSpecifier::Identifier:
5311 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5312 AddSourceRange(NNS.getLocalSourceRange(), Record);
5313 break;
5314
5315 case NestedNameSpecifier::Namespace:
5316 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5317 AddSourceRange(NNS.getLocalSourceRange(), Record);
5318 break;
5319
5320 case NestedNameSpecifier::NamespaceAlias:
5321 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5322 AddSourceRange(NNS.getLocalSourceRange(), Record);
5323 break;
5324
5325 case NestedNameSpecifier::TypeSpec:
5326 case NestedNameSpecifier::TypeSpecWithTemplate:
5327 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5328 AddTypeLoc(NNS.getTypeLoc(), Record);
5329 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5330 break;
5331
5332 case NestedNameSpecifier::Global:
5333 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5334 break;
Stephen Hines176edba2014-12-01 14:53:08 -08005335
5336 case NestedNameSpecifier::Super:
5337 AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5338 AddSourceRange(NNS.getLocalSourceRange(), Record);
5339 break;
Douglas Gregordc355712011-02-25 00:36:19 +00005340 }
5341 }
5342}
5343
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005344void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00005345 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005346 Record.push_back(Kind);
5347 switch (Kind) {
5348 case TemplateName::Template:
5349 AddDeclRef(Name.getAsTemplateDecl(), Record);
5350 break;
5351
5352 case TemplateName::OverloadedTemplate: {
5353 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5354 Record.push_back(OvT->size());
5355 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
5356 I != E; ++I)
5357 AddDeclRef(*I, Record);
5358 break;
5359 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005360
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005361 case TemplateName::QualifiedTemplate: {
5362 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5363 AddNestedNameSpecifier(QualT->getQualifier(), Record);
5364 Record.push_back(QualT->hasTemplateKeyword());
5365 AddDeclRef(QualT->getTemplateDecl(), Record);
5366 break;
5367 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00005368
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005369 case TemplateName::DependentTemplate: {
5370 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5371 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5372 Record.push_back(DepT->isIdentifier());
5373 if (DepT->isIdentifier())
5374 AddIdentifierRef(DepT->getIdentifier(), Record);
5375 else
5376 Record.push_back(DepT->getOperator());
5377 break;
5378 }
John McCall14606042011-06-30 08:33:18 +00005379
5380 case TemplateName::SubstTemplateTemplateParm: {
5381 SubstTemplateTemplateParmStorage *subst
5382 = Name.getAsSubstTemplateTemplateParm();
5383 AddDeclRef(subst->getParameter(), Record);
5384 AddTemplateName(subst->getReplacement(), Record);
5385 break;
5386 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005387
5388 case TemplateName::SubstTemplateTemplateParmPack: {
5389 SubstTemplateTemplateParmPackStorage *SubstPack
5390 = Name.getAsSubstTemplateTemplateParmPack();
5391 AddDeclRef(SubstPack->getParameterPack(), Record);
5392 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5393 break;
5394 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005395 }
5396}
5397
Michael J. Spencer20249a12010-10-21 03:16:25 +00005398void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005399 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005400 Record.push_back(Arg.getKind());
5401 switch (Arg.getKind()) {
5402 case TemplateArgument::Null:
5403 break;
5404 case TemplateArgument::Type:
5405 AddTypeRef(Arg.getAsType(), Record);
5406 break;
5407 case TemplateArgument::Declaration:
5408 AddDeclRef(Arg.getAsDecl(), Record);
Stephen Hines176edba2014-12-01 14:53:08 -08005409 AddTypeRef(Arg.getParamTypeForDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00005410 break;
5411 case TemplateArgument::NullPtr:
5412 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005413 break;
5414 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00005415 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005416 AddTypeRef(Arg.getIntegralType(), Record);
5417 break;
5418 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00005419 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5420 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00005421 case TemplateArgument::TemplateExpansion:
5422 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00005423 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00005424 Record.push_back(*NumExpansions + 1);
5425 else
5426 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005427 break;
5428 case TemplateArgument::Expression:
5429 AddStmt(Arg.getAsExpr());
5430 break;
5431 case TemplateArgument::Pack:
5432 Record.push_back(Arg.pack_size());
Stephen Hines176edba2014-12-01 14:53:08 -08005433 for (const auto &P : Arg.pack_elements())
5434 AddTemplateArgument(P, Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005435 break;
5436 }
5437}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005438
5439void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005440ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005441 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005442 assert(TemplateParams && "No TemplateParams!");
5443 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5444 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5445 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5446 Record.push_back(TemplateParams->size());
5447 for (TemplateParameterList::const_iterator
5448 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5449 P != PEnd; ++P)
5450 AddDeclRef(*P, Record);
5451}
5452
5453/// \brief Emit a template argument list.
5454void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005455ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005456 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005457 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00005458 Record.push_back(TemplateArgs->size());
5459 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005460 AddTemplateArgument(TemplateArgs->get(i), Record);
5461}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005462
Enea Zaffanellac1cef082013-08-10 07:24:53 +00005463void
5464ASTWriter::AddASTTemplateArgumentListInfo
5465(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5466 assert(ASTTemplArgList && "No ASTTemplArgList!");
5467 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5468 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5469 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5470 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5471 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5472 AddTemplateArgumentLoc(TemplArgs[i], Record);
5473}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005474
5475void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005476ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005477 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005478 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005479 I = Set.begin(), E = Set.end(); I != E; ++I) {
5480 AddDeclRef(I.getDecl(), Record);
5481 Record.push_back(I.getAccess());
5482 }
5483}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005484
Sebastian Redla4232eb2010-08-18 23:56:21 +00005485void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005486 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005487 Record.push_back(Base.isVirtual());
5488 Record.push_back(Base.isBaseOfClass());
5489 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00005490 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00005491 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005492 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005493 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5494 : SourceLocation(),
5495 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005496}
Sebastian Redl30c514c2010-07-14 23:45:08 +00005497
Douglas Gregor7c789c12010-10-29 22:39:52 +00005498void ASTWriter::FlushCXXBaseSpecifiers() {
5499 RecordData Record;
5500 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5501 Record.clear();
5502
5503 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00005504 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005505 if (Index == CXXBaseSpecifiersOffsets.size())
5506 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5507 else {
5508 if (Index > CXXBaseSpecifiersOffsets.size())
5509 CXXBaseSpecifiersOffsets.resize(Index + 1);
5510 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5511 }
5512
5513 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5514 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5515 Record.push_back(BEnd - B);
5516 for (; B != BEnd; ++B)
5517 AddCXXBaseSpecifier(*B, Record);
5518 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00005519
5520 // Flush any expressions that were written as part of the base specifiers.
5521 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00005522 }
5523
5524 CXXBaseSpecifiersToWrite.clear();
5525}
5526
Sean Huntcbb67482011-01-08 20:30:50 +00005527void ASTWriter::AddCXXCtorInitializers(
5528 const CXXCtorInitializer * const *CtorInitializers,
5529 unsigned NumCtorInitializers,
5530 RecordDataImpl &Record) {
5531 Record.push_back(NumCtorInitializers);
5532 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5533 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005534
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005535 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00005536 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00005537 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005538 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00005539 } else if (Init->isDelegatingInitializer()) {
5540 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00005541 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00005542 } else if (Init->isMemberInitializer()){
5543 Record.push_back(CTOR_INITIALIZER_MEMBER);
5544 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005545 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00005546 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5547 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005548 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00005549
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005550 AddSourceLocation(Init->getMemberLocation(), Record);
5551 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005552 AddSourceLocation(Init->getLParenLoc(), Record);
5553 AddSourceLocation(Init->getRParenLoc(), Record);
5554 Record.push_back(Init->isWritten());
5555 if (Init->isWritten()) {
5556 Record.push_back(Init->getSourceOrder());
5557 } else {
5558 Record.push_back(Init->getNumArrayIndices());
5559 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5560 AddDeclRef(Init->getArrayIndex(i), Record);
5561 }
5562 }
5563}
5564
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005565void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005566 auto &Data = D->data();
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005567 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005568 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005569 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005570 Record.push_back(Data.Aggregate);
5571 Record.push_back(Data.PlainOldData);
5572 Record.push_back(Data.Empty);
5573 Record.push_back(Data.Polymorphic);
5574 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005575 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005576 Record.push_back(Data.HasNoNonEmptyBases);
5577 Record.push_back(Data.HasPrivateFields);
5578 Record.push_back(Data.HasProtectedFields);
5579 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005580 Record.push_back(Data.HasMutableFields);
Stephen Hines651f13c2014-04-23 16:59:28 -07005581 Record.push_back(Data.HasVariantMembers);
Richard Smithdfefb842012-02-25 07:33:38 +00005582 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005583 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005584 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005585 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5586 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5587 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5588 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5589 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5590 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005591 Record.push_back(Data.HasTrivialSpecialMembers);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005592 Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005593 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005594 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005595 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005596 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005597 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005598 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005599 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005600 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005601 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5602 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5603 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5604 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smithdfefb842012-02-25 07:33:38 +00005605 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005606
5607 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005608 if (Data.NumBases > 0)
5609 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5610 Record);
5611
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005612 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5613 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005614 if (Data.NumVBases > 0)
5615 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5616 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005617
Richard Smithc2d77572013-08-30 04:46:40 +00005618 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5619 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005620 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005621 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005622
5623 // Add lambda-specific data.
5624 if (Data.IsLambda) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005625 auto &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005626 Record.push_back(Lambda.Dependent);
Faisal Valibef582b2013-10-23 16:10:50 +00005627 Record.push_back(Lambda.IsGenericLambda);
5628 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005629 Record.push_back(Lambda.NumCaptures);
5630 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005631 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005632 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005633 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005634 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005635 const LambdaCapture &Capture = Lambda.Captures[I];
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005636 AddSourceLocation(Capture.getLocation(), Record);
5637 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005638 Record.push_back(Capture.getCaptureKind());
5639 switch (Capture.getCaptureKind()) {
5640 case LCK_This:
Stephen Hines176edba2014-12-01 14:53:08 -08005641 case LCK_VLAType:
Richard Smith0d8e9642013-05-16 06:20:58 +00005642 break;
5643 case LCK_ByCopy:
Richard Smith04fa7a32013-09-28 04:02:39 +00005644 case LCK_ByRef:
Richard Smith0d8e9642013-05-16 06:20:58 +00005645 VarDecl *Var =
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005646 Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
Richard Smith0d8e9642013-05-16 06:20:58 +00005647 AddDeclRef(Var, Record);
5648 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5649 : SourceLocation(),
5650 Record);
5651 break;
5652 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005653 }
5654 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005655}
5656
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005657void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005658 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005659 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005660 assert(FirstDeclID == NextDeclID &&
5661 FirstTypeID == NextTypeID &&
5662 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005663 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005664 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005665 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005666 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005667
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005668 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005669
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005670 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5671 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5672 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005673 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005674 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005675 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005676 NextDeclID = FirstDeclID;
5677 NextTypeID = FirstTypeID;
5678 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005679 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005680 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005681 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005682}
5683
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005684void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005685 // Always keep the highest ID. See \p TypeRead() for more information.
5686 IdentID &StoredID = IdentifierIDs[II];
5687 if (ID > StoredID)
5688 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005689}
5690
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005691void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005692 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005693 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005694 if (ID > StoredID)
5695 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005696}
5697
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005698void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005699 // Always take the highest-numbered type index. This copes with an interesting
5700 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005701 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005702 // keep the higher-numbered entry so that we can properly write it out to
5703 // the AST file.
5704 TypeIdx &StoredIdx = TypeIdxs[T];
5705 if (Idx.getIndex() >= StoredIdx.getIndex())
5706 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005707}
5708
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005709void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005710 // Always keep the highest ID. See \p TypeRead() for more information.
5711 SelectorID &StoredID = SelectorIDs[S];
5712 if (ID > StoredID)
5713 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005714}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005715
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005716void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005717 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005718 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005719 MacroDefinitions[MD] = ID;
5720}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005721
Douglas Gregora015cab2011-12-02 17:30:13 +00005722void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5723 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5724 SubmoduleIDs[Mod] = ID;
5725}
5726
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005727void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005728 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005729 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005730 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5731 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005732 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005733 // A forward reference was mutated into a definition. Rewrite it.
5734 // FIXME: This happens during template instantiation, should we
5735 // have created a new definition decl instead ?
Stephen Hines6bcf27b2014-05-29 04:14:42 -07005736 assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5737 "completed a tag from another module but not by instantiation?");
5738 DeclUpdates[RD].push_back(
5739 DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005740 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005741 }
5742}
Douglas Gregora8235d62012-10-09 23:05:51 +00005743
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005744void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5745 // TU and namespaces are handled elsewhere.
5746 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5747 return;
5748
Douglas Gregor919814d2011-09-09 23:01:35 +00005749 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005750 return; // Not a source decl added to a DeclContext from PCH.
5751
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005752 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Stephen Hines176edba2014-12-01 14:53:08 -08005753 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005754 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005755 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005756}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005757
5758void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5759 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005760 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005761 return; // Not a source member added to a class from PCH.
5762 if (!isa<CXXMethodDecl>(D))
5763 return; // We are interested in lazily declared implicit methods.
5764
5765 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005766 assert(RD->isCompleteDefinition());
Stephen Hines176edba2014-12-01 14:53:08 -08005767 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005768 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005769}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005770
5771void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5772 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005773 // The specializations set is kept in the canonical template.
5774 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005775 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005776 return; // Not a source specialization added to a template from PCH.
5777
Stephen Hines176edba2014-12-01 14:53:08 -08005778 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005779 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5780 D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005781}
Douglas Gregor89d99802010-11-30 06:16:57 +00005782
Larisse Voufoef4579c2013-08-06 01:03:05 +00005783void ASTWriter::AddedCXXTemplateSpecialization(
5784 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5785 // The specializations set is kept in the canonical template.
Larisse Voufoef4579c2013-08-06 01:03:05 +00005786 TD = TD->getCanonicalDecl();
5787 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5788 return; // Not a source specialization added to a template from PCH.
5789
Stephen Hines176edba2014-12-01 14:53:08 -08005790 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005791 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5792 D));
Larisse Voufoef4579c2013-08-06 01:03:05 +00005793}
5794
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005795void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5796 const FunctionDecl *D) {
5797 // The specializations set is kept in the canonical template.
5798 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005799 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005800 return; // Not a source specialization added to a template from PCH.
5801
Stephen Hines176edba2014-12-01 14:53:08 -08005802 assert(!WritingAST && "Already writing the AST!");
Stephen Hines651f13c2014-04-23 16:59:28 -07005803 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5804 D));
5805}
5806
5807void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5808 assert(!WritingAST && "Already writing the AST!");
5809 FD = FD->getCanonicalDecl();
5810 if (!FD->isFromASTFile())
5811 return; // Not a function declared in PCH and defined outside.
5812
5813 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005814}
5815
Richard Smith9dadfab2013-05-11 05:45:24 +00005816void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5817 assert(!WritingAST && "Already writing the AST!");
5818 FD = FD->getCanonicalDecl();
5819 if (!FD->isFromASTFile())
5820 return; // Not a function declared in PCH and defined outside.
5821
Stephen Hines651f13c2014-04-23 16:59:28 -07005822 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith9dadfab2013-05-11 05:45:24 +00005823}
5824
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005825void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005826 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005827 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005828 return; // Declaration not imported from PCH.
5829
Stephen Hines176edba2014-12-01 14:53:08 -08005830 // Implicit function decl from a PCH was defined.
5831 DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005832}
5833
Stephen Hines651f13c2014-04-23 16:59:28 -07005834void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5835 assert(!WritingAST && "Already writing the AST!");
5836 if (!D->isFromASTFile())
5837 return;
5838
Stephen Hines651f13c2014-04-23 16:59:28 -07005839 DeclUpdates[D].push_back(
Stephen Hines176edba2014-12-01 14:53:08 -08005840 DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
Stephen Hines651f13c2014-04-23 16:59:28 -07005841}
5842
Sebastian Redlf79a7192011-04-29 08:19:30 +00005843void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005844 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005845 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005846 return;
5847
5848 // Since the actual instantiation is delayed, this really means that we need
5849 // to update the instantiation location.
Stephen Hines651f13c2014-04-23 16:59:28 -07005850 DeclUpdates[D].push_back(
5851 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5852 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redlf79a7192011-04-29 08:19:30 +00005853}
5854
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005855void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5856 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005857 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005858 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005859 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005860
5861 assert(IFD->getDefinition() && "Category on a class without a definition?");
5862 ObjCClassesWithCategories.insert(
5863 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005864}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005865
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005866
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005867void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5868 const ObjCPropertyDecl *OrigProp,
5869 const ObjCCategoryDecl *ClassExt) {
5870 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5871 if (!D)
5872 return;
5873
5874 assert(!WritingAST && "Already writing the AST!");
5875 if (!D->isFromASTFile())
5876 return; // Declaration not imported from PCH.
5877
5878 RewriteDecl(D);
5879}
Eli Friedman86164e82013-09-05 00:02:25 +00005880
5881void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5882 assert(!WritingAST && "Already writing the AST!");
5883 if (!D->isFromASTFile())
5884 return;
5885
Stephen Hines651f13c2014-04-23 16:59:28 -07005886 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman86164e82013-09-05 00:02:25 +00005887}
Stephen Hines176edba2014-12-01 14:53:08 -08005888
5889void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5890 assert(!WritingAST && "Already writing the AST!");
5891 if (!D->isFromASTFile())
5892 return;
5893
5894 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5895}