blob: 1186bfb58294903f0917a292e1b2c43c8efac4aa [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Benjamin Kramerae0cdff2013-08-24 13:16:22 +000046#include "llvm/ADT/STLExtras.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"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000049#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000050#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000051#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000052#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000053#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000054#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000055#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000056using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000057using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
Sebastian Redlade50002010-07-30 17:03:48 +000059template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000060static StringRef data(const std::vector<T, Allocator> &v) {
61 if (v.empty()) return StringRef();
62 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000063 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000064}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065
66template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000067static StringRef data(const SmallVectorImpl<T> &v) {
68 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000069 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000070}
71
Douglas Gregor2cf26342009-04-09 22:27:44 +000072//===----------------------------------------------------------------------===//
73// Type serialization
74//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000075
Douglas Gregor2cf26342009-04-09 22:27:44 +000076namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000077 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000078 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000079 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
81 public:
82 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000083 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000084
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000085 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000086 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000087
88 void VisitArrayType(const ArrayType *T);
89 void VisitFunctionType(const FunctionType *T);
90 void VisitTagType(const TagType *T);
91
92#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
93#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000094#include "clang/AST/TypeNodes.def"
95 };
96}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000099 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Reid Kleckner12df2462013-06-24 17:51:48 +0000112void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
113 Writer.AddTypeRef(T->getOriginalType(), Record);
114 Code = TYPE_DECAYED;
115}
116
Sebastian Redl3397c552010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000118 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000123 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
124 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000125 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126}
127
Sebastian Redl3397c552010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000129 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 Writer.AddTypeRef(T->getPointeeType(), Record);
135 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000136 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 Writer.AddTypeRef(T->getElementType(), Record);
141 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000142 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
147 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000148 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000149}
150
Sebastian Redl3397c552010-08-18 23:56:27 +0000151void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000153 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154}
155
Sebastian Redl3397c552010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000158 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
159 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000160 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000161 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162}
163
Sebastian Redl3397c552010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165 Writer.AddTypeRef(T->getElementType(), Record);
166 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000167 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000168 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169}
170
Sebastian Redl3397c552010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000172 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000173 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174}
175
Sebastian Redl3397c552010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000177 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000178 FunctionType::ExtInfo C = T->getExtInfo();
179 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000180 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000181 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000182 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000183 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000184 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185}
186
Sebastian Redl3397c552010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000188 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000189 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000190}
191
Sebastian Redl3397c552010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 VisitFunctionType(T);
194 Record.push_back(T->getNumArgs());
195 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
196 Writer.AddTypeRef(T->getArgType(I), Record);
197 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000198 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000200 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
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 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000214 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000215}
216
Sebastian Redl3397c552010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000218 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000219 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000220}
John McCalled976492009-12-04 22:46:56 +0000221
Sebastian Redl3397c552010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000224 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
225 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227}
228
Sebastian Redl3397c552010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000230 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000231 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000232}
233
Sebastian Redl3397c552010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000235 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000237}
238
Sebastian Redl3397c552010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000241 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000242 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000243}
244
Sean Huntca63c202011-05-24 22:41:36 +0000245void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
246 Writer.AddTypeRef(T->getBaseType(), Record);
247 Writer.AddTypeRef(T->getUnderlyingType(), Record);
248 Record.push_back(T->getUTTKind());
249 Code = TYPE_UNARY_TRANSFORM;
250}
251
Richard Smith34b41d92011-02-20 03:19:35 +0000252void ASTTypeWriter::VisitAutoType(const AutoType *T) {
253 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smitha2c36462013-04-26 16:15:35 +0000254 Record.push_back(T->isDecltypeAuto());
Richard Smithdc7a4f52013-04-30 13:56:41 +0000255 if (T->getDeducedType().isNull())
256 Record.push_back(T->isDependentType());
Richard Smith34b41d92011-02-20 03:19:35 +0000257 Code = TYPE_AUTO;
258}
259
Sebastian Redl3397c552010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000261 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000262 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000263 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264 "Cannot serialize in the middle of a type definition");
265}
266
Sebastian Redl3397c552010-08-18 23:56:27 +0000267void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000268 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000269 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000270}
271
Sebastian Redl3397c552010-08-18 23:56:27 +0000272void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000273 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000274 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000275}
276
John McCall9d156a72011-01-06 01:58:22 +0000277void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
278 Writer.AddTypeRef(T->getModifiedType(), Record);
279 Writer.AddTypeRef(T->getEquivalentType(), Record);
280 Record.push_back(T->getAttrKind());
281 Code = TYPE_ATTRIBUTED;
282}
283
Mike Stump1eb44332009-09-09 15:08:12 +0000284void
Sebastian Redl3397c552010-08-18 23:56:27 +0000285ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000286 const SubstTemplateTypeParmType *T) {
287 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
288 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000289 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000290}
291
292void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000293ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
294 const SubstTemplateTypeParmPackType *T) {
295 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
296 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
297 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
298}
299
300void
Sebastian Redl3397c552010-08-18 23:56:27 +0000301ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000302 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000303 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000304 Writer.AddTemplateName(T->getTemplateName(), Record);
305 Record.push_back(T->getNumArgs());
306 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
307 ArgI != ArgE; ++ArgI)
308 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000309 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
310 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000311 : T->getCanonicalTypeInternal(),
312 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000313 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314}
315
316void
Sebastian Redl3397c552010-08-18 23:56:27 +0000317ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000318 VisitArrayType(T);
319 Writer.AddStmt(T->getSizeExpr());
320 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000321 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000322}
323
324void
Sebastian Redl3397c552010-08-18 23:56:27 +0000325ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000326 const DependentSizedExtVectorType *T) {
327 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000328 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000329}
330
331void
Sebastian Redl3397c552010-08-18 23:56:27 +0000332ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000333 Record.push_back(T->getDepth());
334 Record.push_back(T->getIndex());
335 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000336 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000337 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000338}
339
340void
Sebastian Redl3397c552010-08-18 23:56:27 +0000341ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000342 Record.push_back(T->getKeyword());
343 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
344 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000345 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
346 : T->getCanonicalTypeInternal(),
347 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000349}
350
351void
Sebastian Redl3397c552010-08-18 23:56:27 +0000352ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000353 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000354 Record.push_back(T->getKeyword());
355 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
356 Writer.AddIdentifierRef(T->getIdentifier(), Record);
357 Record.push_back(T->getNumArgs());
358 for (DependentTemplateSpecializationType::iterator
359 I = T->begin(), E = T->end(); I != E; ++I)
360 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000361 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000362}
363
Douglas Gregor7536dd52010-12-20 02:24:11 +0000364void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
365 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000366 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000367 Record.push_back(*NumExpansions + 1);
368 else
369 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000370 Code = TYPE_PACK_EXPANSION;
371}
372
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000373void ASTTypeWriter::VisitParenType(const ParenType *T) {
374 Writer.AddTypeRef(T->getInnerType(), Record);
375 Code = TYPE_PAREN;
376}
377
Sebastian Redl3397c552010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000379 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000380 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
381 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000383}
384
Sebastian Redl3397c552010-08-18 23:56:27 +0000385void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000386 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000387 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000388 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000389}
390
Sebastian Redl3397c552010-08-18 23:56:27 +0000391void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000392 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000393 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000394}
395
Sebastian Redl3397c552010-08-18 23:56:27 +0000396void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000397 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000398 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000399 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000400 E = T->qual_end(); I != E; ++I)
401 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000402 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000403}
404
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000405void
Sebastian Redl3397c552010-08-18 23:56:27 +0000406ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000407 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000408 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000409}
410
Eli Friedmanb001de72011-10-06 23:00:33 +0000411void
412ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
413 Writer.AddTypeRef(T->getValueType(), Record);
414 Code = TYPE_ATOMIC;
415}
416
John McCalla1ee0c52009-10-16 21:56:05 +0000417namespace {
418
419class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000420 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000421 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000422
423public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000424 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000425 : Writer(Writer), Record(Record) { }
426
John McCall51bd8032009-10-18 01:05:36 +0000427#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000428#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000429 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000430#include "clang/AST/TypeLocNodes.def"
431
John McCall51bd8032009-10-18 01:05:36 +0000432 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
433 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000434};
435
436}
437
John McCall51bd8032009-10-18 01:05:36 +0000438void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
439 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000442 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
443 if (TL.needsExtraLocalData()) {
444 Record.push_back(TL.getWrittenTypeSpec());
445 Record.push_back(TL.getWrittenSignSpec());
446 Record.push_back(TL.getWrittenWidthSpec());
447 Record.push_back(TL.hasModeAttr());
448 }
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
Reid Kleckner12df2462013-06-24 17:51:48 +0000456void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
457 // nothing to do
458}
John McCall51bd8032009-10-18 01:05:36 +0000459void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000461}
John McCall51bd8032009-10-18 01:05:36 +0000462void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000464}
John McCall51bd8032009-10-18 01:05:36 +0000465void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000467}
John McCall51bd8032009-10-18 01:05:36 +0000468void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000470 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000471}
John McCall51bd8032009-10-18 01:05:36 +0000472void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
474 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
475 Record.push_back(TL.getSizeExpr() ? 1 : 0);
476 if (TL.getSizeExpr())
477 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000478}
John McCall51bd8032009-10-18 01:05:36 +0000479void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
480 VisitArrayTypeLoc(TL);
481}
482void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
483 VisitArrayTypeLoc(TL);
484}
485void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
486 VisitArrayTypeLoc(TL);
487}
488void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
489 DependentSizedArrayTypeLoc TL) {
490 VisitArrayTypeLoc(TL);
491}
492void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
493 DependentSizedExtVectorTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
495}
496void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getNameLoc(), Record);
498}
499void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getNameLoc(), Record);
501}
502void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000503 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000504 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
505 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000506 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000507 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
508 Writer.AddDeclRef(TL.getArg(i), Record);
509}
510void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
511 VisitFunctionTypeLoc(TL);
512}
513void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
514 VisitFunctionTypeLoc(TL);
515}
John McCalled976492009-12-04 22:46:56 +0000516void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
John McCall51bd8032009-10-18 01:05:36 +0000519void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
522void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000523 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
524 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
525 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000526}
527void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000528 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
529 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
530 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
531 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000532}
533void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
Sean Huntca63c202011-05-24 22:41:36 +0000536void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getKWLoc(), Record);
538 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
539 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
540 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
541}
Richard Smith34b41d92011-02-20 03:19:35 +0000542void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall51bd8032009-10-18 01:05:36 +0000545void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
548void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
John McCall9d156a72011-01-06 01:58:22 +0000551void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
553 if (TL.hasAttrOperand()) {
554 SourceRange range = TL.getAttrOperandParensRange();
555 Writer.AddSourceLocation(range.getBegin(), Record);
556 Writer.AddSourceLocation(range.getEnd(), Record);
557 }
558 if (TL.hasAttrExprOperand()) {
559 Expr *operand = TL.getAttrExprOperand();
560 Record.push_back(operand ? 1 : 0);
561 if (operand) Writer.AddStmt(operand);
562 } else if (TL.hasAttrEnumOperand()) {
563 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
564 }
565}
John McCall51bd8032009-10-18 01:05:36 +0000566void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
567 Writer.AddSourceLocation(TL.getNameLoc(), Record);
568}
John McCall49a832b2009-10-18 09:09:24 +0000569void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
570 SubstTemplateTypeParmTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000573void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
574 SubstTemplateTypeParmPackTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576}
John McCall51bd8032009-10-18 01:05:36 +0000577void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
578 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000579 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000580 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
581 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
582 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
583 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000584 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
585 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000586}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000587void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
589 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
590}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000591void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000592 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000593 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000594}
John McCall3cb0ebd2010-03-10 03:28:59 +0000595void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
596 Writer.AddSourceLocation(TL.getNameLoc(), Record);
597}
Douglas Gregor4714c122010-03-31 17:34:00 +0000598void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000599 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000600 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000601 Writer.AddSourceLocation(TL.getNameLoc(), Record);
602}
John McCall33500952010-06-11 00:33:02 +0000603void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
604 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000605 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000606 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000607 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000608 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000609 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
610 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
611 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000612 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
613 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000614}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000615void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
617}
John McCall51bd8032009-10-18 01:05:36 +0000618void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
619 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000620}
621void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
622 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000623 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
624 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
625 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
626 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000627}
John McCall54e14c42009-10-22 22:37:11 +0000628void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
629 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000630}
Eli Friedmanb001de72011-10-06 23:00:33 +0000631void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
632 Writer.AddSourceLocation(TL.getKWLoc(), Record);
633 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
634 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
635}
John McCalla1ee0c52009-10-16 21:56:05 +0000636
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000637//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000638// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000639//===----------------------------------------------------------------------===//
640
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000641static void EmitBlockID(unsigned ID, const char *Name,
642 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000643 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000644 Record.clear();
645 Record.push_back(ID);
646 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
647
648 // Emit the block name if present.
649 if (Name == 0 || Name[0] == 0) return;
650 Record.clear();
651 while (*Name)
652 Record.push_back(*Name++);
653 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
654}
655
656static void EmitRecordID(unsigned ID, const char *Name,
657 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000658 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000659 Record.clear();
660 Record.push_back(ID);
661 while (*Name)
662 Record.push_back(*Name++);
663 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000664}
665
666static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000667 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000668#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000669 RECORD(STMT_STOP);
670 RECORD(STMT_NULL_PTR);
671 RECORD(STMT_NULL);
672 RECORD(STMT_COMPOUND);
673 RECORD(STMT_CASE);
674 RECORD(STMT_DEFAULT);
675 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000676 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000677 RECORD(STMT_IF);
678 RECORD(STMT_SWITCH);
679 RECORD(STMT_WHILE);
680 RECORD(STMT_DO);
681 RECORD(STMT_FOR);
682 RECORD(STMT_GOTO);
683 RECORD(STMT_INDIRECT_GOTO);
684 RECORD(STMT_CONTINUE);
685 RECORD(STMT_BREAK);
686 RECORD(STMT_RETURN);
687 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000688 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000689 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000690 RECORD(EXPR_PREDEFINED);
691 RECORD(EXPR_DECL_REF);
692 RECORD(EXPR_INTEGER_LITERAL);
693 RECORD(EXPR_FLOATING_LITERAL);
694 RECORD(EXPR_IMAGINARY_LITERAL);
695 RECORD(EXPR_STRING_LITERAL);
696 RECORD(EXPR_CHARACTER_LITERAL);
697 RECORD(EXPR_PAREN);
698 RECORD(EXPR_UNARY_OPERATOR);
699 RECORD(EXPR_SIZEOF_ALIGN_OF);
700 RECORD(EXPR_ARRAY_SUBSCRIPT);
701 RECORD(EXPR_CALL);
702 RECORD(EXPR_MEMBER);
703 RECORD(EXPR_BINARY_OPERATOR);
704 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
705 RECORD(EXPR_CONDITIONAL_OPERATOR);
706 RECORD(EXPR_IMPLICIT_CAST);
707 RECORD(EXPR_CSTYLE_CAST);
708 RECORD(EXPR_COMPOUND_LITERAL);
709 RECORD(EXPR_EXT_VECTOR_ELEMENT);
710 RECORD(EXPR_INIT_LIST);
711 RECORD(EXPR_DESIGNATED_INIT);
712 RECORD(EXPR_IMPLICIT_VALUE_INIT);
713 RECORD(EXPR_VA_ARG);
714 RECORD(EXPR_ADDR_LABEL);
715 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000716 RECORD(EXPR_CHOOSE);
717 RECORD(EXPR_GNU_NULL);
718 RECORD(EXPR_SHUFFLE_VECTOR);
719 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000720 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000721 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000722 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000723 RECORD(EXPR_OBJC_ARRAY_LITERAL);
724 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000725 RECORD(EXPR_OBJC_ENCODE);
726 RECORD(EXPR_OBJC_SELECTOR_EXPR);
727 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
728 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
729 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
730 RECORD(EXPR_OBJC_KVC_REF_EXPR);
731 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000732 RECORD(STMT_OBJC_FOR_COLLECTION);
733 RECORD(STMT_OBJC_CATCH);
734 RECORD(STMT_OBJC_FINALLY);
735 RECORD(STMT_OBJC_AT_TRY);
736 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
737 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000738 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000739 RECORD(EXPR_CXX_OPERATOR_CALL);
740 RECORD(EXPR_CXX_CONSTRUCT);
741 RECORD(EXPR_CXX_STATIC_CAST);
742 RECORD(EXPR_CXX_DYNAMIC_CAST);
743 RECORD(EXPR_CXX_REINTERPRET_CAST);
744 RECORD(EXPR_CXX_CONST_CAST);
745 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000746 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smith7c3e6152013-06-12 22:31:48 +0000747 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000748 RECORD(EXPR_CXX_BOOL_LITERAL);
749 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000750 RECORD(EXPR_CXX_TYPEID_EXPR);
751 RECORD(EXPR_CXX_TYPEID_TYPE);
752 RECORD(EXPR_CXX_UUIDOF_EXPR);
753 RECORD(EXPR_CXX_UUIDOF_TYPE);
754 RECORD(EXPR_CXX_THIS);
755 RECORD(EXPR_CXX_THROW);
756 RECORD(EXPR_CXX_DEFAULT_ARG);
757 RECORD(EXPR_CXX_BIND_TEMPORARY);
758 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
759 RECORD(EXPR_CXX_NEW);
760 RECORD(EXPR_CXX_DELETE);
761 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
762 RECORD(EXPR_EXPR_WITH_CLEANUPS);
763 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
764 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
765 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
766 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
767 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
768 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
769 RECORD(EXPR_CXX_NOEXCEPT);
770 RECORD(EXPR_OPAQUE_VALUE);
771 RECORD(EXPR_BINARY_TYPE_TRAIT);
772 RECORD(EXPR_PACK_EXPANSION);
773 RECORD(EXPR_SIZEOF_PACK);
774 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000775 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000776#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000777}
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Sebastian Redla4232eb2010-08-18 23:56:21 +0000779void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000780 RecordData Record;
781 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000783#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
784#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000786 // Control Block.
787 BLOCK(CONTROL_BLOCK);
788 RECORD(METADATA);
789 RECORD(IMPORTS);
790 RECORD(LANGUAGE_OPTIONS);
791 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000792 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000793 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000794 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000795 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000796 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000797 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000798 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000799 RECORD(PREPROCESSOR_OPTIONS);
800
Douglas Gregorc337fef2012-10-19 00:45:00 +0000801 BLOCK(INPUT_FILES_BLOCK);
802 RECORD(INPUT_FILE);
803
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000804 // AST Top-Level Block.
805 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000806 RECORD(TYPE_OFFSET);
807 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000808 RECORD(IDENTIFIER_OFFSET);
809 RECORD(IDENTIFIER_TABLE);
810 RECORD(EXTERNAL_DEFINITIONS);
811 RECORD(SPECIAL_TYPES);
812 RECORD(STATISTICS);
813 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000814 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000815 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000816 RECORD(SELECTOR_OFFSETS);
817 RECORD(METHOD_POOL);
818 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000819 RECORD(SOURCE_LOCATION_OFFSETS);
820 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000821 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000822 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000823 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000824 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000825 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000826 RECORD(SEMA_DECL_REFS);
827 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
828 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
829 RECORD(DECL_REPLACEMENTS);
830 RECORD(UPDATE_VISIBLE);
831 RECORD(DECL_UPDATE_OFFSETS);
832 RECORD(DECL_UPDATES);
833 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
834 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000835 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000836 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000837 RECORD(FP_PRAGMA_OPTIONS);
838 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000839 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000840 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000841 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000842 RECORD(MODULE_OFFSET_MAP);
843 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000844 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000845 RECORD(FILE_SORTED_DECLS);
846 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000847 RECORD(MERGED_DECLARATIONS);
848 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000849 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000850 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000851 RECORD(MACRO_TABLE);
Richard Smithac32d902013-08-07 21:41:30 +0000852 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000853
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000854 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000855 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000856 RECORD(SM_SLOC_FILE_ENTRY);
857 RECORD(SM_SLOC_BUFFER_ENTRY);
858 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000859 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000861 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000862 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000863 RECORD(PP_MACRO_OBJECT_LIKE);
864 RECORD(PP_MACRO_FUNCTION_LIKE);
865 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000866
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000867 // Decls and Types block.
868 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000869 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000870 RECORD(TYPE_COMPLEX);
871 RECORD(TYPE_POINTER);
872 RECORD(TYPE_BLOCK_POINTER);
873 RECORD(TYPE_LVALUE_REFERENCE);
874 RECORD(TYPE_RVALUE_REFERENCE);
875 RECORD(TYPE_MEMBER_POINTER);
876 RECORD(TYPE_CONSTANT_ARRAY);
877 RECORD(TYPE_INCOMPLETE_ARRAY);
878 RECORD(TYPE_VARIABLE_ARRAY);
879 RECORD(TYPE_VECTOR);
880 RECORD(TYPE_EXT_VECTOR);
881 RECORD(TYPE_FUNCTION_PROTO);
882 RECORD(TYPE_FUNCTION_NO_PROTO);
883 RECORD(TYPE_TYPEDEF);
884 RECORD(TYPE_TYPEOF_EXPR);
885 RECORD(TYPE_TYPEOF);
886 RECORD(TYPE_RECORD);
887 RECORD(TYPE_ENUM);
888 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000889 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000890 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000891 RECORD(TYPE_DECLTYPE);
892 RECORD(TYPE_ELABORATED);
893 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
894 RECORD(TYPE_UNRESOLVED_USING);
895 RECORD(TYPE_INJECTED_CLASS_NAME);
896 RECORD(TYPE_OBJC_OBJECT);
897 RECORD(TYPE_TEMPLATE_TYPE_PARM);
898 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
899 RECORD(TYPE_DEPENDENT_NAME);
900 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
901 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
902 RECORD(TYPE_PAREN);
903 RECORD(TYPE_PACK_EXPANSION);
904 RECORD(TYPE_ATTRIBUTED);
905 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000906 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000907 RECORD(DECL_TYPEDEF);
908 RECORD(DECL_ENUM);
909 RECORD(DECL_RECORD);
910 RECORD(DECL_ENUM_CONSTANT);
911 RECORD(DECL_FUNCTION);
912 RECORD(DECL_OBJC_METHOD);
913 RECORD(DECL_OBJC_INTERFACE);
914 RECORD(DECL_OBJC_PROTOCOL);
915 RECORD(DECL_OBJC_IVAR);
916 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000917 RECORD(DECL_OBJC_CATEGORY);
918 RECORD(DECL_OBJC_CATEGORY_IMPL);
919 RECORD(DECL_OBJC_IMPLEMENTATION);
920 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
921 RECORD(DECL_OBJC_PROPERTY);
922 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000923 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000924 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000925 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000926 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000927 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000928 RECORD(DECL_FILE_SCOPE_ASM);
929 RECORD(DECL_BLOCK);
930 RECORD(DECL_CONTEXT_LEXICAL);
931 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000932 RECORD(DECL_NAMESPACE);
933 RECORD(DECL_NAMESPACE_ALIAS);
934 RECORD(DECL_USING);
935 RECORD(DECL_USING_SHADOW);
936 RECORD(DECL_USING_DIRECTIVE);
937 RECORD(DECL_UNRESOLVED_USING_VALUE);
938 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
939 RECORD(DECL_LINKAGE_SPEC);
940 RECORD(DECL_CXX_RECORD);
941 RECORD(DECL_CXX_METHOD);
942 RECORD(DECL_CXX_CONSTRUCTOR);
943 RECORD(DECL_CXX_DESTRUCTOR);
944 RECORD(DECL_CXX_CONVERSION);
945 RECORD(DECL_ACCESS_SPEC);
946 RECORD(DECL_FRIEND);
947 RECORD(DECL_FRIEND_TEMPLATE);
948 RECORD(DECL_CLASS_TEMPLATE);
949 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
950 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufoef4579c2013-08-06 01:03:05 +0000951 RECORD(DECL_VAR_TEMPLATE);
952 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
953 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000954 RECORD(DECL_FUNCTION_TEMPLATE);
955 RECORD(DECL_TEMPLATE_TYPE_PARM);
956 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
957 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
958 RECORD(DECL_STATIC_ASSERT);
959 RECORD(DECL_CXX_BASE_SPECIFIERS);
960 RECORD(DECL_INDIRECTFIELD);
961 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
962
Douglas Gregora72d8c42011-06-03 02:27:19 +0000963 // Statements and Exprs can occur in the Decls and Types block.
964 AddStmtsExprs(Stream, Record);
965
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000966 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000967 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000968 RECORD(PPD_MACRO_DEFINITION);
969 RECORD(PPD_INCLUSION_DIRECTIVE);
970
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000971#undef RECORD
972#undef BLOCK
973 Stream.ExitBlock();
974}
975
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976/// \brief Adjusts the given filename to only write out the portion of the
977/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000978///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979/// \param Filename the file name to adjust.
980///
981/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
982/// the returned filename will be adjusted by this system root.
983///
984/// \returns either the original filename (if it needs no adjustment) or the
985/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000986static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000987adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregor832d6202011-07-22 16:35:34 +0000990 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregore650c8c2009-07-07 00:12:59 +0000993 // Verify that the filename and the system root have the same prefix.
994 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000995 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000996 if (Filename[Pos] != isysroot[Pos])
997 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Douglas Gregore650c8c2009-07-07 00:12:59 +0000999 // We hit the end of the filename before we hit the end of the system root.
1000 if (!Filename[Pos])
1001 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Douglas Gregore650c8c2009-07-07 00:12:59 +00001003 // If the file name has a '/' at the current position, skip over the '/'.
1004 // We distinguish sysroot-based includes from absolute includes by the
1005 // absence of '/' at the beginning of sysroot-based includes.
1006 if (Filename[Pos] == '/')
1007 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Douglas Gregore650c8c2009-07-07 00:12:59 +00001009 return Filename + Pos;
1010}
Chris Lattnerb145b1e2009-04-26 22:26:21 +00001011
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001012/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001013void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1014 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001015 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001016 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001017 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1018 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001019
Douglas Gregore650c8c2009-07-07 00:12:59 +00001020 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001021 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1022 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1023 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1024 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1025 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1026 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1027 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1028 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1029 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1030 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1031 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001032 Record.push_back(VERSION_MAJOR);
1033 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001034 Record.push_back(CLANG_VERSION_MAJOR);
1035 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001036 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001037 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001038 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1039 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001040
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001041 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001042 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001043 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001044 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001045 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001046
1047 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1048 M != MEnd; ++M) {
1049 // Skip modules that weren't directly imported.
1050 if (!(*M)->isDirectlyImported())
1051 continue;
1052
1053 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001054 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001055 Record.push_back((*M)->File->getSize());
1056 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001057 // FIXME: This writes the absolute path for AST files we depend on.
1058 const std::string &FileName = (*M)->FileName;
1059 Record.push_back(FileName.size());
1060 Record.append(FileName.begin(), FileName.end());
1061 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001062 Stream.EmitRecord(IMPORTS, Record);
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001065 // Language options.
1066 Record.clear();
1067 const LangOptions &LangOpts = Context.getLangOpts();
1068#define LANGOPT(Name, Bits, Default, Description) \
1069 Record.push_back(LangOpts.Name);
1070#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1071 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1072#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001073#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1074#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001075
1076 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1077 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1078
1079 Record.push_back(LangOpts.CurrentModule.size());
1080 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001081
1082 // Comment options.
1083 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1084 for (CommentOptions::BlockCommandNamesTy::const_iterator
1085 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1086 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1087 I != IEnd; ++I) {
1088 AddString(*I, Record);
1089 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001090 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001091
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001092 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1093
Douglas Gregoree097c12012-10-18 17:58:09 +00001094 // Target options.
1095 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001096 const TargetInfo &Target = Context.getTargetInfo();
1097 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001098 AddString(TargetOpts.Triple, Record);
1099 AddString(TargetOpts.CPU, Record);
1100 AddString(TargetOpts.ABI, Record);
1101 AddString(TargetOpts.CXXABI, Record);
1102 AddString(TargetOpts.LinkerVersion, Record);
1103 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1104 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1105 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1106 }
1107 Record.push_back(TargetOpts.Features.size());
1108 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1109 AddString(TargetOpts.Features[I], Record);
1110 }
1111 Stream.EmitRecord(TARGET_OPTIONS, Record);
1112
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001113 // Diagnostic options.
1114 Record.clear();
1115 const DiagnosticOptions &DiagOpts
1116 = Context.getDiagnostics().getDiagnosticOptions();
1117#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1118#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1119 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1120#include "clang/Basic/DiagnosticOptions.def"
1121 Record.push_back(DiagOpts.Warnings.size());
1122 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1123 AddString(DiagOpts.Warnings[I], Record);
1124 // Note: we don't serialize the log or serialization file names, because they
1125 // are generally transient files and will almost always be overridden.
1126 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1127
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001128 // File system options.
1129 Record.clear();
1130 const FileSystemOptions &FSOpts
1131 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1132 AddString(FSOpts.WorkingDir, Record);
1133 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1134
Douglas Gregorbbf38312012-10-24 16:50:34 +00001135 // Header search options.
1136 Record.clear();
1137 const HeaderSearchOptions &HSOpts
1138 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1139 AddString(HSOpts.Sysroot, Record);
1140
1141 // Include entries.
1142 Record.push_back(HSOpts.UserEntries.size());
1143 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1144 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1145 AddString(Entry.Path, Record);
1146 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001147 Record.push_back(Entry.IsFramework);
1148 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001149 }
1150
1151 // System header prefixes.
1152 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1153 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1154 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1155 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1156 }
1157
1158 AddString(HSOpts.ResourceDir, Record);
1159 AddString(HSOpts.ModuleCachePath, Record);
1160 Record.push_back(HSOpts.DisableModuleHash);
1161 Record.push_back(HSOpts.UseBuiltinIncludes);
1162 Record.push_back(HSOpts.UseStandardSystemIncludes);
1163 Record.push_back(HSOpts.UseStandardCXXIncludes);
1164 Record.push_back(HSOpts.UseLibcxx);
1165 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1166
Douglas Gregora71a7d82012-10-24 20:05:57 +00001167 // Preprocessor options.
1168 Record.clear();
1169 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1170
1171 // Macro definitions.
1172 Record.push_back(PPOpts.Macros.size());
1173 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1174 AddString(PPOpts.Macros[I].first, Record);
1175 Record.push_back(PPOpts.Macros[I].second);
1176 }
1177
1178 // Includes
1179 Record.push_back(PPOpts.Includes.size());
1180 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1181 AddString(PPOpts.Includes[I], Record);
1182
1183 // Macro includes
1184 Record.push_back(PPOpts.MacroIncludes.size());
1185 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1186 AddString(PPOpts.MacroIncludes[I], Record);
1187
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001188 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001189 // Detailed record is important since it is used for the module cache hash.
1190 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001191 AddString(PPOpts.ImplicitPCHInclude, Record);
1192 AddString(PPOpts.ImplicitPTHInclude, Record);
1193 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1194 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1195
Douglas Gregor31d375f2011-05-06 21:43:30 +00001196 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001197 SourceManager &SM = Context.getSourceManager();
1198 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1199 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001200 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1201 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001202 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1203 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1204
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001205 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001207 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001208
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001209 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001210 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001211 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001212 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001213 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001214 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001215 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001216 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001217
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001218 Record.clear();
1219 Record.push_back(SM.getMainFileID().getOpaqueValue());
1220 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1221
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001222 // Original PCH directory
1223 if (!OutputFile.empty() && OutputFile != "-") {
1224 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1225 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1227 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1228
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001229 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001230
1231 llvm::sys::fs::make_absolute(OutputPath);
1232 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1233
1234 RecordData Record;
1235 Record.push_back(ORIGINAL_PCH_DIR);
1236 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1237 }
1238
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001239 WriteInputFiles(Context.SourceMgr,
1240 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregorb22d1942013-07-22 20:48:33 +00001241 isysroot,
1242 PP.getLangOpts().Modules);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001243 Stream.ExitBlock();
1244}
1245
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001246namespace {
1247 /// \brief An input file.
1248 struct InputFileEntry {
1249 const FileEntry *File;
1250 bool IsSystemFile;
1251 bool BufferOverridden;
1252 };
1253}
1254
1255void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1256 HeaderSearchOptions &HSOpts,
Douglas Gregorb22d1942013-07-22 20:48:33 +00001257 StringRef isysroot,
1258 bool Modules) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001259 using namespace llvm;
1260 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1261 RecordData Record;
1262
1263 // Create input-file abbreviation.
1264 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1265 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001266 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001267 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1268 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001269 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001270 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1271 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1272
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001273 // Get all ContentCache objects for files, sorted by whether the file is a
1274 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001275 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001276 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1277 // Get this source location entry.
1278 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001279 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001280
1281 // We only care about file entries that were not overridden.
1282 if (!SLoc->isFile())
1283 continue;
1284 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001285 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001286 continue;
1287
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001288 InputFileEntry Entry;
1289 Entry.File = Cache->OrigEntry;
1290 Entry.IsSystemFile = Cache->IsSystemFile;
1291 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001292 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001293 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001294 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001295 SortedFiles.push_front(Entry);
1296 }
1297
1298 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1299 // the set of (non-system) input files. This is simple heuristic for
1300 // detecting whether the system headers may have changed, because it is too
1301 // expensive to stat() all of the system headers.
Richard Smithcc8e22b2013-05-20 23:40:27 +00001302 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001303 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001304 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1305 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1306 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1307 InputFileEntry Entry = { SDKSettingsFile, false, false };
1308 SortedFiles.push_front(Entry);
1309 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001310 }
1311
Douglas Gregorb22d1942013-07-22 20:48:33 +00001312 // Add the compiler's own module.map in the set of (non-system) input files.
1313 // This is a simple heuristic for detecting whether the compiler's headers
1314 // have changed, because we don't want to stat() all of them.
1315 if (Modules && !Chain) {
1316 SmallString<128> P = StringRef(HSOpts.ResourceDir);
1317 llvm::sys::path::append(P, "include");
1318 llvm::sys::path::append(P, "module.map");
1319 if (const FileEntry *ModuleMapFile = FileMgr.getFile(P)) {
1320 InputFileEntry Entry = { ModuleMapFile, false, false };
1321 SortedFiles.push_front(Entry);
1322 }
1323 }
1324
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001325 unsigned UserFilesNum = 0;
1326 // Write out all of the input files.
1327 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001328 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001329 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001330 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001331
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001332 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001333 if (InputFileID != 0)
1334 continue; // already recorded this file.
1335
Douglas Gregora930dc92012-10-22 18:42:04 +00001336 // Record this entry's offset.
1337 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001338
1339 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001340
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001341 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001342 ++UserFilesNum;
1343
Douglas Gregor745e6f12012-10-19 00:38:02 +00001344 Record.clear();
1345 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001346 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001347
1348 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001349 Record.push_back(Entry.File->getSize());
1350 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001351
Douglas Gregora930dc92012-10-22 18:42:04 +00001352 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001353 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001354
Douglas Gregor745e6f12012-10-19 00:38:02 +00001355 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001356 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001357 SmallString<128> FilePath(Filename);
1358
1359 // Ask the file manager to fixup the relative path for us. This will
1360 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001361 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001362
1363 // FIXME: This call to make_absolute shouldn't be necessary, the
1364 // call to FixupRelativePath should always return an absolute path.
1365 llvm::sys::fs::make_absolute(FilePath);
1366 Filename = FilePath.c_str();
1367
1368 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1369
1370 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1371 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001372
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001373 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001374
1375 // Create input file offsets abbreviation.
1376 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1377 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1378 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001379 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1380 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001381 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1382 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1383
1384 // Write input file offsets.
1385 Record.clear();
1386 Record.push_back(INPUT_FILE_OFFSETS);
1387 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001388 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001389 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001390}
1391
Douglas Gregor14f79002009-04-10 03:52:48 +00001392//===----------------------------------------------------------------------===//
1393// Source Manager Serialization
1394//===----------------------------------------------------------------------===//
1395
1396/// \brief Create an abbreviation for the SLocEntry that refers to a
1397/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001398static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001399 using namespace llvm;
1400 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001401 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001406 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001411 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001412}
1413
1414/// \brief Create an abbreviation for the SLocEntry that refers to a
1415/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001416static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001417 using namespace llvm;
1418 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001419 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001425 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001426}
1427
1428/// \brief Create an abbreviation for the SLocEntry that refers to a
1429/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001430static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001431 using namespace llvm;
1432 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001433 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001434 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001435 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001436}
1437
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001438/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1439/// expansion.
1440static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001441 using namespace llvm;
1442 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001443 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001449 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001450}
1451
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001452namespace {
1453 // Trait used for the on-disk hash table of header search information.
1454 class HeaderFileInfoTrait {
1455 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001456 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001457
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001458 // Keep track of the framework names we've used during serialization.
1459 SmallVector<char, 128> FrameworkStringData;
1460 llvm::StringMap<unsigned> FrameworkNameOffset;
1461
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001462 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001463 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1464 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001465
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001466 struct key_type {
1467 const FileEntry *FE;
1468 const char *Filename;
1469 };
1470 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001471
1472 typedef HeaderFileInfo data_type;
1473 typedef const data_type &data_type_ref;
1474
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001475 static unsigned ComputeHash(key_type_ref key) {
1476 // The hash is based only on size/time of the file, so that the reader can
1477 // match even when symlinking or excess path elements ("foo/../", "../")
1478 // change the form of the name. However, complete path is still the key.
1479 return llvm::hash_combine(key.FE->getSize(),
1480 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001481 }
1482
1483 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001484 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1485 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1486 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001487 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001488 if (Data.isModuleHeader)
1489 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001490 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001491 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001492 }
1493
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001494 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1495 clang::io::Emit64(Out, key.FE->getSize());
1496 KeyLen -= 8;
1497 clang::io::Emit64(Out, key.FE->getModificationTime());
1498 KeyLen -= 8;
1499 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001500 }
1501
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001502 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001503 data_type_ref Data, unsigned DataLen) {
1504 using namespace clang::io;
1505 uint64_t Start = Out.tell(); (void)Start;
1506
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001507 unsigned char Flags = (Data.HeaderRole << 6)
1508 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001509 | (Data.isPragmaOnce << 4)
1510 | (Data.DirInfo << 2)
1511 | (Data.Resolved << 1)
1512 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001513 Emit8(Out, (uint8_t)Flags);
1514 Emit16(Out, (uint16_t) Data.NumIncludes);
1515
1516 if (!Data.ControllingMacro)
1517 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1518 else
1519 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001520
1521 unsigned Offset = 0;
1522 if (!Data.Framework.empty()) {
1523 // If this header refers into a framework, save the framework name.
1524 llvm::StringMap<unsigned>::iterator Pos
1525 = FrameworkNameOffset.find(Data.Framework);
1526 if (Pos == FrameworkNameOffset.end()) {
1527 Offset = FrameworkStringData.size() + 1;
1528 FrameworkStringData.append(Data.Framework.begin(),
1529 Data.Framework.end());
1530 FrameworkStringData.push_back(0);
1531
1532 FrameworkNameOffset[Data.Framework] = Offset;
1533 } else
1534 Offset = Pos->second;
1535 }
1536 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001537
1538 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001539 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001540 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1541 }
1542
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001543 assert(Out.tell() - Start == DataLen && "Wrong data length");
1544 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001545
1546 const char *strings_begin() const { return FrameworkStringData.begin(); }
1547 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001548 };
1549} // end anonymous namespace
1550
1551/// \brief Write the header search block for the list of files that
1552///
1553/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001554void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001555 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001556 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1557
1558 if (FilesByUID.size() > HS.header_file_size())
1559 FilesByUID.resize(HS.header_file_size());
1560
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001561 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001562 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001563 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001564 unsigned NumHeaderSearchEntries = 0;
1565 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1566 const FileEntry *File = FilesByUID[UID];
1567 if (!File)
1568 continue;
1569
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001570 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1571 // from the external source if it was not provided already.
1572 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001573 if (HFI.External && Chain)
1574 continue;
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001575 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1576 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001577
1578 // Turn the file name into an absolute path, if it isn't already.
1579 const char *Filename = File->getName();
1580 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1581
1582 // If we performed any translation on the file name at all, we need to
1583 // save this string, since the generator will refer to it later.
1584 if (Filename != File->getName()) {
1585 Filename = strdup(Filename);
1586 SavedStrings.push_back(Filename);
1587 }
1588
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001589 HeaderFileInfoTrait::key_type key = { File, Filename };
1590 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001591 ++NumHeaderSearchEntries;
1592 }
1593
1594 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001595 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001596 uint32_t BucketOffset;
1597 {
1598 llvm::raw_svector_ostream Out(TableData);
1599 // Make sure that no bucket is at offset 0
1600 clang::io::Emit32(Out, 0);
1601 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1602 }
1603
1604 // Create a blob abbreviation
1605 using namespace llvm;
1606 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1607 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001610 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1612 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1613
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001614 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001615 RecordData Record;
1616 Record.push_back(HEADER_SEARCH_TABLE);
1617 Record.push_back(BucketOffset);
1618 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001619 Record.push_back(TableData.size());
1620 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001621 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1622
1623 // Free all of the strings we had to duplicate.
1624 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001625 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001626}
1627
Douglas Gregor14f79002009-04-10 03:52:48 +00001628/// \brief Writes the block containing the serialized form of the
1629/// source manager.
1630///
1631/// TODO: We should probably use an on-disk hash table (stored in a
1632/// blob), indexed based on the file name, so that we only create
1633/// entries for files that we actually need. In the common case (no
1634/// errors), we probably won't have to create file entries for any of
1635/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001636void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001637 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001638 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001639 RecordData Record;
1640
Chris Lattnerf04ad692009-04-10 17:16:57 +00001641 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001642 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001643
1644 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001645 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1646 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1647 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001648 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001649
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001650 // Write out the source location entry table. We skip the first
1651 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001652 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001653 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001654 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1655 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001656 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001657 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001658 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001659 FileID FID = FileID::get(I);
1660 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001661
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001662 // Record the offset of this source-location entry.
1663 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1664
1665 // Figure out which record code to use.
1666 unsigned Code;
1667 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001668 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1669 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001670 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001671 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001672 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001673 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001674 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001675 Record.clear();
1676 Record.push_back(Code);
1677
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001678 // Starting offset of this entry within this module, so skip the dummy.
1679 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001680 if (SLoc->isFile()) {
1681 const SrcMgr::FileInfo &File = SLoc->getFile();
1682 Record.push_back(File.getIncludeLoc().getRawEncoding());
1683 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1684 Record.push_back(File.hasLineDirectives());
1685
1686 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001687 if (Content->OrigEntry) {
1688 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001689 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001690
Douglas Gregora930dc92012-10-22 18:42:04 +00001691 // The source location entry is a file. Emit input file ID.
1692 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1693 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001695 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001696
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001697 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001698 if (FDI != FileDeclIDs.end()) {
1699 Record.push_back(FDI->second->FirstDeclIndex);
1700 Record.push_back(FDI->second->DeclIDs.size());
1701 } else {
1702 Record.push_back(0);
1703 Record.push_back(0);
1704 }
Douglas Gregora081da52011-11-16 20:05:18 +00001705
Douglas Gregora930dc92012-10-22 18:42:04 +00001706 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001707
1708 if (Content->BufferOverridden) {
1709 Record.clear();
1710 Record.push_back(SM_SLOC_BUFFER_BLOB);
1711 const llvm::MemoryBuffer *Buffer
1712 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1713 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1714 StringRef(Buffer->getBufferStart(),
1715 Buffer->getBufferSize() + 1));
1716 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001717 } else {
1718 // The source location entry is a buffer. The blob associated
1719 // with this entry contains the contents of the buffer.
1720
1721 // We add one to the size so that we capture the trailing NULL
1722 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1723 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001724 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001725 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001726 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001727 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001728 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001729 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001730 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001731 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001732 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001733 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001734
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001735 if (strcmp(Name, "<built-in>") == 0) {
1736 PreloadSLocs.push_back(SLocEntryOffsets.size());
1737 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001738 }
1739 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001740 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001741 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001742 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1743 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001744 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1745 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001746
1747 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001748 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001749 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001750 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001751 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001752 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001753 }
1754 }
1755
Douglas Gregorc9490c02009-04-16 22:23:12 +00001756 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001757
1758 if (SLocEntryOffsets.empty())
1759 return;
1760
Sebastian Redl3397c552010-08-18 23:56:27 +00001761 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001762 // table is used for lazily loading source-location information.
1763 using namespace llvm;
1764 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001765 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1769 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001771 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001772 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001773 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001774 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001775 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001776
Sebastian Redl3397c552010-08-18 23:56:27 +00001777 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001778 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001779 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001780
1781 // Write the line table. It depends on remapping working, so it must come
1782 // after the source location offsets.
1783 if (SourceMgr.hasLineTable()) {
1784 LineTableInfo &LineTable = SourceMgr.getLineTable();
1785
1786 Record.clear();
1787 // Emit the file names
1788 Record.push_back(LineTable.getNumFilenames());
1789 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1790 // Emit the file name
1791 const char *Filename = LineTable.getFilename(I);
1792 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1793 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1794 Record.push_back(FilenameLen);
1795 if (FilenameLen)
1796 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1797 }
1798
1799 // Emit the line entries
1800 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1801 L != LEnd; ++L) {
1802 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001803 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001804 continue;
1805
1806 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001807 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001808
1809 // Emit the line entries
1810 Record.push_back(L->second.size());
1811 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1812 LEEnd = L->second.end();
1813 LE != LEEnd; ++LE) {
1814 Record.push_back(LE->FileOffset);
1815 Record.push_back(LE->LineNo);
1816 Record.push_back(LE->FilenameID);
1817 Record.push_back((unsigned)LE->FileKind);
1818 Record.push_back(LE->IncludeOffset);
1819 }
1820 }
1821 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1822 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001823}
1824
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001825//===----------------------------------------------------------------------===//
1826// Preprocessor Serialization
1827//===----------------------------------------------------------------------===//
1828
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001829namespace {
1830class ASTMacroTableTrait {
1831public:
1832 typedef IdentID key_type;
1833 typedef key_type key_type_ref;
1834
1835 struct Data {
1836 uint32_t MacroDirectivesOffset;
1837 };
1838
1839 typedef Data data_type;
1840 typedef const data_type &data_type_ref;
1841
1842 static unsigned ComputeHash(IdentID IdID) {
1843 return llvm::hash_value(IdID);
1844 }
1845
1846 std::pair<unsigned,unsigned>
1847 static EmitKeyDataLength(raw_ostream& Out,
1848 key_type_ref Key, data_type_ref Data) {
1849 unsigned KeyLen = 4; // IdentID.
1850 unsigned DataLen = 4; // MacroDirectivesOffset.
1851 return std::make_pair(KeyLen, DataLen);
1852 }
1853
1854 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1855 clang::io::Emit32(Out, Key);
1856 }
1857
1858 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1859 unsigned) {
1860 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1861 }
1862};
1863} // end anonymous namespace
1864
1865static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1866 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1867 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1868 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1869 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001870 return X.first->getName().compare(Y.first->getName());
1871}
1872
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001873static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1874 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001875 if (MacroInfo *MI = MD->getMacroInfo())
1876 if (MI->isBuiltinMacro())
1877 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001878
1879 if (IsModule) {
1880 SourceLocation Loc = MD->getLocation();
1881 if (Loc.isInvalid())
1882 return true;
1883 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1884 return true;
1885 }
1886
1887 return false;
1888}
1889
Chris Lattner0b1fb982009-04-10 17:15:23 +00001890/// \brief Writes the block containing the serialized form of the
1891/// preprocessor.
1892///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001893void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001894 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1895 if (PPRec)
1896 WritePreprocessorDetail(*PPRec);
1897
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001898 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001899
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001900 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1901 if (PP.getCounterValue() != 0) {
1902 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001903 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001904 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001905 }
1906
1907 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001908 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Sebastian Redl3397c552010-08-18 23:56:27 +00001910 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001911 // FIXME: use diagnostics subsystem for localization etc.
1912 if (PP.SawDateOrTime())
1913 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Douglas Gregorecdcb882010-10-20 22:00:55 +00001915
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001916 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001917 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001918
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001919 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001920 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001921 MacroDirectives;
1922 for (Preprocessor::macro_iterator
1923 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1924 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001925 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001926 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001927 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001928
Douglas Gregor9c736102011-02-10 18:20:09 +00001929 // Sort the set of macro definitions that need to be serialized by the
1930 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001931 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1932 &compareMacroDirectives);
1933
1934 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1935
1936 // Emit the macro directives as a list and associate the offset with the
1937 // identifier they belong to.
1938 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1939 const IdentifierInfo *Name = MacroDirectives[I].first;
1940 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1941 MacroDirective *MD = MacroDirectives[I].second;
1942
1943 // If the macro or identifier need no updates, don't write the macro history
1944 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001945 // FIXME: Chain the macro history instead of re-writing it.
1946 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001947 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1948 continue;
1949
1950 // Emit the macro directives in reverse source order.
1951 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001952 if (MD->isHidden())
1953 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001954 if (shouldIgnoreMacro(MD, IsModule, PP))
1955 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001956
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001957 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001958 Record.push_back(MD->getKind());
1959 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1960 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1961 Record.push_back(InfoID);
1962 Record.push_back(DefMD->isImported());
1963 Record.push_back(DefMD->isAmbiguous());
1964
1965 } else if (VisibilityMacroDirective *
1966 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1967 Record.push_back(VisMD->isPublic());
1968 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001969 }
1970 if (Record.empty())
1971 continue;
1972
1973 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1974 Record.clear();
1975
1976 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1977
1978 IdentID NameID = getIdentifierRef(Name);
1979 ASTMacroTableTrait::Data data;
1980 data.MacroDirectivesOffset = MacroDirectiveOffset;
1981 Generator.insert(NameID, data);
1982 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001983
Douglas Gregora8235d62012-10-09 23:05:51 +00001984 /// \brief Offsets of each of the macros into the bitstream, indexed by
1985 /// the local macro ID
1986 ///
1987 /// For each identifier that is associated with a macro, this map
1988 /// provides the offset into the bitstream where that macro is
1989 /// defined.
1990 std::vector<uint32_t> MacroOffsets;
1991
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001992 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1993 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1994 MacroInfo *MI = MacroInfosToEmit[I].MI;
1995 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001996
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001997 if (ID < FirstMacroID) {
1998 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1999 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00002000 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002001
2002 // Record the local offset of this macro.
2003 unsigned Index = ID - FirstMacroID;
2004 if (Index == MacroOffsets.size())
2005 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2006 else {
2007 if (Index > MacroOffsets.size())
2008 MacroOffsets.resize(Index + 1);
2009
2010 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2011 }
2012
2013 AddIdentifierRef(Name, Record);
2014 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2015 AddSourceLocation(MI->getDefinitionLoc(), Record);
2016 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2017 Record.push_back(MI->isUsed());
2018 unsigned Code;
2019 if (MI->isObjectLike()) {
2020 Code = PP_MACRO_OBJECT_LIKE;
2021 } else {
2022 Code = PP_MACRO_FUNCTION_LIKE;
2023
2024 Record.push_back(MI->isC99Varargs());
2025 Record.push_back(MI->isGNUVarargs());
2026 Record.push_back(MI->hasCommaPasting());
2027 Record.push_back(MI->getNumArgs());
2028 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2029 I != E; ++I)
2030 AddIdentifierRef(*I, Record);
2031 }
2032
2033 // If we have a detailed preprocessing record, record the macro definition
2034 // ID that corresponds to this macro.
2035 if (PPRec)
2036 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2037
2038 Stream.EmitRecord(Code, Record);
2039 Record.clear();
2040
2041 // Emit the tokens array.
2042 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2043 // Note that we know that the preprocessor does not have any annotation
2044 // tokens in it because they are created by the parser, and thus can't
2045 // be in a macro definition.
2046 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002047 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002048 Stream.EmitRecord(PP_TOKEN, Record);
2049 Record.clear();
2050 }
2051 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002052 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002053
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002054 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002055
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002056 // Create the on-disk hash table in a buffer.
2057 SmallString<4096> MacroTable;
2058 uint32_t BucketOffset;
2059 {
2060 llvm::raw_svector_ostream Out(MacroTable);
2061 // Make sure that no bucket is at offset 0
2062 clang::io::Emit32(Out, 0);
2063 BucketOffset = Generator.Emit(Out);
2064 }
2065
2066 // Write the macro table
2067 using namespace llvm;
2068 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2069 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2072 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2073
2074 Record.push_back(MACRO_TABLE);
2075 Record.push_back(BucketOffset);
2076 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2077 Record.clear();
2078
Douglas Gregora8235d62012-10-09 23:05:51 +00002079 // Write the offsets table for macro IDs.
2080 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002081 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002082 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2086
2087 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2088 Record.clear();
2089 Record.push_back(MACRO_OFFSET);
2090 Record.push_back(MacroOffsets.size());
2091 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2092 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2093 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002094}
2095
2096void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002097 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002098 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002099
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002100 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002101
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002102 // Enter the preprocessor block.
2103 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002104
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002105 // If the preprocessor has a preprocessing record, emit it.
2106 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002107 using namespace llvm;
2108
2109 // Set up the abbreviation for
2110 unsigned InclusionAbbrev = 0;
2111 {
2112 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2113 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2119 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2120 }
2121
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002122 unsigned FirstPreprocessorEntityID
2123 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2124 + NUM_PREDEF_PP_ENTITY_IDS;
2125 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002126 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002127 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2128 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002129 E != EEnd;
2130 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002131 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002132
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002133 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2134 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002135
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002136 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002137 // Record this macro definition's ID.
2138 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002139
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002140 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002141 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2142 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002143 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002144
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002145 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002146 Record.push_back(ME->isBuiltinMacro());
2147 if (ME->isBuiltinMacro())
2148 AddIdentifierRef(ME->getName(), Record);
2149 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002150 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002151 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002152 continue;
2153 }
2154
2155 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2156 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002157 Record.push_back(ID->getFileName().size());
2158 Record.push_back(ID->wasInQuotes());
2159 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002160 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002161 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002162 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002163 // Check that the FileEntry is not null because it was not resolved and
2164 // we create a PCH even with compiler errors.
2165 if (ID->getFile())
2166 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002167 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2168 continue;
2169 }
2170
2171 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2172 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002173 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002174
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002175 // Write the offsets table for the preprocessing record.
2176 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002177 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2178
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002179 // Write the offsets table for identifier IDs.
2180 using namespace llvm;
2181 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002182 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002185 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002186
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002187 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002188 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002189 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002190 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2191 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002192 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002193}
2194
Douglas Gregore209e502011-12-06 01:10:29 +00002195unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2196 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2197 if (Known != SubmoduleIDs.end())
2198 return Known->second;
2199
2200 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2201}
2202
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002203unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2204 if (!Mod)
2205 return 0;
2206
2207 llvm::DenseMap<Module *, unsigned>::const_iterator
2208 Known = SubmoduleIDs.find(Mod);
2209 if (Known != SubmoduleIDs.end())
2210 return Known->second;
2211
2212 return 0;
2213}
2214
Douglas Gregor26ced122011-12-01 00:59:36 +00002215/// \brief Compute the number of modules within the given tree (including the
2216/// given module).
2217static unsigned getNumberOfModules(Module *Mod) {
2218 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002219 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2220 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002221 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002222 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002223
2224 return ChildModules + 1;
2225}
2226
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002227void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002228 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002229 // FIXME: This feels like it belongs somewhere else, but there are no
2230 // other consumers of this information.
2231 SourceManager &SrcMgr = PP->getSourceManager();
2232 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2233 for (ASTContext::import_iterator I = Context->local_import_begin(),
2234 IEnd = Context->local_import_end();
2235 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002236 if (Module *ImportedFrom
2237 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2238 SrcMgr))) {
2239 ImportedFrom->Imports.push_back(I->getImportedModule());
2240 }
2241 }
2242
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002243 // Enter the submodule description block.
2244 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2245
2246 // Write the abbreviations needed for the submodules block.
2247 using namespace llvm;
2248 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2249 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2260 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2261
2262 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002263 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2265 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2266
2267 Abbrev = new BitCodeAbbrev();
2268 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2270 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002271
2272 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002273 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2275 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2276
2277 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002278 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2280 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2281
Douglas Gregor51f564f2011-12-31 04:05:44 +00002282 Abbrev = new BitCodeAbbrev();
2283 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2285 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2286
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002287 Abbrev = new BitCodeAbbrev();
2288 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2290 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2291
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002292 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002293 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2295 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2296
2297 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002298 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2301 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2302
Douglas Gregor63a72682013-03-20 00:22:05 +00002303 Abbrev = new BitCodeAbbrev();
2304 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2305 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2306 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2307
Douglas Gregor906d66a2013-03-20 21:10:35 +00002308 Abbrev = new BitCodeAbbrev();
2309 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2312 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2313
Douglas Gregor26ced122011-12-01 00:59:36 +00002314 // Write the submodule metadata block.
2315 RecordData Record;
2316 Record.push_back(getNumberOfModules(WritingModule));
2317 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2318 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2319
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002320 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002321 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002322 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002323 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002324 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002325 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002326 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002327
2328 // Emit the definition of the block.
2329 Record.clear();
2330 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002331 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002332 if (Mod->Parent) {
2333 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2334 Record.push_back(SubmoduleIDs[Mod->Parent]);
2335 } else {
2336 Record.push_back(0);
2337 }
2338 Record.push_back(Mod->IsFramework);
2339 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002340 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002341 Record.push_back(Mod->InferSubmodules);
2342 Record.push_back(Mod->InferExplicitSubmodules);
2343 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002344 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002345 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2346
Douglas Gregor51f564f2011-12-31 04:05:44 +00002347 // Emit the requirements.
2348 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2349 Record.clear();
2350 Record.push_back(SUBMODULE_REQUIRES);
2351 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2352 Mod->Requires[I].data(),
2353 Mod->Requires[I].size());
2354 }
2355
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002356 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002357 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002358 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002359 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002360 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002361 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002362 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2363 Record.clear();
2364 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2365 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2366 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002367 }
2368
2369 // Emit the headers.
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002370 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002371 Record.clear();
2372 Record.push_back(SUBMODULE_HEADER);
2373 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002374 Mod->NormalHeaders[I]->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002375 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002376 // Emit the excluded headers.
2377 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2378 Record.clear();
2379 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2380 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2381 Mod->ExcludedHeaders[I]->getName());
2382 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002383 // Emit the private headers.
2384 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2385 Record.clear();
2386 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2387 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2388 Mod->PrivateHeaders[I]->getName());
2389 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002390 ArrayRef<const FileEntry *>
2391 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2392 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002393 Record.clear();
2394 Record.push_back(SUBMODULE_TOPHEADER);
2395 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002396 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002397 }
Douglas Gregor55988682011-12-05 16:33:54 +00002398
2399 // Emit the imports.
2400 if (!Mod->Imports.empty()) {
2401 Record.clear();
2402 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002403 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002404 assert(ImportedID && "Unknown submodule!");
2405 Record.push_back(ImportedID);
2406 }
2407 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2408 }
2409
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002410 // Emit the exports.
2411 if (!Mod->Exports.empty()) {
2412 Record.clear();
2413 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002414 if (Module *Exported = Mod->Exports[I].getPointer()) {
2415 unsigned ExportedID = SubmoduleIDs[Exported];
2416 assert(ExportedID > 0 && "Unknown submodule ID?");
2417 Record.push_back(ExportedID);
2418 } else {
2419 Record.push_back(0);
2420 }
2421
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002422 Record.push_back(Mod->Exports[I].getInt());
2423 }
2424 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2425 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002426
2427 // Emit the link libraries.
2428 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2429 Record.clear();
2430 Record.push_back(SUBMODULE_LINK_LIBRARY);
2431 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2432 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2433 Mod->LinkLibraries[I].Library);
2434 }
2435
Douglas Gregor906d66a2013-03-20 21:10:35 +00002436 // Emit the conflicts.
2437 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2438 Record.clear();
2439 Record.push_back(SUBMODULE_CONFLICT);
2440 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2441 assert(OtherID && "Unknown submodule!");
2442 Record.push_back(OtherID);
2443 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2444 Mod->Conflicts[I].Message);
2445 }
2446
Douglas Gregor63a72682013-03-20 00:22:05 +00002447 // Emit the configuration macros.
2448 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2449 Record.clear();
2450 Record.push_back(SUBMODULE_CONFIG_MACRO);
2451 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2452 Mod->ConfigMacros[I]);
2453 }
2454
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002455 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002456 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2457 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002458 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002459 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002460 }
2461
2462 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002463
2464 assert((NextSubmoduleID - FirstSubmoduleID
2465 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002466}
2467
Douglas Gregor185dbd72011-12-01 02:07:58 +00002468serialization::SubmoduleID
2469ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002470 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002471 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002472
2473 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002474 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002475 Module *OwningMod
2476 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002477 if (!OwningMod)
2478 return 0;
2479
Douglas Gregore209e502011-12-06 01:10:29 +00002480 // Check whether this submodule is part of our own module.
2481 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002482 return 0;
2483
Douglas Gregore209e502011-12-06 01:10:29 +00002484 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002485}
2486
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002487void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2488 bool isModule) {
2489 // Make sure set diagnostic pragmas don't affect the translation unit that
2490 // imports the module.
2491 // FIXME: Make diagnostic pragma sections work properly with modules.
2492 if (isModule)
2493 return;
2494
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002495 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2496 DiagStateIDMap;
2497 unsigned CurrID = 0;
2498 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002499 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002500 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002501 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2502 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002503 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002504 if (point.Loc.isInvalid())
2505 continue;
2506
2507 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002508 unsigned &DiagStateID = DiagStateIDMap[point.State];
2509 Record.push_back(DiagStateID);
2510
2511 if (DiagStateID == 0) {
2512 DiagStateID = ++CurrID;
2513 for (DiagnosticsEngine::DiagState::const_iterator
2514 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2515 if (I->second.isPragma()) {
2516 Record.push_back(I->first);
2517 Record.push_back(I->second.getMapping());
2518 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002519 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002520 Record.push_back(-1); // mark the end of the diag/map pairs for this
2521 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002522 }
2523 }
2524
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002525 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002526 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002527}
2528
Anders Carlssonc8505782011-03-06 18:41:18 +00002529void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2530 if (CXXBaseSpecifiersOffsets.empty())
2531 return;
2532
2533 RecordData Record;
2534
2535 // Create a blob abbreviation for the C++ base specifiers offsets.
2536 using namespace llvm;
2537
2538 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2539 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2542 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2543
Douglas Gregore92b8a12011-08-04 00:01:48 +00002544 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002545 Record.clear();
2546 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2547 Record.push_back(CXXBaseSpecifiersOffsets.size());
2548 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002549 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002550}
2551
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002552//===----------------------------------------------------------------------===//
2553// Type Serialization
2554//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002555
Sebastian Redl3397c552010-08-18 23:56:27 +00002556/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002557void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002558 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002559 if (Idx.getIndex() == 0) // we haven't seen this type before.
2560 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002561
Douglas Gregor97475832010-10-05 18:37:06 +00002562 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002563
Douglas Gregor2cf26342009-04-09 22:27:44 +00002564 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002565 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002566 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002567 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002568 else if (TypeOffsets.size() < Index) {
2569 TypeOffsets.resize(Index + 1);
2570 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002571 }
2572
2573 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002574
Douglas Gregor2cf26342009-04-09 22:27:44 +00002575 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002576 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002577
Douglas Gregora4923eb2009-11-16 21:35:15 +00002578 if (T.hasLocalNonFastQualifiers()) {
2579 Qualifiers Qs = T.getLocalQualifiers();
2580 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002581 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002582 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002583 } else {
2584 switch (T->getTypeClass()) {
2585 // For all of the concrete, non-dependent types, call the
2586 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002587#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002588 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002589#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002590#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002591 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002592 }
2593
2594 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002595 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002596
2597 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002598 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002599}
2600
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002601//===----------------------------------------------------------------------===//
2602// Declaration Serialization
2603//===----------------------------------------------------------------------===//
2604
Douglas Gregor2cf26342009-04-09 22:27:44 +00002605/// \brief Write the block containing all of the declaration IDs
2606/// lexically declared within the given DeclContext.
2607///
2608/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2609/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002610uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002611 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002612 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002613 return 0;
2614
Douglas Gregorc9490c02009-04-16 22:23:12 +00002615 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002616 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002617 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002618 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002619 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2620 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002621 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002622
Douglas Gregor25123082009-04-22 22:34:57 +00002623 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002624 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002625 return Offset;
2626}
2627
Sebastian Redla4232eb2010-08-18 23:56:21 +00002628void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002629 using namespace llvm;
2630 RecordData Record;
2631
2632 // Write the type offsets array
2633 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002634 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2638 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2639 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002640 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002641 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002642 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002643 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002644
2645 // Write the declaration offsets array
2646 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002647 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002648 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002649 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2651 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2652 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002653 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002654 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002655 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002656 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002657}
2658
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002659void ASTWriter::WriteFileDeclIDsMap() {
2660 using namespace llvm;
2661 RecordData Record;
2662
2663 // Join the vectors of DeclIDs from all files.
2664 SmallVector<DeclID, 256> FileSortedIDs;
2665 for (FileDeclIDsTy::iterator
2666 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2667 DeclIDInFileInfo &Info = *FI->second;
2668 Info.FirstDeclIndex = FileSortedIDs.size();
2669 for (LocDeclIDsTy::iterator
2670 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2671 FileSortedIDs.push_back(DI->second);
2672 }
2673
2674 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2675 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2678 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2679 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002680 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002681 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2682}
2683
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002684void ASTWriter::WriteComments() {
2685 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002686 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002687 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002688 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2689 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002690 I != E; ++I) {
2691 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002692 AddSourceRange((*I)->getSourceRange(), Record);
2693 Record.push_back((*I)->getKind());
2694 Record.push_back((*I)->isTrailingComment());
2695 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002696 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2697 }
2698 Stream.ExitBlock();
2699}
2700
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002701//===----------------------------------------------------------------------===//
2702// Global Method Pool and Selector Serialization
2703//===----------------------------------------------------------------------===//
2704
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002705namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002706// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002707class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002708 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002709
2710public:
2711 typedef Selector key_type;
2712 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Sebastian Redl5d050072010-08-04 17:20:04 +00002714 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002715 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002716 ObjCMethodList Instance, Factory;
2717 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002718 typedef const data_type& data_type_ref;
2719
Sebastian Redl3397c552010-08-18 23:56:27 +00002720 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002722 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002723 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002724 }
Mike Stump1eb44332009-09-09 15:08:12 +00002725
2726 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002727 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002728 data_type_ref Methods) {
2729 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2730 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002731 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2732 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002733 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002734 if (Method->Method)
2735 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002736 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002737 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002738 if (Method->Method)
2739 DataLen += 4;
2740 clang::io::Emit16(Out, DataLen);
2741 return std::make_pair(KeyLen, DataLen);
2742 }
Mike Stump1eb44332009-09-09 15:08:12 +00002743
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002745 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002746 assert((Start >> 32) == 0 && "Selector key offset too large");
2747 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002748 unsigned N = Sel.getNumArgs();
2749 clang::io::Emit16(Out, N);
2750 if (N == 0)
2751 N = 1;
2752 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002753 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002754 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2755 }
Mike Stump1eb44332009-09-09 15:08:12 +00002756
Chris Lattner5f9e2722011-07-23 10:55:15 +00002757 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002758 data_type_ref Methods, unsigned DataLen) {
2759 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002760 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002761 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002762 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002763 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002764 if (Method->Method)
2765 ++NumInstanceMethods;
2766
2767 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002768 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002769 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002770 if (Method->Method)
2771 ++NumFactoryMethods;
2772
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002773 unsigned InstanceBits = Methods.Instance.getBits();
2774 assert(InstanceBits < 4);
2775 unsigned NumInstanceMethodsAndBits =
2776 (NumInstanceMethods << 2) | InstanceBits;
2777 unsigned FactoryBits = Methods.Factory.getBits();
2778 assert(FactoryBits < 4);
2779 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2780 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2781 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002782 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002783 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002784 if (Method->Method)
2785 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002786 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002787 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002788 if (Method->Method)
2789 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002790
2791 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002792 }
2793};
2794} // end anonymous namespace
2795
Sebastian Redl059612d2010-08-03 21:58:15 +00002796/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002797///
2798/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002799/// in an on-disk hash table indexed by the selector. The hash table also
2800/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002801void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002802 using namespace llvm;
2803
Sebastian Redl059612d2010-08-03 21:58:15 +00002804 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002805 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002806 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002807 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002808 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002809 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002810 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002811 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Sebastian Redl059612d2010-08-03 21:58:15 +00002813 // Create the on-disk hash table representation. We walk through every
2814 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002815 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002816 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002817 I = SelectorIDs.begin(), E = SelectorIDs.end();
2818 I != E; ++I) {
2819 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002820 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002821 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002822 I->second,
2823 ObjCMethodList(),
2824 ObjCMethodList()
2825 };
2826 if (F != SemaRef.MethodPool.end()) {
2827 Data.Instance = F->second.first;
2828 Data.Factory = F->second.second;
2829 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002830 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002831 // changed.
2832 if (Chain && I->second < FirstSelectorID) {
2833 // Selector already exists. Did it change?
2834 bool changed = false;
2835 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002836 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002837 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002838 changed = true;
2839 }
2840 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002841 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002842 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002843 changed = true;
2844 }
2845 if (!changed)
2846 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002847 } else if (Data.Instance.Method || Data.Factory.Method) {
2848 // A new method pool entry.
2849 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002850 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002851 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002852 }
2853
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002854 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002855 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002856 uint32_t BucketOffset;
2857 {
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
Douglas Gregora67e58c2009-04-24 21:49:02 +00002861 clang::io::Emit32(Out, 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
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002960 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2961 SubmoduleID &ModID) {
2962 ModID = 0;
2963 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2964 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2965 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002966 return 0;
2967 }
2968
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002969 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2970 SubmoduleID &ModID) {
2971 if (DefMacroDirective *
2972 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2973 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2974 return DefMD;
2975 return 0;
2976 }
2977
2978 /// \brief Traverses the macro directives history and returns the latest
2979 /// macro that is public and not undefined in the same submodule.
2980 /// A macro that is defined in submodule A and undefined in submodule B,
2981 /// will still be considered as defined/exported from submodule A.
2982 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2983 SubmoduleID &ModID) {
2984 if (!MD)
2985 return 0;
2986
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002987 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002988 bool isUndefined = false;
2989 Optional<bool> isPublic;
2990 for (; MD; MD = MD->getPrevious()) {
2991 if (MD->isHidden())
2992 continue;
2993
2994 SubmoduleID ThisModID = getSubmoduleID(MD);
2995 if (ThisModID == 0) {
2996 isUndefined = false;
2997 isPublic = Optional<bool>();
2998 continue;
2999 }
3000 if (ThisModID != ModID){
3001 ModID = ThisModID;
3002 isUndefined = false;
3003 isPublic = Optional<bool>();
3004 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00003005 // We are looking for a definition in a different submodule than the one
3006 // that we started with. If a submodule has re-definitions of the same
3007 // macro, only the last definition will be used as the "exported" one.
3008 if (ModID == OrigModID)
3009 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003010
3011 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3012 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
3013 return DefMD;
3014 continue;
3015 }
3016
3017 if (isa<UndefMacroDirective>(MD)) {
3018 isUndefined = true;
3019 continue;
3020 }
3021
3022 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
3023 if (!isPublic.hasValue())
3024 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003025 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003026
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003027 return 0;
3028 }
3029
3030 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003031 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3032 MacroInfo *MI = DefMD->getInfo();
3033 if (unsigned ID = MI->getOwningModuleID())
3034 return ID;
3035 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3036 }
3037 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003038 }
3039
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003040public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003041 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003042 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003044 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003045 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Douglas Gregoreee242f2011-10-27 09:33:13 +00003047 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3048 IdentifierResolver &IdResolver, bool IsModule)
3049 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003050
3051 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003052 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003053 }
Mike Stump1eb44332009-09-09 15:08:12 +00003054
3055 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003056 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003057 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003058 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003059 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003060 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003061 DataLen += 2; // 2 bytes for builtin ID
3062 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003063 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003064 DataLen += 4; // MacroDirectives offset.
3065 if (IsModule) {
3066 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003067 for (DefMacroDirective *
3068 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3069 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003070 DataLen += 4; // MacroInfo ID.
3071 }
3072 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003073 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003074 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003075
Douglas Gregoreee242f2011-10-27 09:33:13 +00003076 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3077 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003078 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003079 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003080 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003081 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003082 // We emit the key length after the data length so that every
3083 // string is preceded by a 16-bit length. This matches the PTH
3084 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003085 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003086 return std::make_pair(KeyLen, DataLen);
3087 }
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Chris Lattner5f9e2722011-07-23 10:55:15 +00003089 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003090 unsigned KeyLen) {
3091 // Record the location of the key data. This is used when generating
3092 // the mapping from persistent IDs to strings.
3093 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003094 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003095 }
Mike Stump1eb44332009-09-09 15:08:12 +00003096
Douglas Gregor7143aab2011-09-01 17:04:32 +00003097 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003098 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003099 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003100 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003101 clang::io::Emit32(Out, ID << 1);
3102 return;
3103 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003104
Douglas Gregora92193e2009-04-28 21:18:29 +00003105 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003106 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3107 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3108 clang::io::Emit16(Out, Bits);
3109 Bits = 0;
3110 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003111 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003112 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003113 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3114 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003115 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003116 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003117 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003118
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003119 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003120 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3121 if (IsModule) {
3122 // Write the IDs of macros coming from different submodules.
3123 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003124 for (DefMacroDirective *
3125 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3126 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3127 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003128 assert(InfoID);
3129 clang::io::Emit32(Out, InfoID);
3130 }
3131 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003132 }
Douglas Gregor13292642011-12-02 15:45:10 +00003133 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003134
Douglas Gregor668c1a42009-04-21 22:25:48 +00003135 // Emit the declaration IDs in reverse order, because the
3136 // IdentifierResolver provides the declarations as they would be
3137 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003138 // "stat"), but the ASTReader adds declarations to the end of the list
3139 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003140 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003141 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3142 IdResolver.end());
Craig Topper09d19ef2013-07-04 03:08:24 +00003143 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003144 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003145 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003146 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3147 }
3148
3149 /// \brief Returns the most recent local decl or the given decl if there are
3150 /// no local ones. The given decl is assumed to be the most recent one.
3151 Decl *getMostRecentLocalDecl(Decl *Orig) {
3152 // The only way a "from AST file" decl would be more recent from a local one
3153 // is if it came from a module.
3154 if (!PP.getLangOpts().Modules)
3155 return Orig;
3156
3157 // Look for a local in the decl chain.
3158 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3159 if (!D->isFromASTFile())
3160 return D;
3161 // If we come up a decl from a (chained-)PCH stop since we won't find a
3162 // local one.
3163 if (D->getOwningModuleID() == 0)
3164 break;
3165 }
3166
3167 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003168 }
3169};
3170} // end anonymous namespace
3171
Sebastian Redl3397c552010-08-18 23:56:27 +00003172/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003173///
3174/// The identifier table consists of a blob containing string data
3175/// (the actual identifiers themselves) and a separate "offsets" index
3176/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003177void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3178 IdentifierResolver &IdResolver,
3179 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003180 using namespace llvm;
3181
3182 // Create and write out the blob that contains the identifier
3183 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003184 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003185 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003186 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Douglas Gregor92b059e2009-04-28 20:33:11 +00003188 // Look for any identifiers that were named while processing the
3189 // headers, but are otherwise not needed. We add these to the hash
3190 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003191 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003192 // file.
3193 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3194 IDEnd = PP.getIdentifierTable().end();
3195 ID != IDEnd; ++ID)
3196 getIdentifierRef(ID->second);
3197
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003198 // Create the on-disk hash table representation. We only store offsets
3199 // for identifiers that appear here for the first time.
3200 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003201 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003202 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3203 ID != IDEnd; ++ID) {
3204 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003205 if (!Chain || !ID->first->isFromAST() ||
3206 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003207 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003208 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003209 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003210
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003211 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003212 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003213 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003214 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003215 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003216 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003217 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003218 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003219 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003220 }
3221
3222 // Create a blob abbreviation
3223 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003224 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003227 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003228
3229 // Write the identifier table
3230 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003231 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003232 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003233 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003234 }
3235
3236 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003237 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003238 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3242 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3243
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003244#ifndef NDEBUG
3245 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3246 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3247#endif
3248
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003249 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003250 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003251 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003252 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003253 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003254 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003255}
3256
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003257//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003258// DeclContext's Name Lookup Table Serialization
3259//===----------------------------------------------------------------------===//
3260
3261namespace {
3262// Trait used for the on-disk hash table used in the method pool.
3263class ASTDeclContextNameLookupTrait {
3264 ASTWriter &Writer;
3265
3266public:
3267 typedef DeclarationName key_type;
3268 typedef key_type key_type_ref;
3269
3270 typedef DeclContext::lookup_result data_type;
3271 typedef const data_type& data_type_ref;
3272
3273 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3274
3275 unsigned ComputeHash(DeclarationName Name) {
3276 llvm::FoldingSetNodeID ID;
3277 ID.AddInteger(Name.getNameKind());
3278
3279 switch (Name.getNameKind()) {
3280 case DeclarationName::Identifier:
3281 ID.AddString(Name.getAsIdentifierInfo()->getName());
3282 break;
3283 case DeclarationName::ObjCZeroArgSelector:
3284 case DeclarationName::ObjCOneArgSelector:
3285 case DeclarationName::ObjCMultiArgSelector:
3286 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3287 break;
3288 case DeclarationName::CXXConstructorName:
3289 case DeclarationName::CXXDestructorName:
3290 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003291 break;
3292 case DeclarationName::CXXOperatorName:
3293 ID.AddInteger(Name.getCXXOverloadedOperator());
3294 break;
3295 case DeclarationName::CXXLiteralOperatorName:
3296 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3297 case DeclarationName::CXXUsingDirective:
3298 break;
3299 }
3300
3301 return ID.ComputeHash();
3302 }
3303
3304 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003305 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003306 data_type_ref Lookup) {
3307 unsigned KeyLen = 1;
3308 switch (Name.getNameKind()) {
3309 case DeclarationName::Identifier:
3310 case DeclarationName::ObjCZeroArgSelector:
3311 case DeclarationName::ObjCOneArgSelector:
3312 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003313 case DeclarationName::CXXLiteralOperatorName:
3314 KeyLen += 4;
3315 break;
3316 case DeclarationName::CXXOperatorName:
3317 KeyLen += 1;
3318 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003319 case DeclarationName::CXXConstructorName:
3320 case DeclarationName::CXXDestructorName:
3321 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003322 case DeclarationName::CXXUsingDirective:
3323 break;
3324 }
3325 clang::io::Emit16(Out, KeyLen);
3326
3327 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003328 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003329 clang::io::Emit16(Out, DataLen);
3330
3331 return std::make_pair(KeyLen, DataLen);
3332 }
3333
Chris Lattner5f9e2722011-07-23 10:55:15 +00003334 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003335 using namespace clang::io;
3336
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003337 Emit8(Out, Name.getNameKind());
3338 switch (Name.getNameKind()) {
3339 case DeclarationName::Identifier:
3340 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003341 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003342 case DeclarationName::ObjCZeroArgSelector:
3343 case DeclarationName::ObjCOneArgSelector:
3344 case DeclarationName::ObjCMultiArgSelector:
3345 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003346 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003347 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003348 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3349 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003350 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003351 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003352 case DeclarationName::CXXLiteralOperatorName:
3353 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003354 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003355 case DeclarationName::CXXConstructorName:
3356 case DeclarationName::CXXDestructorName:
3357 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003358 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003359 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003360 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003361
3362 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003363 }
3364
Chris Lattner5f9e2722011-07-23 10:55:15 +00003365 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003366 data_type Lookup, unsigned DataLen) {
3367 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003368 clang::io::Emit16(Out, Lookup.size());
3369 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3370 I != E; ++I)
3371 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003372
3373 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3374 }
3375};
3376} // end anonymous namespace
3377
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003378/// \brief Write the block containing all of the declaration IDs
3379/// visible from the given DeclContext.
3380///
3381/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003382/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003383uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3384 DeclContext *DC) {
3385 if (DC->getPrimaryContext() != DC)
3386 return 0;
3387
3388 // Since there is no name lookup into functions or methods, don't bother to
3389 // build a visible-declarations table for these entities.
3390 if (DC->isFunctionOrMethod())
3391 return 0;
3392
3393 // If not in C++, we perform name lookup for the translation unit via the
3394 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003395 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003396 return 0;
3397
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003398 // Serialize the contents of the mapping used for lookup. Note that,
3399 // although we have two very different code paths, the serialized
3400 // representation is the same for both cases: a declaration name,
3401 // followed by a size, followed by references to the visible
3402 // declarations that have that name.
3403 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003404 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003405 if (!Map || Map->empty())
3406 return 0;
3407
3408 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3409 ASTDeclContextNameLookupTrait Trait(*this);
3410
3411 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003412 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003413 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003414 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3415 D != DEnd; ++D) {
3416 DeclarationName Name = D->first;
3417 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003418 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003419 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3420 // Hash all conversion function names to the same name. The actual
3421 // type information in conversion function name is not used in the
3422 // key (since such type information is not stable across different
3423 // modules), so the intended effect is to coalesce all of the conversion
3424 // functions under a single key.
3425 if (!ConversionName)
3426 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003427 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003428 continue;
3429 }
3430
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003431 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003432 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003433 }
3434
Douglas Gregore5a54b62011-08-30 20:49:19 +00003435 // Add the conversion functions
3436 if (!ConversionDecls.empty()) {
3437 Generator.insert(ConversionName,
3438 DeclContext::lookup_result(ConversionDecls.begin(),
3439 ConversionDecls.end()),
3440 Trait);
3441 }
3442
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003443 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003444 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003445 uint32_t BucketOffset;
3446 {
3447 llvm::raw_svector_ostream Out(LookupTable);
3448 // Make sure that no bucket is at offset 0
3449 clang::io::Emit32(Out, 0);
3450 BucketOffset = Generator.Emit(Out, Trait);
3451 }
3452
3453 // Write the lookup table
3454 RecordData Record;
3455 Record.push_back(DECL_CONTEXT_VISIBLE);
3456 Record.push_back(BucketOffset);
3457 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3458 LookupTable.str());
3459
3460 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3461 ++NumVisibleDeclContexts;
3462 return Offset;
3463}
3464
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003465/// \brief Write an UPDATE_VISIBLE block for the given context.
3466///
3467/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3468/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003469/// (in C++), for namespaces, and for classes with forward-declared unscoped
3470/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003471void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003472 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3473 if (!Map || Map->empty())
3474 return;
3475
3476 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3477 ASTDeclContextNameLookupTrait Trait(*this);
3478
3479 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003480 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3481 D != DEnd; ++D) {
3482 DeclarationName Name = D->first;
3483 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003484 // For any name that appears in this table, the results are complete, i.e.
3485 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003486 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003487 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003488 }
3489
3490 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003491 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003492 uint32_t BucketOffset;
3493 {
3494 llvm::raw_svector_ostream Out(LookupTable);
3495 // Make sure that no bucket is at offset 0
3496 clang::io::Emit32(Out, 0);
3497 BucketOffset = Generator.Emit(Out, Trait);
3498 }
3499
3500 // Write the lookup table
3501 RecordData Record;
3502 Record.push_back(UPDATE_VISIBLE);
3503 Record.push_back(getDeclID(cast<Decl>(DC)));
3504 Record.push_back(BucketOffset);
3505 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3506}
3507
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003508/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3509void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3510 RecordData Record;
3511 Record.push_back(Opts.fp_contract);
3512 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3513}
3514
3515/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3516void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003517 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003518 return;
3519
3520 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3521 RecordData Record;
3522#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3523#include "clang/Basic/OpenCLExtensions.def"
3524 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3525}
3526
Douglas Gregor2171bf12012-01-15 16:58:34 +00003527void ASTWriter::WriteRedeclarations() {
3528 RecordData LocalRedeclChains;
3529 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3530
3531 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3532 Decl *First = Redeclarations[I];
3533 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3534
3535 Decl *MostRecent = First->getMostRecentDecl();
3536
3537 // If we only have a single declaration, there is no point in storing
3538 // a redeclaration chain.
3539 if (First == MostRecent)
3540 continue;
3541
3542 unsigned Offset = LocalRedeclChains.size();
3543 unsigned Size = 0;
3544 LocalRedeclChains.push_back(0); // Placeholder for the size.
3545
3546 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003547 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003548 Prev = Prev->getPreviousDecl()) {
3549 if (!Prev->isFromASTFile()) {
3550 AddDeclRef(Prev, LocalRedeclChains);
3551 ++Size;
3552 }
3553 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003554
3555 if (!First->isFromASTFile() && Chain) {
3556 Decl *FirstFromAST = MostRecent;
3557 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3558 if (Prev->isFromASTFile())
3559 FirstFromAST = Prev;
3560 }
3561
3562 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3563 }
3564
Douglas Gregor2171bf12012-01-15 16:58:34 +00003565 LocalRedeclChains[Offset] = Size;
3566
3567 // Reverse the set of local redeclarations, so that we store them in
3568 // order (since we found them in reverse order).
3569 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3570
Douglas Gregoraa945902013-02-18 15:53:43 +00003571 // Add the mapping from the first ID from the AST to the set of local
3572 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003573 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3574 LocalRedeclsMap.push_back(Info);
3575
3576 assert(N == Redeclarations.size() &&
3577 "Deserialized a declaration we shouldn't have");
3578 }
3579
3580 if (LocalRedeclChains.empty())
3581 return;
3582
3583 // Sort the local redeclarations map by the first declaration ID,
3584 // since the reader will be performing binary searches on this information.
3585 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3586
3587 // Emit the local redeclarations map.
3588 using namespace llvm;
3589 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3590 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3593 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3594
3595 RecordData Record;
3596 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3597 Record.push_back(LocalRedeclsMap.size());
3598 Stream.EmitRecordWithBlob(AbbrevID, Record,
3599 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3600 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3601
3602 // Emit the redeclaration chains.
3603 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3604}
3605
Douglas Gregorcff9f262012-01-27 01:47:08 +00003606void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003607 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003608 RecordData Categories;
3609
3610 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3611 unsigned Size = 0;
3612 unsigned StartIndex = Categories.size();
3613
3614 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3615
3616 // Allocate space for the size.
3617 Categories.push_back(0);
3618
3619 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003620 for (ObjCInterfaceDecl::known_categories_iterator
3621 Cat = Class->known_categories_begin(),
3622 CatEnd = Class->known_categories_end();
3623 Cat != CatEnd; ++Cat, ++Size) {
3624 assert(getDeclID(*Cat) != 0 && "Bogus category");
3625 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003626 }
3627
3628 // Update the size.
3629 Categories[StartIndex] = Size;
3630
3631 // Record this interface -> category map.
3632 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3633 CategoriesMap.push_back(CatInfo);
3634 }
3635
3636 // Sort the categories map by the definition ID, since the reader will be
3637 // performing binary searches on this information.
3638 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3639
3640 // Emit the categories map.
3641 using namespace llvm;
3642 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3643 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3646 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3647
3648 RecordData Record;
3649 Record.push_back(OBJC_CATEGORIES_MAP);
3650 Record.push_back(CategoriesMap.size());
3651 Stream.EmitRecordWithBlob(AbbrevID, Record,
3652 reinterpret_cast<char*>(CategoriesMap.data()),
3653 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3654
3655 // Emit the category lists.
3656 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3657}
3658
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003659void ASTWriter::WriteMergedDecls() {
3660 if (!Chain || Chain->MergedDecls.empty())
3661 return;
3662
3663 RecordData Record;
3664 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3665 IEnd = Chain->MergedDecls.end();
3666 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003667 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003668 : getDeclID(I->first);
3669 assert(CanonID && "Merged declaration not known?");
3670
3671 Record.push_back(CanonID);
3672 Record.push_back(I->second.size());
3673 Record.append(I->second.begin(), I->second.end());
3674 }
3675 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3676}
3677
Richard Smithac32d902013-08-07 21:41:30 +00003678void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3679 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3680
3681 if (LPTMap.empty())
3682 return;
3683
3684 RecordData Record;
3685 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3686 ItEnd = LPTMap.end();
3687 It != ItEnd; ++It) {
3688 LateParsedTemplate *LPT = It->second;
3689 AddDeclRef(It->first, Record);
3690 AddDeclRef(LPT->D, Record);
3691 Record.push_back(LPT->Toks.size());
3692
3693 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3694 TokEnd = LPT->Toks.end();
3695 TokIt != TokEnd; ++TokIt) {
3696 AddToken(*TokIt, Record);
3697 }
3698 }
3699 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3700}
3701
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003702//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003703// General Serialization Routines
3704//===----------------------------------------------------------------------===//
3705
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003706/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003707void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3708 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003709 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003710 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3711 e = Attrs.end(); i != e; ++i){
3712 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003713 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003714 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003715
Sean Huntcf807c42010-08-18 23:23:40 +00003716#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003717
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003718 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003719}
3720
John McCallaeeacf72013-05-03 00:10:13 +00003721void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3722 AddSourceLocation(Tok.getLocation(), Record);
3723 Record.push_back(Tok.getLength());
3724
3725 // FIXME: When reading literal tokens, reconstruct the literal pointer
3726 // if it is needed.
3727 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3728 // FIXME: Should translate token kind to a stable encoding.
3729 Record.push_back(Tok.getKind());
3730 // FIXME: Should translate token flags to a stable encoding.
3731 Record.push_back(Tok.getFlags());
3732}
3733
Chris Lattner5f9e2722011-07-23 10:55:15 +00003734void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003735 Record.push_back(Str.size());
3736 Record.insert(Record.end(), Str.begin(), Str.end());
3737}
3738
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003739void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3740 RecordDataImpl &Record) {
3741 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003742 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003743 Record.push_back(*Minor + 1);
3744 else
3745 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003746 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003747 Record.push_back(*Subminor + 1);
3748 else
3749 Record.push_back(0);
3750}
3751
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003752/// \brief Note that the identifier II occurs at the given offset
3753/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003754void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003755 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003756 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003757 // up earlier in the chain and thus don't need an offset.
3758 if (ID >= FirstIdentID)
3759 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003760}
3761
Douglas Gregor83941df2009-04-25 17:48:32 +00003762/// \brief Note that the selector Sel occurs at the given offset
3763/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003764void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003765 unsigned ID = SelectorIDs[Sel];
3766 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003767 // Don't record offsets for selectors that are also available in a different
3768 // file.
3769 if (ID < FirstSelectorID)
3770 return;
3771 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003772}
3773
Sebastian Redla4232eb2010-08-18 23:56:21 +00003774ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003775 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003776 WritingAST(false), DoneWritingDeclsAndTypes(false),
3777 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003778 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003779 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003780 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3781 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003782 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3783 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003784 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003785 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003786 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003787 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003788 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003789 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003790 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3791 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3792 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003793 DeclTypedefAbbrev(0),
3794 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3795 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003796{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003797}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003798
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003799ASTWriter::~ASTWriter() {
3800 for (FileDeclIDsTy::iterator
3801 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3802 delete I->second;
3803}
3804
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003805void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003806 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003807 Module *WritingModule, StringRef isysroot,
3808 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003809 WritingAST = true;
3810
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003811 ASTHasCompilerErrors = hasErrors;
3812
Douglas Gregor2cf26342009-04-09 22:27:44 +00003813 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003814 Stream.Emit((unsigned)'C', 8);
3815 Stream.Emit((unsigned)'P', 8);
3816 Stream.Emit((unsigned)'C', 8);
3817 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003818
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003819 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003820
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003821 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003822 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003823 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003824 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003825 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003826 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003827 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003828
3829 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003830}
3831
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003832template<typename Vector>
3833static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3834 ASTWriter::RecordData &Record) {
3835 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3836 I != E; ++I) {
3837 Writer.AddDeclRef(*I, Record);
3838 }
3839}
3840
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003841void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003842 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003843 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003844 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003845 using namespace llvm;
3846
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003847 bool isModule = WritingModule != 0;
3848
Douglas Gregorecc2c092011-12-01 22:20:10 +00003849 // Make sure that the AST reader knows to finalize itself.
3850 if (Chain)
3851 Chain->finalizeForWriting();
3852
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003853 ASTContext &Context = SemaRef.Context;
3854 Preprocessor &PP = SemaRef.PP;
3855
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003856 // Set up predefined declaration IDs.
3857 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003858 if (Context.ObjCIdDecl)
3859 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003860 if (Context.ObjCSelDecl)
3861 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003862 if (Context.ObjCClassDecl)
3863 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003864 if (Context.ObjCProtocolClassDecl)
3865 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003866 if (Context.Int128Decl)
3867 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3868 if (Context.UInt128Decl)
3869 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003870 if (Context.ObjCInstanceTypeDecl)
3871 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003872 if (Context.BuiltinVaListDecl)
3873 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3874
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003875 if (!Chain) {
3876 // Make sure that we emit IdentifierInfos (and any attached
3877 // declarations) for builtins. We don't need to do this when we're
3878 // emitting chained PCH files, because all of the builtins will be
3879 // in the original PCH file.
3880 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003881 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003882 SmallVector<const char *, 32> BuiltinNames;
Eli Bendersky97a03cf2013-07-11 16:53:04 +00003883 if (!Context.getLangOpts().NoBuiltin) {
3884 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3885 }
Douglas Gregor2deaea32009-04-22 18:49:13 +00003886 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3887 getIdentifierRef(&Table.get(BuiltinNames[I]));
3888 }
3889
Douglas Gregoreee242f2011-10-27 09:33:13 +00003890 // If there are any out-of-date identifiers, bring them up to date.
3891 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003892 // Find out-of-date identifiers.
3893 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003894 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3895 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003896 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003897 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003898 OutOfDate.push_back(ID->second);
3899 }
3900
3901 // Update the out-of-date identifiers.
3902 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3903 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3904 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003905 }
3906
Chris Lattner63d65f82009-09-08 18:19:27 +00003907 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003908 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003909 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003910 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003911 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003912
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003913 // Build a record containing all of the file scoped decls in this file.
3914 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003915 if (!isModule)
3916 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3917 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003918
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003919 // Build a record containing all of the delegating constructors we still need
3920 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003921 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003922 if (!isModule)
3923 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003924
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003925 // Write the set of weak, undeclared identifiers. We always write the
3926 // entire table, since later PCH files in a PCH chain are only interested in
3927 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003928 RecordData WeakUndeclaredIdentifiers;
3929 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003930 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003931 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3932 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3933 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3934 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3935 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3936 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3937 }
3938 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003939
Richard Smith5ea6ef42013-01-10 23:43:47 +00003940 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003941 // declarations in this header file. Generally, this record will be
3942 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003943 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003944 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003945 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003946 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003947 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3948 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003949 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003950 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003951 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003952 }
3953
Douglas Gregorb81c1702009-04-27 20:06:05 +00003954 // Build a record containing all of the ext_vector declarations.
3955 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003956 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003957
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003958 // Build a record containing all of the VTable uses information.
3959 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003960 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003961 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3962 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3963 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3964 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3965 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003966 }
3967
3968 // Build a record containing all of dynamic classes declarations.
3969 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003970 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003971
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003972 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003973 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003974 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003975 I = SemaRef.PendingInstantiations.begin(),
3976 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3977 AddDeclRef(I->first, PendingInstantiations);
3978 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003979 }
3980 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3981 "There are local ones at end of translation unit!");
3982
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003983 // Build a record containing some declaration references.
3984 RecordData SemaDeclRefs;
3985 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3986 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3987 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3988 }
3989
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003990 RecordData CUDASpecialDeclRefs;
3991 if (Context.getcudaConfigureCallDecl()) {
3992 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3993 }
3994
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003995 // Build a record containing all of the known namespaces.
3996 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003997 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003998 I = SemaRef.KnownNamespaces.begin(),
3999 IEnd = SemaRef.KnownNamespaces.end();
4000 I != IEnd; ++I) {
4001 if (!I->second)
4002 AddDeclRef(I->first, KnownNamespaces);
4003 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004004
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004005 // Build a record of all used, undefined objects that require definitions.
4006 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00004007
4008 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004009 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00004010 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4011 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004012 AddDeclRef(I->first, UndefinedButUsed);
4013 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00004014 }
4015
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004016 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00004017 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00004018
Sebastian Redl3397c552010-08-18 23:56:27 +00004019 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00004020 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004021 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004022
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00004023 // This is so that older clang versions, before the introduction
4024 // of the control block, can read and reject the newer PCH format.
4025 Record.clear();
4026 Record.push_back(VERSION_MAJOR);
4027 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4028
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004029 // Create a lexical update block containing all of the declarations in the
4030 // translation unit that do not come from other AST files.
4031 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4032 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4033 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
4034 E = TU->noload_decls_end();
4035 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00004036 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004037 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004038 }
4039
4040 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4041 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4042 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4043 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4044 Record.clear();
4045 Record.push_back(TU_UPDATE_LEXICAL);
4046 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4047 data(NewGlobalDecls));
4048
4049 // And a visible updates block for the translation unit.
4050 Abv = new llvm::BitCodeAbbrev();
4051 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4052 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4053 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4054 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4055 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4056 WriteDeclContextVisibleUpdate(TU);
4057
4058 // If the translation unit has an anonymous namespace, and we don't already
4059 // have an update block for it, write it as an update block.
4060 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4061 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4062 if (Record.empty()) {
4063 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004064 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004065 }
4066 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004067
4068 // Make sure visible decls, added to DeclContexts previously loaded from
4069 // an AST file, are registered for serialization.
Craig Topper09d19ef2013-07-04 03:08:24 +00004070 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004071 I = UpdatingVisibleDecls.begin(),
4072 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4073 GetDeclRef(*I);
4074 }
4075
Argyrios Kyrtzidis51e75ae2013-08-07 21:17:33 +00004076 // Make sure all decls associated with an identifier are registered for
4077 // serialization.
4078 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4079 IDEnd = PP.getIdentifierTable().end();
4080 ID != IDEnd; ++ID) {
4081 const IdentifierInfo *II = ID->second;
4082 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4083 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4084 DEnd = SemaRef.IdResolver.end();
4085 D != DEnd; ++D) {
4086 GetDeclRef(*D);
4087 }
4088 }
4089 }
4090
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004091 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004092 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004093
Douglas Gregora119da02011-08-02 16:26:37 +00004094 // Form the record of special types.
4095 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004096 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004097 AddTypeRef(Context.getFILEType(), SpecialTypes);
4098 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4099 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4100 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4101 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004102 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004103 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004104
Douglas Gregor366809a2009-04-26 03:49:13 +00004105 // Keep writing types and declarations until all types and
4106 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004107 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004108 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004109 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4110 E = DeclsToRewrite.end();
4111 I != E; ++I)
4112 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004113 while (!DeclTypesToEmit.empty()) {
4114 DeclOrType DOT = DeclTypesToEmit.front();
4115 DeclTypesToEmit.pop();
4116 if (DOT.isType())
4117 WriteType(DOT.getType());
4118 else
4119 WriteDecl(Context, DOT.getDecl());
4120 }
4121 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004122
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004123 DoneWritingDeclsAndTypes = true;
4124
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004125 WriteFileDeclIDsMap();
4126 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004127 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004128
4129 if (Chain) {
4130 // Write the mapping information describing our module dependencies and how
4131 // each of those modules were mapped into our own offset/ID space, so that
4132 // the reader can build the appropriate mapping to its own offset/ID space.
4133 // The map consists solely of a blob with the following format:
4134 // *(module-name-len:i16 module-name:len*i8
4135 // source-location-offset:i32
4136 // identifier-id:i32
4137 // preprocessed-entity-id:i32
4138 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004139 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004140 // selector-id:i32
4141 // declaration-id:i32
4142 // c++-base-specifiers-id:i32
4143 // type-id:i32)
4144 //
4145 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4146 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4147 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4148 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004149 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004150 {
4151 llvm::raw_svector_ostream Out(Buffer);
4152 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004153 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004154 M != MEnd; ++M) {
4155 StringRef FileName = (*M)->FileName;
4156 io::Emit16(Out, FileName.size());
4157 Out.write(FileName.data(), FileName.size());
4158 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4159 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004160 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004161 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004162 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004163 io::Emit32(Out, (*M)->BaseSelectorID);
4164 io::Emit32(Out, (*M)->BaseDeclID);
4165 io::Emit32(Out, (*M)->BaseTypeIndex);
4166 }
4167 }
4168 Record.clear();
4169 Record.push_back(MODULE_OFFSET_MAP);
4170 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4171 Buffer.data(), Buffer.size());
4172 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004173 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004174 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004175 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004176 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004177 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004178 WriteFPPragmaOptions(SemaRef.getFPOptions());
4179 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004180
Sebastian Redl1476ed42010-07-16 16:36:56 +00004181 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004182 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004183
Anders Carlssonc8505782011-03-06 18:41:18 +00004184 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004185
Douglas Gregore209e502011-12-06 01:10:29 +00004186 // If we're emitting a module, write out the submodule information.
4187 if (WritingModule)
4188 WriteSubmodules(WritingModule);
4189
Douglas Gregora119da02011-08-02 16:26:37 +00004190 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4191
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004192 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004193 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004194 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004195
4196 // Write the record containing tentative definitions.
4197 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004198 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004199
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004200 // Write the record containing unused file scoped decls.
4201 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004202 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004203
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004204 // Write the record containing weak undeclared identifiers.
4205 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004206 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004207 WeakUndeclaredIdentifiers);
4208
Richard Smith5ea6ef42013-01-10 23:43:47 +00004209 // Write the record containing locally-scoped extern "C" definitions.
4210 if (!LocallyScopedExternCDecls.empty())
4211 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4212 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004213
4214 // Write the record containing ext_vector type names.
4215 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004216 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004217
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004218 // Write the record containing VTable uses information.
4219 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004220 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004221
4222 // Write the record containing dynamic classes declarations.
4223 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004224 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004225
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004226 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004227 if (!PendingInstantiations.empty())
4228 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004229
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004230 // Write the record containing declaration references of Sema.
4231 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004232 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004233
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004234 // Write the record containing CUDA-specific declaration references.
4235 if (!CUDASpecialDeclRefs.empty())
4236 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004237
4238 // Write the delegating constructors.
4239 if (!DelegatingCtorDecls.empty())
4240 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004241
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004242 // Write the known namespaces.
4243 if (!KnownNamespaces.empty())
4244 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004245
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004246 // Write the undefined internal functions and variables, and inline functions.
4247 if (!UndefinedButUsed.empty())
4248 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004249
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004250 // Write the visible updates to DeclContexts.
4251 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4252 I = UpdatedDeclContexts.begin(),
4253 E = UpdatedDeclContexts.end();
4254 I != E; ++I)
4255 WriteDeclContextVisibleUpdate(*I);
4256
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004257 if (!WritingModule) {
4258 // Write the submodules that were imported, if any.
4259 RecordData ImportedModules;
4260 for (ASTContext::import_iterator I = Context.local_import_begin(),
4261 IEnd = Context.local_import_end();
4262 I != IEnd; ++I) {
4263 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4264 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4265 }
4266 if (!ImportedModules.empty()) {
4267 // Sort module IDs.
4268 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4269
4270 // Unique module IDs.
4271 ImportedModules.erase(std::unique(ImportedModules.begin(),
4272 ImportedModules.end()),
4273 ImportedModules.end());
4274
4275 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4276 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004277 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004278
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004279 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004280 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004281 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004282 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004283 WriteObjCCategories();
Richard Smithac32d902013-08-07 21:41:30 +00004284 WriteLateParsedTemplates(SemaRef);
4285
Douglas Gregor3e1af842009-04-17 22:13:46 +00004286 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004287 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004288 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004289 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004290 Record.push_back(NumLexicalDeclContexts);
4291 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004292 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004293 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004294}
4295
Douglas Gregor61c5e342011-09-17 00:05:03 +00004296/// \brief Go through the declaration update blocks and resolve declaration
4297/// pointers into declaration IDs.
4298void ASTWriter::ResolveDeclUpdatesBlocks() {
4299 for (DeclUpdateMap::iterator
4300 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4301 const Decl *D = I->first;
4302 UpdateRecord &URec = I->second;
4303
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004304 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004305 continue; // The decl will be written completely
4306
4307 unsigned Idx = 0, N = URec.size();
4308 while (Idx < N) {
4309 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004310 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4311 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4312 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4313 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4314 ++Idx;
4315 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004316
Douglas Gregor61c5e342011-09-17 00:05:03 +00004317 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4318 ++Idx;
4319 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004320
4321 case UPD_CXX_DEDUCED_RETURN_TYPE:
4322 URec[Idx] = GetOrCreateTypeID(
4323 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4324 ++Idx;
4325 break;
Douglas Gregor61c5e342011-09-17 00:05:03 +00004326 }
4327 }
4328 }
4329}
4330
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004331void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004332 if (DeclUpdates.empty())
4333 return;
4334
4335 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004336 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004337 for (DeclUpdateMap::iterator
4338 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4339 const Decl *D = I->first;
4340 UpdateRecord &URec = I->second;
4341
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004342 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004343 continue; // The decl will be written completely,no need to store updates.
4344
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004345 uint64_t Offset = Stream.GetCurrentBitNo();
4346 Stream.EmitRecord(DECL_UPDATES, URec);
4347
4348 OffsetsRecord.push_back(GetDeclRef(D));
4349 OffsetsRecord.push_back(Offset);
4350 }
4351 Stream.ExitBlock();
4352 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4353}
4354
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004355void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004356 if (ReplacedDecls.empty())
4357 return;
4358
4359 RecordData Record;
Craig Topper09d19ef2013-07-04 03:08:24 +00004360 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4361 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004362 Record.push_back(I->ID);
4363 Record.push_back(I->Offset);
4364 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004365 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004366 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004367}
4368
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004369void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004370 Record.push_back(Loc.getRawEncoding());
4371}
4372
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004373void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004374 AddSourceLocation(Range.getBegin(), Record);
4375 AddSourceLocation(Range.getEnd(), Record);
4376}
4377
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004378void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004379 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004380 const uint64_t *Words = Value.getRawData();
4381 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004382}
4383
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004384void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004385 Record.push_back(Value.isUnsigned());
4386 AddAPInt(Value, Record);
4387}
4388
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004389void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004390 AddAPInt(Value.bitcastToAPInt(), Record);
4391}
4392
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004393void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004394 Record.push_back(getIdentifierRef(II));
4395}
4396
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004397IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004398 if (II == 0)
4399 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004400
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004401 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004402 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004403 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004404 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004405}
4406
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004407MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004408 // Don't emit builtin macros like __LINE__ to the AST file unless they
4409 // have been redefined by the header (in which case they are not
4410 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004411 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004412 return 0;
4413
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004414 MacroID &ID = MacroIDs[MI];
4415 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004416 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004417 MacroInfoToEmitData Info = { Name, MI, ID };
4418 MacroInfosToEmit.push_back(Info);
4419 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004420 return ID;
4421}
4422
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004423MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4424 if (MI == 0 || MI->isBuiltinMacro())
4425 return 0;
4426
4427 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4428 return MacroIDs[MI];
4429}
4430
4431uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4432 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4433 return IdentMacroDirectivesOffsetMap[Name];
4434}
4435
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004436void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004437 Record.push_back(getSelectorRef(SelRef));
4438}
4439
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004440SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004441 if (Sel.getAsOpaquePtr() == 0) {
4442 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004443 }
4444
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004445 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004446 if (SID == 0 && Chain) {
4447 // This might trigger a ReadSelector callback, which will set the ID for
4448 // this selector.
4449 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004450 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004451 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004452 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004453 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004454 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004455 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004456 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004457}
4458
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004459void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004460 AddDeclRef(Temp->getDestructor(), Record);
4461}
4462
Douglas Gregor7c789c12010-10-29 22:39:52 +00004463void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4464 CXXBaseSpecifier const *BasesEnd,
4465 RecordDataImpl &Record) {
4466 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4467 CXXBaseSpecifiersToWrite.push_back(
4468 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4469 Bases, BasesEnd));
4470 Record.push_back(NextCXXBaseSpecifiersID++);
4471}
4472
Sebastian Redla4232eb2010-08-18 23:56:21 +00004473void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004474 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004475 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004476 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004477 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004478 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004479 break;
4480 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004481 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004482 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004483 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004484 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004485 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004486 break;
4487 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004488 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004489 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004490 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004491 break;
John McCall833ca992009-10-29 08:12:44 +00004492 case TemplateArgument::Null:
4493 case TemplateArgument::Integral:
4494 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004495 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004496 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004497 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004498 break;
4499 }
4500}
4501
Sebastian Redla4232eb2010-08-18 23:56:21 +00004502void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004503 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004504 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004505
4506 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4507 bool InfoHasSameExpr
4508 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4509 Record.push_back(InfoHasSameExpr);
4510 if (InfoHasSameExpr)
4511 return; // Avoid storing the same expr twice.
4512 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004513 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4514 Record);
4515}
4516
Douglas Gregordc355712011-02-25 00:36:19 +00004517void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4518 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004519 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004520 AddTypeRef(QualType(), Record);
4521 return;
4522 }
4523
Douglas Gregordc355712011-02-25 00:36:19 +00004524 AddTypeLoc(TInfo->getTypeLoc(), Record);
4525}
4526
4527void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4528 AddTypeRef(TL.getType(), Record);
4529
John McCalla1ee0c52009-10-16 21:56:05 +00004530 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004531 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004532 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004533}
4534
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004535void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004536 Record.push_back(GetOrCreateTypeID(T));
4537}
4538
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004539TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004540 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004541 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004542 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4543}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004544
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004545TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004546 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004547 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004548 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004549}
4550
4551TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4552 if (T.isNull())
4553 return TypeIdx();
4554 assert(!T.getLocalFastQualifiers());
4555
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004556 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004557 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004558 if (DoneWritingDeclsAndTypes) {
4559 assert(0 && "New type seen after serializing all the types to emit!");
4560 return TypeIdx();
4561 }
4562
Douglas Gregor366809a2009-04-26 03:49:13 +00004563 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004564 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004565 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004566 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004567 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004568 return Idx;
4569}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004570
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004571TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004572 if (T.isNull())
4573 return TypeIdx();
4574 assert(!T.getLocalFastQualifiers());
4575
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004576 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4577 assert(I != TypeIdxs.end() && "Type not emitted!");
4578 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004579}
4580
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004581void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004582 Record.push_back(GetDeclRef(D));
4583}
4584
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004585DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004586 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4587
Douglas Gregor2cf26342009-04-09 22:27:44 +00004588 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004589 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004590 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004591
4592 // If D comes from an AST file, its declaration ID is already known and
4593 // fixed.
4594 if (D->isFromASTFile())
4595 return D->getGlobalID();
4596
Douglas Gregor97475832010-10-05 18:37:06 +00004597 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004598 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004599 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004600 if (DoneWritingDeclsAndTypes) {
4601 assert(0 && "New decl seen after serializing all the decls to emit!");
4602 return 0;
4603 }
4604
Douglas Gregor2cf26342009-04-09 22:27:44 +00004605 // We haven't seen this declaration before. Give it a new ID and
4606 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004607 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004608 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004609 }
4610
Sebastian Redl681d7232010-07-27 00:17:23 +00004611 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004612}
4613
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004614DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004615 if (D == 0)
4616 return 0;
4617
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004618 // If D comes from an AST file, its declaration ID is already known and
4619 // fixed.
4620 if (D->isFromASTFile())
4621 return D->getGlobalID();
4622
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004623 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4624 return DeclIDs[D];
4625}
4626
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004627void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004628 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004629 assert(D);
4630
4631 SourceLocation Loc = D->getLocation();
4632 if (Loc.isInvalid())
4633 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004634
4635 // We only keep track of the file-level declarations of each file.
4636 if (!D->getLexicalDeclContext()->isFileContext())
4637 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004638 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4639 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004640 if (isa<ParmVarDecl>(D))
4641 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004642
4643 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004644 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004645 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004646 FileID FID;
4647 unsigned Offset;
4648 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004649 if (FID.isInvalid())
4650 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004651 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004652
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004653 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004654 if (!Info)
4655 Info = new DeclIDInFileInfo();
4656
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004657 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004658 LocDeclIDsTy &Decls = Info->DeclIDs;
4659
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004660 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004661 Decls.push_back(LocDecl);
4662 return;
4663 }
4664
4665 LocDeclIDsTy::iterator
Benjamin Kramera9bdbce2013-08-24 13:12:34 +00004666 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, less_first());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004667
4668 Decls.insert(I, LocDecl);
4669}
4670
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004671void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004672 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004673 Record.push_back(Name.getNameKind());
4674 switch (Name.getNameKind()) {
4675 case DeclarationName::Identifier:
4676 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4677 break;
4678
4679 case DeclarationName::ObjCZeroArgSelector:
4680 case DeclarationName::ObjCOneArgSelector:
4681 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004682 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004683 break;
4684
4685 case DeclarationName::CXXConstructorName:
4686 case DeclarationName::CXXDestructorName:
4687 case DeclarationName::CXXConversionFunctionName:
4688 AddTypeRef(Name.getCXXNameType(), Record);
4689 break;
4690
4691 case DeclarationName::CXXOperatorName:
4692 Record.push_back(Name.getCXXOverloadedOperator());
4693 break;
4694
Sean Hunt3e518bd2009-11-29 07:34:05 +00004695 case DeclarationName::CXXLiteralOperatorName:
4696 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4697 break;
4698
Douglas Gregor2cf26342009-04-09 22:27:44 +00004699 case DeclarationName::CXXUsingDirective:
4700 // No extra data to emit
4701 break;
4702 }
4703}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004704
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004705void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004706 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004707 switch (Name.getNameKind()) {
4708 case DeclarationName::CXXConstructorName:
4709 case DeclarationName::CXXDestructorName:
4710 case DeclarationName::CXXConversionFunctionName:
4711 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4712 break;
4713
4714 case DeclarationName::CXXOperatorName:
4715 AddSourceLocation(
4716 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4717 Record);
4718 AddSourceLocation(
4719 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4720 Record);
4721 break;
4722
4723 case DeclarationName::CXXLiteralOperatorName:
4724 AddSourceLocation(
4725 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4726 Record);
4727 break;
4728
4729 case DeclarationName::Identifier:
4730 case DeclarationName::ObjCZeroArgSelector:
4731 case DeclarationName::ObjCOneArgSelector:
4732 case DeclarationName::ObjCMultiArgSelector:
4733 case DeclarationName::CXXUsingDirective:
4734 break;
4735 }
4736}
4737
4738void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004739 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004740 AddDeclarationName(NameInfo.getName(), Record);
4741 AddSourceLocation(NameInfo.getLoc(), Record);
4742 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4743}
4744
4745void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004746 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004747 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004748 Record.push_back(Info.NumTemplParamLists);
4749 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4750 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4751}
4752
Sebastian Redla4232eb2010-08-18 23:56:21 +00004753void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004754 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004755 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004756 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004757 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004758
4759 // Push each of the NNS's onto a stack for serialization in reverse order.
4760 while (NNS) {
4761 NestedNames.push_back(NNS);
4762 NNS = NNS->getPrefix();
4763 }
4764
4765 Record.push_back(NestedNames.size());
4766 while(!NestedNames.empty()) {
4767 NNS = NestedNames.pop_back_val();
4768 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4769 Record.push_back(Kind);
4770 switch (Kind) {
4771 case NestedNameSpecifier::Identifier:
4772 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4773 break;
4774
4775 case NestedNameSpecifier::Namespace:
4776 AddDeclRef(NNS->getAsNamespace(), Record);
4777 break;
4778
Douglas Gregor14aba762011-02-24 02:36:08 +00004779 case NestedNameSpecifier::NamespaceAlias:
4780 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4781 break;
4782
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004783 case NestedNameSpecifier::TypeSpec:
4784 case NestedNameSpecifier::TypeSpecWithTemplate:
4785 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4786 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4787 break;
4788
4789 case NestedNameSpecifier::Global:
4790 // Don't need to write an associated value.
4791 break;
4792 }
4793 }
4794}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004795
Douglas Gregordc355712011-02-25 00:36:19 +00004796void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4797 RecordDataImpl &Record) {
4798 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004799 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004800 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004801
4802 // Push each of the nested-name-specifiers's onto a stack for
4803 // serialization in reverse order.
4804 while (NNS) {
4805 NestedNames.push_back(NNS);
4806 NNS = NNS.getPrefix();
4807 }
4808
4809 Record.push_back(NestedNames.size());
4810 while(!NestedNames.empty()) {
4811 NNS = NestedNames.pop_back_val();
4812 NestedNameSpecifier::SpecifierKind Kind
4813 = NNS.getNestedNameSpecifier()->getKind();
4814 Record.push_back(Kind);
4815 switch (Kind) {
4816 case NestedNameSpecifier::Identifier:
4817 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4818 AddSourceRange(NNS.getLocalSourceRange(), Record);
4819 break;
4820
4821 case NestedNameSpecifier::Namespace:
4822 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4823 AddSourceRange(NNS.getLocalSourceRange(), Record);
4824 break;
4825
4826 case NestedNameSpecifier::NamespaceAlias:
4827 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4828 AddSourceRange(NNS.getLocalSourceRange(), Record);
4829 break;
4830
4831 case NestedNameSpecifier::TypeSpec:
4832 case NestedNameSpecifier::TypeSpecWithTemplate:
4833 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4834 AddTypeLoc(NNS.getTypeLoc(), Record);
4835 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4836 break;
4837
4838 case NestedNameSpecifier::Global:
4839 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4840 break;
4841 }
4842 }
4843}
4844
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004845void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004846 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004847 Record.push_back(Kind);
4848 switch (Kind) {
4849 case TemplateName::Template:
4850 AddDeclRef(Name.getAsTemplateDecl(), Record);
4851 break;
4852
4853 case TemplateName::OverloadedTemplate: {
4854 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4855 Record.push_back(OvT->size());
4856 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4857 I != E; ++I)
4858 AddDeclRef(*I, Record);
4859 break;
4860 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004861
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004862 case TemplateName::QualifiedTemplate: {
4863 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4864 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4865 Record.push_back(QualT->hasTemplateKeyword());
4866 AddDeclRef(QualT->getTemplateDecl(), Record);
4867 break;
4868 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004869
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004870 case TemplateName::DependentTemplate: {
4871 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4872 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4873 Record.push_back(DepT->isIdentifier());
4874 if (DepT->isIdentifier())
4875 AddIdentifierRef(DepT->getIdentifier(), Record);
4876 else
4877 Record.push_back(DepT->getOperator());
4878 break;
4879 }
John McCall14606042011-06-30 08:33:18 +00004880
4881 case TemplateName::SubstTemplateTemplateParm: {
4882 SubstTemplateTemplateParmStorage *subst
4883 = Name.getAsSubstTemplateTemplateParm();
4884 AddDeclRef(subst->getParameter(), Record);
4885 AddTemplateName(subst->getReplacement(), Record);
4886 break;
4887 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004888
4889 case TemplateName::SubstTemplateTemplateParmPack: {
4890 SubstTemplateTemplateParmPackStorage *SubstPack
4891 = Name.getAsSubstTemplateTemplateParmPack();
4892 AddDeclRef(SubstPack->getParameterPack(), Record);
4893 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4894 break;
4895 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004896 }
4897}
4898
Michael J. Spencer20249a12010-10-21 03:16:25 +00004899void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004900 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004901 Record.push_back(Arg.getKind());
4902 switch (Arg.getKind()) {
4903 case TemplateArgument::Null:
4904 break;
4905 case TemplateArgument::Type:
4906 AddTypeRef(Arg.getAsType(), Record);
4907 break;
4908 case TemplateArgument::Declaration:
4909 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004910 Record.push_back(Arg.isDeclForReferenceParam());
4911 break;
4912 case TemplateArgument::NullPtr:
4913 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004914 break;
4915 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004916 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004917 AddTypeRef(Arg.getIntegralType(), Record);
4918 break;
4919 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004920 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4921 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004922 case TemplateArgument::TemplateExpansion:
4923 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004924 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004925 Record.push_back(*NumExpansions + 1);
4926 else
4927 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004928 break;
4929 case TemplateArgument::Expression:
4930 AddStmt(Arg.getAsExpr());
4931 break;
4932 case TemplateArgument::Pack:
4933 Record.push_back(Arg.pack_size());
4934 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4935 I != E; ++I)
4936 AddTemplateArgument(*I, Record);
4937 break;
4938 }
4939}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004940
4941void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004942ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004943 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004944 assert(TemplateParams && "No TemplateParams!");
4945 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4946 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4947 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4948 Record.push_back(TemplateParams->size());
4949 for (TemplateParameterList::const_iterator
4950 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4951 P != PEnd; ++P)
4952 AddDeclRef(*P, Record);
4953}
4954
4955/// \brief Emit a template argument list.
4956void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004957ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004958 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004959 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004960 Record.push_back(TemplateArgs->size());
4961 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004962 AddTemplateArgument(TemplateArgs->get(i), Record);
4963}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004964
Enea Zaffanellac1cef082013-08-10 07:24:53 +00004965void
4966ASTWriter::AddASTTemplateArgumentListInfo
4967(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4968 assert(ASTTemplArgList && "No ASTTemplArgList!");
4969 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4970 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4971 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4972 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4973 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4974 AddTemplateArgumentLoc(TemplArgs[i], Record);
4975}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004976
4977void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004978ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004979 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004980 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004981 I = Set.begin(), E = Set.end(); I != E; ++I) {
4982 AddDeclRef(I.getDecl(), Record);
4983 Record.push_back(I.getAccess());
4984 }
4985}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004986
Sebastian Redla4232eb2010-08-18 23:56:21 +00004987void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004988 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004989 Record.push_back(Base.isVirtual());
4990 Record.push_back(Base.isBaseOfClass());
4991 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004992 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004993 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004994 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004995 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4996 : SourceLocation(),
4997 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004998}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004999
Douglas Gregor7c789c12010-10-29 22:39:52 +00005000void ASTWriter::FlushCXXBaseSpecifiers() {
5001 RecordData Record;
5002 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5003 Record.clear();
5004
5005 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00005006 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00005007 if (Index == CXXBaseSpecifiersOffsets.size())
5008 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5009 else {
5010 if (Index > CXXBaseSpecifiersOffsets.size())
5011 CXXBaseSpecifiersOffsets.resize(Index + 1);
5012 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5013 }
5014
5015 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5016 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5017 Record.push_back(BEnd - B);
5018 for (; B != BEnd; ++B)
5019 AddCXXBaseSpecifier(*B, Record);
5020 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00005021
5022 // Flush any expressions that were written as part of the base specifiers.
5023 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00005024 }
5025
5026 CXXBaseSpecifiersToWrite.clear();
5027}
5028
Sean Huntcbb67482011-01-08 20:30:50 +00005029void ASTWriter::AddCXXCtorInitializers(
5030 const CXXCtorInitializer * const *CtorInitializers,
5031 unsigned NumCtorInitializers,
5032 RecordDataImpl &Record) {
5033 Record.push_back(NumCtorInitializers);
5034 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5035 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005036
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005037 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00005038 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00005039 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005040 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00005041 } else if (Init->isDelegatingInitializer()) {
5042 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00005043 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00005044 } else if (Init->isMemberInitializer()){
5045 Record.push_back(CTOR_INITIALIZER_MEMBER);
5046 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005047 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00005048 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5049 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005050 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00005051
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005052 AddSourceLocation(Init->getMemberLocation(), Record);
5053 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00005054 AddSourceLocation(Init->getLParenLoc(), Record);
5055 AddSourceLocation(Init->getRParenLoc(), Record);
5056 Record.push_back(Init->isWritten());
5057 if (Init->isWritten()) {
5058 Record.push_back(Init->getSourceOrder());
5059 } else {
5060 Record.push_back(Init->getNumArrayIndices());
5061 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5062 AddDeclRef(Init->getArrayIndex(i), Record);
5063 }
5064 }
5065}
5066
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005067void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5068 assert(D->DefinitionData);
5069 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005070 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005071 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005072 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005073 Record.push_back(Data.Aggregate);
5074 Record.push_back(Data.PlainOldData);
5075 Record.push_back(Data.Empty);
5076 Record.push_back(Data.Polymorphic);
5077 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005078 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005079 Record.push_back(Data.HasNoNonEmptyBases);
5080 Record.push_back(Data.HasPrivateFields);
5081 Record.push_back(Data.HasProtectedFields);
5082 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005083 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00005084 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005085 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005086 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005087 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5088 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5089 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5090 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5091 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5092 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005093 Record.push_back(Data.HasTrivialSpecialMembers);
5094 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005095 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005096 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005097 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005098 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005099 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005100 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005101 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005102 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5103 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5104 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5105 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005106 Record.push_back(Data.FailedImplicitMoveConstructor);
5107 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005108 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005109
5110 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005111 if (Data.NumBases > 0)
5112 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5113 Record);
5114
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005115 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5116 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005117 if (Data.NumVBases > 0)
5118 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5119 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005120
5121 AddUnresolvedSet(Data.Conversions, Record);
5122 AddUnresolvedSet(Data.VisibleConversions, Record);
5123 // Data.Definition is the owning decl, no need to write it.
Richard Smith4fc50892013-06-26 02:41:25 +00005124 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005125
5126 // Add lambda-specific data.
5127 if (Data.IsLambda) {
5128 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005129 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005130 Record.push_back(Lambda.NumCaptures);
5131 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005132 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005133 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005134 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005135 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5136 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5137 AddSourceLocation(Capture.getLocation(), Record);
5138 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005139 Record.push_back(Capture.getCaptureKind());
5140 switch (Capture.getCaptureKind()) {
5141 case LCK_This:
5142 break;
5143 case LCK_ByCopy:
5144 case LCK_ByRef: {
5145 VarDecl *Var =
5146 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5147 AddDeclRef(Var, Record);
5148 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5149 : SourceLocation(),
5150 Record);
5151 break;
5152 }
5153 case LCK_Init:
5154 FieldDecl *Field = Capture.getInitCaptureField();
5155 AddDeclRef(Field, Record);
5156 break;
5157 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005158 }
5159 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005160}
5161
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005162void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005163 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005164 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005165 assert(FirstDeclID == NextDeclID &&
5166 FirstTypeID == NextTypeID &&
5167 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005168 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005169 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005170 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005171 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005172
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005173 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005174
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005175 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5176 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5177 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005178 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005179 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005180 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005181 NextDeclID = FirstDeclID;
5182 NextTypeID = FirstTypeID;
5183 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005184 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005185 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005186 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005187}
5188
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005189void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005190 // Always keep the highest ID. See \p TypeRead() for more information.
5191 IdentID &StoredID = IdentifierIDs[II];
5192 if (ID > StoredID)
5193 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005194}
5195
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005196void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005197 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005198 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005199 if (ID > StoredID)
5200 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005201}
5202
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005203void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005204 // Always take the highest-numbered type index. This copes with an interesting
5205 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005206 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005207 // keep the higher-numbered entry so that we can properly write it out to
5208 // the AST file.
5209 TypeIdx &StoredIdx = TypeIdxs[T];
5210 if (Idx.getIndex() >= StoredIdx.getIndex())
5211 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005212}
5213
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005214void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005215 // Always keep the highest ID. See \p TypeRead() for more information.
5216 SelectorID &StoredID = SelectorIDs[S];
5217 if (ID > StoredID)
5218 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005219}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005220
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005221void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005222 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005223 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005224 MacroDefinitions[MD] = ID;
5225}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005226
Douglas Gregora015cab2011-12-02 17:30:13 +00005227void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5228 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5229 SubmoduleIDs[Mod] = ID;
5230}
5231
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005232void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005233 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005234 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005235 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5236 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005237 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005238 // A forward reference was mutated into a definition. Rewrite it.
5239 // FIXME: This happens during template instantiation, should we
5240 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005241 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005242 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005243 }
5244}
Douglas Gregora8235d62012-10-09 23:05:51 +00005245
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005246void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005247 assert(!WritingAST && "Already writing the AST!");
5248
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005249 // TU and namespaces are handled elsewhere.
5250 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5251 return;
5252
Douglas Gregor919814d2011-09-09 23:01:35 +00005253 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005254 return; // Not a source decl added to a DeclContext from PCH.
5255
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005256 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005257 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005258 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005259}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005260
5261void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005262 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005263 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005264 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005265 return; // Not a source member added to a class from PCH.
5266 if (!isa<CXXMethodDecl>(D))
5267 return; // We are interested in lazily declared implicit methods.
5268
5269 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005270 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005271 UpdateRecord &Record = DeclUpdates[RD];
5272 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005273 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005274}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005275
5276void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5277 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005278 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005279 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005280 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005281 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005282 return; // Not a source specialization added to a template from PCH.
5283
5284 UpdateRecord &Record = DeclUpdates[TD];
5285 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005286 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005287}
Douglas Gregor89d99802010-11-30 06:16:57 +00005288
Larisse Voufoef4579c2013-08-06 01:03:05 +00005289void ASTWriter::AddedCXXTemplateSpecialization(
5290 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5291 // The specializations set is kept in the canonical template.
5292 assert(!WritingAST && "Already writing the AST!");
5293 TD = TD->getCanonicalDecl();
5294 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5295 return; // Not a source specialization added to a template from PCH.
5296
5297 UpdateRecord &Record = DeclUpdates[TD];
5298 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5299 Record.push_back(reinterpret_cast<uint64_t>(D));
5300}
5301
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005302void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5303 const FunctionDecl *D) {
5304 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005305 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005306 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005307 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005308 return; // Not a source specialization added to a template from PCH.
5309
5310 UpdateRecord &Record = DeclUpdates[TD];
5311 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005312 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005313}
5314
Richard Smith9dadfab2013-05-11 05:45:24 +00005315void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5316 assert(!WritingAST && "Already writing the AST!");
5317 FD = FD->getCanonicalDecl();
5318 if (!FD->isFromASTFile())
5319 return; // Not a function declared in PCH and defined outside.
5320
5321 UpdateRecord &Record = DeclUpdates[FD];
5322 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5323 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5324}
5325
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005326void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005327 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005328 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005329 return; // Declaration not imported from PCH.
5330
5331 // Implicit decl from a PCH was defined.
5332 // FIXME: Should implicit definition be a separate FunctionDecl?
5333 RewriteDecl(D);
5334}
5335
Sebastian Redlf79a7192011-04-29 08:19:30 +00005336void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005337 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005338 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005339 return;
5340
5341 // Since the actual instantiation is delayed, this really means that we need
5342 // to update the instantiation location.
5343 UpdateRecord &Record = DeclUpdates[D];
5344 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5345 AddSourceLocation(
5346 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5347}
5348
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005349void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5350 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005351 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005352 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005353 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005354
5355 assert(IFD->getDefinition() && "Category on a class without a definition?");
5356 ObjCClassesWithCategories.insert(
5357 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005358}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005359
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005360
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005361void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5362 const ObjCPropertyDecl *OrigProp,
5363 const ObjCCategoryDecl *ClassExt) {
5364 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5365 if (!D)
5366 return;
5367
5368 assert(!WritingAST && "Already writing the AST!");
5369 if (!D->isFromASTFile())
5370 return; // Declaration not imported from PCH.
5371
5372 RewriteDecl(D);
5373}