blob: cb059ecc4e3ea77bd9675f279ae3b84a978b0775 [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());
John McCall8b07ec22010-05-15 11:32:37 +0000404 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000405 E = T->qual_end(); I != E; ++I)
406 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000407 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000408}
409
Steve Narofffb4330f2009-06-17 22:40:22 +0000410void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000412 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000413 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000414}
415
Eli Friedman0dfb8892011-10-06 23:00:33 +0000416void
417ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
418 Writer.AddTypeRef(T->getValueType(), Record);
419 Code = TYPE_ATOMIC;
420}
421
John McCall8f115c62009-10-16 21:56:05 +0000422namespace {
423
424class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000425 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000426 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000427
428public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000429 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000430 : Writer(Writer), Record(Record) { }
431
John McCall17001972009-10-18 01:05:36 +0000432#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000433#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000434 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000435#include "clang/AST/TypeLocNodes.def"
436
John McCall17001972009-10-18 01:05:36 +0000437 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
438 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000439};
440
441}
442
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
444 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000447 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
448 if (TL.needsExtraLocalData()) {
449 Record.push_back(TL.getWrittenTypeSpec());
450 Record.push_back(TL.getWrittenSignSpec());
451 Record.push_back(TL.getWrittenWidthSpec());
452 Record.push_back(TL.hasModeAttr());
453 }
John McCall8f115c62009-10-16 21:56:05 +0000454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000457}
John McCall17001972009-10-18 01:05:36 +0000458void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000460}
Reid Kleckner8a365022013-06-24 17:51:48 +0000461void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
462 // nothing to do
463}
Reid Kleckner0503a872013-12-05 01:23:43 +0000464void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
465 // nothing to do
466}
John McCall17001972009-10-18 01:05:36 +0000467void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000469}
John McCall17001972009-10-18 01:05:36 +0000470void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000472}
John McCall17001972009-10-18 01:05:36 +0000473void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000475}
John McCall17001972009-10-18 01:05:36 +0000476void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000478 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
482 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
483 Record.push_back(TL.getSizeExpr() ? 1 : 0);
484 if (TL.getSizeExpr())
485 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
488 VisitArrayTypeLoc(TL);
489}
490void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
491 VisitArrayTypeLoc(TL);
492}
493void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
494 VisitArrayTypeLoc(TL);
495}
496void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
497 DependentSizedArrayTypeLoc TL) {
498 VisitArrayTypeLoc(TL);
499}
500void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
501 DependentSizedExtVectorTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
504void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000511 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000514 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000515 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
516 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
519 VisitFunctionTypeLoc(TL);
520}
521void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
522 VisitFunctionTypeLoc(TL);
523}
John McCallb96ec562009-12-04 22:46:56 +0000524void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall17001972009-10-18 01:05:36 +0000527void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000531 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000534}
535void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000536 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
537 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
538 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
539 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000540}
541void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Alexis Hunte852b102011-05-24 22:41:36 +0000544void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getKWLoc(), Record);
546 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
547 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
548 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
549}
Richard Smith30482bc2011-02-20 03:19:35 +0000550void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
556void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall81904512011-01-06 01:58:22 +0000559void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
561 if (TL.hasAttrOperand()) {
562 SourceRange range = TL.getAttrOperandParensRange();
563 Writer.AddSourceLocation(range.getBegin(), Record);
564 Writer.AddSourceLocation(range.getEnd(), Record);
565 }
566 if (TL.hasAttrExprOperand()) {
567 Expr *operand = TL.getAttrExprOperand();
568 Record.push_back(operand ? 1 : 0);
569 if (operand) Writer.AddStmt(operand);
570 } else if (TL.hasAttrEnumOperand()) {
571 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
572 }
573}
John McCall17001972009-10-18 01:05:36 +0000574void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576}
John McCallcebee162009-10-18 09:09:24 +0000577void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
578 SubstTemplateTypeParmTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
Douglas Gregorada4b792011-01-14 02:55:32 +0000581void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
582 SubstTemplateTypeParmPackTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
586 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000587 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000588 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
589 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
590 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
591 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000592 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
593 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000594}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000595void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
596 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
597 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
598}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000599void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000600 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000601 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000602}
John McCalle78aac42010-03-10 03:28:59 +0000603void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
605}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000606void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000607 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000608 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000609 Writer.AddSourceLocation(TL.getNameLoc(), Record);
610}
John McCallc392f372010-06-11 00:33:02 +0000611void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
612 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000613 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000614 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000615 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000616 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000617 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
618 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
619 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000620 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
621 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000622}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000623void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
625}
John McCall17001972009-10-18 01:05:36 +0000626void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
627 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000628}
629void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
630 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000631 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
632 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
633 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
634 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000635}
John McCallfc93cf92009-10-22 22:37:11 +0000636void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
637 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000638}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000639void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
640 Writer.AddSourceLocation(TL.getKWLoc(), Record);
641 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
642 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
643}
John McCall8f115c62009-10-16 21:56:05 +0000644
Chris Lattner19cea4e2009-04-22 05:57:30 +0000645//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000646// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000647//===----------------------------------------------------------------------===//
648
Chris Lattner28fa4e62009-04-26 22:26:21 +0000649static void EmitBlockID(unsigned ID, const char *Name,
650 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000651 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000652 Record.clear();
653 Record.push_back(ID);
654 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
655
656 // Emit the block name if present.
657 if (Name == 0 || Name[0] == 0) return;
658 Record.clear();
659 while (*Name)
660 Record.push_back(*Name++);
661 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
662}
663
664static void EmitRecordID(unsigned ID, const char *Name,
665 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000666 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 Record.clear();
668 Record.push_back(ID);
669 while (*Name)
670 Record.push_back(*Name++);
671 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000672}
673
674static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000675 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000676#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000677 RECORD(STMT_STOP);
678 RECORD(STMT_NULL_PTR);
679 RECORD(STMT_NULL);
680 RECORD(STMT_COMPOUND);
681 RECORD(STMT_CASE);
682 RECORD(STMT_DEFAULT);
683 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000684 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000685 RECORD(STMT_IF);
686 RECORD(STMT_SWITCH);
687 RECORD(STMT_WHILE);
688 RECORD(STMT_DO);
689 RECORD(STMT_FOR);
690 RECORD(STMT_GOTO);
691 RECORD(STMT_INDIRECT_GOTO);
692 RECORD(STMT_CONTINUE);
693 RECORD(STMT_BREAK);
694 RECORD(STMT_RETURN);
695 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000696 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000697 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000698 RECORD(EXPR_PREDEFINED);
699 RECORD(EXPR_DECL_REF);
700 RECORD(EXPR_INTEGER_LITERAL);
701 RECORD(EXPR_FLOATING_LITERAL);
702 RECORD(EXPR_IMAGINARY_LITERAL);
703 RECORD(EXPR_STRING_LITERAL);
704 RECORD(EXPR_CHARACTER_LITERAL);
705 RECORD(EXPR_PAREN);
706 RECORD(EXPR_UNARY_OPERATOR);
707 RECORD(EXPR_SIZEOF_ALIGN_OF);
708 RECORD(EXPR_ARRAY_SUBSCRIPT);
709 RECORD(EXPR_CALL);
710 RECORD(EXPR_MEMBER);
711 RECORD(EXPR_BINARY_OPERATOR);
712 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
713 RECORD(EXPR_CONDITIONAL_OPERATOR);
714 RECORD(EXPR_IMPLICIT_CAST);
715 RECORD(EXPR_CSTYLE_CAST);
716 RECORD(EXPR_COMPOUND_LITERAL);
717 RECORD(EXPR_EXT_VECTOR_ELEMENT);
718 RECORD(EXPR_INIT_LIST);
719 RECORD(EXPR_DESIGNATED_INIT);
720 RECORD(EXPR_IMPLICIT_VALUE_INIT);
721 RECORD(EXPR_VA_ARG);
722 RECORD(EXPR_ADDR_LABEL);
723 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000724 RECORD(EXPR_CHOOSE);
725 RECORD(EXPR_GNU_NULL);
726 RECORD(EXPR_SHUFFLE_VECTOR);
727 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000728 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000729 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000730 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000731 RECORD(EXPR_OBJC_ARRAY_LITERAL);
732 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000733 RECORD(EXPR_OBJC_ENCODE);
734 RECORD(EXPR_OBJC_SELECTOR_EXPR);
735 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
736 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
737 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
738 RECORD(EXPR_OBJC_KVC_REF_EXPR);
739 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000740 RECORD(STMT_OBJC_FOR_COLLECTION);
741 RECORD(STMT_OBJC_CATCH);
742 RECORD(STMT_OBJC_FINALLY);
743 RECORD(STMT_OBJC_AT_TRY);
744 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
745 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000746 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000747 RECORD(EXPR_CXX_OPERATOR_CALL);
748 RECORD(EXPR_CXX_CONSTRUCT);
749 RECORD(EXPR_CXX_STATIC_CAST);
750 RECORD(EXPR_CXX_DYNAMIC_CAST);
751 RECORD(EXPR_CXX_REINTERPRET_CAST);
752 RECORD(EXPR_CXX_CONST_CAST);
753 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000754 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000755 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000756 RECORD(EXPR_CXX_BOOL_LITERAL);
757 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000758 RECORD(EXPR_CXX_TYPEID_EXPR);
759 RECORD(EXPR_CXX_TYPEID_TYPE);
760 RECORD(EXPR_CXX_UUIDOF_EXPR);
761 RECORD(EXPR_CXX_UUIDOF_TYPE);
762 RECORD(EXPR_CXX_THIS);
763 RECORD(EXPR_CXX_THROW);
764 RECORD(EXPR_CXX_DEFAULT_ARG);
765 RECORD(EXPR_CXX_BIND_TEMPORARY);
766 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
767 RECORD(EXPR_CXX_NEW);
768 RECORD(EXPR_CXX_DELETE);
769 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
770 RECORD(EXPR_EXPR_WITH_CLEANUPS);
771 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
772 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
773 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
774 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
775 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000776 RECORD(EXPR_CXX_NOEXCEPT);
777 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000778 RECORD(EXPR_PACK_EXPANSION);
779 RECORD(EXPR_SIZEOF_PACK);
780 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000781 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000782#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000783}
Mike Stump11289f42009-09-09 15:08:12 +0000784
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000785void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RecordData Record;
787 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000788
Sebastian Redl539c5062010-08-18 23:57:32 +0000789#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
790#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000791
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000792 // Control Block.
793 BLOCK(CONTROL_BLOCK);
794 RECORD(METADATA);
795 RECORD(IMPORTS);
796 RECORD(LANGUAGE_OPTIONS);
797 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000798 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000799 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000800 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000801 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000802 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000803 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000804 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000805 RECORD(PREPROCESSOR_OPTIONS);
806
Douglas Gregor108cb222012-10-19 00:45:00 +0000807 BLOCK(INPUT_FILES_BLOCK);
808 RECORD(INPUT_FILE);
809
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000810 // AST Top-Level Block.
811 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000812 RECORD(TYPE_OFFSET);
813 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000814 RECORD(IDENTIFIER_OFFSET);
815 RECORD(IDENTIFIER_TABLE);
816 RECORD(EXTERNAL_DEFINITIONS);
817 RECORD(SPECIAL_TYPES);
818 RECORD(STATISTICS);
819 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000820 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000821 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 RECORD(SELECTOR_OFFSETS);
823 RECORD(METHOD_POOL);
824 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000825 RECORD(SOURCE_LOCATION_OFFSETS);
826 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000827 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000828 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000829 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000830 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000831 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000832 RECORD(SEMA_DECL_REFS);
833 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
834 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
835 RECORD(DECL_REPLACEMENTS);
836 RECORD(UPDATE_VISIBLE);
837 RECORD(DECL_UPDATE_OFFSETS);
838 RECORD(DECL_UPDATES);
839 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
840 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000841 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000842 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000843 RECORD(FP_PRAGMA_OPTIONS);
844 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000845 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000846 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000847 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000848 RECORD(MODULE_OFFSET_MAP);
849 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000850 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000851 RECORD(FILE_SORTED_DECLS);
852 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000853 RECORD(MERGED_DECLARATIONS);
854 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000855 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000856 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000857 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000858 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000859
Chris Lattner28fa4e62009-04-26 22:26:21 +0000860 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000861 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000862 RECORD(SM_SLOC_FILE_ENTRY);
863 RECORD(SM_SLOC_BUFFER_ENTRY);
864 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000865 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000866
Chris Lattner28fa4e62009-04-26 22:26:21 +0000867 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000868 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000869 RECORD(PP_MACRO_OBJECT_LIKE);
870 RECORD(PP_MACRO_FUNCTION_LIKE);
871 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000872
Douglas Gregor12bfa382009-10-17 00:13:19 +0000873 // Decls and Types block.
874 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000875 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000876 RECORD(TYPE_COMPLEX);
877 RECORD(TYPE_POINTER);
878 RECORD(TYPE_BLOCK_POINTER);
879 RECORD(TYPE_LVALUE_REFERENCE);
880 RECORD(TYPE_RVALUE_REFERENCE);
881 RECORD(TYPE_MEMBER_POINTER);
882 RECORD(TYPE_CONSTANT_ARRAY);
883 RECORD(TYPE_INCOMPLETE_ARRAY);
884 RECORD(TYPE_VARIABLE_ARRAY);
885 RECORD(TYPE_VECTOR);
886 RECORD(TYPE_EXT_VECTOR);
887 RECORD(TYPE_FUNCTION_PROTO);
888 RECORD(TYPE_FUNCTION_NO_PROTO);
889 RECORD(TYPE_TYPEDEF);
890 RECORD(TYPE_TYPEOF_EXPR);
891 RECORD(TYPE_TYPEOF);
892 RECORD(TYPE_RECORD);
893 RECORD(TYPE_ENUM);
894 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000895 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000896 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000897 RECORD(TYPE_DECLTYPE);
898 RECORD(TYPE_ELABORATED);
899 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
900 RECORD(TYPE_UNRESOLVED_USING);
901 RECORD(TYPE_INJECTED_CLASS_NAME);
902 RECORD(TYPE_OBJC_OBJECT);
903 RECORD(TYPE_TEMPLATE_TYPE_PARM);
904 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
905 RECORD(TYPE_DEPENDENT_NAME);
906 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
907 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
908 RECORD(TYPE_PAREN);
909 RECORD(TYPE_PACK_EXPANSION);
910 RECORD(TYPE_ATTRIBUTED);
911 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000912 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000913 RECORD(DECL_TYPEDEF);
914 RECORD(DECL_ENUM);
915 RECORD(DECL_RECORD);
916 RECORD(DECL_ENUM_CONSTANT);
917 RECORD(DECL_FUNCTION);
918 RECORD(DECL_OBJC_METHOD);
919 RECORD(DECL_OBJC_INTERFACE);
920 RECORD(DECL_OBJC_PROTOCOL);
921 RECORD(DECL_OBJC_IVAR);
922 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000923 RECORD(DECL_OBJC_CATEGORY);
924 RECORD(DECL_OBJC_CATEGORY_IMPL);
925 RECORD(DECL_OBJC_IMPLEMENTATION);
926 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
927 RECORD(DECL_OBJC_PROPERTY);
928 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000929 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000930 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000931 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000932 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000933 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000934 RECORD(DECL_FILE_SCOPE_ASM);
935 RECORD(DECL_BLOCK);
936 RECORD(DECL_CONTEXT_LEXICAL);
937 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000938 RECORD(DECL_NAMESPACE);
939 RECORD(DECL_NAMESPACE_ALIAS);
940 RECORD(DECL_USING);
941 RECORD(DECL_USING_SHADOW);
942 RECORD(DECL_USING_DIRECTIVE);
943 RECORD(DECL_UNRESOLVED_USING_VALUE);
944 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
945 RECORD(DECL_LINKAGE_SPEC);
946 RECORD(DECL_CXX_RECORD);
947 RECORD(DECL_CXX_METHOD);
948 RECORD(DECL_CXX_CONSTRUCTOR);
949 RECORD(DECL_CXX_DESTRUCTOR);
950 RECORD(DECL_CXX_CONVERSION);
951 RECORD(DECL_ACCESS_SPEC);
952 RECORD(DECL_FRIEND);
953 RECORD(DECL_FRIEND_TEMPLATE);
954 RECORD(DECL_CLASS_TEMPLATE);
955 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
956 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000957 RECORD(DECL_VAR_TEMPLATE);
958 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
959 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000960 RECORD(DECL_FUNCTION_TEMPLATE);
961 RECORD(DECL_TEMPLATE_TYPE_PARM);
962 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
963 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
964 RECORD(DECL_STATIC_ASSERT);
965 RECORD(DECL_CXX_BASE_SPECIFIERS);
966 RECORD(DECL_INDIRECTFIELD);
967 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
968
Douglas Gregor03412ba2011-06-03 02:27:19 +0000969 // Statements and Exprs can occur in the Decls and Types block.
970 AddStmtsExprs(Stream, Record);
971
Douglas Gregor92a96f52011-02-08 21:58:10 +0000972 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000973 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000974 RECORD(PPD_MACRO_DEFINITION);
975 RECORD(PPD_INCLUSION_DIRECTIVE);
976
Chris Lattner28fa4e62009-04-26 22:26:21 +0000977#undef RECORD
978#undef BLOCK
979 Stream.ExitBlock();
980}
981
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000982/// \brief Adjusts the given filename to only write out the portion of the
983/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000984///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985/// \param Filename the file name to adjust.
986///
987/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
988/// the returned filename will be adjusted by this system root.
989///
990/// \returns either the original filename (if it needs no adjustment) or the
991/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000992static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000993adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000994 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000995
Douglas Gregorc567ba22011-07-22 16:35:34 +0000996 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000997 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000998
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000999 // Verify that the filename and the system root have the same prefix.
1000 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001001 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001002 if (Filename[Pos] != isysroot[Pos])
1003 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001004
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001005 // We hit the end of the filename before we hit the end of the system root.
1006 if (!Filename[Pos])
1007 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001008
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001009 // If the file name has a '/' at the current position, skip over the '/'.
1010 // We distinguish sysroot-based includes from absolute includes by the
1011 // absence of '/' at the beginning of sysroot-based includes.
1012 if (Filename[Pos] == '/')
1013 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001015 return Filename + Pos;
1016}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001017
Douglas Gregor112b9072012-10-18 05:31:06 +00001018/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001019void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1020 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001021 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001022 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001023 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1024 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001025
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001026 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001027 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1028 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1029 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1030 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1031 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1032 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1033 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1035 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1036 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1037 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001038 Record.push_back(VERSION_MAJOR);
1039 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001040 Record.push_back(CLANG_VERSION_MAJOR);
1041 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001042 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001043 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001044 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1045 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001046
Douglas Gregor112b9072012-10-18 05:31:06 +00001047 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001048 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001049 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001050 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001051
1052 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1053 M != MEnd; ++M) {
1054 // Skip modules that weren't directly imported.
1055 if (!(*M)->isDirectlyImported())
1056 continue;
1057
1058 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001059 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001060 Record.push_back((*M)->File->getSize());
1061 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001062 // FIXME: This writes the absolute path for AST files we depend on.
1063 const std::string &FileName = (*M)->FileName;
1064 Record.push_back(FileName.size());
1065 Record.append(FileName.begin(), FileName.end());
1066 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001067 Stream.EmitRecord(IMPORTS, Record);
1068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
Douglas Gregor112b9072012-10-18 05:31:06 +00001070 // Language options.
1071 Record.clear();
1072 const LangOptions &LangOpts = Context.getLangOpts();
1073#define LANGOPT(Name, Bits, Default, Description) \
1074 Record.push_back(LangOpts.Name);
1075#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1076 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1077#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001078#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1079#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001080
1081 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1082 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1083
1084 Record.push_back(LangOpts.CurrentModule.size());
1085 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001086
1087 // Comment options.
1088 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1089 for (CommentOptions::BlockCommandNamesTy::const_iterator
1090 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1091 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1092 I != IEnd; ++I) {
1093 AddString(*I, Record);
1094 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001095 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001096
Douglas Gregor112b9072012-10-18 05:31:06 +00001097 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1098
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001099 // Target options.
1100 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001101 const TargetInfo &Target = Context.getTargetInfo();
1102 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001103 AddString(TargetOpts.Triple, Record);
1104 AddString(TargetOpts.CPU, Record);
1105 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001106 AddString(TargetOpts.LinkerVersion, Record);
1107 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1108 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1109 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1110 }
1111 Record.push_back(TargetOpts.Features.size());
1112 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1113 AddString(TargetOpts.Features[I], Record);
1114 }
1115 Stream.EmitRecord(TARGET_OPTIONS, Record);
1116
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001117 // Diagnostic options.
1118 Record.clear();
1119 const DiagnosticOptions &DiagOpts
1120 = Context.getDiagnostics().getDiagnosticOptions();
1121#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1122#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1123 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1124#include "clang/Basic/DiagnosticOptions.def"
1125 Record.push_back(DiagOpts.Warnings.size());
1126 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1127 AddString(DiagOpts.Warnings[I], Record);
1128 // Note: we don't serialize the log or serialization file names, because they
1129 // are generally transient files and will almost always be overridden.
1130 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1131
Douglas Gregorc6317db2012-10-24 15:49:58 +00001132 // File system options.
1133 Record.clear();
1134 const FileSystemOptions &FSOpts
1135 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1136 AddString(FSOpts.WorkingDir, Record);
1137 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1138
Douglas Gregor2d302362012-10-24 16:50:34 +00001139 // Header search options.
1140 Record.clear();
1141 const HeaderSearchOptions &HSOpts
1142 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1143 AddString(HSOpts.Sysroot, Record);
1144
1145 // Include entries.
1146 Record.push_back(HSOpts.UserEntries.size());
1147 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1148 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1149 AddString(Entry.Path, Record);
1150 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001151 Record.push_back(Entry.IsFramework);
1152 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001153 }
1154
1155 // System header prefixes.
1156 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1157 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1158 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1159 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1160 }
1161
1162 AddString(HSOpts.ResourceDir, Record);
1163 AddString(HSOpts.ModuleCachePath, Record);
1164 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
1302 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1303 // the set of (non-system) input files. This is simple heuristic for
1304 // detecting whether the system headers may have changed, because it is too
1305 // expensive to stat() all of the system headers.
Richard Smith0b1f4762013-05-20 23:40:27 +00001306 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bb719f2013-03-20 16:59:53 +00001307 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001308 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1309 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1310 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1311 InputFileEntry Entry = { SDKSettingsFile, false, false };
1312 SortedFiles.push_front(Entry);
1313 }
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001314 }
1315
Douglas Gregora3dd9002013-07-22 20:48:33 +00001316 // Add the compiler's own module.map in the set of (non-system) input files.
1317 // This is a simple heuristic for detecting whether the compiler's headers
1318 // have changed, because we don't want to stat() all of them.
1319 if (Modules && !Chain) {
1320 SmallString<128> P = StringRef(HSOpts.ResourceDir);
1321 llvm::sys::path::append(P, "include");
1322 llvm::sys::path::append(P, "module.map");
1323 if (const FileEntry *ModuleMapFile = FileMgr.getFile(P)) {
1324 InputFileEntry Entry = { ModuleMapFile, false, false };
1325 SortedFiles.push_front(Entry);
1326 }
1327 }
1328
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001329 unsigned UserFilesNum = 0;
1330 // Write out all of the input files.
1331 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001332 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001333 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001334 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001335
Douglas Gregor49491f72013-03-15 22:15:07 +00001336 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001337 if (InputFileID != 0)
1338 continue; // already recorded this file.
1339
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001340 // Record this entry's offset.
1341 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001342
1343 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001344
Douglas Gregor49491f72013-03-15 22:15:07 +00001345 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001346 ++UserFilesNum;
1347
Douglas Gregor72be3902012-10-19 00:38:02 +00001348 Record.clear();
1349 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001350 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001351
1352 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001353 Record.push_back(Entry.File->getSize());
1354 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001355
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001356 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001357 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001358
Douglas Gregor72be3902012-10-19 00:38:02 +00001359 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001360 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001361 SmallString<128> FilePath(Filename);
1362
1363 // Ask the file manager to fixup the relative path for us. This will
1364 // honor the working directory.
Douglas Gregor49491f72013-03-15 22:15:07 +00001365 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001366
1367 // FIXME: This call to make_absolute shouldn't be necessary, the
1368 // call to FixupRelativePath should always return an absolute path.
1369 llvm::sys::fs::make_absolute(FilePath);
1370 Filename = FilePath.c_str();
1371
1372 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1373
1374 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1375 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001376
Douglas Gregor112b9072012-10-18 05:31:06 +00001377 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001378
1379 // Create input file offsets abbreviation.
1380 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1381 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1382 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001383 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1384 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001385 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1386 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1387
1388 // Write input file offsets.
1389 Record.clear();
1390 Record.push_back(INPUT_FILE_OFFSETS);
1391 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001392 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001393 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001394}
1395
Douglas Gregora7f71a92009-04-10 03:52:48 +00001396//===----------------------------------------------------------------------===//
1397// Source Manager Serialization
1398//===----------------------------------------------------------------------===//
1399
1400/// \brief Create an abbreviation for the SLocEntry that refers to a
1401/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001402static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001403 using namespace llvm;
1404 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001405 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001410 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001415 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001416}
1417
1418/// \brief Create an abbreviation for the SLocEntry that refers to a
1419/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001420static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001421 using namespace llvm;
1422 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001423 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001429 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001430}
1431
1432/// \brief Create an abbreviation for the SLocEntry that refers to a
1433/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001434static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001435 using namespace llvm;
1436 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001437 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001439 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001440}
1441
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001442/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1443/// expansion.
1444static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001445 using namespace llvm;
1446 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001447 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001448 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1450 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001453 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001454}
1455
Douglas Gregor09b69892011-02-10 17:09:37 +00001456namespace {
1457 // Trait used for the on-disk hash table of header search information.
1458 class HeaderFileInfoTrait {
1459 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001460 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001461
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001462 // Keep track of the framework names we've used during serialization.
1463 SmallVector<char, 128> FrameworkStringData;
1464 llvm::StringMap<unsigned> FrameworkNameOffset;
1465
Douglas Gregor09b69892011-02-10 17:09:37 +00001466 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001467 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1468 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001469
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001470 struct key_type {
1471 const FileEntry *FE;
1472 const char *Filename;
1473 };
1474 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001475
1476 typedef HeaderFileInfo data_type;
1477 typedef const data_type &data_type_ref;
1478
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001479 static unsigned ComputeHash(key_type_ref key) {
1480 // The hash is based only on size/time of the file, so that the reader can
1481 // match even when symlinking or excess path elements ("foo/../", "../")
1482 // change the form of the name. However, complete path is still the key.
1483 return llvm::hash_combine(key.FE->getSize(),
1484 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001485 }
1486
1487 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001488 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1489 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1490 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001491 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001492 if (Data.isModuleHeader)
1493 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001494 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001495 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001496 }
1497
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001498 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1499 clang::io::Emit64(Out, key.FE->getSize());
1500 KeyLen -= 8;
1501 clang::io::Emit64(Out, key.FE->getModificationTime());
1502 KeyLen -= 8;
1503 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001504 }
1505
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001506 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001507 data_type_ref Data, unsigned DataLen) {
1508 using namespace clang::io;
1509 uint64_t Start = Out.tell(); (void)Start;
1510
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001511 unsigned char Flags = (Data.HeaderRole << 6)
1512 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001513 | (Data.isPragmaOnce << 4)
1514 | (Data.DirInfo << 2)
1515 | (Data.Resolved << 1)
1516 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001517 Emit8(Out, (uint8_t)Flags);
1518 Emit16(Out, (uint16_t) Data.NumIncludes);
1519
1520 if (!Data.ControllingMacro)
1521 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1522 else
1523 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001524
1525 unsigned Offset = 0;
1526 if (!Data.Framework.empty()) {
1527 // If this header refers into a framework, save the framework name.
1528 llvm::StringMap<unsigned>::iterator Pos
1529 = FrameworkNameOffset.find(Data.Framework);
1530 if (Pos == FrameworkNameOffset.end()) {
1531 Offset = FrameworkStringData.size() + 1;
1532 FrameworkStringData.append(Data.Framework.begin(),
1533 Data.Framework.end());
1534 FrameworkStringData.push_back(0);
1535
1536 FrameworkNameOffset[Data.Framework] = Offset;
1537 } else
1538 Offset = Pos->second;
1539 }
1540 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001541
1542 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001543 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001544 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1545 }
1546
Douglas Gregor09b69892011-02-10 17:09:37 +00001547 assert(Out.tell() - Start == DataLen && "Wrong data length");
1548 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001549
1550 const char *strings_begin() const { return FrameworkStringData.begin(); }
1551 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001552 };
1553} // end anonymous namespace
1554
1555/// \brief Write the header search block for the list of files that
1556///
1557/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001558void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001559 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001560 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1561
1562 if (FilesByUID.size() > HS.header_file_size())
1563 FilesByUID.resize(HS.header_file_size());
1564
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001565 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001566 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001567 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001568 unsigned NumHeaderSearchEntries = 0;
1569 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1570 const FileEntry *File = FilesByUID[UID];
1571 if (!File)
1572 continue;
1573
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001574 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1575 // from the external source if it was not provided already.
1576 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001577 if (HFI.External && Chain)
1578 continue;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001579 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1580 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001581
1582 // Turn the file name into an absolute path, if it isn't already.
1583 const char *Filename = File->getName();
1584 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1585
1586 // If we performed any translation on the file name at all, we need to
1587 // save this string, since the generator will refer to it later.
1588 if (Filename != File->getName()) {
1589 Filename = strdup(Filename);
1590 SavedStrings.push_back(Filename);
1591 }
1592
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001593 HeaderFileInfoTrait::key_type key = { File, Filename };
1594 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001595 ++NumHeaderSearchEntries;
1596 }
1597
1598 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001599 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001600 uint32_t BucketOffset;
1601 {
1602 llvm::raw_svector_ostream Out(TableData);
1603 // Make sure that no bucket is at offset 0
1604 clang::io::Emit32(Out, 0);
1605 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1606 }
1607
1608 // Create a blob abbreviation
1609 using namespace llvm;
1610 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1611 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1616 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1617
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001618 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001619 RecordData Record;
1620 Record.push_back(HEADER_SEARCH_TABLE);
1621 Record.push_back(BucketOffset);
1622 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001623 Record.push_back(TableData.size());
1624 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001625 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1626
1627 // Free all of the strings we had to duplicate.
1628 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001629 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001630}
1631
Douglas Gregora7f71a92009-04-10 03:52:48 +00001632/// \brief Writes the block containing the serialized form of the
1633/// source manager.
1634///
1635/// TODO: We should probably use an on-disk hash table (stored in a
1636/// blob), indexed based on the file name, so that we only create
1637/// entries for files that we actually need. In the common case (no
1638/// errors), we probably won't have to create file entries for any of
1639/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001640void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001641 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001642 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001643 RecordData Record;
1644
Chris Lattner0910e3b2009-04-10 17:16:57 +00001645 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001646 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001647
1648 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001649 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1650 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1651 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001652 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001653
Douglas Gregor258ae542009-04-27 06:38:32 +00001654 // Write out the source location entry table. We skip the first
1655 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001656 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001657 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001658 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1659 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001660 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001661 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001662 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001663 FileID FID = FileID::get(I);
1664 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001665
Douglas Gregor258ae542009-04-27 06:38:32 +00001666 // Record the offset of this source-location entry.
1667 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1668
1669 // Figure out which record code to use.
1670 unsigned Code;
1671 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001672 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1673 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001674 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001675 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001676 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001677 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001678 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001679 Record.clear();
1680 Record.push_back(Code);
1681
Douglas Gregor925296b2011-07-19 16:10:42 +00001682 // Starting offset of this entry within this module, so skip the dummy.
1683 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001684 if (SLoc->isFile()) {
1685 const SrcMgr::FileInfo &File = SLoc->getFile();
1686 Record.push_back(File.getIncludeLoc().getRawEncoding());
1687 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1688 Record.push_back(File.hasLineDirectives());
1689
1690 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001691 if (Content->OrigEntry) {
1692 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001693 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001694
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001695 // The source location entry is a file. Emit input file ID.
1696 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1697 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001698
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001699 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001700
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001701 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001702 if (FDI != FileDeclIDs.end()) {
1703 Record.push_back(FDI->second->FirstDeclIndex);
1704 Record.push_back(FDI->second->DeclIDs.size());
1705 } else {
1706 Record.push_back(0);
1707 Record.push_back(0);
1708 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001709
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001710 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001711
1712 if (Content->BufferOverridden) {
1713 Record.clear();
1714 Record.push_back(SM_SLOC_BUFFER_BLOB);
1715 const llvm::MemoryBuffer *Buffer
1716 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1717 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1718 StringRef(Buffer->getBufferStart(),
1719 Buffer->getBufferSize() + 1));
1720 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001721 } else {
1722 // The source location entry is a buffer. The blob associated
1723 // with this entry contains the contents of the buffer.
1724
1725 // We add one to the size so that we capture the trailing NULL
1726 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1727 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001728 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001729 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001730 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001731 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001732 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001733 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001734 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001735 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001736 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001737 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001738
Douglas Gregor925296b2011-07-19 16:10:42 +00001739 if (strcmp(Name, "<built-in>") == 0) {
1740 PreloadSLocs.push_back(SLocEntryOffsets.size());
1741 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001742 }
1743 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001744 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001745 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001746 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1747 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001748 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1749 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001750
1751 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001752 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001753 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001754 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001755 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001756 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001757 }
1758 }
1759
Douglas Gregor8f45df52009-04-16 22:23:12 +00001760 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001761
1762 if (SLocEntryOffsets.empty())
1763 return;
1764
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001765 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001766 // table is used for lazily loading source-location information.
1767 using namespace llvm;
1768 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001769 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1773 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001774
Douglas Gregor258ae542009-04-27 06:38:32 +00001775 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001776 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001777 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001778 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001779 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001780
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001781 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001782 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001783 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001784
1785 // Write the line table. It depends on remapping working, so it must come
1786 // after the source location offsets.
1787 if (SourceMgr.hasLineTable()) {
1788 LineTableInfo &LineTable = SourceMgr.getLineTable();
1789
1790 Record.clear();
1791 // Emit the file names
1792 Record.push_back(LineTable.getNumFilenames());
1793 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1794 // Emit the file name
1795 const char *Filename = LineTable.getFilename(I);
1796 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1797 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1798 Record.push_back(FilenameLen);
1799 if (FilenameLen)
1800 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1801 }
1802
1803 // Emit the line entries
1804 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1805 L != LEnd; ++L) {
1806 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001807 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001808 continue;
1809
1810 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001811 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001812
1813 // Emit the line entries
1814 Record.push_back(L->second.size());
1815 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1816 LEEnd = L->second.end();
1817 LE != LEEnd; ++LE) {
1818 Record.push_back(LE->FileOffset);
1819 Record.push_back(LE->LineNo);
1820 Record.push_back(LE->FilenameID);
1821 Record.push_back((unsigned)LE->FileKind);
1822 Record.push_back(LE->IncludeOffset);
1823 }
1824 }
1825 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1826 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001827}
1828
Douglas Gregorc5046832009-04-27 18:38:38 +00001829//===----------------------------------------------------------------------===//
1830// Preprocessor Serialization
1831//===----------------------------------------------------------------------===//
1832
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001833namespace {
1834class ASTMacroTableTrait {
1835public:
1836 typedef IdentID key_type;
1837 typedef key_type key_type_ref;
1838
1839 struct Data {
1840 uint32_t MacroDirectivesOffset;
1841 };
1842
1843 typedef Data data_type;
1844 typedef const data_type &data_type_ref;
1845
1846 static unsigned ComputeHash(IdentID IdID) {
1847 return llvm::hash_value(IdID);
1848 }
1849
1850 std::pair<unsigned,unsigned>
1851 static EmitKeyDataLength(raw_ostream& Out,
1852 key_type_ref Key, data_type_ref Data) {
1853 unsigned KeyLen = 4; // IdentID.
1854 unsigned DataLen = 4; // MacroDirectivesOffset.
1855 return std::make_pair(KeyLen, DataLen);
1856 }
1857
1858 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1859 clang::io::Emit32(Out, Key);
1860 }
1861
1862 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1863 unsigned) {
1864 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1865 }
1866};
1867} // end anonymous namespace
1868
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001869static int compareMacroDirectives(
1870 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1871 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1872 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001873}
1874
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001875static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1876 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001877 if (MacroInfo *MI = MD->getMacroInfo())
1878 if (MI->isBuiltinMacro())
1879 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001880
1881 if (IsModule) {
1882 SourceLocation Loc = MD->getLocation();
1883 if (Loc.isInvalid())
1884 return true;
1885 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1886 return true;
1887 }
1888
1889 return false;
1890}
1891
Chris Lattnereeffaef2009-04-10 17:15:23 +00001892/// \brief Writes the block containing the serialized form of the
1893/// preprocessor.
1894///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001895void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001896 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1897 if (PPRec)
1898 WritePreprocessorDetail(*PPRec);
1899
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001900 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001901
Chris Lattner0af3ba12009-04-13 01:29:17 +00001902 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1903 if (PP.getCounterValue() != 0) {
1904 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001905 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001906 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001907 }
1908
1909 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001910 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001911
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001912 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001913 // FIXME: use diagnostics subsystem for localization etc.
1914 if (PP.SawDateOrTime())
1915 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001916
Douglas Gregor796d76a2010-10-20 22:00:55 +00001917
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001918 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001919 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001920
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001921 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001922 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001923 MacroDirectives;
1924 for (Preprocessor::macro_iterator
1925 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1926 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001927 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001928 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001929 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001930
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001931 // Sort the set of macro definitions that need to be serialized by the
1932 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001933 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1934 &compareMacroDirectives);
1935
1936 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1937
1938 // Emit the macro directives as a list and associate the offset with the
1939 // identifier they belong to.
1940 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1941 const IdentifierInfo *Name = MacroDirectives[I].first;
1942 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1943 MacroDirective *MD = MacroDirectives[I].second;
1944
1945 // If the macro or identifier need no updates, don't write the macro history
1946 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001947 // FIXME: Chain the macro history instead of re-writing it.
1948 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001949 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1950 continue;
1951
1952 // Emit the macro directives in reverse source order.
1953 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001954 if (MD->isHidden())
1955 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001956 if (shouldIgnoreMacro(MD, IsModule, PP))
1957 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001958
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001959 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001960 Record.push_back(MD->getKind());
1961 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1962 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1963 Record.push_back(InfoID);
1964 Record.push_back(DefMD->isImported());
1965 Record.push_back(DefMD->isAmbiguous());
1966
1967 } else if (VisibilityMacroDirective *
1968 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1969 Record.push_back(VisMD->isPublic());
1970 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001971 }
1972 if (Record.empty())
1973 continue;
1974
1975 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1976 Record.clear();
1977
1978 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1979
1980 IdentID NameID = getIdentifierRef(Name);
1981 ASTMacroTableTrait::Data data;
1982 data.MacroDirectivesOffset = MacroDirectiveOffset;
1983 Generator.insert(NameID, data);
1984 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001985
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001986 /// \brief Offsets of each of the macros into the bitstream, indexed by
1987 /// the local macro ID
1988 ///
1989 /// For each identifier that is associated with a macro, this map
1990 /// provides the offset into the bitstream where that macro is
1991 /// defined.
1992 std::vector<uint32_t> MacroOffsets;
1993
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001994 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1995 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1996 MacroInfo *MI = MacroInfosToEmit[I].MI;
1997 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001998
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001999 if (ID < FirstMacroID) {
2000 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2001 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002002 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002003
2004 // Record the local offset of this macro.
2005 unsigned Index = ID - FirstMacroID;
2006 if (Index == MacroOffsets.size())
2007 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2008 else {
2009 if (Index > MacroOffsets.size())
2010 MacroOffsets.resize(Index + 1);
2011
2012 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2013 }
2014
2015 AddIdentifierRef(Name, Record);
2016 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2017 AddSourceLocation(MI->getDefinitionLoc(), Record);
2018 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2019 Record.push_back(MI->isUsed());
2020 unsigned Code;
2021 if (MI->isObjectLike()) {
2022 Code = PP_MACRO_OBJECT_LIKE;
2023 } else {
2024 Code = PP_MACRO_FUNCTION_LIKE;
2025
2026 Record.push_back(MI->isC99Varargs());
2027 Record.push_back(MI->isGNUVarargs());
2028 Record.push_back(MI->hasCommaPasting());
2029 Record.push_back(MI->getNumArgs());
2030 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2031 I != E; ++I)
2032 AddIdentifierRef(*I, Record);
2033 }
2034
2035 // If we have a detailed preprocessing record, record the macro definition
2036 // ID that corresponds to this macro.
2037 if (PPRec)
2038 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2039
2040 Stream.EmitRecord(Code, Record);
2041 Record.clear();
2042
2043 // Emit the tokens array.
2044 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2045 // Note that we know that the preprocessor does not have any annotation
2046 // tokens in it because they are created by the parser, and thus can't
2047 // be in a macro definition.
2048 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002049 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002050 Stream.EmitRecord(PP_TOKEN, Record);
2051 Record.clear();
2052 }
2053 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002054 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002055
Douglas Gregor92a96f52011-02-08 21:58:10 +00002056 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002057
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002058 // Create the on-disk hash table in a buffer.
2059 SmallString<4096> MacroTable;
2060 uint32_t BucketOffset;
2061 {
2062 llvm::raw_svector_ostream Out(MacroTable);
2063 // Make sure that no bucket is at offset 0
2064 clang::io::Emit32(Out, 0);
2065 BucketOffset = Generator.Emit(Out);
2066 }
2067
2068 // Write the macro table
2069 using namespace llvm;
2070 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2071 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2074 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2075
2076 Record.push_back(MACRO_TABLE);
2077 Record.push_back(BucketOffset);
2078 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2079 Record.clear();
2080
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002081 // Write the offsets table for macro IDs.
2082 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002083 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002084 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2088
2089 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2090 Record.clear();
2091 Record.push_back(MACRO_OFFSET);
2092 Record.push_back(MacroOffsets.size());
2093 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2094 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2095 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002096}
2097
2098void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002099 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002100 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002101
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002102 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002103
Douglas Gregor92a96f52011-02-08 21:58:10 +00002104 // Enter the preprocessor block.
2105 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002106
Douglas Gregoraae92242010-03-19 21:51:54 +00002107 // If the preprocessor has a preprocessing record, emit it.
2108 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002109 using namespace llvm;
2110
2111 // Set up the abbreviation for
2112 unsigned InclusionAbbrev = 0;
2113 {
2114 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2115 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002120 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2121 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2122 }
2123
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002124 unsigned FirstPreprocessorEntityID
2125 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2126 + NUM_PREDEF_PP_ENTITY_IDS;
2127 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002128 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002129 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2130 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002131 E != EEnd;
2132 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002133 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002134
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002135 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2136 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002137
Douglas Gregor92a96f52011-02-08 21:58:10 +00002138 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002139 // Record this macro definition's ID.
2140 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002141
Douglas Gregor92a96f52011-02-08 21:58:10 +00002142 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002143 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2144 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002145 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002146
Chandler Carrutha88a22182011-07-14 08:20:46 +00002147 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002148 Record.push_back(ME->isBuiltinMacro());
2149 if (ME->isBuiltinMacro())
2150 AddIdentifierRef(ME->getName(), Record);
2151 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002152 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002153 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002154 continue;
2155 }
2156
2157 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2158 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002159 Record.push_back(ID->getFileName().size());
2160 Record.push_back(ID->wasInQuotes());
2161 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002162 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002163 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002164 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002165 // Check that the FileEntry is not null because it was not resolved and
2166 // we create a PCH even with compiler errors.
2167 if (ID->getFile())
2168 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002169 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2170 continue;
2171 }
2172
2173 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2174 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002175 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002176
Douglas Gregoraae92242010-03-19 21:51:54 +00002177 // Write the offsets table for the preprocessing record.
2178 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002179 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2180
Douglas Gregoraae92242010-03-19 21:51:54 +00002181 // Write the offsets table for identifier IDs.
2182 using namespace llvm;
2183 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002184 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002187 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002188
Douglas Gregoraae92242010-03-19 21:51:54 +00002189 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002190 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002191 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002192 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2193 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002194 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002195}
2196
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002197unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2198 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2199 if (Known != SubmoduleIDs.end())
2200 return Known->second;
2201
2202 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2203}
2204
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002205unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2206 if (!Mod)
2207 return 0;
2208
2209 llvm::DenseMap<Module *, unsigned>::const_iterator
2210 Known = SubmoduleIDs.find(Mod);
2211 if (Known != SubmoduleIDs.end())
2212 return Known->second;
2213
2214 return 0;
2215}
2216
Douglas Gregor253eefe2011-12-01 00:59:36 +00002217/// \brief Compute the number of modules within the given tree (including the
2218/// given module).
2219static unsigned getNumberOfModules(Module *Mod) {
2220 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002221 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2222 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002223 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002224 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002225
2226 return ChildModules + 1;
2227}
2228
Douglas Gregorde3ef502011-11-30 23:21:26 +00002229void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002230 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002231 // FIXME: This feels like it belongs somewhere else, but there are no
2232 // other consumers of this information.
2233 SourceManager &SrcMgr = PP->getSourceManager();
2234 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2235 for (ASTContext::import_iterator I = Context->local_import_begin(),
2236 IEnd = Context->local_import_end();
2237 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002238 if (Module *ImportedFrom
2239 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2240 SrcMgr))) {
2241 ImportedFrom->Imports.push_back(I->getImportedModule());
2242 }
2243 }
2244
Douglas Gregor69021972011-11-30 17:33:56 +00002245 // Enter the submodule description block.
2246 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2247
2248 // Write the abbreviations needed for the submodules block.
2249 using namespace llvm;
2250 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2251 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2262 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2263
2264 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002265 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2267 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2268
2269 Abbrev = new BitCodeAbbrev();
2270 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2272 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002273
2274 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002275 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2277 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2278
2279 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002280 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2282 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2283
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002284 Abbrev = new BitCodeAbbrev();
2285 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002288 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2289
Douglas Gregor59527662012-10-15 06:28:11 +00002290 Abbrev = new BitCodeAbbrev();
2291 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2293 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2294
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002295 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002296 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2298 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2299
2300 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002301 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2303 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2304 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2305
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002306 Abbrev = new BitCodeAbbrev();
2307 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2309 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2310
Douglas Gregorfb912652013-03-20 21:10:35 +00002311 Abbrev = new BitCodeAbbrev();
2312 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2315 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2316
Douglas Gregor253eefe2011-12-01 00:59:36 +00002317 // Write the submodule metadata block.
2318 RecordData Record;
2319 Record.push_back(getNumberOfModules(WritingModule));
2320 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2321 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2322
Douglas Gregor69021972011-11-30 17:33:56 +00002323 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002324 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002325 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002326 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002327 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002328 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002329 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002330
2331 // Emit the definition of the block.
2332 Record.clear();
2333 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002334 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002335 if (Mod->Parent) {
2336 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2337 Record.push_back(SubmoduleIDs[Mod->Parent]);
2338 } else {
2339 Record.push_back(0);
2340 }
2341 Record.push_back(Mod->IsFramework);
2342 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002343 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002344 Record.push_back(Mod->InferSubmodules);
2345 Record.push_back(Mod->InferExplicitSubmodules);
2346 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002347 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002348 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2349
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002350 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002351 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002352 Record.clear();
2353 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002354 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002355 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002356 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002357 }
2358
Douglas Gregor69021972011-11-30 17:33:56 +00002359 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002360 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002361 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002362 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002363 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002364 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002365 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2366 Record.clear();
2367 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2368 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2369 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002370 }
2371
2372 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002373 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002374 Record.clear();
2375 Record.push_back(SUBMODULE_HEADER);
2376 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002377 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002378 }
Douglas Gregor59527662012-10-15 06:28:11 +00002379 // Emit the excluded headers.
2380 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2381 Record.clear();
2382 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2383 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2384 Mod->ExcludedHeaders[I]->getName());
2385 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002386 // Emit the private headers.
2387 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2388 Record.clear();
2389 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2390 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2391 Mod->PrivateHeaders[I]->getName());
2392 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002393 ArrayRef<const FileEntry *>
2394 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2395 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002396 Record.clear();
2397 Record.push_back(SUBMODULE_TOPHEADER);
2398 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002399 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002400 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002401
2402 // Emit the imports.
2403 if (!Mod->Imports.empty()) {
2404 Record.clear();
2405 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002406 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002407 assert(ImportedID && "Unknown submodule!");
2408 Record.push_back(ImportedID);
2409 }
2410 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2411 }
2412
Douglas Gregor24bb9232011-12-02 18:58:38 +00002413 // Emit the exports.
2414 if (!Mod->Exports.empty()) {
2415 Record.clear();
2416 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002417 if (Module *Exported = Mod->Exports[I].getPointer()) {
2418 unsigned ExportedID = SubmoduleIDs[Exported];
2419 assert(ExportedID > 0 && "Unknown submodule ID?");
2420 Record.push_back(ExportedID);
2421 } else {
2422 Record.push_back(0);
2423 }
2424
Douglas Gregor24bb9232011-12-02 18:58:38 +00002425 Record.push_back(Mod->Exports[I].getInt());
2426 }
2427 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2428 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002429
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002430 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2431 // Might be unnecessary as use declarations are only used to build the
2432 // module itself.
2433
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002434 // Emit the link libraries.
2435 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2436 Record.clear();
2437 Record.push_back(SUBMODULE_LINK_LIBRARY);
2438 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2439 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2440 Mod->LinkLibraries[I].Library);
2441 }
2442
Douglas Gregorfb912652013-03-20 21:10:35 +00002443 // Emit the conflicts.
2444 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2445 Record.clear();
2446 Record.push_back(SUBMODULE_CONFLICT);
2447 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2448 assert(OtherID && "Unknown submodule!");
2449 Record.push_back(OtherID);
2450 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2451 Mod->Conflicts[I].Message);
2452 }
2453
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002454 // Emit the configuration macros.
2455 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2456 Record.clear();
2457 Record.push_back(SUBMODULE_CONFIG_MACRO);
2458 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2459 Mod->ConfigMacros[I]);
2460 }
2461
Douglas Gregor69021972011-11-30 17:33:56 +00002462 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002463 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2464 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002465 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002466 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002467 }
2468
2469 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002470
2471 assert((NextSubmoduleID - FirstSubmoduleID
2472 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002473}
2474
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002475serialization::SubmoduleID
2476ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002477 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002478 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002479
2480 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002481 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002482 Module *OwningMod
2483 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002484 if (!OwningMod)
2485 return 0;
2486
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002487 // Check whether this submodule is part of our own module.
2488 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002489 return 0;
2490
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002491 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002492}
2493
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002494void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2495 bool isModule) {
2496 // Make sure set diagnostic pragmas don't affect the translation unit that
2497 // imports the module.
2498 // FIXME: Make diagnostic pragma sections work properly with modules.
2499 if (isModule)
2500 return;
2501
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002502 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2503 DiagStateIDMap;
2504 unsigned CurrID = 0;
2505 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002506 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002507 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002508 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2509 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002510 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002511 if (point.Loc.isInvalid())
2512 continue;
2513
2514 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002515 unsigned &DiagStateID = DiagStateIDMap[point.State];
2516 Record.push_back(DiagStateID);
2517
2518 if (DiagStateID == 0) {
2519 DiagStateID = ++CurrID;
2520 for (DiagnosticsEngine::DiagState::const_iterator
2521 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2522 if (I->second.isPragma()) {
2523 Record.push_back(I->first);
2524 Record.push_back(I->second.getMapping());
2525 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002526 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002527 Record.push_back(-1); // mark the end of the diag/map pairs for this
2528 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002529 }
2530 }
2531
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002532 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002533 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002534}
2535
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002536void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2537 if (CXXBaseSpecifiersOffsets.empty())
2538 return;
2539
2540 RecordData Record;
2541
2542 // Create a blob abbreviation for the C++ base specifiers offsets.
2543 using namespace llvm;
2544
2545 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2546 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2548 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2549 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2550
Douglas Gregorc27b2872011-08-04 00:01:48 +00002551 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002552 Record.clear();
2553 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2554 Record.push_back(CXXBaseSpecifiersOffsets.size());
2555 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002556 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002557}
2558
Douglas Gregorc5046832009-04-27 18:38:38 +00002559//===----------------------------------------------------------------------===//
2560// Type Serialization
2561//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002562
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002563/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002564void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002565 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002566 if (Idx.getIndex() == 0) // we haven't seen this type before.
2567 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002568
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002569 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002570
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002571 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002572 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002573 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002574 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002575 else if (TypeOffsets.size() < Index) {
2576 TypeOffsets.resize(Index + 1);
2577 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002578 }
2579
2580 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002581
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002582 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002583 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002584
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002585 if (T.hasLocalNonFastQualifiers()) {
2586 Qualifiers Qs = T.getLocalQualifiers();
2587 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002588 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002589 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002590 } else {
2591 switch (T->getTypeClass()) {
2592 // For all of the concrete, non-dependent types, call the
2593 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002594#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002595 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002596#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002597#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002598 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002599 }
2600
2601 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002602 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002603
2604 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002605 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002606}
2607
Douglas Gregorc5046832009-04-27 18:38:38 +00002608//===----------------------------------------------------------------------===//
2609// Declaration Serialization
2610//===----------------------------------------------------------------------===//
2611
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002612/// \brief Write the block containing all of the declaration IDs
2613/// lexically declared within the given DeclContext.
2614///
2615/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2616/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002617uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002618 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002619 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002620 return 0;
2621
Douglas Gregor8f45df52009-04-16 22:23:12 +00002622 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002623 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002624 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002625 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002626 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2627 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002628 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002629
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002630 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002631 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002632 return Offset;
2633}
2634
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002635void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002636 using namespace llvm;
2637 RecordData Record;
2638
2639 // Write the type offsets array
2640 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002641 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002642 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2645 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2646 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002647 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002648 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002649 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002650 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002651
2652 // Write the declaration offsets array
2653 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002654 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002657 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2658 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2659 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002660 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002661 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002662 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002663 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002664}
2665
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002666void ASTWriter::WriteFileDeclIDsMap() {
2667 using namespace llvm;
2668 RecordData Record;
2669
2670 // Join the vectors of DeclIDs from all files.
2671 SmallVector<DeclID, 256> FileSortedIDs;
2672 for (FileDeclIDsTy::iterator
2673 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2674 DeclIDInFileInfo &Info = *FI->second;
2675 Info.FirstDeclIndex = FileSortedIDs.size();
2676 for (LocDeclIDsTy::iterator
2677 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2678 FileSortedIDs.push_back(DI->second);
2679 }
2680
2681 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2682 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002684 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2685 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2686 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002687 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002688 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2689}
2690
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002691void ASTWriter::WriteComments() {
2692 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002693 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002694 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002695 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2696 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002697 I != E; ++I) {
2698 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002699 AddSourceRange((*I)->getSourceRange(), Record);
2700 Record.push_back((*I)->getKind());
2701 Record.push_back((*I)->isTrailingComment());
2702 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002703 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2704 }
2705 Stream.ExitBlock();
2706}
2707
Douglas Gregorc5046832009-04-27 18:38:38 +00002708//===----------------------------------------------------------------------===//
2709// Global Method Pool and Selector Serialization
2710//===----------------------------------------------------------------------===//
2711
Douglas Gregore84a9da2009-04-20 20:36:09 +00002712namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002713// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002714class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002715 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002716
2717public:
2718 typedef Selector key_type;
2719 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002720
Sebastian Redl834bb972010-08-04 17:20:04 +00002721 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002722 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002723 ObjCMethodList Instance, Factory;
2724 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002725 typedef const data_type& data_type_ref;
2726
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002727 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002728
Douglas Gregorc78d3462009-04-24 21:10:55 +00002729 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002730 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002731 }
Mike Stump11289f42009-09-09 15:08:12 +00002732
2733 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002734 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002735 data_type_ref Methods) {
2736 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2737 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002738 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2739 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 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002743 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002744 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002745 if (Method->Method)
2746 DataLen += 4;
2747 clang::io::Emit16(Out, DataLen);
2748 return std::make_pair(KeyLen, DataLen);
2749 }
Mike Stump11289f42009-09-09 15:08:12 +00002750
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002751 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002752 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002753 assert((Start >> 32) == 0 && "Selector key offset too large");
2754 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002755 unsigned N = Sel.getNumArgs();
2756 clang::io::Emit16(Out, N);
2757 if (N == 0)
2758 N = 1;
2759 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002760 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002761 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2762 }
Mike Stump11289f42009-09-09 15:08:12 +00002763
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002764 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002765 data_type_ref Methods, unsigned DataLen) {
2766 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002767 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002768 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002769 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002770 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002771 if (Method->Method)
2772 ++NumInstanceMethods;
2773
2774 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002775 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002776 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002777 if (Method->Method)
2778 ++NumFactoryMethods;
2779
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002780 unsigned InstanceBits = Methods.Instance.getBits();
2781 assert(InstanceBits < 4);
2782 unsigned NumInstanceMethodsAndBits =
2783 (NumInstanceMethods << 2) | InstanceBits;
2784 unsigned FactoryBits = Methods.Factory.getBits();
2785 assert(FactoryBits < 4);
2786 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2787 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2788 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002789 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002790 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002791 if (Method->Method)
2792 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002793 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002794 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002795 if (Method->Method)
2796 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002797
2798 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002799 }
2800};
2801} // end anonymous namespace
2802
Sebastian Redla19a67f2010-08-03 21:58:15 +00002803/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002804///
2805/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002806/// in an on-disk hash table indexed by the selector. The hash table also
2807/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002808void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002809 using namespace llvm;
2810
Sebastian Redla19a67f2010-08-03 21:58:15 +00002811 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002812 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002813 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002814 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002815 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002816 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002817 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002818 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002819
Sebastian Redla19a67f2010-08-03 21:58:15 +00002820 // Create the on-disk hash table representation. We walk through every
2821 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002822 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002823 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002824 I = SelectorIDs.begin(), E = SelectorIDs.end();
2825 I != E; ++I) {
2826 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002827 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002828 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002829 I->second,
2830 ObjCMethodList(),
2831 ObjCMethodList()
2832 };
2833 if (F != SemaRef.MethodPool.end()) {
2834 Data.Instance = F->second.first;
2835 Data.Factory = F->second.second;
2836 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002837 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002838 // changed.
2839 if (Chain && I->second < FirstSelectorID) {
2840 // Selector already exists. Did it change?
2841 bool changed = false;
2842 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002843 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002844 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002845 changed = true;
2846 }
2847 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002848 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002849 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002850 changed = true;
2851 }
2852 if (!changed)
2853 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002854 } else if (Data.Instance.Method || Data.Factory.Method) {
2855 // A new method pool entry.
2856 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002857 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002858 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002859 }
2860
Douglas Gregorc78d3462009-04-24 21:10:55 +00002861 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002862 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002863 uint32_t BucketOffset;
2864 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002865 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002866 llvm::raw_svector_ostream Out(MethodPool);
2867 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002868 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002869 BucketOffset = Generator.Emit(Out, Trait);
2870 }
2871
2872 // Create a blob abbreviation
2873 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002874 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2878 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2879
Douglas Gregor95c13f52009-04-25 17:48:32 +00002880 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002881 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002882 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002883 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002884 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002885 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002886
2887 // Create a blob abbreviation for the selector table offsets.
2888 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002889 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002892 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2893 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2894
2895 // Write the selector offsets table.
2896 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002897 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002898 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002899 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002900 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002901 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002902 }
2903}
2904
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002905/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002906void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002907 using namespace llvm;
2908 if (SemaRef.ReferencedSelectors.empty())
2909 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002910
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002911 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002912
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002913 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002914 // very tricky to fix, and given that @selector shouldn't really appear in
2915 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002916 for (DenseMap<Selector, SourceLocation>::iterator S =
2917 SemaRef.ReferencedSelectors.begin(),
2918 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2919 Selector Sel = (*S).first;
2920 SourceLocation Loc = (*S).second;
2921 AddSelectorRef(Sel, Record);
2922 AddSourceLocation(Loc, Record);
2923 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002924 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002925}
2926
Douglas Gregorc5046832009-04-27 18:38:38 +00002927//===----------------------------------------------------------------------===//
2928// Identifier Table Serialization
2929//===----------------------------------------------------------------------===//
2930
Douglas Gregorc78d3462009-04-24 21:10:55 +00002931namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002932class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002933 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002934 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002935 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002936 bool IsModule;
2937
Douglas Gregor1d583f22009-04-28 21:18:29 +00002938 /// \brief Determines whether this is an "interesting" identifier
2939 /// that needs a full IdentifierInfo structure written into the hash
2940 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002941 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002942 if (II->isPoisoned() ||
2943 II->isExtensionToken() ||
2944 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002945 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002946 II->getFETokenInfo<void>())
2947 return true;
2948
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002949 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002950 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002951
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002952 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002953 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002954 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002955
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002956 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2957 if (!IsModule)
2958 return !shouldIgnoreMacro(Macro, IsModule, PP);
2959 SubmoduleID ModID;
2960 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2961 return true;
2962 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002963
2964 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002965 }
2966
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002967 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2968 SubmoduleID &ModID) {
2969 ModID = 0;
2970 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2971 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2972 return DefMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002973 return 0;
2974 }
2975
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002976 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2977 SubmoduleID &ModID) {
2978 if (DefMacroDirective *
2979 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2980 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2981 return DefMD;
2982 return 0;
2983 }
2984
2985 /// \brief Traverses the macro directives history and returns the latest
2986 /// macro that is public and not undefined in the same submodule.
2987 /// A macro that is defined in submodule A and undefined in submodule B,
2988 /// will still be considered as defined/exported from submodule A.
2989 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2990 SubmoduleID &ModID) {
2991 if (!MD)
2992 return 0;
2993
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002994 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002995 bool isUndefined = false;
2996 Optional<bool> isPublic;
2997 for (; MD; MD = MD->getPrevious()) {
2998 if (MD->isHidden())
2999 continue;
3000
3001 SubmoduleID ThisModID = getSubmoduleID(MD);
3002 if (ThisModID == 0) {
3003 isUndefined = false;
3004 isPublic = Optional<bool>();
3005 continue;
3006 }
3007 if (ThisModID != ModID){
3008 ModID = ThisModID;
3009 isUndefined = false;
3010 isPublic = Optional<bool>();
3011 }
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003012 // We are looking for a definition in a different submodule than the one
3013 // that we started with. If a submodule has re-definitions of the same
3014 // macro, only the last definition will be used as the "exported" one.
3015 if (ModID == OrigModID)
3016 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003017
3018 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3019 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
3020 return DefMD;
3021 continue;
3022 }
3023
3024 if (isa<UndefMacroDirective>(MD)) {
3025 isUndefined = true;
3026 continue;
3027 }
3028
3029 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
3030 if (!isPublic.hasValue())
3031 isPublic = VisMD->isPublic();
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003032 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003033
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003034 return 0;
3035 }
3036
3037 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003038 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3039 MacroInfo *MI = DefMD->getInfo();
3040 if (unsigned ID = MI->getOwningModuleID())
3041 return ID;
3042 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3043 }
3044 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003045 }
3046
Douglas Gregore84a9da2009-04-20 20:36:09 +00003047public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003048 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003049 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003050
Sebastian Redl539c5062010-08-18 23:57:32 +00003051 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003052 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003053
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003054 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3055 IdentifierResolver &IdResolver, bool IsModule)
3056 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003057
3058 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003059 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003060 }
Mike Stump11289f42009-09-09 15:08:12 +00003061
3062 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003063 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003064 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003065 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003066 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003067 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003068 DataLen += 2; // 2 bytes for builtin ID
3069 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003070 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003071 DataLen += 4; // MacroDirectives offset.
3072 if (IsModule) {
3073 SubmoduleID ModID;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003074 for (DefMacroDirective *
3075 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3076 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003077 DataLen += 4; // MacroInfo ID.
3078 }
3079 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003080 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003081 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003082
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003083 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3084 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003085 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003087 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003088 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003089 // We emit the key length after the data length so that every
3090 // string is preceded by a 16-bit length. This matches the PTH
3091 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003092 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003093 return std::make_pair(KeyLen, DataLen);
3094 }
Mike Stump11289f42009-09-09 15:08:12 +00003095
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003096 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003097 unsigned KeyLen) {
3098 // Record the location of the key data. This is used when generating
3099 // the mapping from persistent IDs to strings.
3100 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003101 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003102 }
Mike Stump11289f42009-09-09 15:08:12 +00003103
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003104 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003105 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003106 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003107 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003108 clang::io::Emit32(Out, ID << 1);
3109 return;
3110 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003111
Douglas Gregor1d583f22009-04-28 21:18:29 +00003112 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003113 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3114 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3115 clang::io::Emit16(Out, Bits);
3116 Bits = 0;
3117 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003118 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003119 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003120 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3121 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003122 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003123 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003124 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003125
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003126 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003127 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3128 if (IsModule) {
3129 // Write the IDs of macros coming from different submodules.
3130 SubmoduleID ModID;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003131 for (DefMacroDirective *
3132 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3133 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3134 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003135 assert(InfoID);
3136 clang::io::Emit32(Out, InfoID);
3137 }
3138 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003139 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003140 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003141
Douglas Gregora868bbd2009-04-21 22:25:48 +00003142 // Emit the declaration IDs in reverse order, because the
3143 // IdentifierResolver provides the declarations as they would be
3144 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003145 // "stat"), but the ASTReader adds declarations to the end of the list
3146 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003147 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003148 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3149 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003150 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003151 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003152 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003153 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3154 }
3155
3156 /// \brief Returns the most recent local decl or the given decl if there are
3157 /// no local ones. The given decl is assumed to be the most recent one.
3158 Decl *getMostRecentLocalDecl(Decl *Orig) {
3159 // The only way a "from AST file" decl would be more recent from a local one
3160 // is if it came from a module.
3161 if (!PP.getLangOpts().Modules)
3162 return Orig;
3163
3164 // Look for a local in the decl chain.
3165 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3166 if (!D->isFromASTFile())
3167 return D;
3168 // If we come up a decl from a (chained-)PCH stop since we won't find a
3169 // local one.
3170 if (D->getOwningModuleID() == 0)
3171 break;
3172 }
3173
3174 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003175 }
3176};
3177} // end anonymous namespace
3178
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003179/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003180///
3181/// The identifier table consists of a blob containing string data
3182/// (the actual identifiers themselves) and a separate "offsets" index
3183/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003184void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3185 IdentifierResolver &IdResolver,
3186 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003187 using namespace llvm;
3188
3189 // Create and write out the blob that contains the identifier
3190 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003191 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003192 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003193 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003194
Douglas Gregore6648fb2009-04-28 20:33:11 +00003195 // Look for any identifiers that were named while processing the
3196 // headers, but are otherwise not needed. We add these to the hash
3197 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003198 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003199 // file.
3200 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3201 IDEnd = PP.getIdentifierTable().end();
3202 ID != IDEnd; ++ID)
3203 getIdentifierRef(ID->second);
3204
Sebastian Redlff4a2952010-07-23 23:49:55 +00003205 // Create the on-disk hash table representation. We only store offsets
3206 // for identifiers that appear here for the first time.
3207 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003208 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003209 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3210 ID != IDEnd; ++ID) {
3211 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003212 if (!Chain || !ID->first->isFromAST() ||
3213 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003214 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003215 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003216 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003217
Douglas Gregore84a9da2009-04-20 20:36:09 +00003218 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003219 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003220 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003221 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003222 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003223 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003224 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003225 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003226 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003227 }
3228
3229 // Create a blob abbreviation
3230 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003231 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003234 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003235
3236 // Write the identifier table
3237 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003238 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003239 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003240 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003241 }
3242
3243 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003244 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003245 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3249 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3250
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003251#ifndef NDEBUG
3252 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3253 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3254#endif
3255
Douglas Gregor0e149972009-04-25 19:10:14 +00003256 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003257 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003258 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003259 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003260 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003261 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003262}
3263
Douglas Gregorc5046832009-04-27 18:38:38 +00003264//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003265// DeclContext's Name Lookup Table Serialization
3266//===----------------------------------------------------------------------===//
3267
3268namespace {
3269// Trait used for the on-disk hash table used in the method pool.
3270class ASTDeclContextNameLookupTrait {
3271 ASTWriter &Writer;
3272
3273public:
3274 typedef DeclarationName key_type;
3275 typedef key_type key_type_ref;
3276
3277 typedef DeclContext::lookup_result data_type;
3278 typedef const data_type& data_type_ref;
3279
3280 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3281
3282 unsigned ComputeHash(DeclarationName Name) {
3283 llvm::FoldingSetNodeID ID;
3284 ID.AddInteger(Name.getNameKind());
3285
3286 switch (Name.getNameKind()) {
3287 case DeclarationName::Identifier:
3288 ID.AddString(Name.getAsIdentifierInfo()->getName());
3289 break;
3290 case DeclarationName::ObjCZeroArgSelector:
3291 case DeclarationName::ObjCOneArgSelector:
3292 case DeclarationName::ObjCMultiArgSelector:
3293 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3294 break;
3295 case DeclarationName::CXXConstructorName:
3296 case DeclarationName::CXXDestructorName:
3297 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003298 break;
3299 case DeclarationName::CXXOperatorName:
3300 ID.AddInteger(Name.getCXXOverloadedOperator());
3301 break;
3302 case DeclarationName::CXXLiteralOperatorName:
3303 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3304 case DeclarationName::CXXUsingDirective:
3305 break;
3306 }
3307
3308 return ID.ComputeHash();
3309 }
3310
3311 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003312 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003313 data_type_ref Lookup) {
3314 unsigned KeyLen = 1;
3315 switch (Name.getNameKind()) {
3316 case DeclarationName::Identifier:
3317 case DeclarationName::ObjCZeroArgSelector:
3318 case DeclarationName::ObjCOneArgSelector:
3319 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003320 case DeclarationName::CXXLiteralOperatorName:
3321 KeyLen += 4;
3322 break;
3323 case DeclarationName::CXXOperatorName:
3324 KeyLen += 1;
3325 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003326 case DeclarationName::CXXConstructorName:
3327 case DeclarationName::CXXDestructorName:
3328 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003329 case DeclarationName::CXXUsingDirective:
3330 break;
3331 }
3332 clang::io::Emit16(Out, KeyLen);
3333
3334 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003335 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003336 clang::io::Emit16(Out, DataLen);
3337
3338 return std::make_pair(KeyLen, DataLen);
3339 }
3340
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003341 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003342 using namespace clang::io;
3343
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003344 Emit8(Out, Name.getNameKind());
3345 switch (Name.getNameKind()) {
3346 case DeclarationName::Identifier:
3347 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003348 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003349 case DeclarationName::ObjCZeroArgSelector:
3350 case DeclarationName::ObjCOneArgSelector:
3351 case DeclarationName::ObjCMultiArgSelector:
3352 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003353 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003354 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003355 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3356 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003357 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003358 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003359 case DeclarationName::CXXLiteralOperatorName:
3360 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003361 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003362 case DeclarationName::CXXConstructorName:
3363 case DeclarationName::CXXDestructorName:
3364 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003365 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003366 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003367 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003368
3369 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003370 }
3371
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003372 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003373 data_type Lookup, unsigned DataLen) {
3374 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003375 clang::io::Emit16(Out, Lookup.size());
3376 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3377 I != E; ++I)
3378 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003379
3380 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3381 }
3382};
3383} // end anonymous namespace
3384
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003385/// \brief Write the block containing all of the declaration IDs
3386/// visible from the given DeclContext.
3387///
3388/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003389/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003390uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3391 DeclContext *DC) {
3392 if (DC->getPrimaryContext() != DC)
3393 return 0;
3394
3395 // Since there is no name lookup into functions or methods, don't bother to
3396 // build a visible-declarations table for these entities.
3397 if (DC->isFunctionOrMethod())
3398 return 0;
3399
3400 // If not in C++, we perform name lookup for the translation unit via the
3401 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003402 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003403 return 0;
3404
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003405 // Serialize the contents of the mapping used for lookup. Note that,
3406 // although we have two very different code paths, the serialized
3407 // representation is the same for both cases: a declaration name,
3408 // followed by a size, followed by references to the visible
3409 // declarations that have that name.
3410 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003411 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003412 if (!Map || Map->empty())
3413 return 0;
3414
3415 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3416 ASTDeclContextNameLookupTrait Trait(*this);
3417
3418 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003419 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003420 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003421 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3422 D != DEnd; ++D) {
3423 DeclarationName Name = D->first;
3424 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003425 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003426 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3427 // Hash all conversion function names to the same name. The actual
3428 // type information in conversion function name is not used in the
3429 // key (since such type information is not stable across different
3430 // modules), so the intended effect is to coalesce all of the conversion
3431 // functions under a single key.
3432 if (!ConversionName)
3433 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003434 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003435 continue;
3436 }
3437
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003438 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003439 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003440 }
3441
Douglas Gregor05ef9312011-08-30 20:49:19 +00003442 // Add the conversion functions
3443 if (!ConversionDecls.empty()) {
3444 Generator.insert(ConversionName,
3445 DeclContext::lookup_result(ConversionDecls.begin(),
3446 ConversionDecls.end()),
3447 Trait);
3448 }
3449
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003450 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003451 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003452 uint32_t BucketOffset;
3453 {
3454 llvm::raw_svector_ostream Out(LookupTable);
3455 // Make sure that no bucket is at offset 0
3456 clang::io::Emit32(Out, 0);
3457 BucketOffset = Generator.Emit(Out, Trait);
3458 }
3459
3460 // Write the lookup table
3461 RecordData Record;
3462 Record.push_back(DECL_CONTEXT_VISIBLE);
3463 Record.push_back(BucketOffset);
3464 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3465 LookupTable.str());
3466
3467 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3468 ++NumVisibleDeclContexts;
3469 return Offset;
3470}
3471
Sebastian Redla4071b42010-08-24 00:50:09 +00003472/// \brief Write an UPDATE_VISIBLE block for the given context.
3473///
3474/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3475/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003476/// (in C++), for namespaces, and for classes with forward-declared unscoped
3477/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003478void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003479 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3480 if (!Map || Map->empty())
3481 return;
3482
3483 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3484 ASTDeclContextNameLookupTrait Trait(*this);
3485
3486 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003487 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3488 D != DEnd; ++D) {
3489 DeclarationName Name = D->first;
3490 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003491 // For any name that appears in this table, the results are complete, i.e.
3492 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003493 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003494 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003495 }
3496
3497 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003498 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003499 uint32_t BucketOffset;
3500 {
3501 llvm::raw_svector_ostream Out(LookupTable);
3502 // Make sure that no bucket is at offset 0
3503 clang::io::Emit32(Out, 0);
3504 BucketOffset = Generator.Emit(Out, Trait);
3505 }
3506
3507 // Write the lookup table
3508 RecordData Record;
3509 Record.push_back(UPDATE_VISIBLE);
3510 Record.push_back(getDeclID(cast<Decl>(DC)));
3511 Record.push_back(BucketOffset);
3512 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3513}
3514
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003515/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3516void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3517 RecordData Record;
3518 Record.push_back(Opts.fp_contract);
3519 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3520}
3521
3522/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3523void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003524 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003525 return;
3526
3527 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3528 RecordData Record;
3529#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3530#include "clang/Basic/OpenCLExtensions.def"
3531 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3532}
3533
Douglas Gregor358cd442012-01-15 16:58:34 +00003534void ASTWriter::WriteRedeclarations() {
3535 RecordData LocalRedeclChains;
3536 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3537
3538 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3539 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003540 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003541
3542 Decl *MostRecent = First->getMostRecentDecl();
3543
3544 // If we only have a single declaration, there is no point in storing
3545 // a redeclaration chain.
3546 if (First == MostRecent)
3547 continue;
3548
3549 unsigned Offset = LocalRedeclChains.size();
3550 unsigned Size = 0;
3551 LocalRedeclChains.push_back(0); // Placeholder for the size.
3552
3553 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003554 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003555 Prev = Prev->getPreviousDecl()) {
3556 if (!Prev->isFromASTFile()) {
3557 AddDeclRef(Prev, LocalRedeclChains);
3558 ++Size;
3559 }
3560 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003561
3562 if (!First->isFromASTFile() && Chain) {
3563 Decl *FirstFromAST = MostRecent;
3564 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3565 if (Prev->isFromASTFile())
3566 FirstFromAST = Prev;
3567 }
3568
3569 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3570 }
3571
Douglas Gregor358cd442012-01-15 16:58:34 +00003572 LocalRedeclChains[Offset] = Size;
3573
3574 // Reverse the set of local redeclarations, so that we store them in
3575 // order (since we found them in reverse order).
3576 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3577
Douglas Gregor6168bd22013-02-18 15:53:43 +00003578 // Add the mapping from the first ID from the AST to the set of local
3579 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003580 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3581 LocalRedeclsMap.push_back(Info);
3582
3583 assert(N == Redeclarations.size() &&
3584 "Deserialized a declaration we shouldn't have");
3585 }
3586
3587 if (LocalRedeclChains.empty())
3588 return;
3589
3590 // Sort the local redeclarations map by the first declaration ID,
3591 // since the reader will be performing binary searches on this information.
3592 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3593
3594 // Emit the local redeclarations map.
3595 using namespace llvm;
3596 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3597 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3599 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3600 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3601
3602 RecordData Record;
3603 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3604 Record.push_back(LocalRedeclsMap.size());
3605 Stream.EmitRecordWithBlob(AbbrevID, Record,
3606 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3607 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3608
3609 // Emit the redeclaration chains.
3610 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3611}
3612
Douglas Gregor404cdde2012-01-27 01:47:08 +00003613void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003614 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003615 RecordData Categories;
3616
3617 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3618 unsigned Size = 0;
3619 unsigned StartIndex = Categories.size();
3620
3621 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3622
3623 // Allocate space for the size.
3624 Categories.push_back(0);
3625
3626 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003627 for (ObjCInterfaceDecl::known_categories_iterator
3628 Cat = Class->known_categories_begin(),
3629 CatEnd = Class->known_categories_end();
3630 Cat != CatEnd; ++Cat, ++Size) {
3631 assert(getDeclID(*Cat) != 0 && "Bogus category");
3632 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003633 }
3634
3635 // Update the size.
3636 Categories[StartIndex] = Size;
3637
3638 // Record this interface -> category map.
3639 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3640 CategoriesMap.push_back(CatInfo);
3641 }
3642
3643 // Sort the categories map by the definition ID, since the reader will be
3644 // performing binary searches on this information.
3645 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3646
3647 // Emit the categories map.
3648 using namespace llvm;
3649 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3650 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3653 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3654
3655 RecordData Record;
3656 Record.push_back(OBJC_CATEGORIES_MAP);
3657 Record.push_back(CategoriesMap.size());
3658 Stream.EmitRecordWithBlob(AbbrevID, Record,
3659 reinterpret_cast<char*>(CategoriesMap.data()),
3660 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3661
3662 // Emit the category lists.
3663 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3664}
3665
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003666void ASTWriter::WriteMergedDecls() {
3667 if (!Chain || Chain->MergedDecls.empty())
3668 return;
3669
3670 RecordData Record;
3671 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3672 IEnd = Chain->MergedDecls.end();
3673 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003674 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003675 : getDeclID(I->first);
3676 assert(CanonID && "Merged declaration not known?");
3677
3678 Record.push_back(CanonID);
3679 Record.push_back(I->second.size());
3680 Record.append(I->second.begin(), I->second.end());
3681 }
3682 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3683}
3684
Richard Smithe40f2ba2013-08-07 21:41:30 +00003685void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3686 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3687
3688 if (LPTMap.empty())
3689 return;
3690
3691 RecordData Record;
3692 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3693 ItEnd = LPTMap.end();
3694 It != ItEnd; ++It) {
3695 LateParsedTemplate *LPT = It->second;
3696 AddDeclRef(It->first, Record);
3697 AddDeclRef(LPT->D, Record);
3698 Record.push_back(LPT->Toks.size());
3699
3700 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3701 TokEnd = LPT->Toks.end();
3702 TokIt != TokEnd; ++TokIt) {
3703 AddToken(*TokIt, Record);
3704 }
3705 }
3706 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3707}
3708
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003709//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003710// General Serialization Routines
3711//===----------------------------------------------------------------------===//
3712
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003713/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003714void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3715 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003716 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003717 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3718 e = Attrs.end(); i != e; ++i){
3719 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003720 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003721 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003722
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003723#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003724
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003725 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003726}
3727
John McCallf413f5e2013-05-03 00:10:13 +00003728void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3729 AddSourceLocation(Tok.getLocation(), Record);
3730 Record.push_back(Tok.getLength());
3731
3732 // FIXME: When reading literal tokens, reconstruct the literal pointer
3733 // if it is needed.
3734 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3735 // FIXME: Should translate token kind to a stable encoding.
3736 Record.push_back(Tok.getKind());
3737 // FIXME: Should translate token flags to a stable encoding.
3738 Record.push_back(Tok.getFlags());
3739}
3740
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003741void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003742 Record.push_back(Str.size());
3743 Record.insert(Record.end(), Str.begin(), Str.end());
3744}
3745
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003746void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3747 RecordDataImpl &Record) {
3748 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003749 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003750 Record.push_back(*Minor + 1);
3751 else
3752 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003753 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003754 Record.push_back(*Subminor + 1);
3755 else
3756 Record.push_back(0);
3757}
3758
Douglas Gregore84a9da2009-04-20 20:36:09 +00003759/// \brief Note that the identifier II occurs at the given offset
3760/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003761void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003762 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003763 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003764 // up earlier in the chain and thus don't need an offset.
3765 if (ID >= FirstIdentID)
3766 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003767}
3768
Douglas Gregor95c13f52009-04-25 17:48:32 +00003769/// \brief Note that the selector Sel occurs at the given offset
3770/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003771void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003772 unsigned ID = SelectorIDs[Sel];
3773 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003774 // Don't record offsets for selectors that are also available in a different
3775 // file.
3776 if (ID < FirstSelectorID)
3777 return;
3778 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003779}
3780
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003781ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003782 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003783 WritingAST(false), DoneWritingDeclsAndTypes(false),
3784 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003785 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003786 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003787 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3788 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003789 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3790 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003791 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003792 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003793 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003794 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003795 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003796 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003797 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3798 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3799 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003800 DeclTypedefAbbrev(0),
3801 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3802 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003803{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003804}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003805
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003806ASTWriter::~ASTWriter() {
3807 for (FileDeclIDsTy::iterator
3808 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3809 delete I->second;
3810}
3811
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003812void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003813 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003814 Module *WritingModule, StringRef isysroot,
3815 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003816 WritingAST = true;
3817
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003818 ASTHasCompilerErrors = hasErrors;
3819
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003820 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003821 Stream.Emit((unsigned)'C', 8);
3822 Stream.Emit((unsigned)'P', 8);
3823 Stream.Emit((unsigned)'C', 8);
3824 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003825
Chris Lattner28fa4e62009-04-26 22:26:21 +00003826 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003827
Douglas Gregoreda8e122011-08-09 15:13:55 +00003828 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003829 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003830 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003831 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003832 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003833 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003834 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003835
3836 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003837}
3838
Douglas Gregora94a1542011-07-27 21:45:57 +00003839template<typename Vector>
3840static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3841 ASTWriter::RecordData &Record) {
3842 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3843 I != E; ++I) {
3844 Writer.AddDeclRef(*I, Record);
3845 }
3846}
3847
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003848void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003849 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003850 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003851 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003852 using namespace llvm;
3853
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003854 bool isModule = WritingModule != 0;
3855
Douglas Gregorcf68c582011-12-01 22:20:10 +00003856 // Make sure that the AST reader knows to finalize itself.
3857 if (Chain)
3858 Chain->finalizeForWriting();
3859
Sebastian Redl143413f2010-07-12 22:02:52 +00003860 ASTContext &Context = SemaRef.Context;
3861 Preprocessor &PP = SemaRef.PP;
3862
Douglas Gregordab42432011-08-12 00:15:20 +00003863 // Set up predefined declaration IDs.
3864 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003865 if (Context.ObjCIdDecl)
3866 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003867 if (Context.ObjCSelDecl)
3868 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003869 if (Context.ObjCClassDecl)
3870 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003871 if (Context.ObjCProtocolClassDecl)
3872 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003873 if (Context.Int128Decl)
3874 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3875 if (Context.UInt128Decl)
3876 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003877 if (Context.ObjCInstanceTypeDecl)
3878 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003879 if (Context.BuiltinVaListDecl)
3880 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3881
Douglas Gregor851443c2011-08-12 01:39:19 +00003882 if (!Chain) {
3883 // Make sure that we emit IdentifierInfos (and any attached
3884 // declarations) for builtins. We don't need to do this when we're
3885 // emitting chained PCH files, because all of the builtins will be
3886 // in the original PCH file.
3887 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003888 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003889 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003890 if (!Context.getLangOpts().NoBuiltin) {
3891 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3892 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003893 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3894 getIdentifierRef(&Table.get(BuiltinNames[I]));
3895 }
3896
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003897 // If there are any out-of-date identifiers, bring them up to date.
3898 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003899 // Find out-of-date identifiers.
3900 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003901 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3902 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003903 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003904 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003905 OutOfDate.push_back(ID->second);
3906 }
3907
3908 // Update the out-of-date identifiers.
3909 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3910 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3911 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003912 }
3913
Chris Lattner0c797362009-09-08 18:19:27 +00003914 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003915 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003916 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003917 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003918 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003919
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003920 // Build a record containing all of the file scoped decls in this file.
3921 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003922 if (!isModule)
3923 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3924 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003925
Douglas Gregor851443c2011-08-12 01:39:19 +00003926 // Build a record containing all of the delegating constructors we still need
3927 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003928 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003929 if (!isModule)
3930 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003931
Douglas Gregor851443c2011-08-12 01:39:19 +00003932 // Write the set of weak, undeclared identifiers. We always write the
3933 // entire table, since later PCH files in a PCH chain are only interested in
3934 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003935 RecordData WeakUndeclaredIdentifiers;
3936 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003937 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003938 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3939 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3940 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3941 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3942 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3943 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3944 }
3945 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003946
Richard Smith78165b52013-01-10 23:43:47 +00003947 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003948 // declarations in this header file. Generally, this record will be
3949 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003950 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003951 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003952 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003953 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003954 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3955 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003956 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003957 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003958 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003959 }
3960
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003961 // Build a record containing all of the ext_vector declarations.
3962 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003963 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003964
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003965 // Build a record containing all of the VTable uses information.
3966 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003967 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003968 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3969 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3970 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3971 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3972 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003973 }
3974
3975 // Build a record containing all of dynamic classes declarations.
3976 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003977 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003978
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003979 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003980 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003981 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003982 I = SemaRef.PendingInstantiations.begin(),
3983 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3984 AddDeclRef(I->first, PendingInstantiations);
3985 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003986 }
3987 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3988 "There are local ones at end of translation unit!");
3989
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003990 // Build a record containing some declaration references.
3991 RecordData SemaDeclRefs;
3992 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3993 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3994 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3995 }
3996
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003997 RecordData CUDASpecialDeclRefs;
3998 if (Context.getcudaConfigureCallDecl()) {
3999 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4000 }
4001
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004002 // Build a record containing all of the known namespaces.
4003 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004004 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004005 I = SemaRef.KnownNamespaces.begin(),
4006 IEnd = SemaRef.KnownNamespaces.end();
4007 I != IEnd; ++I) {
4008 if (!I->second)
4009 AddDeclRef(I->first, KnownNamespaces);
4010 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004011
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004012 // Build a record of all used, undefined objects that require definitions.
4013 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004014
4015 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004016 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004017 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4018 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004019 AddDeclRef(I->first, UndefinedButUsed);
4020 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004021 }
4022
Douglas Gregor112b9072012-10-18 05:31:06 +00004023 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004024 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004025
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004026 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004027 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004028 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004029
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004030 // This is so that older clang versions, before the introduction
4031 // of the control block, can read and reject the newer PCH format.
4032 Record.clear();
4033 Record.push_back(VERSION_MAJOR);
4034 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4035
Douglas Gregor851443c2011-08-12 01:39:19 +00004036 // Create a lexical update block containing all of the declarations in the
4037 // translation unit that do not come from other AST files.
4038 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4039 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4040 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
4041 E = TU->noload_decls_end();
4042 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004043 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00004044 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004045 }
4046
4047 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4048 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4049 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4050 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4051 Record.clear();
4052 Record.push_back(TU_UPDATE_LEXICAL);
4053 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4054 data(NewGlobalDecls));
4055
4056 // And a visible updates block for the translation unit.
4057 Abv = new llvm::BitCodeAbbrev();
4058 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4059 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4060 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4061 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4062 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4063 WriteDeclContextVisibleUpdate(TU);
4064
4065 // If the translation unit has an anonymous namespace, and we don't already
4066 // have an update block for it, write it as an update block.
4067 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4068 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4069 if (Record.empty()) {
4070 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004071 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004072 }
4073 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004074
4075 // Make sure visible decls, added to DeclContexts previously loaded from
4076 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004077 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004078 I = UpdatingVisibleDecls.begin(),
4079 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4080 GetDeclRef(*I);
4081 }
4082
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004083 // Make sure all decls associated with an identifier are registered for
4084 // serialization.
4085 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4086 IDEnd = PP.getIdentifierTable().end();
4087 ID != IDEnd; ++ID) {
4088 const IdentifierInfo *II = ID->second;
4089 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4090 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4091 DEnd = SemaRef.IdResolver.end();
4092 D != DEnd; ++D) {
4093 GetDeclRef(*D);
4094 }
4095 }
4096 }
4097
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00004098 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004099 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004100
Douglas Gregor5204bde2011-08-02 16:26:37 +00004101 // Form the record of special types.
4102 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004103 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004104 AddTypeRef(Context.getFILEType(), SpecialTypes);
4105 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4106 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4107 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4108 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004109 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004110 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004111
Douglas Gregor1970d882009-04-26 03:49:13 +00004112 // Keep writing types and declarations until all types and
4113 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00004114 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004115 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00004116 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4117 E = DeclsToRewrite.end();
4118 I != E; ++I)
4119 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004120 while (!DeclTypesToEmit.empty()) {
4121 DeclOrType DOT = DeclTypesToEmit.front();
4122 DeclTypesToEmit.pop();
4123 if (DOT.isType())
4124 WriteType(DOT.getType());
4125 else
4126 WriteDecl(Context, DOT.getDecl());
4127 }
4128 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004129
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004130 DoneWritingDeclsAndTypes = true;
4131
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004132 WriteFileDeclIDsMap();
4133 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00004134 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004135
4136 if (Chain) {
4137 // Write the mapping information describing our module dependencies and how
4138 // each of those modules were mapped into our own offset/ID space, so that
4139 // the reader can build the appropriate mapping to its own offset/ID space.
4140 // The map consists solely of a blob with the following format:
4141 // *(module-name-len:i16 module-name:len*i8
4142 // source-location-offset:i32
4143 // identifier-id:i32
4144 // preprocessed-entity-id:i32
4145 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004146 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004147 // selector-id:i32
4148 // declaration-id:i32
4149 // c++-base-specifiers-id:i32
4150 // type-id:i32)
4151 //
4152 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4153 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4155 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004156 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004157 {
4158 llvm::raw_svector_ostream Out(Buffer);
4159 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004160 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004161 M != MEnd; ++M) {
4162 StringRef FileName = (*M)->FileName;
4163 io::Emit16(Out, FileName.size());
4164 Out.write(FileName.data(), FileName.size());
4165 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4166 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004167 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004168 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004169 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004170 io::Emit32(Out, (*M)->BaseSelectorID);
4171 io::Emit32(Out, (*M)->BaseDeclID);
4172 io::Emit32(Out, (*M)->BaseTypeIndex);
4173 }
4174 }
4175 Record.clear();
4176 Record.push_back(MODULE_OFFSET_MAP);
4177 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4178 Buffer.data(), Buffer.size());
4179 }
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004180 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004181 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004182 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004183 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004184 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004185 WriteFPPragmaOptions(SemaRef.getFPOptions());
4186 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00004187
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004188 WriteTypeDeclOffsets();
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004189 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004190
Anders Carlsson9bb83e82011-03-06 18:41:18 +00004191 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004192
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004193 // If we're emitting a module, write out the submodule information.
4194 if (WritingModule)
4195 WriteSubmodules(WritingModule);
4196
Douglas Gregor5204bde2011-08-02 16:26:37 +00004197 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4198
Douglas Gregord4df8652009-04-22 22:02:47 +00004199 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00004200 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004201 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00004202
4203 // Write the record containing tentative definitions.
4204 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004205 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004206
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004207 // Write the record containing unused file scoped decls.
4208 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004209 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004210
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004211 // Write the record containing weak undeclared identifiers.
4212 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004213 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004214 WeakUndeclaredIdentifiers);
4215
Richard Smith78165b52013-01-10 23:43:47 +00004216 // Write the record containing locally-scoped extern "C" definitions.
4217 if (!LocallyScopedExternCDecls.empty())
4218 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4219 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004220
4221 // Write the record containing ext_vector type names.
4222 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004223 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004224
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004225 // Write the record containing VTable uses information.
4226 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004227 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004228
4229 // Write the record containing dynamic classes declarations.
4230 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004231 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004232
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004233 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004234 if (!PendingInstantiations.empty())
4235 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004236
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004237 // Write the record containing declaration references of Sema.
4238 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004239 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004240
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004241 // Write the record containing CUDA-specific declaration references.
4242 if (!CUDASpecialDeclRefs.empty())
4243 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004244
4245 // Write the delegating constructors.
4246 if (!DelegatingCtorDecls.empty())
4247 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004248
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004249 // Write the known namespaces.
4250 if (!KnownNamespaces.empty())
4251 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004252
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004253 // Write the undefined internal functions and variables, and inline functions.
4254 if (!UndefinedButUsed.empty())
4255 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004256
Douglas Gregor851443c2011-08-12 01:39:19 +00004257 // Write the visible updates to DeclContexts.
4258 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4259 I = UpdatedDeclContexts.begin(),
4260 E = UpdatedDeclContexts.end();
4261 I != E; ++I)
4262 WriteDeclContextVisibleUpdate(*I);
4263
Douglas Gregor959bb062011-12-03 01:15:29 +00004264 if (!WritingModule) {
4265 // Write the submodules that were imported, if any.
4266 RecordData ImportedModules;
4267 for (ASTContext::import_iterator I = Context.local_import_begin(),
4268 IEnd = Context.local_import_end();
4269 I != IEnd; ++I) {
4270 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4271 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4272 }
4273 if (!ImportedModules.empty()) {
4274 // Sort module IDs.
4275 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4276
4277 // Unique module IDs.
4278 ImportedModules.erase(std::unique(ImportedModules.begin(),
4279 ImportedModules.end()),
4280 ImportedModules.end());
4281
4282 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4283 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004284 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004285
Douglas Gregordab42432011-08-12 00:15:20 +00004286 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00004287 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004288 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004289 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004290 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004291 WriteLateParsedTemplates(SemaRef);
4292
Douglas Gregor08f01292009-04-17 22:13:46 +00004293 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004294 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004295 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004296 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004297 Record.push_back(NumLexicalDeclContexts);
4298 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004299 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004300 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004301}
4302
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004303/// \brief Go through the declaration update blocks and resolve declaration
4304/// pointers into declaration IDs.
4305void ASTWriter::ResolveDeclUpdatesBlocks() {
4306 for (DeclUpdateMap::iterator
4307 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4308 const Decl *D = I->first;
4309 UpdateRecord &URec = I->second;
4310
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004311 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004312 continue; // The decl will be written completely
4313
4314 unsigned Idx = 0, N = URec.size();
4315 while (Idx < N) {
4316 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004317 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4318 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4319 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4320 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4321 ++Idx;
4322 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004323
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004324 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman276dd182013-09-05 00:02:25 +00004325 case UPD_DECL_MARKED_USED:
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004326 ++Idx;
4327 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004328
4329 case UPD_CXX_DEDUCED_RETURN_TYPE:
4330 URec[Idx] = GetOrCreateTypeID(
4331 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4332 ++Idx;
4333 break;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004334 }
4335 }
4336 }
4337}
4338
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004339void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004340 if (DeclUpdates.empty())
4341 return;
4342
4343 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00004344 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004345 for (DeclUpdateMap::iterator
4346 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4347 const Decl *D = I->first;
4348 UpdateRecord &URec = I->second;
4349
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004350 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004351 continue; // The decl will be written completely,no need to store updates.
4352
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004353 uint64_t Offset = Stream.GetCurrentBitNo();
4354 Stream.EmitRecord(DECL_UPDATES, URec);
4355
4356 OffsetsRecord.push_back(GetDeclRef(D));
4357 OffsetsRecord.push_back(Offset);
4358 }
4359 Stream.ExitBlock();
4360 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4361}
4362
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004363void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004364 if (ReplacedDecls.empty())
4365 return;
4366
4367 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004368 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4369 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004370 Record.push_back(I->ID);
4371 Record.push_back(I->Offset);
4372 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004373 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004374 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004375}
4376
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004377void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004378 Record.push_back(Loc.getRawEncoding());
4379}
4380
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004381void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004382 AddSourceLocation(Range.getBegin(), Record);
4383 AddSourceLocation(Range.getEnd(), Record);
4384}
4385
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004386void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004387 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004388 const uint64_t *Words = Value.getRawData();
4389 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004390}
4391
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004392void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004393 Record.push_back(Value.isUnsigned());
4394 AddAPInt(Value, Record);
4395}
4396
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004397void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004398 AddAPInt(Value.bitcastToAPInt(), Record);
4399}
4400
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004401void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004402 Record.push_back(getIdentifierRef(II));
4403}
4404
Sebastian Redl539c5062010-08-18 23:57:32 +00004405IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004406 if (II == 0)
4407 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004408
Sebastian Redl539c5062010-08-18 23:57:32 +00004409 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004410 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004411 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004412 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004413}
4414
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004415MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004416 // Don't emit builtin macros like __LINE__ to the AST file unless they
4417 // have been redefined by the header (in which case they are not
4418 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004419 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004420 return 0;
4421
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004422 MacroID &ID = MacroIDs[MI];
4423 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004424 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004425 MacroInfoToEmitData Info = { Name, MI, ID };
4426 MacroInfosToEmit.push_back(Info);
4427 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004428 return ID;
4429}
4430
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004431MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4432 if (MI == 0 || MI->isBuiltinMacro())
4433 return 0;
4434
4435 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4436 return MacroIDs[MI];
4437}
4438
4439uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4440 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4441 return IdentMacroDirectivesOffsetMap[Name];
4442}
4443
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004444void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004445 Record.push_back(getSelectorRef(SelRef));
4446}
4447
Sebastian Redl539c5062010-08-18 23:57:32 +00004448SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004449 if (Sel.getAsOpaquePtr() == 0) {
4450 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004451 }
4452
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004453 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004454 if (SID == 0 && Chain) {
4455 // This might trigger a ReadSelector callback, which will set the ID for
4456 // this selector.
4457 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004458 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004459 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004460 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004461 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004462 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004463 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004464 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004465}
4466
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004467void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004468 AddDeclRef(Temp->getDestructor(), Record);
4469}
4470
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004471void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4472 CXXBaseSpecifier const *BasesEnd,
4473 RecordDataImpl &Record) {
4474 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4475 CXXBaseSpecifiersToWrite.push_back(
4476 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4477 Bases, BasesEnd));
4478 Record.push_back(NextCXXBaseSpecifiersID++);
4479}
4480
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004481void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004482 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004483 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004484 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004485 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004486 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004487 break;
4488 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004489 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004490 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004491 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004492 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004493 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004494 break;
4495 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004496 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004497 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004498 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004499 break;
John McCall0ad16662009-10-29 08:12:44 +00004500 case TemplateArgument::Null:
4501 case TemplateArgument::Integral:
4502 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004503 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004504 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004505 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004506 break;
4507 }
4508}
4509
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004510void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004511 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004512 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004513
4514 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4515 bool InfoHasSameExpr
4516 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4517 Record.push_back(InfoHasSameExpr);
4518 if (InfoHasSameExpr)
4519 return; // Avoid storing the same expr twice.
4520 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004521 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4522 Record);
4523}
4524
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004525void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4526 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004527 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004528 AddTypeRef(QualType(), Record);
4529 return;
4530 }
4531
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004532 AddTypeLoc(TInfo->getTypeLoc(), Record);
4533}
4534
4535void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4536 AddTypeRef(TL.getType(), Record);
4537
John McCall8f115c62009-10-16 21:56:05 +00004538 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004539 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004540 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004541}
4542
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004543void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004544 Record.push_back(GetOrCreateTypeID(T));
4545}
4546
Douglas Gregoreda8e122011-08-09 15:13:55 +00004547TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004548 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004549 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004550 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4551}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004552
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004553TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004554 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004555 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004556 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004557}
4558
4559TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4560 if (T.isNull())
4561 return TypeIdx();
4562 assert(!T.getLocalFastQualifiers());
4563
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004564 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004565 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004566 if (DoneWritingDeclsAndTypes) {
4567 assert(0 && "New type seen after serializing all the types to emit!");
4568 return TypeIdx();
4569 }
4570
Douglas Gregor1970d882009-04-26 03:49:13 +00004571 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004572 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004573 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004574 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004575 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004576 return Idx;
4577}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004578
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004579TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004580 if (T.isNull())
4581 return TypeIdx();
4582 assert(!T.getLocalFastQualifiers());
4583
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004584 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4585 assert(I != TypeIdxs.end() && "Type not emitted!");
4586 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004587}
4588
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004589void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004590 Record.push_back(GetDeclRef(D));
4591}
4592
Sebastian Redl539c5062010-08-18 23:57:32 +00004593DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004594 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4595
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004596 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004597 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004598 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004599
4600 // If D comes from an AST file, its declaration ID is already known and
4601 // fixed.
4602 if (D->isFromASTFile())
4603 return D->getGlobalID();
4604
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004605 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004606 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004607 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004608 if (DoneWritingDeclsAndTypes) {
4609 assert(0 && "New decl seen after serializing all the decls to emit!");
4610 return 0;
4611 }
4612
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004613 // We haven't seen this declaration before. Give it a new ID and
4614 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004615 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004616 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004617 }
4618
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004619 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004620}
4621
Sebastian Redl539c5062010-08-18 23:57:32 +00004622DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004623 if (D == 0)
4624 return 0;
4625
Douglas Gregorb3163e52012-01-05 22:33:30 +00004626 // If D comes from an AST file, its declaration ID is already known and
4627 // fixed.
4628 if (D->isFromASTFile())
4629 return D->getGlobalID();
4630
Douglas Gregore84a9da2009-04-20 20:36:09 +00004631 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4632 return DeclIDs[D];
4633}
4634
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004635void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004636 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004637 assert(D);
4638
4639 SourceLocation Loc = D->getLocation();
4640 if (Loc.isInvalid())
4641 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004642
4643 // We only keep track of the file-level declarations of each file.
4644 if (!D->getLexicalDeclContext()->isFileContext())
4645 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004646 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4647 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004648 if (isa<ParmVarDecl>(D))
4649 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004650
4651 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004652 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004653 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004654 FileID FID;
4655 unsigned Offset;
4656 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004657 if (FID.isInvalid())
4658 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004659 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004660
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004661 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004662 if (!Info)
4663 Info = new DeclIDInFileInfo();
4664
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004665 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004666 LocDeclIDsTy &Decls = Info->DeclIDs;
4667
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004668 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004669 Decls.push_back(LocDecl);
4670 return;
4671 }
4672
Benjamin Kramer45025c02013-08-24 13:22:59 +00004673 LocDeclIDsTy::iterator I =
4674 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004675
4676 Decls.insert(I, LocDecl);
4677}
4678
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004679void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004680 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004681 Record.push_back(Name.getNameKind());
4682 switch (Name.getNameKind()) {
4683 case DeclarationName::Identifier:
4684 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4685 break;
4686
4687 case DeclarationName::ObjCZeroArgSelector:
4688 case DeclarationName::ObjCOneArgSelector:
4689 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004690 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004691 break;
4692
4693 case DeclarationName::CXXConstructorName:
4694 case DeclarationName::CXXDestructorName:
4695 case DeclarationName::CXXConversionFunctionName:
4696 AddTypeRef(Name.getCXXNameType(), Record);
4697 break;
4698
4699 case DeclarationName::CXXOperatorName:
4700 Record.push_back(Name.getCXXOverloadedOperator());
4701 break;
4702
Alexis Hunt3d221f22009-11-29 07:34:05 +00004703 case DeclarationName::CXXLiteralOperatorName:
4704 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4705 break;
4706
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004707 case DeclarationName::CXXUsingDirective:
4708 // No extra data to emit
4709 break;
4710 }
4711}
Chris Lattnerca025db2010-05-07 21:43:38 +00004712
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004713void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004714 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004715 switch (Name.getNameKind()) {
4716 case DeclarationName::CXXConstructorName:
4717 case DeclarationName::CXXDestructorName:
4718 case DeclarationName::CXXConversionFunctionName:
4719 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4720 break;
4721
4722 case DeclarationName::CXXOperatorName:
4723 AddSourceLocation(
4724 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4725 Record);
4726 AddSourceLocation(
4727 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4728 Record);
4729 break;
4730
4731 case DeclarationName::CXXLiteralOperatorName:
4732 AddSourceLocation(
4733 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4734 Record);
4735 break;
4736
4737 case DeclarationName::Identifier:
4738 case DeclarationName::ObjCZeroArgSelector:
4739 case DeclarationName::ObjCOneArgSelector:
4740 case DeclarationName::ObjCMultiArgSelector:
4741 case DeclarationName::CXXUsingDirective:
4742 break;
4743 }
4744}
4745
4746void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004747 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004748 AddDeclarationName(NameInfo.getName(), Record);
4749 AddSourceLocation(NameInfo.getLoc(), Record);
4750 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4751}
4752
4753void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004754 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004755 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004756 Record.push_back(Info.NumTemplParamLists);
4757 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4758 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4759}
4760
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004761void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004762 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004763 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004764 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004765 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004766
4767 // Push each of the NNS's onto a stack for serialization in reverse order.
4768 while (NNS) {
4769 NestedNames.push_back(NNS);
4770 NNS = NNS->getPrefix();
4771 }
4772
4773 Record.push_back(NestedNames.size());
4774 while(!NestedNames.empty()) {
4775 NNS = NestedNames.pop_back_val();
4776 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4777 Record.push_back(Kind);
4778 switch (Kind) {
4779 case NestedNameSpecifier::Identifier:
4780 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4781 break;
4782
4783 case NestedNameSpecifier::Namespace:
4784 AddDeclRef(NNS->getAsNamespace(), Record);
4785 break;
4786
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004787 case NestedNameSpecifier::NamespaceAlias:
4788 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4789 break;
4790
Chris Lattnerca025db2010-05-07 21:43:38 +00004791 case NestedNameSpecifier::TypeSpec:
4792 case NestedNameSpecifier::TypeSpecWithTemplate:
4793 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4794 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4795 break;
4796
4797 case NestedNameSpecifier::Global:
4798 // Don't need to write an associated value.
4799 break;
4800 }
4801 }
4802}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004803
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004804void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4805 RecordDataImpl &Record) {
4806 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004807 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004808 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004809
4810 // Push each of the nested-name-specifiers's onto a stack for
4811 // serialization in reverse order.
4812 while (NNS) {
4813 NestedNames.push_back(NNS);
4814 NNS = NNS.getPrefix();
4815 }
4816
4817 Record.push_back(NestedNames.size());
4818 while(!NestedNames.empty()) {
4819 NNS = NestedNames.pop_back_val();
4820 NestedNameSpecifier::SpecifierKind Kind
4821 = NNS.getNestedNameSpecifier()->getKind();
4822 Record.push_back(Kind);
4823 switch (Kind) {
4824 case NestedNameSpecifier::Identifier:
4825 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4826 AddSourceRange(NNS.getLocalSourceRange(), Record);
4827 break;
4828
4829 case NestedNameSpecifier::Namespace:
4830 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4831 AddSourceRange(NNS.getLocalSourceRange(), Record);
4832 break;
4833
4834 case NestedNameSpecifier::NamespaceAlias:
4835 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4836 AddSourceRange(NNS.getLocalSourceRange(), Record);
4837 break;
4838
4839 case NestedNameSpecifier::TypeSpec:
4840 case NestedNameSpecifier::TypeSpecWithTemplate:
4841 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4842 AddTypeLoc(NNS.getTypeLoc(), Record);
4843 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4844 break;
4845
4846 case NestedNameSpecifier::Global:
4847 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4848 break;
4849 }
4850 }
4851}
4852
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004853void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004854 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004855 Record.push_back(Kind);
4856 switch (Kind) {
4857 case TemplateName::Template:
4858 AddDeclRef(Name.getAsTemplateDecl(), Record);
4859 break;
4860
4861 case TemplateName::OverloadedTemplate: {
4862 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4863 Record.push_back(OvT->size());
4864 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4865 I != E; ++I)
4866 AddDeclRef(*I, Record);
4867 break;
4868 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004869
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004870 case TemplateName::QualifiedTemplate: {
4871 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4872 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4873 Record.push_back(QualT->hasTemplateKeyword());
4874 AddDeclRef(QualT->getTemplateDecl(), Record);
4875 break;
4876 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004877
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004878 case TemplateName::DependentTemplate: {
4879 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4880 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4881 Record.push_back(DepT->isIdentifier());
4882 if (DepT->isIdentifier())
4883 AddIdentifierRef(DepT->getIdentifier(), Record);
4884 else
4885 Record.push_back(DepT->getOperator());
4886 break;
4887 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004888
4889 case TemplateName::SubstTemplateTemplateParm: {
4890 SubstTemplateTemplateParmStorage *subst
4891 = Name.getAsSubstTemplateTemplateParm();
4892 AddDeclRef(subst->getParameter(), Record);
4893 AddTemplateName(subst->getReplacement(), Record);
4894 break;
4895 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004896
4897 case TemplateName::SubstTemplateTemplateParmPack: {
4898 SubstTemplateTemplateParmPackStorage *SubstPack
4899 = Name.getAsSubstTemplateTemplateParmPack();
4900 AddDeclRef(SubstPack->getParameterPack(), Record);
4901 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4902 break;
4903 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004904 }
4905}
4906
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004907void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004908 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004909 Record.push_back(Arg.getKind());
4910 switch (Arg.getKind()) {
4911 case TemplateArgument::Null:
4912 break;
4913 case TemplateArgument::Type:
4914 AddTypeRef(Arg.getAsType(), Record);
4915 break;
4916 case TemplateArgument::Declaration:
4917 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004918 Record.push_back(Arg.isDeclForReferenceParam());
4919 break;
4920 case TemplateArgument::NullPtr:
4921 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004922 break;
4923 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004924 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004925 AddTypeRef(Arg.getIntegralType(), Record);
4926 break;
4927 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004928 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4929 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004930 case TemplateArgument::TemplateExpansion:
4931 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004932 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004933 Record.push_back(*NumExpansions + 1);
4934 else
4935 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004936 break;
4937 case TemplateArgument::Expression:
4938 AddStmt(Arg.getAsExpr());
4939 break;
4940 case TemplateArgument::Pack:
4941 Record.push_back(Arg.pack_size());
4942 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4943 I != E; ++I)
4944 AddTemplateArgument(*I, Record);
4945 break;
4946 }
4947}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004948
4949void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004950ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004951 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004952 assert(TemplateParams && "No TemplateParams!");
4953 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4954 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4955 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4956 Record.push_back(TemplateParams->size());
4957 for (TemplateParameterList::const_iterator
4958 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4959 P != PEnd; ++P)
4960 AddDeclRef(*P, Record);
4961}
4962
4963/// \brief Emit a template argument list.
4964void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004965ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004966 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004967 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004968 Record.push_back(TemplateArgs->size());
4969 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004970 AddTemplateArgument(TemplateArgs->get(i), Record);
4971}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004972
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00004973void
4974ASTWriter::AddASTTemplateArgumentListInfo
4975(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4976 assert(ASTTemplArgList && "No ASTTemplArgList!");
4977 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4978 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4979 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4980 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4981 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4982 AddTemplateArgumentLoc(TemplArgs[i], Record);
4983}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004984
4985void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004986ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004987 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004988 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004989 I = Set.begin(), E = Set.end(); I != E; ++I) {
4990 AddDeclRef(I.getDecl(), Record);
4991 Record.push_back(I.getAccess());
4992 }
4993}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004994
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004995void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004996 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004997 Record.push_back(Base.isVirtual());
4998 Record.push_back(Base.isBaseOfClass());
4999 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005000 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005001 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005002 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005003 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5004 : SourceLocation(),
5005 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005006}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005007
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005008void ASTWriter::FlushCXXBaseSpecifiers() {
5009 RecordData Record;
5010 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5011 Record.clear();
5012
5013 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005014 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005015 if (Index == CXXBaseSpecifiersOffsets.size())
5016 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5017 else {
5018 if (Index > CXXBaseSpecifiersOffsets.size())
5019 CXXBaseSpecifiersOffsets.resize(Index + 1);
5020 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5021 }
5022
5023 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5024 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5025 Record.push_back(BEnd - B);
5026 for (; B != BEnd; ++B)
5027 AddCXXBaseSpecifier(*B, Record);
5028 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005029
5030 // Flush any expressions that were written as part of the base specifiers.
5031 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005032 }
5033
5034 CXXBaseSpecifiersToWrite.clear();
5035}
5036
Alexis Hunt1d792652011-01-08 20:30:50 +00005037void ASTWriter::AddCXXCtorInitializers(
5038 const CXXCtorInitializer * const *CtorInitializers,
5039 unsigned NumCtorInitializers,
5040 RecordDataImpl &Record) {
5041 Record.push_back(NumCtorInitializers);
5042 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5043 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005044
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005045 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005046 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005047 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005048 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005049 } else if (Init->isDelegatingInitializer()) {
5050 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005051 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005052 } else if (Init->isMemberInitializer()){
5053 Record.push_back(CTOR_INITIALIZER_MEMBER);
5054 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005055 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005056 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5057 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005058 }
Francois Pichetd583da02010-12-04 09:14:42 +00005059
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005060 AddSourceLocation(Init->getMemberLocation(), Record);
5061 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005062 AddSourceLocation(Init->getLParenLoc(), Record);
5063 AddSourceLocation(Init->getRParenLoc(), Record);
5064 Record.push_back(Init->isWritten());
5065 if (Init->isWritten()) {
5066 Record.push_back(Init->getSourceOrder());
5067 } else {
5068 Record.push_back(Init->getNumArrayIndices());
5069 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5070 AddDeclRef(Init->getArrayIndex(i), Record);
5071 }
5072 }
5073}
5074
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005075void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5076 assert(D->DefinitionData);
5077 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005078 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005079 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005080 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005081 Record.push_back(Data.Aggregate);
5082 Record.push_back(Data.PlainOldData);
5083 Record.push_back(Data.Empty);
5084 Record.push_back(Data.Polymorphic);
5085 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005086 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005087 Record.push_back(Data.HasNoNonEmptyBases);
5088 Record.push_back(Data.HasPrivateFields);
5089 Record.push_back(Data.HasProtectedFields);
5090 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005091 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005092 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005093 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005094 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005095 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005096 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5097 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5098 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5099 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5100 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5101 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005102 Record.push_back(Data.HasTrivialSpecialMembers);
5103 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005104 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005105 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005106 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005107 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005108 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005109 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005110 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005111 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5112 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5113 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5114 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005115 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005116
5117 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005118 if (Data.NumBases > 0)
5119 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5120 Record);
5121
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005122 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5123 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005124 if (Data.NumVBases > 0)
5125 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5126 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005127
Richard Smitha4ba74c2013-08-30 04:46:40 +00005128 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5129 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005130 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005131 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005132
5133 // Add lambda-specific data.
5134 if (Data.IsLambda) {
5135 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005136 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005137 Record.push_back(Lambda.IsGenericLambda);
5138 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005139 Record.push_back(Lambda.NumCaptures);
5140 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005141 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005142 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005143 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005144 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5145 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5146 AddSourceLocation(Capture.getLocation(), Record);
5147 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005148 Record.push_back(Capture.getCaptureKind());
5149 switch (Capture.getCaptureKind()) {
5150 case LCK_This:
5151 break;
5152 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005153 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005154 VarDecl *Var =
5155 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5156 AddDeclRef(Var, Record);
5157 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5158 : SourceLocation(),
5159 Record);
5160 break;
5161 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005162 }
5163 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005164}
5165
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005166void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005167 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005168 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005169 assert(FirstDeclID == NextDeclID &&
5170 FirstTypeID == NextTypeID &&
5171 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005172 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005173 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005174 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005175 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005176
Sebastian Redl07a89a82010-07-30 00:29:29 +00005177 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005178
Douglas Gregordf0c1512011-08-18 04:12:04 +00005179 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5180 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5181 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005182 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005183 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005184 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005185 NextDeclID = FirstDeclID;
5186 NextTypeID = FirstTypeID;
5187 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005188 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005189 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005190 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005191}
5192
Sebastian Redl539c5062010-08-18 23:57:32 +00005193void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005194 // Always keep the highest ID. See \p TypeRead() for more information.
5195 IdentID &StoredID = IdentifierIDs[II];
5196 if (ID > StoredID)
5197 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005198}
5199
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005200void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005201 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005202 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005203 if (ID > StoredID)
5204 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005205}
5206
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005207void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005208 // Always take the highest-numbered type index. This copes with an interesting
5209 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005210 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005211 // keep the higher-numbered entry so that we can properly write it out to
5212 // the AST file.
5213 TypeIdx &StoredIdx = TypeIdxs[T];
5214 if (Idx.getIndex() >= StoredIdx.getIndex())
5215 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005216}
5217
Sebastian Redl539c5062010-08-18 23:57:32 +00005218void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005219 // Always keep the highest ID. See \p TypeRead() for more information.
5220 SelectorID &StoredID = SelectorIDs[S];
5221 if (ID > StoredID)
5222 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005223}
Douglas Gregor91096292010-10-02 19:29:26 +00005224
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005225void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005226 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005227 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005228 MacroDefinitions[MD] = ID;
5229}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005230
Douglas Gregore37a85a2011-12-02 17:30:13 +00005231void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5232 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5233 SubmoduleIDs[Mod] = ID;
5234}
5235
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005236void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005237 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005238 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005239 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5240 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005241 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005242 // A forward reference was mutated into a definition. Rewrite it.
5243 // FIXME: This happens during template instantiation, should we
5244 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005245 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005246 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005247 }
5248}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005249
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005250void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005251 assert(!WritingAST && "Already writing the AST!");
5252
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005253 // TU and namespaces are handled elsewhere.
5254 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5255 return;
5256
Douglas Gregorb3722e22011-09-09 23:01:35 +00005257 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005258 return; // Not a source decl added to a DeclContext from PCH.
5259
Douglas Gregor9f782892013-01-21 15:25:38 +00005260 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005261 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005262 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005263}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005264
5265void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005266 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005267 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005268 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005269 return; // Not a source member added to a class from PCH.
5270 if (!isa<CXXMethodDecl>(D))
5271 return; // We are interested in lazily declared implicit methods.
5272
5273 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005274 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005275 UpdateRecord &Record = DeclUpdates[RD];
5276 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005277 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005278}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005279
5280void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5281 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005282 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005283 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005284 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005285 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005286 return; // Not a source specialization added to a template from PCH.
5287
5288 UpdateRecord &Record = DeclUpdates[TD];
5289 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005290 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005291}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005292
Larisse Voufo39a1e502013-08-06 01:03:05 +00005293void ASTWriter::AddedCXXTemplateSpecialization(
5294 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5295 // The specializations set is kept in the canonical template.
5296 assert(!WritingAST && "Already writing the AST!");
5297 TD = TD->getCanonicalDecl();
5298 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5299 return; // Not a source specialization added to a template from PCH.
5300
5301 UpdateRecord &Record = DeclUpdates[TD];
5302 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5303 Record.push_back(reinterpret_cast<uint64_t>(D));
5304}
5305
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005306void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5307 const FunctionDecl *D) {
5308 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005309 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005310 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005311 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005312 return; // Not a source specialization added to a template from PCH.
5313
5314 UpdateRecord &Record = DeclUpdates[TD];
5315 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005316 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005317}
5318
Richard Smith1fa5d642013-05-11 05:45:24 +00005319void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5320 assert(!WritingAST && "Already writing the AST!");
5321 FD = FD->getCanonicalDecl();
5322 if (!FD->isFromASTFile())
5323 return; // Not a function declared in PCH and defined outside.
5324
5325 UpdateRecord &Record = DeclUpdates[FD];
5326 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5327 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5328}
5329
Sebastian Redlab238a72011-04-24 16:28:06 +00005330void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005331 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005332 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005333 return; // Declaration not imported from PCH.
5334
5335 // Implicit decl from a PCH was defined.
5336 // FIXME: Should implicit definition be a separate FunctionDecl?
5337 RewriteDecl(D);
5338}
5339
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005340void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005341 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005342 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005343 return;
5344
5345 // Since the actual instantiation is delayed, this really means that we need
5346 // to update the instantiation location.
5347 UpdateRecord &Record = DeclUpdates[D];
5348 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5349 AddSourceLocation(
5350 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5351}
5352
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005353void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5354 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005355 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005356 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005357 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005358
5359 assert(IFD->getDefinition() && "Category on a class without a definition?");
5360 ObjCClassesWithCategories.insert(
5361 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005362}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005363
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005364
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005365void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5366 const ObjCPropertyDecl *OrigProp,
5367 const ObjCCategoryDecl *ClassExt) {
5368 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5369 if (!D)
5370 return;
5371
5372 assert(!WritingAST && "Already writing the AST!");
5373 if (!D->isFromASTFile())
5374 return; // Declaration not imported from PCH.
5375
5376 RewriteDecl(D);
5377}
Eli Friedman276dd182013-09-05 00:02:25 +00005378
5379void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5380 assert(!WritingAST && "Already writing the AST!");
5381 if (!D->isFromASTFile())
5382 return;
5383
5384 UpdateRecord &Record = DeclUpdates[D];
5385 Record.push_back(UPD_DECL_MARKED_USED);
5386}