blob: 2967fb3486edafbdca894e962650048624221037 [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
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000197void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198 VisitFunctionType(T);
Alp Toker9cacbab2014-01-20 20:26:09 +0000199 Record.push_back(T->getNumParams());
200 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
201 Writer.AddTypeRef(T->getParamType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000203 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000205 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000206 Record.push_back(T->getExceptionSpecType());
207 if (T->getExceptionSpecType() == EST_Dynamic) {
208 Record.push_back(T->getNumExceptions());
209 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
210 Writer.AddTypeRef(T->getExceptionType(I), Record);
211 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
212 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000213 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
214 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
215 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000216 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
217 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000218 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000225}
John McCallb96ec562009-12-04 22:46:56 +0000226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000229 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
230 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000231 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000232}
233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237}
238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000245 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000246 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000248}
249
Alexis Hunte852b102011-05-24 22:41:36 +0000250void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
251 Writer.AddTypeRef(T->getBaseType(), Record);
252 Writer.AddTypeRef(T->getUnderlyingType(), Record);
253 Record.push_back(T->getUTTKind());
254 Code = TYPE_UNARY_TRANSFORM;
255}
256
Richard Smith30482bc2011-02-20 03:19:35 +0000257void ASTTypeWriter::VisitAutoType(const AutoType *T) {
258 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000259 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000260 if (T->getDeducedType().isNull())
261 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000262 Code = TYPE_AUTO;
263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000266 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000267 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000268 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000269 "Cannot serialize in the middle of a type definition");
270}
271
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000273 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000274 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000275}
276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000278 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280}
281
John McCall81904512011-01-06 01:58:22 +0000282void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
283 Writer.AddTypeRef(T->getModifiedType(), Record);
284 Writer.AddTypeRef(T->getEquivalentType(), Record);
285 Record.push_back(T->getAttrKind());
286 Code = TYPE_ATTRIBUTED;
287}
288
Mike Stump11289f42009-09-09 15:08:12 +0000289void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000290ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000291 const SubstTemplateTypeParmType *T) {
292 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
293 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000294 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000295}
296
297void
Douglas Gregorada4b792011-01-14 02:55:32 +0000298ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
299 const SubstTemplateTypeParmPackType *T) {
300 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
301 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
302 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
303}
304
305void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000306ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000307 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000308 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309 Writer.AddTemplateName(T->getTemplateName(), Record);
310 Record.push_back(T->getNumArgs());
311 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
312 ArgI != ArgE; ++ArgI)
313 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000314 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
315 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000316 : T->getCanonicalTypeInternal(),
317 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000323 VisitArrayType(T);
324 Writer.AddStmt(T->getSizeExpr());
325 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331 const DependentSizedExtVectorType *T) {
332 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000333 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000334}
335
336void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000337ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000338 Record.push_back(T->getDepth());
339 Record.push_back(T->getIndex());
340 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000341 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343}
344
345void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000347 Record.push_back(T->getKeyword());
348 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
349 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000350 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
351 : T->getCanonicalTypeInternal(),
352 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000354}
355
356void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000357ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000358 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000359 Record.push_back(T->getKeyword());
360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddIdentifierRef(T->getIdentifier(), Record);
362 Record.push_back(T->getNumArgs());
363 for (DependentTemplateSpecializationType::iterator
364 I = T->begin(), E = T->end(); I != E; ++I)
365 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000367}
368
Douglas Gregord2fa7662010-12-20 02:24:11 +0000369void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
370 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000371 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000372 Record.push_back(*NumExpansions + 1);
373 else
374 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000375 Code = TYPE_PACK_EXPANSION;
376}
377
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000378void ASTTypeWriter::VisitParenType(const ParenType *T) {
379 Writer.AddTypeRef(T->getInnerType(), Record);
380 Code = TYPE_PAREN;
381}
382
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000384 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000385 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
386 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000391 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000392 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000393 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000394}
395
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000396void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000397 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000399}
400
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000401void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000402 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000403 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000404 for (const auto *I : T->quals())
405 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000406 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000407}
408
Steve Narofffb4330f2009-06-17 22:40:22 +0000409void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000410ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000411 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000412 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000413}
414
Eli Friedman0dfb8892011-10-06 23:00:33 +0000415void
416ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
417 Writer.AddTypeRef(T->getValueType(), Record);
418 Code = TYPE_ATOMIC;
419}
420
John McCall8f115c62009-10-16 21:56:05 +0000421namespace {
422
423class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000424 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000425 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000426
427public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000428 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000429 : Writer(Writer), Record(Record) { }
430
John McCall17001972009-10-18 01:05:36 +0000431#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000432#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000433 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000434#include "clang/AST/TypeLocNodes.def"
435
John McCall17001972009-10-18 01:05:36 +0000436 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
437 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000438};
439
440}
441
John McCall17001972009-10-18 01:05:36 +0000442void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
443 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000444}
John McCall17001972009-10-18 01:05:36 +0000445void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000446 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
447 if (TL.needsExtraLocalData()) {
448 Record.push_back(TL.getWrittenTypeSpec());
449 Record.push_back(TL.getWrittenSignSpec());
450 Record.push_back(TL.getWrittenWidthSpec());
451 Record.push_back(TL.hasModeAttr());
452 }
John McCall8f115c62009-10-16 21:56:05 +0000453}
John McCall17001972009-10-18 01:05:36 +0000454void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000456}
John McCall17001972009-10-18 01:05:36 +0000457void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000459}
Reid Kleckner8a365022013-06-24 17:51:48 +0000460void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
461 // nothing to do
462}
Reid Kleckner0503a872013-12-05 01:23:43 +0000463void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
464 // nothing to do
465}
John McCall17001972009-10-18 01:05:36 +0000466void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000468}
John McCall17001972009-10-18 01:05:36 +0000469void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000471}
John McCall17001972009-10-18 01:05:36 +0000472void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000474}
John McCall17001972009-10-18 01:05:36 +0000475void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000477 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000478}
John McCall17001972009-10-18 01:05:36 +0000479void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
481 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
482 Record.push_back(TL.getSizeExpr() ? 1 : 0);
483 if (TL.getSizeExpr())
484 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000485}
John McCall17001972009-10-18 01:05:36 +0000486void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
487 VisitArrayTypeLoc(TL);
488}
489void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
490 VisitArrayTypeLoc(TL);
491}
492void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
493 VisitArrayTypeLoc(TL);
494}
495void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
496 DependentSizedArrayTypeLoc TL) {
497 VisitArrayTypeLoc(TL);
498}
499void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
500 DependentSizedExtVectorTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
503void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
506void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getNameLoc(), Record);
508}
509void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000510 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000513 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000514 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
515 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000516}
517void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
518 VisitFunctionTypeLoc(TL);
519}
520void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
521 VisitFunctionTypeLoc(TL);
522}
John McCallb96ec562009-12-04 22:46:56 +0000523void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getNameLoc(), Record);
525}
John McCall17001972009-10-18 01:05:36 +0000526void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
529void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000530 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
531 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
532 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000533}
534void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc 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);
538 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000539}
540void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
541 Writer.AddSourceLocation(TL.getNameLoc(), Record);
542}
Alexis Hunte852b102011-05-24 22:41:36 +0000543void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
544 Writer.AddSourceLocation(TL.getKWLoc(), Record);
545 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
546 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
547 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
548}
Richard Smith30482bc2011-02-20 03:19:35 +0000549void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
551}
John McCall17001972009-10-18 01:05:36 +0000552void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
555void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
John McCall81904512011-01-06 01:58:22 +0000558void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
560 if (TL.hasAttrOperand()) {
561 SourceRange range = TL.getAttrOperandParensRange();
562 Writer.AddSourceLocation(range.getBegin(), Record);
563 Writer.AddSourceLocation(range.getEnd(), Record);
564 }
565 if (TL.hasAttrExprOperand()) {
566 Expr *operand = TL.getAttrExprOperand();
567 Record.push_back(operand ? 1 : 0);
568 if (operand) Writer.AddStmt(operand);
569 } else if (TL.hasAttrEnumOperand()) {
570 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
571 }
572}
John McCall17001972009-10-18 01:05:36 +0000573void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
John McCallcebee162009-10-18 09:09:24 +0000576void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
577 SubstTemplateTypeParmTypeLoc TL) {
578 Writer.AddSourceLocation(TL.getNameLoc(), Record);
579}
Douglas Gregorada4b792011-01-14 02:55:32 +0000580void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
581 SubstTemplateTypeParmPackTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583}
John McCall17001972009-10-18 01:05:36 +0000584void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
585 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000586 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000587 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
588 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
589 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
590 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000591 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
592 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000593}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000594void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
596 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
597}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000598void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000599 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000600 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000601}
John McCalle78aac42010-03-10 03:28:59 +0000602void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
604}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000605void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000606 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000607 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000608 Writer.AddSourceLocation(TL.getNameLoc(), Record);
609}
John McCallc392f372010-06-11 00:33:02 +0000610void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
611 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000612 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000613 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000614 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000615 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000616 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
617 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
618 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000619 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
620 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000621}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000622void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
623 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
624}
John McCall17001972009-10-18 01:05:36 +0000625void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
626 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000627}
628void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
629 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000630 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
631 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
632 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
633 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000634}
John McCallfc93cf92009-10-22 22:37:11 +0000635void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
636 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000637}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000638void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
639 Writer.AddSourceLocation(TL.getKWLoc(), Record);
640 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
641 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
642}
John McCall8f115c62009-10-16 21:56:05 +0000643
Chris Lattner19cea4e2009-04-22 05:57:30 +0000644//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000645// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000646//===----------------------------------------------------------------------===//
647
Chris Lattner28fa4e62009-04-26 22:26:21 +0000648static void EmitBlockID(unsigned ID, const char *Name,
649 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000650 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000651 Record.clear();
652 Record.push_back(ID);
653 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
654
655 // Emit the block name if present.
656 if (Name == 0 || Name[0] == 0) return;
657 Record.clear();
658 while (*Name)
659 Record.push_back(*Name++);
660 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
661}
662
663static void EmitRecordID(unsigned ID, const char *Name,
664 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000665 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000666 Record.clear();
667 Record.push_back(ID);
668 while (*Name)
669 Record.push_back(*Name++);
670 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000671}
672
673static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000674 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000675#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000676 RECORD(STMT_STOP);
677 RECORD(STMT_NULL_PTR);
678 RECORD(STMT_NULL);
679 RECORD(STMT_COMPOUND);
680 RECORD(STMT_CASE);
681 RECORD(STMT_DEFAULT);
682 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000683 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000684 RECORD(STMT_IF);
685 RECORD(STMT_SWITCH);
686 RECORD(STMT_WHILE);
687 RECORD(STMT_DO);
688 RECORD(STMT_FOR);
689 RECORD(STMT_GOTO);
690 RECORD(STMT_INDIRECT_GOTO);
691 RECORD(STMT_CONTINUE);
692 RECORD(STMT_BREAK);
693 RECORD(STMT_RETURN);
694 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000695 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000696 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000697 RECORD(EXPR_PREDEFINED);
698 RECORD(EXPR_DECL_REF);
699 RECORD(EXPR_INTEGER_LITERAL);
700 RECORD(EXPR_FLOATING_LITERAL);
701 RECORD(EXPR_IMAGINARY_LITERAL);
702 RECORD(EXPR_STRING_LITERAL);
703 RECORD(EXPR_CHARACTER_LITERAL);
704 RECORD(EXPR_PAREN);
705 RECORD(EXPR_UNARY_OPERATOR);
706 RECORD(EXPR_SIZEOF_ALIGN_OF);
707 RECORD(EXPR_ARRAY_SUBSCRIPT);
708 RECORD(EXPR_CALL);
709 RECORD(EXPR_MEMBER);
710 RECORD(EXPR_BINARY_OPERATOR);
711 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
712 RECORD(EXPR_CONDITIONAL_OPERATOR);
713 RECORD(EXPR_IMPLICIT_CAST);
714 RECORD(EXPR_CSTYLE_CAST);
715 RECORD(EXPR_COMPOUND_LITERAL);
716 RECORD(EXPR_EXT_VECTOR_ELEMENT);
717 RECORD(EXPR_INIT_LIST);
718 RECORD(EXPR_DESIGNATED_INIT);
719 RECORD(EXPR_IMPLICIT_VALUE_INIT);
720 RECORD(EXPR_VA_ARG);
721 RECORD(EXPR_ADDR_LABEL);
722 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000723 RECORD(EXPR_CHOOSE);
724 RECORD(EXPR_GNU_NULL);
725 RECORD(EXPR_SHUFFLE_VECTOR);
726 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000727 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000728 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000729 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000730 RECORD(EXPR_OBJC_ARRAY_LITERAL);
731 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000732 RECORD(EXPR_OBJC_ENCODE);
733 RECORD(EXPR_OBJC_SELECTOR_EXPR);
734 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
735 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
736 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
737 RECORD(EXPR_OBJC_KVC_REF_EXPR);
738 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000739 RECORD(STMT_OBJC_FOR_COLLECTION);
740 RECORD(STMT_OBJC_CATCH);
741 RECORD(STMT_OBJC_FINALLY);
742 RECORD(STMT_OBJC_AT_TRY);
743 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
744 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000745 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000746 RECORD(EXPR_CXX_OPERATOR_CALL);
747 RECORD(EXPR_CXX_CONSTRUCT);
748 RECORD(EXPR_CXX_STATIC_CAST);
749 RECORD(EXPR_CXX_DYNAMIC_CAST);
750 RECORD(EXPR_CXX_REINTERPRET_CAST);
751 RECORD(EXPR_CXX_CONST_CAST);
752 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000753 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000754 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000755 RECORD(EXPR_CXX_BOOL_LITERAL);
756 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000757 RECORD(EXPR_CXX_TYPEID_EXPR);
758 RECORD(EXPR_CXX_TYPEID_TYPE);
759 RECORD(EXPR_CXX_UUIDOF_EXPR);
760 RECORD(EXPR_CXX_UUIDOF_TYPE);
761 RECORD(EXPR_CXX_THIS);
762 RECORD(EXPR_CXX_THROW);
763 RECORD(EXPR_CXX_DEFAULT_ARG);
764 RECORD(EXPR_CXX_BIND_TEMPORARY);
765 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
766 RECORD(EXPR_CXX_NEW);
767 RECORD(EXPR_CXX_DELETE);
768 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
769 RECORD(EXPR_EXPR_WITH_CLEANUPS);
770 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
771 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
772 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
773 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
774 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000775 RECORD(EXPR_CXX_NOEXCEPT);
776 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000777 RECORD(EXPR_PACK_EXPANSION);
778 RECORD(EXPR_SIZEOF_PACK);
779 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000780 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000781#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000782}
Mike Stump11289f42009-09-09 15:08:12 +0000783
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000784void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000785 RecordData Record;
786 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000787
Sebastian Redl539c5062010-08-18 23:57:32 +0000788#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
789#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000790
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000791 // Control Block.
792 BLOCK(CONTROL_BLOCK);
793 RECORD(METADATA);
794 RECORD(IMPORTS);
795 RECORD(LANGUAGE_OPTIONS);
796 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000797 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000798 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000799 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000800 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000801 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000802 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000803 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000804 RECORD(PREPROCESSOR_OPTIONS);
805
Douglas Gregor108cb222012-10-19 00:45:00 +0000806 BLOCK(INPUT_FILES_BLOCK);
807 RECORD(INPUT_FILE);
808
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000809 // AST Top-Level Block.
810 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000811 RECORD(TYPE_OFFSET);
812 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000813 RECORD(IDENTIFIER_OFFSET);
814 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000815 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000816 RECORD(SPECIAL_TYPES);
817 RECORD(STATISTICS);
818 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000819 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000820 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000821 RECORD(SELECTOR_OFFSETS);
822 RECORD(METHOD_POOL);
823 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000824 RECORD(SOURCE_LOCATION_OFFSETS);
825 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000826 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000827 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000828 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000829 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000830 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000831 RECORD(SEMA_DECL_REFS);
832 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
833 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
834 RECORD(DECL_REPLACEMENTS);
835 RECORD(UPDATE_VISIBLE);
836 RECORD(DECL_UPDATE_OFFSETS);
837 RECORD(DECL_UPDATES);
838 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
839 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000840 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000841 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000842 RECORD(FP_PRAGMA_OPTIONS);
843 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000844 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000845 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000846 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000847 RECORD(MODULE_OFFSET_MAP);
848 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000849 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000850 RECORD(FILE_SORTED_DECLS);
851 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000852 RECORD(MERGED_DECLARATIONS);
853 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000854 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000855 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000856 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000857 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000858
Chris Lattner28fa4e62009-04-26 22:26:21 +0000859 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000860 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000861 RECORD(SM_SLOC_FILE_ENTRY);
862 RECORD(SM_SLOC_BUFFER_ENTRY);
863 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000864 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattner28fa4e62009-04-26 22:26:21 +0000866 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000867 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000868 RECORD(PP_MACRO_OBJECT_LIKE);
869 RECORD(PP_MACRO_FUNCTION_LIKE);
870 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000871
Douglas Gregor12bfa382009-10-17 00:13:19 +0000872 // Decls and Types block.
873 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000874 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000875 RECORD(TYPE_COMPLEX);
876 RECORD(TYPE_POINTER);
877 RECORD(TYPE_BLOCK_POINTER);
878 RECORD(TYPE_LVALUE_REFERENCE);
879 RECORD(TYPE_RVALUE_REFERENCE);
880 RECORD(TYPE_MEMBER_POINTER);
881 RECORD(TYPE_CONSTANT_ARRAY);
882 RECORD(TYPE_INCOMPLETE_ARRAY);
883 RECORD(TYPE_VARIABLE_ARRAY);
884 RECORD(TYPE_VECTOR);
885 RECORD(TYPE_EXT_VECTOR);
886 RECORD(TYPE_FUNCTION_PROTO);
887 RECORD(TYPE_FUNCTION_NO_PROTO);
888 RECORD(TYPE_TYPEDEF);
889 RECORD(TYPE_TYPEOF_EXPR);
890 RECORD(TYPE_TYPEOF);
891 RECORD(TYPE_RECORD);
892 RECORD(TYPE_ENUM);
893 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000894 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000895 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000896 RECORD(TYPE_DECLTYPE);
897 RECORD(TYPE_ELABORATED);
898 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
899 RECORD(TYPE_UNRESOLVED_USING);
900 RECORD(TYPE_INJECTED_CLASS_NAME);
901 RECORD(TYPE_OBJC_OBJECT);
902 RECORD(TYPE_TEMPLATE_TYPE_PARM);
903 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
904 RECORD(TYPE_DEPENDENT_NAME);
905 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
906 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
907 RECORD(TYPE_PAREN);
908 RECORD(TYPE_PACK_EXPANSION);
909 RECORD(TYPE_ATTRIBUTED);
910 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000911 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000912 RECORD(DECL_TYPEDEF);
913 RECORD(DECL_ENUM);
914 RECORD(DECL_RECORD);
915 RECORD(DECL_ENUM_CONSTANT);
916 RECORD(DECL_FUNCTION);
917 RECORD(DECL_OBJC_METHOD);
918 RECORD(DECL_OBJC_INTERFACE);
919 RECORD(DECL_OBJC_PROTOCOL);
920 RECORD(DECL_OBJC_IVAR);
921 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000922 RECORD(DECL_OBJC_CATEGORY);
923 RECORD(DECL_OBJC_CATEGORY_IMPL);
924 RECORD(DECL_OBJC_IMPLEMENTATION);
925 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
926 RECORD(DECL_OBJC_PROPERTY);
927 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000928 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000929 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000930 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000931 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000932 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000933 RECORD(DECL_FILE_SCOPE_ASM);
934 RECORD(DECL_BLOCK);
935 RECORD(DECL_CONTEXT_LEXICAL);
936 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000937 RECORD(DECL_NAMESPACE);
938 RECORD(DECL_NAMESPACE_ALIAS);
939 RECORD(DECL_USING);
940 RECORD(DECL_USING_SHADOW);
941 RECORD(DECL_USING_DIRECTIVE);
942 RECORD(DECL_UNRESOLVED_USING_VALUE);
943 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
944 RECORD(DECL_LINKAGE_SPEC);
945 RECORD(DECL_CXX_RECORD);
946 RECORD(DECL_CXX_METHOD);
947 RECORD(DECL_CXX_CONSTRUCTOR);
948 RECORD(DECL_CXX_DESTRUCTOR);
949 RECORD(DECL_CXX_CONVERSION);
950 RECORD(DECL_ACCESS_SPEC);
951 RECORD(DECL_FRIEND);
952 RECORD(DECL_FRIEND_TEMPLATE);
953 RECORD(DECL_CLASS_TEMPLATE);
954 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
955 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000956 RECORD(DECL_VAR_TEMPLATE);
957 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
958 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000959 RECORD(DECL_FUNCTION_TEMPLATE);
960 RECORD(DECL_TEMPLATE_TYPE_PARM);
961 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
962 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
963 RECORD(DECL_STATIC_ASSERT);
964 RECORD(DECL_CXX_BASE_SPECIFIERS);
965 RECORD(DECL_INDIRECTFIELD);
966 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
967
Douglas Gregor03412ba2011-06-03 02:27:19 +0000968 // Statements and Exprs can occur in the Decls and Types block.
969 AddStmtsExprs(Stream, Record);
970
Douglas Gregor92a96f52011-02-08 21:58:10 +0000971 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000972 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000973 RECORD(PPD_MACRO_DEFINITION);
974 RECORD(PPD_INCLUSION_DIRECTIVE);
975
Chris Lattner28fa4e62009-04-26 22:26:21 +0000976#undef RECORD
977#undef BLOCK
978 Stream.ExitBlock();
979}
980
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000981/// \brief Adjusts the given filename to only write out the portion of the
982/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000983///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000984/// \param Filename the file name to adjust.
985///
986/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
987/// the returned filename will be adjusted by this system root.
988///
989/// \returns either the original filename (if it needs no adjustment) or the
990/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000991static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000992adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000993 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000994
Douglas Gregorc567ba22011-07-22 16:35:34 +0000995 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000996 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000997
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000998 // Verify that the filename and the system root have the same prefix.
999 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001000 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001001 if (Filename[Pos] != isysroot[Pos])
1002 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001003
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001004 // We hit the end of the filename before we hit the end of the system root.
1005 if (!Filename[Pos])
1006 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001007
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001008 // If the file name has a '/' at the current position, skip over the '/'.
1009 // We distinguish sysroot-based includes from absolute includes by the
1010 // absence of '/' at the beginning of sysroot-based includes.
1011 if (Filename[Pos] == '/')
1012 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001013
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001014 return Filename + Pos;
1015}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001016
Douglas Gregor112b9072012-10-18 05:31:06 +00001017/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001018void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1019 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001020 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001021 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001022 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1023 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001024
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001025 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001026 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1027 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1028 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1029 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1030 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1031 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1032 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1033 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1035 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1036 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001037 Record.push_back(VERSION_MAJOR);
1038 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001039 Record.push_back(CLANG_VERSION_MAJOR);
1040 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001041 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001042 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001043 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1044 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001045
Douglas Gregor112b9072012-10-18 05:31:06 +00001046 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001047 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001048 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001049 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001050
1051 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1052 M != MEnd; ++M) {
1053 // Skip modules that weren't directly imported.
1054 if (!(*M)->isDirectlyImported())
1055 continue;
1056
1057 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001058 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001059 Record.push_back((*M)->File->getSize());
1060 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001061 // FIXME: This writes the absolute path for AST files we depend on.
1062 const std::string &FileName = (*M)->FileName;
1063 Record.push_back(FileName.size());
1064 Record.append(FileName.begin(), FileName.end());
1065 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001066 Stream.EmitRecord(IMPORTS, Record);
1067 }
Mike Stump11289f42009-09-09 15:08:12 +00001068
Douglas Gregor112b9072012-10-18 05:31:06 +00001069 // Language options.
1070 Record.clear();
1071 const LangOptions &LangOpts = Context.getLangOpts();
1072#define LANGOPT(Name, Bits, Default, Description) \
1073 Record.push_back(LangOpts.Name);
1074#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1075 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1076#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001077#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1078#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001079
1080 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1081 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1082
1083 Record.push_back(LangOpts.CurrentModule.size());
1084 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001085
1086 // Comment options.
1087 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1088 for (CommentOptions::BlockCommandNamesTy::const_iterator
1089 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1090 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1091 I != IEnd; ++I) {
1092 AddString(*I, Record);
1093 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001094 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001095
Douglas Gregor112b9072012-10-18 05:31:06 +00001096 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1097
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001098 // Target options.
1099 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001100 const TargetInfo &Target = Context.getTargetInfo();
1101 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001102 AddString(TargetOpts.Triple, Record);
1103 AddString(TargetOpts.CPU, Record);
1104 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001105 AddString(TargetOpts.LinkerVersion, Record);
1106 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1107 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1108 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1109 }
1110 Record.push_back(TargetOpts.Features.size());
1111 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1112 AddString(TargetOpts.Features[I], Record);
1113 }
1114 Stream.EmitRecord(TARGET_OPTIONS, Record);
1115
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001116 // Diagnostic options.
1117 Record.clear();
1118 const DiagnosticOptions &DiagOpts
1119 = Context.getDiagnostics().getDiagnosticOptions();
1120#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1121#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1122 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1123#include "clang/Basic/DiagnosticOptions.def"
1124 Record.push_back(DiagOpts.Warnings.size());
1125 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1126 AddString(DiagOpts.Warnings[I], Record);
1127 // Note: we don't serialize the log or serialization file names, because they
1128 // are generally transient files and will almost always be overridden.
1129 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1130
Douglas Gregorc6317db2012-10-24 15:49:58 +00001131 // File system options.
1132 Record.clear();
1133 const FileSystemOptions &FSOpts
1134 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1135 AddString(FSOpts.WorkingDir, Record);
1136 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1137
Douglas Gregor2d302362012-10-24 16:50:34 +00001138 // Header search options.
1139 Record.clear();
1140 const HeaderSearchOptions &HSOpts
1141 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1142 AddString(HSOpts.Sysroot, Record);
1143
1144 // Include entries.
1145 Record.push_back(HSOpts.UserEntries.size());
1146 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1147 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1148 AddString(Entry.Path, Record);
1149 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001150 Record.push_back(Entry.IsFramework);
1151 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001152 }
1153
1154 // System header prefixes.
1155 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1156 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1157 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1158 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1159 }
1160
1161 AddString(HSOpts.ResourceDir, Record);
1162 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001163 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001164 Record.push_back(HSOpts.DisableModuleHash);
1165 Record.push_back(HSOpts.UseBuiltinIncludes);
1166 Record.push_back(HSOpts.UseStandardSystemIncludes);
1167 Record.push_back(HSOpts.UseStandardCXXIncludes);
1168 Record.push_back(HSOpts.UseLibcxx);
1169 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1170
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001171 // Preprocessor options.
1172 Record.clear();
1173 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1174
1175 // Macro definitions.
1176 Record.push_back(PPOpts.Macros.size());
1177 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1178 AddString(PPOpts.Macros[I].first, Record);
1179 Record.push_back(PPOpts.Macros[I].second);
1180 }
1181
1182 // Includes
1183 Record.push_back(PPOpts.Includes.size());
1184 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1185 AddString(PPOpts.Includes[I], Record);
1186
1187 // Macro includes
1188 Record.push_back(PPOpts.MacroIncludes.size());
1189 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1190 AddString(PPOpts.MacroIncludes[I], Record);
1191
Douglas Gregorb6368752012-10-24 23:41:50 +00001192 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001193 // Detailed record is important since it is used for the module cache hash.
1194 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001195 AddString(PPOpts.ImplicitPCHInclude, Record);
1196 AddString(PPOpts.ImplicitPTHInclude, Record);
1197 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1198 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1199
Douglas Gregora3b20262011-05-06 21:43:30 +00001200 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001201 SourceManager &SM = Context.getSourceManager();
1202 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1203 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001204 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1205 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001206 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1207 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1208
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001209 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001210
Michael J. Spencer740857f2010-12-21 16:45:57 +00001211 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001212
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001213 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001214 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001215 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001216 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001217 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001218 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001219 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001220 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001221
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001222 Record.clear();
1223 Record.push_back(SM.getMainFileID().getOpaqueValue());
1224 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1225
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001226 // Original PCH directory
1227 if (!OutputFile.empty() && OutputFile != "-") {
1228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1229 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1231 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1232
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001233 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001234
1235 llvm::sys::fs::make_absolute(OutputPath);
1236 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1237
1238 RecordData Record;
1239 Record.push_back(ORIGINAL_PCH_DIR);
1240 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1241 }
1242
Douglas Gregor49491f72013-03-15 22:15:07 +00001243 WriteInputFiles(Context.SourceMgr,
1244 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001245 isysroot,
1246 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001247 Stream.ExitBlock();
1248}
1249
Douglas Gregor49491f72013-03-15 22:15:07 +00001250namespace {
1251 /// \brief An input file.
1252 struct InputFileEntry {
1253 const FileEntry *File;
1254 bool IsSystemFile;
1255 bool BufferOverridden;
1256 };
1257}
1258
1259void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1260 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001261 StringRef isysroot,
1262 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001263 using namespace llvm;
1264 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1265 RecordData Record;
1266
1267 // Create input-file abbreviation.
1268 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1269 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001270 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001271 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1272 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001273 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001274 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1275 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1276
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001277 // Get all ContentCache objects for files, sorted by whether the file is a
1278 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001279 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001280 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1281 // Get this source location entry.
1282 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001283 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001284
1285 // We only care about file entries that were not overridden.
1286 if (!SLoc->isFile())
1287 continue;
1288 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001289 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001290 continue;
1291
Douglas Gregor49491f72013-03-15 22:15:07 +00001292 InputFileEntry Entry;
1293 Entry.File = Cache->OrigEntry;
1294 Entry.IsSystemFile = Cache->IsSystemFile;
1295 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001296 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001297 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001298 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001299 SortedFiles.push_front(Entry);
1300 }
1301
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001302 unsigned UserFilesNum = 0;
1303 // Write out all of the input files.
1304 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001305 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001306 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001307 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001308
Douglas Gregor49491f72013-03-15 22:15:07 +00001309 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001310 if (InputFileID != 0)
1311 continue; // already recorded this file.
1312
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001313 // Record this entry's offset.
1314 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001315
1316 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001317
Douglas Gregor49491f72013-03-15 22:15:07 +00001318 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001319 ++UserFilesNum;
1320
Douglas Gregor72be3902012-10-19 00:38:02 +00001321 Record.clear();
1322 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001323 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001324
1325 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001326 Record.push_back(Entry.File->getSize());
1327 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001328
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001329 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001330 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001331
Douglas Gregor72be3902012-10-19 00:38:02 +00001332 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001333 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001334 SmallString<128> FilePath(Filename);
1335
1336 // Ask the file manager to fixup the relative path for us. This will
1337 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001338 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001339
1340 // FIXME: This call to make_absolute shouldn't be necessary, the
1341 // call to FixupRelativePath should always return an absolute path.
1342 llvm::sys::fs::make_absolute(FilePath);
1343 Filename = FilePath.c_str();
1344
1345 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1346
1347 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1348 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001349
Douglas Gregor112b9072012-10-18 05:31:06 +00001350 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001351
1352 // Create input file offsets abbreviation.
1353 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1354 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1355 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001356 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1357 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001358 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1359 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1360
1361 // Write input file offsets.
1362 Record.clear();
1363 Record.push_back(INPUT_FILE_OFFSETS);
1364 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001365 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001366 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001367}
1368
Douglas Gregora7f71a92009-04-10 03:52:48 +00001369//===----------------------------------------------------------------------===//
1370// Source Manager Serialization
1371//===----------------------------------------------------------------------===//
1372
1373/// \brief Create an abbreviation for the SLocEntry that refers to a
1374/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001375static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001376 using namespace llvm;
1377 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001378 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001383 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001388 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001389}
1390
1391/// \brief Create an abbreviation for the SLocEntry that refers to a
1392/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001393static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001394 using namespace llvm;
1395 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001396 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001402 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001403}
1404
1405/// \brief Create an abbreviation for the SLocEntry that refers to a
1406/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001407static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001408 using namespace llvm;
1409 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001410 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001412 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001413}
1414
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001415/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1416/// expansion.
1417static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001418 using namespace llvm;
1419 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001420 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001421 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001426 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001427}
1428
Douglas Gregor09b69892011-02-10 17:09:37 +00001429namespace {
1430 // Trait used for the on-disk hash table of header search information.
1431 class HeaderFileInfoTrait {
1432 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001433 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001434
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001435 // Keep track of the framework names we've used during serialization.
1436 SmallVector<char, 128> FrameworkStringData;
1437 llvm::StringMap<unsigned> FrameworkNameOffset;
1438
Douglas Gregor09b69892011-02-10 17:09:37 +00001439 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001440 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1441 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001442
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001443 struct key_type {
1444 const FileEntry *FE;
1445 const char *Filename;
1446 };
1447 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001448
1449 typedef HeaderFileInfo data_type;
1450 typedef const data_type &data_type_ref;
1451
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001452 static unsigned ComputeHash(key_type_ref key) {
1453 // The hash is based only on size/time of the file, so that the reader can
1454 // match even when symlinking or excess path elements ("foo/../", "../")
1455 // change the form of the name. However, complete path is still the key.
1456 return llvm::hash_combine(key.FE->getSize(),
1457 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001458 }
1459
1460 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001461 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1462 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1463 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001464 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001465 if (Data.isModuleHeader)
1466 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001467 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001468 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001469 }
1470
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001471 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1472 clang::io::Emit64(Out, key.FE->getSize());
1473 KeyLen -= 8;
1474 clang::io::Emit64(Out, key.FE->getModificationTime());
1475 KeyLen -= 8;
1476 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001477 }
1478
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001479 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001480 data_type_ref Data, unsigned DataLen) {
1481 using namespace clang::io;
1482 uint64_t Start = Out.tell(); (void)Start;
1483
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001484 unsigned char Flags = (Data.HeaderRole << 6)
1485 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001486 | (Data.isPragmaOnce << 4)
1487 | (Data.DirInfo << 2)
1488 | (Data.Resolved << 1)
1489 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001490 Emit8(Out, (uint8_t)Flags);
1491 Emit16(Out, (uint16_t) Data.NumIncludes);
1492
1493 if (!Data.ControllingMacro)
1494 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1495 else
1496 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001497
1498 unsigned Offset = 0;
1499 if (!Data.Framework.empty()) {
1500 // If this header refers into a framework, save the framework name.
1501 llvm::StringMap<unsigned>::iterator Pos
1502 = FrameworkNameOffset.find(Data.Framework);
1503 if (Pos == FrameworkNameOffset.end()) {
1504 Offset = FrameworkStringData.size() + 1;
1505 FrameworkStringData.append(Data.Framework.begin(),
1506 Data.Framework.end());
1507 FrameworkStringData.push_back(0);
1508
1509 FrameworkNameOffset[Data.Framework] = Offset;
1510 } else
1511 Offset = Pos->second;
1512 }
1513 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001514
1515 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001516 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001517 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1518 }
1519
Douglas Gregor09b69892011-02-10 17:09:37 +00001520 assert(Out.tell() - Start == DataLen && "Wrong data length");
1521 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001522
1523 const char *strings_begin() const { return FrameworkStringData.begin(); }
1524 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001525 };
1526} // end anonymous namespace
1527
1528/// \brief Write the header search block for the list of files that
1529///
1530/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001531void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001532 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001533 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1534
1535 if (FilesByUID.size() > HS.header_file_size())
1536 FilesByUID.resize(HS.header_file_size());
1537
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001538 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001539 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001540 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001541 unsigned NumHeaderSearchEntries = 0;
1542 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1543 const FileEntry *File = FilesByUID[UID];
1544 if (!File)
1545 continue;
1546
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001547 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1548 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001549 HeaderFileInfo HFI;
1550 if (!HS.tryGetFileInfo(File, HFI) ||
1551 (HFI.External && Chain) ||
1552 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001553 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001554
1555 // Turn the file name into an absolute path, if it isn't already.
1556 const char *Filename = File->getName();
1557 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1558
1559 // If we performed any translation on the file name at all, we need to
1560 // save this string, since the generator will refer to it later.
1561 if (Filename != File->getName()) {
1562 Filename = strdup(Filename);
1563 SavedStrings.push_back(Filename);
1564 }
1565
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001566 HeaderFileInfoTrait::key_type key = { File, Filename };
1567 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001568 ++NumHeaderSearchEntries;
1569 }
1570
1571 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001572 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001573 uint32_t BucketOffset;
1574 {
1575 llvm::raw_svector_ostream Out(TableData);
1576 // Make sure that no bucket is at offset 0
1577 clang::io::Emit32(Out, 0);
1578 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1579 }
1580
1581 // Create a blob abbreviation
1582 using namespace llvm;
1583 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1584 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1589 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1590
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001591 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001592 RecordData Record;
1593 Record.push_back(HEADER_SEARCH_TABLE);
1594 Record.push_back(BucketOffset);
1595 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001596 Record.push_back(TableData.size());
1597 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001598 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1599
1600 // Free all of the strings we had to duplicate.
1601 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001602 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001603}
1604
Douglas Gregora7f71a92009-04-10 03:52:48 +00001605/// \brief Writes the block containing the serialized form of the
1606/// source manager.
1607///
1608/// TODO: We should probably use an on-disk hash table (stored in a
1609/// blob), indexed based on the file name, so that we only create
1610/// entries for files that we actually need. In the common case (no
1611/// errors), we probably won't have to create file entries for any of
1612/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001613void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001614 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001615 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001616 RecordData Record;
1617
Chris Lattner0910e3b2009-04-10 17:16:57 +00001618 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001619 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001620
1621 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001622 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1623 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1624 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001625 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001626
Douglas Gregor258ae542009-04-27 06:38:32 +00001627 // Write out the source location entry table. We skip the first
1628 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001629 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001630 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001631 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1632 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001633 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001634 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001635 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001636 FileID FID = FileID::get(I);
1637 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001638
Douglas Gregor258ae542009-04-27 06:38:32 +00001639 // Record the offset of this source-location entry.
1640 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1641
1642 // Figure out which record code to use.
1643 unsigned Code;
1644 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001645 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1646 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001647 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001648 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001649 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001650 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001651 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001652 Record.clear();
1653 Record.push_back(Code);
1654
Douglas Gregor925296b2011-07-19 16:10:42 +00001655 // Starting offset of this entry within this module, so skip the dummy.
1656 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001657 if (SLoc->isFile()) {
1658 const SrcMgr::FileInfo &File = SLoc->getFile();
1659 Record.push_back(File.getIncludeLoc().getRawEncoding());
1660 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1661 Record.push_back(File.hasLineDirectives());
1662
1663 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001664 if (Content->OrigEntry) {
1665 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001666 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001667
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001668 // The source location entry is a file. Emit input file ID.
1669 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1670 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001671
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001672 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001673
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001674 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001675 if (FDI != FileDeclIDs.end()) {
1676 Record.push_back(FDI->second->FirstDeclIndex);
1677 Record.push_back(FDI->second->DeclIDs.size());
1678 } else {
1679 Record.push_back(0);
1680 Record.push_back(0);
1681 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001682
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001683 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001684
1685 if (Content->BufferOverridden) {
1686 Record.clear();
1687 Record.push_back(SM_SLOC_BUFFER_BLOB);
1688 const llvm::MemoryBuffer *Buffer
1689 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1690 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1691 StringRef(Buffer->getBufferStart(),
1692 Buffer->getBufferSize() + 1));
1693 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001694 } else {
1695 // The source location entry is a buffer. The blob associated
1696 // with this entry contains the contents of the buffer.
1697
1698 // We add one to the size so that we capture the trailing NULL
1699 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1700 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001701 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001702 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001703 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001704 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001705 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001706 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001707 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001708 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001709 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001710 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001711
Douglas Gregor925296b2011-07-19 16:10:42 +00001712 if (strcmp(Name, "<built-in>") == 0) {
1713 PreloadSLocs.push_back(SLocEntryOffsets.size());
1714 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001715 }
1716 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001717 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001718 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001719 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1720 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001721 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1722 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001723
1724 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001725 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001726 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001727 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001728 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001729 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001730 }
1731 }
1732
Douglas Gregor8f45df52009-04-16 22:23:12 +00001733 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001734
1735 if (SLocEntryOffsets.empty())
1736 return;
1737
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001738 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001739 // table is used for lazily loading source-location information.
1740 using namespace llvm;
1741 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001742 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1746 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001747
Douglas Gregor258ae542009-04-27 06:38:32 +00001748 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001749 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001750 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001751 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001752 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001753
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001754 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001755 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001756 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001757
1758 // Write the line table. It depends on remapping working, so it must come
1759 // after the source location offsets.
1760 if (SourceMgr.hasLineTable()) {
1761 LineTableInfo &LineTable = SourceMgr.getLineTable();
1762
1763 Record.clear();
1764 // Emit the file names
1765 Record.push_back(LineTable.getNumFilenames());
1766 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1767 // Emit the file name
1768 const char *Filename = LineTable.getFilename(I);
1769 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1770 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1771 Record.push_back(FilenameLen);
1772 if (FilenameLen)
1773 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1774 }
1775
1776 // Emit the line entries
1777 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1778 L != LEnd; ++L) {
1779 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001780 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001781 continue;
1782
1783 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001784 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001785
1786 // Emit the line entries
1787 Record.push_back(L->second.size());
1788 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1789 LEEnd = L->second.end();
1790 LE != LEEnd; ++LE) {
1791 Record.push_back(LE->FileOffset);
1792 Record.push_back(LE->LineNo);
1793 Record.push_back(LE->FilenameID);
1794 Record.push_back((unsigned)LE->FileKind);
1795 Record.push_back(LE->IncludeOffset);
1796 }
1797 }
1798 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1799 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001800}
1801
Douglas Gregorc5046832009-04-27 18:38:38 +00001802//===----------------------------------------------------------------------===//
1803// Preprocessor Serialization
1804//===----------------------------------------------------------------------===//
1805
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001806namespace {
1807class ASTMacroTableTrait {
1808public:
1809 typedef IdentID key_type;
1810 typedef key_type key_type_ref;
1811
1812 struct Data {
1813 uint32_t MacroDirectivesOffset;
1814 };
1815
1816 typedef Data data_type;
1817 typedef const data_type &data_type_ref;
1818
1819 static unsigned ComputeHash(IdentID IdID) {
1820 return llvm::hash_value(IdID);
1821 }
1822
1823 std::pair<unsigned,unsigned>
1824 static EmitKeyDataLength(raw_ostream& Out,
1825 key_type_ref Key, data_type_ref Data) {
1826 unsigned KeyLen = 4; // IdentID.
1827 unsigned DataLen = 4; // MacroDirectivesOffset.
1828 return std::make_pair(KeyLen, DataLen);
1829 }
1830
1831 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1832 clang::io::Emit32(Out, Key);
1833 }
1834
1835 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1836 unsigned) {
1837 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1838 }
1839};
1840} // end anonymous namespace
1841
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001842static int compareMacroDirectives(
1843 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1844 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1845 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001846}
1847
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001848static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1849 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001850 if (MacroInfo *MI = MD->getMacroInfo())
1851 if (MI->isBuiltinMacro())
1852 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001853
1854 if (IsModule) {
1855 SourceLocation Loc = MD->getLocation();
1856 if (Loc.isInvalid())
1857 return true;
1858 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1859 return true;
1860 }
1861
1862 return false;
1863}
1864
Chris Lattnereeffaef2009-04-10 17:15:23 +00001865/// \brief Writes the block containing the serialized form of the
1866/// preprocessor.
1867///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001868void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001869 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1870 if (PPRec)
1871 WritePreprocessorDetail(*PPRec);
1872
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001873 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001874
Chris Lattner0af3ba12009-04-13 01:29:17 +00001875 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1876 if (PP.getCounterValue() != 0) {
1877 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001878 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001879 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001880 }
1881
1882 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001883 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001884
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001885 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001886 // FIXME: use diagnostics subsystem for localization etc.
1887 if (PP.SawDateOrTime())
1888 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001889
Douglas Gregor796d76a2010-10-20 22:00:55 +00001890
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001891 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001892 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001893
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001894 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001895 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001896 MacroDirectives;
1897 for (Preprocessor::macro_iterator
1898 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1899 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001900 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001901 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001902 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001903
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001904 // Sort the set of macro definitions that need to be serialized by the
1905 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001906 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1907 &compareMacroDirectives);
1908
1909 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1910
1911 // Emit the macro directives as a list and associate the offset with the
1912 // identifier they belong to.
1913 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1914 const IdentifierInfo *Name = MacroDirectives[I].first;
1915 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1916 MacroDirective *MD = MacroDirectives[I].second;
1917
1918 // If the macro or identifier need no updates, don't write the macro history
1919 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001920 // FIXME: Chain the macro history instead of re-writing it.
1921 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001922 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1923 continue;
1924
1925 // Emit the macro directives in reverse source order.
1926 for (; MD; MD = MD->getPrevious()) {
1927 if (shouldIgnoreMacro(MD, IsModule, PP))
1928 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001929
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001930 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001931 Record.push_back(MD->getKind());
1932 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1933 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1934 Record.push_back(InfoID);
1935 Record.push_back(DefMD->isImported());
1936 Record.push_back(DefMD->isAmbiguous());
1937
1938 } else if (VisibilityMacroDirective *
1939 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1940 Record.push_back(VisMD->isPublic());
1941 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001942 }
1943 if (Record.empty())
1944 continue;
1945
1946 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1947 Record.clear();
1948
1949 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1950
1951 IdentID NameID = getIdentifierRef(Name);
1952 ASTMacroTableTrait::Data data;
1953 data.MacroDirectivesOffset = MacroDirectiveOffset;
1954 Generator.insert(NameID, data);
1955 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001956
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001957 /// \brief Offsets of each of the macros into the bitstream, indexed by
1958 /// the local macro ID
1959 ///
1960 /// For each identifier that is associated with a macro, this map
1961 /// provides the offset into the bitstream where that macro is
1962 /// defined.
1963 std::vector<uint32_t> MacroOffsets;
1964
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001965 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1966 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1967 MacroInfo *MI = MacroInfosToEmit[I].MI;
1968 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001969
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001970 if (ID < FirstMacroID) {
1971 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1972 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001973 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001974
1975 // Record the local offset of this macro.
1976 unsigned Index = ID - FirstMacroID;
1977 if (Index == MacroOffsets.size())
1978 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1979 else {
1980 if (Index > MacroOffsets.size())
1981 MacroOffsets.resize(Index + 1);
1982
1983 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1984 }
1985
1986 AddIdentifierRef(Name, Record);
1987 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1988 AddSourceLocation(MI->getDefinitionLoc(), Record);
1989 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1990 Record.push_back(MI->isUsed());
1991 unsigned Code;
1992 if (MI->isObjectLike()) {
1993 Code = PP_MACRO_OBJECT_LIKE;
1994 } else {
1995 Code = PP_MACRO_FUNCTION_LIKE;
1996
1997 Record.push_back(MI->isC99Varargs());
1998 Record.push_back(MI->isGNUVarargs());
1999 Record.push_back(MI->hasCommaPasting());
2000 Record.push_back(MI->getNumArgs());
2001 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2002 I != E; ++I)
2003 AddIdentifierRef(*I, Record);
2004 }
2005
2006 // If we have a detailed preprocessing record, record the macro definition
2007 // ID that corresponds to this macro.
2008 if (PPRec)
2009 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2010
2011 Stream.EmitRecord(Code, Record);
2012 Record.clear();
2013
2014 // Emit the tokens array.
2015 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2016 // Note that we know that the preprocessor does not have any annotation
2017 // tokens in it because they are created by the parser, and thus can't
2018 // be in a macro definition.
2019 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002020 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002021 Stream.EmitRecord(PP_TOKEN, Record);
2022 Record.clear();
2023 }
2024 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002025 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002026
Douglas Gregor92a96f52011-02-08 21:58:10 +00002027 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002028
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002029 // Create the on-disk hash table in a buffer.
2030 SmallString<4096> MacroTable;
2031 uint32_t BucketOffset;
2032 {
2033 llvm::raw_svector_ostream Out(MacroTable);
2034 // Make sure that no bucket is at offset 0
2035 clang::io::Emit32(Out, 0);
2036 BucketOffset = Generator.Emit(Out);
2037 }
2038
2039 // Write the macro table
2040 using namespace llvm;
2041 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2042 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2045 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2046
2047 Record.push_back(MACRO_TABLE);
2048 Record.push_back(BucketOffset);
2049 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2050 Record.clear();
2051
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002052 // Write the offsets table for macro IDs.
2053 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002054 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002055 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2059
2060 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2061 Record.clear();
2062 Record.push_back(MACRO_OFFSET);
2063 Record.push_back(MacroOffsets.size());
2064 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2065 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2066 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002067}
2068
2069void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002070 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002071 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002072
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002073 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002074
Douglas Gregor92a96f52011-02-08 21:58:10 +00002075 // Enter the preprocessor block.
2076 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002077
Douglas Gregoraae92242010-03-19 21:51:54 +00002078 // If the preprocessor has a preprocessing record, emit it.
2079 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002080 using namespace llvm;
2081
2082 // Set up the abbreviation for
2083 unsigned InclusionAbbrev = 0;
2084 {
2085 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2086 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2092 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2093 }
2094
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002095 unsigned FirstPreprocessorEntityID
2096 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2097 + NUM_PREDEF_PP_ENTITY_IDS;
2098 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002099 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002100 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2101 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002102 E != EEnd;
2103 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002104 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002105
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002106 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2107 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002108
Douglas Gregor92a96f52011-02-08 21:58:10 +00002109 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002110 // Record this macro definition's ID.
2111 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002112
Douglas Gregor92a96f52011-02-08 21:58:10 +00002113 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002114 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2115 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002116 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002117
Chandler Carrutha88a22182011-07-14 08:20:46 +00002118 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002119 Record.push_back(ME->isBuiltinMacro());
2120 if (ME->isBuiltinMacro())
2121 AddIdentifierRef(ME->getName(), Record);
2122 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002123 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002124 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002125 continue;
2126 }
2127
2128 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2129 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002130 Record.push_back(ID->getFileName().size());
2131 Record.push_back(ID->wasInQuotes());
2132 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002133 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002134 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002135 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002136 // Check that the FileEntry is not null because it was not resolved and
2137 // we create a PCH even with compiler errors.
2138 if (ID->getFile())
2139 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002140 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2141 continue;
2142 }
2143
2144 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2145 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002146 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002147
Douglas Gregoraae92242010-03-19 21:51:54 +00002148 // Write the offsets table for the preprocessing record.
2149 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002150 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2151
Douglas Gregoraae92242010-03-19 21:51:54 +00002152 // Write the offsets table for identifier IDs.
2153 using namespace llvm;
2154 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002155 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002158 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002159
Douglas Gregoraae92242010-03-19 21:51:54 +00002160 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002161 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002162 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002163 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2164 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002165 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002166}
2167
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002168unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2169 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2170 if (Known != SubmoduleIDs.end())
2171 return Known->second;
2172
2173 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2174}
2175
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002176unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2177 if (!Mod)
2178 return 0;
2179
2180 llvm::DenseMap<Module *, unsigned>::const_iterator
2181 Known = SubmoduleIDs.find(Mod);
2182 if (Known != SubmoduleIDs.end())
2183 return Known->second;
2184
2185 return 0;
2186}
2187
Douglas Gregor253eefe2011-12-01 00:59:36 +00002188/// \brief Compute the number of modules within the given tree (including the
2189/// given module).
2190static unsigned getNumberOfModules(Module *Mod) {
2191 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002192 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2193 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002194 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002195 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002196
2197 return ChildModules + 1;
2198}
2199
Douglas Gregorde3ef502011-11-30 23:21:26 +00002200void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002201 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002202 // FIXME: This feels like it belongs somewhere else, but there are no
2203 // other consumers of this information.
2204 SourceManager &SrcMgr = PP->getSourceManager();
2205 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002206 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002207 if (Module *ImportedFrom
2208 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2209 SrcMgr))) {
2210 ImportedFrom->Imports.push_back(I->getImportedModule());
2211 }
2212 }
2213
Douglas Gregor69021972011-11-30 17:33:56 +00002214 // Enter the submodule description block.
2215 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2216
2217 // Write the abbreviations needed for the submodules block.
2218 using namespace llvm;
2219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2220 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2232 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2233
2234 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002235 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2237 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2238
2239 Abbrev = new BitCodeAbbrev();
2240 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2242 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002243
2244 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002245 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2247 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2248
2249 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002250 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2252 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2253
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002254 Abbrev = new BitCodeAbbrev();
2255 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002258 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2259
Douglas Gregor59527662012-10-15 06:28:11 +00002260 Abbrev = new BitCodeAbbrev();
2261 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2263 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2264
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002265 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002266 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2268 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2269
2270 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002271 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2274 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2275
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002276 Abbrev = new BitCodeAbbrev();
2277 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2279 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2280
Douglas Gregorfb912652013-03-20 21:10:35 +00002281 Abbrev = new BitCodeAbbrev();
2282 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2285 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2286
Douglas Gregor253eefe2011-12-01 00:59:36 +00002287 // Write the submodule metadata block.
2288 RecordData Record;
2289 Record.push_back(getNumberOfModules(WritingModule));
2290 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2291 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2292
Douglas Gregor69021972011-11-30 17:33:56 +00002293 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002294 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002295 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002296 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002297 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002298 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002299 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002300
2301 // Emit the definition of the block.
2302 Record.clear();
2303 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002304 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002305 if (Mod->Parent) {
2306 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2307 Record.push_back(SubmoduleIDs[Mod->Parent]);
2308 } else {
2309 Record.push_back(0);
2310 }
2311 Record.push_back(Mod->IsFramework);
2312 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002313 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002314 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002315 Record.push_back(Mod->InferSubmodules);
2316 Record.push_back(Mod->InferExplicitSubmodules);
2317 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002318 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002319 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2320
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002321 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002322 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002323 Record.clear();
2324 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002325 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002326 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002327 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002328 }
2329
Douglas Gregor69021972011-11-30 17:33:56 +00002330 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002331 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002332 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002333 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002334 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002335 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002336 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2337 Record.clear();
2338 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2339 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2340 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002341 }
2342
2343 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002344 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002345 Record.clear();
2346 Record.push_back(SUBMODULE_HEADER);
2347 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002348 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002349 }
Douglas Gregor59527662012-10-15 06:28:11 +00002350 // Emit the excluded headers.
2351 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2352 Record.clear();
2353 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2354 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2355 Mod->ExcludedHeaders[I]->getName());
2356 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002357 // Emit the private headers.
2358 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2359 Record.clear();
2360 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2361 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2362 Mod->PrivateHeaders[I]->getName());
2363 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002364 ArrayRef<const FileEntry *>
2365 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2366 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002367 Record.clear();
2368 Record.push_back(SUBMODULE_TOPHEADER);
2369 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002370 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002371 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002372
2373 // Emit the imports.
2374 if (!Mod->Imports.empty()) {
2375 Record.clear();
2376 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002377 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002378 assert(ImportedID && "Unknown submodule!");
2379 Record.push_back(ImportedID);
2380 }
2381 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2382 }
2383
Douglas Gregor24bb9232011-12-02 18:58:38 +00002384 // Emit the exports.
2385 if (!Mod->Exports.empty()) {
2386 Record.clear();
2387 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002388 if (Module *Exported = Mod->Exports[I].getPointer()) {
2389 unsigned ExportedID = SubmoduleIDs[Exported];
2390 assert(ExportedID > 0 && "Unknown submodule ID?");
2391 Record.push_back(ExportedID);
2392 } else {
2393 Record.push_back(0);
2394 }
2395
Douglas Gregor24bb9232011-12-02 18:58:38 +00002396 Record.push_back(Mod->Exports[I].getInt());
2397 }
2398 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2399 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002400
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002401 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2402 // Might be unnecessary as use declarations are only used to build the
2403 // module itself.
2404
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002405 // Emit the link libraries.
2406 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2407 Record.clear();
2408 Record.push_back(SUBMODULE_LINK_LIBRARY);
2409 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2410 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2411 Mod->LinkLibraries[I].Library);
2412 }
2413
Douglas Gregorfb912652013-03-20 21:10:35 +00002414 // Emit the conflicts.
2415 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2416 Record.clear();
2417 Record.push_back(SUBMODULE_CONFLICT);
2418 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2419 assert(OtherID && "Unknown submodule!");
2420 Record.push_back(OtherID);
2421 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2422 Mod->Conflicts[I].Message);
2423 }
2424
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002425 // Emit the configuration macros.
2426 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2427 Record.clear();
2428 Record.push_back(SUBMODULE_CONFIG_MACRO);
2429 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2430 Mod->ConfigMacros[I]);
2431 }
2432
Douglas Gregor69021972011-11-30 17:33:56 +00002433 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002434 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2435 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002436 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002437 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002438 }
2439
2440 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002441
2442 assert((NextSubmoduleID - FirstSubmoduleID
2443 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002444}
2445
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002446serialization::SubmoduleID
2447ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002448 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002449 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002450
2451 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002452 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002453 Module *OwningMod
2454 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002455 if (!OwningMod)
2456 return 0;
2457
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002458 // Check whether this submodule is part of our own module.
2459 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002460 return 0;
2461
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002462 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002463}
2464
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002465void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2466 bool isModule) {
2467 // Make sure set diagnostic pragmas don't affect the translation unit that
2468 // imports the module.
2469 // FIXME: Make diagnostic pragma sections work properly with modules.
2470 if (isModule)
2471 return;
2472
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002473 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2474 DiagStateIDMap;
2475 unsigned CurrID = 0;
2476 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002477 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002478 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002479 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2480 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002481 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002482 if (point.Loc.isInvalid())
2483 continue;
2484
2485 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002486 unsigned &DiagStateID = DiagStateIDMap[point.State];
2487 Record.push_back(DiagStateID);
2488
2489 if (DiagStateID == 0) {
2490 DiagStateID = ++CurrID;
2491 for (DiagnosticsEngine::DiagState::const_iterator
2492 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2493 if (I->second.isPragma()) {
2494 Record.push_back(I->first);
2495 Record.push_back(I->second.getMapping());
2496 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002497 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002498 Record.push_back(-1); // mark the end of the diag/map pairs for this
2499 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002500 }
2501 }
2502
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002503 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002504 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002505}
2506
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002507void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2508 if (CXXBaseSpecifiersOffsets.empty())
2509 return;
2510
2511 RecordData Record;
2512
2513 // Create a blob abbreviation for the C++ base specifiers offsets.
2514 using namespace llvm;
2515
2516 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2517 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2518 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2520 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2521
Douglas Gregorc27b2872011-08-04 00:01:48 +00002522 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002523 Record.clear();
2524 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2525 Record.push_back(CXXBaseSpecifiersOffsets.size());
2526 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002527 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002528}
2529
Douglas Gregorc5046832009-04-27 18:38:38 +00002530//===----------------------------------------------------------------------===//
2531// Type Serialization
2532//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002533
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002534/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002535void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002536 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002537 if (Idx.getIndex() == 0) // we haven't seen this type before.
2538 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002539
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002540 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002541
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002542 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002543 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002544 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002545 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002546 else if (TypeOffsets.size() < Index) {
2547 TypeOffsets.resize(Index + 1);
2548 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002549 }
2550
2551 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002552
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002553 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002554 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002555
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002556 if (T.hasLocalNonFastQualifiers()) {
2557 Qualifiers Qs = T.getLocalQualifiers();
2558 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002559 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002560 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002561 } else {
2562 switch (T->getTypeClass()) {
2563 // For all of the concrete, non-dependent types, call the
2564 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002565#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002566 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002567#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002568#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002569 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570 }
2571
2572 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002573 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002574
2575 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002576 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002577}
2578
Douglas Gregorc5046832009-04-27 18:38:38 +00002579//===----------------------------------------------------------------------===//
2580// Declaration Serialization
2581//===----------------------------------------------------------------------===//
2582
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002583/// \brief Write the block containing all of the declaration IDs
2584/// lexically declared within the given DeclContext.
2585///
2586/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2587/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002588uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002589 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002590 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002591 return 0;
2592
Douglas Gregor8f45df52009-04-16 22:23:12 +00002593 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002594 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002595 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002596 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002597 for (const auto *D : DC->decls())
2598 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002599
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002600 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002601 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002602 return Offset;
2603}
2604
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002605void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002606 using namespace llvm;
2607 RecordData Record;
2608
2609 // Write the type offsets array
2610 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002611 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2615 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2616 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002617 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002618 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002619 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002620 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002621
2622 // Write the declaration offsets array
2623 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002624 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2628 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2629 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002630 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002631 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002632 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002633 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002634}
2635
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002636void ASTWriter::WriteFileDeclIDsMap() {
2637 using namespace llvm;
2638 RecordData Record;
2639
2640 // Join the vectors of DeclIDs from all files.
2641 SmallVector<DeclID, 256> FileSortedIDs;
2642 for (FileDeclIDsTy::iterator
2643 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2644 DeclIDInFileInfo &Info = *FI->second;
2645 Info.FirstDeclIndex = FileSortedIDs.size();
2646 for (LocDeclIDsTy::iterator
2647 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2648 FileSortedIDs.push_back(DI->second);
2649 }
2650
2651 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2652 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002653 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2655 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2656 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002657 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002658 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2659}
2660
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002661void ASTWriter::WriteComments() {
2662 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002663 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002664 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002665 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2666 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002667 I != E; ++I) {
2668 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002669 AddSourceRange((*I)->getSourceRange(), Record);
2670 Record.push_back((*I)->getKind());
2671 Record.push_back((*I)->isTrailingComment());
2672 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002673 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2674 }
2675 Stream.ExitBlock();
2676}
2677
Douglas Gregorc5046832009-04-27 18:38:38 +00002678//===----------------------------------------------------------------------===//
2679// Global Method Pool and Selector Serialization
2680//===----------------------------------------------------------------------===//
2681
Douglas Gregore84a9da2009-04-20 20:36:09 +00002682namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002683// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002684class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002685 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002686
2687public:
2688 typedef Selector key_type;
2689 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002690
Sebastian Redl834bb972010-08-04 17:20:04 +00002691 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002692 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002693 ObjCMethodList Instance, Factory;
2694 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002695 typedef const data_type& data_type_ref;
2696
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002697 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002698
Douglas Gregorc78d3462009-04-24 21:10:55 +00002699 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002700 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002701 }
Mike Stump11289f42009-09-09 15:08:12 +00002702
2703 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002704 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002705 data_type_ref Methods) {
2706 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2707 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002708 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2709 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002710 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002711 if (Method->Method)
2712 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002713 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002714 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002715 if (Method->Method)
2716 DataLen += 4;
2717 clang::io::Emit16(Out, DataLen);
2718 return std::make_pair(KeyLen, DataLen);
2719 }
Mike Stump11289f42009-09-09 15:08:12 +00002720
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002721 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002722 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002723 assert((Start >> 32) == 0 && "Selector key offset too large");
2724 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002725 unsigned N = Sel.getNumArgs();
2726 clang::io::Emit16(Out, N);
2727 if (N == 0)
2728 N = 1;
2729 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002730 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002731 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2732 }
Mike Stump11289f42009-09-09 15:08:12 +00002733
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002734 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002735 data_type_ref Methods, unsigned DataLen) {
2736 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002737 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002738 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002739 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002740 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002741 if (Method->Method)
2742 ++NumInstanceMethods;
2743
2744 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002745 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002746 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002747 if (Method->Method)
2748 ++NumFactoryMethods;
2749
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002750 unsigned InstanceBits = Methods.Instance.getBits();
2751 assert(InstanceBits < 4);
2752 unsigned NumInstanceMethodsAndBits =
2753 (NumInstanceMethods << 2) | InstanceBits;
2754 unsigned FactoryBits = Methods.Factory.getBits();
2755 assert(FactoryBits < 4);
2756 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2757 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2758 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002759 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002760 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002761 if (Method->Method)
2762 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002763 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002764 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002765 if (Method->Method)
2766 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002767
2768 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002769 }
2770};
2771} // end anonymous namespace
2772
Sebastian Redla19a67f2010-08-03 21:58:15 +00002773/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002774///
2775/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002776/// in an on-disk hash table indexed by the selector. The hash table also
2777/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002778void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002779 using namespace llvm;
2780
Sebastian Redla19a67f2010-08-03 21:58:15 +00002781 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002782 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002783 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002784 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002785 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002786 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002787 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002788 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002789
Sebastian Redla19a67f2010-08-03 21:58:15 +00002790 // Create the on-disk hash table representation. We walk through every
2791 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002792 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002793 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002794 I = SelectorIDs.begin(), E = SelectorIDs.end();
2795 I != E; ++I) {
2796 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002797 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002798 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002799 I->second,
2800 ObjCMethodList(),
2801 ObjCMethodList()
2802 };
2803 if (F != SemaRef.MethodPool.end()) {
2804 Data.Instance = F->second.first;
2805 Data.Factory = F->second.second;
2806 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002807 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002808 // changed.
2809 if (Chain && I->second < FirstSelectorID) {
2810 // Selector already exists. Did it change?
2811 bool changed = false;
2812 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002813 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002814 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002815 changed = true;
2816 }
2817 for (ObjCMethodList *M = &Data.Factory; !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 if (!changed)
2823 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002824 } else if (Data.Instance.Method || Data.Factory.Method) {
2825 // A new method pool entry.
2826 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002827 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002828 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002829 }
2830
Douglas Gregorc78d3462009-04-24 21:10:55 +00002831 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002832 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002833 uint32_t BucketOffset;
2834 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002835 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002836 llvm::raw_svector_ostream Out(MethodPool);
2837 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002838 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002839 BucketOffset = Generator.Emit(Out, Trait);
2840 }
2841
2842 // Create a blob abbreviation
2843 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002844 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2848 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2849
Douglas Gregor95c13f52009-04-25 17:48:32 +00002850 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002851 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002852 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002853 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002854 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002855 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002856
2857 // Create a blob abbreviation for the selector table offsets.
2858 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002859 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002860 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2863 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2864
2865 // Write the selector offsets table.
2866 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002867 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002868 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002869 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002870 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002871 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002872 }
2873}
2874
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002875/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002876void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002877 using namespace llvm;
2878 if (SemaRef.ReferencedSelectors.empty())
2879 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002880
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002881 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002882
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002883 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002884 // very tricky to fix, and given that @selector shouldn't really appear in
2885 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002886 for (DenseMap<Selector, SourceLocation>::iterator S =
2887 SemaRef.ReferencedSelectors.begin(),
2888 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2889 Selector Sel = (*S).first;
2890 SourceLocation Loc = (*S).second;
2891 AddSelectorRef(Sel, Record);
2892 AddSourceLocation(Loc, Record);
2893 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002894 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002895}
2896
Douglas Gregorc5046832009-04-27 18:38:38 +00002897//===----------------------------------------------------------------------===//
2898// Identifier Table Serialization
2899//===----------------------------------------------------------------------===//
2900
Douglas Gregorc78d3462009-04-24 21:10:55 +00002901namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002902class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002903 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002904 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002905 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002906 bool IsModule;
2907
Douglas Gregor1d583f22009-04-28 21:18:29 +00002908 /// \brief Determines whether this is an "interesting" identifier
2909 /// that needs a full IdentifierInfo structure written into the hash
2910 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002911 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002912 if (II->isPoisoned() ||
2913 II->isExtensionToken() ||
2914 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002915 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002916 II->getFETokenInfo<void>())
2917 return true;
2918
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002919 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002920 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002921
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002922 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002923 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002924 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002925
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002926 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2927 if (!IsModule)
2928 return !shouldIgnoreMacro(Macro, IsModule, PP);
2929 SubmoduleID ModID;
2930 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2931 return true;
2932 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002933
2934 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002935 }
2936
Richard Smith49f906a2014-03-01 00:08:04 +00002937 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2938
2939 MacroDirective *
2940 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002941 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002942 llvm::SmallVector<SubmoduleID, 1> Overridden;
2943 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2944 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2945 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002946 return 0;
2947 }
2948
Richard Smith49f906a2014-03-01 00:08:04 +00002949 MacroDirective *
2950 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2951 OverriddenList &Overridden) {
2952 if (MacroDirective *NextMD =
2953 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2954 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2955 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002956 return 0;
2957 }
2958
2959 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00002960 /// public macro definition or undefinition that is not in ModID.
2961 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002962 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00002963 /// ModID is updated to the module containing the returned directive.
2964 ///
2965 /// FIXME: This process breaks down if a module defines a macro, imports
2966 /// another submodule that changes the macro, then changes the
2967 /// macro again itself.
2968 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2969 SubmoduleID &ModID,
2970 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002971 if (!MD)
2972 return 0;
2973
Richard Smith49f906a2014-03-01 00:08:04 +00002974 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002975 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002976 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002977 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002978 SubmoduleID ThisModID = getSubmoduleID(MD);
2979 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00002980 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002981 continue;
2982 }
Richard Smith49f906a2014-03-01 00:08:04 +00002983 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002984 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002985 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002986 }
Richard Smith49f906a2014-03-01 00:08:04 +00002987
2988 // If this is a definition from a submodule import, that submodule's
2989 // definition is overridden by the definition or undefinition that we
2990 // started with.
2991 // FIXME: This should only apply to macros defined in OrigModID.
2992 // We can't do that currently, because a #include of a different submodule
2993 // of the same module just leaks through macros instead of providing new
2994 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00002995 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2996 // Figure out which submodule the macro was originally defined within.
2997 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
2998 if (!SourceID) {
2999 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3000 if (DefLoc == MD->getLocation())
3001 SourceID = ThisModID;
3002 else
3003 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3004 }
3005 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003006 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003007 }
Richard Smith49f906a2014-03-01 00:08:04 +00003008
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003009 // We are looking for a definition in a different submodule than the one
3010 // that we started with. If a submodule has re-definitions of the same
3011 // macro, only the last definition will be used as the "exported" one.
3012 if (ModID == OrigModID)
3013 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003014
Richard Smith49f906a2014-03-01 00:08:04 +00003015 // The latest visibility directive for a name in a submodule affects all
3016 // the directives that come before it.
3017 if (VisibilityMacroDirective *VisMD =
3018 dyn_cast<VisibilityMacroDirective>(MD)) {
3019 if (!IsPublic.hasValue())
3020 IsPublic = VisMD->isPublic();
3021 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3022 // FIXME: If we find an imported macro, we should include its list of
3023 // overrides in our export.
3024 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003025 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003026 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003027
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003028 return 0;
3029 }
3030
3031 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003032 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003033 }
3034
Douglas Gregore84a9da2009-04-20 20:36:09 +00003035public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003036 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003037 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003038
Sebastian Redl539c5062010-08-18 23:57:32 +00003039 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003040 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003041
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003042 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3043 IdentifierResolver &IdResolver, bool IsModule)
3044 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003045
3046 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003047 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003048 }
Mike Stump11289f42009-09-09 15:08:12 +00003049
3050 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003051 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003052 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003053 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003054 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003055 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003056 DataLen += 2; // 2 bytes for builtin ID
3057 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003058 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003059 DataLen += 4; // MacroDirectives offset.
3060 if (IsModule) {
3061 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003062 llvm::SmallVector<SubmoduleID, 4> Overridden;
3063 for (MacroDirective *
3064 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3065 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3066 // Previous macro's overrides.
3067 if (!Overridden.empty())
3068 DataLen += 4 * (1 + Overridden.size());
3069 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003070 }
Richard Smith49f906a2014-03-01 00:08:04 +00003071 // Previous macro's overrides.
3072 if (!Overridden.empty())
3073 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003074 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003075 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003076 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003077
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003078 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3079 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003080 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003081 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003082 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003083 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003084 // We emit the key length after the data length so that every
3085 // string is preceded by a 16-bit length. This matches the PTH
3086 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003087 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003088 return std::make_pair(KeyLen, DataLen);
3089 }
Mike Stump11289f42009-09-09 15:08:12 +00003090
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003091 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003092 unsigned KeyLen) {
3093 // Record the location of the key data. This is used when generating
3094 // the mapping from persistent IDs to strings.
3095 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003096 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003097 }
Mike Stump11289f42009-09-09 15:08:12 +00003098
Richard Smith49f906a2014-03-01 00:08:04 +00003099 static void emitMacroOverrides(raw_ostream &Out,
3100 llvm::ArrayRef<SubmoduleID> Overridden) {
3101 if (!Overridden.empty()) {
3102 clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
3103 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3104 clang::io::Emit32(Out, Overridden[I]);
3105 }
3106 }
3107
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003108 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003109 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003110 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003111 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003112 clang::io::Emit32(Out, ID << 1);
3113 return;
3114 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003115
Douglas Gregor1d583f22009-04-28 21:18:29 +00003116 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003117 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3118 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3119 clang::io::Emit16(Out, Bits);
3120 Bits = 0;
3121 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003122 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003123 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003124 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3125 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003126 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003127 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003128 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003129
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003130 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003131 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3132 if (IsModule) {
3133 // Write the IDs of macros coming from different submodules.
3134 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003135 llvm::SmallVector<SubmoduleID, 4> Overridden;
3136 for (MacroDirective *
3137 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3138 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3139 MacroID InfoID = 0;
3140 emitMacroOverrides(Out, Overridden);
3141 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3142 InfoID = Writer.getMacroID(DefMD->getInfo());
3143 assert(InfoID);
3144 clang::io::Emit32(Out, InfoID << 1);
3145 } else {
3146 assert(isa<UndefMacroDirective>(MD));
3147 clang::io::Emit32(Out, (ModID << 1) | 1);
3148 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003149 }
Richard Smith49f906a2014-03-01 00:08:04 +00003150 emitMacroOverrides(Out, Overridden);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003151 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003152 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003153 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003154
Douglas Gregora868bbd2009-04-21 22:25:48 +00003155 // Emit the declaration IDs in reverse order, because the
3156 // IdentifierResolver provides the declarations as they would be
3157 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003158 // "stat"), but the ASTReader adds declarations to the end of the list
3159 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003160 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003161 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3162 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003163 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003164 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003165 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003166 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3167 }
3168
3169 /// \brief Returns the most recent local decl or the given decl if there are
3170 /// no local ones. The given decl is assumed to be the most recent one.
3171 Decl *getMostRecentLocalDecl(Decl *Orig) {
3172 // The only way a "from AST file" decl would be more recent from a local one
3173 // is if it came from a module.
3174 if (!PP.getLangOpts().Modules)
3175 return Orig;
3176
3177 // Look for a local in the decl chain.
3178 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3179 if (!D->isFromASTFile())
3180 return D;
3181 // If we come up a decl from a (chained-)PCH stop since we won't find a
3182 // local one.
3183 if (D->getOwningModuleID() == 0)
3184 break;
3185 }
3186
3187 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003188 }
3189};
3190} // end anonymous namespace
3191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003192/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003193///
3194/// The identifier table consists of a blob containing string data
3195/// (the actual identifiers themselves) and a separate "offsets" index
3196/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003197void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3198 IdentifierResolver &IdResolver,
3199 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003200 using namespace llvm;
3201
3202 // Create and write out the blob that contains the identifier
3203 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003204 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003205 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003206 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003207
Douglas Gregore6648fb2009-04-28 20:33:11 +00003208 // Look for any identifiers that were named while processing the
3209 // headers, but are otherwise not needed. We add these to the hash
3210 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003211 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003212 // file.
3213 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3214 IDEnd = PP.getIdentifierTable().end();
3215 ID != IDEnd; ++ID)
3216 getIdentifierRef(ID->second);
3217
Sebastian Redlff4a2952010-07-23 23:49:55 +00003218 // Create the on-disk hash table representation. We only store offsets
3219 // for identifiers that appear here for the first time.
3220 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003221 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003222 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3223 ID != IDEnd; ++ID) {
3224 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003225 if (!Chain || !ID->first->isFromAST() ||
3226 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003227 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003228 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003229 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003230
Douglas Gregore84a9da2009-04-20 20:36:09 +00003231 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003232 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003233 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003234 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003235 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003236 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003237 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003238 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003239 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003240 }
3241
3242 // Create a blob abbreviation
3243 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003244 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003247 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003248
3249 // Write the identifier table
3250 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003251 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003252 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003253 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003254 }
3255
3256 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003257 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003258 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3262 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3263
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003264#ifndef NDEBUG
3265 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3266 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3267#endif
3268
Douglas Gregor0e149972009-04-25 19:10:14 +00003269 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003270 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003271 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003272 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003273 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003274 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003275}
3276
Douglas Gregorc5046832009-04-27 18:38:38 +00003277//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003278// DeclContext's Name Lookup Table Serialization
3279//===----------------------------------------------------------------------===//
3280
3281namespace {
3282// Trait used for the on-disk hash table used in the method pool.
3283class ASTDeclContextNameLookupTrait {
3284 ASTWriter &Writer;
3285
3286public:
3287 typedef DeclarationName key_type;
3288 typedef key_type key_type_ref;
3289
3290 typedef DeclContext::lookup_result data_type;
3291 typedef const data_type& data_type_ref;
3292
3293 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3294
3295 unsigned ComputeHash(DeclarationName Name) {
3296 llvm::FoldingSetNodeID ID;
3297 ID.AddInteger(Name.getNameKind());
3298
3299 switch (Name.getNameKind()) {
3300 case DeclarationName::Identifier:
3301 ID.AddString(Name.getAsIdentifierInfo()->getName());
3302 break;
3303 case DeclarationName::ObjCZeroArgSelector:
3304 case DeclarationName::ObjCOneArgSelector:
3305 case DeclarationName::ObjCMultiArgSelector:
3306 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3307 break;
3308 case DeclarationName::CXXConstructorName:
3309 case DeclarationName::CXXDestructorName:
3310 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003311 break;
3312 case DeclarationName::CXXOperatorName:
3313 ID.AddInteger(Name.getCXXOverloadedOperator());
3314 break;
3315 case DeclarationName::CXXLiteralOperatorName:
3316 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3317 case DeclarationName::CXXUsingDirective:
3318 break;
3319 }
3320
3321 return ID.ComputeHash();
3322 }
3323
3324 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003325 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003326 data_type_ref Lookup) {
3327 unsigned KeyLen = 1;
3328 switch (Name.getNameKind()) {
3329 case DeclarationName::Identifier:
3330 case DeclarationName::ObjCZeroArgSelector:
3331 case DeclarationName::ObjCOneArgSelector:
3332 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003333 case DeclarationName::CXXLiteralOperatorName:
3334 KeyLen += 4;
3335 break;
3336 case DeclarationName::CXXOperatorName:
3337 KeyLen += 1;
3338 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003339 case DeclarationName::CXXConstructorName:
3340 case DeclarationName::CXXDestructorName:
3341 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003342 case DeclarationName::CXXUsingDirective:
3343 break;
3344 }
3345 clang::io::Emit16(Out, KeyLen);
3346
3347 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003348 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003349 clang::io::Emit16(Out, DataLen);
3350
3351 return std::make_pair(KeyLen, DataLen);
3352 }
3353
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003354 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003355 using namespace clang::io;
3356
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003357 Emit8(Out, Name.getNameKind());
3358 switch (Name.getNameKind()) {
3359 case DeclarationName::Identifier:
3360 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003361 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362 case DeclarationName::ObjCZeroArgSelector:
3363 case DeclarationName::ObjCOneArgSelector:
3364 case DeclarationName::ObjCMultiArgSelector:
3365 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003366 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003367 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003368 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3369 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003370 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003371 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003372 case DeclarationName::CXXLiteralOperatorName:
3373 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003374 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003375 case DeclarationName::CXXConstructorName:
3376 case DeclarationName::CXXDestructorName:
3377 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003378 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003379 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003380 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003381
3382 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003383 }
3384
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003385 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003386 data_type Lookup, unsigned DataLen) {
3387 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003388 clang::io::Emit16(Out, Lookup.size());
3389 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3390 I != E; ++I)
3391 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003392
3393 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3394 }
3395};
3396} // end anonymous namespace
3397
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003398/// \brief Write the block containing all of the declaration IDs
3399/// visible from the given DeclContext.
3400///
3401/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003402/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003403uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3404 DeclContext *DC) {
3405 if (DC->getPrimaryContext() != DC)
3406 return 0;
3407
3408 // Since there is no name lookup into functions or methods, don't bother to
3409 // build a visible-declarations table for these entities.
3410 if (DC->isFunctionOrMethod())
3411 return 0;
3412
3413 // If not in C++, we perform name lookup for the translation unit via the
3414 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003415 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003416 return 0;
3417
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003418 // Serialize the contents of the mapping used for lookup. Note that,
3419 // although we have two very different code paths, the serialized
3420 // representation is the same for both cases: a declaration name,
3421 // followed by a size, followed by references to the visible
3422 // declarations that have that name.
3423 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003424 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003425 if (!Map || Map->empty())
3426 return 0;
3427
3428 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3429 ASTDeclContextNameLookupTrait Trait(*this);
3430
3431 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003432 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003433 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003434 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3435 D != DEnd; ++D) {
3436 DeclarationName Name = D->first;
3437 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003438 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003439 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3440 // Hash all conversion function names to the same name. The actual
3441 // type information in conversion function name is not used in the
3442 // key (since such type information is not stable across different
3443 // modules), so the intended effect is to coalesce all of the conversion
3444 // functions under a single key.
3445 if (!ConversionName)
3446 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003447 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003448 continue;
3449 }
3450
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003451 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003452 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003453 }
3454
Douglas Gregor05ef9312011-08-30 20:49:19 +00003455 // Add the conversion functions
3456 if (!ConversionDecls.empty()) {
3457 Generator.insert(ConversionName,
3458 DeclContext::lookup_result(ConversionDecls.begin(),
3459 ConversionDecls.end()),
3460 Trait);
3461 }
3462
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003463 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003464 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003465 uint32_t BucketOffset;
3466 {
3467 llvm::raw_svector_ostream Out(LookupTable);
3468 // Make sure that no bucket is at offset 0
3469 clang::io::Emit32(Out, 0);
3470 BucketOffset = Generator.Emit(Out, Trait);
3471 }
3472
3473 // Write the lookup table
3474 RecordData Record;
3475 Record.push_back(DECL_CONTEXT_VISIBLE);
3476 Record.push_back(BucketOffset);
3477 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3478 LookupTable.str());
3479
3480 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3481 ++NumVisibleDeclContexts;
3482 return Offset;
3483}
3484
Sebastian Redla4071b42010-08-24 00:50:09 +00003485/// \brief Write an UPDATE_VISIBLE block for the given context.
3486///
3487/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3488/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003489/// (in C++), for namespaces, and for classes with forward-declared unscoped
3490/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003491void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003492 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3493 if (!Map || Map->empty())
3494 return;
3495
3496 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3497 ASTDeclContextNameLookupTrait Trait(*this);
3498
3499 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003500 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3501 D != DEnd; ++D) {
3502 DeclarationName Name = D->first;
3503 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003504 // For any name that appears in this table, the results are complete, i.e.
3505 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003506 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003507 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003508 }
3509
3510 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003511 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003512 uint32_t BucketOffset;
3513 {
3514 llvm::raw_svector_ostream Out(LookupTable);
3515 // Make sure that no bucket is at offset 0
3516 clang::io::Emit32(Out, 0);
3517 BucketOffset = Generator.Emit(Out, Trait);
3518 }
3519
3520 // Write the lookup table
3521 RecordData Record;
3522 Record.push_back(UPDATE_VISIBLE);
3523 Record.push_back(getDeclID(cast<Decl>(DC)));
3524 Record.push_back(BucketOffset);
3525 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3526}
3527
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003528/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3529void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3530 RecordData Record;
3531 Record.push_back(Opts.fp_contract);
3532 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3533}
3534
3535/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3536void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003537 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003538 return;
3539
3540 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3541 RecordData Record;
3542#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3543#include "clang/Basic/OpenCLExtensions.def"
3544 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3545}
3546
Douglas Gregor358cd442012-01-15 16:58:34 +00003547void ASTWriter::WriteRedeclarations() {
3548 RecordData LocalRedeclChains;
3549 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3550
3551 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3552 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003553 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003554
3555 Decl *MostRecent = First->getMostRecentDecl();
3556
3557 // If we only have a single declaration, there is no point in storing
3558 // a redeclaration chain.
3559 if (First == MostRecent)
3560 continue;
3561
3562 unsigned Offset = LocalRedeclChains.size();
3563 unsigned Size = 0;
3564 LocalRedeclChains.push_back(0); // Placeholder for the size.
3565
3566 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003567 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003568 Prev = Prev->getPreviousDecl()) {
3569 if (!Prev->isFromASTFile()) {
3570 AddDeclRef(Prev, LocalRedeclChains);
3571 ++Size;
3572 }
3573 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003574
3575 if (!First->isFromASTFile() && Chain) {
3576 Decl *FirstFromAST = MostRecent;
3577 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3578 if (Prev->isFromASTFile())
3579 FirstFromAST = Prev;
3580 }
3581
3582 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3583 }
3584
Douglas Gregor358cd442012-01-15 16:58:34 +00003585 LocalRedeclChains[Offset] = Size;
3586
3587 // Reverse the set of local redeclarations, so that we store them in
3588 // order (since we found them in reverse order).
3589 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3590
Douglas Gregor6168bd22013-02-18 15:53:43 +00003591 // Add the mapping from the first ID from the AST to the set of local
3592 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003593 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3594 LocalRedeclsMap.push_back(Info);
3595
3596 assert(N == Redeclarations.size() &&
3597 "Deserialized a declaration we shouldn't have");
3598 }
3599
3600 if (LocalRedeclChains.empty())
3601 return;
3602
3603 // Sort the local redeclarations map by the first declaration ID,
3604 // since the reader will be performing binary searches on this information.
3605 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3606
3607 // Emit the local redeclarations map.
3608 using namespace llvm;
3609 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3610 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3611 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3613 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3614
3615 RecordData Record;
3616 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3617 Record.push_back(LocalRedeclsMap.size());
3618 Stream.EmitRecordWithBlob(AbbrevID, Record,
3619 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3620 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3621
3622 // Emit the redeclaration chains.
3623 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3624}
3625
Douglas Gregor404cdde2012-01-27 01:47:08 +00003626void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003627 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003628 RecordData Categories;
3629
3630 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3631 unsigned Size = 0;
3632 unsigned StartIndex = Categories.size();
3633
3634 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3635
3636 // Allocate space for the size.
3637 Categories.push_back(0);
3638
3639 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003640 for (ObjCInterfaceDecl::known_categories_iterator
3641 Cat = Class->known_categories_begin(),
3642 CatEnd = Class->known_categories_end();
3643 Cat != CatEnd; ++Cat, ++Size) {
3644 assert(getDeclID(*Cat) != 0 && "Bogus category");
3645 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003646 }
3647
3648 // Update the size.
3649 Categories[StartIndex] = Size;
3650
3651 // Record this interface -> category map.
3652 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3653 CategoriesMap.push_back(CatInfo);
3654 }
3655
3656 // Sort the categories map by the definition ID, since the reader will be
3657 // performing binary searches on this information.
3658 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3659
3660 // Emit the categories map.
3661 using namespace llvm;
3662 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3663 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3666 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3667
3668 RecordData Record;
3669 Record.push_back(OBJC_CATEGORIES_MAP);
3670 Record.push_back(CategoriesMap.size());
3671 Stream.EmitRecordWithBlob(AbbrevID, Record,
3672 reinterpret_cast<char*>(CategoriesMap.data()),
3673 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3674
3675 // Emit the category lists.
3676 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3677}
3678
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003679void ASTWriter::WriteMergedDecls() {
3680 if (!Chain || Chain->MergedDecls.empty())
3681 return;
3682
3683 RecordData Record;
3684 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3685 IEnd = Chain->MergedDecls.end();
3686 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003687 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003688 : getDeclID(I->first);
3689 assert(CanonID && "Merged declaration not known?");
3690
3691 Record.push_back(CanonID);
3692 Record.push_back(I->second.size());
3693 Record.append(I->second.begin(), I->second.end());
3694 }
3695 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3696}
3697
Richard Smithe40f2ba2013-08-07 21:41:30 +00003698void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3699 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3700
3701 if (LPTMap.empty())
3702 return;
3703
3704 RecordData Record;
3705 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3706 ItEnd = LPTMap.end();
3707 It != ItEnd; ++It) {
3708 LateParsedTemplate *LPT = It->second;
3709 AddDeclRef(It->first, Record);
3710 AddDeclRef(LPT->D, Record);
3711 Record.push_back(LPT->Toks.size());
3712
3713 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3714 TokEnd = LPT->Toks.end();
3715 TokIt != TokEnd; ++TokIt) {
3716 AddToken(*TokIt, Record);
3717 }
3718 }
3719 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3720}
3721
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003722//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003723// General Serialization Routines
3724//===----------------------------------------------------------------------===//
3725
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003726/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003727void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3728 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003729 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003730 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3731 e = Attrs.end(); i != e; ++i){
3732 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003733 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003734 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003735
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003736#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003737
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003738 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003739}
3740
John McCallf413f5e2013-05-03 00:10:13 +00003741void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3742 AddSourceLocation(Tok.getLocation(), Record);
3743 Record.push_back(Tok.getLength());
3744
3745 // FIXME: When reading literal tokens, reconstruct the literal pointer
3746 // if it is needed.
3747 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3748 // FIXME: Should translate token kind to a stable encoding.
3749 Record.push_back(Tok.getKind());
3750 // FIXME: Should translate token flags to a stable encoding.
3751 Record.push_back(Tok.getFlags());
3752}
3753
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003754void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003755 Record.push_back(Str.size());
3756 Record.insert(Record.end(), Str.begin(), Str.end());
3757}
3758
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003759void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3760 RecordDataImpl &Record) {
3761 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003762 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003763 Record.push_back(*Minor + 1);
3764 else
3765 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003766 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003767 Record.push_back(*Subminor + 1);
3768 else
3769 Record.push_back(0);
3770}
3771
Douglas Gregore84a9da2009-04-20 20:36:09 +00003772/// \brief Note that the identifier II occurs at the given offset
3773/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003774void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003775 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003776 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003777 // up earlier in the chain and thus don't need an offset.
3778 if (ID >= FirstIdentID)
3779 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003780}
3781
Douglas Gregor95c13f52009-04-25 17:48:32 +00003782/// \brief Note that the selector Sel occurs at the given offset
3783/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003784void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003785 unsigned ID = SelectorIDs[Sel];
3786 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003787 // Don't record offsets for selectors that are also available in a different
3788 // file.
3789 if (ID < FirstSelectorID)
3790 return;
3791 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003792}
3793
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003794ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003795 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003796 WritingAST(false), DoneWritingDeclsAndTypes(false),
3797 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003798 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003799 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003800 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3801 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003802 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3803 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003804 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003805 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003806 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003807 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003808 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003809 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003810 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3811 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3812 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003813 DeclTypedefAbbrev(0),
3814 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3815 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003816{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003817}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003818
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003819ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003820 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003821}
3822
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003823void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003824 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003825 Module *WritingModule, StringRef isysroot,
3826 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003827 WritingAST = true;
3828
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003829 ASTHasCompilerErrors = hasErrors;
3830
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003831 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003832 Stream.Emit((unsigned)'C', 8);
3833 Stream.Emit((unsigned)'P', 8);
3834 Stream.Emit((unsigned)'C', 8);
3835 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003836
Chris Lattner28fa4e62009-04-26 22:26:21 +00003837 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003838
Douglas Gregoreda8e122011-08-09 15:13:55 +00003839 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003840 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003841 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003842 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003843 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003844 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003845 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003846
3847 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003848}
3849
Douglas Gregora94a1542011-07-27 21:45:57 +00003850template<typename Vector>
3851static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3852 ASTWriter::RecordData &Record) {
3853 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3854 I != E; ++I) {
3855 Writer.AddDeclRef(*I, Record);
3856 }
3857}
3858
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003859void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003860 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003861 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003862 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003863 using namespace llvm;
3864
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003865 bool isModule = WritingModule != 0;
3866
Douglas Gregorcf68c582011-12-01 22:20:10 +00003867 // Make sure that the AST reader knows to finalize itself.
3868 if (Chain)
3869 Chain->finalizeForWriting();
3870
Sebastian Redl143413f2010-07-12 22:02:52 +00003871 ASTContext &Context = SemaRef.Context;
3872 Preprocessor &PP = SemaRef.PP;
3873
Douglas Gregordab42432011-08-12 00:15:20 +00003874 // Set up predefined declaration IDs.
3875 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003876 if (Context.ObjCIdDecl)
3877 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003878 if (Context.ObjCSelDecl)
3879 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003880 if (Context.ObjCClassDecl)
3881 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003882 if (Context.ObjCProtocolClassDecl)
3883 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003884 if (Context.Int128Decl)
3885 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3886 if (Context.UInt128Decl)
3887 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003888 if (Context.ObjCInstanceTypeDecl)
3889 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003890 if (Context.BuiltinVaListDecl)
3891 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3892
Douglas Gregor851443c2011-08-12 01:39:19 +00003893 if (!Chain) {
3894 // Make sure that we emit IdentifierInfos (and any attached
3895 // declarations) for builtins. We don't need to do this when we're
3896 // emitting chained PCH files, because all of the builtins will be
3897 // in the original PCH file.
3898 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003899 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003900 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003901 if (!Context.getLangOpts().NoBuiltin) {
3902 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3903 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003904 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3905 getIdentifierRef(&Table.get(BuiltinNames[I]));
3906 }
3907
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003908 // If there are any out-of-date identifiers, bring them up to date.
3909 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003910 // Find out-of-date identifiers.
3911 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003912 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3913 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003914 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003915 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003916 OutOfDate.push_back(ID->second);
3917 }
3918
3919 // Update the out-of-date identifiers.
3920 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3921 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3922 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003923 }
3924
Chris Lattner0c797362009-09-08 18:19:27 +00003925 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003926 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003927 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003928 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003929 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003930
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003931 // Build a record containing all of the file scoped decls in this file.
3932 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003933 if (!isModule)
3934 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3935 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003936
Douglas Gregor851443c2011-08-12 01:39:19 +00003937 // Build a record containing all of the delegating constructors we still need
3938 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003939 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003940 if (!isModule)
3941 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003942
Douglas Gregor851443c2011-08-12 01:39:19 +00003943 // Write the set of weak, undeclared identifiers. We always write the
3944 // entire table, since later PCH files in a PCH chain are only interested in
3945 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003946 RecordData WeakUndeclaredIdentifiers;
3947 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003948 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003949 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3950 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3951 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3952 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3953 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3954 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3955 }
3956 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003957
Richard Smith78165b52013-01-10 23:43:47 +00003958 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003959 // declarations in this header file. Generally, this record will be
3960 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003961 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003962 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003963 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003964 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003965 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3966 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003967 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003968 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003969 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003970 }
3971
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003972 // Build a record containing all of the ext_vector declarations.
3973 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003974 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003975
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003976 // Build a record containing all of the VTable uses information.
3977 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003978 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003979 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3980 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3981 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3982 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3983 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003984 }
3985
3986 // Build a record containing all of dynamic classes declarations.
3987 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003988 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003989
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003990 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003991 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003992 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003993 I = SemaRef.PendingInstantiations.begin(),
3994 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3995 AddDeclRef(I->first, PendingInstantiations);
3996 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003997 }
3998 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3999 "There are local ones at end of translation unit!");
4000
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004001 // Build a record containing some declaration references.
4002 RecordData SemaDeclRefs;
4003 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4004 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4005 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4006 }
4007
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004008 RecordData CUDASpecialDeclRefs;
4009 if (Context.getcudaConfigureCallDecl()) {
4010 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4011 }
4012
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004013 // Build a record containing all of the known namespaces.
4014 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004015 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004016 I = SemaRef.KnownNamespaces.begin(),
4017 IEnd = SemaRef.KnownNamespaces.end();
4018 I != IEnd; ++I) {
4019 if (!I->second)
4020 AddDeclRef(I->first, KnownNamespaces);
4021 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004022
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004023 // Build a record of all used, undefined objects that require definitions.
4024 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004025
4026 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004027 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004028 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4029 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004030 AddDeclRef(I->first, UndefinedButUsed);
4031 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004032 }
4033
Douglas Gregor112b9072012-10-18 05:31:06 +00004034 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004035 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004036
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004037 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004038 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004039 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004040
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004041 // This is so that older clang versions, before the introduction
4042 // of the control block, can read and reject the newer PCH format.
4043 Record.clear();
4044 Record.push_back(VERSION_MAJOR);
4045 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4046
Douglas Gregor851443c2011-08-12 01:39:19 +00004047 // Create a lexical update block containing all of the declarations in the
4048 // translation unit that do not come from other AST files.
4049 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4050 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004051 for (const auto *I : TU->noload_decls()) {
4052 if (!I->isFromASTFile())
4053 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004054 }
4055
4056 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4057 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4058 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4059 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4060 Record.clear();
4061 Record.push_back(TU_UPDATE_LEXICAL);
4062 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4063 data(NewGlobalDecls));
4064
4065 // And a visible updates block for the translation unit.
4066 Abv = new llvm::BitCodeAbbrev();
4067 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4068 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4069 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4070 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4071 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4072 WriteDeclContextVisibleUpdate(TU);
4073
4074 // If the translation unit has an anonymous namespace, and we don't already
4075 // have an update block for it, write it as an update block.
4076 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4077 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4078 if (Record.empty()) {
4079 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004080 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004081 }
4082 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004083
4084 // Make sure visible decls, added to DeclContexts previously loaded from
4085 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004086 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004087 I = UpdatingVisibleDecls.begin(),
4088 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4089 GetDeclRef(*I);
4090 }
4091
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004092 // Make sure all decls associated with an identifier are registered for
4093 // serialization.
4094 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4095 IDEnd = PP.getIdentifierTable().end();
4096 ID != IDEnd; ++ID) {
4097 const IdentifierInfo *II = ID->second;
4098 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4099 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4100 DEnd = SemaRef.IdResolver.end();
4101 D != DEnd; ++D) {
4102 GetDeclRef(*D);
4103 }
4104 }
4105 }
4106
Douglas Gregor5204bde2011-08-02 16:26:37 +00004107 // Form the record of special types.
4108 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004109 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004110 AddTypeRef(Context.getFILEType(), SpecialTypes);
4111 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4112 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4113 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4114 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004115 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004116 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004117
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004118 if (Chain) {
4119 // Write the mapping information describing our module dependencies and how
4120 // each of those modules were mapped into our own offset/ID space, so that
4121 // the reader can build the appropriate mapping to its own offset/ID space.
4122 // The map consists solely of a blob with the following format:
4123 // *(module-name-len:i16 module-name:len*i8
4124 // source-location-offset:i32
4125 // identifier-id:i32
4126 // preprocessed-entity-id:i32
4127 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004128 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004129 // selector-id:i32
4130 // declaration-id:i32
4131 // c++-base-specifiers-id:i32
4132 // type-id:i32)
4133 //
4134 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4135 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4137 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004138 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004139 {
4140 llvm::raw_svector_ostream Out(Buffer);
4141 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004142 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004143 M != MEnd; ++M) {
4144 StringRef FileName = (*M)->FileName;
4145 io::Emit16(Out, FileName.size());
4146 Out.write(FileName.data(), FileName.size());
4147 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4148 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004149 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004150 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004151 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004152 io::Emit32(Out, (*M)->BaseSelectorID);
4153 io::Emit32(Out, (*M)->BaseDeclID);
4154 io::Emit32(Out, (*M)->BaseTypeIndex);
4155 }
4156 }
4157 Record.clear();
4158 Record.push_back(MODULE_OFFSET_MAP);
4159 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4160 Buffer.data(), Buffer.size());
4161 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004162
4163 // Resolve any declaration pointers within the declaration updates block.
4164 // FIXME: Fold this into WriteDeclUpdatesBlocks.
4165 ResolveDeclUpdatesBlocks();
4166
4167 RecordData DeclUpdatesOffsetsRecord;
4168
4169 // Keep writing types and declarations until all types and
4170 // declarations have been written.
4171 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4172 WriteDeclsBlockAbbrevs();
4173 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4174 E = DeclsToRewrite.end();
4175 I != E; ++I)
4176 DeclTypesToEmit.push(const_cast<Decl*>(*I));
4177 while (!DeclTypesToEmit.empty()) {
4178 DeclOrType DOT = DeclTypesToEmit.front();
4179 DeclTypesToEmit.pop();
4180 if (DOT.isType())
4181 WriteType(DOT.getType());
4182 else
4183 WriteDecl(Context, DOT.getDecl());
4184 }
4185 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4186 Stream.ExitBlock();
4187
4188 if (!DeclUpdatesOffsetsRecord.empty())
4189 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4190
4191 DoneWritingDeclsAndTypes = true;
4192
4193 // These things can only be done once we've written out decls and types.
4194 WriteTypeDeclOffsets();
4195 WriteCXXBaseSpecifiersOffsets();
4196 WriteFileDeclIDsMap();
4197 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4198
4199 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004200 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004201 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004202 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004203 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004204 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004205 WriteFPPragmaOptions(SemaRef.getFPOptions());
4206 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004207 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004208
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004209 // If we're emitting a module, write out the submodule information.
4210 if (WritingModule)
4211 WriteSubmodules(WritingModule);
4212
Douglas Gregor5204bde2011-08-02 16:26:37 +00004213 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4214
Douglas Gregord4df8652009-04-22 22:02:47 +00004215 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004216 if (!EagerlyDeserializedDecls.empty())
4217 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004218
4219 // Write the record containing tentative definitions.
4220 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004221 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004222
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004223 // Write the record containing unused file scoped decls.
4224 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004225 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004226
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004227 // Write the record containing weak undeclared identifiers.
4228 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004229 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004230 WeakUndeclaredIdentifiers);
4231
Richard Smith78165b52013-01-10 23:43:47 +00004232 // Write the record containing locally-scoped extern "C" definitions.
4233 if (!LocallyScopedExternCDecls.empty())
4234 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4235 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004236
4237 // Write the record containing ext_vector type names.
4238 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004239 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004240
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004241 // Write the record containing VTable uses information.
4242 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004243 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004244
4245 // Write the record containing dynamic classes declarations.
4246 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004247 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004248
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004249 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004250 if (!PendingInstantiations.empty())
4251 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004252
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004253 // Write the record containing declaration references of Sema.
4254 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004255 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004256
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004257 // Write the record containing CUDA-specific declaration references.
4258 if (!CUDASpecialDeclRefs.empty())
4259 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004260
4261 // Write the delegating constructors.
4262 if (!DelegatingCtorDecls.empty())
4263 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004264
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004265 // Write the known namespaces.
4266 if (!KnownNamespaces.empty())
4267 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004268
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004269 // Write the undefined internal functions and variables, and inline functions.
4270 if (!UndefinedButUsed.empty())
4271 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004272
Douglas Gregor851443c2011-08-12 01:39:19 +00004273 // Write the visible updates to DeclContexts.
4274 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4275 I = UpdatedDeclContexts.begin(),
4276 E = UpdatedDeclContexts.end();
4277 I != E; ++I)
4278 WriteDeclContextVisibleUpdate(*I);
4279
Douglas Gregor959bb062011-12-03 01:15:29 +00004280 if (!WritingModule) {
4281 // Write the submodules that were imported, if any.
4282 RecordData ImportedModules;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004283 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004284 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4285 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4286 }
4287 if (!ImportedModules.empty()) {
4288 // Sort module IDs.
4289 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4290
4291 // Unique module IDs.
4292 ImportedModules.erase(std::unique(ImportedModules.begin(),
4293 ImportedModules.end()),
4294 ImportedModules.end());
4295
4296 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4297 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004298 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004299
Douglas Gregor851443c2011-08-12 01:39:19 +00004300 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004301 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004302 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004303 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004304 WriteLateParsedTemplates(SemaRef);
4305
Douglas Gregor08f01292009-04-17 22:13:46 +00004306 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004307 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004308 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004309 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004310 Record.push_back(NumLexicalDeclContexts);
4311 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004312 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004313 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004314}
4315
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004316/// \brief Go through the declaration update blocks and resolve declaration
4317/// pointers into declaration IDs.
4318void ASTWriter::ResolveDeclUpdatesBlocks() {
4319 for (DeclUpdateMap::iterator
4320 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4321 const Decl *D = I->first;
4322 UpdateRecord &URec = I->second;
4323
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004324 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004325 continue; // The decl will be written completely
4326
4327 unsigned Idx = 0, N = URec.size();
4328 while (Idx < N) {
4329 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004330 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4331 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4332 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4333 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4334 ++Idx;
4335 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004336
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004337 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman276dd182013-09-05 00:02:25 +00004338 case UPD_DECL_MARKED_USED:
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004339 ++Idx;
4340 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004341
4342 case UPD_CXX_DEDUCED_RETURN_TYPE:
4343 URec[Idx] = GetOrCreateTypeID(
4344 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4345 ++Idx;
4346 break;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004347 }
4348 }
4349 }
4350}
4351
Richard Smithb9eab6d2014-03-20 19:44:17 +00004352void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004353 if (DeclUpdates.empty())
4354 return;
4355
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004356 for (DeclUpdateMap::iterator
4357 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4358 const Decl *D = I->first;
4359 UpdateRecord &URec = I->second;
4360
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004361 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004362 continue; // The decl will be written completely,no need to store updates.
4363
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004364 uint64_t Offset = Stream.GetCurrentBitNo();
4365 Stream.EmitRecord(DECL_UPDATES, URec);
4366
Richard Smithb9eab6d2014-03-20 19:44:17 +00004367 // Flush any statements that were written as part of this update record.
4368 FlushStmts();
4369
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004370 OffsetsRecord.push_back(GetDeclRef(D));
4371 OffsetsRecord.push_back(Offset);
4372 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004373}
4374
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004375void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004376 if (ReplacedDecls.empty())
4377 return;
4378
4379 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004380 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4381 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004382 Record.push_back(I->ID);
4383 Record.push_back(I->Offset);
4384 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004385 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004386 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004387}
4388
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004389void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004390 Record.push_back(Loc.getRawEncoding());
4391}
4392
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004393void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004394 AddSourceLocation(Range.getBegin(), Record);
4395 AddSourceLocation(Range.getEnd(), Record);
4396}
4397
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004398void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004399 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004400 const uint64_t *Words = Value.getRawData();
4401 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004402}
4403
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004404void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004405 Record.push_back(Value.isUnsigned());
4406 AddAPInt(Value, Record);
4407}
4408
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004409void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004410 AddAPInt(Value.bitcastToAPInt(), Record);
4411}
4412
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004413void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004414 Record.push_back(getIdentifierRef(II));
4415}
4416
Sebastian Redl539c5062010-08-18 23:57:32 +00004417IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004418 if (II == 0)
4419 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004420
Sebastian Redl539c5062010-08-18 23:57:32 +00004421 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004422 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004423 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004424 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004425}
4426
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004427MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004428 // Don't emit builtin macros like __LINE__ to the AST file unless they
4429 // have been redefined by the header (in which case they are not
4430 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004431 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004432 return 0;
4433
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004434 MacroID &ID = MacroIDs[MI];
4435 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004436 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004437 MacroInfoToEmitData Info = { Name, MI, ID };
4438 MacroInfosToEmit.push_back(Info);
4439 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004440 return ID;
4441}
4442
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004443MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4444 if (MI == 0 || MI->isBuiltinMacro())
4445 return 0;
4446
4447 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4448 return MacroIDs[MI];
4449}
4450
4451uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4452 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4453 return IdentMacroDirectivesOffsetMap[Name];
4454}
4455
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004456void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004457 Record.push_back(getSelectorRef(SelRef));
4458}
4459
Sebastian Redl539c5062010-08-18 23:57:32 +00004460SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004461 if (Sel.getAsOpaquePtr() == 0) {
4462 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004463 }
4464
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004465 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004466 if (SID == 0 && Chain) {
4467 // This might trigger a ReadSelector callback, which will set the ID for
4468 // this selector.
4469 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004470 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004471 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004472 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004473 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004474 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004475 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004476 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004477}
4478
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004479void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004480 AddDeclRef(Temp->getDestructor(), Record);
4481}
4482
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004483void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4484 CXXBaseSpecifier const *BasesEnd,
4485 RecordDataImpl &Record) {
4486 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4487 CXXBaseSpecifiersToWrite.push_back(
4488 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4489 Bases, BasesEnd));
4490 Record.push_back(NextCXXBaseSpecifiersID++);
4491}
4492
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004493void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004494 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004495 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004496 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004497 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004498 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004499 break;
4500 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004501 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004502 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004503 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004504 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004505 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004506 break;
4507 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004508 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004509 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004510 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004511 break;
John McCall0ad16662009-10-29 08:12:44 +00004512 case TemplateArgument::Null:
4513 case TemplateArgument::Integral:
4514 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004515 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004516 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004517 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004518 break;
4519 }
4520}
4521
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004522void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004523 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004524 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004525
4526 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4527 bool InfoHasSameExpr
4528 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4529 Record.push_back(InfoHasSameExpr);
4530 if (InfoHasSameExpr)
4531 return; // Avoid storing the same expr twice.
4532 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004533 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4534 Record);
4535}
4536
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004537void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4538 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004539 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004540 AddTypeRef(QualType(), Record);
4541 return;
4542 }
4543
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004544 AddTypeLoc(TInfo->getTypeLoc(), Record);
4545}
4546
4547void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4548 AddTypeRef(TL.getType(), Record);
4549
John McCall8f115c62009-10-16 21:56:05 +00004550 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004551 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004552 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004553}
4554
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004555void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004556 Record.push_back(GetOrCreateTypeID(T));
4557}
4558
Douglas Gregoreda8e122011-08-09 15:13:55 +00004559TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004560 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004561 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004562 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4563}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004564
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004565TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004566 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004567 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004568 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004569}
4570
4571TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4572 if (T.isNull())
4573 return TypeIdx();
4574 assert(!T.getLocalFastQualifiers());
4575
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004576 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004577 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004578 if (DoneWritingDeclsAndTypes) {
4579 assert(0 && "New type seen after serializing all the types to emit!");
4580 return TypeIdx();
4581 }
4582
Douglas Gregor1970d882009-04-26 03:49:13 +00004583 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004584 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004585 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004586 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004587 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004588 return Idx;
4589}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004590
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004591TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004592 if (T.isNull())
4593 return TypeIdx();
4594 assert(!T.getLocalFastQualifiers());
4595
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004596 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4597 assert(I != TypeIdxs.end() && "Type not emitted!");
4598 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004599}
4600
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004601void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004602 Record.push_back(GetDeclRef(D));
4603}
4604
Sebastian Redl539c5062010-08-18 23:57:32 +00004605DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004606 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4607
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004608 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004609 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004610 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004611
4612 // If D comes from an AST file, its declaration ID is already known and
4613 // fixed.
4614 if (D->isFromASTFile())
4615 return D->getGlobalID();
4616
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004617 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004618 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004619 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004620 if (DoneWritingDeclsAndTypes) {
4621 assert(0 && "New decl seen after serializing all the decls to emit!");
4622 return 0;
4623 }
4624
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004625 // We haven't seen this declaration before. Give it a new ID and
4626 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004627 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004628 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004629 }
4630
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004631 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004632}
4633
Sebastian Redl539c5062010-08-18 23:57:32 +00004634DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004635 if (D == 0)
4636 return 0;
4637
Douglas Gregorb3163e52012-01-05 22:33:30 +00004638 // If D comes from an AST file, its declaration ID is already known and
4639 // fixed.
4640 if (D->isFromASTFile())
4641 return D->getGlobalID();
4642
Douglas Gregore84a9da2009-04-20 20:36:09 +00004643 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4644 return DeclIDs[D];
4645}
4646
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004647void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004648 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004649 assert(D);
4650
4651 SourceLocation Loc = D->getLocation();
4652 if (Loc.isInvalid())
4653 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004654
4655 // We only keep track of the file-level declarations of each file.
4656 if (!D->getLexicalDeclContext()->isFileContext())
4657 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004658 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4659 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004660 if (isa<ParmVarDecl>(D))
4661 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004662
4663 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004664 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004665 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004666 FileID FID;
4667 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004668 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004669 if (FID.isInvalid())
4670 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004671 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004672
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004673 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004674 if (!Info)
4675 Info = new DeclIDInFileInfo();
4676
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004677 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004678 LocDeclIDsTy &Decls = Info->DeclIDs;
4679
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004680 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004681 Decls.push_back(LocDecl);
4682 return;
4683 }
4684
Benjamin Kramer45025c02013-08-24 13:22:59 +00004685 LocDeclIDsTy::iterator I =
4686 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004687
4688 Decls.insert(I, LocDecl);
4689}
4690
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004691void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004692 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004693 Record.push_back(Name.getNameKind());
4694 switch (Name.getNameKind()) {
4695 case DeclarationName::Identifier:
4696 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4697 break;
4698
4699 case DeclarationName::ObjCZeroArgSelector:
4700 case DeclarationName::ObjCOneArgSelector:
4701 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004702 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004703 break;
4704
4705 case DeclarationName::CXXConstructorName:
4706 case DeclarationName::CXXDestructorName:
4707 case DeclarationName::CXXConversionFunctionName:
4708 AddTypeRef(Name.getCXXNameType(), Record);
4709 break;
4710
4711 case DeclarationName::CXXOperatorName:
4712 Record.push_back(Name.getCXXOverloadedOperator());
4713 break;
4714
Alexis Hunt3d221f22009-11-29 07:34:05 +00004715 case DeclarationName::CXXLiteralOperatorName:
4716 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4717 break;
4718
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004719 case DeclarationName::CXXUsingDirective:
4720 // No extra data to emit
4721 break;
4722 }
4723}
Chris Lattnerca025db2010-05-07 21:43:38 +00004724
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004725void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004726 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004727 switch (Name.getNameKind()) {
4728 case DeclarationName::CXXConstructorName:
4729 case DeclarationName::CXXDestructorName:
4730 case DeclarationName::CXXConversionFunctionName:
4731 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4732 break;
4733
4734 case DeclarationName::CXXOperatorName:
4735 AddSourceLocation(
4736 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4737 Record);
4738 AddSourceLocation(
4739 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4740 Record);
4741 break;
4742
4743 case DeclarationName::CXXLiteralOperatorName:
4744 AddSourceLocation(
4745 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4746 Record);
4747 break;
4748
4749 case DeclarationName::Identifier:
4750 case DeclarationName::ObjCZeroArgSelector:
4751 case DeclarationName::ObjCOneArgSelector:
4752 case DeclarationName::ObjCMultiArgSelector:
4753 case DeclarationName::CXXUsingDirective:
4754 break;
4755 }
4756}
4757
4758void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004759 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004760 AddDeclarationName(NameInfo.getName(), Record);
4761 AddSourceLocation(NameInfo.getLoc(), Record);
4762 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4763}
4764
4765void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004766 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004767 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004768 Record.push_back(Info.NumTemplParamLists);
4769 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4770 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4771}
4772
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004773void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004774 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004775 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004776 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004777 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004778
4779 // Push each of the NNS's onto a stack for serialization in reverse order.
4780 while (NNS) {
4781 NestedNames.push_back(NNS);
4782 NNS = NNS->getPrefix();
4783 }
4784
4785 Record.push_back(NestedNames.size());
4786 while(!NestedNames.empty()) {
4787 NNS = NestedNames.pop_back_val();
4788 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4789 Record.push_back(Kind);
4790 switch (Kind) {
4791 case NestedNameSpecifier::Identifier:
4792 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4793 break;
4794
4795 case NestedNameSpecifier::Namespace:
4796 AddDeclRef(NNS->getAsNamespace(), Record);
4797 break;
4798
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004799 case NestedNameSpecifier::NamespaceAlias:
4800 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4801 break;
4802
Chris Lattnerca025db2010-05-07 21:43:38 +00004803 case NestedNameSpecifier::TypeSpec:
4804 case NestedNameSpecifier::TypeSpecWithTemplate:
4805 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4806 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4807 break;
4808
4809 case NestedNameSpecifier::Global:
4810 // Don't need to write an associated value.
4811 break;
4812 }
4813 }
4814}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004815
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004816void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4817 RecordDataImpl &Record) {
4818 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004819 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004820 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004821
4822 // Push each of the nested-name-specifiers's onto a stack for
4823 // serialization in reverse order.
4824 while (NNS) {
4825 NestedNames.push_back(NNS);
4826 NNS = NNS.getPrefix();
4827 }
4828
4829 Record.push_back(NestedNames.size());
4830 while(!NestedNames.empty()) {
4831 NNS = NestedNames.pop_back_val();
4832 NestedNameSpecifier::SpecifierKind Kind
4833 = NNS.getNestedNameSpecifier()->getKind();
4834 Record.push_back(Kind);
4835 switch (Kind) {
4836 case NestedNameSpecifier::Identifier:
4837 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4838 AddSourceRange(NNS.getLocalSourceRange(), Record);
4839 break;
4840
4841 case NestedNameSpecifier::Namespace:
4842 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4843 AddSourceRange(NNS.getLocalSourceRange(), Record);
4844 break;
4845
4846 case NestedNameSpecifier::NamespaceAlias:
4847 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4848 AddSourceRange(NNS.getLocalSourceRange(), Record);
4849 break;
4850
4851 case NestedNameSpecifier::TypeSpec:
4852 case NestedNameSpecifier::TypeSpecWithTemplate:
4853 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4854 AddTypeLoc(NNS.getTypeLoc(), Record);
4855 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4856 break;
4857
4858 case NestedNameSpecifier::Global:
4859 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4860 break;
4861 }
4862 }
4863}
4864
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004865void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004866 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004867 Record.push_back(Kind);
4868 switch (Kind) {
4869 case TemplateName::Template:
4870 AddDeclRef(Name.getAsTemplateDecl(), Record);
4871 break;
4872
4873 case TemplateName::OverloadedTemplate: {
4874 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4875 Record.push_back(OvT->size());
4876 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4877 I != E; ++I)
4878 AddDeclRef(*I, Record);
4879 break;
4880 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004881
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004882 case TemplateName::QualifiedTemplate: {
4883 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4884 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4885 Record.push_back(QualT->hasTemplateKeyword());
4886 AddDeclRef(QualT->getTemplateDecl(), Record);
4887 break;
4888 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004889
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004890 case TemplateName::DependentTemplate: {
4891 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4892 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4893 Record.push_back(DepT->isIdentifier());
4894 if (DepT->isIdentifier())
4895 AddIdentifierRef(DepT->getIdentifier(), Record);
4896 else
4897 Record.push_back(DepT->getOperator());
4898 break;
4899 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004900
4901 case TemplateName::SubstTemplateTemplateParm: {
4902 SubstTemplateTemplateParmStorage *subst
4903 = Name.getAsSubstTemplateTemplateParm();
4904 AddDeclRef(subst->getParameter(), Record);
4905 AddTemplateName(subst->getReplacement(), Record);
4906 break;
4907 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004908
4909 case TemplateName::SubstTemplateTemplateParmPack: {
4910 SubstTemplateTemplateParmPackStorage *SubstPack
4911 = Name.getAsSubstTemplateTemplateParmPack();
4912 AddDeclRef(SubstPack->getParameterPack(), Record);
4913 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4914 break;
4915 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004916 }
4917}
4918
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004919void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004920 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004921 Record.push_back(Arg.getKind());
4922 switch (Arg.getKind()) {
4923 case TemplateArgument::Null:
4924 break;
4925 case TemplateArgument::Type:
4926 AddTypeRef(Arg.getAsType(), Record);
4927 break;
4928 case TemplateArgument::Declaration:
4929 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004930 Record.push_back(Arg.isDeclForReferenceParam());
4931 break;
4932 case TemplateArgument::NullPtr:
4933 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004934 break;
4935 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004936 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004937 AddTypeRef(Arg.getIntegralType(), Record);
4938 break;
4939 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004940 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4941 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004942 case TemplateArgument::TemplateExpansion:
4943 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004944 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004945 Record.push_back(*NumExpansions + 1);
4946 else
4947 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004948 break;
4949 case TemplateArgument::Expression:
4950 AddStmt(Arg.getAsExpr());
4951 break;
4952 case TemplateArgument::Pack:
4953 Record.push_back(Arg.pack_size());
4954 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4955 I != E; ++I)
4956 AddTemplateArgument(*I, Record);
4957 break;
4958 }
4959}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004960
4961void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004962ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004963 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004964 assert(TemplateParams && "No TemplateParams!");
4965 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4966 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4967 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4968 Record.push_back(TemplateParams->size());
4969 for (TemplateParameterList::const_iterator
4970 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4971 P != PEnd; ++P)
4972 AddDeclRef(*P, Record);
4973}
4974
4975/// \brief Emit a template argument list.
4976void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004977ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004978 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004979 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004980 Record.push_back(TemplateArgs->size());
4981 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004982 AddTemplateArgument(TemplateArgs->get(i), Record);
4983}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004984
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00004985void
4986ASTWriter::AddASTTemplateArgumentListInfo
4987(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4988 assert(ASTTemplArgList && "No ASTTemplArgList!");
4989 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4990 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4991 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4992 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4993 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4994 AddTemplateArgumentLoc(TemplArgs[i], Record);
4995}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004996
4997void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004998ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004999 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005000 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005001 I = Set.begin(), E = Set.end(); I != E; ++I) {
5002 AddDeclRef(I.getDecl(), Record);
5003 Record.push_back(I.getAccess());
5004 }
5005}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005006
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005007void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005008 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005009 Record.push_back(Base.isVirtual());
5010 Record.push_back(Base.isBaseOfClass());
5011 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005012 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005013 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005014 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005015 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5016 : SourceLocation(),
5017 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005018}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005019
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005020void ASTWriter::FlushCXXBaseSpecifiers() {
5021 RecordData Record;
5022 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5023 Record.clear();
5024
5025 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005026 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005027 if (Index == CXXBaseSpecifiersOffsets.size())
5028 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5029 else {
5030 if (Index > CXXBaseSpecifiersOffsets.size())
5031 CXXBaseSpecifiersOffsets.resize(Index + 1);
5032 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5033 }
5034
5035 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5036 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5037 Record.push_back(BEnd - B);
5038 for (; B != BEnd; ++B)
5039 AddCXXBaseSpecifier(*B, Record);
5040 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005041
5042 // Flush any expressions that were written as part of the base specifiers.
5043 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005044 }
5045
5046 CXXBaseSpecifiersToWrite.clear();
5047}
5048
Alexis Hunt1d792652011-01-08 20:30:50 +00005049void ASTWriter::AddCXXCtorInitializers(
5050 const CXXCtorInitializer * const *CtorInitializers,
5051 unsigned NumCtorInitializers,
5052 RecordDataImpl &Record) {
5053 Record.push_back(NumCtorInitializers);
5054 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5055 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005056
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005057 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005058 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005059 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005060 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005061 } else if (Init->isDelegatingInitializer()) {
5062 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005063 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005064 } else if (Init->isMemberInitializer()){
5065 Record.push_back(CTOR_INITIALIZER_MEMBER);
5066 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005067 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005068 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5069 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005070 }
Francois Pichetd583da02010-12-04 09:14:42 +00005071
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005072 AddSourceLocation(Init->getMemberLocation(), Record);
5073 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005074 AddSourceLocation(Init->getLParenLoc(), Record);
5075 AddSourceLocation(Init->getRParenLoc(), Record);
5076 Record.push_back(Init->isWritten());
5077 if (Init->isWritten()) {
5078 Record.push_back(Init->getSourceOrder());
5079 } else {
5080 Record.push_back(Init->getNumArrayIndices());
5081 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5082 AddDeclRef(Init->getArrayIndex(i), Record);
5083 }
5084 }
5085}
5086
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005087void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5088 assert(D->DefinitionData);
5089 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005090 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005091 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005092 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005093 Record.push_back(Data.Aggregate);
5094 Record.push_back(Data.PlainOldData);
5095 Record.push_back(Data.Empty);
5096 Record.push_back(Data.Polymorphic);
5097 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005098 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005099 Record.push_back(Data.HasNoNonEmptyBases);
5100 Record.push_back(Data.HasPrivateFields);
5101 Record.push_back(Data.HasProtectedFields);
5102 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005103 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005104 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005105 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005106 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005107 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005108 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5109 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5110 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5111 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5112 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5113 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005114 Record.push_back(Data.HasTrivialSpecialMembers);
5115 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005116 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005117 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005118 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005119 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005120 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005121 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005122 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005123 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5124 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5125 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5126 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005127 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005128
5129 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005130 if (Data.NumBases > 0)
5131 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5132 Record);
5133
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005134 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5135 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005136 if (Data.NumVBases > 0)
5137 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5138 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005139
Richard Smitha4ba74c2013-08-30 04:46:40 +00005140 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5141 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005142 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005143 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005144
5145 // Add lambda-specific data.
5146 if (Data.IsLambda) {
5147 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005148 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005149 Record.push_back(Lambda.IsGenericLambda);
5150 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005151 Record.push_back(Lambda.NumCaptures);
5152 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005153 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005154 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005155 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005156 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5157 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5158 AddSourceLocation(Capture.getLocation(), Record);
5159 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005160 Record.push_back(Capture.getCaptureKind());
5161 switch (Capture.getCaptureKind()) {
5162 case LCK_This:
5163 break;
5164 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005165 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005166 VarDecl *Var =
5167 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5168 AddDeclRef(Var, Record);
5169 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5170 : SourceLocation(),
5171 Record);
5172 break;
5173 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005174 }
5175 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005176}
5177
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005178void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005179 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005180 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005181 assert(FirstDeclID == NextDeclID &&
5182 FirstTypeID == NextTypeID &&
5183 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005184 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005185 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005186 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005187 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005188
Sebastian Redl07a89a82010-07-30 00:29:29 +00005189 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005190
Douglas Gregordf0c1512011-08-18 04:12:04 +00005191 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5192 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5193 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005194 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005195 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005196 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005197 NextDeclID = FirstDeclID;
5198 NextTypeID = FirstTypeID;
5199 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005200 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005201 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005202 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005203}
5204
Sebastian Redl539c5062010-08-18 23:57:32 +00005205void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005206 // Always keep the highest ID. See \p TypeRead() for more information.
5207 IdentID &StoredID = IdentifierIDs[II];
5208 if (ID > StoredID)
5209 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005210}
5211
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005212void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005213 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005214 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005215 if (ID > StoredID)
5216 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005217}
5218
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005219void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005220 // Always take the highest-numbered type index. This copes with an interesting
5221 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005222 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005223 // keep the higher-numbered entry so that we can properly write it out to
5224 // the AST file.
5225 TypeIdx &StoredIdx = TypeIdxs[T];
5226 if (Idx.getIndex() >= StoredIdx.getIndex())
5227 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005228}
5229
Sebastian Redl539c5062010-08-18 23:57:32 +00005230void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005231 // Always keep the highest ID. See \p TypeRead() for more information.
5232 SelectorID &StoredID = SelectorIDs[S];
5233 if (ID > StoredID)
5234 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005235}
Douglas Gregor91096292010-10-02 19:29:26 +00005236
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005237void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005238 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005239 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005240 MacroDefinitions[MD] = ID;
5241}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005242
Douglas Gregore37a85a2011-12-02 17:30:13 +00005243void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5244 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5245 SubmoduleIDs[Mod] = ID;
5246}
5247
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005248void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005249 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005250 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005251 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5252 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005253 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005254 // A forward reference was mutated into a definition. Rewrite it.
5255 // FIXME: This happens during template instantiation, should we
5256 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005257 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005258 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005259 }
5260}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005261
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005262void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005263 assert(!WritingAST && "Already writing the AST!");
5264
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005265 // TU and namespaces are handled elsewhere.
5266 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5267 return;
5268
Douglas Gregorb3722e22011-09-09 23:01:35 +00005269 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005270 return; // Not a source decl added to a DeclContext from PCH.
5271
Douglas Gregor9f782892013-01-21 15:25:38 +00005272 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005273 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005274 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005275}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005276
5277void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005278 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005279 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005280 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005281 return; // Not a source member added to a class from PCH.
5282 if (!isa<CXXMethodDecl>(D))
5283 return; // We are interested in lazily declared implicit methods.
5284
5285 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005286 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005287 UpdateRecord &Record = DeclUpdates[RD];
5288 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005289 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005290}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005291
5292void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5293 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005294 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005295 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005296 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005297 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005298 return; // Not a source specialization added to a template from PCH.
5299
5300 UpdateRecord &Record = DeclUpdates[TD];
5301 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005302 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005303}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005304
Larisse Voufo39a1e502013-08-06 01:03:05 +00005305void ASTWriter::AddedCXXTemplateSpecialization(
5306 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5307 // The specializations set is kept in the canonical template.
5308 assert(!WritingAST && "Already writing the AST!");
5309 TD = TD->getCanonicalDecl();
5310 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5311 return; // Not a source specialization added to a template from PCH.
5312
5313 UpdateRecord &Record = DeclUpdates[TD];
5314 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5315 Record.push_back(reinterpret_cast<uint64_t>(D));
5316}
5317
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005318void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5319 const FunctionDecl *D) {
5320 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005321 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005322 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005323 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005324 return; // Not a source specialization added to a template from PCH.
5325
5326 UpdateRecord &Record = DeclUpdates[TD];
5327 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005328 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005329}
5330
Richard Smith1fa5d642013-05-11 05:45:24 +00005331void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5332 assert(!WritingAST && "Already writing the AST!");
5333 FD = FD->getCanonicalDecl();
5334 if (!FD->isFromASTFile())
5335 return; // Not a function declared in PCH and defined outside.
5336
5337 UpdateRecord &Record = DeclUpdates[FD];
5338 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5339 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5340}
5341
Sebastian Redlab238a72011-04-24 16:28:06 +00005342void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005343 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005344 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005345 return; // Declaration not imported from PCH.
5346
5347 // Implicit decl from a PCH was defined.
5348 // FIXME: Should implicit definition be a separate FunctionDecl?
5349 RewriteDecl(D);
5350}
5351
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005352void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005353 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005354 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005355 return;
5356
5357 // Since the actual instantiation is delayed, this really means that we need
5358 // to update the instantiation location.
5359 UpdateRecord &Record = DeclUpdates[D];
5360 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5361 AddSourceLocation(
5362 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5363}
5364
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005365void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5366 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005367 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005368 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005369 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005370
5371 assert(IFD->getDefinition() && "Category on a class without a definition?");
5372 ObjCClassesWithCategories.insert(
5373 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005374}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005375
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005376
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005377void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5378 const ObjCPropertyDecl *OrigProp,
5379 const ObjCCategoryDecl *ClassExt) {
5380 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5381 if (!D)
5382 return;
5383
5384 assert(!WritingAST && "Already writing the AST!");
5385 if (!D->isFromASTFile())
5386 return; // Declaration not imported from PCH.
5387
5388 RewriteDecl(D);
5389}
Eli Friedman276dd182013-09-05 00:02:25 +00005390
5391void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5392 assert(!WritingAST && "Already writing the AST!");
5393 if (!D->isFromASTFile())
5394 return;
5395
5396 UpdateRecord &Record = DeclUpdates[D];
5397 Record.push_back(UPD_DECL_MARKED_USED);
5398}