blob: 1399ce2434c100c7b3fe38b5aab1976c38079fe3 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-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 Gregore0a3a512009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057
Sebastian Redl3df5a082010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000063}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000069}
70
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000074
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Reid Kleckner8a365022013-06-24 17:51:48 +0000111void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
112 Writer.AddTypeRef(T->getOriginalType(), Record);
113 Code = TYPE_DECAYED;
114}
115
Reid Kleckner0503a872013-12-05 01:23:43 +0000116void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
117 Writer.AddTypeRef(T->getOriginalType(), Record);
118 Writer.AddTypeRef(T->getAdjustedType(), Record);
119 Code = TYPE_ADJUSTED;
120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000124 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000128 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
129 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000139 Writer.AddTypeRef(T->getPointeeType(), Record);
140 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145 Writer.AddTypeRef(T->getElementType(), Record);
146 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000147 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 VisitArrayType(T);
152 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000153 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000163 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
164 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000165 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000166 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getElementType(), Record);
171 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000172 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000178 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179}
180
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000182 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000183 FunctionType::ExtInfo C = T->getExtInfo();
184 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000185 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000186 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000187 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000188 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000189 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190}
191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000194 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195}
196
Richard Smith564417a2014-03-20 21:47:22 +0000197static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
198 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000199 Record.push_back(T->getExceptionSpecType());
200 if (T->getExceptionSpecType() == EST_Dynamic) {
201 Record.push_back(T->getNumExceptions());
202 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
203 Writer.AddTypeRef(T->getExceptionType(I), Record);
204 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
205 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000206 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
207 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
208 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000209 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
210 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000211 }
Richard Smith564417a2014-03-20 21:47:22 +0000212}
213
214void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
215 VisitFunctionType(T);
216 Record.push_back(T->getNumParams());
217 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
218 Writer.AddTypeRef(T->getParamType(I), Record);
219 Record.push_back(T->isVariadic());
220 Record.push_back(T->hasTrailingReturn());
221 Record.push_back(T->getTypeQuals());
222 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
223 addExceptionSpec(Writer, T, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000228 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000229 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000230}
John McCallb96ec562009-12-04 22:46:56 +0000231
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000233 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000234 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
235 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237}
238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000240 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000246 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000247}
248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000250 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000251 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000252 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000253}
254
Alexis Hunte852b102011-05-24 22:41:36 +0000255void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
256 Writer.AddTypeRef(T->getBaseType(), Record);
257 Writer.AddTypeRef(T->getUnderlyingType(), Record);
258 Record.push_back(T->getUTTKind());
259 Code = TYPE_UNARY_TRANSFORM;
260}
261
Richard Smith30482bc2011-02-20 03:19:35 +0000262void ASTTypeWriter::VisitAutoType(const AutoType *T) {
263 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000264 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000265 if (T->getDeducedType().isNull())
266 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000267 Code = TYPE_AUTO;
268}
269
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000271 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000272 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000273 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000274 "Cannot serialize in the middle of a type definition");
275}
276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000278 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280}
281
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000282void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000283 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000284 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000285}
286
John McCall81904512011-01-06 01:58:22 +0000287void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
288 Writer.AddTypeRef(T->getModifiedType(), Record);
289 Writer.AddTypeRef(T->getEquivalentType(), Record);
290 Record.push_back(T->getAttrKind());
291 Code = TYPE_ATTRIBUTED;
292}
293
Mike Stump11289f42009-09-09 15:08:12 +0000294void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000295ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000296 const SubstTemplateTypeParmType *T) {
297 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
298 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000299 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000300}
301
302void
Douglas Gregorada4b792011-01-14 02:55:32 +0000303ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
304 const SubstTemplateTypeParmPackType *T) {
305 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
306 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
307 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
308}
309
310void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000312 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000313 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000314 Writer.AddTemplateName(T->getTemplateName(), Record);
315 Record.push_back(T->getNumArgs());
316 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
317 ArgI != ArgE; ++ArgI)
318 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000319 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
320 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000321 : T->getCanonicalTypeInternal(),
322 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000323 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000324}
325
326void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000327ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000328 VisitArrayType(T);
329 Writer.AddStmt(T->getSizeExpr());
330 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000331 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000332}
333
334void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000335ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000336 const DependentSizedExtVectorType *T) {
337 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000338 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000339}
340
341void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000342ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343 Record.push_back(T->getDepth());
344 Record.push_back(T->getIndex());
345 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000346 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000347 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000348}
349
350void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000352 Record.push_back(T->getKeyword());
353 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
354 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000355 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
356 : T->getCanonicalTypeInternal(),
357 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000358 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000359}
360
361void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000362ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000363 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000364 Record.push_back(T->getKeyword());
365 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
366 Writer.AddIdentifierRef(T->getIdentifier(), Record);
367 Record.push_back(T->getNumArgs());
368 for (DependentTemplateSpecializationType::iterator
369 I = T->begin(), E = T->end(); I != E; ++I)
370 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000371 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000372}
373
Douglas Gregord2fa7662010-12-20 02:24:11 +0000374void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
375 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000376 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000377 Record.push_back(*NumExpansions + 1);
378 else
379 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000380 Code = TYPE_PACK_EXPANSION;
381}
382
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000383void ASTTypeWriter::VisitParenType(const ParenType *T) {
384 Writer.AddTypeRef(T->getInnerType(), Record);
385 Code = TYPE_PAREN;
386}
387
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000388void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000389 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000390 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
391 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000392 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000393}
394
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000395void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000396 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000397 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000398 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000399}
400
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000401void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000402 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000403 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000404}
405
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000406void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000407 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000408 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000409 for (const auto *I : T->quals())
410 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000411 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000412}
413
Steve Narofffb4330f2009-06-17 22:40:22 +0000414void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000415ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000416 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000417 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000418}
419
Eli Friedman0dfb8892011-10-06 23:00:33 +0000420void
421ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
422 Writer.AddTypeRef(T->getValueType(), Record);
423 Code = TYPE_ATOMIC;
424}
425
John McCall8f115c62009-10-16 21:56:05 +0000426namespace {
427
428class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000429 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000430 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000431
432public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000433 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000434 : Writer(Writer), Record(Record) { }
435
John McCall17001972009-10-18 01:05:36 +0000436#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000437#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000438 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000439#include "clang/AST/TypeLocNodes.def"
440
John McCall17001972009-10-18 01:05:36 +0000441 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
442 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000443};
444
445}
446
John McCall17001972009-10-18 01:05:36 +0000447void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
448 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000449}
John McCall17001972009-10-18 01:05:36 +0000450void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000451 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
452 if (TL.needsExtraLocalData()) {
453 Record.push_back(TL.getWrittenTypeSpec());
454 Record.push_back(TL.getWrittenSignSpec());
455 Record.push_back(TL.getWrittenWidthSpec());
456 Record.push_back(TL.hasModeAttr());
457 }
John McCall8f115c62009-10-16 21:56:05 +0000458}
John McCall17001972009-10-18 01:05:36 +0000459void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000461}
John McCall17001972009-10-18 01:05:36 +0000462void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000464}
Reid Kleckner8a365022013-06-24 17:51:48 +0000465void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
466 // nothing to do
467}
Reid Kleckner0503a872013-12-05 01:23:43 +0000468void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
469 // nothing to do
470}
John McCall17001972009-10-18 01:05:36 +0000471void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000473}
John McCall17001972009-10-18 01:05:36 +0000474void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000476}
John McCall17001972009-10-18 01:05:36 +0000477void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000482 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000483}
John McCall17001972009-10-18 01:05:36 +0000484void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
486 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
487 Record.push_back(TL.getSizeExpr() ? 1 : 0);
488 if (TL.getSizeExpr())
489 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000490}
John McCall17001972009-10-18 01:05:36 +0000491void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
492 VisitArrayTypeLoc(TL);
493}
494void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
495 VisitArrayTypeLoc(TL);
496}
497void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
498 VisitArrayTypeLoc(TL);
499}
500void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
501 DependentSizedArrayTypeLoc TL) {
502 VisitArrayTypeLoc(TL);
503}
504void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
505 DependentSizedExtVectorTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getNameLoc(), Record);
507}
508void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getNameLoc(), Record);
510}
511void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
514void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000515 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000518 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000519 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
520 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000521}
522void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
523 VisitFunctionTypeLoc(TL);
524}
525void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
526 VisitFunctionTypeLoc(TL);
527}
John McCallb96ec562009-12-04 22:46:56 +0000528void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
John McCall17001972009-10-18 01:05:36 +0000531void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
532 Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
534void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000535 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
536 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
537 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000538}
539void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000540 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
541 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
542 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
543 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000544}
545void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
Alexis Hunte852b102011-05-24 22:41:36 +0000548void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getKWLoc(), Record);
550 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
551 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
552 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
553}
Richard Smith30482bc2011-02-20 03:19:35 +0000554void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
John McCall17001972009-10-18 01:05:36 +0000557void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
560void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562}
John McCall81904512011-01-06 01:58:22 +0000563void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
564 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
565 if (TL.hasAttrOperand()) {
566 SourceRange range = TL.getAttrOperandParensRange();
567 Writer.AddSourceLocation(range.getBegin(), Record);
568 Writer.AddSourceLocation(range.getEnd(), Record);
569 }
570 if (TL.hasAttrExprOperand()) {
571 Expr *operand = TL.getAttrExprOperand();
572 Record.push_back(operand ? 1 : 0);
573 if (operand) Writer.AddStmt(operand);
574 } else if (TL.hasAttrEnumOperand()) {
575 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
576 }
577}
John McCall17001972009-10-18 01:05:36 +0000578void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
John McCallcebee162009-10-18 09:09:24 +0000581void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
582 SubstTemplateTypeParmTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
Douglas Gregorada4b792011-01-14 02:55:32 +0000585void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
586 SubstTemplateTypeParmPackTypeLoc TL) {
587 Writer.AddSourceLocation(TL.getNameLoc(), Record);
588}
John McCall17001972009-10-18 01:05:36 +0000589void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
590 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000591 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000592 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
593 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
594 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
595 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000596 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
597 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000598}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000599void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
600 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
601 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
602}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000603void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000604 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000605 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000606}
John McCalle78aac42010-03-10 03:28:59 +0000607void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
608 Writer.AddSourceLocation(TL.getNameLoc(), Record);
609}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000610void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000611 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000612 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000613 Writer.AddSourceLocation(TL.getNameLoc(), Record);
614}
John McCallc392f372010-06-11 00:33:02 +0000615void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
616 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000617 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000618 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000619 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000620 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000621 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
622 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
623 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000624 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
625 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000626}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000627void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
628 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
629}
John McCall17001972009-10-18 01:05:36 +0000630void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
631 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000632}
633void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
634 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000635 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
636 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
637 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
638 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000639}
John McCallfc93cf92009-10-22 22:37:11 +0000640void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
641 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000642}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000643void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
644 Writer.AddSourceLocation(TL.getKWLoc(), Record);
645 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
646 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
647}
John McCall8f115c62009-10-16 21:56:05 +0000648
Chris Lattner19cea4e2009-04-22 05:57:30 +0000649//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000650// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000651//===----------------------------------------------------------------------===//
652
Chris Lattner28fa4e62009-04-26 22:26:21 +0000653static void EmitBlockID(unsigned ID, const char *Name,
654 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000655 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000656 Record.clear();
657 Record.push_back(ID);
658 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
659
660 // Emit the block name if present.
661 if (Name == 0 || Name[0] == 0) return;
662 Record.clear();
663 while (*Name)
664 Record.push_back(*Name++);
665 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
666}
667
668static void EmitRecordID(unsigned ID, const char *Name,
669 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000670 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000671 Record.clear();
672 Record.push_back(ID);
673 while (*Name)
674 Record.push_back(*Name++);
675 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000676}
677
678static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000679 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000680#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000681 RECORD(STMT_STOP);
682 RECORD(STMT_NULL_PTR);
683 RECORD(STMT_NULL);
684 RECORD(STMT_COMPOUND);
685 RECORD(STMT_CASE);
686 RECORD(STMT_DEFAULT);
687 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000688 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000689 RECORD(STMT_IF);
690 RECORD(STMT_SWITCH);
691 RECORD(STMT_WHILE);
692 RECORD(STMT_DO);
693 RECORD(STMT_FOR);
694 RECORD(STMT_GOTO);
695 RECORD(STMT_INDIRECT_GOTO);
696 RECORD(STMT_CONTINUE);
697 RECORD(STMT_BREAK);
698 RECORD(STMT_RETURN);
699 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000700 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000701 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000702 RECORD(EXPR_PREDEFINED);
703 RECORD(EXPR_DECL_REF);
704 RECORD(EXPR_INTEGER_LITERAL);
705 RECORD(EXPR_FLOATING_LITERAL);
706 RECORD(EXPR_IMAGINARY_LITERAL);
707 RECORD(EXPR_STRING_LITERAL);
708 RECORD(EXPR_CHARACTER_LITERAL);
709 RECORD(EXPR_PAREN);
710 RECORD(EXPR_UNARY_OPERATOR);
711 RECORD(EXPR_SIZEOF_ALIGN_OF);
712 RECORD(EXPR_ARRAY_SUBSCRIPT);
713 RECORD(EXPR_CALL);
714 RECORD(EXPR_MEMBER);
715 RECORD(EXPR_BINARY_OPERATOR);
716 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
717 RECORD(EXPR_CONDITIONAL_OPERATOR);
718 RECORD(EXPR_IMPLICIT_CAST);
719 RECORD(EXPR_CSTYLE_CAST);
720 RECORD(EXPR_COMPOUND_LITERAL);
721 RECORD(EXPR_EXT_VECTOR_ELEMENT);
722 RECORD(EXPR_INIT_LIST);
723 RECORD(EXPR_DESIGNATED_INIT);
724 RECORD(EXPR_IMPLICIT_VALUE_INIT);
725 RECORD(EXPR_VA_ARG);
726 RECORD(EXPR_ADDR_LABEL);
727 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000728 RECORD(EXPR_CHOOSE);
729 RECORD(EXPR_GNU_NULL);
730 RECORD(EXPR_SHUFFLE_VECTOR);
731 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000732 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000733 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000734 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000735 RECORD(EXPR_OBJC_ARRAY_LITERAL);
736 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000737 RECORD(EXPR_OBJC_ENCODE);
738 RECORD(EXPR_OBJC_SELECTOR_EXPR);
739 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
740 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
741 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
742 RECORD(EXPR_OBJC_KVC_REF_EXPR);
743 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000744 RECORD(STMT_OBJC_FOR_COLLECTION);
745 RECORD(STMT_OBJC_CATCH);
746 RECORD(STMT_OBJC_FINALLY);
747 RECORD(STMT_OBJC_AT_TRY);
748 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
749 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000750 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000751 RECORD(EXPR_CXX_OPERATOR_CALL);
752 RECORD(EXPR_CXX_CONSTRUCT);
753 RECORD(EXPR_CXX_STATIC_CAST);
754 RECORD(EXPR_CXX_DYNAMIC_CAST);
755 RECORD(EXPR_CXX_REINTERPRET_CAST);
756 RECORD(EXPR_CXX_CONST_CAST);
757 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000758 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000759 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000760 RECORD(EXPR_CXX_BOOL_LITERAL);
761 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000762 RECORD(EXPR_CXX_TYPEID_EXPR);
763 RECORD(EXPR_CXX_TYPEID_TYPE);
764 RECORD(EXPR_CXX_UUIDOF_EXPR);
765 RECORD(EXPR_CXX_UUIDOF_TYPE);
766 RECORD(EXPR_CXX_THIS);
767 RECORD(EXPR_CXX_THROW);
768 RECORD(EXPR_CXX_DEFAULT_ARG);
769 RECORD(EXPR_CXX_BIND_TEMPORARY);
770 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
771 RECORD(EXPR_CXX_NEW);
772 RECORD(EXPR_CXX_DELETE);
773 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
774 RECORD(EXPR_EXPR_WITH_CLEANUPS);
775 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
776 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
777 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
778 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
779 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000780 RECORD(EXPR_CXX_NOEXCEPT);
781 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000782 RECORD(EXPR_PACK_EXPANSION);
783 RECORD(EXPR_SIZEOF_PACK);
784 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000785 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000786#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000787}
Mike Stump11289f42009-09-09 15:08:12 +0000788
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000789void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000790 RecordData Record;
791 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000792
Sebastian Redl539c5062010-08-18 23:57:32 +0000793#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
794#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000796 // Control Block.
797 BLOCK(CONTROL_BLOCK);
798 RECORD(METADATA);
799 RECORD(IMPORTS);
800 RECORD(LANGUAGE_OPTIONS);
801 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000802 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000803 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000804 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000805 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000806 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000807 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000808 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000809 RECORD(PREPROCESSOR_OPTIONS);
810
Douglas Gregor108cb222012-10-19 00:45:00 +0000811 BLOCK(INPUT_FILES_BLOCK);
812 RECORD(INPUT_FILE);
813
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000814 // AST Top-Level Block.
815 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000816 RECORD(TYPE_OFFSET);
817 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000818 RECORD(IDENTIFIER_OFFSET);
819 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000820 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000821 RECORD(SPECIAL_TYPES);
822 RECORD(STATISTICS);
823 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000824 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000825 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000826 RECORD(SELECTOR_OFFSETS);
827 RECORD(METHOD_POOL);
828 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000829 RECORD(SOURCE_LOCATION_OFFSETS);
830 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000831 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000832 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000833 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000834 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000835 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000836 RECORD(SEMA_DECL_REFS);
837 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
838 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
839 RECORD(DECL_REPLACEMENTS);
840 RECORD(UPDATE_VISIBLE);
841 RECORD(DECL_UPDATE_OFFSETS);
842 RECORD(DECL_UPDATES);
843 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
844 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000845 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000846 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000847 RECORD(FP_PRAGMA_OPTIONS);
848 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000849 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000850 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000851 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000852 RECORD(MODULE_OFFSET_MAP);
853 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000854 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000855 RECORD(FILE_SORTED_DECLS);
856 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000857 RECORD(MERGED_DECLARATIONS);
858 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000859 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000860 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000861 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000862 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000863
Chris Lattner28fa4e62009-04-26 22:26:21 +0000864 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000865 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000866 RECORD(SM_SLOC_FILE_ENTRY);
867 RECORD(SM_SLOC_BUFFER_ENTRY);
868 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000869 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000870
Chris Lattner28fa4e62009-04-26 22:26:21 +0000871 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000872 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000873 RECORD(PP_MACRO_OBJECT_LIKE);
874 RECORD(PP_MACRO_FUNCTION_LIKE);
875 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000876
Douglas Gregor12bfa382009-10-17 00:13:19 +0000877 // Decls and Types block.
878 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000879 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000880 RECORD(TYPE_COMPLEX);
881 RECORD(TYPE_POINTER);
882 RECORD(TYPE_BLOCK_POINTER);
883 RECORD(TYPE_LVALUE_REFERENCE);
884 RECORD(TYPE_RVALUE_REFERENCE);
885 RECORD(TYPE_MEMBER_POINTER);
886 RECORD(TYPE_CONSTANT_ARRAY);
887 RECORD(TYPE_INCOMPLETE_ARRAY);
888 RECORD(TYPE_VARIABLE_ARRAY);
889 RECORD(TYPE_VECTOR);
890 RECORD(TYPE_EXT_VECTOR);
891 RECORD(TYPE_FUNCTION_PROTO);
892 RECORD(TYPE_FUNCTION_NO_PROTO);
893 RECORD(TYPE_TYPEDEF);
894 RECORD(TYPE_TYPEOF_EXPR);
895 RECORD(TYPE_TYPEOF);
896 RECORD(TYPE_RECORD);
897 RECORD(TYPE_ENUM);
898 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000899 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000900 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000901 RECORD(TYPE_DECLTYPE);
902 RECORD(TYPE_ELABORATED);
903 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
904 RECORD(TYPE_UNRESOLVED_USING);
905 RECORD(TYPE_INJECTED_CLASS_NAME);
906 RECORD(TYPE_OBJC_OBJECT);
907 RECORD(TYPE_TEMPLATE_TYPE_PARM);
908 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
909 RECORD(TYPE_DEPENDENT_NAME);
910 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
911 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
912 RECORD(TYPE_PAREN);
913 RECORD(TYPE_PACK_EXPANSION);
914 RECORD(TYPE_ATTRIBUTED);
915 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000916 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000917 RECORD(DECL_TYPEDEF);
918 RECORD(DECL_ENUM);
919 RECORD(DECL_RECORD);
920 RECORD(DECL_ENUM_CONSTANT);
921 RECORD(DECL_FUNCTION);
922 RECORD(DECL_OBJC_METHOD);
923 RECORD(DECL_OBJC_INTERFACE);
924 RECORD(DECL_OBJC_PROTOCOL);
925 RECORD(DECL_OBJC_IVAR);
926 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000927 RECORD(DECL_OBJC_CATEGORY);
928 RECORD(DECL_OBJC_CATEGORY_IMPL);
929 RECORD(DECL_OBJC_IMPLEMENTATION);
930 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
931 RECORD(DECL_OBJC_PROPERTY);
932 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000933 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000934 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000935 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000936 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000937 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000938 RECORD(DECL_FILE_SCOPE_ASM);
939 RECORD(DECL_BLOCK);
940 RECORD(DECL_CONTEXT_LEXICAL);
941 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000942 RECORD(DECL_NAMESPACE);
943 RECORD(DECL_NAMESPACE_ALIAS);
944 RECORD(DECL_USING);
945 RECORD(DECL_USING_SHADOW);
946 RECORD(DECL_USING_DIRECTIVE);
947 RECORD(DECL_UNRESOLVED_USING_VALUE);
948 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
949 RECORD(DECL_LINKAGE_SPEC);
950 RECORD(DECL_CXX_RECORD);
951 RECORD(DECL_CXX_METHOD);
952 RECORD(DECL_CXX_CONSTRUCTOR);
953 RECORD(DECL_CXX_DESTRUCTOR);
954 RECORD(DECL_CXX_CONVERSION);
955 RECORD(DECL_ACCESS_SPEC);
956 RECORD(DECL_FRIEND);
957 RECORD(DECL_FRIEND_TEMPLATE);
958 RECORD(DECL_CLASS_TEMPLATE);
959 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
960 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000961 RECORD(DECL_VAR_TEMPLATE);
962 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
963 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000964 RECORD(DECL_FUNCTION_TEMPLATE);
965 RECORD(DECL_TEMPLATE_TYPE_PARM);
966 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
967 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
968 RECORD(DECL_STATIC_ASSERT);
969 RECORD(DECL_CXX_BASE_SPECIFIERS);
970 RECORD(DECL_INDIRECTFIELD);
971 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
972
Douglas Gregor03412ba2011-06-03 02:27:19 +0000973 // Statements and Exprs can occur in the Decls and Types block.
974 AddStmtsExprs(Stream, Record);
975
Douglas Gregor92a96f52011-02-08 21:58:10 +0000976 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000977 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000978 RECORD(PPD_MACRO_DEFINITION);
979 RECORD(PPD_INCLUSION_DIRECTIVE);
980
Chris Lattner28fa4e62009-04-26 22:26:21 +0000981#undef RECORD
982#undef BLOCK
983 Stream.ExitBlock();
984}
985
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000986/// \brief Adjusts the given filename to only write out the portion of the
987/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000988///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000989/// \param Filename the file name to adjust.
990///
991/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
992/// the returned filename will be adjusted by this system root.
993///
994/// \returns either the original filename (if it needs no adjustment) or the
995/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000996static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000997adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000998 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregorc567ba22011-07-22 16:35:34 +00001000 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001001 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001002
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001003 // Verify that the filename and the system root have the same prefix.
1004 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001005 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001006 if (Filename[Pos] != isysroot[Pos])
1007 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001008
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001009 // We hit the end of the filename before we hit the end of the system root.
1010 if (!Filename[Pos])
1011 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001012
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001013 // If the file name has a '/' at the current position, skip over the '/'.
1014 // We distinguish sysroot-based includes from absolute includes by the
1015 // absence of '/' at the beginning of sysroot-based includes.
1016 if (Filename[Pos] == '/')
1017 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001018
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001019 return Filename + Pos;
1020}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001021
Douglas Gregor112b9072012-10-18 05:31:06 +00001022/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001023void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1024 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001025 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001026 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001027 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1028 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001029
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001030 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001031 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1032 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1033 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1035 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1036 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1037 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1038 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1039 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1040 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1041 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001042 Record.push_back(VERSION_MAJOR);
1043 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001044 Record.push_back(CLANG_VERSION_MAJOR);
1045 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001046 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001047 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001048 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1049 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001050
Douglas Gregor112b9072012-10-18 05:31:06 +00001051 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001052 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001053 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001054 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001055
1056 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1057 M != MEnd; ++M) {
1058 // Skip modules that weren't directly imported.
1059 if (!(*M)->isDirectlyImported())
1060 continue;
1061
1062 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001063 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001064 Record.push_back((*M)->File->getSize());
1065 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001066 // FIXME: This writes the absolute path for AST files we depend on.
1067 const std::string &FileName = (*M)->FileName;
1068 Record.push_back(FileName.size());
1069 Record.append(FileName.begin(), FileName.end());
1070 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001071 Stream.EmitRecord(IMPORTS, Record);
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Douglas Gregor112b9072012-10-18 05:31:06 +00001074 // Language options.
1075 Record.clear();
1076 const LangOptions &LangOpts = Context.getLangOpts();
1077#define LANGOPT(Name, Bits, Default, Description) \
1078 Record.push_back(LangOpts.Name);
1079#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1080 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1081#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001082#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1083#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001084
1085 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1086 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1087
1088 Record.push_back(LangOpts.CurrentModule.size());
1089 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001090
1091 // Comment options.
1092 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1093 for (CommentOptions::BlockCommandNamesTy::const_iterator
1094 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1095 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1096 I != IEnd; ++I) {
1097 AddString(*I, Record);
1098 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001099 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001100
Douglas Gregor112b9072012-10-18 05:31:06 +00001101 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1102
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001103 // Target options.
1104 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001105 const TargetInfo &Target = Context.getTargetInfo();
1106 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001107 AddString(TargetOpts.Triple, Record);
1108 AddString(TargetOpts.CPU, Record);
1109 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001110 AddString(TargetOpts.LinkerVersion, Record);
1111 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1112 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1113 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1114 }
1115 Record.push_back(TargetOpts.Features.size());
1116 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1117 AddString(TargetOpts.Features[I], Record);
1118 }
1119 Stream.EmitRecord(TARGET_OPTIONS, Record);
1120
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001121 // Diagnostic options.
1122 Record.clear();
1123 const DiagnosticOptions &DiagOpts
1124 = Context.getDiagnostics().getDiagnosticOptions();
1125#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1126#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1127 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1128#include "clang/Basic/DiagnosticOptions.def"
1129 Record.push_back(DiagOpts.Warnings.size());
1130 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1131 AddString(DiagOpts.Warnings[I], Record);
1132 // Note: we don't serialize the log or serialization file names, because they
1133 // are generally transient files and will almost always be overridden.
1134 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1135
Douglas Gregorc6317db2012-10-24 15:49:58 +00001136 // File system options.
1137 Record.clear();
1138 const FileSystemOptions &FSOpts
1139 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1140 AddString(FSOpts.WorkingDir, Record);
1141 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1142
Douglas Gregor2d302362012-10-24 16:50:34 +00001143 // Header search options.
1144 Record.clear();
1145 const HeaderSearchOptions &HSOpts
1146 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1147 AddString(HSOpts.Sysroot, Record);
1148
1149 // Include entries.
1150 Record.push_back(HSOpts.UserEntries.size());
1151 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1152 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1153 AddString(Entry.Path, Record);
1154 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001155 Record.push_back(Entry.IsFramework);
1156 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001157 }
1158
1159 // System header prefixes.
1160 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1161 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1162 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1163 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1164 }
1165
1166 AddString(HSOpts.ResourceDir, Record);
1167 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001168 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001169 Record.push_back(HSOpts.DisableModuleHash);
1170 Record.push_back(HSOpts.UseBuiltinIncludes);
1171 Record.push_back(HSOpts.UseStandardSystemIncludes);
1172 Record.push_back(HSOpts.UseStandardCXXIncludes);
1173 Record.push_back(HSOpts.UseLibcxx);
1174 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1175
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001176 // Preprocessor options.
1177 Record.clear();
1178 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1179
1180 // Macro definitions.
1181 Record.push_back(PPOpts.Macros.size());
1182 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1183 AddString(PPOpts.Macros[I].first, Record);
1184 Record.push_back(PPOpts.Macros[I].second);
1185 }
1186
1187 // Includes
1188 Record.push_back(PPOpts.Includes.size());
1189 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1190 AddString(PPOpts.Includes[I], Record);
1191
1192 // Macro includes
1193 Record.push_back(PPOpts.MacroIncludes.size());
1194 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1195 AddString(PPOpts.MacroIncludes[I], Record);
1196
Douglas Gregorb6368752012-10-24 23:41:50 +00001197 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001198 // Detailed record is important since it is used for the module cache hash.
1199 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001200 AddString(PPOpts.ImplicitPCHInclude, Record);
1201 AddString(PPOpts.ImplicitPTHInclude, Record);
1202 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1203 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1204
Douglas Gregora3b20262011-05-06 21:43:30 +00001205 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001206 SourceManager &SM = Context.getSourceManager();
1207 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1208 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001209 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1210 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001211 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1212 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1213
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001214 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001215
Michael J. Spencer740857f2010-12-21 16:45:57 +00001216 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001217
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001218 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001219 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001220 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001221 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001222 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001223 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001224 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001225 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001226
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001227 Record.clear();
1228 Record.push_back(SM.getMainFileID().getOpaqueValue());
1229 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1230
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001231 // Original PCH directory
1232 if (!OutputFile.empty() && OutputFile != "-") {
1233 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1234 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1236 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1237
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001238 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001239
1240 llvm::sys::fs::make_absolute(OutputPath);
1241 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1242
1243 RecordData Record;
1244 Record.push_back(ORIGINAL_PCH_DIR);
1245 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1246 }
1247
Douglas Gregor49491f72013-03-15 22:15:07 +00001248 WriteInputFiles(Context.SourceMgr,
1249 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001250 isysroot,
1251 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001252 Stream.ExitBlock();
1253}
1254
Douglas Gregor49491f72013-03-15 22:15:07 +00001255namespace {
1256 /// \brief An input file.
1257 struct InputFileEntry {
1258 const FileEntry *File;
1259 bool IsSystemFile;
1260 bool BufferOverridden;
1261 };
1262}
1263
1264void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1265 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001266 StringRef isysroot,
1267 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001268 using namespace llvm;
1269 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1270 RecordData Record;
1271
1272 // Create input-file abbreviation.
1273 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1274 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001275 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001276 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1277 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001278 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001279 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1280 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1281
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001282 // Get all ContentCache objects for files, sorted by whether the file is a
1283 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001284 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001285 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1286 // Get this source location entry.
1287 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001288 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001289
1290 // We only care about file entries that were not overridden.
1291 if (!SLoc->isFile())
1292 continue;
1293 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001294 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001295 continue;
1296
Douglas Gregor49491f72013-03-15 22:15:07 +00001297 InputFileEntry Entry;
1298 Entry.File = Cache->OrigEntry;
1299 Entry.IsSystemFile = Cache->IsSystemFile;
1300 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001301 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001302 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001303 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001304 SortedFiles.push_front(Entry);
1305 }
1306
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001307 unsigned UserFilesNum = 0;
1308 // Write out all of the input files.
1309 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001310 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001311 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001312 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001313
Douglas Gregor49491f72013-03-15 22:15:07 +00001314 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001315 if (InputFileID != 0)
1316 continue; // already recorded this file.
1317
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001318 // Record this entry's offset.
1319 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001320
1321 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001322
Douglas Gregor49491f72013-03-15 22:15:07 +00001323 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001324 ++UserFilesNum;
1325
Douglas Gregor72be3902012-10-19 00:38:02 +00001326 Record.clear();
1327 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001328 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001329
1330 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001331 Record.push_back(Entry.File->getSize());
1332 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001333
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001334 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001335 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001336
Douglas Gregor72be3902012-10-19 00:38:02 +00001337 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001338 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001339 SmallString<128> FilePath(Filename);
1340
1341 // Ask the file manager to fixup the relative path for us. This will
1342 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001343 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001344
1345 // FIXME: This call to make_absolute shouldn't be necessary, the
1346 // call to FixupRelativePath should always return an absolute path.
1347 llvm::sys::fs::make_absolute(FilePath);
1348 Filename = FilePath.c_str();
1349
1350 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1351
1352 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1353 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001354
Douglas Gregor112b9072012-10-18 05:31:06 +00001355 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001356
1357 // Create input file offsets abbreviation.
1358 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1359 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1360 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001361 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1362 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001363 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1364 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1365
1366 // Write input file offsets.
1367 Record.clear();
1368 Record.push_back(INPUT_FILE_OFFSETS);
1369 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001370 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001371 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001372}
1373
Douglas Gregora7f71a92009-04-10 03:52:48 +00001374//===----------------------------------------------------------------------===//
1375// Source Manager Serialization
1376//===----------------------------------------------------------------------===//
1377
1378/// \brief Create an abbreviation for the SLocEntry that refers to a
1379/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001380static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001381 using namespace llvm;
1382 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001383 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001388 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001393 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001394}
1395
1396/// \brief Create an abbreviation for the SLocEntry that refers to a
1397/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001398static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001399 using namespace llvm;
1400 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001401 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-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
1406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001407 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001408}
1409
1410/// \brief Create an abbreviation for the SLocEntry that refers to a
1411/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001412static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001413 using namespace llvm;
1414 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001415 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001417 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001418}
1419
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001420/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1421/// expansion.
1422static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001423 using namespace llvm;
1424 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001425 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001431 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001432}
1433
Douglas Gregor09b69892011-02-10 17:09:37 +00001434namespace {
1435 // Trait used for the on-disk hash table of header search information.
1436 class HeaderFileInfoTrait {
1437 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001438 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001439
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001440 // Keep track of the framework names we've used during serialization.
1441 SmallVector<char, 128> FrameworkStringData;
1442 llvm::StringMap<unsigned> FrameworkNameOffset;
1443
Douglas Gregor09b69892011-02-10 17:09:37 +00001444 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001445 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1446 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001447
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001448 struct key_type {
1449 const FileEntry *FE;
1450 const char *Filename;
1451 };
1452 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001453
1454 typedef HeaderFileInfo data_type;
1455 typedef const data_type &data_type_ref;
1456
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001457 static unsigned ComputeHash(key_type_ref key) {
1458 // The hash is based only on size/time of the file, so that the reader can
1459 // match even when symlinking or excess path elements ("foo/../", "../")
1460 // change the form of the name. However, complete path is still the key.
1461 return llvm::hash_combine(key.FE->getSize(),
1462 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001463 }
1464
1465 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001466 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1467 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1468 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001469 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001470 if (Data.isModuleHeader)
1471 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001472 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001473 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001474 }
1475
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001476 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1477 clang::io::Emit64(Out, key.FE->getSize());
1478 KeyLen -= 8;
1479 clang::io::Emit64(Out, key.FE->getModificationTime());
1480 KeyLen -= 8;
1481 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001482 }
1483
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001484 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001485 data_type_ref Data, unsigned DataLen) {
1486 using namespace clang::io;
1487 uint64_t Start = Out.tell(); (void)Start;
1488
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001489 unsigned char Flags = (Data.HeaderRole << 6)
1490 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001491 | (Data.isPragmaOnce << 4)
1492 | (Data.DirInfo << 2)
1493 | (Data.Resolved << 1)
1494 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001495 Emit8(Out, (uint8_t)Flags);
1496 Emit16(Out, (uint16_t) Data.NumIncludes);
1497
1498 if (!Data.ControllingMacro)
1499 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1500 else
1501 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001502
1503 unsigned Offset = 0;
1504 if (!Data.Framework.empty()) {
1505 // If this header refers into a framework, save the framework name.
1506 llvm::StringMap<unsigned>::iterator Pos
1507 = FrameworkNameOffset.find(Data.Framework);
1508 if (Pos == FrameworkNameOffset.end()) {
1509 Offset = FrameworkStringData.size() + 1;
1510 FrameworkStringData.append(Data.Framework.begin(),
1511 Data.Framework.end());
1512 FrameworkStringData.push_back(0);
1513
1514 FrameworkNameOffset[Data.Framework] = Offset;
1515 } else
1516 Offset = Pos->second;
1517 }
1518 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001519
1520 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001521 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001522 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1523 }
1524
Douglas Gregor09b69892011-02-10 17:09:37 +00001525 assert(Out.tell() - Start == DataLen && "Wrong data length");
1526 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001527
1528 const char *strings_begin() const { return FrameworkStringData.begin(); }
1529 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001530 };
1531} // end anonymous namespace
1532
1533/// \brief Write the header search block for the list of files that
1534///
1535/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001536void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001537 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001538 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1539
1540 if (FilesByUID.size() > HS.header_file_size())
1541 FilesByUID.resize(HS.header_file_size());
1542
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001543 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001544 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001545 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001546 unsigned NumHeaderSearchEntries = 0;
1547 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1548 const FileEntry *File = FilesByUID[UID];
1549 if (!File)
1550 continue;
1551
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001552 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1553 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001554 HeaderFileInfo HFI;
1555 if (!HS.tryGetFileInfo(File, HFI) ||
1556 (HFI.External && Chain) ||
1557 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001558 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001559
1560 // Turn the file name into an absolute path, if it isn't already.
1561 const char *Filename = File->getName();
1562 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1563
1564 // If we performed any translation on the file name at all, we need to
1565 // save this string, since the generator will refer to it later.
1566 if (Filename != File->getName()) {
1567 Filename = strdup(Filename);
1568 SavedStrings.push_back(Filename);
1569 }
1570
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001571 HeaderFileInfoTrait::key_type key = { File, Filename };
1572 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001573 ++NumHeaderSearchEntries;
1574 }
1575
1576 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001577 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001578 uint32_t BucketOffset;
1579 {
1580 llvm::raw_svector_ostream Out(TableData);
1581 // Make sure that no bucket is at offset 0
1582 clang::io::Emit32(Out, 0);
1583 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1584 }
1585
1586 // Create a blob abbreviation
1587 using namespace llvm;
1588 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1589 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1594 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1595
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001596 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001597 RecordData Record;
1598 Record.push_back(HEADER_SEARCH_TABLE);
1599 Record.push_back(BucketOffset);
1600 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001601 Record.push_back(TableData.size());
1602 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001603 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1604
1605 // Free all of the strings we had to duplicate.
1606 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001607 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001608}
1609
Douglas Gregora7f71a92009-04-10 03:52:48 +00001610/// \brief Writes the block containing the serialized form of the
1611/// source manager.
1612///
1613/// TODO: We should probably use an on-disk hash table (stored in a
1614/// blob), indexed based on the file name, so that we only create
1615/// entries for files that we actually need. In the common case (no
1616/// errors), we probably won't have to create file entries for any of
1617/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001618void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001619 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001620 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001621 RecordData Record;
1622
Chris Lattner0910e3b2009-04-10 17:16:57 +00001623 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001624 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001625
1626 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001627 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1628 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1629 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001630 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001631
Douglas Gregor258ae542009-04-27 06:38:32 +00001632 // Write out the source location entry table. We skip the first
1633 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001634 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001635 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001636 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1637 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001638 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001639 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001640 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001641 FileID FID = FileID::get(I);
1642 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001643
Douglas Gregor258ae542009-04-27 06:38:32 +00001644 // Record the offset of this source-location entry.
1645 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1646
1647 // Figure out which record code to use.
1648 unsigned Code;
1649 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001650 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1651 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001652 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001653 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001654 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001655 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001656 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001657 Record.clear();
1658 Record.push_back(Code);
1659
Douglas Gregor925296b2011-07-19 16:10:42 +00001660 // Starting offset of this entry within this module, so skip the dummy.
1661 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001662 if (SLoc->isFile()) {
1663 const SrcMgr::FileInfo &File = SLoc->getFile();
1664 Record.push_back(File.getIncludeLoc().getRawEncoding());
1665 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1666 Record.push_back(File.hasLineDirectives());
1667
1668 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001669 if (Content->OrigEntry) {
1670 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001671 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001672
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001673 // The source location entry is a file. Emit input file ID.
1674 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1675 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001676
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001677 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001678
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001679 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001680 if (FDI != FileDeclIDs.end()) {
1681 Record.push_back(FDI->second->FirstDeclIndex);
1682 Record.push_back(FDI->second->DeclIDs.size());
1683 } else {
1684 Record.push_back(0);
1685 Record.push_back(0);
1686 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001687
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001688 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001689
1690 if (Content->BufferOverridden) {
1691 Record.clear();
1692 Record.push_back(SM_SLOC_BUFFER_BLOB);
1693 const llvm::MemoryBuffer *Buffer
1694 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1695 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1696 StringRef(Buffer->getBufferStart(),
1697 Buffer->getBufferSize() + 1));
1698 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001699 } else {
1700 // The source location entry is a buffer. The blob associated
1701 // with this entry contains the contents of the buffer.
1702
1703 // We add one to the size so that we capture the trailing NULL
1704 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1705 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001706 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001707 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001708 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001709 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001710 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001711 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001712 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001713 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001714 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001715 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001716
Douglas Gregor925296b2011-07-19 16:10:42 +00001717 if (strcmp(Name, "<built-in>") == 0) {
1718 PreloadSLocs.push_back(SLocEntryOffsets.size());
1719 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001720 }
1721 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001722 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001723 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001724 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1725 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001726 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1727 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001728
1729 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001730 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001731 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001732 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001733 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001734 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001735 }
1736 }
1737
Douglas Gregor8f45df52009-04-16 22:23:12 +00001738 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001739
1740 if (SLocEntryOffsets.empty())
1741 return;
1742
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001743 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001744 // table is used for lazily loading source-location information.
1745 using namespace llvm;
1746 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001747 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1751 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001752
Douglas Gregor258ae542009-04-27 06:38:32 +00001753 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001754 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001755 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001756 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001757 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001758
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001759 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001760 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001761 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001762
1763 // Write the line table. It depends on remapping working, so it must come
1764 // after the source location offsets.
1765 if (SourceMgr.hasLineTable()) {
1766 LineTableInfo &LineTable = SourceMgr.getLineTable();
1767
1768 Record.clear();
1769 // Emit the file names
1770 Record.push_back(LineTable.getNumFilenames());
1771 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1772 // Emit the file name
1773 const char *Filename = LineTable.getFilename(I);
1774 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1775 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1776 Record.push_back(FilenameLen);
1777 if (FilenameLen)
1778 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1779 }
1780
1781 // Emit the line entries
1782 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1783 L != LEnd; ++L) {
1784 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001785 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001786 continue;
1787
1788 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001789 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001790
1791 // Emit the line entries
1792 Record.push_back(L->second.size());
1793 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1794 LEEnd = L->second.end();
1795 LE != LEEnd; ++LE) {
1796 Record.push_back(LE->FileOffset);
1797 Record.push_back(LE->LineNo);
1798 Record.push_back(LE->FilenameID);
1799 Record.push_back((unsigned)LE->FileKind);
1800 Record.push_back(LE->IncludeOffset);
1801 }
1802 }
1803 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1804 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001805}
1806
Douglas Gregorc5046832009-04-27 18:38:38 +00001807//===----------------------------------------------------------------------===//
1808// Preprocessor Serialization
1809//===----------------------------------------------------------------------===//
1810
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001811namespace {
1812class ASTMacroTableTrait {
1813public:
1814 typedef IdentID key_type;
1815 typedef key_type key_type_ref;
1816
1817 struct Data {
1818 uint32_t MacroDirectivesOffset;
1819 };
1820
1821 typedef Data data_type;
1822 typedef const data_type &data_type_ref;
1823
1824 static unsigned ComputeHash(IdentID IdID) {
1825 return llvm::hash_value(IdID);
1826 }
1827
1828 std::pair<unsigned,unsigned>
1829 static EmitKeyDataLength(raw_ostream& Out,
1830 key_type_ref Key, data_type_ref Data) {
1831 unsigned KeyLen = 4; // IdentID.
1832 unsigned DataLen = 4; // MacroDirectivesOffset.
1833 return std::make_pair(KeyLen, DataLen);
1834 }
1835
1836 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1837 clang::io::Emit32(Out, Key);
1838 }
1839
1840 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1841 unsigned) {
1842 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1843 }
1844};
1845} // end anonymous namespace
1846
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001847static int compareMacroDirectives(
1848 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1849 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1850 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001851}
1852
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001853static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1854 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001855 if (MacroInfo *MI = MD->getMacroInfo())
1856 if (MI->isBuiltinMacro())
1857 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001858
1859 if (IsModule) {
1860 SourceLocation Loc = MD->getLocation();
1861 if (Loc.isInvalid())
1862 return true;
1863 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1864 return true;
1865 }
1866
1867 return false;
1868}
1869
Chris Lattnereeffaef2009-04-10 17:15:23 +00001870/// \brief Writes the block containing the serialized form of the
1871/// preprocessor.
1872///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001873void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001874 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1875 if (PPRec)
1876 WritePreprocessorDetail(*PPRec);
1877
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001878 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001879
Chris Lattner0af3ba12009-04-13 01:29:17 +00001880 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1881 if (PP.getCounterValue() != 0) {
1882 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001883 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001884 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001885 }
1886
1887 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001888 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001889
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001890 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001891 // FIXME: use diagnostics subsystem for localization etc.
1892 if (PP.SawDateOrTime())
1893 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001894
Douglas Gregor796d76a2010-10-20 22:00:55 +00001895
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001896 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001897 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001898
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001899 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001900 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001901 MacroDirectives;
1902 for (Preprocessor::macro_iterator
1903 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1904 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001905 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001906 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001907 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001908
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001909 // Sort the set of macro definitions that need to be serialized by the
1910 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001911 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1912 &compareMacroDirectives);
1913
1914 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1915
1916 // Emit the macro directives as a list and associate the offset with the
1917 // identifier they belong to.
1918 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1919 const IdentifierInfo *Name = MacroDirectives[I].first;
1920 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1921 MacroDirective *MD = MacroDirectives[I].second;
1922
1923 // If the macro or identifier need no updates, don't write the macro history
1924 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001925 // FIXME: Chain the macro history instead of re-writing it.
1926 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001927 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1928 continue;
1929
1930 // Emit the macro directives in reverse source order.
1931 for (; MD; MD = MD->getPrevious()) {
1932 if (shouldIgnoreMacro(MD, IsModule, PP))
1933 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001934
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001935 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001936 Record.push_back(MD->getKind());
1937 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1938 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1939 Record.push_back(InfoID);
1940 Record.push_back(DefMD->isImported());
1941 Record.push_back(DefMD->isAmbiguous());
1942
1943 } else if (VisibilityMacroDirective *
1944 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1945 Record.push_back(VisMD->isPublic());
1946 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001947 }
1948 if (Record.empty())
1949 continue;
1950
1951 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1952 Record.clear();
1953
1954 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1955
1956 IdentID NameID = getIdentifierRef(Name);
1957 ASTMacroTableTrait::Data data;
1958 data.MacroDirectivesOffset = MacroDirectiveOffset;
1959 Generator.insert(NameID, data);
1960 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001961
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001962 /// \brief Offsets of each of the macros into the bitstream, indexed by
1963 /// the local macro ID
1964 ///
1965 /// For each identifier that is associated with a macro, this map
1966 /// provides the offset into the bitstream where that macro is
1967 /// defined.
1968 std::vector<uint32_t> MacroOffsets;
1969
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001970 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1971 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1972 MacroInfo *MI = MacroInfosToEmit[I].MI;
1973 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001974
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001975 if (ID < FirstMacroID) {
1976 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1977 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001978 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001979
1980 // Record the local offset of this macro.
1981 unsigned Index = ID - FirstMacroID;
1982 if (Index == MacroOffsets.size())
1983 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1984 else {
1985 if (Index > MacroOffsets.size())
1986 MacroOffsets.resize(Index + 1);
1987
1988 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1989 }
1990
1991 AddIdentifierRef(Name, Record);
1992 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1993 AddSourceLocation(MI->getDefinitionLoc(), Record);
1994 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1995 Record.push_back(MI->isUsed());
1996 unsigned Code;
1997 if (MI->isObjectLike()) {
1998 Code = PP_MACRO_OBJECT_LIKE;
1999 } else {
2000 Code = PP_MACRO_FUNCTION_LIKE;
2001
2002 Record.push_back(MI->isC99Varargs());
2003 Record.push_back(MI->isGNUVarargs());
2004 Record.push_back(MI->hasCommaPasting());
2005 Record.push_back(MI->getNumArgs());
2006 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2007 I != E; ++I)
2008 AddIdentifierRef(*I, Record);
2009 }
2010
2011 // If we have a detailed preprocessing record, record the macro definition
2012 // ID that corresponds to this macro.
2013 if (PPRec)
2014 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2015
2016 Stream.EmitRecord(Code, Record);
2017 Record.clear();
2018
2019 // Emit the tokens array.
2020 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2021 // Note that we know that the preprocessor does not have any annotation
2022 // tokens in it because they are created by the parser, and thus can't
2023 // be in a macro definition.
2024 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002025 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002026 Stream.EmitRecord(PP_TOKEN, Record);
2027 Record.clear();
2028 }
2029 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002030 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002031
Douglas Gregor92a96f52011-02-08 21:58:10 +00002032 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002033
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002034 // Create the on-disk hash table in a buffer.
2035 SmallString<4096> MacroTable;
2036 uint32_t BucketOffset;
2037 {
2038 llvm::raw_svector_ostream Out(MacroTable);
2039 // Make sure that no bucket is at offset 0
2040 clang::io::Emit32(Out, 0);
2041 BucketOffset = Generator.Emit(Out);
2042 }
2043
2044 // Write the macro table
2045 using namespace llvm;
2046 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2047 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2050 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2051
2052 Record.push_back(MACRO_TABLE);
2053 Record.push_back(BucketOffset);
2054 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2055 Record.clear();
2056
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002057 // Write the offsets table for macro IDs.
2058 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002059 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002060 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2064
2065 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2066 Record.clear();
2067 Record.push_back(MACRO_OFFSET);
2068 Record.push_back(MacroOffsets.size());
2069 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2070 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2071 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002072}
2073
2074void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002075 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002076 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002077
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002078 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002079
Douglas Gregor92a96f52011-02-08 21:58:10 +00002080 // Enter the preprocessor block.
2081 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002082
Douglas Gregoraae92242010-03-19 21:51:54 +00002083 // If the preprocessor has a preprocessing record, emit it.
2084 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002085 using namespace llvm;
2086
2087 // Set up the abbreviation for
2088 unsigned InclusionAbbrev = 0;
2089 {
2090 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2091 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002092 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2097 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2098 }
2099
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002100 unsigned FirstPreprocessorEntityID
2101 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2102 + NUM_PREDEF_PP_ENTITY_IDS;
2103 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002104 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002105 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2106 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002107 E != EEnd;
2108 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002109 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002110
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002111 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2112 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002113
Douglas Gregor92a96f52011-02-08 21:58:10 +00002114 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002115 // Record this macro definition's ID.
2116 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002117
Douglas Gregor92a96f52011-02-08 21:58:10 +00002118 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002119 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2120 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002121 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002122
Chandler Carrutha88a22182011-07-14 08:20:46 +00002123 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002124 Record.push_back(ME->isBuiltinMacro());
2125 if (ME->isBuiltinMacro())
2126 AddIdentifierRef(ME->getName(), Record);
2127 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002128 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002129 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002130 continue;
2131 }
2132
2133 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2134 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002135 Record.push_back(ID->getFileName().size());
2136 Record.push_back(ID->wasInQuotes());
2137 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002138 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002139 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002140 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002141 // Check that the FileEntry is not null because it was not resolved and
2142 // we create a PCH even with compiler errors.
2143 if (ID->getFile())
2144 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002145 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2146 continue;
2147 }
2148
2149 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2150 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002151 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002152
Douglas Gregoraae92242010-03-19 21:51:54 +00002153 // Write the offsets table for the preprocessing record.
2154 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002155 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2156
Douglas Gregoraae92242010-03-19 21:51:54 +00002157 // Write the offsets table for identifier IDs.
2158 using namespace llvm;
2159 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002160 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002163 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002164
Douglas Gregoraae92242010-03-19 21:51:54 +00002165 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002166 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002167 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002168 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2169 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002170 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002171}
2172
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002173unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2174 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2175 if (Known != SubmoduleIDs.end())
2176 return Known->second;
2177
2178 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2179}
2180
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002181unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2182 if (!Mod)
2183 return 0;
2184
2185 llvm::DenseMap<Module *, unsigned>::const_iterator
2186 Known = SubmoduleIDs.find(Mod);
2187 if (Known != SubmoduleIDs.end())
2188 return Known->second;
2189
2190 return 0;
2191}
2192
Douglas Gregor253eefe2011-12-01 00:59:36 +00002193/// \brief Compute the number of modules within the given tree (including the
2194/// given module).
2195static unsigned getNumberOfModules(Module *Mod) {
2196 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002197 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2198 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002199 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002200 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002201
2202 return ChildModules + 1;
2203}
2204
Douglas Gregorde3ef502011-11-30 23:21:26 +00002205void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002206 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002207 // FIXME: This feels like it belongs somewhere else, but there are no
2208 // other consumers of this information.
2209 SourceManager &SrcMgr = PP->getSourceManager();
2210 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002211 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002212 if (Module *ImportedFrom
2213 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2214 SrcMgr))) {
2215 ImportedFrom->Imports.push_back(I->getImportedModule());
2216 }
2217 }
2218
Douglas Gregor69021972011-11-30 17:33:56 +00002219 // Enter the submodule description block.
2220 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2221
2222 // Write the abbreviations needed for the submodules block.
2223 using namespace llvm;
2224 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2225 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2237 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2238
2239 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002240 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2242 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2243
2244 Abbrev = new BitCodeAbbrev();
2245 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2247 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002248
2249 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002250 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2252 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2253
2254 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002255 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2257 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2258
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002259 Abbrev = new BitCodeAbbrev();
2260 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002263 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2264
Douglas Gregor59527662012-10-15 06:28:11 +00002265 Abbrev = new BitCodeAbbrev();
2266 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2268 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2269
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002270 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002271 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2273 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2274
2275 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002276 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2279 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2280
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002281 Abbrev = new BitCodeAbbrev();
2282 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2284 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2285
Douglas Gregorfb912652013-03-20 21:10:35 +00002286 Abbrev = new BitCodeAbbrev();
2287 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2290 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2291
Douglas Gregor253eefe2011-12-01 00:59:36 +00002292 // Write the submodule metadata block.
2293 RecordData Record;
2294 Record.push_back(getNumberOfModules(WritingModule));
2295 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2296 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2297
Douglas Gregor69021972011-11-30 17:33:56 +00002298 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002299 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002300 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002301 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002302 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002303 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002304 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002305
2306 // Emit the definition of the block.
2307 Record.clear();
2308 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002309 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002310 if (Mod->Parent) {
2311 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2312 Record.push_back(SubmoduleIDs[Mod->Parent]);
2313 } else {
2314 Record.push_back(0);
2315 }
2316 Record.push_back(Mod->IsFramework);
2317 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002318 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002319 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002320 Record.push_back(Mod->InferSubmodules);
2321 Record.push_back(Mod->InferExplicitSubmodules);
2322 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002323 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002324 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2325
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002326 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002327 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002328 Record.clear();
2329 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002330 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002331 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002332 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002333 }
2334
Douglas Gregor69021972011-11-30 17:33:56 +00002335 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002336 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002337 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002338 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002339 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002340 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002341 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2342 Record.clear();
2343 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2344 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2345 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002346 }
2347
2348 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002349 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002350 Record.clear();
2351 Record.push_back(SUBMODULE_HEADER);
2352 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002353 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002354 }
Douglas Gregor59527662012-10-15 06:28:11 +00002355 // Emit the excluded headers.
2356 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2357 Record.clear();
2358 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2359 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2360 Mod->ExcludedHeaders[I]->getName());
2361 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002362 // Emit the private headers.
2363 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2364 Record.clear();
2365 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2366 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2367 Mod->PrivateHeaders[I]->getName());
2368 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002369 ArrayRef<const FileEntry *>
2370 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2371 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002372 Record.clear();
2373 Record.push_back(SUBMODULE_TOPHEADER);
2374 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002375 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002376 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002377
2378 // Emit the imports.
2379 if (!Mod->Imports.empty()) {
2380 Record.clear();
2381 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002382 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002383 assert(ImportedID && "Unknown submodule!");
2384 Record.push_back(ImportedID);
2385 }
2386 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2387 }
2388
Douglas Gregor24bb9232011-12-02 18:58:38 +00002389 // Emit the exports.
2390 if (!Mod->Exports.empty()) {
2391 Record.clear();
2392 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002393 if (Module *Exported = Mod->Exports[I].getPointer()) {
2394 unsigned ExportedID = SubmoduleIDs[Exported];
2395 assert(ExportedID > 0 && "Unknown submodule ID?");
2396 Record.push_back(ExportedID);
2397 } else {
2398 Record.push_back(0);
2399 }
2400
Douglas Gregor24bb9232011-12-02 18:58:38 +00002401 Record.push_back(Mod->Exports[I].getInt());
2402 }
2403 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2404 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002405
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002406 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2407 // Might be unnecessary as use declarations are only used to build the
2408 // module itself.
2409
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002410 // Emit the link libraries.
2411 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2412 Record.clear();
2413 Record.push_back(SUBMODULE_LINK_LIBRARY);
2414 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2415 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2416 Mod->LinkLibraries[I].Library);
2417 }
2418
Douglas Gregorfb912652013-03-20 21:10:35 +00002419 // Emit the conflicts.
2420 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2421 Record.clear();
2422 Record.push_back(SUBMODULE_CONFLICT);
2423 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2424 assert(OtherID && "Unknown submodule!");
2425 Record.push_back(OtherID);
2426 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2427 Mod->Conflicts[I].Message);
2428 }
2429
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002430 // Emit the configuration macros.
2431 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2432 Record.clear();
2433 Record.push_back(SUBMODULE_CONFIG_MACRO);
2434 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2435 Mod->ConfigMacros[I]);
2436 }
2437
Douglas Gregor69021972011-11-30 17:33:56 +00002438 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002439 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2440 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002441 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002442 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002443 }
2444
2445 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002446
2447 assert((NextSubmoduleID - FirstSubmoduleID
2448 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002449}
2450
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002451serialization::SubmoduleID
2452ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002453 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002454 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002455
2456 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002457 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002458 Module *OwningMod
2459 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002460 if (!OwningMod)
2461 return 0;
2462
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002463 // Check whether this submodule is part of our own module.
2464 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002465 return 0;
2466
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002467 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002468}
2469
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002470void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2471 bool isModule) {
2472 // Make sure set diagnostic pragmas don't affect the translation unit that
2473 // imports the module.
2474 // FIXME: Make diagnostic pragma sections work properly with modules.
2475 if (isModule)
2476 return;
2477
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002478 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2479 DiagStateIDMap;
2480 unsigned CurrID = 0;
2481 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002482 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002483 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002484 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2485 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002486 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002487 if (point.Loc.isInvalid())
2488 continue;
2489
2490 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002491 unsigned &DiagStateID = DiagStateIDMap[point.State];
2492 Record.push_back(DiagStateID);
2493
2494 if (DiagStateID == 0) {
2495 DiagStateID = ++CurrID;
2496 for (DiagnosticsEngine::DiagState::const_iterator
2497 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2498 if (I->second.isPragma()) {
2499 Record.push_back(I->first);
2500 Record.push_back(I->second.getMapping());
2501 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002502 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002503 Record.push_back(-1); // mark the end of the diag/map pairs for this
2504 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002505 }
2506 }
2507
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002508 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002509 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002510}
2511
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002512void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2513 if (CXXBaseSpecifiersOffsets.empty())
2514 return;
2515
2516 RecordData Record;
2517
2518 // Create a blob abbreviation for the C++ base specifiers offsets.
2519 using namespace llvm;
2520
2521 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2522 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2525 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2526
Douglas Gregorc27b2872011-08-04 00:01:48 +00002527 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002528 Record.clear();
2529 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2530 Record.push_back(CXXBaseSpecifiersOffsets.size());
2531 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002532 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002533}
2534
Douglas Gregorc5046832009-04-27 18:38:38 +00002535//===----------------------------------------------------------------------===//
2536// Type Serialization
2537//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002538
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002539/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002540void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002541 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002542 if (Idx.getIndex() == 0) // we haven't seen this type before.
2543 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002544
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002545 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002546
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002547 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002548 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002549 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002550 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002551 else if (TypeOffsets.size() < Index) {
2552 TypeOffsets.resize(Index + 1);
2553 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002554 }
2555
2556 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002557
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002558 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002559 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002560
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002561 if (T.hasLocalNonFastQualifiers()) {
2562 Qualifiers Qs = T.getLocalQualifiers();
2563 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002564 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002565 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002566 } else {
2567 switch (T->getTypeClass()) {
2568 // For all of the concrete, non-dependent types, call the
2569 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002571 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002572#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002573#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002574 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002575 }
2576
2577 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002578 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002579
2580 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002581 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002582}
2583
Douglas Gregorc5046832009-04-27 18:38:38 +00002584//===----------------------------------------------------------------------===//
2585// Declaration Serialization
2586//===----------------------------------------------------------------------===//
2587
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002588/// \brief Write the block containing all of the declaration IDs
2589/// lexically declared within the given DeclContext.
2590///
2591/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2592/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002593uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002594 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002595 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002596 return 0;
2597
Douglas Gregor8f45df52009-04-16 22:23:12 +00002598 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002599 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002600 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002601 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002602 for (const auto *D : DC->decls())
2603 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002604
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002605 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002606 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002607 return Offset;
2608}
2609
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002610void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002611 using namespace llvm;
2612 RecordData Record;
2613
2614 // Write the type offsets array
2615 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002616 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2620 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2621 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002622 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002623 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002624 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002625 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002626
2627 // Write the declaration offsets array
2628 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2633 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2634 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002635 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002636 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002637 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002638 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002639}
2640
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002641void ASTWriter::WriteFileDeclIDsMap() {
2642 using namespace llvm;
2643 RecordData Record;
2644
2645 // Join the vectors of DeclIDs from all files.
2646 SmallVector<DeclID, 256> FileSortedIDs;
2647 for (FileDeclIDsTy::iterator
2648 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2649 DeclIDInFileInfo &Info = *FI->second;
2650 Info.FirstDeclIndex = FileSortedIDs.size();
2651 for (LocDeclIDsTy::iterator
2652 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2653 FileSortedIDs.push_back(DI->second);
2654 }
2655
2656 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2657 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2660 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2661 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002662 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002663 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2664}
2665
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002666void ASTWriter::WriteComments() {
2667 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002668 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002669 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002670 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2671 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002672 I != E; ++I) {
2673 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002674 AddSourceRange((*I)->getSourceRange(), Record);
2675 Record.push_back((*I)->getKind());
2676 Record.push_back((*I)->isTrailingComment());
2677 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002678 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2679 }
2680 Stream.ExitBlock();
2681}
2682
Douglas Gregorc5046832009-04-27 18:38:38 +00002683//===----------------------------------------------------------------------===//
2684// Global Method Pool and Selector Serialization
2685//===----------------------------------------------------------------------===//
2686
Douglas Gregore84a9da2009-04-20 20:36:09 +00002687namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002688// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002689class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002690 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002691
2692public:
2693 typedef Selector key_type;
2694 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002695
Sebastian Redl834bb972010-08-04 17:20:04 +00002696 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002697 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002698 ObjCMethodList Instance, Factory;
2699 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002700 typedef const data_type& data_type_ref;
2701
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002702 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002703
Douglas Gregorc78d3462009-04-24 21:10:55 +00002704 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002705 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002706 }
Mike Stump11289f42009-09-09 15:08:12 +00002707
2708 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002709 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002710 data_type_ref Methods) {
2711 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2712 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002713 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2714 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002715 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002716 if (Method->Method)
2717 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002718 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002719 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002720 if (Method->Method)
2721 DataLen += 4;
2722 clang::io::Emit16(Out, DataLen);
2723 return std::make_pair(KeyLen, DataLen);
2724 }
Mike Stump11289f42009-09-09 15:08:12 +00002725
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002726 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002727 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002728 assert((Start >> 32) == 0 && "Selector key offset too large");
2729 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002730 unsigned N = Sel.getNumArgs();
2731 clang::io::Emit16(Out, N);
2732 if (N == 0)
2733 N = 1;
2734 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002735 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002736 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2737 }
Mike Stump11289f42009-09-09 15:08:12 +00002738
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002739 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002740 data_type_ref Methods, unsigned DataLen) {
2741 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002742 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002743 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002744 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002745 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002746 if (Method->Method)
2747 ++NumInstanceMethods;
2748
2749 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002750 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002751 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002752 if (Method->Method)
2753 ++NumFactoryMethods;
2754
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002755 unsigned InstanceBits = Methods.Instance.getBits();
2756 assert(InstanceBits < 4);
2757 unsigned NumInstanceMethodsAndBits =
2758 (NumInstanceMethods << 2) | InstanceBits;
2759 unsigned FactoryBits = Methods.Factory.getBits();
2760 assert(FactoryBits < 4);
2761 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2762 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2763 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002764 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002765 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002766 if (Method->Method)
2767 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002768 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002769 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002770 if (Method->Method)
2771 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002772
2773 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002774 }
2775};
2776} // end anonymous namespace
2777
Sebastian Redla19a67f2010-08-03 21:58:15 +00002778/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002779///
2780/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002781/// in an on-disk hash table indexed by the selector. The hash table also
2782/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002783void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002784 using namespace llvm;
2785
Sebastian Redla19a67f2010-08-03 21:58:15 +00002786 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002787 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002788 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002789 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002790 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002791 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002792 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002793 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002794
Sebastian Redla19a67f2010-08-03 21:58:15 +00002795 // Create the on-disk hash table representation. We walk through every
2796 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002797 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002798 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002799 I = SelectorIDs.begin(), E = SelectorIDs.end();
2800 I != E; ++I) {
2801 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002802 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002803 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002804 I->second,
2805 ObjCMethodList(),
2806 ObjCMethodList()
2807 };
2808 if (F != SemaRef.MethodPool.end()) {
2809 Data.Instance = F->second.first;
2810 Data.Factory = F->second.second;
2811 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002812 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002813 // changed.
2814 if (Chain && I->second < FirstSelectorID) {
2815 // Selector already exists. Did it change?
2816 bool changed = false;
2817 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002818 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002819 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002820 changed = true;
2821 }
2822 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002823 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002824 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002825 changed = true;
2826 }
2827 if (!changed)
2828 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002829 } else if (Data.Instance.Method || Data.Factory.Method) {
2830 // A new method pool entry.
2831 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002832 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002833 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002834 }
2835
Douglas Gregorc78d3462009-04-24 21:10:55 +00002836 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002837 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002838 uint32_t BucketOffset;
2839 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002840 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002841 llvm::raw_svector_ostream Out(MethodPool);
2842 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002843 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002844 BucketOffset = Generator.Emit(Out, Trait);
2845 }
2846
2847 // Create a blob abbreviation
2848 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002849 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2853 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2854
Douglas Gregor95c13f52009-04-25 17:48:32 +00002855 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002856 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002857 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002858 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002859 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002860 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002861
2862 // Create a blob abbreviation for the selector table offsets.
2863 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002864 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002865 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2868 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2869
2870 // Write the selector offsets table.
2871 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002872 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002873 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002874 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002875 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002876 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002877 }
2878}
2879
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002880/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002881void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002882 using namespace llvm;
2883 if (SemaRef.ReferencedSelectors.empty())
2884 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002885
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002886 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002887
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002888 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002889 // very tricky to fix, and given that @selector shouldn't really appear in
2890 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002891 for (DenseMap<Selector, SourceLocation>::iterator S =
2892 SemaRef.ReferencedSelectors.begin(),
2893 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2894 Selector Sel = (*S).first;
2895 SourceLocation Loc = (*S).second;
2896 AddSelectorRef(Sel, Record);
2897 AddSourceLocation(Loc, Record);
2898 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002899 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002900}
2901
Douglas Gregorc5046832009-04-27 18:38:38 +00002902//===----------------------------------------------------------------------===//
2903// Identifier Table Serialization
2904//===----------------------------------------------------------------------===//
2905
Douglas Gregorc78d3462009-04-24 21:10:55 +00002906namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002907class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002908 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002909 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002910 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002911 bool IsModule;
2912
Douglas Gregor1d583f22009-04-28 21:18:29 +00002913 /// \brief Determines whether this is an "interesting" identifier
2914 /// that needs a full IdentifierInfo structure written into the hash
2915 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002916 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002917 if (II->isPoisoned() ||
2918 II->isExtensionToken() ||
2919 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002920 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002921 II->getFETokenInfo<void>())
2922 return true;
2923
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002924 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002925 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002926
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002927 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002928 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002929 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002930
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002931 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2932 if (!IsModule)
2933 return !shouldIgnoreMacro(Macro, IsModule, PP);
2934 SubmoduleID ModID;
2935 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2936 return true;
2937 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002938
2939 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002940 }
2941
Richard Smith49f906a2014-03-01 00:08:04 +00002942 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2943
2944 MacroDirective *
2945 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002946 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002947 llvm::SmallVector<SubmoduleID, 1> Overridden;
2948 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2949 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2950 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002951 return 0;
2952 }
2953
Richard Smith49f906a2014-03-01 00:08:04 +00002954 MacroDirective *
2955 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2956 OverriddenList &Overridden) {
2957 if (MacroDirective *NextMD =
2958 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2959 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2960 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002961 return 0;
2962 }
2963
2964 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00002965 /// public macro definition or undefinition that is not in ModID.
2966 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002967 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00002968 /// ModID is updated to the module containing the returned directive.
2969 ///
2970 /// FIXME: This process breaks down if a module defines a macro, imports
2971 /// another submodule that changes the macro, then changes the
2972 /// macro again itself.
2973 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2974 SubmoduleID &ModID,
2975 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002976 if (!MD)
2977 return 0;
2978
Richard Smith49f906a2014-03-01 00:08:04 +00002979 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002980 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002981 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002982 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002983 SubmoduleID ThisModID = getSubmoduleID(MD);
2984 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00002985 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002986 continue;
2987 }
Richard Smith49f906a2014-03-01 00:08:04 +00002988 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002989 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002990 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002991 }
Richard Smith49f906a2014-03-01 00:08:04 +00002992
2993 // If this is a definition from a submodule import, that submodule's
2994 // definition is overridden by the definition or undefinition that we
2995 // started with.
2996 // FIXME: This should only apply to macros defined in OrigModID.
2997 // We can't do that currently, because a #include of a different submodule
2998 // of the same module just leaks through macros instead of providing new
2999 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00003000 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3001 // Figure out which submodule the macro was originally defined within.
3002 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3003 if (!SourceID) {
3004 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3005 if (DefLoc == MD->getLocation())
3006 SourceID = ThisModID;
3007 else
3008 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3009 }
3010 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003011 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003012 }
Richard Smith49f906a2014-03-01 00:08:04 +00003013
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003014 // We are looking for a definition in a different submodule than the one
3015 // that we started with. If a submodule has re-definitions of the same
3016 // macro, only the last definition will be used as the "exported" one.
3017 if (ModID == OrigModID)
3018 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003019
Richard Smith49f906a2014-03-01 00:08:04 +00003020 // The latest visibility directive for a name in a submodule affects all
3021 // the directives that come before it.
3022 if (VisibilityMacroDirective *VisMD =
3023 dyn_cast<VisibilityMacroDirective>(MD)) {
3024 if (!IsPublic.hasValue())
3025 IsPublic = VisMD->isPublic();
3026 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3027 // FIXME: If we find an imported macro, we should include its list of
3028 // overrides in our export.
3029 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003030 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003031 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003032
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003033 return 0;
3034 }
3035
3036 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003037 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003038 }
3039
Douglas Gregore84a9da2009-04-20 20:36:09 +00003040public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003041 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003042 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003043
Sebastian Redl539c5062010-08-18 23:57:32 +00003044 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003045 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003046
Douglas Gregor935bc7a22011-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 Gregore84a9da2009-04-20 20:36:09 +00003050
3051 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003052 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003053 }
Mike Stump11289f42009-09-09 15:08:12 +00003054
3055 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003056 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003057 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003058 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003059 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003060 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003061 DataLen += 2; // 2 bytes for builtin ID
3062 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003063 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003064 DataLen += 4; // MacroDirectives offset.
3065 if (IsModule) {
3066 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003067 llvm::SmallVector<SubmoduleID, 4> Overridden;
3068 for (MacroDirective *
3069 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3070 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3071 // Previous macro's overrides.
3072 if (!Overridden.empty())
3073 DataLen += 4 * (1 + Overridden.size());
3074 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003075 }
Richard Smith49f906a2014-03-01 00:08:04 +00003076 // Previous macro's overrides.
3077 if (!Overridden.empty())
3078 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003079 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003080 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003081 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003082
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003083 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3084 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003085 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003087 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003088 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003089 // We emit the key length after the data length so that every
3090 // string is preceded by a 16-bit length. This matches the PTH
3091 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003092 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003093 return std::make_pair(KeyLen, DataLen);
3094 }
Mike Stump11289f42009-09-09 15:08:12 +00003095
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003096 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003097 unsigned KeyLen) {
3098 // Record the location of the key data. This is used when generating
3099 // the mapping from persistent IDs to strings.
3100 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003101 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003102 }
Mike Stump11289f42009-09-09 15:08:12 +00003103
Richard Smith49f906a2014-03-01 00:08:04 +00003104 static void emitMacroOverrides(raw_ostream &Out,
3105 llvm::ArrayRef<SubmoduleID> Overridden) {
3106 if (!Overridden.empty()) {
3107 clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
3108 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3109 clang::io::Emit32(Out, Overridden[I]);
3110 }
3111 }
3112
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003113 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003115 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003116 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003117 clang::io::Emit32(Out, ID << 1);
3118 return;
3119 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003120
Douglas Gregor1d583f22009-04-28 21:18:29 +00003121 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003122 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3123 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3124 clang::io::Emit16(Out, Bits);
3125 Bits = 0;
3126 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003127 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003128 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003129 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3130 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003131 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003132 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003133 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003134
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003135 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003136 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3137 if (IsModule) {
3138 // Write the IDs of macros coming from different submodules.
3139 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003140 llvm::SmallVector<SubmoduleID, 4> Overridden;
3141 for (MacroDirective *
3142 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3143 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3144 MacroID InfoID = 0;
3145 emitMacroOverrides(Out, Overridden);
3146 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3147 InfoID = Writer.getMacroID(DefMD->getInfo());
3148 assert(InfoID);
3149 clang::io::Emit32(Out, InfoID << 1);
3150 } else {
3151 assert(isa<UndefMacroDirective>(MD));
3152 clang::io::Emit32(Out, (ModID << 1) | 1);
3153 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003154 }
Richard Smith49f906a2014-03-01 00:08:04 +00003155 emitMacroOverrides(Out, Overridden);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003156 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003157 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003158 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003159
Douglas Gregora868bbd2009-04-21 22:25:48 +00003160 // Emit the declaration IDs in reverse order, because the
3161 // IdentifierResolver provides the declarations as they would be
3162 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003163 // "stat"), but the ASTReader adds declarations to the end of the list
3164 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003165 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003166 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3167 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003168 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003169 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003170 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003171 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3172 }
3173
3174 /// \brief Returns the most recent local decl or the given decl if there are
3175 /// no local ones. The given decl is assumed to be the most recent one.
3176 Decl *getMostRecentLocalDecl(Decl *Orig) {
3177 // The only way a "from AST file" decl would be more recent from a local one
3178 // is if it came from a module.
3179 if (!PP.getLangOpts().Modules)
3180 return Orig;
3181
3182 // Look for a local in the decl chain.
3183 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3184 if (!D->isFromASTFile())
3185 return D;
3186 // If we come up a decl from a (chained-)PCH stop since we won't find a
3187 // local one.
3188 if (D->getOwningModuleID() == 0)
3189 break;
3190 }
3191
3192 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003193 }
3194};
3195} // end anonymous namespace
3196
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003197/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003198///
3199/// The identifier table consists of a blob containing string data
3200/// (the actual identifiers themselves) and a separate "offsets" index
3201/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003202void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3203 IdentifierResolver &IdResolver,
3204 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003205 using namespace llvm;
3206
3207 // Create and write out the blob that contains the identifier
3208 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003209 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003210 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003211 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003212
Douglas Gregore6648fb2009-04-28 20:33:11 +00003213 // Look for any identifiers that were named while processing the
3214 // headers, but are otherwise not needed. We add these to the hash
3215 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003216 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003217 // file.
3218 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3219 IDEnd = PP.getIdentifierTable().end();
3220 ID != IDEnd; ++ID)
3221 getIdentifierRef(ID->second);
3222
Sebastian Redlff4a2952010-07-23 23:49:55 +00003223 // Create the on-disk hash table representation. We only store offsets
3224 // for identifiers that appear here for the first time.
3225 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003226 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003227 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3228 ID != IDEnd; ++ID) {
3229 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003230 if (!Chain || !ID->first->isFromAST() ||
3231 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003232 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003233 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003234 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003235
Douglas Gregore84a9da2009-04-20 20:36:09 +00003236 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003237 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003238 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003239 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003240 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003241 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003242 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003243 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003244 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003245 }
3246
3247 // Create a blob abbreviation
3248 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003249 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003252 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003253
3254 // Write the identifier table
3255 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003256 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003257 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003258 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003259 }
3260
3261 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003262 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003263 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3267 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3268
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003269#ifndef NDEBUG
3270 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3271 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3272#endif
3273
Douglas Gregor0e149972009-04-25 19:10:14 +00003274 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003275 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003276 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003277 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003278 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003279 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003280}
3281
Douglas Gregorc5046832009-04-27 18:38:38 +00003282//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003283// DeclContext's Name Lookup Table Serialization
3284//===----------------------------------------------------------------------===//
3285
3286namespace {
3287// Trait used for the on-disk hash table used in the method pool.
3288class ASTDeclContextNameLookupTrait {
3289 ASTWriter &Writer;
3290
3291public:
3292 typedef DeclarationName key_type;
3293 typedef key_type key_type_ref;
3294
3295 typedef DeclContext::lookup_result data_type;
3296 typedef const data_type& data_type_ref;
3297
3298 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3299
3300 unsigned ComputeHash(DeclarationName Name) {
3301 llvm::FoldingSetNodeID ID;
3302 ID.AddInteger(Name.getNameKind());
3303
3304 switch (Name.getNameKind()) {
3305 case DeclarationName::Identifier:
3306 ID.AddString(Name.getAsIdentifierInfo()->getName());
3307 break;
3308 case DeclarationName::ObjCZeroArgSelector:
3309 case DeclarationName::ObjCOneArgSelector:
3310 case DeclarationName::ObjCMultiArgSelector:
3311 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3312 break;
3313 case DeclarationName::CXXConstructorName:
3314 case DeclarationName::CXXDestructorName:
3315 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003316 break;
3317 case DeclarationName::CXXOperatorName:
3318 ID.AddInteger(Name.getCXXOverloadedOperator());
3319 break;
3320 case DeclarationName::CXXLiteralOperatorName:
3321 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3322 case DeclarationName::CXXUsingDirective:
3323 break;
3324 }
3325
3326 return ID.ComputeHash();
3327 }
3328
3329 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003330 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003331 data_type_ref Lookup) {
3332 unsigned KeyLen = 1;
3333 switch (Name.getNameKind()) {
3334 case DeclarationName::Identifier:
3335 case DeclarationName::ObjCZeroArgSelector:
3336 case DeclarationName::ObjCOneArgSelector:
3337 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003338 case DeclarationName::CXXLiteralOperatorName:
3339 KeyLen += 4;
3340 break;
3341 case DeclarationName::CXXOperatorName:
3342 KeyLen += 1;
3343 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003344 case DeclarationName::CXXConstructorName:
3345 case DeclarationName::CXXDestructorName:
3346 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003347 case DeclarationName::CXXUsingDirective:
3348 break;
3349 }
3350 clang::io::Emit16(Out, KeyLen);
3351
3352 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003353 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003354 clang::io::Emit16(Out, DataLen);
3355
3356 return std::make_pair(KeyLen, DataLen);
3357 }
3358
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003359 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003360 using namespace clang::io;
3361
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362 Emit8(Out, Name.getNameKind());
3363 switch (Name.getNameKind()) {
3364 case DeclarationName::Identifier:
3365 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003366 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003367 case DeclarationName::ObjCZeroArgSelector:
3368 case DeclarationName::ObjCOneArgSelector:
3369 case DeclarationName::ObjCMultiArgSelector:
3370 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003371 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003372 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003373 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3374 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003375 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003376 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003377 case DeclarationName::CXXLiteralOperatorName:
3378 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003379 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003380 case DeclarationName::CXXConstructorName:
3381 case DeclarationName::CXXDestructorName:
3382 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003383 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003384 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003385 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003386
3387 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003388 }
3389
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003390 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003391 data_type Lookup, unsigned DataLen) {
3392 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003393 clang::io::Emit16(Out, Lookup.size());
3394 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3395 I != E; ++I)
3396 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003397
3398 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3399 }
3400};
3401} // end anonymous namespace
3402
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003403/// \brief Write the block containing all of the declaration IDs
3404/// visible from the given DeclContext.
3405///
3406/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003407/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003408uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3409 DeclContext *DC) {
3410 if (DC->getPrimaryContext() != DC)
3411 return 0;
3412
3413 // Since there is no name lookup into functions or methods, don't bother to
3414 // build a visible-declarations table for these entities.
3415 if (DC->isFunctionOrMethod())
3416 return 0;
3417
3418 // If not in C++, we perform name lookup for the translation unit via the
3419 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003420 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003421 return 0;
3422
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003423 // Serialize the contents of the mapping used for lookup. Note that,
3424 // although we have two very different code paths, the serialized
3425 // representation is the same for both cases: a declaration name,
3426 // followed by a size, followed by references to the visible
3427 // declarations that have that name.
3428 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003429 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003430 if (!Map || Map->empty())
3431 return 0;
3432
3433 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3434 ASTDeclContextNameLookupTrait Trait(*this);
3435
3436 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003437 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003438 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003439 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3440 D != DEnd; ++D) {
3441 DeclarationName Name = D->first;
3442 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003443 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003444 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3445 // Hash all conversion function names to the same name. The actual
3446 // type information in conversion function name is not used in the
3447 // key (since such type information is not stable across different
3448 // modules), so the intended effect is to coalesce all of the conversion
3449 // functions under a single key.
3450 if (!ConversionName)
3451 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003452 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003453 continue;
3454 }
3455
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003456 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003457 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003458 }
3459
Douglas Gregor05ef9312011-08-30 20:49:19 +00003460 // Add the conversion functions
3461 if (!ConversionDecls.empty()) {
3462 Generator.insert(ConversionName,
3463 DeclContext::lookup_result(ConversionDecls.begin(),
3464 ConversionDecls.end()),
3465 Trait);
3466 }
3467
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003468 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003469 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003470 uint32_t BucketOffset;
3471 {
3472 llvm::raw_svector_ostream Out(LookupTable);
3473 // Make sure that no bucket is at offset 0
3474 clang::io::Emit32(Out, 0);
3475 BucketOffset = Generator.Emit(Out, Trait);
3476 }
3477
3478 // Write the lookup table
3479 RecordData Record;
3480 Record.push_back(DECL_CONTEXT_VISIBLE);
3481 Record.push_back(BucketOffset);
3482 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3483 LookupTable.str());
3484
3485 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3486 ++NumVisibleDeclContexts;
3487 return Offset;
3488}
3489
Sebastian Redla4071b42010-08-24 00:50:09 +00003490/// \brief Write an UPDATE_VISIBLE block for the given context.
3491///
3492/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3493/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003494/// (in C++), for namespaces, and for classes with forward-declared unscoped
3495/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003496void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003497 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3498 if (!Map || Map->empty())
3499 return;
3500
3501 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3502 ASTDeclContextNameLookupTrait Trait(*this);
3503
3504 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003505 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3506 D != DEnd; ++D) {
3507 DeclarationName Name = D->first;
3508 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003509 // For any name that appears in this table, the results are complete, i.e.
3510 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003511 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003512 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003513 }
3514
3515 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003516 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003517 uint32_t BucketOffset;
3518 {
3519 llvm::raw_svector_ostream Out(LookupTable);
3520 // Make sure that no bucket is at offset 0
3521 clang::io::Emit32(Out, 0);
3522 BucketOffset = Generator.Emit(Out, Trait);
3523 }
3524
3525 // Write the lookup table
3526 RecordData Record;
3527 Record.push_back(UPDATE_VISIBLE);
3528 Record.push_back(getDeclID(cast<Decl>(DC)));
3529 Record.push_back(BucketOffset);
3530 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3531}
3532
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003533/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3534void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3535 RecordData Record;
3536 Record.push_back(Opts.fp_contract);
3537 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3538}
3539
3540/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3541void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003542 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003543 return;
3544
3545 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3546 RecordData Record;
3547#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3548#include "clang/Basic/OpenCLExtensions.def"
3549 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3550}
3551
Douglas Gregor358cd442012-01-15 16:58:34 +00003552void ASTWriter::WriteRedeclarations() {
3553 RecordData LocalRedeclChains;
3554 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3555
3556 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3557 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003558 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003559
3560 Decl *MostRecent = First->getMostRecentDecl();
3561
3562 // If we only have a single declaration, there is no point in storing
3563 // a redeclaration chain.
3564 if (First == MostRecent)
3565 continue;
3566
3567 unsigned Offset = LocalRedeclChains.size();
3568 unsigned Size = 0;
3569 LocalRedeclChains.push_back(0); // Placeholder for the size.
3570
3571 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003572 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003573 Prev = Prev->getPreviousDecl()) {
3574 if (!Prev->isFromASTFile()) {
3575 AddDeclRef(Prev, LocalRedeclChains);
3576 ++Size;
3577 }
3578 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003579
3580 if (!First->isFromASTFile() && Chain) {
3581 Decl *FirstFromAST = MostRecent;
3582 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3583 if (Prev->isFromASTFile())
3584 FirstFromAST = Prev;
3585 }
3586
3587 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3588 }
3589
Douglas Gregor358cd442012-01-15 16:58:34 +00003590 LocalRedeclChains[Offset] = Size;
3591
3592 // Reverse the set of local redeclarations, so that we store them in
3593 // order (since we found them in reverse order).
3594 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3595
Douglas Gregor6168bd22013-02-18 15:53:43 +00003596 // Add the mapping from the first ID from the AST to the set of local
3597 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003598 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3599 LocalRedeclsMap.push_back(Info);
3600
3601 assert(N == Redeclarations.size() &&
3602 "Deserialized a declaration we shouldn't have");
3603 }
3604
3605 if (LocalRedeclChains.empty())
3606 return;
3607
3608 // Sort the local redeclarations map by the first declaration ID,
3609 // since the reader will be performing binary searches on this information.
3610 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3611
3612 // Emit the local redeclarations map.
3613 using namespace llvm;
3614 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3615 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3618 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3619
3620 RecordData Record;
3621 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3622 Record.push_back(LocalRedeclsMap.size());
3623 Stream.EmitRecordWithBlob(AbbrevID, Record,
3624 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3625 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3626
3627 // Emit the redeclaration chains.
3628 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3629}
3630
Douglas Gregor404cdde2012-01-27 01:47:08 +00003631void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003632 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003633 RecordData Categories;
3634
3635 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3636 unsigned Size = 0;
3637 unsigned StartIndex = Categories.size();
3638
3639 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3640
3641 // Allocate space for the size.
3642 Categories.push_back(0);
3643
3644 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003645 for (ObjCInterfaceDecl::known_categories_iterator
3646 Cat = Class->known_categories_begin(),
3647 CatEnd = Class->known_categories_end();
3648 Cat != CatEnd; ++Cat, ++Size) {
3649 assert(getDeclID(*Cat) != 0 && "Bogus category");
3650 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003651 }
3652
3653 // Update the size.
3654 Categories[StartIndex] = Size;
3655
3656 // Record this interface -> category map.
3657 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3658 CategoriesMap.push_back(CatInfo);
3659 }
3660
3661 // Sort the categories map by the definition ID, since the reader will be
3662 // performing binary searches on this information.
3663 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3664
3665 // Emit the categories map.
3666 using namespace llvm;
3667 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3668 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3669 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3670 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3671 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3672
3673 RecordData Record;
3674 Record.push_back(OBJC_CATEGORIES_MAP);
3675 Record.push_back(CategoriesMap.size());
3676 Stream.EmitRecordWithBlob(AbbrevID, Record,
3677 reinterpret_cast<char*>(CategoriesMap.data()),
3678 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3679
3680 // Emit the category lists.
3681 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3682}
3683
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003684void ASTWriter::WriteMergedDecls() {
3685 if (!Chain || Chain->MergedDecls.empty())
3686 return;
3687
3688 RecordData Record;
3689 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3690 IEnd = Chain->MergedDecls.end();
3691 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003692 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003693 : getDeclID(I->first);
3694 assert(CanonID && "Merged declaration not known?");
3695
3696 Record.push_back(CanonID);
3697 Record.push_back(I->second.size());
3698 Record.append(I->second.begin(), I->second.end());
3699 }
3700 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3701}
3702
Richard Smithe40f2ba2013-08-07 21:41:30 +00003703void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3704 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3705
3706 if (LPTMap.empty())
3707 return;
3708
3709 RecordData Record;
3710 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3711 ItEnd = LPTMap.end();
3712 It != ItEnd; ++It) {
3713 LateParsedTemplate *LPT = It->second;
3714 AddDeclRef(It->first, Record);
3715 AddDeclRef(LPT->D, Record);
3716 Record.push_back(LPT->Toks.size());
3717
3718 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3719 TokEnd = LPT->Toks.end();
3720 TokIt != TokEnd; ++TokIt) {
3721 AddToken(*TokIt, Record);
3722 }
3723 }
3724 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3725}
3726
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003727//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003728// General Serialization Routines
3729//===----------------------------------------------------------------------===//
3730
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003731/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003732void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3733 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003734 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003735 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3736 e = Attrs.end(); i != e; ++i){
3737 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003738 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003739 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003740
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003741#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003742
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003743 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003744}
3745
John McCallf413f5e2013-05-03 00:10:13 +00003746void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3747 AddSourceLocation(Tok.getLocation(), Record);
3748 Record.push_back(Tok.getLength());
3749
3750 // FIXME: When reading literal tokens, reconstruct the literal pointer
3751 // if it is needed.
3752 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3753 // FIXME: Should translate token kind to a stable encoding.
3754 Record.push_back(Tok.getKind());
3755 // FIXME: Should translate token flags to a stable encoding.
3756 Record.push_back(Tok.getFlags());
3757}
3758
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003759void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003760 Record.push_back(Str.size());
3761 Record.insert(Record.end(), Str.begin(), Str.end());
3762}
3763
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003764void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3765 RecordDataImpl &Record) {
3766 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003767 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003768 Record.push_back(*Minor + 1);
3769 else
3770 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003771 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003772 Record.push_back(*Subminor + 1);
3773 else
3774 Record.push_back(0);
3775}
3776
Douglas Gregore84a9da2009-04-20 20:36:09 +00003777/// \brief Note that the identifier II occurs at the given offset
3778/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003779void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003780 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003781 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003782 // up earlier in the chain and thus don't need an offset.
3783 if (ID >= FirstIdentID)
3784 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003785}
3786
Douglas Gregor95c13f52009-04-25 17:48:32 +00003787/// \brief Note that the selector Sel occurs at the given offset
3788/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003789void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003790 unsigned ID = SelectorIDs[Sel];
3791 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003792 // Don't record offsets for selectors that are also available in a different
3793 // file.
3794 if (ID < FirstSelectorID)
3795 return;
3796 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003797}
3798
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003799ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003800 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003801 WritingAST(false), DoneWritingDeclsAndTypes(false),
3802 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003803 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003804 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003805 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3806 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003807 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3808 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003809 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003810 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003811 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003812 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003813 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003814 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003815 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3816 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3817 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003818 DeclTypedefAbbrev(0),
3819 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3820 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003821{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003822}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003823
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003824ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003825 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003826}
3827
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003828void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003829 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003830 Module *WritingModule, StringRef isysroot,
3831 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003832 WritingAST = true;
3833
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003834 ASTHasCompilerErrors = hasErrors;
3835
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003836 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003837 Stream.Emit((unsigned)'C', 8);
3838 Stream.Emit((unsigned)'P', 8);
3839 Stream.Emit((unsigned)'C', 8);
3840 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003841
Chris Lattner28fa4e62009-04-26 22:26:21 +00003842 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003843
Douglas Gregoreda8e122011-08-09 15:13:55 +00003844 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003845 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003846 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003847 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003848 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003849 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003850 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003851
3852 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003853}
3854
Douglas Gregora94a1542011-07-27 21:45:57 +00003855template<typename Vector>
3856static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3857 ASTWriter::RecordData &Record) {
3858 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3859 I != E; ++I) {
3860 Writer.AddDeclRef(*I, Record);
3861 }
3862}
3863
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003864void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003865 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003866 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003867 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003868 using namespace llvm;
3869
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003870 bool isModule = WritingModule != 0;
3871
Douglas Gregorcf68c582011-12-01 22:20:10 +00003872 // Make sure that the AST reader knows to finalize itself.
3873 if (Chain)
3874 Chain->finalizeForWriting();
3875
Sebastian Redl143413f2010-07-12 22:02:52 +00003876 ASTContext &Context = SemaRef.Context;
3877 Preprocessor &PP = SemaRef.PP;
3878
Douglas Gregordab42432011-08-12 00:15:20 +00003879 // Set up predefined declaration IDs.
3880 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003881 if (Context.ObjCIdDecl)
3882 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003883 if (Context.ObjCSelDecl)
3884 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003885 if (Context.ObjCClassDecl)
3886 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003887 if (Context.ObjCProtocolClassDecl)
3888 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003889 if (Context.Int128Decl)
3890 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3891 if (Context.UInt128Decl)
3892 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003893 if (Context.ObjCInstanceTypeDecl)
3894 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003895 if (Context.BuiltinVaListDecl)
3896 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3897
Douglas Gregor851443c2011-08-12 01:39:19 +00003898 if (!Chain) {
3899 // Make sure that we emit IdentifierInfos (and any attached
3900 // declarations) for builtins. We don't need to do this when we're
3901 // emitting chained PCH files, because all of the builtins will be
3902 // in the original PCH file.
3903 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003904 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003905 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003906 if (!Context.getLangOpts().NoBuiltin) {
3907 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3908 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003909 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3910 getIdentifierRef(&Table.get(BuiltinNames[I]));
3911 }
3912
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003913 // If there are any out-of-date identifiers, bring them up to date.
3914 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003915 // Find out-of-date identifiers.
3916 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003917 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3918 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003919 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003920 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003921 OutOfDate.push_back(ID->second);
3922 }
3923
3924 // Update the out-of-date identifiers.
3925 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3926 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3927 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003928 }
3929
Chris Lattner0c797362009-09-08 18:19:27 +00003930 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003931 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003932 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003933 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003934 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003935
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003936 // Build a record containing all of the file scoped decls in this file.
3937 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003938 if (!isModule)
3939 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3940 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003941
Douglas Gregor851443c2011-08-12 01:39:19 +00003942 // Build a record containing all of the delegating constructors we still need
3943 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003944 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003945 if (!isModule)
3946 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003947
Douglas Gregor851443c2011-08-12 01:39:19 +00003948 // Write the set of weak, undeclared identifiers. We always write the
3949 // entire table, since later PCH files in a PCH chain are only interested in
3950 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003951 RecordData WeakUndeclaredIdentifiers;
3952 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003953 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003954 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3955 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3956 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3957 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3958 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3959 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3960 }
3961 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003962
Richard Smith78165b52013-01-10 23:43:47 +00003963 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003964 // declarations in this header file. Generally, this record will be
3965 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003966 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003967 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003968 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003969 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003970 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3971 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003972 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003973 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003974 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003975 }
3976
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003977 // Build a record containing all of the ext_vector declarations.
3978 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003979 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003980
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003981 // Build a record containing all of the VTable uses information.
3982 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003983 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003984 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3985 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3986 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3987 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3988 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003989 }
3990
3991 // Build a record containing all of dynamic classes declarations.
3992 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003993 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003994
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003995 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003996 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003997 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003998 I = SemaRef.PendingInstantiations.begin(),
3999 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4000 AddDeclRef(I->first, PendingInstantiations);
4001 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004002 }
4003 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4004 "There are local ones at end of translation unit!");
4005
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004006 // Build a record containing some declaration references.
4007 RecordData SemaDeclRefs;
4008 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4009 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4010 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4011 }
4012
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004013 RecordData CUDASpecialDeclRefs;
4014 if (Context.getcudaConfigureCallDecl()) {
4015 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4016 }
4017
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004018 // Build a record containing all of the known namespaces.
4019 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004020 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004021 I = SemaRef.KnownNamespaces.begin(),
4022 IEnd = SemaRef.KnownNamespaces.end();
4023 I != IEnd; ++I) {
4024 if (!I->second)
4025 AddDeclRef(I->first, KnownNamespaces);
4026 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004027
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004028 // Build a record of all used, undefined objects that require definitions.
4029 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004030
4031 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004032 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004033 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4034 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004035 AddDeclRef(I->first, UndefinedButUsed);
4036 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004037 }
4038
Douglas Gregor112b9072012-10-18 05:31:06 +00004039 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004040 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004041
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004042 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004043 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004044 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004045
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004046 // This is so that older clang versions, before the introduction
4047 // of the control block, can read and reject the newer PCH format.
4048 Record.clear();
4049 Record.push_back(VERSION_MAJOR);
4050 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4051
Douglas Gregor851443c2011-08-12 01:39:19 +00004052 // Create a lexical update block containing all of the declarations in the
4053 // translation unit that do not come from other AST files.
4054 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4055 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004056 for (const auto *I : TU->noload_decls()) {
4057 if (!I->isFromASTFile())
4058 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004059 }
4060
4061 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4062 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4063 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4064 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4065 Record.clear();
4066 Record.push_back(TU_UPDATE_LEXICAL);
4067 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4068 data(NewGlobalDecls));
4069
4070 // And a visible updates block for the translation unit.
4071 Abv = new llvm::BitCodeAbbrev();
4072 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4073 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4074 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4075 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4076 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4077 WriteDeclContextVisibleUpdate(TU);
4078
4079 // If the translation unit has an anonymous namespace, and we don't already
4080 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004081 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004082 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4083 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004084 if (Record.empty())
4085 Record.push_back({UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS});
Douglas Gregor851443c2011-08-12 01:39:19 +00004086 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004087
Richard Smith5652c0f2014-03-21 01:48:23 +00004088 // Add update records for all mangling numbers and static local numbers.
4089 // These aren't really update records, but this is a convenient way of
4090 // tagging this rare extra data onto the declarations.
4091 for (const auto &Number : Context.MangleNumbers)
4092 if (!Number.first->isFromASTFile())
4093 DeclUpdates[Number.first].push_back({UPD_MANGLING_NUMBER, Number.second});
4094 for (const auto &Number : Context.StaticLocalNumbers)
4095 if (!Number.first->isFromASTFile())
4096 DeclUpdates[Number.first].push_back({UPD_STATIC_LOCAL_NUMBER,
4097 Number.second});
4098
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004099 // Make sure visible decls, added to DeclContexts previously loaded from
4100 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004101 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004102 I = UpdatingVisibleDecls.begin(),
4103 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4104 GetDeclRef(*I);
4105 }
4106
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004107 // Make sure all decls associated with an identifier are registered for
4108 // serialization.
4109 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4110 IDEnd = PP.getIdentifierTable().end();
4111 ID != IDEnd; ++ID) {
4112 const IdentifierInfo *II = ID->second;
4113 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4114 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4115 DEnd = SemaRef.IdResolver.end();
4116 D != DEnd; ++D) {
4117 GetDeclRef(*D);
4118 }
4119 }
4120 }
4121
Douglas Gregor5204bde2011-08-02 16:26:37 +00004122 // Form the record of special types.
4123 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004124 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004125 AddTypeRef(Context.getFILEType(), SpecialTypes);
4126 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4127 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4128 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4129 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004130 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004131 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004132
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004133 if (Chain) {
4134 // Write the mapping information describing our module dependencies and how
4135 // each of those modules were mapped into our own offset/ID space, so that
4136 // the reader can build the appropriate mapping to its own offset/ID space.
4137 // The map consists solely of a blob with the following format:
4138 // *(module-name-len:i16 module-name:len*i8
4139 // source-location-offset:i32
4140 // identifier-id:i32
4141 // preprocessed-entity-id:i32
4142 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004143 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004144 // selector-id:i32
4145 // declaration-id:i32
4146 // c++-base-specifiers-id:i32
4147 // type-id:i32)
4148 //
4149 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4150 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4152 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004153 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004154 {
4155 llvm::raw_svector_ostream Out(Buffer);
4156 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004157 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004158 M != MEnd; ++M) {
4159 StringRef FileName = (*M)->FileName;
4160 io::Emit16(Out, FileName.size());
4161 Out.write(FileName.data(), FileName.size());
4162 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4163 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004164 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004165 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004166 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004167 io::Emit32(Out, (*M)->BaseSelectorID);
4168 io::Emit32(Out, (*M)->BaseDeclID);
4169 io::Emit32(Out, (*M)->BaseTypeIndex);
4170 }
4171 }
4172 Record.clear();
4173 Record.push_back(MODULE_OFFSET_MAP);
4174 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4175 Buffer.data(), Buffer.size());
4176 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004177
Richard Smithb9eab6d2014-03-20 19:44:17 +00004178 RecordData DeclUpdatesOffsetsRecord;
4179
Richard Smith59442b42014-03-20 20:07:19 +00004180 // Keep writing types, declarations, and declaration update records
4181 // until we've emitted all of them.
Richard Smithb9eab6d2014-03-20 19:44:17 +00004182 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4183 WriteDeclsBlockAbbrevs();
4184 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4185 E = DeclsToRewrite.end();
4186 I != E; ++I)
4187 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004188 do {
4189 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4190 while (!DeclTypesToEmit.empty()) {
4191 DeclOrType DOT = DeclTypesToEmit.front();
4192 DeclTypesToEmit.pop();
4193 if (DOT.isType())
4194 WriteType(DOT.getType());
4195 else
4196 WriteDecl(Context, DOT.getDecl());
4197 }
4198 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004199 Stream.ExitBlock();
4200
Richard Smithb9eab6d2014-03-20 19:44:17 +00004201 DoneWritingDeclsAndTypes = true;
4202
4203 // These things can only be done once we've written out decls and types.
4204 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004205 if (!DeclUpdatesOffsetsRecord.empty())
4206 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004207 WriteCXXBaseSpecifiersOffsets();
4208 WriteFileDeclIDsMap();
4209 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4210
4211 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004212 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004213 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004214 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004215 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004216 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004217 WriteFPPragmaOptions(SemaRef.getFPOptions());
4218 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004219 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004220
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004221 // If we're emitting a module, write out the submodule information.
4222 if (WritingModule)
4223 WriteSubmodules(WritingModule);
4224
Douglas Gregor5204bde2011-08-02 16:26:37 +00004225 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4226
Douglas Gregord4df8652009-04-22 22:02:47 +00004227 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004228 if (!EagerlyDeserializedDecls.empty())
4229 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004230
4231 // Write the record containing tentative definitions.
4232 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004233 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004234
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004235 // Write the record containing unused file scoped decls.
4236 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004237 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004238
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004239 // Write the record containing weak undeclared identifiers.
4240 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004241 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004242 WeakUndeclaredIdentifiers);
4243
Richard Smith78165b52013-01-10 23:43:47 +00004244 // Write the record containing locally-scoped extern "C" definitions.
4245 if (!LocallyScopedExternCDecls.empty())
4246 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4247 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004248
4249 // Write the record containing ext_vector type names.
4250 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004251 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004252
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004253 // Write the record containing VTable uses information.
4254 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004255 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004256
4257 // Write the record containing dynamic classes declarations.
4258 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004259 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004260
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004261 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004262 if (!PendingInstantiations.empty())
4263 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004264
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004265 // Write the record containing declaration references of Sema.
4266 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004267 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004268
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004269 // Write the record containing CUDA-specific declaration references.
4270 if (!CUDASpecialDeclRefs.empty())
4271 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004272
4273 // Write the delegating constructors.
4274 if (!DelegatingCtorDecls.empty())
4275 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004276
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004277 // Write the known namespaces.
4278 if (!KnownNamespaces.empty())
4279 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004280
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004281 // Write the undefined internal functions and variables, and inline functions.
4282 if (!UndefinedButUsed.empty())
4283 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004284
Douglas Gregor851443c2011-08-12 01:39:19 +00004285 // Write the visible updates to DeclContexts.
4286 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4287 I = UpdatedDeclContexts.begin(),
4288 E = UpdatedDeclContexts.end();
4289 I != E; ++I)
4290 WriteDeclContextVisibleUpdate(*I);
4291
Douglas Gregor959bb062011-12-03 01:15:29 +00004292 if (!WritingModule) {
4293 // Write the submodules that were imported, if any.
Richard Smith56be7542014-03-21 00:33:59 +00004294 struct ModuleInfo { uint64_t ID; Module *M; };
4295 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004296 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004297 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Richard Smith56be7542014-03-21 00:33:59 +00004298 Imports.push_back({SubmoduleIDs[I->getImportedModule()],
4299 I->getImportedModule()});
Douglas Gregor959bb062011-12-03 01:15:29 +00004300 }
Richard Smith56be7542014-03-21 00:33:59 +00004301
4302 if (!Imports.empty()) {
4303 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4304 return A.ID < B.ID;
4305 };
4306
4307 // Sort and deduplicate module IDs.
4308 std::sort(Imports.begin(), Imports.end(), Cmp);
4309 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4310 Imports.end());
4311
4312 RecordData ImportedModules;
4313 for (const auto &Import : Imports) {
4314 ImportedModules.push_back(Import.ID);
4315 // FIXME: If the module has macros imported then later has declarations
4316 // imported, this location won't be the right one as a location for the
4317 // declaration imports.
4318 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4319 }
4320
Douglas Gregor959bb062011-12-03 01:15:29 +00004321 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4322 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004323 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004324
Douglas Gregor851443c2011-08-12 01:39:19 +00004325 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004326 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004327 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004328 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004329 WriteLateParsedTemplates(SemaRef);
4330
Douglas Gregor08f01292009-04-17 22:13:46 +00004331 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004332 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004333 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004334 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004335 Record.push_back(NumLexicalDeclContexts);
4336 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004337 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004338 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004339}
4340
Richard Smithb9eab6d2014-03-20 19:44:17 +00004341void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004342 if (DeclUpdates.empty())
4343 return;
4344
Richard Smith59442b42014-03-20 20:07:19 +00004345 DeclUpdateMap LocalUpdates;
4346 LocalUpdates.swap(DeclUpdates);
4347
Richard Smith6ef42932014-03-20 21:02:00 +00004348 for (auto &DeclUpdate : LocalUpdates) {
4349 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004350 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004351 continue; // The decl will be written completely,no need to store updates.
4352
Richard Smith6ef42932014-03-20 21:02:00 +00004353 OffsetsRecord.push_back(GetDeclRef(D));
4354 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4355
4356 RecordData Record;
4357 for (auto &Update : DeclUpdate.second) {
4358 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4359
4360 Record.push_back(Kind);
4361 switch (Kind) {
4362 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4363 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4364 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4365 Record.push_back(GetDeclRef(Update.getDecl()));
4366 break;
4367
4368 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4369 AddSourceLocation(Update.getLoc(), Record);
4370 break;
4371
Richard Smith564417a2014-03-20 21:47:22 +00004372 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4373 addExceptionSpec(
4374 *this,
4375 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4376 Record);
4377 break;
4378
Richard Smith6ef42932014-03-20 21:02:00 +00004379 case UPD_CXX_DEDUCED_RETURN_TYPE:
4380 Record.push_back(GetOrCreateTypeID(Update.getType()));
4381 break;
4382
4383 case UPD_DECL_MARKED_USED:
4384 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004385
4386 case UPD_MANGLING_NUMBER:
4387 case UPD_STATIC_LOCAL_NUMBER:
4388 Record.push_back(Update.getNumber());
4389 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004390 }
4391 }
4392
4393 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004394
Richard Smithb9eab6d2014-03-20 19:44:17 +00004395 // Flush any statements that were written as part of this update record.
4396 FlushStmts();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004397 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004398}
4399
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004400void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004401 if (ReplacedDecls.empty())
4402 return;
4403
4404 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004405 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4406 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004407 Record.push_back(I->ID);
4408 Record.push_back(I->Offset);
4409 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004410 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004411 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004412}
4413
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004414void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004415 Record.push_back(Loc.getRawEncoding());
4416}
4417
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004418void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004419 AddSourceLocation(Range.getBegin(), Record);
4420 AddSourceLocation(Range.getEnd(), Record);
4421}
4422
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004423void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004424 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004425 const uint64_t *Words = Value.getRawData();
4426 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004427}
4428
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004429void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004430 Record.push_back(Value.isUnsigned());
4431 AddAPInt(Value, Record);
4432}
4433
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004434void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004435 AddAPInt(Value.bitcastToAPInt(), Record);
4436}
4437
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004438void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004439 Record.push_back(getIdentifierRef(II));
4440}
4441
Sebastian Redl539c5062010-08-18 23:57:32 +00004442IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004443 if (II == 0)
4444 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004445
Sebastian Redl539c5062010-08-18 23:57:32 +00004446 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004447 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004448 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004449 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004450}
4451
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004452MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004453 // Don't emit builtin macros like __LINE__ to the AST file unless they
4454 // have been redefined by the header (in which case they are not
4455 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004456 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004457 return 0;
4458
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004459 MacroID &ID = MacroIDs[MI];
4460 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004461 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004462 MacroInfoToEmitData Info = { Name, MI, ID };
4463 MacroInfosToEmit.push_back(Info);
4464 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004465 return ID;
4466}
4467
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004468MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4469 if (MI == 0 || MI->isBuiltinMacro())
4470 return 0;
4471
4472 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4473 return MacroIDs[MI];
4474}
4475
4476uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4477 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4478 return IdentMacroDirectivesOffsetMap[Name];
4479}
4480
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004481void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004482 Record.push_back(getSelectorRef(SelRef));
4483}
4484
Sebastian Redl539c5062010-08-18 23:57:32 +00004485SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004486 if (Sel.getAsOpaquePtr() == 0) {
4487 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004488 }
4489
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004490 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004491 if (SID == 0 && Chain) {
4492 // This might trigger a ReadSelector callback, which will set the ID for
4493 // this selector.
4494 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004495 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004496 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004497 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004498 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004499 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004500 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004501 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004502}
4503
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004504void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004505 AddDeclRef(Temp->getDestructor(), Record);
4506}
4507
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004508void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4509 CXXBaseSpecifier const *BasesEnd,
4510 RecordDataImpl &Record) {
4511 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4512 CXXBaseSpecifiersToWrite.push_back(
4513 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4514 Bases, BasesEnd));
4515 Record.push_back(NextCXXBaseSpecifiersID++);
4516}
4517
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004518void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004519 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004520 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004521 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004522 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004523 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004524 break;
4525 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004526 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004527 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004528 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004529 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004530 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004531 break;
4532 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004533 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004534 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004535 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004536 break;
John McCall0ad16662009-10-29 08:12:44 +00004537 case TemplateArgument::Null:
4538 case TemplateArgument::Integral:
4539 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004540 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004541 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004542 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004543 break;
4544 }
4545}
4546
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004547void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004548 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004549 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004550
4551 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4552 bool InfoHasSameExpr
4553 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4554 Record.push_back(InfoHasSameExpr);
4555 if (InfoHasSameExpr)
4556 return; // Avoid storing the same expr twice.
4557 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004558 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4559 Record);
4560}
4561
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004562void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4563 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004564 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004565 AddTypeRef(QualType(), Record);
4566 return;
4567 }
4568
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004569 AddTypeLoc(TInfo->getTypeLoc(), Record);
4570}
4571
4572void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4573 AddTypeRef(TL.getType(), Record);
4574
John McCall8f115c62009-10-16 21:56:05 +00004575 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004576 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004577 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004578}
4579
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004580void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004581 Record.push_back(GetOrCreateTypeID(T));
4582}
4583
Douglas Gregoreda8e122011-08-09 15:13:55 +00004584TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004585 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004586 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004587 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4588}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004589
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004590TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004591 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004592 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004593 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004594}
4595
4596TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4597 if (T.isNull())
4598 return TypeIdx();
4599 assert(!T.getLocalFastQualifiers());
4600
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004601 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004602 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004603 if (DoneWritingDeclsAndTypes) {
4604 assert(0 && "New type seen after serializing all the types to emit!");
4605 return TypeIdx();
4606 }
4607
Douglas Gregor1970d882009-04-26 03:49:13 +00004608 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004609 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004610 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004611 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004612 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004613 return Idx;
4614}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004615
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004616TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004617 if (T.isNull())
4618 return TypeIdx();
4619 assert(!T.getLocalFastQualifiers());
4620
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004621 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4622 assert(I != TypeIdxs.end() && "Type not emitted!");
4623 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004624}
4625
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004626void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004627 Record.push_back(GetDeclRef(D));
4628}
4629
Sebastian Redl539c5062010-08-18 23:57:32 +00004630DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004631 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4632
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004633 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004634 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004635 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004636
4637 // If D comes from an AST file, its declaration ID is already known and
4638 // fixed.
4639 if (D->isFromASTFile())
4640 return D->getGlobalID();
4641
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004642 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004643 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004644 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004645 if (DoneWritingDeclsAndTypes) {
4646 assert(0 && "New decl seen after serializing all the decls to emit!");
4647 return 0;
4648 }
4649
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004650 // We haven't seen this declaration before. Give it a new ID and
4651 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004652 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004653 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004654 }
4655
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004656 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004657}
4658
Sebastian Redl539c5062010-08-18 23:57:32 +00004659DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004660 if (D == 0)
4661 return 0;
4662
Douglas Gregorb3163e52012-01-05 22:33:30 +00004663 // If D comes from an AST file, its declaration ID is already known and
4664 // fixed.
4665 if (D->isFromASTFile())
4666 return D->getGlobalID();
4667
Douglas Gregore84a9da2009-04-20 20:36:09 +00004668 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4669 return DeclIDs[D];
4670}
4671
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004672void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004673 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004674 assert(D);
4675
4676 SourceLocation Loc = D->getLocation();
4677 if (Loc.isInvalid())
4678 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004679
4680 // We only keep track of the file-level declarations of each file.
4681 if (!D->getLexicalDeclContext()->isFileContext())
4682 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004683 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4684 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004685 if (isa<ParmVarDecl>(D))
4686 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004687
4688 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004689 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004690 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004691 FileID FID;
4692 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004693 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004694 if (FID.isInvalid())
4695 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004696 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004697
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004698 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004699 if (!Info)
4700 Info = new DeclIDInFileInfo();
4701
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004702 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004703 LocDeclIDsTy &Decls = Info->DeclIDs;
4704
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004705 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004706 Decls.push_back(LocDecl);
4707 return;
4708 }
4709
Benjamin Kramer45025c02013-08-24 13:22:59 +00004710 LocDeclIDsTy::iterator I =
4711 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004712
4713 Decls.insert(I, LocDecl);
4714}
4715
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004716void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004717 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004718 Record.push_back(Name.getNameKind());
4719 switch (Name.getNameKind()) {
4720 case DeclarationName::Identifier:
4721 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4722 break;
4723
4724 case DeclarationName::ObjCZeroArgSelector:
4725 case DeclarationName::ObjCOneArgSelector:
4726 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004727 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004728 break;
4729
4730 case DeclarationName::CXXConstructorName:
4731 case DeclarationName::CXXDestructorName:
4732 case DeclarationName::CXXConversionFunctionName:
4733 AddTypeRef(Name.getCXXNameType(), Record);
4734 break;
4735
4736 case DeclarationName::CXXOperatorName:
4737 Record.push_back(Name.getCXXOverloadedOperator());
4738 break;
4739
Alexis Hunt3d221f22009-11-29 07:34:05 +00004740 case DeclarationName::CXXLiteralOperatorName:
4741 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4742 break;
4743
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004744 case DeclarationName::CXXUsingDirective:
4745 // No extra data to emit
4746 break;
4747 }
4748}
Chris Lattnerca025db2010-05-07 21:43:38 +00004749
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004750void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004751 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004752 switch (Name.getNameKind()) {
4753 case DeclarationName::CXXConstructorName:
4754 case DeclarationName::CXXDestructorName:
4755 case DeclarationName::CXXConversionFunctionName:
4756 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4757 break;
4758
4759 case DeclarationName::CXXOperatorName:
4760 AddSourceLocation(
4761 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4762 Record);
4763 AddSourceLocation(
4764 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4765 Record);
4766 break;
4767
4768 case DeclarationName::CXXLiteralOperatorName:
4769 AddSourceLocation(
4770 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4771 Record);
4772 break;
4773
4774 case DeclarationName::Identifier:
4775 case DeclarationName::ObjCZeroArgSelector:
4776 case DeclarationName::ObjCOneArgSelector:
4777 case DeclarationName::ObjCMultiArgSelector:
4778 case DeclarationName::CXXUsingDirective:
4779 break;
4780 }
4781}
4782
4783void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004784 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004785 AddDeclarationName(NameInfo.getName(), Record);
4786 AddSourceLocation(NameInfo.getLoc(), Record);
4787 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4788}
4789
4790void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004791 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004792 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004793 Record.push_back(Info.NumTemplParamLists);
4794 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4795 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4796}
4797
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004798void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004799 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004800 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004801 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004802 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004803
4804 // Push each of the NNS's onto a stack for serialization in reverse order.
4805 while (NNS) {
4806 NestedNames.push_back(NNS);
4807 NNS = NNS->getPrefix();
4808 }
4809
4810 Record.push_back(NestedNames.size());
4811 while(!NestedNames.empty()) {
4812 NNS = NestedNames.pop_back_val();
4813 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4814 Record.push_back(Kind);
4815 switch (Kind) {
4816 case NestedNameSpecifier::Identifier:
4817 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4818 break;
4819
4820 case NestedNameSpecifier::Namespace:
4821 AddDeclRef(NNS->getAsNamespace(), Record);
4822 break;
4823
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004824 case NestedNameSpecifier::NamespaceAlias:
4825 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4826 break;
4827
Chris Lattnerca025db2010-05-07 21:43:38 +00004828 case NestedNameSpecifier::TypeSpec:
4829 case NestedNameSpecifier::TypeSpecWithTemplate:
4830 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4831 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4832 break;
4833
4834 case NestedNameSpecifier::Global:
4835 // Don't need to write an associated value.
4836 break;
4837 }
4838 }
4839}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004840
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004841void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4842 RecordDataImpl &Record) {
4843 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004844 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004845 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004846
4847 // Push each of the nested-name-specifiers's onto a stack for
4848 // serialization in reverse order.
4849 while (NNS) {
4850 NestedNames.push_back(NNS);
4851 NNS = NNS.getPrefix();
4852 }
4853
4854 Record.push_back(NestedNames.size());
4855 while(!NestedNames.empty()) {
4856 NNS = NestedNames.pop_back_val();
4857 NestedNameSpecifier::SpecifierKind Kind
4858 = NNS.getNestedNameSpecifier()->getKind();
4859 Record.push_back(Kind);
4860 switch (Kind) {
4861 case NestedNameSpecifier::Identifier:
4862 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4863 AddSourceRange(NNS.getLocalSourceRange(), Record);
4864 break;
4865
4866 case NestedNameSpecifier::Namespace:
4867 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4868 AddSourceRange(NNS.getLocalSourceRange(), Record);
4869 break;
4870
4871 case NestedNameSpecifier::NamespaceAlias:
4872 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4873 AddSourceRange(NNS.getLocalSourceRange(), Record);
4874 break;
4875
4876 case NestedNameSpecifier::TypeSpec:
4877 case NestedNameSpecifier::TypeSpecWithTemplate:
4878 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4879 AddTypeLoc(NNS.getTypeLoc(), Record);
4880 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4881 break;
4882
4883 case NestedNameSpecifier::Global:
4884 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4885 break;
4886 }
4887 }
4888}
4889
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004890void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004891 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004892 Record.push_back(Kind);
4893 switch (Kind) {
4894 case TemplateName::Template:
4895 AddDeclRef(Name.getAsTemplateDecl(), Record);
4896 break;
4897
4898 case TemplateName::OverloadedTemplate: {
4899 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4900 Record.push_back(OvT->size());
4901 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4902 I != E; ++I)
4903 AddDeclRef(*I, Record);
4904 break;
4905 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004906
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004907 case TemplateName::QualifiedTemplate: {
4908 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4909 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4910 Record.push_back(QualT->hasTemplateKeyword());
4911 AddDeclRef(QualT->getTemplateDecl(), Record);
4912 break;
4913 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004914
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004915 case TemplateName::DependentTemplate: {
4916 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4917 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4918 Record.push_back(DepT->isIdentifier());
4919 if (DepT->isIdentifier())
4920 AddIdentifierRef(DepT->getIdentifier(), Record);
4921 else
4922 Record.push_back(DepT->getOperator());
4923 break;
4924 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004925
4926 case TemplateName::SubstTemplateTemplateParm: {
4927 SubstTemplateTemplateParmStorage *subst
4928 = Name.getAsSubstTemplateTemplateParm();
4929 AddDeclRef(subst->getParameter(), Record);
4930 AddTemplateName(subst->getReplacement(), Record);
4931 break;
4932 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004933
4934 case TemplateName::SubstTemplateTemplateParmPack: {
4935 SubstTemplateTemplateParmPackStorage *SubstPack
4936 = Name.getAsSubstTemplateTemplateParmPack();
4937 AddDeclRef(SubstPack->getParameterPack(), Record);
4938 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4939 break;
4940 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004941 }
4942}
4943
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004944void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004945 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004946 Record.push_back(Arg.getKind());
4947 switch (Arg.getKind()) {
4948 case TemplateArgument::Null:
4949 break;
4950 case TemplateArgument::Type:
4951 AddTypeRef(Arg.getAsType(), Record);
4952 break;
4953 case TemplateArgument::Declaration:
4954 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004955 Record.push_back(Arg.isDeclForReferenceParam());
4956 break;
4957 case TemplateArgument::NullPtr:
4958 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004959 break;
4960 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004961 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004962 AddTypeRef(Arg.getIntegralType(), Record);
4963 break;
4964 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004965 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4966 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004967 case TemplateArgument::TemplateExpansion:
4968 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004969 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004970 Record.push_back(*NumExpansions + 1);
4971 else
4972 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004973 break;
4974 case TemplateArgument::Expression:
4975 AddStmt(Arg.getAsExpr());
4976 break;
4977 case TemplateArgument::Pack:
4978 Record.push_back(Arg.pack_size());
4979 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4980 I != E; ++I)
4981 AddTemplateArgument(*I, Record);
4982 break;
4983 }
4984}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004985
4986void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004987ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004988 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004989 assert(TemplateParams && "No TemplateParams!");
4990 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4991 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4992 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4993 Record.push_back(TemplateParams->size());
4994 for (TemplateParameterList::const_iterator
4995 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4996 P != PEnd; ++P)
4997 AddDeclRef(*P, Record);
4998}
4999
5000/// \brief Emit a template argument list.
5001void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005002ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005003 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005004 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005005 Record.push_back(TemplateArgs->size());
5006 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005007 AddTemplateArgument(TemplateArgs->get(i), Record);
5008}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005009
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005010void
5011ASTWriter::AddASTTemplateArgumentListInfo
5012(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5013 assert(ASTTemplArgList && "No ASTTemplArgList!");
5014 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5015 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5016 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5017 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5018 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5019 AddTemplateArgumentLoc(TemplArgs[i], Record);
5020}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005021
5022void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005023ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005024 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005025 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005026 I = Set.begin(), E = Set.end(); I != E; ++I) {
5027 AddDeclRef(I.getDecl(), Record);
5028 Record.push_back(I.getAccess());
5029 }
5030}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005031
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005032void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005033 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005034 Record.push_back(Base.isVirtual());
5035 Record.push_back(Base.isBaseOfClass());
5036 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005037 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005038 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005039 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005040 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5041 : SourceLocation(),
5042 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005043}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005044
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005045void ASTWriter::FlushCXXBaseSpecifiers() {
5046 RecordData Record;
5047 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5048 Record.clear();
5049
5050 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005051 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005052 if (Index == CXXBaseSpecifiersOffsets.size())
5053 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5054 else {
5055 if (Index > CXXBaseSpecifiersOffsets.size())
5056 CXXBaseSpecifiersOffsets.resize(Index + 1);
5057 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5058 }
5059
5060 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5061 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5062 Record.push_back(BEnd - B);
5063 for (; B != BEnd; ++B)
5064 AddCXXBaseSpecifier(*B, Record);
5065 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005066
5067 // Flush any expressions that were written as part of the base specifiers.
5068 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005069 }
5070
5071 CXXBaseSpecifiersToWrite.clear();
5072}
5073
Alexis Hunt1d792652011-01-08 20:30:50 +00005074void ASTWriter::AddCXXCtorInitializers(
5075 const CXXCtorInitializer * const *CtorInitializers,
5076 unsigned NumCtorInitializers,
5077 RecordDataImpl &Record) {
5078 Record.push_back(NumCtorInitializers);
5079 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5080 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005081
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005082 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005083 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005084 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005085 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005086 } else if (Init->isDelegatingInitializer()) {
5087 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005088 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005089 } else if (Init->isMemberInitializer()){
5090 Record.push_back(CTOR_INITIALIZER_MEMBER);
5091 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005092 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005093 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5094 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005095 }
Francois Pichetd583da02010-12-04 09:14:42 +00005096
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005097 AddSourceLocation(Init->getMemberLocation(), Record);
5098 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005099 AddSourceLocation(Init->getLParenLoc(), Record);
5100 AddSourceLocation(Init->getRParenLoc(), Record);
5101 Record.push_back(Init->isWritten());
5102 if (Init->isWritten()) {
5103 Record.push_back(Init->getSourceOrder());
5104 } else {
5105 Record.push_back(Init->getNumArrayIndices());
5106 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5107 AddDeclRef(Init->getArrayIndex(i), Record);
5108 }
5109 }
5110}
5111
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005112void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5113 assert(D->DefinitionData);
5114 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005115 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005116 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005117 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005118 Record.push_back(Data.Aggregate);
5119 Record.push_back(Data.PlainOldData);
5120 Record.push_back(Data.Empty);
5121 Record.push_back(Data.Polymorphic);
5122 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005123 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005124 Record.push_back(Data.HasNoNonEmptyBases);
5125 Record.push_back(Data.HasPrivateFields);
5126 Record.push_back(Data.HasProtectedFields);
5127 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005128 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005129 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005130 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005131 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005132 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005133 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5134 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5135 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5136 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5137 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5138 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005139 Record.push_back(Data.HasTrivialSpecialMembers);
5140 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005141 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005142 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005143 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005144 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005145 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005146 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005147 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005148 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5149 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5150 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5151 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005152 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005153
5154 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005155 if (Data.NumBases > 0)
5156 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5157 Record);
5158
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005159 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5160 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005161 if (Data.NumVBases > 0)
5162 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5163 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005164
Richard Smitha4ba74c2013-08-30 04:46:40 +00005165 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5166 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005167 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005168 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005169
5170 // Add lambda-specific data.
5171 if (Data.IsLambda) {
5172 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005173 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005174 Record.push_back(Lambda.IsGenericLambda);
5175 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005176 Record.push_back(Lambda.NumCaptures);
5177 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005178 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005179 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005180 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005181 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5182 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5183 AddSourceLocation(Capture.getLocation(), Record);
5184 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005185 Record.push_back(Capture.getCaptureKind());
5186 switch (Capture.getCaptureKind()) {
5187 case LCK_This:
5188 break;
5189 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005190 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005191 VarDecl *Var =
5192 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5193 AddDeclRef(Var, Record);
5194 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5195 : SourceLocation(),
5196 Record);
5197 break;
5198 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005199 }
5200 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005201}
5202
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005203void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005204 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005205 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005206 assert(FirstDeclID == NextDeclID &&
5207 FirstTypeID == NextTypeID &&
5208 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005209 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005210 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005211 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005212 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005213
Sebastian Redl07a89a82010-07-30 00:29:29 +00005214 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005215
Douglas Gregordf0c1512011-08-18 04:12:04 +00005216 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5217 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5218 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005219 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005220 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005221 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005222 NextDeclID = FirstDeclID;
5223 NextTypeID = FirstTypeID;
5224 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005225 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005226 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005227 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005228}
5229
Sebastian Redl539c5062010-08-18 23:57:32 +00005230void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005231 // Always keep the highest ID. See \p TypeRead() for more information.
5232 IdentID &StoredID = IdentifierIDs[II];
5233 if (ID > StoredID)
5234 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005235}
5236
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005237void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005238 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005239 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005240 if (ID > StoredID)
5241 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005242}
5243
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005244void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005245 // Always take the highest-numbered type index. This copes with an interesting
5246 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005247 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005248 // keep the higher-numbered entry so that we can properly write it out to
5249 // the AST file.
5250 TypeIdx &StoredIdx = TypeIdxs[T];
5251 if (Idx.getIndex() >= StoredIdx.getIndex())
5252 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005253}
5254
Sebastian Redl539c5062010-08-18 23:57:32 +00005255void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005256 // Always keep the highest ID. See \p TypeRead() for more information.
5257 SelectorID &StoredID = SelectorIDs[S];
5258 if (ID > StoredID)
5259 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005260}
Douglas Gregor91096292010-10-02 19:29:26 +00005261
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005262void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005263 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005264 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005265 MacroDefinitions[MD] = ID;
5266}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005267
Douglas Gregore37a85a2011-12-02 17:30:13 +00005268void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5269 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5270 SubmoduleIDs[Mod] = ID;
5271}
5272
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005273void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005274 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005275 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005276 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5277 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005278 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005279 // A forward reference was mutated into a definition. Rewrite it.
5280 // FIXME: This happens during template instantiation, should we
5281 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005282 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005283 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005284 }
5285}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005286
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005287void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005288 assert(!WritingAST && "Already writing the AST!");
5289
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005290 // TU and namespaces are handled elsewhere.
5291 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5292 return;
5293
Douglas Gregorb3722e22011-09-09 23:01:35 +00005294 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005295 return; // Not a source decl added to a DeclContext from PCH.
5296
Douglas Gregor9f782892013-01-21 15:25:38 +00005297 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005298 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005299 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005300}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005301
5302void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005303 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005304 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005305 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005306 return; // Not a source member added to a class from PCH.
5307 if (!isa<CXXMethodDecl>(D))
5308 return; // We are interested in lazily declared implicit methods.
5309
5310 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005311 assert(RD->isCompleteDefinition());
Richard Smith6ef42932014-03-20 21:02:00 +00005312 DeclUpdates[RD].push_back({UPD_CXX_ADDED_IMPLICIT_MEMBER, D});
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005313}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005314
5315void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5316 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005317 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005318 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005319 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005320 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005321 return; // Not a source specialization added to a template from PCH.
5322
Richard Smith6ef42932014-03-20 21:02:00 +00005323 DeclUpdates[TD].push_back({UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, D});
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005324}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005325
Larisse Voufo39a1e502013-08-06 01:03:05 +00005326void ASTWriter::AddedCXXTemplateSpecialization(
5327 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5328 // The specializations set is kept in the canonical template.
5329 assert(!WritingAST && "Already writing the AST!");
5330 TD = TD->getCanonicalDecl();
5331 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5332 return; // Not a source specialization added to a template from PCH.
5333
Richard Smith6ef42932014-03-20 21:02:00 +00005334 DeclUpdates[TD].push_back({UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, D});
Larisse Voufo39a1e502013-08-06 01:03:05 +00005335}
5336
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005337void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5338 const FunctionDecl *D) {
5339 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005340 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005341 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005342 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005343 return; // Not a source specialization added to a template from PCH.
5344
Richard Smith6ef42932014-03-20 21:02:00 +00005345 DeclUpdates[TD].push_back({UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, D});
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005346}
5347
Richard Smith564417a2014-03-20 21:47:22 +00005348void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5349 assert(!WritingAST && "Already writing the AST!");
5350 FD = FD->getCanonicalDecl();
5351 if (!FD->isFromASTFile())
5352 return; // Not a function declared in PCH and defined outside.
5353
5354 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5355}
5356
Richard Smith1fa5d642013-05-11 05:45:24 +00005357void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5358 assert(!WritingAST && "Already writing the AST!");
5359 FD = FD->getCanonicalDecl();
5360 if (!FD->isFromASTFile())
5361 return; // Not a function declared in PCH and defined outside.
5362
Richard Smith6ef42932014-03-20 21:02:00 +00005363 DeclUpdates[FD].push_back({UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType});
Richard Smith1fa5d642013-05-11 05:45:24 +00005364}
5365
Sebastian Redlab238a72011-04-24 16:28:06 +00005366void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005367 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005368 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005369 return; // Declaration not imported from PCH.
5370
5371 // Implicit decl from a PCH was defined.
5372 // FIXME: Should implicit definition be a separate FunctionDecl?
5373 RewriteDecl(D);
5374}
5375
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005376void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005377 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005378 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005379 return;
5380
5381 // Since the actual instantiation is delayed, this really means that we need
5382 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005383 DeclUpdates[D].push_back(
5384 {UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5385 D->getMemberSpecializationInfo()->getPointOfInstantiation()});
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005386}
5387
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005388void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5389 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005390 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005391 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005392 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005393
5394 assert(IFD->getDefinition() && "Category on a class without a definition?");
5395 ObjCClassesWithCategories.insert(
5396 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005397}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005398
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005399
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005400void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5401 const ObjCPropertyDecl *OrigProp,
5402 const ObjCCategoryDecl *ClassExt) {
5403 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5404 if (!D)
5405 return;
5406
5407 assert(!WritingAST && "Already writing the AST!");
5408 if (!D->isFromASTFile())
5409 return; // Declaration not imported from PCH.
5410
5411 RewriteDecl(D);
5412}
Eli Friedman276dd182013-09-05 00:02:25 +00005413
5414void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5415 assert(!WritingAST && "Already writing the AST!");
5416 if (!D->isFromASTFile())
5417 return;
5418
Richard Smith6ef42932014-03-20 21:02:00 +00005419 DeclUpdates[D].push_back({UPD_DECL_MARKED_USED});
Eli Friedman276dd182013-09-05 00:02:25 +00005420}