blob: e4b5cdb9685f3736502bce101fdfbec095b4fcec [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"
Douglas Gregor14f79002009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000027#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000052#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000053#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000054#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000055#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000056#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000057using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000058using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000059
Sebastian Redlade50002010-07-30 17:03:48 +000060template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000061static StringRef data(const std::vector<T, Allocator> &v) {
62 if (v.empty()) return StringRef();
63 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000066
67template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000068static StringRef data(const SmallVectorImpl<T> &v) {
69 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000070 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000071}
72
Douglas Gregor2cf26342009-04-09 22:27:44 +000073//===----------------------------------------------------------------------===//
74// Type serialization
75//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000076
Douglas Gregor2cf26342009-04-09 22:27:44 +000077namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000078 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000079 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000081
82 public:
83 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000084 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000085
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000086 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000087 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000088
89 void VisitArrayType(const ArrayType *T);
90 void VisitFunctionType(const FunctionType *T);
91 void VisitTagType(const TagType *T);
92
93#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
94#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000095#include "clang/AST/TypeNodes.def"
96 };
97}
98
Sebastian Redl3397c552010-08-18 23:56:27 +000099void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000100 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Reid Kleckner12df2462013-06-24 17:51:48 +0000113void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
114 Writer.AddTypeRef(T->getOriginalType(), Record);
115 Code = TYPE_DECAYED;
116}
117
Stephen Hines651f13c2014-04-23 16:59:28 -0700118void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
119 Writer.AddTypeRef(T->getOriginalType(), Record);
120 Writer.AddTypeRef(T->getAdjustedType(), Record);
121 Code = TYPE_ADJUSTED;
122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000130 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
131 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000132 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000136 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000137 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138}
139
Sebastian Redl3397c552010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000141 Writer.AddTypeRef(T->getPointeeType(), Record);
142 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000149 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150}
151
Sebastian Redl3397c552010-08-18 23:56:27 +0000152void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153 VisitArrayType(T);
154 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000160 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161}
162
Sebastian Redl3397c552010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000165 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
166 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000167 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000168 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169}
170
Sebastian Redl3397c552010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000172 Writer.AddTypeRef(T->getElementType(), Record);
173 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000174 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000175 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700184 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000185 FunctionType::ExtInfo C = T->getExtInfo();
186 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000187 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000188 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000189 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000190 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000191 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192}
193
Sebastian Redl3397c552010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000195 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000196 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197}
198
Stephen Hines651f13c2014-04-23 16:59:28 -0700199static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
200 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl60618fa2011-03-12 11:50:43 +0000201 Record.push_back(T->getExceptionSpecType());
202 if (T->getExceptionSpecType() == EST_Dynamic) {
203 Record.push_back(T->getNumExceptions());
204 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
205 Writer.AddTypeRef(T->getExceptionType(I), Record);
206 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
207 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000208 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
209 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
210 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000211 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
212 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000213 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700214}
215
216void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
217 VisitFunctionType(T);
218 Record.push_back(T->getNumParams());
219 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
220 Writer.AddTypeRef(T->getParamType(I), Record);
221 Record.push_back(T->isVariadic());
222 Record.push_back(T->hasTrailingReturn());
223 Record.push_back(T->getTypeQuals());
224 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
225 addExceptionSpec(Writer, T, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227}
228
Sebastian Redl3397c552010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000230 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000231 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000232}
John McCalled976492009-12-04 22:46:56 +0000233
Sebastian Redl3397c552010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000235 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000236 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
237 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000238 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000239}
240
Sebastian Redl3397c552010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000242 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000243 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244}
245
Sebastian Redl3397c552010-08-18 23:56:27 +0000246void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000247 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000248 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249}
250
Sebastian Redl3397c552010-08-18 23:56:27 +0000251void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000252 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000253 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000255}
256
Sean Huntca63c202011-05-24 22:41:36 +0000257void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
258 Writer.AddTypeRef(T->getBaseType(), Record);
259 Writer.AddTypeRef(T->getUnderlyingType(), Record);
260 Record.push_back(T->getUTTKind());
261 Code = TYPE_UNARY_TRANSFORM;
262}
263
Richard Smith34b41d92011-02-20 03:19:35 +0000264void ASTTypeWriter::VisitAutoType(const AutoType *T) {
265 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smitha2c36462013-04-26 16:15:35 +0000266 Record.push_back(T->isDecltypeAuto());
Richard Smithdc7a4f52013-04-30 13:56:41 +0000267 if (T->getDeducedType().isNull())
268 Record.push_back(T->isDependentType());
Richard Smith34b41d92011-02-20 03:19:35 +0000269 Code = TYPE_AUTO;
270}
271
Sebastian Redl3397c552010-08-18 23:56:27 +0000272void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000273 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000274 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000275 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000276 "Cannot serialize in the middle of a type definition");
277}
278
Sebastian Redl3397c552010-08-18 23:56:27 +0000279void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000280 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000281 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000282}
283
Sebastian Redl3397c552010-08-18 23:56:27 +0000284void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000285 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000286 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000287}
288
John McCall9d156a72011-01-06 01:58:22 +0000289void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
290 Writer.AddTypeRef(T->getModifiedType(), Record);
291 Writer.AddTypeRef(T->getEquivalentType(), Record);
292 Record.push_back(T->getAttrKind());
293 Code = TYPE_ATTRIBUTED;
294}
295
Mike Stump1eb44332009-09-09 15:08:12 +0000296void
Sebastian Redl3397c552010-08-18 23:56:27 +0000297ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000298 const SubstTemplateTypeParmType *T) {
299 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
300 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000302}
303
304void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000305ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
306 const SubstTemplateTypeParmPackType *T) {
307 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
308 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
309 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000314 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000315 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316 Writer.AddTemplateName(T->getTemplateName(), Record);
317 Record.push_back(T->getNumArgs());
318 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
319 ArgI != ArgE; ++ArgI)
320 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000321 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
322 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000323 : T->getCanonicalTypeInternal(),
324 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000325 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000326}
327
328void
Sebastian Redl3397c552010-08-18 23:56:27 +0000329ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000330 VisitArrayType(T);
331 Writer.AddStmt(T->getSizeExpr());
332 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000333 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000334}
335
336void
Sebastian Redl3397c552010-08-18 23:56:27 +0000337ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000338 const DependentSizedExtVectorType *T) {
339 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000340 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000341}
342
343void
Sebastian Redl3397c552010-08-18 23:56:27 +0000344ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000345 Record.push_back(T->getDepth());
346 Record.push_back(T->getIndex());
347 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000348 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000349 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000350}
351
352void
Sebastian Redl3397c552010-08-18 23:56:27 +0000353ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000354 Record.push_back(T->getKeyword());
355 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
356 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000357 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
358 : T->getCanonicalTypeInternal(),
359 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000360 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000361}
362
363void
Sebastian Redl3397c552010-08-18 23:56:27 +0000364ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000365 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000366 Record.push_back(T->getKeyword());
367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddIdentifierRef(T->getIdentifier(), Record);
369 Record.push_back(T->getNumArgs());
370 for (DependentTemplateSpecializationType::iterator
371 I = T->begin(), E = T->end(); I != E; ++I)
372 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
Douglas Gregor7536dd52010-12-20 02:24:11 +0000376void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
377 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000378 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000379 Record.push_back(*NumExpansions + 1);
380 else
381 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000382 Code = TYPE_PACK_EXPANSION;
383}
384
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000385void ASTTypeWriter::VisitParenType(const ParenType *T) {
386 Writer.AddTypeRef(T->getInnerType(), Record);
387 Code = TYPE_PAREN;
388}
389
Sebastian Redl3397c552010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000391 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000392 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
393 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000394 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000395}
396
Sebastian Redl3397c552010-08-18 23:56:27 +0000397void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000398 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000399 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000400 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000401}
402
Sebastian Redl3397c552010-08-18 23:56:27 +0000403void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000404 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000405 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000406}
407
Sebastian Redl3397c552010-08-18 23:56:27 +0000408void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000409 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000410 Record.push_back(T->getNumProtocols());
Stephen Hines651f13c2014-04-23 16:59:28 -0700411 for (const auto *I : T->quals())
412 Writer.AddDeclRef(I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000413 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000414}
415
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000416void
Sebastian Redl3397c552010-08-18 23:56:27 +0000417ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000418 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000419 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000420}
421
Eli Friedmanb001de72011-10-06 23:00:33 +0000422void
423ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
424 Writer.AddTypeRef(T->getValueType(), Record);
425 Code = TYPE_ATOMIC;
426}
427
John McCalla1ee0c52009-10-16 21:56:05 +0000428namespace {
429
430class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000431 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000432 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000433
434public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000435 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000436 : Writer(Writer), Record(Record) { }
437
John McCall51bd8032009-10-18 01:05:36 +0000438#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000439#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000440 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000441#include "clang/AST/TypeLocNodes.def"
442
John McCall51bd8032009-10-18 01:05:36 +0000443 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
444 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000445};
446
447}
448
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
450 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000453 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
454 if (TL.needsExtraLocalData()) {
455 Record.push_back(TL.getWrittenTypeSpec());
456 Record.push_back(TL.getWrittenSignSpec());
457 Record.push_back(TL.getWrittenWidthSpec());
458 Record.push_back(TL.hasModeAttr());
459 }
John McCalla1ee0c52009-10-16 21:56:05 +0000460}
John McCall51bd8032009-10-18 01:05:36 +0000461void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000463}
John McCall51bd8032009-10-18 01:05:36 +0000464void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
465 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000466}
Reid Kleckner12df2462013-06-24 17:51:48 +0000467void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
468 // nothing to do
469}
Stephen Hines651f13c2014-04-23 16:59:28 -0700470void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
471 // nothing to do
472}
John McCall51bd8032009-10-18 01:05:36 +0000473void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000475}
John McCall51bd8032009-10-18 01:05:36 +0000476void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000478}
John McCall51bd8032009-10-18 01:05:36 +0000479void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000481}
John McCall51bd8032009-10-18 01:05:36 +0000482void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000484 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000485}
John McCall51bd8032009-10-18 01:05:36 +0000486void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
488 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
489 Record.push_back(TL.getSizeExpr() ? 1 : 0);
490 if (TL.getSizeExpr())
491 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000492}
John McCall51bd8032009-10-18 01:05:36 +0000493void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
494 VisitArrayTypeLoc(TL);
495}
496void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
497 VisitArrayTypeLoc(TL);
498}
499void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
500 VisitArrayTypeLoc(TL);
501}
502void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
503 DependentSizedArrayTypeLoc TL) {
504 VisitArrayTypeLoc(TL);
505}
506void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
507 DependentSizedExtVectorTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
513void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
516void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000517 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000518 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
519 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000520 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Stephen Hines651f13c2014-04-23 16:59:28 -0700521 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
522 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall51bd8032009-10-18 01:05:36 +0000523}
524void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
525 VisitFunctionTypeLoc(TL);
526}
527void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
528 VisitFunctionTypeLoc(TL);
529}
John McCalled976492009-12-04 22:46:56 +0000530void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall51bd8032009-10-18 01:05:36 +0000533void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
536void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000537 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
538 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
539 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000540}
541void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000542 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
543 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
544 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
545 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000546}
547void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
Sean Huntca63c202011-05-24 22:41:36 +0000550void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getKWLoc(), Record);
552 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
553 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
554 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
555}
Richard Smith34b41d92011-02-20 03:19:35 +0000556void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall51bd8032009-10-18 01:05:36 +0000559void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
562void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
John McCall9d156a72011-01-06 01:58:22 +0000565void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
567 if (TL.hasAttrOperand()) {
568 SourceRange range = TL.getAttrOperandParensRange();
569 Writer.AddSourceLocation(range.getBegin(), Record);
570 Writer.AddSourceLocation(range.getEnd(), Record);
571 }
572 if (TL.hasAttrExprOperand()) {
573 Expr *operand = TL.getAttrExprOperand();
574 Record.push_back(operand ? 1 : 0);
575 if (operand) Writer.AddStmt(operand);
576 } else if (TL.hasAttrEnumOperand()) {
577 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
578 }
579}
John McCall51bd8032009-10-18 01:05:36 +0000580void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
John McCall49a832b2009-10-18 09:09:24 +0000583void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
584 SubstTemplateTypeParmTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
586}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000587void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
588 SubstTemplateTypeParmPackTypeLoc TL) {
589 Writer.AddSourceLocation(TL.getNameLoc(), Record);
590}
John McCall51bd8032009-10-18 01:05:36 +0000591void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
592 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000594 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
595 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
596 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
597 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000598 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
599 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000600}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000601void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
602 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
603 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
604}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000605void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000606 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000607 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000608}
John McCall3cb0ebd2010-03-10 03:28:59 +0000609void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getNameLoc(), Record);
611}
Douglas Gregor4714c122010-03-31 17:34:00 +0000612void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000613 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000614 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000615 Writer.AddSourceLocation(TL.getNameLoc(), Record);
616}
John McCall33500952010-06-11 00:33:02 +0000617void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
618 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000619 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000620 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000621 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000622 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000623 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
624 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
625 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000626 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
627 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000628}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000629void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
630 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
631}
John McCall51bd8032009-10-18 01:05:36 +0000632void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
633 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000634}
635void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
636 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000637 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
638 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
639 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
640 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000641}
John McCall54e14c42009-10-22 22:37:11 +0000642void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
643 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000644}
Eli Friedmanb001de72011-10-06 23:00:33 +0000645void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
646 Writer.AddSourceLocation(TL.getKWLoc(), Record);
647 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
648 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
649}
John McCalla1ee0c52009-10-16 21:56:05 +0000650
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000651//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000652// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000653//===----------------------------------------------------------------------===//
654
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000655static void EmitBlockID(unsigned ID, const char *Name,
656 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000657 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000658 Record.clear();
659 Record.push_back(ID);
660 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
661
662 // Emit the block name if present.
663 if (Name == 0 || Name[0] == 0) return;
664 Record.clear();
665 while (*Name)
666 Record.push_back(*Name++);
667 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
668}
669
670static void EmitRecordID(unsigned ID, const char *Name,
671 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000672 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000673 Record.clear();
674 Record.push_back(ID);
675 while (*Name)
676 Record.push_back(*Name++);
677 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000678}
679
680static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000681 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000682#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000683 RECORD(STMT_STOP);
684 RECORD(STMT_NULL_PTR);
685 RECORD(STMT_NULL);
686 RECORD(STMT_COMPOUND);
687 RECORD(STMT_CASE);
688 RECORD(STMT_DEFAULT);
689 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000690 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000691 RECORD(STMT_IF);
692 RECORD(STMT_SWITCH);
693 RECORD(STMT_WHILE);
694 RECORD(STMT_DO);
695 RECORD(STMT_FOR);
696 RECORD(STMT_GOTO);
697 RECORD(STMT_INDIRECT_GOTO);
698 RECORD(STMT_CONTINUE);
699 RECORD(STMT_BREAK);
700 RECORD(STMT_RETURN);
701 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000702 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000703 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000704 RECORD(EXPR_PREDEFINED);
705 RECORD(EXPR_DECL_REF);
706 RECORD(EXPR_INTEGER_LITERAL);
707 RECORD(EXPR_FLOATING_LITERAL);
708 RECORD(EXPR_IMAGINARY_LITERAL);
709 RECORD(EXPR_STRING_LITERAL);
710 RECORD(EXPR_CHARACTER_LITERAL);
711 RECORD(EXPR_PAREN);
712 RECORD(EXPR_UNARY_OPERATOR);
713 RECORD(EXPR_SIZEOF_ALIGN_OF);
714 RECORD(EXPR_ARRAY_SUBSCRIPT);
715 RECORD(EXPR_CALL);
716 RECORD(EXPR_MEMBER);
717 RECORD(EXPR_BINARY_OPERATOR);
718 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
719 RECORD(EXPR_CONDITIONAL_OPERATOR);
720 RECORD(EXPR_IMPLICIT_CAST);
721 RECORD(EXPR_CSTYLE_CAST);
722 RECORD(EXPR_COMPOUND_LITERAL);
723 RECORD(EXPR_EXT_VECTOR_ELEMENT);
724 RECORD(EXPR_INIT_LIST);
725 RECORD(EXPR_DESIGNATED_INIT);
726 RECORD(EXPR_IMPLICIT_VALUE_INIT);
727 RECORD(EXPR_VA_ARG);
728 RECORD(EXPR_ADDR_LABEL);
729 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000730 RECORD(EXPR_CHOOSE);
731 RECORD(EXPR_GNU_NULL);
732 RECORD(EXPR_SHUFFLE_VECTOR);
733 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000734 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000735 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000736 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000737 RECORD(EXPR_OBJC_ARRAY_LITERAL);
738 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000739 RECORD(EXPR_OBJC_ENCODE);
740 RECORD(EXPR_OBJC_SELECTOR_EXPR);
741 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
742 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
743 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
744 RECORD(EXPR_OBJC_KVC_REF_EXPR);
745 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000746 RECORD(STMT_OBJC_FOR_COLLECTION);
747 RECORD(STMT_OBJC_CATCH);
748 RECORD(STMT_OBJC_FINALLY);
749 RECORD(STMT_OBJC_AT_TRY);
750 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
751 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000752 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000753 RECORD(EXPR_CXX_OPERATOR_CALL);
754 RECORD(EXPR_CXX_CONSTRUCT);
755 RECORD(EXPR_CXX_STATIC_CAST);
756 RECORD(EXPR_CXX_DYNAMIC_CAST);
757 RECORD(EXPR_CXX_REINTERPRET_CAST);
758 RECORD(EXPR_CXX_CONST_CAST);
759 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000760 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smith7c3e6152013-06-12 22:31:48 +0000761 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000762 RECORD(EXPR_CXX_BOOL_LITERAL);
763 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000764 RECORD(EXPR_CXX_TYPEID_EXPR);
765 RECORD(EXPR_CXX_TYPEID_TYPE);
766 RECORD(EXPR_CXX_UUIDOF_EXPR);
767 RECORD(EXPR_CXX_UUIDOF_TYPE);
768 RECORD(EXPR_CXX_THIS);
769 RECORD(EXPR_CXX_THROW);
770 RECORD(EXPR_CXX_DEFAULT_ARG);
771 RECORD(EXPR_CXX_BIND_TEMPORARY);
772 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
773 RECORD(EXPR_CXX_NEW);
774 RECORD(EXPR_CXX_DELETE);
775 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
776 RECORD(EXPR_EXPR_WITH_CLEANUPS);
777 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
778 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
779 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
780 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
781 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000782 RECORD(EXPR_CXX_NOEXCEPT);
783 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000784 RECORD(EXPR_PACK_EXPANSION);
785 RECORD(EXPR_SIZEOF_PACK);
786 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000787 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000788#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000789}
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Sebastian Redla4232eb2010-08-18 23:56:21 +0000791void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000792 RecordData Record;
793 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000795#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
796#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000798 // Control Block.
799 BLOCK(CONTROL_BLOCK);
800 RECORD(METADATA);
801 RECORD(IMPORTS);
802 RECORD(LANGUAGE_OPTIONS);
803 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000804 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000805 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000806 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000807 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000808 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000809 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000810 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000811 RECORD(PREPROCESSOR_OPTIONS);
812
Douglas Gregorc337fef2012-10-19 00:45:00 +0000813 BLOCK(INPUT_FILES_BLOCK);
814 RECORD(INPUT_FILE);
815
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000816 // AST Top-Level Block.
817 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000818 RECORD(TYPE_OFFSET);
819 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000820 RECORD(IDENTIFIER_OFFSET);
821 RECORD(IDENTIFIER_TABLE);
Stephen Hines651f13c2014-04-23 16:59:28 -0700822 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000823 RECORD(SPECIAL_TYPES);
824 RECORD(STATISTICS);
825 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000826 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000827 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000828 RECORD(SELECTOR_OFFSETS);
829 RECORD(METHOD_POOL);
830 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000831 RECORD(SOURCE_LOCATION_OFFSETS);
832 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000833 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000834 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000835 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000836 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000837 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000838 RECORD(SEMA_DECL_REFS);
839 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
840 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
841 RECORD(DECL_REPLACEMENTS);
842 RECORD(UPDATE_VISIBLE);
843 RECORD(DECL_UPDATE_OFFSETS);
844 RECORD(DECL_UPDATES);
845 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
846 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000847 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000848 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000849 RECORD(FP_PRAGMA_OPTIONS);
850 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000851 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000852 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000853 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000854 RECORD(MODULE_OFFSET_MAP);
855 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000856 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000857 RECORD(FILE_SORTED_DECLS);
858 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000859 RECORD(MERGED_DECLARATIONS);
860 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000861 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000862 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000863 RECORD(MACRO_TABLE);
Richard Smithac32d902013-08-07 21:41:30 +0000864 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000865
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000866 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000867 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000868 RECORD(SM_SLOC_FILE_ENTRY);
869 RECORD(SM_SLOC_BUFFER_ENTRY);
870 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000871 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000873 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000874 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000875 RECORD(PP_MACRO_OBJECT_LIKE);
876 RECORD(PP_MACRO_FUNCTION_LIKE);
877 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000878
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000879 // Decls and Types block.
880 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000881 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000882 RECORD(TYPE_COMPLEX);
883 RECORD(TYPE_POINTER);
884 RECORD(TYPE_BLOCK_POINTER);
885 RECORD(TYPE_LVALUE_REFERENCE);
886 RECORD(TYPE_RVALUE_REFERENCE);
887 RECORD(TYPE_MEMBER_POINTER);
888 RECORD(TYPE_CONSTANT_ARRAY);
889 RECORD(TYPE_INCOMPLETE_ARRAY);
890 RECORD(TYPE_VARIABLE_ARRAY);
891 RECORD(TYPE_VECTOR);
892 RECORD(TYPE_EXT_VECTOR);
893 RECORD(TYPE_FUNCTION_PROTO);
894 RECORD(TYPE_FUNCTION_NO_PROTO);
895 RECORD(TYPE_TYPEDEF);
896 RECORD(TYPE_TYPEOF_EXPR);
897 RECORD(TYPE_TYPEOF);
898 RECORD(TYPE_RECORD);
899 RECORD(TYPE_ENUM);
900 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000901 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000902 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000903 RECORD(TYPE_DECLTYPE);
904 RECORD(TYPE_ELABORATED);
905 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
906 RECORD(TYPE_UNRESOLVED_USING);
907 RECORD(TYPE_INJECTED_CLASS_NAME);
908 RECORD(TYPE_OBJC_OBJECT);
909 RECORD(TYPE_TEMPLATE_TYPE_PARM);
910 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
911 RECORD(TYPE_DEPENDENT_NAME);
912 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
913 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
914 RECORD(TYPE_PAREN);
915 RECORD(TYPE_PACK_EXPANSION);
916 RECORD(TYPE_ATTRIBUTED);
917 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000918 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000919 RECORD(DECL_TYPEDEF);
920 RECORD(DECL_ENUM);
921 RECORD(DECL_RECORD);
922 RECORD(DECL_ENUM_CONSTANT);
923 RECORD(DECL_FUNCTION);
924 RECORD(DECL_OBJC_METHOD);
925 RECORD(DECL_OBJC_INTERFACE);
926 RECORD(DECL_OBJC_PROTOCOL);
927 RECORD(DECL_OBJC_IVAR);
928 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000929 RECORD(DECL_OBJC_CATEGORY);
930 RECORD(DECL_OBJC_CATEGORY_IMPL);
931 RECORD(DECL_OBJC_IMPLEMENTATION);
932 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
933 RECORD(DECL_OBJC_PROPERTY);
934 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000935 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000936 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000937 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000938 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000939 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000940 RECORD(DECL_FILE_SCOPE_ASM);
941 RECORD(DECL_BLOCK);
942 RECORD(DECL_CONTEXT_LEXICAL);
943 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000944 RECORD(DECL_NAMESPACE);
945 RECORD(DECL_NAMESPACE_ALIAS);
946 RECORD(DECL_USING);
947 RECORD(DECL_USING_SHADOW);
948 RECORD(DECL_USING_DIRECTIVE);
949 RECORD(DECL_UNRESOLVED_USING_VALUE);
950 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
951 RECORD(DECL_LINKAGE_SPEC);
952 RECORD(DECL_CXX_RECORD);
953 RECORD(DECL_CXX_METHOD);
954 RECORD(DECL_CXX_CONSTRUCTOR);
955 RECORD(DECL_CXX_DESTRUCTOR);
956 RECORD(DECL_CXX_CONVERSION);
957 RECORD(DECL_ACCESS_SPEC);
958 RECORD(DECL_FRIEND);
959 RECORD(DECL_FRIEND_TEMPLATE);
960 RECORD(DECL_CLASS_TEMPLATE);
961 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
962 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000963 RECORD(DECL_VAR_TEMPLATE);
964 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
965 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000966 RECORD(DECL_FUNCTION_TEMPLATE);
967 RECORD(DECL_TEMPLATE_TYPE_PARM);
968 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
969 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
970 RECORD(DECL_STATIC_ASSERT);
971 RECORD(DECL_CXX_BASE_SPECIFIERS);
972 RECORD(DECL_INDIRECTFIELD);
973 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
974
Douglas Gregora72d8c42011-06-03 02:27:19 +0000975 // Statements and Exprs can occur in the Decls and Types block.
976 AddStmtsExprs(Stream, Record);
977
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000978 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000979 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000980 RECORD(PPD_MACRO_DEFINITION);
981 RECORD(PPD_INCLUSION_DIRECTIVE);
982
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000983#undef RECORD
984#undef BLOCK
985 Stream.ExitBlock();
986}
987
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988/// \brief Adjusts the given filename to only write out the portion of the
989/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000990///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991/// \param Filename the file name to adjust.
992///
993/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
994/// the returned filename will be adjusted by this system root.
995///
996/// \returns either the original filename (if it needs no adjustment) or the
997/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000998static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000999adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +00001000 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregor832d6202011-07-22 16:35:34 +00001002 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +00001003 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Douglas Gregore650c8c2009-07-07 00:12:59 +00001005 // Verify that the filename and the system root have the same prefix.
1006 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +00001007 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +00001008 if (Filename[Pos] != isysroot[Pos])
1009 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Douglas Gregore650c8c2009-07-07 00:12:59 +00001011 // We hit the end of the filename before we hit the end of the system root.
1012 if (!Filename[Pos])
1013 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregore650c8c2009-07-07 00:12:59 +00001015 // If the file name has a '/' at the current position, skip over the '/'.
1016 // We distinguish sysroot-based includes from absolute includes by the
1017 // absence of '/' at the beginning of sysroot-based includes.
1018 if (Filename[Pos] == '/')
1019 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Douglas Gregore650c8c2009-07-07 00:12:59 +00001021 return Filename + Pos;
1022}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001023
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001024/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001025void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1026 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001027 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001028 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001029 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1030 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001031
Douglas Gregore650c8c2009-07-07 00:12:59 +00001032 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001033 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1035 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1036 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1037 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1038 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1039 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1040 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1041 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1042 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1043 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001044 Record.push_back(VERSION_MAJOR);
1045 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001046 Record.push_back(CLANG_VERSION_MAJOR);
1047 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001048 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001049 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001050 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1051 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001052
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001053 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001054 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001055 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregore95b9192011-08-17 21:07:30 +00001056 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001057
1058 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1059 M != MEnd; ++M) {
1060 // Skip modules that weren't directly imported.
1061 if (!(*M)->isDirectlyImported())
1062 continue;
1063
1064 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001065 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001066 Record.push_back((*M)->File->getSize());
1067 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001068 // FIXME: This writes the absolute path for AST files we depend on.
1069 const std::string &FileName = (*M)->FileName;
1070 Record.push_back(FileName.size());
1071 Record.append(FileName.begin(), FileName.end());
1072 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001073 Stream.EmitRecord(IMPORTS, Record);
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001076 // Language options.
1077 Record.clear();
1078 const LangOptions &LangOpts = Context.getLangOpts();
1079#define LANGOPT(Name, Bits, Default, Description) \
1080 Record.push_back(LangOpts.Name);
1081#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1082 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1083#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001084#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1085#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001086
1087 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1088 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1089
1090 Record.push_back(LangOpts.CurrentModule.size());
1091 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001092
1093 // Comment options.
1094 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1095 for (CommentOptions::BlockCommandNamesTy::const_iterator
1096 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1097 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1098 I != IEnd; ++I) {
1099 AddString(*I, Record);
1100 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001101 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001102
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001103 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1104
Douglas Gregoree097c12012-10-18 17:58:09 +00001105 // Target options.
1106 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001107 const TargetInfo &Target = Context.getTargetInfo();
1108 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001109 AddString(TargetOpts.Triple, Record);
1110 AddString(TargetOpts.CPU, Record);
1111 AddString(TargetOpts.ABI, Record);
Douglas Gregoree097c12012-10-18 17:58:09 +00001112 AddString(TargetOpts.LinkerVersion, Record);
1113 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1114 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1115 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1116 }
1117 Record.push_back(TargetOpts.Features.size());
1118 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1119 AddString(TargetOpts.Features[I], Record);
1120 }
1121 Stream.EmitRecord(TARGET_OPTIONS, Record);
1122
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001123 // Diagnostic options.
1124 Record.clear();
1125 const DiagnosticOptions &DiagOpts
1126 = Context.getDiagnostics().getDiagnosticOptions();
1127#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1128#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1129 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1130#include "clang/Basic/DiagnosticOptions.def"
1131 Record.push_back(DiagOpts.Warnings.size());
1132 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1133 AddString(DiagOpts.Warnings[I], Record);
1134 // Note: we don't serialize the log or serialization file names, because they
1135 // are generally transient files and will almost always be overridden.
1136 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1137
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001138 // File system options.
1139 Record.clear();
1140 const FileSystemOptions &FSOpts
1141 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1142 AddString(FSOpts.WorkingDir, Record);
1143 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1144
Douglas Gregorbbf38312012-10-24 16:50:34 +00001145 // Header search options.
1146 Record.clear();
1147 const HeaderSearchOptions &HSOpts
1148 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1149 AddString(HSOpts.Sysroot, Record);
1150
1151 // Include entries.
1152 Record.push_back(HSOpts.UserEntries.size());
1153 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1154 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1155 AddString(Entry.Path, Record);
1156 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001157 Record.push_back(Entry.IsFramework);
1158 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001159 }
1160
1161 // System header prefixes.
1162 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1163 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1164 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1165 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1166 }
1167
1168 AddString(HSOpts.ResourceDir, Record);
1169 AddString(HSOpts.ModuleCachePath, Record);
Stephen Hines651f13c2014-04-23 16:59:28 -07001170 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001171 Record.push_back(HSOpts.DisableModuleHash);
1172 Record.push_back(HSOpts.UseBuiltinIncludes);
1173 Record.push_back(HSOpts.UseStandardSystemIncludes);
1174 Record.push_back(HSOpts.UseStandardCXXIncludes);
1175 Record.push_back(HSOpts.UseLibcxx);
1176 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1177
Douglas Gregora71a7d82012-10-24 20:05:57 +00001178 // Preprocessor options.
1179 Record.clear();
1180 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1181
1182 // Macro definitions.
1183 Record.push_back(PPOpts.Macros.size());
1184 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1185 AddString(PPOpts.Macros[I].first, Record);
1186 Record.push_back(PPOpts.Macros[I].second);
1187 }
1188
1189 // Includes
1190 Record.push_back(PPOpts.Includes.size());
1191 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1192 AddString(PPOpts.Includes[I], Record);
1193
1194 // Macro includes
1195 Record.push_back(PPOpts.MacroIncludes.size());
1196 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1197 AddString(PPOpts.MacroIncludes[I], Record);
1198
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001199 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001200 // Detailed record is important since it is used for the module cache hash.
1201 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001202 AddString(PPOpts.ImplicitPCHInclude, Record);
1203 AddString(PPOpts.ImplicitPTHInclude, Record);
1204 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1205 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1206
Douglas Gregor31d375f2011-05-06 21:43:30 +00001207 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001208 SourceManager &SM = Context.getSourceManager();
1209 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1210 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001211 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1212 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001213 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1214 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1215
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001216 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001218 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001219
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001220 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001221 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001222 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001223 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001224 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001225 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001226 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001227 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001228
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001229 Record.clear();
1230 Record.push_back(SM.getMainFileID().getOpaqueValue());
1231 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1232
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001233 // Original PCH directory
1234 if (!OutputFile.empty() && OutputFile != "-") {
1235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1236 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1238 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1239
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001240 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001241
1242 llvm::sys::fs::make_absolute(OutputPath);
1243 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1244
1245 RecordData Record;
1246 Record.push_back(ORIGINAL_PCH_DIR);
1247 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1248 }
1249
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001250 WriteInputFiles(Context.SourceMgr,
1251 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregorb22d1942013-07-22 20:48:33 +00001252 isysroot,
1253 PP.getLangOpts().Modules);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001254 Stream.ExitBlock();
1255}
1256
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001257namespace {
1258 /// \brief An input file.
1259 struct InputFileEntry {
1260 const FileEntry *File;
1261 bool IsSystemFile;
1262 bool BufferOverridden;
1263 };
1264}
1265
1266void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1267 HeaderSearchOptions &HSOpts,
Douglas Gregorb22d1942013-07-22 20:48:33 +00001268 StringRef isysroot,
1269 bool Modules) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001270 using namespace llvm;
1271 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1272 RecordData Record;
1273
1274 // Create input-file abbreviation.
1275 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1276 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001277 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001278 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1279 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001280 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001281 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1282 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1283
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001284 // Get all ContentCache objects for files, sorted by whether the file is a
1285 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001286 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001287 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1288 // Get this source location entry.
1289 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001290 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001291
1292 // We only care about file entries that were not overridden.
1293 if (!SLoc->isFile())
1294 continue;
1295 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001296 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001297 continue;
1298
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001299 InputFileEntry Entry;
1300 Entry.File = Cache->OrigEntry;
1301 Entry.IsSystemFile = Cache->IsSystemFile;
1302 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001303 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001304 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001305 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001306 SortedFiles.push_front(Entry);
1307 }
1308
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001309 unsigned UserFilesNum = 0;
1310 // Write out all of the input files.
1311 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001312 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001313 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001314 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001315
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001316 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001317 if (InputFileID != 0)
1318 continue; // already recorded this file.
1319
Douglas Gregora930dc92012-10-22 18:42:04 +00001320 // Record this entry's offset.
1321 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001322
1323 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001324
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001325 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001326 ++UserFilesNum;
1327
Douglas Gregor745e6f12012-10-19 00:38:02 +00001328 Record.clear();
1329 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001330 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001331
1332 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001333 Record.push_back(Entry.File->getSize());
1334 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001335
Douglas Gregora930dc92012-10-22 18:42:04 +00001336 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001337 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001338
Douglas Gregor745e6f12012-10-19 00:38:02 +00001339 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001340 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001341 SmallString<128> FilePath(Filename);
1342
1343 // Ask the file manager to fixup the relative path for us. This will
1344 // honor the working directory.
Stephen Hines651f13c2014-04-23 16:59:28 -07001345 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001346
1347 // FIXME: This call to make_absolute shouldn't be necessary, the
1348 // call to FixupRelativePath should always return an absolute path.
1349 llvm::sys::fs::make_absolute(FilePath);
1350 Filename = FilePath.c_str();
1351
1352 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1353
1354 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1355 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001356
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001357 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001358
1359 // Create input file offsets abbreviation.
1360 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1361 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1362 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001363 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1364 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001365 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1366 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1367
1368 // Write input file offsets.
1369 Record.clear();
1370 Record.push_back(INPUT_FILE_OFFSETS);
1371 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001372 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001373 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001374}
1375
Douglas Gregor14f79002009-04-10 03:52:48 +00001376//===----------------------------------------------------------------------===//
1377// Source Manager Serialization
1378//===----------------------------------------------------------------------===//
1379
1380/// \brief Create an abbreviation for the SLocEntry that refers to a
1381/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001382static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001383 using namespace llvm;
1384 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001385 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001390 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001395 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001396}
1397
1398/// \brief Create an abbreviation for the SLocEntry that refers to a
1399/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001400static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001401 using namespace llvm;
1402 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001403 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001409 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001410}
1411
1412/// \brief Create an abbreviation for the SLocEntry that refers to a
1413/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001414static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001415 using namespace llvm;
1416 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001417 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001419 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001420}
1421
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001422/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1423/// expansion.
1424static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001425 using namespace llvm;
1426 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001427 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001433 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001434}
1435
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001436namespace {
1437 // Trait used for the on-disk hash table of header search information.
1438 class HeaderFileInfoTrait {
1439 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001440 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001441
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001442 // Keep track of the framework names we've used during serialization.
1443 SmallVector<char, 128> FrameworkStringData;
1444 llvm::StringMap<unsigned> FrameworkNameOffset;
1445
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001446 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001447 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1448 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001449
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001450 struct key_type {
1451 const FileEntry *FE;
1452 const char *Filename;
1453 };
1454 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001455
1456 typedef HeaderFileInfo data_type;
1457 typedef const data_type &data_type_ref;
1458
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001459 static unsigned ComputeHash(key_type_ref key) {
1460 // The hash is based only on size/time of the file, so that the reader can
1461 // match even when symlinking or excess path elements ("foo/../", "../")
1462 // change the form of the name. However, complete path is still the key.
1463 return llvm::hash_combine(key.FE->getSize(),
1464 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465 }
1466
1467 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001468 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001469 using namespace llvm::support;
1470 endian::Writer<little> Writer(Out);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001471 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001472 Writer.write<uint16_t>(KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001473 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001474 if (Data.isModuleHeader)
1475 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07001476 Writer.write<uint8_t>(DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001477 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001478 }
1479
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001480 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001481 using namespace llvm::support;
1482 endian::Writer<little> LE(Out);
1483 LE.write<uint64_t>(key.FE->getSize());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001484 KeyLen -= 8;
Stephen Hines651f13c2014-04-23 16:59:28 -07001485 LE.write<uint64_t>(key.FE->getModificationTime());
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001486 KeyLen -= 8;
1487 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001488 }
1489
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001490 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001491 data_type_ref Data, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001492 using namespace llvm::support;
1493 endian::Writer<little> LE(Out);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001494 uint64_t Start = Out.tell(); (void)Start;
1495
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001496 unsigned char Flags = (Data.HeaderRole << 6)
1497 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001498 | (Data.isPragmaOnce << 4)
1499 | (Data.DirInfo << 2)
1500 | (Data.Resolved << 1)
1501 | Data.IndexHeaderMapHeader;
Stephen Hines651f13c2014-04-23 16:59:28 -07001502 LE.write<uint8_t>(Flags);
1503 LE.write<uint16_t>(Data.NumIncludes);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001504
1505 if (!Data.ControllingMacro)
Stephen Hines651f13c2014-04-23 16:59:28 -07001506 LE.write<uint32_t>(Data.ControllingMacroID);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001507 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001508 LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001509
1510 unsigned Offset = 0;
1511 if (!Data.Framework.empty()) {
1512 // If this header refers into a framework, save the framework name.
1513 llvm::StringMap<unsigned>::iterator Pos
1514 = FrameworkNameOffset.find(Data.Framework);
1515 if (Pos == FrameworkNameOffset.end()) {
1516 Offset = FrameworkStringData.size() + 1;
1517 FrameworkStringData.append(Data.Framework.begin(),
1518 Data.Framework.end());
1519 FrameworkStringData.push_back(0);
1520
1521 FrameworkNameOffset[Data.Framework] = Offset;
1522 } else
1523 Offset = Pos->second;
1524 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001525 LE.write<uint32_t>(Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001526
1527 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001528 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Stephen Hines651f13c2014-04-23 16:59:28 -07001529 LE.write<uint32_t>(Writer.getExistingSubmoduleID(Mod));
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001530 }
1531
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001532 assert(Out.tell() - Start == DataLen && "Wrong data length");
1533 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001534
1535 const char *strings_begin() const { return FrameworkStringData.begin(); }
1536 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001537 };
1538} // end anonymous namespace
1539
1540/// \brief Write the header search block for the list of files that
1541///
1542/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001543void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001544 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001545 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1546
1547 if (FilesByUID.size() > HS.header_file_size())
1548 FilesByUID.resize(HS.header_file_size());
1549
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001550 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001551 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001552 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001553 unsigned NumHeaderSearchEntries = 0;
1554 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1555 const FileEntry *File = FilesByUID[UID];
1556 if (!File)
1557 continue;
1558
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001559 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1560 // from the external source if it was not provided already.
Stephen Hines651f13c2014-04-23 16:59:28 -07001561 HeaderFileInfo HFI;
1562 if (!HS.tryGetFileInfo(File, HFI) ||
1563 (HFI.External && Chain) ||
1564 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001565 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001566
1567 // Turn the file name into an absolute path, if it isn't already.
1568 const char *Filename = File->getName();
1569 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1570
1571 // If we performed any translation on the file name at all, we need to
1572 // save this string, since the generator will refer to it later.
1573 if (Filename != File->getName()) {
1574 Filename = strdup(Filename);
1575 SavedStrings.push_back(Filename);
1576 }
1577
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001578 HeaderFileInfoTrait::key_type key = { File, Filename };
1579 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001580 ++NumHeaderSearchEntries;
1581 }
1582
1583 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001584 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001585 uint32_t BucketOffset;
1586 {
Stephen Hines651f13c2014-04-23 16:59:28 -07001587 using namespace llvm::support;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001588 llvm::raw_svector_ostream Out(TableData);
1589 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07001590 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001591 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1592 }
1593
1594 // Create a blob abbreviation
1595 using namespace llvm;
1596 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1597 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1599 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001600 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1602 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1603
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001604 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001605 RecordData Record;
1606 Record.push_back(HEADER_SEARCH_TABLE);
1607 Record.push_back(BucketOffset);
1608 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001609 Record.push_back(TableData.size());
1610 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001611 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1612
1613 // Free all of the strings we had to duplicate.
1614 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001615 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001616}
1617
Douglas Gregor14f79002009-04-10 03:52:48 +00001618/// \brief Writes the block containing the serialized form of the
1619/// source manager.
1620///
1621/// TODO: We should probably use an on-disk hash table (stored in a
1622/// blob), indexed based on the file name, so that we only create
1623/// entries for files that we actually need. In the common case (no
1624/// errors), we probably won't have to create file entries for any of
1625/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001626void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001627 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001628 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001629 RecordData Record;
1630
Chris Lattnerf04ad692009-04-10 17:16:57 +00001631 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001632 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001633
1634 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001635 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1636 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1637 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001638 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001639
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001640 // Write out the source location entry table. We skip the first
1641 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001642 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001643 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001644 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1645 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001646 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001647 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001648 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001649 FileID FID = FileID::get(I);
1650 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001651
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001652 // Record the offset of this source-location entry.
1653 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1654
1655 // Figure out which record code to use.
1656 unsigned Code;
1657 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001658 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1659 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001660 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001661 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001662 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001663 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001664 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001665 Record.clear();
1666 Record.push_back(Code);
1667
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001668 // Starting offset of this entry within this module, so skip the dummy.
1669 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001670 if (SLoc->isFile()) {
1671 const SrcMgr::FileInfo &File = SLoc->getFile();
1672 Record.push_back(File.getIncludeLoc().getRawEncoding());
1673 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1674 Record.push_back(File.hasLineDirectives());
1675
1676 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001677 if (Content->OrigEntry) {
1678 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001679 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001680
Douglas Gregora930dc92012-10-22 18:42:04 +00001681 // The source location entry is a file. Emit input file ID.
1682 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1683 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001685 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001686
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001687 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001688 if (FDI != FileDeclIDs.end()) {
1689 Record.push_back(FDI->second->FirstDeclIndex);
1690 Record.push_back(FDI->second->DeclIDs.size());
1691 } else {
1692 Record.push_back(0);
1693 Record.push_back(0);
1694 }
Douglas Gregora081da52011-11-16 20:05:18 +00001695
Douglas Gregora930dc92012-10-22 18:42:04 +00001696 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001697
1698 if (Content->BufferOverridden) {
1699 Record.clear();
1700 Record.push_back(SM_SLOC_BUFFER_BLOB);
1701 const llvm::MemoryBuffer *Buffer
1702 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1703 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1704 StringRef(Buffer->getBufferStart(),
1705 Buffer->getBufferSize() + 1));
1706 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001707 } else {
1708 // The source location entry is a buffer. The blob associated
1709 // with this entry contains the contents of the buffer.
1710
1711 // We add one to the size so that we capture the trailing NULL
1712 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1713 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001714 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001715 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001716 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001717 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001718 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001719 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001720 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001721 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001722 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001723 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001724
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001725 if (strcmp(Name, "<built-in>") == 0) {
1726 PreloadSLocs.push_back(SLocEntryOffsets.size());
1727 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001728 }
1729 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001730 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001731 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001732 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1733 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001734 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1735 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001736
1737 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001738 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001739 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001740 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001741 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001742 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001743 }
1744 }
1745
Douglas Gregorc9490c02009-04-16 22:23:12 +00001746 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001747
1748 if (SLocEntryOffsets.empty())
1749 return;
1750
Sebastian Redl3397c552010-08-18 23:56:27 +00001751 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001752 // table is used for lazily loading source-location information.
1753 using namespace llvm;
1754 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001755 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001757 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1759 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001761 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001762 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001763 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001764 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001765 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001766
Sebastian Redl3397c552010-08-18 23:56:27 +00001767 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001768 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001769 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001770
1771 // Write the line table. It depends on remapping working, so it must come
1772 // after the source location offsets.
1773 if (SourceMgr.hasLineTable()) {
1774 LineTableInfo &LineTable = SourceMgr.getLineTable();
1775
1776 Record.clear();
1777 // Emit the file names
1778 Record.push_back(LineTable.getNumFilenames());
1779 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1780 // Emit the file name
1781 const char *Filename = LineTable.getFilename(I);
1782 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1783 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1784 Record.push_back(FilenameLen);
1785 if (FilenameLen)
1786 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1787 }
1788
1789 // Emit the line entries
1790 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1791 L != LEnd; ++L) {
1792 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001793 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001794 continue;
1795
1796 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001797 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001798
1799 // Emit the line entries
1800 Record.push_back(L->second.size());
1801 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1802 LEEnd = L->second.end();
1803 LE != LEEnd; ++LE) {
1804 Record.push_back(LE->FileOffset);
1805 Record.push_back(LE->LineNo);
1806 Record.push_back(LE->FilenameID);
1807 Record.push_back((unsigned)LE->FileKind);
1808 Record.push_back(LE->IncludeOffset);
1809 }
1810 }
1811 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1812 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001813}
1814
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001815//===----------------------------------------------------------------------===//
1816// Preprocessor Serialization
1817//===----------------------------------------------------------------------===//
1818
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001819namespace {
1820class ASTMacroTableTrait {
1821public:
1822 typedef IdentID key_type;
1823 typedef key_type key_type_ref;
1824
1825 struct Data {
1826 uint32_t MacroDirectivesOffset;
1827 };
1828
1829 typedef Data data_type;
1830 typedef const data_type &data_type_ref;
1831
1832 static unsigned ComputeHash(IdentID IdID) {
1833 return llvm::hash_value(IdID);
1834 }
1835
1836 std::pair<unsigned,unsigned>
1837 static EmitKeyDataLength(raw_ostream& Out,
1838 key_type_ref Key, data_type_ref Data) {
1839 unsigned KeyLen = 4; // IdentID.
1840 unsigned DataLen = 4; // MacroDirectivesOffset.
1841 return std::make_pair(KeyLen, DataLen);
1842 }
1843
1844 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001845 using namespace llvm::support;
1846 endian::Writer<little>(Out).write<uint32_t>(Key);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001847 }
1848
1849 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1850 unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001851 using namespace llvm::support;
1852 endian::Writer<little>(Out).write<uint32_t>(Data.MacroDirectivesOffset);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001853 }
1854};
1855} // end anonymous namespace
1856
Benjamin Kramer767b3d22013-09-22 14:10:29 +00001857static int compareMacroDirectives(
1858 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1859 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1860 return X->first->getName().compare(Y->first->getName());
Douglas Gregor9c736102011-02-10 18:20:09 +00001861}
1862
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001863static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1864 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001865 if (MacroInfo *MI = MD->getMacroInfo())
1866 if (MI->isBuiltinMacro())
1867 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001868
1869 if (IsModule) {
1870 SourceLocation Loc = MD->getLocation();
1871 if (Loc.isInvalid())
1872 return true;
1873 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1874 return true;
1875 }
1876
1877 return false;
1878}
1879
Chris Lattner0b1fb982009-04-10 17:15:23 +00001880/// \brief Writes the block containing the serialized form of the
1881/// preprocessor.
1882///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001883void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001884 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1885 if (PPRec)
1886 WritePreprocessorDetail(*PPRec);
1887
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001888 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001889
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001890 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1891 if (PP.getCounterValue() != 0) {
1892 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001893 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001894 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001895 }
1896
1897 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001898 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Sebastian Redl3397c552010-08-18 23:56:27 +00001900 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001901 // FIXME: use diagnostics subsystem for localization etc.
1902 if (PP.SawDateOrTime())
1903 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Douglas Gregorecdcb882010-10-20 22:00:55 +00001905
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001906 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001907 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001908
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001909 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001910 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001911 MacroDirectives;
1912 for (Preprocessor::macro_iterator
1913 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1914 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001915 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001916 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001917 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001918
Douglas Gregor9c736102011-02-10 18:20:09 +00001919 // Sort the set of macro definitions that need to be serialized by the
1920 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001921 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1922 &compareMacroDirectives);
1923
1924 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1925
1926 // Emit the macro directives as a list and associate the offset with the
1927 // identifier they belong to.
1928 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1929 const IdentifierInfo *Name = MacroDirectives[I].first;
1930 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1931 MacroDirective *MD = MacroDirectives[I].second;
1932
1933 // If the macro or identifier need no updates, don't write the macro history
1934 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001935 // FIXME: Chain the macro history instead of re-writing it.
1936 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001937 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1938 continue;
1939
1940 // Emit the macro directives in reverse source order.
1941 for (; MD; MD = MD->getPrevious()) {
1942 if (shouldIgnoreMacro(MD, IsModule, PP))
1943 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001944
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001945 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001946 Record.push_back(MD->getKind());
1947 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1948 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1949 Record.push_back(InfoID);
1950 Record.push_back(DefMD->isImported());
1951 Record.push_back(DefMD->isAmbiguous());
1952
1953 } else if (VisibilityMacroDirective *
1954 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1955 Record.push_back(VisMD->isPublic());
1956 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001957 }
1958 if (Record.empty())
1959 continue;
1960
1961 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1962 Record.clear();
1963
1964 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1965
1966 IdentID NameID = getIdentifierRef(Name);
1967 ASTMacroTableTrait::Data data;
1968 data.MacroDirectivesOffset = MacroDirectiveOffset;
1969 Generator.insert(NameID, data);
1970 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001971
Douglas Gregora8235d62012-10-09 23:05:51 +00001972 /// \brief Offsets of each of the macros into the bitstream, indexed by
1973 /// the local macro ID
1974 ///
1975 /// For each identifier that is associated with a macro, this map
1976 /// provides the offset into the bitstream where that macro is
1977 /// defined.
1978 std::vector<uint32_t> MacroOffsets;
1979
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001980 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1981 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1982 MacroInfo *MI = MacroInfosToEmit[I].MI;
1983 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001984
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001985 if (ID < FirstMacroID) {
1986 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1987 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001988 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001989
1990 // Record the local offset of this macro.
1991 unsigned Index = ID - FirstMacroID;
1992 if (Index == MacroOffsets.size())
1993 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1994 else {
1995 if (Index > MacroOffsets.size())
1996 MacroOffsets.resize(Index + 1);
1997
1998 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1999 }
2000
2001 AddIdentifierRef(Name, Record);
2002 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2003 AddSourceLocation(MI->getDefinitionLoc(), Record);
2004 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2005 Record.push_back(MI->isUsed());
2006 unsigned Code;
2007 if (MI->isObjectLike()) {
2008 Code = PP_MACRO_OBJECT_LIKE;
2009 } else {
2010 Code = PP_MACRO_FUNCTION_LIKE;
2011
2012 Record.push_back(MI->isC99Varargs());
2013 Record.push_back(MI->isGNUVarargs());
2014 Record.push_back(MI->hasCommaPasting());
2015 Record.push_back(MI->getNumArgs());
2016 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2017 I != E; ++I)
2018 AddIdentifierRef(*I, Record);
2019 }
2020
2021 // If we have a detailed preprocessing record, record the macro definition
2022 // ID that corresponds to this macro.
2023 if (PPRec)
2024 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2025
2026 Stream.EmitRecord(Code, Record);
2027 Record.clear();
2028
2029 // Emit the tokens array.
2030 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2031 // Note that we know that the preprocessor does not have any annotation
2032 // tokens in it because they are created by the parser, and thus can't
2033 // be in a macro definition.
2034 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002035 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002036 Stream.EmitRecord(PP_TOKEN, Record);
2037 Record.clear();
2038 }
2039 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002040 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002041
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002042 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002043
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002044 // Create the on-disk hash table in a buffer.
2045 SmallString<4096> MacroTable;
2046 uint32_t BucketOffset;
2047 {
Stephen Hines651f13c2014-04-23 16:59:28 -07002048 using namespace llvm::support;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002049 llvm::raw_svector_ostream Out(MacroTable);
2050 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07002051 endian::Writer<little>(Out).write<uint32_t>(0);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002052 BucketOffset = Generator.Emit(Out);
2053 }
2054
2055 // Write the macro table
2056 using namespace llvm;
2057 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2058 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2061 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2062
2063 Record.push_back(MACRO_TABLE);
2064 Record.push_back(BucketOffset);
2065 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2066 Record.clear();
2067
Douglas Gregora8235d62012-10-09 23:05:51 +00002068 // Write the offsets table for macro IDs.
2069 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002070 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002071 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2075
2076 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2077 Record.clear();
2078 Record.push_back(MACRO_OFFSET);
2079 Record.push_back(MacroOffsets.size());
2080 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2081 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2082 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002083}
2084
2085void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002086 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002087 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002088
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002089 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002090
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002091 // Enter the preprocessor block.
2092 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002093
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002094 // If the preprocessor has a preprocessing record, emit it.
2095 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002096 using namespace llvm;
2097
2098 // Set up the abbreviation for
2099 unsigned InclusionAbbrev = 0;
2100 {
2101 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2102 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2108 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2109 }
2110
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002111 unsigned FirstPreprocessorEntityID
2112 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2113 + NUM_PREDEF_PP_ENTITY_IDS;
2114 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002115 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002116 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2117 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002118 E != EEnd;
2119 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002120 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002121
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002122 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2123 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002124
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002125 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002126 // Record this macro definition's ID.
2127 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002128
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002129 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002130 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2131 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002132 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002133
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002134 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002135 Record.push_back(ME->isBuiltinMacro());
2136 if (ME->isBuiltinMacro())
2137 AddIdentifierRef(ME->getName(), Record);
2138 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002139 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002140 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002141 continue;
2142 }
2143
2144 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2145 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002146 Record.push_back(ID->getFileName().size());
2147 Record.push_back(ID->wasInQuotes());
2148 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002149 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002150 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002151 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002152 // Check that the FileEntry is not null because it was not resolved and
2153 // we create a PCH even with compiler errors.
2154 if (ID->getFile())
2155 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002156 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2157 continue;
2158 }
2159
2160 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2161 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002162 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002163
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002164 // Write the offsets table for the preprocessing record.
2165 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002166 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2167
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002168 // Write the offsets table for identifier IDs.
2169 using namespace llvm;
2170 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002171 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002174 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002175
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002176 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002177 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002178 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002179 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2180 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002181 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002182}
2183
Douglas Gregore209e502011-12-06 01:10:29 +00002184unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2185 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2186 if (Known != SubmoduleIDs.end())
2187 return Known->second;
2188
2189 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2190}
2191
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002192unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2193 if (!Mod)
2194 return 0;
2195
2196 llvm::DenseMap<Module *, unsigned>::const_iterator
2197 Known = SubmoduleIDs.find(Mod);
2198 if (Known != SubmoduleIDs.end())
2199 return Known->second;
2200
2201 return 0;
2202}
2203
Douglas Gregor26ced122011-12-01 00:59:36 +00002204/// \brief Compute the number of modules within the given tree (including the
2205/// given module).
2206static unsigned getNumberOfModules(Module *Mod) {
2207 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002208 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2209 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002210 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002211 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002212
2213 return ChildModules + 1;
2214}
2215
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002216void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002217 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002218 // FIXME: This feels like it belongs somewhere else, but there are no
2219 // other consumers of this information.
2220 SourceManager &SrcMgr = PP->getSourceManager();
2221 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Stephen Hines651f13c2014-04-23 16:59:28 -07002222 for (const auto *I : Context->local_imports()) {
Douglas Gregor55988682011-12-05 16:33:54 +00002223 if (Module *ImportedFrom
2224 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2225 SrcMgr))) {
2226 ImportedFrom->Imports.push_back(I->getImportedModule());
2227 }
2228 }
2229
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002230 // Enter the submodule description block.
2231 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2232
2233 // Write the abbreviations needed for the submodules block.
2234 using namespace llvm;
2235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2236 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Stephen Hines651f13c2014-04-23 16:59:28 -07002241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2248 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2249
2250 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002251 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2253 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2254
2255 Abbrev = new BitCodeAbbrev();
2256 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2258 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002259
2260 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002261 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2263 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2264
2265 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002266 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2268 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2269
Douglas Gregor51f564f2011-12-31 04:05:44 +00002270 Abbrev = new BitCodeAbbrev();
2271 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smith5794b532013-10-28 22:18:19 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor51f564f2011-12-31 04:05:44 +00002274 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2275
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002276 Abbrev = new BitCodeAbbrev();
2277 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2279 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2280
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002281 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002282 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2284 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2285
2286 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002287 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2290 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2291
Douglas Gregor63a72682013-03-20 00:22:05 +00002292 Abbrev = new BitCodeAbbrev();
2293 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2295 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2296
Douglas Gregor906d66a2013-03-20 21:10:35 +00002297 Abbrev = new BitCodeAbbrev();
2298 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2301 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2302
Douglas Gregor26ced122011-12-01 00:59:36 +00002303 // Write the submodule metadata block.
2304 RecordData Record;
2305 Record.push_back(getNumberOfModules(WritingModule));
2306 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2307 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2308
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002309 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002310 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002311 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002312 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002313 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002314 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002315 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002316
2317 // Emit the definition of the block.
2318 Record.clear();
2319 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002320 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002321 if (Mod->Parent) {
2322 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2323 Record.push_back(SubmoduleIDs[Mod->Parent]);
2324 } else {
2325 Record.push_back(0);
2326 }
2327 Record.push_back(Mod->IsFramework);
2328 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002329 Record.push_back(Mod->IsSystem);
Stephen Hines651f13c2014-04-23 16:59:28 -07002330 Record.push_back(Mod->IsExternC);
Douglas Gregor1e123682011-12-05 22:27:44 +00002331 Record.push_back(Mod->InferSubmodules);
2332 Record.push_back(Mod->InferExplicitSubmodules);
2333 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002334 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002335 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2336
Douglas Gregor51f564f2011-12-31 04:05:44 +00002337 // Emit the requirements.
Richard Smith5794b532013-10-28 22:18:19 +00002338 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +00002339 Record.clear();
2340 Record.push_back(SUBMODULE_REQUIRES);
Richard Smith5794b532013-10-28 22:18:19 +00002341 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002342 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smith5794b532013-10-28 22:18:19 +00002343 Mod->Requirements[I].first);
Douglas Gregor51f564f2011-12-31 04:05:44 +00002344 }
2345
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002346 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002347 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002348 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002349 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002350 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002351 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002352 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2353 Record.clear();
2354 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2355 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2356 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002357 }
2358
2359 // Emit the headers.
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002360 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002361 Record.clear();
2362 Record.push_back(SUBMODULE_HEADER);
2363 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002364 Mod->NormalHeaders[I]->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002365 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002366 // Emit the excluded headers.
2367 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2368 Record.clear();
2369 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2370 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2371 Mod->ExcludedHeaders[I]->getName());
2372 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002373 // Emit the private headers.
2374 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2375 Record.clear();
2376 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2377 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2378 Mod->PrivateHeaders[I]->getName());
2379 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002380 ArrayRef<const FileEntry *>
2381 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2382 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002383 Record.clear();
2384 Record.push_back(SUBMODULE_TOPHEADER);
2385 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002386 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002387 }
Douglas Gregor55988682011-12-05 16:33:54 +00002388
2389 // Emit the imports.
2390 if (!Mod->Imports.empty()) {
2391 Record.clear();
2392 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002393 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002394 assert(ImportedID && "Unknown submodule!");
2395 Record.push_back(ImportedID);
2396 }
2397 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2398 }
2399
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002400 // Emit the exports.
2401 if (!Mod->Exports.empty()) {
2402 Record.clear();
2403 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002404 if (Module *Exported = Mod->Exports[I].getPointer()) {
2405 unsigned ExportedID = SubmoduleIDs[Exported];
2406 assert(ExportedID > 0 && "Unknown submodule ID?");
2407 Record.push_back(ExportedID);
2408 } else {
2409 Record.push_back(0);
2410 }
2411
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002412 Record.push_back(Mod->Exports[I].getInt());
2413 }
2414 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2415 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002416
Daniel Jasperddd2dfc2013-09-24 09:14:14 +00002417 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2418 // Might be unnecessary as use declarations are only used to build the
2419 // module itself.
2420
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002421 // Emit the link libraries.
2422 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2423 Record.clear();
2424 Record.push_back(SUBMODULE_LINK_LIBRARY);
2425 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2426 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2427 Mod->LinkLibraries[I].Library);
2428 }
2429
Douglas Gregor906d66a2013-03-20 21:10:35 +00002430 // Emit the conflicts.
2431 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2432 Record.clear();
2433 Record.push_back(SUBMODULE_CONFLICT);
2434 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2435 assert(OtherID && "Unknown submodule!");
2436 Record.push_back(OtherID);
2437 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2438 Mod->Conflicts[I].Message);
2439 }
2440
Douglas Gregor63a72682013-03-20 00:22:05 +00002441 // Emit the configuration macros.
2442 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2443 Record.clear();
2444 Record.push_back(SUBMODULE_CONFIG_MACRO);
2445 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2446 Mod->ConfigMacros[I]);
2447 }
2448
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002449 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002450 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2451 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002452 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002453 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002454 }
2455
2456 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002457
2458 assert((NextSubmoduleID - FirstSubmoduleID
2459 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002460}
2461
Douglas Gregor185dbd72011-12-01 02:07:58 +00002462serialization::SubmoduleID
2463ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002464 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002465 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002466
2467 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002468 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002469 Module *OwningMod
2470 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002471 if (!OwningMod)
2472 return 0;
2473
Douglas Gregore209e502011-12-06 01:10:29 +00002474 // Check whether this submodule is part of our own module.
2475 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002476 return 0;
2477
Douglas Gregore209e502011-12-06 01:10:29 +00002478 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002479}
2480
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002481void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2482 bool isModule) {
2483 // Make sure set diagnostic pragmas don't affect the translation unit that
2484 // imports the module.
2485 // FIXME: Make diagnostic pragma sections work properly with modules.
2486 if (isModule)
2487 return;
2488
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002489 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2490 DiagStateIDMap;
2491 unsigned CurrID = 0;
2492 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002493 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002494 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002495 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2496 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002497 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002498 if (point.Loc.isInvalid())
2499 continue;
2500
2501 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002502 unsigned &DiagStateID = DiagStateIDMap[point.State];
2503 Record.push_back(DiagStateID);
2504
2505 if (DiagStateID == 0) {
2506 DiagStateID = ++CurrID;
2507 for (DiagnosticsEngine::DiagState::const_iterator
2508 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2509 if (I->second.isPragma()) {
2510 Record.push_back(I->first);
2511 Record.push_back(I->second.getMapping());
2512 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002513 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002514 Record.push_back(-1); // mark the end of the diag/map pairs for this
2515 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002516 }
2517 }
2518
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002519 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002520 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002521}
2522
Anders Carlssonc8505782011-03-06 18:41:18 +00002523void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2524 if (CXXBaseSpecifiersOffsets.empty())
2525 return;
2526
2527 RecordData Record;
2528
2529 // Create a blob abbreviation for the C++ base specifiers offsets.
2530 using namespace llvm;
2531
2532 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2533 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2536 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2537
Douglas Gregore92b8a12011-08-04 00:01:48 +00002538 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002539 Record.clear();
2540 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2541 Record.push_back(CXXBaseSpecifiersOffsets.size());
2542 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002543 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002544}
2545
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002546//===----------------------------------------------------------------------===//
2547// Type Serialization
2548//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002549
Sebastian Redl3397c552010-08-18 23:56:27 +00002550/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002551void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002552 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002553 if (Idx.getIndex() == 0) // we haven't seen this type before.
2554 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002555
Douglas Gregor97475832010-10-05 18:37:06 +00002556 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002557
Douglas Gregor2cf26342009-04-09 22:27:44 +00002558 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002559 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002560 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002561 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002562 else if (TypeOffsets.size() < Index) {
2563 TypeOffsets.resize(Index + 1);
2564 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002565 }
2566
2567 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Douglas Gregor2cf26342009-04-09 22:27:44 +00002569 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002570 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002571
Douglas Gregora4923eb2009-11-16 21:35:15 +00002572 if (T.hasLocalNonFastQualifiers()) {
2573 Qualifiers Qs = T.getLocalQualifiers();
2574 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002575 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002576 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002577 } else {
2578 switch (T->getTypeClass()) {
2579 // For all of the concrete, non-dependent types, call the
2580 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002581#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002582 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002583#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002584#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002585 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002586 }
2587
2588 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002589 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002590
2591 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002592 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002593}
2594
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002595//===----------------------------------------------------------------------===//
2596// Declaration Serialization
2597//===----------------------------------------------------------------------===//
2598
Douglas Gregor2cf26342009-04-09 22:27:44 +00002599/// \brief Write the block containing all of the declaration IDs
2600/// lexically declared within the given DeclContext.
2601///
2602/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2603/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002604uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002605 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002606 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002607 return 0;
2608
Douglas Gregorc9490c02009-04-16 22:23:12 +00002609 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002610 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002612 SmallVector<KindDeclIDPair, 64> Decls;
Stephen Hines651f13c2014-04-23 16:59:28 -07002613 for (const auto *D : DC->decls())
2614 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002615
Douglas Gregor25123082009-04-22 22:34:57 +00002616 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002617 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002618 return Offset;
2619}
2620
Sebastian Redla4232eb2010-08-18 23:56:21 +00002621void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002622 using namespace llvm;
2623 RecordData Record;
2624
2625 // Write the type offsets array
2626 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002627 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2631 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2632 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002633 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002634 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002635 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002636 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002637
2638 // Write the declaration offsets array
2639 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002640 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002642 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2644 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2645 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002646 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002647 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002648 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002649 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002650}
2651
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002652void ASTWriter::WriteFileDeclIDsMap() {
2653 using namespace llvm;
2654 RecordData Record;
2655
2656 // Join the vectors of DeclIDs from all files.
2657 SmallVector<DeclID, 256> FileSortedIDs;
2658 for (FileDeclIDsTy::iterator
2659 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2660 DeclIDInFileInfo &Info = *FI->second;
2661 Info.FirstDeclIndex = FileSortedIDs.size();
2662 for (LocDeclIDsTy::iterator
2663 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2664 FileSortedIDs.push_back(DI->second);
2665 }
2666
2667 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2668 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2671 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2672 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002673 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002674 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2675}
2676
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002677void ASTWriter::WriteComments() {
2678 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002679 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002680 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002681 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2682 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002683 I != E; ++I) {
2684 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002685 AddSourceRange((*I)->getSourceRange(), Record);
2686 Record.push_back((*I)->getKind());
2687 Record.push_back((*I)->isTrailingComment());
2688 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002689 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2690 }
2691 Stream.ExitBlock();
2692}
2693
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002694//===----------------------------------------------------------------------===//
2695// Global Method Pool and Selector Serialization
2696//===----------------------------------------------------------------------===//
2697
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002698namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002699// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002700class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002701 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002702
2703public:
2704 typedef Selector key_type;
2705 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002706
Sebastian Redl5d050072010-08-04 17:20:04 +00002707 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002708 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002709 ObjCMethodList Instance, Factory;
2710 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002711 typedef const data_type& data_type_ref;
2712
Sebastian Redl3397c552010-08-18 23:56:27 +00002713 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002715 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002716 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
2719 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002720 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002721 data_type_ref Methods) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002722 using namespace llvm::support;
2723 endian::Writer<little> LE(Out);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002724 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
Stephen Hines651f13c2014-04-23 16:59:28 -07002725 LE.write<uint16_t>(KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002726 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2727 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002728 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002729 if (Method->Method)
2730 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002731 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002732 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002733 if (Method->Method)
2734 DataLen += 4;
Stephen Hines651f13c2014-04-23 16:59:28 -07002735 LE.write<uint16_t>(DataLen);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002736 return std::make_pair(KeyLen, DataLen);
2737 }
Mike Stump1eb44332009-09-09 15:08:12 +00002738
Chris Lattner5f9e2722011-07-23 10:55:15 +00002739 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002740 using namespace llvm::support;
2741 endian::Writer<little> LE(Out);
Mike Stump1eb44332009-09-09 15:08:12 +00002742 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002743 assert((Start >> 32) == 0 && "Selector key offset too large");
2744 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002745 unsigned N = Sel.getNumArgs();
Stephen Hines651f13c2014-04-23 16:59:28 -07002746 LE.write<uint16_t>(N);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002747 if (N == 0)
2748 N = 1;
2749 for (unsigned I = 0; I != N; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07002750 LE.write<uint32_t>(
2751 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002752 }
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Chris Lattner5f9e2722011-07-23 10:55:15 +00002754 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002755 data_type_ref Methods, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002756 using namespace llvm::support;
2757 endian::Writer<little> LE(Out);
Douglas Gregora67e58c2009-04-24 21:49:02 +00002758 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07002759 LE.write<uint32_t>(Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002760 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002761 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002762 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002763 if (Method->Method)
2764 ++NumInstanceMethods;
2765
2766 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002767 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002768 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002769 if (Method->Method)
2770 ++NumFactoryMethods;
2771
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002772 unsigned InstanceBits = Methods.Instance.getBits();
2773 assert(InstanceBits < 4);
2774 unsigned NumInstanceMethodsAndBits =
2775 (NumInstanceMethods << 2) | InstanceBits;
2776 unsigned FactoryBits = Methods.Factory.getBits();
2777 assert(FactoryBits < 4);
2778 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
Stephen Hines651f13c2014-04-23 16:59:28 -07002779 LE.write<uint16_t>(NumInstanceMethodsAndBits);
2780 LE.write<uint16_t>(NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002781 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002782 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002783 if (Method->Method)
Stephen Hines651f13c2014-04-23 16:59:28 -07002784 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002785 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002786 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002787 if (Method->Method)
Stephen Hines651f13c2014-04-23 16:59:28 -07002788 LE.write<uint32_t>(Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002789
2790 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002791 }
2792};
2793} // end anonymous namespace
2794
Sebastian Redl059612d2010-08-03 21:58:15 +00002795/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002796///
2797/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002798/// in an on-disk hash table indexed by the selector. The hash table also
2799/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002800void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002801 using namespace llvm;
2802
Sebastian Redl059612d2010-08-03 21:58:15 +00002803 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002804 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002805 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002806 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002807 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002808 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002809 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002810 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Sebastian Redl059612d2010-08-03 21:58:15 +00002812 // Create the on-disk hash table representation. We walk through every
2813 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002814 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002815 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002816 I = SelectorIDs.begin(), E = SelectorIDs.end();
2817 I != E; ++I) {
2818 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002819 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002820 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002821 I->second,
2822 ObjCMethodList(),
2823 ObjCMethodList()
2824 };
2825 if (F != SemaRef.MethodPool.end()) {
2826 Data.Instance = F->second.first;
2827 Data.Factory = F->second.second;
2828 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002829 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002830 // changed.
2831 if (Chain && I->second < FirstSelectorID) {
2832 // Selector already exists. Did it change?
2833 bool changed = false;
2834 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002835 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002836 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002837 changed = true;
2838 }
2839 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002840 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002841 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002842 changed = true;
2843 }
2844 if (!changed)
2845 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002846 } else if (Data.Instance.Method || Data.Factory.Method) {
2847 // A new method pool entry.
2848 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002849 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002850 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002851 }
2852
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002853 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002854 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002855 uint32_t BucketOffset;
2856 {
Stephen Hines651f13c2014-04-23 16:59:28 -07002857 using namespace llvm::support;
Sebastian Redl3397c552010-08-18 23:56:27 +00002858 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002859 llvm::raw_svector_ostream Out(MethodPool);
2860 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07002861 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002862 BucketOffset = Generator.Emit(Out, Trait);
2863 }
2864
2865 // Create a blob abbreviation
2866 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002867 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2871 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2872
Douglas Gregor83941df2009-04-25 17:48:32 +00002873 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002874 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002875 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002876 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002877 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002878 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002879
2880 // Create a blob abbreviation for the selector table offsets.
2881 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002882 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2886 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2887
2888 // Write the selector offsets table.
2889 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002890 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002891 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002892 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002893 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002894 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002895 }
2896}
2897
Sebastian Redl3397c552010-08-18 23:56:27 +00002898/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002899void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002900 using namespace llvm;
2901 if (SemaRef.ReferencedSelectors.empty())
2902 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002903
Fariborz Jahanian32019832010-07-23 19:11:11 +00002904 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002905
Sebastian Redl3397c552010-08-18 23:56:27 +00002906 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002907 // very tricky to fix, and given that @selector shouldn't really appear in
2908 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002909 for (DenseMap<Selector, SourceLocation>::iterator S =
2910 SemaRef.ReferencedSelectors.begin(),
2911 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2912 Selector Sel = (*S).first;
2913 SourceLocation Loc = (*S).second;
2914 AddSelectorRef(Sel, Record);
2915 AddSourceLocation(Loc, Record);
2916 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002917 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002918}
2919
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002920//===----------------------------------------------------------------------===//
2921// Identifier Table Serialization
2922//===----------------------------------------------------------------------===//
2923
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002924namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002925class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002926 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002927 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002928 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002929 bool IsModule;
2930
Douglas Gregora92193e2009-04-28 21:18:29 +00002931 /// \brief Determines whether this is an "interesting" identifier
2932 /// that needs a full IdentifierInfo structure written into the hash
2933 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002934 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002935 if (II->isPoisoned() ||
2936 II->isExtensionToken() ||
2937 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002938 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002939 II->getFETokenInfo<void>())
2940 return true;
2941
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002942 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002943 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002944
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002945 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002946 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002947 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002948
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002949 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2950 if (!IsModule)
2951 return !shouldIgnoreMacro(Macro, IsModule, PP);
2952 SubmoduleID ModID;
2953 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2954 return true;
2955 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002956
2957 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002958 }
2959
Stephen Hines651f13c2014-04-23 16:59:28 -07002960 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2961
2962 MacroDirective *
2963 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002964 ModID = 0;
Stephen Hines651f13c2014-04-23 16:59:28 -07002965 llvm::SmallVector<SubmoduleID, 1> Overridden;
2966 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2967 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2968 return NextMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002969 return 0;
2970 }
2971
Stephen Hines651f13c2014-04-23 16:59:28 -07002972 MacroDirective *
2973 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2974 OverriddenList &Overridden) {
2975 if (MacroDirective *NextMD =
2976 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2977 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2978 return NextMD;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002979 return 0;
2980 }
2981
2982 /// \brief Traverses the macro directives history and returns the latest
Stephen Hines651f13c2014-04-23 16:59:28 -07002983 /// public macro definition or undefinition that is not in ModID.
2984 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002985 /// will still be considered as defined/exported from submodule A.
Stephen Hines651f13c2014-04-23 16:59:28 -07002986 /// ModID is updated to the module containing the returned directive.
2987 ///
2988 /// FIXME: This process breaks down if a module defines a macro, imports
2989 /// another submodule that changes the macro, then changes the
2990 /// macro again itself.
2991 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2992 SubmoduleID &ModID,
2993 OverriddenList &Overridden) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002994 if (!MD)
2995 return 0;
2996
Stephen Hines651f13c2014-04-23 16:59:28 -07002997 Overridden.clear();
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002998 SubmoduleID OrigModID = ModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07002999 Optional<bool> IsPublic;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003000 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003001 SubmoduleID ThisModID = getSubmoduleID(MD);
3002 if (ThisModID == 0) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003003 IsPublic = Optional<bool>();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003004 continue;
3005 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003006 if (ThisModID != ModID) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003007 ModID = ThisModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003008 IsPublic = Optional<bool>();
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003009 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003010
3011 // If this is a definition from a submodule import, that submodule's
3012 // definition is overridden by the definition or undefinition that we
3013 // started with.
3014 // FIXME: This should only apply to macros defined in OrigModID.
3015 // We can't do that currently, because a #include of a different submodule
3016 // of the same module just leaks through macros instead of providing new
3017 // DefMacroDirectives for them.
3018 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3019 // Figure out which submodule the macro was originally defined within.
3020 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3021 if (!SourceID) {
3022 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3023 if (DefLoc == MD->getLocation())
3024 SourceID = ThisModID;
3025 else
3026 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3027 }
3028 if (SourceID != OrigModID)
3029 Overridden.push_back(SourceID);
3030 }
3031
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003032 // We are looking for a definition in a different submodule than the one
3033 // that we started with. If a submodule has re-definitions of the same
3034 // macro, only the last definition will be used as the "exported" one.
3035 if (ModID == OrigModID)
3036 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003037
Stephen Hines651f13c2014-04-23 16:59:28 -07003038 // The latest visibility directive for a name in a submodule affects all
3039 // the directives that come before it.
3040 if (VisibilityMacroDirective *VisMD =
3041 dyn_cast<VisibilityMacroDirective>(MD)) {
3042 if (!IsPublic.hasValue())
3043 IsPublic = VisMD->isPublic();
3044 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3045 // FIXME: If we find an imported macro, we should include its list of
3046 // overrides in our export.
3047 return MD;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003048 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003049 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003050
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003051 return 0;
3052 }
3053
3054 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003055 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003056 }
3057
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003058public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003059 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003060 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003061
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003062 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003063 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003064
Douglas Gregoreee242f2011-10-27 09:33:13 +00003065 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3066 IdentifierResolver &IdResolver, bool IsModule)
3067 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003068
3069 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003070 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003071 }
Mike Stump1eb44332009-09-09 15:08:12 +00003072
3073 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003074 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003075 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003076 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003077 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003078 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003079 DataLen += 2; // 2 bytes for builtin ID
3080 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003081 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003082 DataLen += 4; // MacroDirectives offset.
3083 if (IsModule) {
3084 SubmoduleID ModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003085 llvm::SmallVector<SubmoduleID, 4> Overridden;
3086 for (MacroDirective *
3087 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3088 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3089 // Previous macro's overrides.
3090 if (!Overridden.empty())
3091 DataLen += 4 * (1 + Overridden.size());
3092 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003093 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003094 // Previous macro's overrides.
3095 if (!Overridden.empty())
3096 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003097 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003098 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003099 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003100
Douglas Gregoreee242f2011-10-27 09:33:13 +00003101 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3102 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003103 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003104 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003105 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003106 using namespace llvm::support;
3107 endian::Writer<little> LE(Out);
3108
3109 LE.write<uint16_t>(DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003110 // We emit the key length after the data length so that every
3111 // string is preceded by a 16-bit length. This matches the PTH
3112 // format for storing identifiers.
Stephen Hines651f13c2014-04-23 16:59:28 -07003113 LE.write<uint16_t>(KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003114 return std::make_pair(KeyLen, DataLen);
3115 }
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Chris Lattner5f9e2722011-07-23 10:55:15 +00003117 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003118 unsigned KeyLen) {
3119 // Record the location of the key data. This is used when generating
3120 // the mapping from persistent IDs to strings.
3121 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003122 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003123 }
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Stephen Hines651f13c2014-04-23 16:59:28 -07003125 static void emitMacroOverrides(raw_ostream &Out,
3126 llvm::ArrayRef<SubmoduleID> Overridden) {
3127 if (!Overridden.empty()) {
3128 using namespace llvm::support;
3129 endian::Writer<little> LE(Out);
3130 LE.write<uint32_t>(Overridden.size() | 0x80000000U);
3131 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3132 LE.write<uint32_t>(Overridden[I]);
3133 }
3134 }
3135
Douglas Gregor7143aab2011-09-01 17:04:32 +00003136 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003137 IdentID ID, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003138 using namespace llvm::support;
3139 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003140 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003141 if (!isInterestingIdentifier(II, Macro)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003142 LE.write<uint32_t>(ID << 1);
Douglas Gregora92193e2009-04-28 21:18:29 +00003143 return;
3144 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003145
Stephen Hines651f13c2014-04-23 16:59:28 -07003146 LE.write<uint32_t>((ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003147 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3148 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Stephen Hines651f13c2014-04-23 16:59:28 -07003149 LE.write<uint16_t>(Bits);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003150 Bits = 0;
3151 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003152 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003153 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003154 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3155 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003156 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003157 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Stephen Hines651f13c2014-04-23 16:59:28 -07003158 LE.write<uint16_t>(Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003159
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003160 if (HadMacroDefinition) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003161 LE.write<uint32_t>(Writer.getMacroDirectivesOffset(II));
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003162 if (IsModule) {
3163 // Write the IDs of macros coming from different submodules.
3164 SubmoduleID ModID;
Stephen Hines651f13c2014-04-23 16:59:28 -07003165 llvm::SmallVector<SubmoduleID, 4> Overridden;
3166 for (MacroDirective *
3167 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3168 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3169 MacroID InfoID = 0;
3170 emitMacroOverrides(Out, Overridden);
3171 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3172 InfoID = Writer.getMacroID(DefMD->getInfo());
3173 assert(InfoID);
3174 LE.write<uint32_t>(InfoID << 1);
3175 } else {
3176 assert(isa<UndefMacroDirective>(MD));
3177 LE.write<uint32_t>((ModID << 1) | 1);
3178 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003179 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003180 emitMacroOverrides(Out, Overridden);
3181 LE.write<uint32_t>(0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003182 }
Douglas Gregor13292642011-12-02 15:45:10 +00003183 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003184
Douglas Gregor668c1a42009-04-21 22:25:48 +00003185 // Emit the declaration IDs in reverse order, because the
3186 // IdentifierResolver provides the declarations as they would be
3187 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003188 // "stat"), but the ASTReader adds declarations to the end of the list
3189 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003190 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003191 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3192 IdResolver.end());
Craig Topper09d19ef2013-07-04 03:08:24 +00003193 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003194 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003195 D != DEnd; ++D)
Stephen Hines651f13c2014-04-23 16:59:28 -07003196 LE.write<uint32_t>(Writer.getDeclID(getMostRecentLocalDecl(*D)));
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003197 }
3198
3199 /// \brief Returns the most recent local decl or the given decl if there are
3200 /// no local ones. The given decl is assumed to be the most recent one.
3201 Decl *getMostRecentLocalDecl(Decl *Orig) {
3202 // The only way a "from AST file" decl would be more recent from a local one
3203 // is if it came from a module.
3204 if (!PP.getLangOpts().Modules)
3205 return Orig;
3206
3207 // Look for a local in the decl chain.
3208 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3209 if (!D->isFromASTFile())
3210 return D;
3211 // If we come up a decl from a (chained-)PCH stop since we won't find a
3212 // local one.
3213 if (D->getOwningModuleID() == 0)
3214 break;
3215 }
3216
3217 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003218 }
3219};
3220} // end anonymous namespace
3221
Sebastian Redl3397c552010-08-18 23:56:27 +00003222/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003223///
3224/// The identifier table consists of a blob containing string data
3225/// (the actual identifiers themselves) and a separate "offsets" index
3226/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003227void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3228 IdentifierResolver &IdResolver,
3229 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003230 using namespace llvm;
3231
3232 // Create and write out the blob that contains the identifier
3233 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003234 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003235 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003236 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003237
Douglas Gregor92b059e2009-04-28 20:33:11 +00003238 // Look for any identifiers that were named while processing the
3239 // headers, but are otherwise not needed. We add these to the hash
3240 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003241 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003242 // file.
3243 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3244 IDEnd = PP.getIdentifierTable().end();
3245 ID != IDEnd; ++ID)
3246 getIdentifierRef(ID->second);
3247
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003248 // Create the on-disk hash table representation. We only store offsets
3249 // for identifiers that appear here for the first time.
3250 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003251 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003252 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3253 ID != IDEnd; ++ID) {
3254 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003255 if (!Chain || !ID->first->isFromAST() ||
3256 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003257 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003258 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003259 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003260
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003261 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003262 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003263 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003264 {
Stephen Hines651f13c2014-04-23 16:59:28 -07003265 using namespace llvm::support;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003266 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003267 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003268 // Make sure that no bucket is at offset 0
Stephen Hines651f13c2014-04-23 16:59:28 -07003269 endian::Writer<little>(Out).write<uint32_t>(0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003270 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003271 }
3272
3273 // Create a blob abbreviation
3274 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003275 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003278 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003279
3280 // Write the identifier table
3281 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003282 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003283 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003284 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003285 }
3286
3287 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003288 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003289 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3293 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3294
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003295#ifndef NDEBUG
3296 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3297 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3298#endif
3299
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003300 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003301 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003302 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003303 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003304 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003305 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003306}
3307
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003308//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003309// DeclContext's Name Lookup Table Serialization
3310//===----------------------------------------------------------------------===//
3311
3312namespace {
3313// Trait used for the on-disk hash table used in the method pool.
3314class ASTDeclContextNameLookupTrait {
3315 ASTWriter &Writer;
3316
3317public:
3318 typedef DeclarationName key_type;
3319 typedef key_type key_type_ref;
3320
3321 typedef DeclContext::lookup_result data_type;
3322 typedef const data_type& data_type_ref;
3323
3324 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3325
3326 unsigned ComputeHash(DeclarationName Name) {
3327 llvm::FoldingSetNodeID ID;
3328 ID.AddInteger(Name.getNameKind());
3329
3330 switch (Name.getNameKind()) {
3331 case DeclarationName::Identifier:
3332 ID.AddString(Name.getAsIdentifierInfo()->getName());
3333 break;
3334 case DeclarationName::ObjCZeroArgSelector:
3335 case DeclarationName::ObjCOneArgSelector:
3336 case DeclarationName::ObjCMultiArgSelector:
3337 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3338 break;
3339 case DeclarationName::CXXConstructorName:
3340 case DeclarationName::CXXDestructorName:
3341 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003342 break;
3343 case DeclarationName::CXXOperatorName:
3344 ID.AddInteger(Name.getCXXOverloadedOperator());
3345 break;
3346 case DeclarationName::CXXLiteralOperatorName:
3347 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3348 case DeclarationName::CXXUsingDirective:
3349 break;
3350 }
3351
3352 return ID.ComputeHash();
3353 }
3354
3355 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003356 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003357 data_type_ref Lookup) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003358 using namespace llvm::support;
3359 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003360 unsigned KeyLen = 1;
3361 switch (Name.getNameKind()) {
3362 case DeclarationName::Identifier:
3363 case DeclarationName::ObjCZeroArgSelector:
3364 case DeclarationName::ObjCOneArgSelector:
3365 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003366 case DeclarationName::CXXLiteralOperatorName:
3367 KeyLen += 4;
3368 break;
3369 case DeclarationName::CXXOperatorName:
3370 KeyLen += 1;
3371 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003372 case DeclarationName::CXXConstructorName:
3373 case DeclarationName::CXXDestructorName:
3374 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003375 case DeclarationName::CXXUsingDirective:
3376 break;
3377 }
Stephen Hines651f13c2014-04-23 16:59:28 -07003378 LE.write<uint16_t>(KeyLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003379
3380 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003381 unsigned DataLen = 2 + 4 * Lookup.size();
Stephen Hines651f13c2014-04-23 16:59:28 -07003382 LE.write<uint16_t>(DataLen);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003383
3384 return std::make_pair(KeyLen, DataLen);
3385 }
3386
Chris Lattner5f9e2722011-07-23 10:55:15 +00003387 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003388 using namespace llvm::support;
3389 endian::Writer<little> LE(Out);
3390 LE.write<uint8_t>(Name.getNameKind());
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003391 switch (Name.getNameKind()) {
3392 case DeclarationName::Identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -07003393 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003394 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003395 case DeclarationName::ObjCZeroArgSelector:
3396 case DeclarationName::ObjCOneArgSelector:
3397 case DeclarationName::ObjCMultiArgSelector:
Stephen Hines651f13c2014-04-23 16:59:28 -07003398 LE.write<uint32_t>(Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003399 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003400 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003401 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3402 "Invalid operator?");
Stephen Hines651f13c2014-04-23 16:59:28 -07003403 LE.write<uint8_t>(Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003404 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003405 case DeclarationName::CXXLiteralOperatorName:
Stephen Hines651f13c2014-04-23 16:59:28 -07003406 LE.write<uint32_t>(Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003407 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003408 case DeclarationName::CXXConstructorName:
3409 case DeclarationName::CXXDestructorName:
3410 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003411 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003412 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003413 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003414
3415 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003416 }
3417
Chris Lattner5f9e2722011-07-23 10:55:15 +00003418 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003419 data_type Lookup, unsigned DataLen) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003420 using namespace llvm::support;
3421 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003422 uint64_t Start = Out.tell(); (void)Start;
Stephen Hines651f13c2014-04-23 16:59:28 -07003423 LE.write<uint16_t>(Lookup.size());
David Blaikie3bc93e32012-12-19 00:45:41 +00003424 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3425 I != E; ++I)
Stephen Hines651f13c2014-04-23 16:59:28 -07003426 LE.write<uint32_t>(Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003427
3428 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3429 }
3430};
3431} // end anonymous namespace
3432
Stephen Hines651f13c2014-04-23 16:59:28 -07003433uint32_t
3434ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3435 llvm::SmallVectorImpl<char> &LookupTable) {
3436 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3437 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3438
3439 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3440 ASTDeclContextNameLookupTrait Trait(*this);
3441
3442 // Create the on-disk hash table representation.
3443 DeclarationName ConstructorName;
3444 DeclarationName ConversionName;
3445 SmallVector<NamedDecl *, 8> ConstructorDecls;
3446 SmallVector<NamedDecl *, 4> ConversionDecls;
3447
3448 auto AddLookupResult = [&](DeclarationName Name,
3449 DeclContext::lookup_result Result) {
3450 if (Result.empty())
3451 return;
3452
3453 // Different DeclarationName values of certain kinds are mapped to
3454 // identical serialized keys, because we don't want to use type
3455 // identifiers in the keys (since type ids are local to the module).
3456 switch (Name.getNameKind()) {
3457 case DeclarationName::CXXConstructorName:
3458 // There may be different CXXConstructorName DeclarationName values
3459 // in a DeclContext because a UsingDecl that inherits constructors
3460 // has the DeclarationName of the inherited constructors.
3461 if (!ConstructorName)
3462 ConstructorName = Name;
3463 ConstructorDecls.append(Result.begin(), Result.end());
3464 return;
3465 case DeclarationName::CXXConversionFunctionName:
3466 if (!ConversionName)
3467 ConversionName = Name;
3468 ConversionDecls.append(Result.begin(), Result.end());
3469 return;
3470 default:
3471 break;
3472 }
3473
3474 Generator.insert(Name, Result, Trait);
3475 };
3476
3477 SmallVector<DeclarationName, 16> ExternalNames;
3478 for (auto &Lookup : *DC->getLookupPtr()) {
3479 if (Lookup.second.hasExternalDecls() ||
3480 DC->NeedToReconcileExternalVisibleStorage) {
3481 // We don't know for sure what declarations are found by this name,
3482 // because the external source might have a different set from the set
3483 // that are in the lookup map, and we can't update it now without
3484 // risking invalidating our lookup iterator. So add it to a queue to
3485 // deal with later.
3486 ExternalNames.push_back(Lookup.first);
3487 continue;
3488 }
3489
3490 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3491 }
3492
3493 // Add the names we needed to defer. Note, this shouldn't add any new decls
3494 // to the list we need to serialize: any new declarations we find here should
3495 // be imported from an external source.
3496 // FIXME: What if the external source isn't an ASTReader?
3497 for (const auto &Name : ExternalNames)
3498 // FIXME: const_cast since OnDiskHashTable wants a non-const lookup result.
3499 AddLookupResult(Name, const_cast<DeclContext*>(DC)->lookup(Name));
3500
3501 // Add the constructors.
3502 if (!ConstructorDecls.empty()) {
3503 Generator.insert(ConstructorName,
3504 DeclContext::lookup_result(ConstructorDecls.begin(),
3505 ConstructorDecls.end()),
3506 Trait);
3507 }
3508 // Add the conversion functions.
3509 if (!ConversionDecls.empty()) {
3510 Generator.insert(ConversionName,
3511 DeclContext::lookup_result(ConversionDecls.begin(),
3512 ConversionDecls.end()),
3513 Trait);
3514 }
3515
3516 // Create the on-disk hash table in a buffer.
3517 llvm::raw_svector_ostream Out(LookupTable);
3518 // Make sure that no bucket is at offset 0
3519 using namespace llvm::support;
3520 endian::Writer<little>(Out).write<uint32_t>(0);
3521 return Generator.Emit(Out, Trait);
3522}
3523
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003524/// \brief Write the block containing all of the declaration IDs
3525/// visible from the given DeclContext.
3526///
3527/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003528/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003529uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3530 DeclContext *DC) {
3531 if (DC->getPrimaryContext() != DC)
3532 return 0;
3533
3534 // Since there is no name lookup into functions or methods, don't bother to
3535 // build a visible-declarations table for these entities.
3536 if (DC->isFunctionOrMethod())
3537 return 0;
3538
3539 // If not in C++, we perform name lookup for the translation unit via the
3540 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003541 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003542 return 0;
3543
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003544 // Serialize the contents of the mapping used for lookup. Note that,
3545 // although we have two very different code paths, the serialized
3546 // representation is the same for both cases: a declaration name,
3547 // followed by a size, followed by references to the visible
3548 // declarations that have that name.
3549 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003550 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003551 if (!Map || Map->empty())
3552 return 0;
3553
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003554 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003555 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003556 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003557
3558 // Write the lookup table
3559 RecordData Record;
3560 Record.push_back(DECL_CONTEXT_VISIBLE);
3561 Record.push_back(BucketOffset);
3562 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3563 LookupTable.str());
3564
3565 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3566 ++NumVisibleDeclContexts;
3567 return Offset;
3568}
3569
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003570/// \brief Write an UPDATE_VISIBLE block for the given context.
3571///
3572/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3573/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003574/// (in C++), for namespaces, and for classes with forward-declared unscoped
3575/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003576void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Stephen Hines651f13c2014-04-23 16:59:28 -07003577 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003578 if (!Map || Map->empty())
3579 return;
3580
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003581 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003582 SmallString<4096> LookupTable;
Stephen Hines651f13c2014-04-23 16:59:28 -07003583 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003584
3585 // Write the lookup table
3586 RecordData Record;
3587 Record.push_back(UPDATE_VISIBLE);
3588 Record.push_back(getDeclID(cast<Decl>(DC)));
3589 Record.push_back(BucketOffset);
3590 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3591}
3592
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003593/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3594void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3595 RecordData Record;
3596 Record.push_back(Opts.fp_contract);
3597 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3598}
3599
3600/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3601void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003602 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003603 return;
3604
3605 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3606 RecordData Record;
3607#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3608#include "clang/Basic/OpenCLExtensions.def"
3609 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3610}
3611
Douglas Gregor2171bf12012-01-15 16:58:34 +00003612void ASTWriter::WriteRedeclarations() {
3613 RecordData LocalRedeclChains;
3614 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3615
3616 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3617 Decl *First = Redeclarations[I];
Rafael Espindola7693b322013-10-19 02:13:21 +00003618 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor2171bf12012-01-15 16:58:34 +00003619
3620 Decl *MostRecent = First->getMostRecentDecl();
3621
3622 // If we only have a single declaration, there is no point in storing
3623 // a redeclaration chain.
3624 if (First == MostRecent)
3625 continue;
3626
3627 unsigned Offset = LocalRedeclChains.size();
3628 unsigned Size = 0;
3629 LocalRedeclChains.push_back(0); // Placeholder for the size.
3630
3631 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003632 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003633 Prev = Prev->getPreviousDecl()) {
3634 if (!Prev->isFromASTFile()) {
3635 AddDeclRef(Prev, LocalRedeclChains);
3636 ++Size;
3637 }
3638 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003639
3640 if (!First->isFromASTFile() && Chain) {
3641 Decl *FirstFromAST = MostRecent;
3642 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3643 if (Prev->isFromASTFile())
3644 FirstFromAST = Prev;
3645 }
3646
3647 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3648 }
3649
Douglas Gregor2171bf12012-01-15 16:58:34 +00003650 LocalRedeclChains[Offset] = Size;
3651
3652 // Reverse the set of local redeclarations, so that we store them in
3653 // order (since we found them in reverse order).
3654 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3655
Douglas Gregoraa945902013-02-18 15:53:43 +00003656 // Add the mapping from the first ID from the AST to the set of local
3657 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003658 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3659 LocalRedeclsMap.push_back(Info);
3660
3661 assert(N == Redeclarations.size() &&
3662 "Deserialized a declaration we shouldn't have");
3663 }
3664
3665 if (LocalRedeclChains.empty())
3666 return;
3667
3668 // Sort the local redeclarations map by the first declaration ID,
3669 // since the reader will be performing binary searches on this information.
3670 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3671
3672 // Emit the local redeclarations map.
3673 using namespace llvm;
3674 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3675 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3678 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3679
3680 RecordData Record;
3681 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3682 Record.push_back(LocalRedeclsMap.size());
3683 Stream.EmitRecordWithBlob(AbbrevID, Record,
3684 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3685 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3686
3687 // Emit the redeclaration chains.
3688 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3689}
3690
Douglas Gregorcff9f262012-01-27 01:47:08 +00003691void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003692 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003693 RecordData Categories;
3694
3695 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3696 unsigned Size = 0;
3697 unsigned StartIndex = Categories.size();
3698
3699 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3700
3701 // Allocate space for the size.
3702 Categories.push_back(0);
3703
3704 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003705 for (ObjCInterfaceDecl::known_categories_iterator
3706 Cat = Class->known_categories_begin(),
3707 CatEnd = Class->known_categories_end();
3708 Cat != CatEnd; ++Cat, ++Size) {
3709 assert(getDeclID(*Cat) != 0 && "Bogus category");
3710 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003711 }
3712
3713 // Update the size.
3714 Categories[StartIndex] = Size;
3715
3716 // Record this interface -> category map.
3717 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3718 CategoriesMap.push_back(CatInfo);
3719 }
3720
3721 // Sort the categories map by the definition ID, since the reader will be
3722 // performing binary searches on this information.
3723 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3724
3725 // Emit the categories map.
3726 using namespace llvm;
3727 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3728 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3731 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3732
3733 RecordData Record;
3734 Record.push_back(OBJC_CATEGORIES_MAP);
3735 Record.push_back(CategoriesMap.size());
3736 Stream.EmitRecordWithBlob(AbbrevID, Record,
3737 reinterpret_cast<char*>(CategoriesMap.data()),
3738 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3739
3740 // Emit the category lists.
3741 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3742}
3743
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003744void ASTWriter::WriteMergedDecls() {
3745 if (!Chain || Chain->MergedDecls.empty())
3746 return;
3747
3748 RecordData Record;
3749 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3750 IEnd = Chain->MergedDecls.end();
3751 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003752 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003753 : getDeclID(I->first);
3754 assert(CanonID && "Merged declaration not known?");
3755
3756 Record.push_back(CanonID);
3757 Record.push_back(I->second.size());
3758 Record.append(I->second.begin(), I->second.end());
3759 }
3760 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3761}
3762
Richard Smithac32d902013-08-07 21:41:30 +00003763void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3764 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3765
3766 if (LPTMap.empty())
3767 return;
3768
3769 RecordData Record;
3770 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3771 ItEnd = LPTMap.end();
3772 It != ItEnd; ++It) {
3773 LateParsedTemplate *LPT = It->second;
3774 AddDeclRef(It->first, Record);
3775 AddDeclRef(LPT->D, Record);
3776 Record.push_back(LPT->Toks.size());
3777
3778 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3779 TokEnd = LPT->Toks.end();
3780 TokIt != TokEnd; ++TokIt) {
3781 AddToken(*TokIt, Record);
3782 }
3783 }
3784 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3785}
3786
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003787//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003788// General Serialization Routines
3789//===----------------------------------------------------------------------===//
3790
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003791/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003792void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3793 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003794 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003795 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3796 e = Attrs.end(); i != e; ++i){
3797 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003798 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003799 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003800
Sean Huntcf807c42010-08-18 23:23:40 +00003801#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003802
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003803 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003804}
3805
John McCallaeeacf72013-05-03 00:10:13 +00003806void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3807 AddSourceLocation(Tok.getLocation(), Record);
3808 Record.push_back(Tok.getLength());
3809
3810 // FIXME: When reading literal tokens, reconstruct the literal pointer
3811 // if it is needed.
3812 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3813 // FIXME: Should translate token kind to a stable encoding.
3814 Record.push_back(Tok.getKind());
3815 // FIXME: Should translate token flags to a stable encoding.
3816 Record.push_back(Tok.getFlags());
3817}
3818
Chris Lattner5f9e2722011-07-23 10:55:15 +00003819void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003820 Record.push_back(Str.size());
3821 Record.insert(Record.end(), Str.begin(), Str.end());
3822}
3823
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003824void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3825 RecordDataImpl &Record) {
3826 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003827 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003828 Record.push_back(*Minor + 1);
3829 else
3830 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003831 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003832 Record.push_back(*Subminor + 1);
3833 else
3834 Record.push_back(0);
3835}
3836
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003837/// \brief Note that the identifier II occurs at the given offset
3838/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003839void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003840 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003841 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003842 // up earlier in the chain and thus don't need an offset.
3843 if (ID >= FirstIdentID)
3844 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003845}
3846
Douglas Gregor83941df2009-04-25 17:48:32 +00003847/// \brief Note that the selector Sel occurs at the given offset
3848/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003849void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003850 unsigned ID = SelectorIDs[Sel];
3851 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003852 // Don't record offsets for selectors that are also available in a different
3853 // file.
3854 if (ID < FirstSelectorID)
3855 return;
3856 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003857}
3858
Sebastian Redla4232eb2010-08-18 23:56:21 +00003859ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003860 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003861 WritingAST(false), DoneWritingDeclsAndTypes(false),
3862 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003863 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003864 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003865 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3866 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003867 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3868 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003869 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003870 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003871 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003872 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003873 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003874 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003875 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3876 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3877 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003878 DeclTypedefAbbrev(0),
3879 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3880 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003881{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003882}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003883
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003884ASTWriter::~ASTWriter() {
Stephen Hines651f13c2014-04-23 16:59:28 -07003885 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003886}
3887
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003888void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003889 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003890 Module *WritingModule, StringRef isysroot,
3891 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003892 WritingAST = true;
3893
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003894 ASTHasCompilerErrors = hasErrors;
3895
Douglas Gregor2cf26342009-04-09 22:27:44 +00003896 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003897 Stream.Emit((unsigned)'C', 8);
3898 Stream.Emit((unsigned)'P', 8);
3899 Stream.Emit((unsigned)'C', 8);
3900 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003901
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003902 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003903
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003904 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003905 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003906 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003907 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003908 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003909 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003910 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003911
3912 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003913}
3914
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003915template<typename Vector>
3916static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3917 ASTWriter::RecordData &Record) {
3918 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3919 I != E; ++I) {
3920 Writer.AddDeclRef(*I, Record);
3921 }
3922}
3923
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003924void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003925 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003926 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003927 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003928 using namespace llvm;
3929
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003930 bool isModule = WritingModule != 0;
3931
Douglas Gregorecc2c092011-12-01 22:20:10 +00003932 // Make sure that the AST reader knows to finalize itself.
3933 if (Chain)
3934 Chain->finalizeForWriting();
3935
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003936 ASTContext &Context = SemaRef.Context;
3937 Preprocessor &PP = SemaRef.PP;
3938
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003939 // Set up predefined declaration IDs.
3940 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003941 if (Context.ObjCIdDecl)
3942 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003943 if (Context.ObjCSelDecl)
3944 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003945 if (Context.ObjCClassDecl)
3946 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003947 if (Context.ObjCProtocolClassDecl)
3948 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003949 if (Context.Int128Decl)
3950 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3951 if (Context.UInt128Decl)
3952 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003953 if (Context.ObjCInstanceTypeDecl)
3954 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003955 if (Context.BuiltinVaListDecl)
3956 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3957
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003958 if (!Chain) {
3959 // Make sure that we emit IdentifierInfos (and any attached
3960 // declarations) for builtins. We don't need to do this when we're
3961 // emitting chained PCH files, because all of the builtins will be
3962 // in the original PCH file.
3963 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003964 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003965 SmallVector<const char *, 32> BuiltinNames;
Eli Bendersky97a03cf2013-07-11 16:53:04 +00003966 if (!Context.getLangOpts().NoBuiltin) {
3967 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3968 }
Douglas Gregor2deaea32009-04-22 18:49:13 +00003969 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3970 getIdentifierRef(&Table.get(BuiltinNames[I]));
3971 }
3972
Douglas Gregoreee242f2011-10-27 09:33:13 +00003973 // If there are any out-of-date identifiers, bring them up to date.
3974 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003975 // Find out-of-date identifiers.
3976 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003977 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3978 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003979 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003980 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003981 OutOfDate.push_back(ID->second);
3982 }
3983
3984 // Update the out-of-date identifiers.
3985 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3986 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3987 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003988 }
3989
Chris Lattner63d65f82009-09-08 18:19:27 +00003990 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003991 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003992 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003993 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003994 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003995
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003996 // Build a record containing all of the file scoped decls in this file.
3997 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003998 if (!isModule)
3999 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4000 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00004001
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004002 // Build a record containing all of the delegating constructors we still need
4003 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00004004 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004005 if (!isModule)
4006 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004007
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004008 // Write the set of weak, undeclared identifiers. We always write the
4009 // entire table, since later PCH files in a PCH chain are only interested in
4010 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004011 RecordData WeakUndeclaredIdentifiers;
4012 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00004013 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004014 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
4015 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
4016 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
4017 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
4018 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
4019 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
4020 }
4021 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004022
Richard Smith5ea6ef42013-01-10 23:43:47 +00004023 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00004024 // declarations in this header file. Generally, this record will be
4025 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00004026 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00004027 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00004028 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00004029 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00004030 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4031 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00004032 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00004033 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00004034 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00004035 }
4036
Douglas Gregorb81c1702009-04-27 20:06:05 +00004037 // Build a record containing all of the ext_vector declarations.
4038 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00004039 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004040
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004041 // Build a record containing all of the VTable uses information.
4042 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004043 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00004044 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4045 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4046 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4047 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4048 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004049 }
4050
4051 // Build a record containing all of dynamic classes declarations.
4052 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00004053 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004054
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004055 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004056 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004057 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00004058 I = SemaRef.PendingInstantiations.begin(),
4059 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4060 AddDeclRef(I->first, PendingInstantiations);
4061 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004062 }
4063 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4064 "There are local ones at end of translation unit!");
4065
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004066 // Build a record containing some declaration references.
4067 RecordData SemaDeclRefs;
4068 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4069 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4070 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4071 }
4072
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004073 RecordData CUDASpecialDeclRefs;
4074 if (Context.getcudaConfigureCallDecl()) {
4075 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4076 }
4077
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004078 // Build a record containing all of the known namespaces.
4079 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00004080 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004081 I = SemaRef.KnownNamespaces.begin(),
4082 IEnd = SemaRef.KnownNamespaces.end();
4083 I != IEnd; ++I) {
4084 if (!I->second)
4085 AddDeclRef(I->first, KnownNamespaces);
4086 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004087
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004088 // Build a record of all used, undefined objects that require definitions.
4089 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00004090
4091 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004092 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00004093 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4094 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004095 AddDeclRef(I->first, UndefinedButUsed);
4096 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00004097 }
4098
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004099 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00004100 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004101
Sebastian Redl3397c552010-08-18 23:56:27 +00004102 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00004103 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004104 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004105
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00004106 // This is so that older clang versions, before the introduction
4107 // of the control block, can read and reject the newer PCH format.
4108 Record.clear();
4109 Record.push_back(VERSION_MAJOR);
4110 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4111
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004112 // Create a lexical update block containing all of the declarations in the
4113 // translation unit that do not come from other AST files.
4114 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4115 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Stephen Hines651f13c2014-04-23 16:59:28 -07004116 for (const auto *I : TU->noload_decls()) {
4117 if (!I->isFromASTFile())
4118 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004119 }
4120
4121 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4122 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4123 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4124 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4125 Record.clear();
4126 Record.push_back(TU_UPDATE_LEXICAL);
4127 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4128 data(NewGlobalDecls));
4129
4130 // And a visible updates block for the translation unit.
4131 Abv = new llvm::BitCodeAbbrev();
4132 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4133 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4134 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4135 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4136 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4137 WriteDeclContextVisibleUpdate(TU);
4138
4139 // If the translation unit has an anonymous namespace, and we don't already
4140 // have an update block for it, write it as an update block.
Stephen Hines651f13c2014-04-23 16:59:28 -07004141 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004142 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4143 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Stephen Hines651f13c2014-04-23 16:59:28 -07004144 if (Record.empty())
4145 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004146 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004147
Stephen Hines651f13c2014-04-23 16:59:28 -07004148 // Add update records for all mangling numbers and static local numbers.
4149 // These aren't really update records, but this is a convenient way of
4150 // tagging this rare extra data onto the declarations.
4151 for (const auto &Number : Context.MangleNumbers)
4152 if (!Number.first->isFromASTFile())
4153 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4154 Number.second));
4155 for (const auto &Number : Context.StaticLocalNumbers)
4156 if (!Number.first->isFromASTFile())
4157 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4158 Number.second));
4159
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004160 // Make sure visible decls, added to DeclContexts previously loaded from
4161 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004162 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004163 I = UpdatingVisibleDecls.begin(),
4164 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4165 GetDeclRef(*I);
4166 }
4167
Argyrios Kyrtzidis51e75ae2013-08-07 21:17:33 +00004168 // Make sure all decls associated with an identifier are registered for
4169 // serialization.
4170 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4171 IDEnd = PP.getIdentifierTable().end();
4172 ID != IDEnd; ++ID) {
4173 const IdentifierInfo *II = ID->second;
4174 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4175 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4176 DEnd = SemaRef.IdResolver.end();
4177 D != DEnd; ++D) {
4178 GetDeclRef(*D);
4179 }
4180 }
4181 }
4182
Douglas Gregora119da02011-08-02 16:26:37 +00004183 // Form the record of special types.
4184 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004185 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004186 AddTypeRef(Context.getFILEType(), SpecialTypes);
4187 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4188 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4189 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4190 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004191 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004192 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004193
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004194 if (Chain) {
4195 // Write the mapping information describing our module dependencies and how
4196 // each of those modules were mapped into our own offset/ID space, so that
4197 // the reader can build the appropriate mapping to its own offset/ID space.
4198 // The map consists solely of a blob with the following format:
4199 // *(module-name-len:i16 module-name:len*i8
4200 // source-location-offset:i32
4201 // identifier-id:i32
4202 // preprocessed-entity-id:i32
4203 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004204 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004205 // selector-id:i32
4206 // declaration-id:i32
4207 // c++-base-specifiers-id:i32
4208 // type-id:i32)
4209 //
4210 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4211 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4213 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004214 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004215 {
4216 llvm::raw_svector_ostream Out(Buffer);
4217 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004218 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004219 M != MEnd; ++M) {
Stephen Hines651f13c2014-04-23 16:59:28 -07004220 using namespace llvm::support;
4221 endian::Writer<little> LE(Out);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004222 StringRef FileName = (*M)->FileName;
Stephen Hines651f13c2014-04-23 16:59:28 -07004223 LE.write<uint16_t>(FileName.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004224 Out.write(FileName.data(), FileName.size());
Stephen Hines651f13c2014-04-23 16:59:28 -07004225 LE.write<uint32_t>((*M)->SLocEntryBaseOffset);
4226 LE.write<uint32_t>((*M)->BaseIdentifierID);
4227 LE.write<uint32_t>((*M)->BaseMacroID);
4228 LE.write<uint32_t>((*M)->BasePreprocessedEntityID);
4229 LE.write<uint32_t>((*M)->BaseSubmoduleID);
4230 LE.write<uint32_t>((*M)->BaseSelectorID);
4231 LE.write<uint32_t>((*M)->BaseDeclID);
4232 LE.write<uint32_t>((*M)->BaseTypeIndex);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004233 }
4234 }
4235 Record.clear();
4236 Record.push_back(MODULE_OFFSET_MAP);
4237 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4238 Buffer.data(), Buffer.size());
4239 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004240
4241 RecordData DeclUpdatesOffsetsRecord;
4242
4243 // Keep writing types, declarations, and declaration update records
4244 // until we've emitted all of them.
4245 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4246 WriteDeclsBlockAbbrevs();
4247 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4248 E = DeclsToRewrite.end();
4249 I != E; ++I)
4250 DeclTypesToEmit.push(const_cast<Decl*>(*I));
4251 do {
4252 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4253 while (!DeclTypesToEmit.empty()) {
4254 DeclOrType DOT = DeclTypesToEmit.front();
4255 DeclTypesToEmit.pop();
4256 if (DOT.isType())
4257 WriteType(DOT.getType());
4258 else
4259 WriteDecl(Context, DOT.getDecl());
4260 }
4261 } while (!DeclUpdates.empty());
4262 Stream.ExitBlock();
4263
4264 DoneWritingDeclsAndTypes = true;
4265
4266 // These things can only be done once we've written out decls and types.
4267 WriteTypeDeclOffsets();
4268 if (!DeclUpdatesOffsetsRecord.empty())
4269 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4270 WriteCXXBaseSpecifiersOffsets();
4271 WriteFileDeclIDsMap();
4272 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4273
4274 WriteComments();
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004275 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004276 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004277 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004278 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004279 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004280 WriteFPPragmaOptions(SemaRef.getFPOptions());
4281 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004282 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004283
Douglas Gregore209e502011-12-06 01:10:29 +00004284 // If we're emitting a module, write out the submodule information.
4285 if (WritingModule)
4286 WriteSubmodules(WritingModule);
4287
Douglas Gregora119da02011-08-02 16:26:37 +00004288 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4289
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004290 // Write the record containing external, unnamed definitions.
Stephen Hines651f13c2014-04-23 16:59:28 -07004291 if (!EagerlyDeserializedDecls.empty())
4292 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004293
4294 // Write the record containing tentative definitions.
4295 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004296 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004297
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004298 // Write the record containing unused file scoped decls.
4299 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004300 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004301
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004302 // Write the record containing weak undeclared identifiers.
4303 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004304 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004305 WeakUndeclaredIdentifiers);
4306
Richard Smith5ea6ef42013-01-10 23:43:47 +00004307 // Write the record containing locally-scoped extern "C" definitions.
4308 if (!LocallyScopedExternCDecls.empty())
4309 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4310 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004311
4312 // Write the record containing ext_vector type names.
4313 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004314 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004315
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004316 // Write the record containing VTable uses information.
4317 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004318 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004319
4320 // Write the record containing dynamic classes declarations.
4321 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004322 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004323
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004324 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004325 if (!PendingInstantiations.empty())
4326 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004327
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004328 // Write the record containing declaration references of Sema.
4329 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004330 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004331
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004332 // Write the record containing CUDA-specific declaration references.
4333 if (!CUDASpecialDeclRefs.empty())
4334 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004335
4336 // Write the delegating constructors.
4337 if (!DelegatingCtorDecls.empty())
4338 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004339
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004340 // Write the known namespaces.
4341 if (!KnownNamespaces.empty())
4342 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004343
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004344 // Write the undefined internal functions and variables, and inline functions.
4345 if (!UndefinedButUsed.empty())
4346 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004347
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004348 // Write the visible updates to DeclContexts.
4349 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4350 I = UpdatedDeclContexts.begin(),
4351 E = UpdatedDeclContexts.end();
4352 I != E; ++I)
4353 WriteDeclContextVisibleUpdate(*I);
4354
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004355 if (!WritingModule) {
4356 // Write the submodules that were imported, if any.
Stephen Hines651f13c2014-04-23 16:59:28 -07004357 struct ModuleInfo {
4358 uint64_t ID;
4359 Module *M;
4360 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4361 };
4362 llvm::SmallVector<ModuleInfo, 64> Imports;
4363 for (const auto *I : Context.local_imports()) {
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004364 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Stephen Hines651f13c2014-04-23 16:59:28 -07004365 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4366 I->getImportedModule()));
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004367 }
Stephen Hines651f13c2014-04-23 16:59:28 -07004368
4369 if (!Imports.empty()) {
4370 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4371 return A.ID < B.ID;
4372 };
4373
4374 // Sort and deduplicate module IDs.
4375 std::sort(Imports.begin(), Imports.end(), Cmp);
4376 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4377 Imports.end());
4378
4379 RecordData ImportedModules;
4380 for (const auto &Import : Imports) {
4381 ImportedModules.push_back(Import.ID);
4382 // FIXME: If the module has macros imported then later has declarations
4383 // imported, this location won't be the right one as a location for the
4384 // declaration imports.
4385 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4386 }
4387
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004388 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4389 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004390 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004391
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004392 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004393 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004394 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004395 WriteObjCCategories();
Richard Smithac32d902013-08-07 21:41:30 +00004396 WriteLateParsedTemplates(SemaRef);
4397
Douglas Gregor3e1af842009-04-17 22:13:46 +00004398 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004399 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004400 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004401 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004402 Record.push_back(NumLexicalDeclContexts);
4403 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004404 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004405 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004406}
4407
Stephen Hines651f13c2014-04-23 16:59:28 -07004408void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004409 if (DeclUpdates.empty())
4410 return;
4411
Stephen Hines651f13c2014-04-23 16:59:28 -07004412 DeclUpdateMap LocalUpdates;
4413 LocalUpdates.swap(DeclUpdates);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004414
Stephen Hines651f13c2014-04-23 16:59:28 -07004415 for (auto &DeclUpdate : LocalUpdates) {
4416 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004417 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004418 continue; // The decl will be written completely,no need to store updates.
4419
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004420 OffsetsRecord.push_back(GetDeclRef(D));
Stephen Hines651f13c2014-04-23 16:59:28 -07004421 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4422
4423 bool HasUpdatedBody = false;
4424 RecordData Record;
4425 for (auto &Update : DeclUpdate.second) {
4426 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4427
4428 Record.push_back(Kind);
4429 switch (Kind) {
4430 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4431 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4432 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4433 Record.push_back(GetDeclRef(Update.getDecl()));
4434 break;
4435
4436 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4437 AddSourceLocation(Update.getLoc(), Record);
4438 break;
4439
4440 case UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION:
4441 // An updated body is emitted last, so that the reader doesn't need
4442 // to skip over the lazy body to reach statements for other records.
4443 Record.pop_back();
4444 HasUpdatedBody = true;
4445 break;
4446
4447 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4448 addExceptionSpec(
4449 *this,
4450 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4451 Record);
4452 break;
4453
4454 case UPD_CXX_DEDUCED_RETURN_TYPE:
4455 Record.push_back(GetOrCreateTypeID(Update.getType()));
4456 break;
4457
4458 case UPD_DECL_MARKED_USED:
4459 break;
4460
4461 case UPD_MANGLING_NUMBER:
4462 case UPD_STATIC_LOCAL_NUMBER:
4463 Record.push_back(Update.getNumber());
4464 break;
4465 }
4466 }
4467
4468 if (HasUpdatedBody) {
4469 const FunctionDecl *Def = cast<FunctionDecl>(D);
4470 Record.push_back(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION);
4471 Record.push_back(Def->isInlined());
4472 AddSourceLocation(Def->getInnerLocStart(), Record);
4473 AddFunctionDefinition(Def, Record);
4474 }
4475
4476 Stream.EmitRecord(DECL_UPDATES, Record);
4477
4478 // Flush any statements that were written as part of this update record.
4479 FlushStmts();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004480 }
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004481}
4482
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004483void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004484 if (ReplacedDecls.empty())
4485 return;
4486
4487 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004488 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4489 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004490 Record.push_back(I->ID);
4491 Record.push_back(I->Offset);
4492 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004493 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004494 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004495}
4496
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004497void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004498 Record.push_back(Loc.getRawEncoding());
4499}
4500
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004501void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004502 AddSourceLocation(Range.getBegin(), Record);
4503 AddSourceLocation(Range.getEnd(), Record);
4504}
4505
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004506void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004507 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004508 const uint64_t *Words = Value.getRawData();
4509 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004510}
4511
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004512void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004513 Record.push_back(Value.isUnsigned());
4514 AddAPInt(Value, Record);
4515}
4516
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004517void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004518 AddAPInt(Value.bitcastToAPInt(), Record);
4519}
4520
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004521void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004522 Record.push_back(getIdentifierRef(II));
4523}
4524
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004525IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004526 if (II == 0)
4527 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004528
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004529 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004530 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004531 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004532 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004533}
4534
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004535MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004536 // Don't emit builtin macros like __LINE__ to the AST file unless they
4537 // have been redefined by the header (in which case they are not
4538 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004539 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004540 return 0;
4541
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004542 MacroID &ID = MacroIDs[MI];
4543 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004544 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004545 MacroInfoToEmitData Info = { Name, MI, ID };
4546 MacroInfosToEmit.push_back(Info);
4547 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004548 return ID;
4549}
4550
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004551MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4552 if (MI == 0 || MI->isBuiltinMacro())
4553 return 0;
4554
4555 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4556 return MacroIDs[MI];
4557}
4558
4559uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4560 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4561 return IdentMacroDirectivesOffsetMap[Name];
4562}
4563
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004564void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004565 Record.push_back(getSelectorRef(SelRef));
4566}
4567
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004568SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004569 if (Sel.getAsOpaquePtr() == 0) {
4570 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004571 }
4572
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004573 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004574 if (SID == 0 && Chain) {
4575 // This might trigger a ReadSelector callback, which will set the ID for
4576 // this selector.
4577 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004578 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004579 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004580 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004581 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004582 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004583 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004584 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004585}
4586
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004587void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004588 AddDeclRef(Temp->getDestructor(), Record);
4589}
4590
Douglas Gregor7c789c12010-10-29 22:39:52 +00004591void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4592 CXXBaseSpecifier const *BasesEnd,
4593 RecordDataImpl &Record) {
4594 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4595 CXXBaseSpecifiersToWrite.push_back(
4596 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4597 Bases, BasesEnd));
4598 Record.push_back(NextCXXBaseSpecifiersID++);
4599}
4600
Sebastian Redla4232eb2010-08-18 23:56:21 +00004601void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004602 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004603 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004604 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004605 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004606 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004607 break;
4608 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004609 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004610 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004611 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004612 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004613 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004614 break;
4615 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004616 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004617 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004618 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004619 break;
John McCall833ca992009-10-29 08:12:44 +00004620 case TemplateArgument::Null:
4621 case TemplateArgument::Integral:
4622 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004623 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004624 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004625 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004626 break;
4627 }
4628}
4629
Sebastian Redla4232eb2010-08-18 23:56:21 +00004630void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004631 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004632 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004633
4634 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4635 bool InfoHasSameExpr
4636 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4637 Record.push_back(InfoHasSameExpr);
4638 if (InfoHasSameExpr)
4639 return; // Avoid storing the same expr twice.
4640 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004641 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4642 Record);
4643}
4644
Douglas Gregordc355712011-02-25 00:36:19 +00004645void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4646 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004647 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004648 AddTypeRef(QualType(), Record);
4649 return;
4650 }
4651
Douglas Gregordc355712011-02-25 00:36:19 +00004652 AddTypeLoc(TInfo->getTypeLoc(), Record);
4653}
4654
4655void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4656 AddTypeRef(TL.getType(), Record);
4657
John McCalla1ee0c52009-10-16 21:56:05 +00004658 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004659 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004660 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004661}
4662
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004663void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004664 Record.push_back(GetOrCreateTypeID(T));
4665}
4666
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004667TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004668 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004669 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004670 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4671}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004672
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004673TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004674 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004675 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004676 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004677}
4678
4679TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4680 if (T.isNull())
4681 return TypeIdx();
4682 assert(!T.getLocalFastQualifiers());
4683
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004684 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004685 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004686 if (DoneWritingDeclsAndTypes) {
4687 assert(0 && "New type seen after serializing all the types to emit!");
4688 return TypeIdx();
4689 }
4690
Douglas Gregor366809a2009-04-26 03:49:13 +00004691 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004692 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004693 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004694 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004695 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004696 return Idx;
4697}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004698
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004699TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004700 if (T.isNull())
4701 return TypeIdx();
4702 assert(!T.getLocalFastQualifiers());
4703
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004704 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4705 assert(I != TypeIdxs.end() && "Type not emitted!");
4706 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004707}
4708
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004709void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004710 Record.push_back(GetDeclRef(D));
4711}
4712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004713DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004714 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4715
Douglas Gregor2cf26342009-04-09 22:27:44 +00004716 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004717 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004718 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004719
4720 // If D comes from an AST file, its declaration ID is already known and
4721 // fixed.
4722 if (D->isFromASTFile())
4723 return D->getGlobalID();
4724
Douglas Gregor97475832010-10-05 18:37:06 +00004725 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004726 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004727 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004728 if (DoneWritingDeclsAndTypes) {
4729 assert(0 && "New decl seen after serializing all the decls to emit!");
4730 return 0;
4731 }
4732
Douglas Gregor2cf26342009-04-09 22:27:44 +00004733 // We haven't seen this declaration before. Give it a new ID and
4734 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004735 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004736 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004737 }
4738
Sebastian Redl681d7232010-07-27 00:17:23 +00004739 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004740}
4741
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004742DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004743 if (D == 0)
4744 return 0;
4745
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004746 // If D comes from an AST file, its declaration ID is already known and
4747 // fixed.
4748 if (D->isFromASTFile())
4749 return D->getGlobalID();
4750
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004751 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4752 return DeclIDs[D];
4753}
4754
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004755void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004756 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004757 assert(D);
4758
4759 SourceLocation Loc = D->getLocation();
4760 if (Loc.isInvalid())
4761 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004762
4763 // We only keep track of the file-level declarations of each file.
4764 if (!D->getLexicalDeclContext()->isFileContext())
4765 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004766 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4767 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004768 if (isa<ParmVarDecl>(D))
4769 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004770
4771 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004772 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004773 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004774 FileID FID;
4775 unsigned Offset;
Stephen Hines651f13c2014-04-23 16:59:28 -07004776 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004777 if (FID.isInvalid())
4778 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004779 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004780
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004781 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004782 if (!Info)
4783 Info = new DeclIDInFileInfo();
4784
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004785 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004786 LocDeclIDsTy &Decls = Info->DeclIDs;
4787
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004788 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004789 Decls.push_back(LocDecl);
4790 return;
4791 }
4792
Benjamin Kramer809d2542013-08-24 13:22:59 +00004793 LocDeclIDsTy::iterator I =
4794 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004795
4796 Decls.insert(I, LocDecl);
4797}
4798
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004799void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004800 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004801 Record.push_back(Name.getNameKind());
4802 switch (Name.getNameKind()) {
4803 case DeclarationName::Identifier:
4804 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4805 break;
4806
4807 case DeclarationName::ObjCZeroArgSelector:
4808 case DeclarationName::ObjCOneArgSelector:
4809 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004810 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004811 break;
4812
4813 case DeclarationName::CXXConstructorName:
4814 case DeclarationName::CXXDestructorName:
4815 case DeclarationName::CXXConversionFunctionName:
4816 AddTypeRef(Name.getCXXNameType(), Record);
4817 break;
4818
4819 case DeclarationName::CXXOperatorName:
4820 Record.push_back(Name.getCXXOverloadedOperator());
4821 break;
4822
Sean Hunt3e518bd2009-11-29 07:34:05 +00004823 case DeclarationName::CXXLiteralOperatorName:
4824 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4825 break;
4826
Douglas Gregor2cf26342009-04-09 22:27:44 +00004827 case DeclarationName::CXXUsingDirective:
4828 // No extra data to emit
4829 break;
4830 }
4831}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004832
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004833void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004834 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004835 switch (Name.getNameKind()) {
4836 case DeclarationName::CXXConstructorName:
4837 case DeclarationName::CXXDestructorName:
4838 case DeclarationName::CXXConversionFunctionName:
4839 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4840 break;
4841
4842 case DeclarationName::CXXOperatorName:
4843 AddSourceLocation(
4844 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4845 Record);
4846 AddSourceLocation(
4847 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4848 Record);
4849 break;
4850
4851 case DeclarationName::CXXLiteralOperatorName:
4852 AddSourceLocation(
4853 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4854 Record);
4855 break;
4856
4857 case DeclarationName::Identifier:
4858 case DeclarationName::ObjCZeroArgSelector:
4859 case DeclarationName::ObjCOneArgSelector:
4860 case DeclarationName::ObjCMultiArgSelector:
4861 case DeclarationName::CXXUsingDirective:
4862 break;
4863 }
4864}
4865
4866void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004867 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004868 AddDeclarationName(NameInfo.getName(), Record);
4869 AddSourceLocation(NameInfo.getLoc(), Record);
4870 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4871}
4872
4873void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004874 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004875 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004876 Record.push_back(Info.NumTemplParamLists);
4877 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4878 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4879}
4880
Sebastian Redla4232eb2010-08-18 23:56:21 +00004881void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004882 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004883 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004884 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004885 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004886
4887 // Push each of the NNS's onto a stack for serialization in reverse order.
4888 while (NNS) {
4889 NestedNames.push_back(NNS);
4890 NNS = NNS->getPrefix();
4891 }
4892
4893 Record.push_back(NestedNames.size());
4894 while(!NestedNames.empty()) {
4895 NNS = NestedNames.pop_back_val();
4896 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4897 Record.push_back(Kind);
4898 switch (Kind) {
4899 case NestedNameSpecifier::Identifier:
4900 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4901 break;
4902
4903 case NestedNameSpecifier::Namespace:
4904 AddDeclRef(NNS->getAsNamespace(), Record);
4905 break;
4906
Douglas Gregor14aba762011-02-24 02:36:08 +00004907 case NestedNameSpecifier::NamespaceAlias:
4908 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4909 break;
4910
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004911 case NestedNameSpecifier::TypeSpec:
4912 case NestedNameSpecifier::TypeSpecWithTemplate:
4913 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4914 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4915 break;
4916
4917 case NestedNameSpecifier::Global:
4918 // Don't need to write an associated value.
4919 break;
4920 }
4921 }
4922}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004923
Douglas Gregordc355712011-02-25 00:36:19 +00004924void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4925 RecordDataImpl &Record) {
4926 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004927 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004928 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004929
4930 // Push each of the nested-name-specifiers's onto a stack for
4931 // serialization in reverse order.
4932 while (NNS) {
4933 NestedNames.push_back(NNS);
4934 NNS = NNS.getPrefix();
4935 }
4936
4937 Record.push_back(NestedNames.size());
4938 while(!NestedNames.empty()) {
4939 NNS = NestedNames.pop_back_val();
4940 NestedNameSpecifier::SpecifierKind Kind
4941 = NNS.getNestedNameSpecifier()->getKind();
4942 Record.push_back(Kind);
4943 switch (Kind) {
4944 case NestedNameSpecifier::Identifier:
4945 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4946 AddSourceRange(NNS.getLocalSourceRange(), Record);
4947 break;
4948
4949 case NestedNameSpecifier::Namespace:
4950 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4951 AddSourceRange(NNS.getLocalSourceRange(), Record);
4952 break;
4953
4954 case NestedNameSpecifier::NamespaceAlias:
4955 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4956 AddSourceRange(NNS.getLocalSourceRange(), Record);
4957 break;
4958
4959 case NestedNameSpecifier::TypeSpec:
4960 case NestedNameSpecifier::TypeSpecWithTemplate:
4961 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4962 AddTypeLoc(NNS.getTypeLoc(), Record);
4963 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4964 break;
4965
4966 case NestedNameSpecifier::Global:
4967 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4968 break;
4969 }
4970 }
4971}
4972
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004973void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004974 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004975 Record.push_back(Kind);
4976 switch (Kind) {
4977 case TemplateName::Template:
4978 AddDeclRef(Name.getAsTemplateDecl(), Record);
4979 break;
4980
4981 case TemplateName::OverloadedTemplate: {
4982 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4983 Record.push_back(OvT->size());
4984 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4985 I != E; ++I)
4986 AddDeclRef(*I, Record);
4987 break;
4988 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004989
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004990 case TemplateName::QualifiedTemplate: {
4991 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4992 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4993 Record.push_back(QualT->hasTemplateKeyword());
4994 AddDeclRef(QualT->getTemplateDecl(), Record);
4995 break;
4996 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004997
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004998 case TemplateName::DependentTemplate: {
4999 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5000 AddNestedNameSpecifier(DepT->getQualifier(), Record);
5001 Record.push_back(DepT->isIdentifier());
5002 if (DepT->isIdentifier())
5003 AddIdentifierRef(DepT->getIdentifier(), Record);
5004 else
5005 Record.push_back(DepT->getOperator());
5006 break;
5007 }
John McCall14606042011-06-30 08:33:18 +00005008
5009 case TemplateName::SubstTemplateTemplateParm: {
5010 SubstTemplateTemplateParmStorage *subst
5011 = Name.getAsSubstTemplateTemplateParm();
5012 AddDeclRef(subst->getParameter(), Record);
5013 AddTemplateName(subst->getReplacement(), Record);
5014 break;
5015 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00005016
5017 case TemplateName::SubstTemplateTemplateParmPack: {
5018 SubstTemplateTemplateParmPackStorage *SubstPack
5019 = Name.getAsSubstTemplateTemplateParmPack();
5020 AddDeclRef(SubstPack->getParameterPack(), Record);
5021 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5022 break;
5023 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005024 }
5025}
5026
Michael J. Spencer20249a12010-10-21 03:16:25 +00005027void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005028 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005029 Record.push_back(Arg.getKind());
5030 switch (Arg.getKind()) {
5031 case TemplateArgument::Null:
5032 break;
5033 case TemplateArgument::Type:
5034 AddTypeRef(Arg.getAsType(), Record);
5035 break;
5036 case TemplateArgument::Declaration:
5037 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00005038 Record.push_back(Arg.isDeclForReferenceParam());
5039 break;
5040 case TemplateArgument::NullPtr:
5041 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005042 break;
5043 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00005044 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005045 AddTypeRef(Arg.getIntegralType(), Record);
5046 break;
5047 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00005048 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5049 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00005050 case TemplateArgument::TemplateExpansion:
5051 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00005052 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00005053 Record.push_back(*NumExpansions + 1);
5054 else
5055 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00005056 break;
5057 case TemplateArgument::Expression:
5058 AddStmt(Arg.getAsExpr());
5059 break;
5060 case TemplateArgument::Pack:
5061 Record.push_back(Arg.pack_size());
5062 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
5063 I != E; ++I)
5064 AddTemplateArgument(*I, Record);
5065 break;
5066 }
5067}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005068
5069void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005070ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005071 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005072 assert(TemplateParams && "No TemplateParams!");
5073 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5074 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5075 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5076 Record.push_back(TemplateParams->size());
5077 for (TemplateParameterList::const_iterator
5078 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5079 P != PEnd; ++P)
5080 AddDeclRef(*P, Record);
5081}
5082
5083/// \brief Emit a template argument list.
5084void
Sebastian Redla4232eb2010-08-18 23:56:21 +00005085ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005086 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005087 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00005088 Record.push_back(TemplateArgs->size());
5089 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00005090 AddTemplateArgument(TemplateArgs->get(i), Record);
5091}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005092
Enea Zaffanellac1cef082013-08-10 07:24:53 +00005093void
5094ASTWriter::AddASTTemplateArgumentListInfo
5095(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5096 assert(ASTTemplArgList && "No ASTTemplArgList!");
5097 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5098 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5099 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5100 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5101 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5102 AddTemplateArgumentLoc(TemplArgs[i], Record);
5103}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005104
5105void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005106ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005107 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00005108 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00005109 I = Set.begin(), E = Set.end(); I != E; ++I) {
5110 AddDeclRef(I.getDecl(), Record);
5111 Record.push_back(I.getAccess());
5112 }
5113}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005114
Sebastian Redla4232eb2010-08-18 23:56:21 +00005115void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005116 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005117 Record.push_back(Base.isVirtual());
5118 Record.push_back(Base.isBaseOfClass());
5119 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00005120 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00005121 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005122 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00005123 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5124 : SourceLocation(),
5125 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00005126}
Sebastian Redl30c514c2010-07-14 23:45:08 +00005127
Douglas Gregor7c789c12010-10-29 22:39:52 +00005128void ASTWriter::FlushCXXBaseSpecifiers() {
5129 RecordData Record;
5130 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5131 Record.clear();
5132
5133 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00005134 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005135 if (Index == CXXBaseSpecifiersOffsets.size())
5136 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5137 else {
5138 if (Index > CXXBaseSpecifiersOffsets.size())
5139 CXXBaseSpecifiersOffsets.resize(Index + 1);
5140 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5141 }
5142
5143 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5144 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5145 Record.push_back(BEnd - B);
5146 for (; B != BEnd; ++B)
5147 AddCXXBaseSpecifier(*B, Record);
5148 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00005149
5150 // Flush any expressions that were written as part of the base specifiers.
5151 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00005152 }
5153
5154 CXXBaseSpecifiersToWrite.clear();
5155}
5156
Sean Huntcbb67482011-01-08 20:30:50 +00005157void ASTWriter::AddCXXCtorInitializers(
5158 const CXXCtorInitializer * const *CtorInitializers,
5159 unsigned NumCtorInitializers,
5160 RecordDataImpl &Record) {
5161 Record.push_back(NumCtorInitializers);
5162 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5163 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005164
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005165 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00005166 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00005167 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005168 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00005169 } else if (Init->isDelegatingInitializer()) {
5170 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00005171 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00005172 } else if (Init->isMemberInitializer()){
5173 Record.push_back(CTOR_INITIALIZER_MEMBER);
5174 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005175 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00005176 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5177 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005178 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00005179
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005180 AddSourceLocation(Init->getMemberLocation(), Record);
5181 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005182 AddSourceLocation(Init->getLParenLoc(), Record);
5183 AddSourceLocation(Init->getRParenLoc(), Record);
5184 Record.push_back(Init->isWritten());
5185 if (Init->isWritten()) {
5186 Record.push_back(Init->getSourceOrder());
5187 } else {
5188 Record.push_back(Init->getNumArrayIndices());
5189 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5190 AddDeclRef(Init->getArrayIndex(i), Record);
5191 }
5192 }
5193}
5194
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005195void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5196 assert(D->DefinitionData);
5197 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005198 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005199 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005200 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005201 Record.push_back(Data.Aggregate);
5202 Record.push_back(Data.PlainOldData);
5203 Record.push_back(Data.Empty);
5204 Record.push_back(Data.Polymorphic);
5205 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005206 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005207 Record.push_back(Data.HasNoNonEmptyBases);
5208 Record.push_back(Data.HasPrivateFields);
5209 Record.push_back(Data.HasProtectedFields);
5210 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005211 Record.push_back(Data.HasMutableFields);
Stephen Hines651f13c2014-04-23 16:59:28 -07005212 Record.push_back(Data.HasVariantMembers);
Richard Smithdfefb842012-02-25 07:33:38 +00005213 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005214 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005215 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005216 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5217 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5218 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5219 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5220 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5221 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005222 Record.push_back(Data.HasTrivialSpecialMembers);
5223 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005224 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005225 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005226 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005227 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005228 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005229 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005230 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005231 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5232 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5233 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5234 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smithdfefb842012-02-25 07:33:38 +00005235 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005236
5237 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005238 if (Data.NumBases > 0)
5239 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5240 Record);
5241
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005242 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5243 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005244 if (Data.NumVBases > 0)
5245 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5246 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005247
Richard Smithc2d77572013-08-30 04:46:40 +00005248 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5249 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005250 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005251 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005252
5253 // Add lambda-specific data.
5254 if (Data.IsLambda) {
5255 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005256 Record.push_back(Lambda.Dependent);
Faisal Valibef582b2013-10-23 16:10:50 +00005257 Record.push_back(Lambda.IsGenericLambda);
5258 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005259 Record.push_back(Lambda.NumCaptures);
5260 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005261 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005262 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005263 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005264 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5265 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5266 AddSourceLocation(Capture.getLocation(), Record);
5267 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005268 Record.push_back(Capture.getCaptureKind());
5269 switch (Capture.getCaptureKind()) {
5270 case LCK_This:
5271 break;
5272 case LCK_ByCopy:
Richard Smith04fa7a32013-09-28 04:02:39 +00005273 case LCK_ByRef:
Richard Smith0d8e9642013-05-16 06:20:58 +00005274 VarDecl *Var =
5275 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5276 AddDeclRef(Var, Record);
5277 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5278 : SourceLocation(),
5279 Record);
5280 break;
5281 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005282 }
5283 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005284}
5285
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005286void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005287 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005288 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005289 assert(FirstDeclID == NextDeclID &&
5290 FirstTypeID == NextTypeID &&
5291 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005292 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005293 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005294 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005295 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005296
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005297 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005298
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005299 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5300 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5301 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005302 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005303 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005304 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005305 NextDeclID = FirstDeclID;
5306 NextTypeID = FirstTypeID;
5307 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005308 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005309 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005310 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005311}
5312
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005313void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005314 // Always keep the highest ID. See \p TypeRead() for more information.
5315 IdentID &StoredID = IdentifierIDs[II];
5316 if (ID > StoredID)
5317 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005318}
5319
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005320void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005321 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005322 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005323 if (ID > StoredID)
5324 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005325}
5326
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005327void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005328 // Always take the highest-numbered type index. This copes with an interesting
5329 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005330 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005331 // keep the higher-numbered entry so that we can properly write it out to
5332 // the AST file.
5333 TypeIdx &StoredIdx = TypeIdxs[T];
5334 if (Idx.getIndex() >= StoredIdx.getIndex())
5335 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005336}
5337
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005338void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005339 // Always keep the highest ID. See \p TypeRead() for more information.
5340 SelectorID &StoredID = SelectorIDs[S];
5341 if (ID > StoredID)
5342 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005343}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005344
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005345void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005346 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005347 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005348 MacroDefinitions[MD] = ID;
5349}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005350
Douglas Gregora015cab2011-12-02 17:30:13 +00005351void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5352 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5353 SubmoduleIDs[Mod] = ID;
5354}
5355
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005356void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005357 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005358 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005359 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5360 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005361 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005362 // A forward reference was mutated into a definition. Rewrite it.
5363 // FIXME: This happens during template instantiation, should we
5364 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005365 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005366 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005367 }
5368}
Douglas Gregora8235d62012-10-09 23:05:51 +00005369
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005370void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005371 assert(!WritingAST && "Already writing the AST!");
5372
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005373 // TU and namespaces are handled elsewhere.
5374 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5375 return;
5376
Douglas Gregor919814d2011-09-09 23:01:35 +00005377 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005378 return; // Not a source decl added to a DeclContext from PCH.
5379
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005380 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005381 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005382 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005383}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005384
5385void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005386 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005387 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005388 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005389 return; // Not a source member added to a class from PCH.
5390 if (!isa<CXXMethodDecl>(D))
5391 return; // We are interested in lazily declared implicit methods.
5392
5393 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005394 assert(RD->isCompleteDefinition());
Stephen Hines651f13c2014-04-23 16:59:28 -07005395 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005396}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005397
5398void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5399 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005400 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005401 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005402 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005403 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005404 return; // Not a source specialization added to a template from PCH.
5405
Stephen Hines651f13c2014-04-23 16:59:28 -07005406 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5407 D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005408}
Douglas Gregor89d99802010-11-30 06:16:57 +00005409
Larisse Voufoef4579c2013-08-06 01:03:05 +00005410void ASTWriter::AddedCXXTemplateSpecialization(
5411 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5412 // The specializations set is kept in the canonical template.
5413 assert(!WritingAST && "Already writing the AST!");
5414 TD = TD->getCanonicalDecl();
5415 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5416 return; // Not a source specialization added to a template from PCH.
5417
Stephen Hines651f13c2014-04-23 16:59:28 -07005418 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5419 D));
Larisse Voufoef4579c2013-08-06 01:03:05 +00005420}
5421
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005422void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5423 const FunctionDecl *D) {
5424 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005425 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005426 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005427 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005428 return; // Not a source specialization added to a template from PCH.
5429
Stephen Hines651f13c2014-04-23 16:59:28 -07005430 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5431 D));
5432}
5433
5434void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5435 assert(!WritingAST && "Already writing the AST!");
5436 FD = FD->getCanonicalDecl();
5437 if (!FD->isFromASTFile())
5438 return; // Not a function declared in PCH and defined outside.
5439
5440 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005441}
5442
Richard Smith9dadfab2013-05-11 05:45:24 +00005443void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5444 assert(!WritingAST && "Already writing the AST!");
5445 FD = FD->getCanonicalDecl();
5446 if (!FD->isFromASTFile())
5447 return; // Not a function declared in PCH and defined outside.
5448
Stephen Hines651f13c2014-04-23 16:59:28 -07005449 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith9dadfab2013-05-11 05:45:24 +00005450}
5451
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005452void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005453 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005454 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005455 return; // Declaration not imported from PCH.
5456
5457 // Implicit decl from a PCH was defined.
5458 // FIXME: Should implicit definition be a separate FunctionDecl?
5459 RewriteDecl(D);
5460}
5461
Stephen Hines651f13c2014-04-23 16:59:28 -07005462void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5463 assert(!WritingAST && "Already writing the AST!");
5464 if (!D->isFromASTFile())
5465 return;
5466
5467 // Since the actual instantiation is delayed, this really means that we need
5468 // to update the instantiation location.
5469 DeclUpdates[D].push_back(
5470 DeclUpdate(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION));
5471}
5472
Sebastian Redlf79a7192011-04-29 08:19:30 +00005473void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005474 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005475 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005476 return;
5477
5478 // Since the actual instantiation is delayed, this really means that we need
5479 // to update the instantiation location.
Stephen Hines651f13c2014-04-23 16:59:28 -07005480 DeclUpdates[D].push_back(
5481 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5482 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redlf79a7192011-04-29 08:19:30 +00005483}
5484
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005485void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5486 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005487 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005488 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005489 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005490
5491 assert(IFD->getDefinition() && "Category on a class without a definition?");
5492 ObjCClassesWithCategories.insert(
5493 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005494}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005495
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005496
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005497void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5498 const ObjCPropertyDecl *OrigProp,
5499 const ObjCCategoryDecl *ClassExt) {
5500 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5501 if (!D)
5502 return;
5503
5504 assert(!WritingAST && "Already writing the AST!");
5505 if (!D->isFromASTFile())
5506 return; // Declaration not imported from PCH.
5507
5508 RewriteDecl(D);
5509}
Eli Friedman86164e82013-09-05 00:02:25 +00005510
5511void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5512 assert(!WritingAST && "Already writing the AST!");
5513 if (!D->isFromASTFile())
5514 return;
5515
Stephen Hines651f13c2014-04-23 16:59:28 -07005516 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman86164e82013-09-05 00:02:25 +00005517}