blob: 8c2eeca0215e38c26d6e69afab1fcda588ccf1c4 [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);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000816 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 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);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001164 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001165 Record.push_back(HSOpts.DisableModuleHash);
1166 Record.push_back(HSOpts.UseBuiltinIncludes);
1167 Record.push_back(HSOpts.UseStandardSystemIncludes);
1168 Record.push_back(HSOpts.UseStandardCXXIncludes);
1169 Record.push_back(HSOpts.UseLibcxx);
1170 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1171
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001172 // Preprocessor options.
1173 Record.clear();
1174 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1175
1176 // Macro definitions.
1177 Record.push_back(PPOpts.Macros.size());
1178 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1179 AddString(PPOpts.Macros[I].first, Record);
1180 Record.push_back(PPOpts.Macros[I].second);
1181 }
1182
1183 // Includes
1184 Record.push_back(PPOpts.Includes.size());
1185 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1186 AddString(PPOpts.Includes[I], Record);
1187
1188 // Macro includes
1189 Record.push_back(PPOpts.MacroIncludes.size());
1190 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1191 AddString(PPOpts.MacroIncludes[I], Record);
1192
Douglas Gregorb6368752012-10-24 23:41:50 +00001193 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001194 // Detailed record is important since it is used for the module cache hash.
1195 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001196 AddString(PPOpts.ImplicitPCHInclude, Record);
1197 AddString(PPOpts.ImplicitPTHInclude, Record);
1198 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1199 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1200
Douglas Gregora3b20262011-05-06 21:43:30 +00001201 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001202 SourceManager &SM = Context.getSourceManager();
1203 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1204 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001205 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1206 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001207 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1208 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1209
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001210 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001211
Michael J. Spencer740857f2010-12-21 16:45:57 +00001212 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001213
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001214 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001215 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001216 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001217 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001218 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001219 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001220 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001221 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001222
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001223 Record.clear();
1224 Record.push_back(SM.getMainFileID().getOpaqueValue());
1225 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1226
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001227 // Original PCH directory
1228 if (!OutputFile.empty() && OutputFile != "-") {
1229 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1230 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1232 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1233
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001234 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001235
1236 llvm::sys::fs::make_absolute(OutputPath);
1237 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1238
1239 RecordData Record;
1240 Record.push_back(ORIGINAL_PCH_DIR);
1241 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1242 }
1243
Douglas Gregor49491f72013-03-15 22:15:07 +00001244 WriteInputFiles(Context.SourceMgr,
1245 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001246 isysroot,
1247 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001248 Stream.ExitBlock();
1249}
1250
Douglas Gregor49491f72013-03-15 22:15:07 +00001251namespace {
1252 /// \brief An input file.
1253 struct InputFileEntry {
1254 const FileEntry *File;
1255 bool IsSystemFile;
1256 bool BufferOverridden;
1257 };
1258}
1259
1260void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1261 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001262 StringRef isysroot,
1263 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001264 using namespace llvm;
1265 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1266 RecordData Record;
1267
1268 // Create input-file abbreviation.
1269 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1270 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001271 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001272 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1273 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001274 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001275 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1276 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1277
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001278 // Get all ContentCache objects for files, sorted by whether the file is a
1279 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001280 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001281 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1282 // Get this source location entry.
1283 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001284 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001285
1286 // We only care about file entries that were not overridden.
1287 if (!SLoc->isFile())
1288 continue;
1289 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001290 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001291 continue;
1292
Douglas Gregor49491f72013-03-15 22:15:07 +00001293 InputFileEntry Entry;
1294 Entry.File = Cache->OrigEntry;
1295 Entry.IsSystemFile = Cache->IsSystemFile;
1296 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001297 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001298 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001299 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001300 SortedFiles.push_front(Entry);
1301 }
1302
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001303 unsigned UserFilesNum = 0;
1304 // Write out all of the input files.
1305 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001306 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001307 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001308 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001309
Douglas Gregor49491f72013-03-15 22:15:07 +00001310 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001311 if (InputFileID != 0)
1312 continue; // already recorded this file.
1313
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001314 // Record this entry's offset.
1315 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001316
1317 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001318
Douglas Gregor49491f72013-03-15 22:15:07 +00001319 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001320 ++UserFilesNum;
1321
Douglas Gregor72be3902012-10-19 00:38:02 +00001322 Record.clear();
1323 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001324 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001325
1326 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001327 Record.push_back(Entry.File->getSize());
1328 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001329
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001330 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001331 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001332
Douglas Gregor72be3902012-10-19 00:38:02 +00001333 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001334 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001335 SmallString<128> FilePath(Filename);
1336
1337 // Ask the file manager to fixup the relative path for us. This will
1338 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001339 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001340
1341 // FIXME: This call to make_absolute shouldn't be necessary, the
1342 // call to FixupRelativePath should always return an absolute path.
1343 llvm::sys::fs::make_absolute(FilePath);
1344 Filename = FilePath.c_str();
1345
1346 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1347
1348 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1349 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001350
Douglas Gregor112b9072012-10-18 05:31:06 +00001351 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001352
1353 // Create input file offsets abbreviation.
1354 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1355 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1356 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001357 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1358 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001359 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1360 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1361
1362 // Write input file offsets.
1363 Record.clear();
1364 Record.push_back(INPUT_FILE_OFFSETS);
1365 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001366 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001367 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001368}
1369
Douglas Gregora7f71a92009-04-10 03:52:48 +00001370//===----------------------------------------------------------------------===//
1371// Source Manager Serialization
1372//===----------------------------------------------------------------------===//
1373
1374/// \brief Create an abbreviation for the SLocEntry that refers to a
1375/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001376static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001377 using namespace llvm;
1378 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001379 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001384 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001389 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001390}
1391
1392/// \brief Create an abbreviation for the SLocEntry that refers to a
1393/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001394static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001395 using namespace llvm;
1396 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001397 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001403 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001404}
1405
1406/// \brief Create an abbreviation for the SLocEntry that refers to a
1407/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001408static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001409 using namespace llvm;
1410 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001411 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001413 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001414}
1415
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001416/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1417/// expansion.
1418static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001419 using namespace llvm;
1420 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001421 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001427 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001428}
1429
Douglas Gregor09b69892011-02-10 17:09:37 +00001430namespace {
1431 // Trait used for the on-disk hash table of header search information.
1432 class HeaderFileInfoTrait {
1433 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001434 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001435
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001436 // Keep track of the framework names we've used during serialization.
1437 SmallVector<char, 128> FrameworkStringData;
1438 llvm::StringMap<unsigned> FrameworkNameOffset;
1439
Douglas Gregor09b69892011-02-10 17:09:37 +00001440 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001441 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1442 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001443
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001444 struct key_type {
1445 const FileEntry *FE;
1446 const char *Filename;
1447 };
1448 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001449
1450 typedef HeaderFileInfo data_type;
1451 typedef const data_type &data_type_ref;
1452
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001453 static unsigned ComputeHash(key_type_ref key) {
1454 // The hash is based only on size/time of the file, so that the reader can
1455 // match even when symlinking or excess path elements ("foo/../", "../")
1456 // change the form of the name. However, complete path is still the key.
1457 return llvm::hash_combine(key.FE->getSize(),
1458 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001459 }
1460
1461 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001462 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1463 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1464 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001465 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001466 if (Data.isModuleHeader)
1467 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001468 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001469 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001470 }
1471
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001472 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1473 clang::io::Emit64(Out, key.FE->getSize());
1474 KeyLen -= 8;
1475 clang::io::Emit64(Out, key.FE->getModificationTime());
1476 KeyLen -= 8;
1477 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001478 }
1479
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001480 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001481 data_type_ref Data, unsigned DataLen) {
1482 using namespace clang::io;
1483 uint64_t Start = Out.tell(); (void)Start;
1484
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001485 unsigned char Flags = (Data.HeaderRole << 6)
1486 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001487 | (Data.isPragmaOnce << 4)
1488 | (Data.DirInfo << 2)
1489 | (Data.Resolved << 1)
1490 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001491 Emit8(Out, (uint8_t)Flags);
1492 Emit16(Out, (uint16_t) Data.NumIncludes);
1493
1494 if (!Data.ControllingMacro)
1495 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1496 else
1497 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001498
1499 unsigned Offset = 0;
1500 if (!Data.Framework.empty()) {
1501 // If this header refers into a framework, save the framework name.
1502 llvm::StringMap<unsigned>::iterator Pos
1503 = FrameworkNameOffset.find(Data.Framework);
1504 if (Pos == FrameworkNameOffset.end()) {
1505 Offset = FrameworkStringData.size() + 1;
1506 FrameworkStringData.append(Data.Framework.begin(),
1507 Data.Framework.end());
1508 FrameworkStringData.push_back(0);
1509
1510 FrameworkNameOffset[Data.Framework] = Offset;
1511 } else
1512 Offset = Pos->second;
1513 }
1514 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001515
1516 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001517 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001518 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1519 }
1520
Douglas Gregor09b69892011-02-10 17:09:37 +00001521 assert(Out.tell() - Start == DataLen && "Wrong data length");
1522 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001523
1524 const char *strings_begin() const { return FrameworkStringData.begin(); }
1525 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001526 };
1527} // end anonymous namespace
1528
1529/// \brief Write the header search block for the list of files that
1530///
1531/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001532void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001533 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001534 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1535
1536 if (FilesByUID.size() > HS.header_file_size())
1537 FilesByUID.resize(HS.header_file_size());
1538
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001539 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001540 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001541 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001542 unsigned NumHeaderSearchEntries = 0;
1543 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1544 const FileEntry *File = FilesByUID[UID];
1545 if (!File)
1546 continue;
1547
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001548 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1549 // from the external source if it was not provided already.
1550 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001551 if (HFI.External && Chain)
1552 continue;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001553 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1554 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001555
1556 // Turn the file name into an absolute path, if it isn't already.
1557 const char *Filename = File->getName();
1558 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1559
1560 // If we performed any translation on the file name at all, we need to
1561 // save this string, since the generator will refer to it later.
1562 if (Filename != File->getName()) {
1563 Filename = strdup(Filename);
1564 SavedStrings.push_back(Filename);
1565 }
1566
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001567 HeaderFileInfoTrait::key_type key = { File, Filename };
1568 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001569 ++NumHeaderSearchEntries;
1570 }
1571
1572 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001573 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001574 uint32_t BucketOffset;
1575 {
1576 llvm::raw_svector_ostream Out(TableData);
1577 // Make sure that no bucket is at offset 0
1578 clang::io::Emit32(Out, 0);
1579 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1580 }
1581
1582 // Create a blob abbreviation
1583 using namespace llvm;
1584 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1585 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1590 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1591
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001592 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001593 RecordData Record;
1594 Record.push_back(HEADER_SEARCH_TABLE);
1595 Record.push_back(BucketOffset);
1596 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001597 Record.push_back(TableData.size());
1598 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001599 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1600
1601 // Free all of the strings we had to duplicate.
1602 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001603 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001604}
1605
Douglas Gregora7f71a92009-04-10 03:52:48 +00001606/// \brief Writes the block containing the serialized form of the
1607/// source manager.
1608///
1609/// TODO: We should probably use an on-disk hash table (stored in a
1610/// blob), indexed based on the file name, so that we only create
1611/// entries for files that we actually need. In the common case (no
1612/// errors), we probably won't have to create file entries for any of
1613/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001614void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001615 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001616 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001617 RecordData Record;
1618
Chris Lattner0910e3b2009-04-10 17:16:57 +00001619 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001620 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001621
1622 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001623 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1624 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1625 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001626 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001627
Douglas Gregor258ae542009-04-27 06:38:32 +00001628 // Write out the source location entry table. We skip the first
1629 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001630 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001631 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001632 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1633 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001634 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001635 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001636 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001637 FileID FID = FileID::get(I);
1638 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001639
Douglas Gregor258ae542009-04-27 06:38:32 +00001640 // Record the offset of this source-location entry.
1641 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1642
1643 // Figure out which record code to use.
1644 unsigned Code;
1645 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001646 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1647 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001648 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001649 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001650 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001651 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001652 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001653 Record.clear();
1654 Record.push_back(Code);
1655
Douglas Gregor925296b2011-07-19 16:10:42 +00001656 // Starting offset of this entry within this module, so skip the dummy.
1657 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001658 if (SLoc->isFile()) {
1659 const SrcMgr::FileInfo &File = SLoc->getFile();
1660 Record.push_back(File.getIncludeLoc().getRawEncoding());
1661 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1662 Record.push_back(File.hasLineDirectives());
1663
1664 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001665 if (Content->OrigEntry) {
1666 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001667 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001668
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001669 // The source location entry is a file. Emit input file ID.
1670 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1671 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001672
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001673 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001674
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001675 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001676 if (FDI != FileDeclIDs.end()) {
1677 Record.push_back(FDI->second->FirstDeclIndex);
1678 Record.push_back(FDI->second->DeclIDs.size());
1679 } else {
1680 Record.push_back(0);
1681 Record.push_back(0);
1682 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001683
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001684 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001685
1686 if (Content->BufferOverridden) {
1687 Record.clear();
1688 Record.push_back(SM_SLOC_BUFFER_BLOB);
1689 const llvm::MemoryBuffer *Buffer
1690 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1691 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1692 StringRef(Buffer->getBufferStart(),
1693 Buffer->getBufferSize() + 1));
1694 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001695 } else {
1696 // The source location entry is a buffer. The blob associated
1697 // with this entry contains the contents of the buffer.
1698
1699 // We add one to the size so that we capture the trailing NULL
1700 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1701 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001702 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001703 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001704 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001705 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001706 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001707 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001708 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001709 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001710 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001711 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001712
Douglas Gregor925296b2011-07-19 16:10:42 +00001713 if (strcmp(Name, "<built-in>") == 0) {
1714 PreloadSLocs.push_back(SLocEntryOffsets.size());
1715 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001716 }
1717 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001718 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001719 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001720 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1721 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001722 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1723 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001724
1725 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001726 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001727 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001728 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001729 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001730 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001731 }
1732 }
1733
Douglas Gregor8f45df52009-04-16 22:23:12 +00001734 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001735
1736 if (SLocEntryOffsets.empty())
1737 return;
1738
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001739 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001740 // table is used for lazily loading source-location information.
1741 using namespace llvm;
1742 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001743 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1747 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001748
Douglas Gregor258ae542009-04-27 06:38:32 +00001749 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001750 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001751 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001752 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001753 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001754
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001755 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001756 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001757 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001758
1759 // Write the line table. It depends on remapping working, so it must come
1760 // after the source location offsets.
1761 if (SourceMgr.hasLineTable()) {
1762 LineTableInfo &LineTable = SourceMgr.getLineTable();
1763
1764 Record.clear();
1765 // Emit the file names
1766 Record.push_back(LineTable.getNumFilenames());
1767 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1768 // Emit the file name
1769 const char *Filename = LineTable.getFilename(I);
1770 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1771 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1772 Record.push_back(FilenameLen);
1773 if (FilenameLen)
1774 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1775 }
1776
1777 // Emit the line entries
1778 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1779 L != LEnd; ++L) {
1780 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001781 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001782 continue;
1783
1784 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001785 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001786
1787 // Emit the line entries
1788 Record.push_back(L->second.size());
1789 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1790 LEEnd = L->second.end();
1791 LE != LEEnd; ++LE) {
1792 Record.push_back(LE->FileOffset);
1793 Record.push_back(LE->LineNo);
1794 Record.push_back(LE->FilenameID);
1795 Record.push_back((unsigned)LE->FileKind);
1796 Record.push_back(LE->IncludeOffset);
1797 }
1798 }
1799 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1800 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001801}
1802
Douglas Gregorc5046832009-04-27 18:38:38 +00001803//===----------------------------------------------------------------------===//
1804// Preprocessor Serialization
1805//===----------------------------------------------------------------------===//
1806
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001807namespace {
1808class ASTMacroTableTrait {
1809public:
1810 typedef IdentID key_type;
1811 typedef key_type key_type_ref;
1812
1813 struct Data {
1814 uint32_t MacroDirectivesOffset;
1815 };
1816
1817 typedef Data data_type;
1818 typedef const data_type &data_type_ref;
1819
1820 static unsigned ComputeHash(IdentID IdID) {
1821 return llvm::hash_value(IdID);
1822 }
1823
1824 std::pair<unsigned,unsigned>
1825 static EmitKeyDataLength(raw_ostream& Out,
1826 key_type_ref Key, data_type_ref Data) {
1827 unsigned KeyLen = 4; // IdentID.
1828 unsigned DataLen = 4; // MacroDirectivesOffset.
1829 return std::make_pair(KeyLen, DataLen);
1830 }
1831
1832 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1833 clang::io::Emit32(Out, Key);
1834 }
1835
1836 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1837 unsigned) {
1838 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1839 }
1840};
1841} // end anonymous namespace
1842
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001843static int compareMacroDirectives(
1844 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1845 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1846 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001847}
1848
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001849static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1850 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001851 if (MacroInfo *MI = MD->getMacroInfo())
1852 if (MI->isBuiltinMacro())
1853 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001854
1855 if (IsModule) {
1856 SourceLocation Loc = MD->getLocation();
1857 if (Loc.isInvalid())
1858 return true;
1859 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1860 return true;
1861 }
1862
1863 return false;
1864}
1865
Chris Lattnereeffaef2009-04-10 17:15:23 +00001866/// \brief Writes the block containing the serialized form of the
1867/// preprocessor.
1868///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001869void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001870 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1871 if (PPRec)
1872 WritePreprocessorDetail(*PPRec);
1873
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001874 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001875
Chris Lattner0af3ba12009-04-13 01:29:17 +00001876 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1877 if (PP.getCounterValue() != 0) {
1878 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001879 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001880 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001881 }
1882
1883 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001884 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001885
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001886 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001887 // FIXME: use diagnostics subsystem for localization etc.
1888 if (PP.SawDateOrTime())
1889 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001890
Douglas Gregor796d76a2010-10-20 22:00:55 +00001891
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001892 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001893 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001894
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001895 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001896 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001897 MacroDirectives;
1898 for (Preprocessor::macro_iterator
1899 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1900 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001901 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001902 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001903 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001904
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001905 // Sort the set of macro definitions that need to be serialized by the
1906 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001907 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1908 &compareMacroDirectives);
1909
1910 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1911
1912 // Emit the macro directives as a list and associate the offset with the
1913 // identifier they belong to.
1914 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1915 const IdentifierInfo *Name = MacroDirectives[I].first;
1916 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1917 MacroDirective *MD = MacroDirectives[I].second;
1918
1919 // If the macro or identifier need no updates, don't write the macro history
1920 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001921 // FIXME: Chain the macro history instead of re-writing it.
1922 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001923 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1924 continue;
1925
1926 // Emit the macro directives in reverse source order.
1927 for (; MD; MD = MD->getPrevious()) {
1928 if (shouldIgnoreMacro(MD, IsModule, PP))
1929 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001930
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001931 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001932 Record.push_back(MD->getKind());
1933 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1934 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1935 Record.push_back(InfoID);
1936 Record.push_back(DefMD->isImported());
1937 Record.push_back(DefMD->isAmbiguous());
1938
1939 } else if (VisibilityMacroDirective *
1940 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1941 Record.push_back(VisMD->isPublic());
1942 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001943 }
1944 if (Record.empty())
1945 continue;
1946
1947 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1948 Record.clear();
1949
1950 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1951
1952 IdentID NameID = getIdentifierRef(Name);
1953 ASTMacroTableTrait::Data data;
1954 data.MacroDirectivesOffset = MacroDirectiveOffset;
1955 Generator.insert(NameID, data);
1956 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001957
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001958 /// \brief Offsets of each of the macros into the bitstream, indexed by
1959 /// the local macro ID
1960 ///
1961 /// For each identifier that is associated with a macro, this map
1962 /// provides the offset into the bitstream where that macro is
1963 /// defined.
1964 std::vector<uint32_t> MacroOffsets;
1965
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001966 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1967 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1968 MacroInfo *MI = MacroInfosToEmit[I].MI;
1969 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001970
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001971 if (ID < FirstMacroID) {
1972 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1973 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001974 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001975
1976 // Record the local offset of this macro.
1977 unsigned Index = ID - FirstMacroID;
1978 if (Index == MacroOffsets.size())
1979 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1980 else {
1981 if (Index > MacroOffsets.size())
1982 MacroOffsets.resize(Index + 1);
1983
1984 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1985 }
1986
1987 AddIdentifierRef(Name, Record);
1988 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1989 AddSourceLocation(MI->getDefinitionLoc(), Record);
1990 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1991 Record.push_back(MI->isUsed());
1992 unsigned Code;
1993 if (MI->isObjectLike()) {
1994 Code = PP_MACRO_OBJECT_LIKE;
1995 } else {
1996 Code = PP_MACRO_FUNCTION_LIKE;
1997
1998 Record.push_back(MI->isC99Varargs());
1999 Record.push_back(MI->isGNUVarargs());
2000 Record.push_back(MI->hasCommaPasting());
2001 Record.push_back(MI->getNumArgs());
2002 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2003 I != E; ++I)
2004 AddIdentifierRef(*I, Record);
2005 }
2006
2007 // If we have a detailed preprocessing record, record the macro definition
2008 // ID that corresponds to this macro.
2009 if (PPRec)
2010 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2011
2012 Stream.EmitRecord(Code, Record);
2013 Record.clear();
2014
2015 // Emit the tokens array.
2016 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2017 // Note that we know that the preprocessor does not have any annotation
2018 // tokens in it because they are created by the parser, and thus can't
2019 // be in a macro definition.
2020 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002021 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002022 Stream.EmitRecord(PP_TOKEN, Record);
2023 Record.clear();
2024 }
2025 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002026 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002027
Douglas Gregor92a96f52011-02-08 21:58:10 +00002028 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002029
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002030 // Create the on-disk hash table in a buffer.
2031 SmallString<4096> MacroTable;
2032 uint32_t BucketOffset;
2033 {
2034 llvm::raw_svector_ostream Out(MacroTable);
2035 // Make sure that no bucket is at offset 0
2036 clang::io::Emit32(Out, 0);
2037 BucketOffset = Generator.Emit(Out);
2038 }
2039
2040 // Write the macro table
2041 using namespace llvm;
2042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2043 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2046 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2047
2048 Record.push_back(MACRO_TABLE);
2049 Record.push_back(BucketOffset);
2050 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2051 Record.clear();
2052
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002053 // Write the offsets table for macro IDs.
2054 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002055 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002056 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2060
2061 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2062 Record.clear();
2063 Record.push_back(MACRO_OFFSET);
2064 Record.push_back(MacroOffsets.size());
2065 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2066 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2067 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002068}
2069
2070void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002071 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002072 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002073
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002074 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002075
Douglas Gregor92a96f52011-02-08 21:58:10 +00002076 // Enter the preprocessor block.
2077 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002078
Douglas Gregoraae92242010-03-19 21:51:54 +00002079 // If the preprocessor has a preprocessing record, emit it.
2080 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002081 using namespace llvm;
2082
2083 // Set up the abbreviation for
2084 unsigned InclusionAbbrev = 0;
2085 {
2086 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2087 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002092 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2093 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2094 }
2095
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002096 unsigned FirstPreprocessorEntityID
2097 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2098 + NUM_PREDEF_PP_ENTITY_IDS;
2099 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002100 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002101 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2102 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002103 E != EEnd;
2104 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002105 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002106
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002107 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2108 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002109
Douglas Gregor92a96f52011-02-08 21:58:10 +00002110 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002111 // Record this macro definition's ID.
2112 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002113
Douglas Gregor92a96f52011-02-08 21:58:10 +00002114 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002115 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2116 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002117 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002118
Chandler Carrutha88a22182011-07-14 08:20:46 +00002119 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002120 Record.push_back(ME->isBuiltinMacro());
2121 if (ME->isBuiltinMacro())
2122 AddIdentifierRef(ME->getName(), Record);
2123 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002124 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002125 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002126 continue;
2127 }
2128
2129 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2130 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002131 Record.push_back(ID->getFileName().size());
2132 Record.push_back(ID->wasInQuotes());
2133 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002134 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002135 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002136 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002137 // Check that the FileEntry is not null because it was not resolved and
2138 // we create a PCH even with compiler errors.
2139 if (ID->getFile())
2140 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002141 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2142 continue;
2143 }
2144
2145 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2146 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002147 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002148
Douglas Gregoraae92242010-03-19 21:51:54 +00002149 // Write the offsets table for the preprocessing record.
2150 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002151 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2152
Douglas Gregoraae92242010-03-19 21:51:54 +00002153 // Write the offsets table for identifier IDs.
2154 using namespace llvm;
2155 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002156 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002159 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002160
Douglas Gregoraae92242010-03-19 21:51:54 +00002161 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002162 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002163 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002164 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2165 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002166 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002167}
2168
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002169unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2170 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2171 if (Known != SubmoduleIDs.end())
2172 return Known->second;
2173
2174 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2175}
2176
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002177unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2178 if (!Mod)
2179 return 0;
2180
2181 llvm::DenseMap<Module *, unsigned>::const_iterator
2182 Known = SubmoduleIDs.find(Mod);
2183 if (Known != SubmoduleIDs.end())
2184 return Known->second;
2185
2186 return 0;
2187}
2188
Douglas Gregor253eefe2011-12-01 00:59:36 +00002189/// \brief Compute the number of modules within the given tree (including the
2190/// given module).
2191static unsigned getNumberOfModules(Module *Mod) {
2192 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002193 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2194 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002195 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002196 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002197
2198 return ChildModules + 1;
2199}
2200
Douglas Gregorde3ef502011-11-30 23:21:26 +00002201void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002202 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002203 // FIXME: This feels like it belongs somewhere else, but there are no
2204 // other consumers of this information.
2205 SourceManager &SrcMgr = PP->getSourceManager();
2206 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2207 for (ASTContext::import_iterator I = Context->local_import_begin(),
2208 IEnd = Context->local_import_end();
2209 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002210 if (Module *ImportedFrom
2211 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2212 SrcMgr))) {
2213 ImportedFrom->Imports.push_back(I->getImportedModule());
2214 }
2215 }
2216
Douglas Gregor69021972011-11-30 17:33:56 +00002217 // Enter the submodule description block.
2218 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2219
2220 // Write the abbreviations needed for the submodules block.
2221 using namespace llvm;
2222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2223 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2234 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2235
2236 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002237 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2239 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2240
2241 Abbrev = new BitCodeAbbrev();
2242 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2244 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002245
2246 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002247 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2249 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2250
2251 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002252 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2254 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2255
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002256 Abbrev = new BitCodeAbbrev();
2257 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002260 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2261
Douglas Gregor59527662012-10-15 06:28:11 +00002262 Abbrev = new BitCodeAbbrev();
2263 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2265 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2266
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002267 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002268 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2270 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2271
2272 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002273 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2276 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2277
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002278 Abbrev = new BitCodeAbbrev();
2279 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2281 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2282
Douglas Gregorfb912652013-03-20 21:10:35 +00002283 Abbrev = new BitCodeAbbrev();
2284 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2287 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2288
Douglas Gregor253eefe2011-12-01 00:59:36 +00002289 // Write the submodule metadata block.
2290 RecordData Record;
2291 Record.push_back(getNumberOfModules(WritingModule));
2292 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2293 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2294
Douglas Gregor69021972011-11-30 17:33:56 +00002295 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002296 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002297 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002298 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002299 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002300 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002301 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002302
2303 // Emit the definition of the block.
2304 Record.clear();
2305 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002306 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002307 if (Mod->Parent) {
2308 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2309 Record.push_back(SubmoduleIDs[Mod->Parent]);
2310 } else {
2311 Record.push_back(0);
2312 }
2313 Record.push_back(Mod->IsFramework);
2314 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002315 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002316 Record.push_back(Mod->InferSubmodules);
2317 Record.push_back(Mod->InferExplicitSubmodules);
2318 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002319 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002320 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2321
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002322 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002323 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002324 Record.clear();
2325 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002326 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002327 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002328 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002329 }
2330
Douglas Gregor69021972011-11-30 17:33:56 +00002331 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002332 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002333 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002334 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002335 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002336 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002337 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2338 Record.clear();
2339 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2340 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2341 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002342 }
2343
2344 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002345 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002346 Record.clear();
2347 Record.push_back(SUBMODULE_HEADER);
2348 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002349 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002350 }
Douglas Gregor59527662012-10-15 06:28:11 +00002351 // Emit the excluded headers.
2352 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2353 Record.clear();
2354 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2355 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2356 Mod->ExcludedHeaders[I]->getName());
2357 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002358 // Emit the private headers.
2359 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2360 Record.clear();
2361 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2362 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2363 Mod->PrivateHeaders[I]->getName());
2364 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002365 ArrayRef<const FileEntry *>
2366 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2367 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002368 Record.clear();
2369 Record.push_back(SUBMODULE_TOPHEADER);
2370 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002371 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002372 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002373
2374 // Emit the imports.
2375 if (!Mod->Imports.empty()) {
2376 Record.clear();
2377 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002378 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002379 assert(ImportedID && "Unknown submodule!");
2380 Record.push_back(ImportedID);
2381 }
2382 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2383 }
2384
Douglas Gregor24bb9232011-12-02 18:58:38 +00002385 // Emit the exports.
2386 if (!Mod->Exports.empty()) {
2387 Record.clear();
2388 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002389 if (Module *Exported = Mod->Exports[I].getPointer()) {
2390 unsigned ExportedID = SubmoduleIDs[Exported];
2391 assert(ExportedID > 0 && "Unknown submodule ID?");
2392 Record.push_back(ExportedID);
2393 } else {
2394 Record.push_back(0);
2395 }
2396
Douglas Gregor24bb9232011-12-02 18:58:38 +00002397 Record.push_back(Mod->Exports[I].getInt());
2398 }
2399 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2400 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002401
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002402 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2403 // Might be unnecessary as use declarations are only used to build the
2404 // module itself.
2405
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002406 // Emit the link libraries.
2407 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2408 Record.clear();
2409 Record.push_back(SUBMODULE_LINK_LIBRARY);
2410 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2411 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2412 Mod->LinkLibraries[I].Library);
2413 }
2414
Douglas Gregorfb912652013-03-20 21:10:35 +00002415 // Emit the conflicts.
2416 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2417 Record.clear();
2418 Record.push_back(SUBMODULE_CONFLICT);
2419 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2420 assert(OtherID && "Unknown submodule!");
2421 Record.push_back(OtherID);
2422 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2423 Mod->Conflicts[I].Message);
2424 }
2425
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002426 // Emit the configuration macros.
2427 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2428 Record.clear();
2429 Record.push_back(SUBMODULE_CONFIG_MACRO);
2430 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2431 Mod->ConfigMacros[I]);
2432 }
2433
Douglas Gregor69021972011-11-30 17:33:56 +00002434 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002435 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2436 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002437 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002438 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002439 }
2440
2441 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002442
2443 assert((NextSubmoduleID - FirstSubmoduleID
2444 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002445}
2446
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002447serialization::SubmoduleID
2448ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002449 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002450 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002451
2452 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002453 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002454 Module *OwningMod
2455 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002456 if (!OwningMod)
2457 return 0;
2458
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002459 // Check whether this submodule is part of our own module.
2460 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002461 return 0;
2462
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002463 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002464}
2465
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002466void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2467 bool isModule) {
2468 // Make sure set diagnostic pragmas don't affect the translation unit that
2469 // imports the module.
2470 // FIXME: Make diagnostic pragma sections work properly with modules.
2471 if (isModule)
2472 return;
2473
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002474 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2475 DiagStateIDMap;
2476 unsigned CurrID = 0;
2477 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002478 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002479 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002480 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2481 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002482 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002483 if (point.Loc.isInvalid())
2484 continue;
2485
2486 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002487 unsigned &DiagStateID = DiagStateIDMap[point.State];
2488 Record.push_back(DiagStateID);
2489
2490 if (DiagStateID == 0) {
2491 DiagStateID = ++CurrID;
2492 for (DiagnosticsEngine::DiagState::const_iterator
2493 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2494 if (I->second.isPragma()) {
2495 Record.push_back(I->first);
2496 Record.push_back(I->second.getMapping());
2497 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002498 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002499 Record.push_back(-1); // mark the end of the diag/map pairs for this
2500 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002501 }
2502 }
2503
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002504 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002505 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002506}
2507
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002508void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2509 if (CXXBaseSpecifiersOffsets.empty())
2510 return;
2511
2512 RecordData Record;
2513
2514 // Create a blob abbreviation for the C++ base specifiers offsets.
2515 using namespace llvm;
2516
2517 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2518 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2521 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2522
Douglas Gregorc27b2872011-08-04 00:01:48 +00002523 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002524 Record.clear();
2525 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2526 Record.push_back(CXXBaseSpecifiersOffsets.size());
2527 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002528 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002529}
2530
Douglas Gregorc5046832009-04-27 18:38:38 +00002531//===----------------------------------------------------------------------===//
2532// Type Serialization
2533//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002534
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002535/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002536void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002537 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002538 if (Idx.getIndex() == 0) // we haven't seen this type before.
2539 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002540
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002541 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002542
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002543 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002544 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002545 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002546 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002547 else if (TypeOffsets.size() < Index) {
2548 TypeOffsets.resize(Index + 1);
2549 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002550 }
2551
2552 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002553
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002554 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002555 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002556
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002557 if (T.hasLocalNonFastQualifiers()) {
2558 Qualifiers Qs = T.getLocalQualifiers();
2559 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002560 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002561 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002562 } else {
2563 switch (T->getTypeClass()) {
2564 // For all of the concrete, non-dependent types, call the
2565 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002566#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002567 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002568#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002569#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002570 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002571 }
2572
2573 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002574 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002575
2576 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002577 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002578}
2579
Douglas Gregorc5046832009-04-27 18:38:38 +00002580//===----------------------------------------------------------------------===//
2581// Declaration Serialization
2582//===----------------------------------------------------------------------===//
2583
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002584/// \brief Write the block containing all of the declaration IDs
2585/// lexically declared within the given DeclContext.
2586///
2587/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2588/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002589uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002590 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002591 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002592 return 0;
2593
Douglas Gregor8f45df52009-04-16 22:23:12 +00002594 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002595 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002596 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002597 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002598 for (const auto *D : DC->decls())
2599 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002600
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002601 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002602 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002603 return Offset;
2604}
2605
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002606void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002607 using namespace llvm;
2608 RecordData Record;
2609
2610 // Write the type offsets array
2611 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002612 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2616 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2617 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002618 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002619 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002620 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002621 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002622
2623 // Write the declaration offsets array
2624 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002625 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002626 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2629 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2630 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002631 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002632 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002633 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002634 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002635}
2636
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002637void ASTWriter::WriteFileDeclIDsMap() {
2638 using namespace llvm;
2639 RecordData Record;
2640
2641 // Join the vectors of DeclIDs from all files.
2642 SmallVector<DeclID, 256> FileSortedIDs;
2643 for (FileDeclIDsTy::iterator
2644 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2645 DeclIDInFileInfo &Info = *FI->second;
2646 Info.FirstDeclIndex = FileSortedIDs.size();
2647 for (LocDeclIDsTy::iterator
2648 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2649 FileSortedIDs.push_back(DI->second);
2650 }
2651
2652 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2653 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2656 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2657 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002658 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002659 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2660}
2661
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002662void ASTWriter::WriteComments() {
2663 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002664 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002665 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002666 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2667 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002668 I != E; ++I) {
2669 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002670 AddSourceRange((*I)->getSourceRange(), Record);
2671 Record.push_back((*I)->getKind());
2672 Record.push_back((*I)->isTrailingComment());
2673 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002674 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2675 }
2676 Stream.ExitBlock();
2677}
2678
Douglas Gregorc5046832009-04-27 18:38:38 +00002679//===----------------------------------------------------------------------===//
2680// Global Method Pool and Selector Serialization
2681//===----------------------------------------------------------------------===//
2682
Douglas Gregore84a9da2009-04-20 20:36:09 +00002683namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002684// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002685class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002686 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002687
2688public:
2689 typedef Selector key_type;
2690 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002691
Sebastian Redl834bb972010-08-04 17:20:04 +00002692 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002693 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002694 ObjCMethodList Instance, Factory;
2695 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002696 typedef const data_type& data_type_ref;
2697
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002698 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002699
Douglas Gregorc78d3462009-04-24 21:10:55 +00002700 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002701 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002702 }
Mike Stump11289f42009-09-09 15:08:12 +00002703
2704 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002705 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002706 data_type_ref Methods) {
2707 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2708 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002709 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2710 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002711 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002712 if (Method->Method)
2713 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002714 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002715 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002716 if (Method->Method)
2717 DataLen += 4;
2718 clang::io::Emit16(Out, DataLen);
2719 return std::make_pair(KeyLen, DataLen);
2720 }
Mike Stump11289f42009-09-09 15:08:12 +00002721
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002722 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002723 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002724 assert((Start >> 32) == 0 && "Selector key offset too large");
2725 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002726 unsigned N = Sel.getNumArgs();
2727 clang::io::Emit16(Out, N);
2728 if (N == 0)
2729 N = 1;
2730 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002731 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002732 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2733 }
Mike Stump11289f42009-09-09 15:08:12 +00002734
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002735 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002736 data_type_ref Methods, unsigned DataLen) {
2737 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002738 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002739 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002740 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002741 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002742 if (Method->Method)
2743 ++NumInstanceMethods;
2744
2745 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002746 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002747 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002748 if (Method->Method)
2749 ++NumFactoryMethods;
2750
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002751 unsigned InstanceBits = Methods.Instance.getBits();
2752 assert(InstanceBits < 4);
2753 unsigned NumInstanceMethodsAndBits =
2754 (NumInstanceMethods << 2) | InstanceBits;
2755 unsigned FactoryBits = Methods.Factory.getBits();
2756 assert(FactoryBits < 4);
2757 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2758 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2759 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002760 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002761 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002762 if (Method->Method)
2763 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002764 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002765 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002766 if (Method->Method)
2767 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002768
2769 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002770 }
2771};
2772} // end anonymous namespace
2773
Sebastian Redla19a67f2010-08-03 21:58:15 +00002774/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002775///
2776/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002777/// in an on-disk hash table indexed by the selector. The hash table also
2778/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002779void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002780 using namespace llvm;
2781
Sebastian Redla19a67f2010-08-03 21:58:15 +00002782 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002783 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002784 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002785 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002786 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002787 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002788 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002789 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002790
Sebastian Redla19a67f2010-08-03 21:58:15 +00002791 // Create the on-disk hash table representation. We walk through every
2792 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002793 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002794 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002795 I = SelectorIDs.begin(), E = SelectorIDs.end();
2796 I != E; ++I) {
2797 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002798 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002799 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002800 I->second,
2801 ObjCMethodList(),
2802 ObjCMethodList()
2803 };
2804 if (F != SemaRef.MethodPool.end()) {
2805 Data.Instance = F->second.first;
2806 Data.Factory = F->second.second;
2807 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002808 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002809 // changed.
2810 if (Chain && I->second < FirstSelectorID) {
2811 // Selector already exists. Did it change?
2812 bool changed = false;
2813 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002814 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002815 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002816 changed = true;
2817 }
2818 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002819 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002820 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002821 changed = true;
2822 }
2823 if (!changed)
2824 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002825 } else if (Data.Instance.Method || Data.Factory.Method) {
2826 // A new method pool entry.
2827 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002828 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002829 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002830 }
2831
Douglas Gregorc78d3462009-04-24 21:10:55 +00002832 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002833 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002834 uint32_t BucketOffset;
2835 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002836 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002837 llvm::raw_svector_ostream Out(MethodPool);
2838 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002839 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002840 BucketOffset = Generator.Emit(Out, Trait);
2841 }
2842
2843 // Create a blob abbreviation
2844 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002845 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2849 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2850
Douglas Gregor95c13f52009-04-25 17:48:32 +00002851 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002852 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002853 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002854 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002855 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002856 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002857
2858 // Create a blob abbreviation for the selector table offsets.
2859 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002860 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2864 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2865
2866 // Write the selector offsets table.
2867 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002868 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002869 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002870 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002871 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002872 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002873 }
2874}
2875
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002876/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002877void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002878 using namespace llvm;
2879 if (SemaRef.ReferencedSelectors.empty())
2880 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002881
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002882 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002883
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002884 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002885 // very tricky to fix, and given that @selector shouldn't really appear in
2886 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002887 for (DenseMap<Selector, SourceLocation>::iterator S =
2888 SemaRef.ReferencedSelectors.begin(),
2889 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2890 Selector Sel = (*S).first;
2891 SourceLocation Loc = (*S).second;
2892 AddSelectorRef(Sel, Record);
2893 AddSourceLocation(Loc, Record);
2894 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002895 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002896}
2897
Douglas Gregorc5046832009-04-27 18:38:38 +00002898//===----------------------------------------------------------------------===//
2899// Identifier Table Serialization
2900//===----------------------------------------------------------------------===//
2901
Douglas Gregorc78d3462009-04-24 21:10:55 +00002902namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002903class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002904 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002905 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002906 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002907 bool IsModule;
2908
Douglas Gregor1d583f22009-04-28 21:18:29 +00002909 /// \brief Determines whether this is an "interesting" identifier
2910 /// that needs a full IdentifierInfo structure written into the hash
2911 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002912 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002913 if (II->isPoisoned() ||
2914 II->isExtensionToken() ||
2915 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002916 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002917 II->getFETokenInfo<void>())
2918 return true;
2919
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002920 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002921 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002922
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002923 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002924 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002925 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002926
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002927 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2928 if (!IsModule)
2929 return !shouldIgnoreMacro(Macro, IsModule, PP);
2930 SubmoduleID ModID;
2931 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2932 return true;
2933 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002934
2935 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002936 }
2937
Richard Smith49f906a2014-03-01 00:08:04 +00002938 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2939
2940 MacroDirective *
2941 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002942 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002943 llvm::SmallVector<SubmoduleID, 1> Overridden;
2944 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2945 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2946 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002947 return 0;
2948 }
2949
Richard Smith49f906a2014-03-01 00:08:04 +00002950 MacroDirective *
2951 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2952 OverriddenList &Overridden) {
2953 if (MacroDirective *NextMD =
2954 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2955 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2956 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002957 return 0;
2958 }
2959
2960 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00002961 /// public macro definition or undefinition that is not in ModID.
2962 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002963 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00002964 /// ModID is updated to the module containing the returned directive.
2965 ///
2966 /// FIXME: This process breaks down if a module defines a macro, imports
2967 /// another submodule that changes the macro, then changes the
2968 /// macro again itself.
2969 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2970 SubmoduleID &ModID,
2971 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002972 if (!MD)
2973 return 0;
2974
Richard Smith49f906a2014-03-01 00:08:04 +00002975 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002976 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002977 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002978 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002979 SubmoduleID ThisModID = getSubmoduleID(MD);
2980 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00002981 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002982 continue;
2983 }
Richard Smith49f906a2014-03-01 00:08:04 +00002984 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002985 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002986 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002987 }
Richard Smith49f906a2014-03-01 00:08:04 +00002988
2989 // If this is a definition from a submodule import, that submodule's
2990 // definition is overridden by the definition or undefinition that we
2991 // started with.
2992 // FIXME: This should only apply to macros defined in OrigModID.
2993 // We can't do that currently, because a #include of a different submodule
2994 // of the same module just leaks through macros instead of providing new
2995 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00002996 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2997 // Figure out which submodule the macro was originally defined within.
2998 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
2999 if (!SourceID) {
3000 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3001 if (DefLoc == MD->getLocation())
3002 SourceID = ThisModID;
3003 else
3004 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3005 }
3006 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003007 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003008 }
Richard Smith49f906a2014-03-01 00:08:04 +00003009
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003010 // We are looking for a definition in a different submodule than the one
3011 // that we started with. If a submodule has re-definitions of the same
3012 // macro, only the last definition will be used as the "exported" one.
3013 if (ModID == OrigModID)
3014 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003015
Richard Smith49f906a2014-03-01 00:08:04 +00003016 // The latest visibility directive for a name in a submodule affects all
3017 // the directives that come before it.
3018 if (VisibilityMacroDirective *VisMD =
3019 dyn_cast<VisibilityMacroDirective>(MD)) {
3020 if (!IsPublic.hasValue())
3021 IsPublic = VisMD->isPublic();
3022 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3023 // FIXME: If we find an imported macro, we should include its list of
3024 // overrides in our export.
3025 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003026 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003027 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003028
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003029 return 0;
3030 }
3031
3032 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003033 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003034 }
3035
Douglas Gregore84a9da2009-04-20 20:36:09 +00003036public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003037 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003038 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003039
Sebastian Redl539c5062010-08-18 23:57:32 +00003040 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003041 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003042
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003043 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3044 IdentifierResolver &IdResolver, bool IsModule)
3045 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003046
3047 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003048 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003049 }
Mike Stump11289f42009-09-09 15:08:12 +00003050
3051 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003052 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003053 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003054 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003055 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003056 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003057 DataLen += 2; // 2 bytes for builtin ID
3058 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003059 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003060 DataLen += 4; // MacroDirectives offset.
3061 if (IsModule) {
3062 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003063 llvm::SmallVector<SubmoduleID, 4> Overridden;
3064 for (MacroDirective *
3065 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3066 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3067 // Previous macro's overrides.
3068 if (!Overridden.empty())
3069 DataLen += 4 * (1 + Overridden.size());
3070 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003071 }
Richard Smith49f906a2014-03-01 00:08:04 +00003072 // Previous macro's overrides.
3073 if (!Overridden.empty())
3074 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003075 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003076 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003077 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003078
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003079 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3080 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003081 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003082 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003083 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003084 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003085 // We emit the key length after the data length so that every
3086 // string is preceded by a 16-bit length. This matches the PTH
3087 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003088 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003089 return std::make_pair(KeyLen, DataLen);
3090 }
Mike Stump11289f42009-09-09 15:08:12 +00003091
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003092 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003093 unsigned KeyLen) {
3094 // Record the location of the key data. This is used when generating
3095 // the mapping from persistent IDs to strings.
3096 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003097 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003098 }
Mike Stump11289f42009-09-09 15:08:12 +00003099
Richard Smith49f906a2014-03-01 00:08:04 +00003100 static void emitMacroOverrides(raw_ostream &Out,
3101 llvm::ArrayRef<SubmoduleID> Overridden) {
3102 if (!Overridden.empty()) {
3103 clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
3104 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3105 clang::io::Emit32(Out, Overridden[I]);
3106 }
3107 }
3108
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003109 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003110 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003111 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003112 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003113 clang::io::Emit32(Out, ID << 1);
3114 return;
3115 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003116
Douglas Gregor1d583f22009-04-28 21:18:29 +00003117 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003118 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3119 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3120 clang::io::Emit16(Out, Bits);
3121 Bits = 0;
3122 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003123 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003124 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003125 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3126 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003127 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003128 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003129 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003130
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003131 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003132 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3133 if (IsModule) {
3134 // Write the IDs of macros coming from different submodules.
3135 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003136 llvm::SmallVector<SubmoduleID, 4> Overridden;
3137 for (MacroDirective *
3138 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3139 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3140 MacroID InfoID = 0;
3141 emitMacroOverrides(Out, Overridden);
3142 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3143 InfoID = Writer.getMacroID(DefMD->getInfo());
3144 assert(InfoID);
3145 clang::io::Emit32(Out, InfoID << 1);
3146 } else {
3147 assert(isa<UndefMacroDirective>(MD));
3148 clang::io::Emit32(Out, (ModID << 1) | 1);
3149 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003150 }
Richard Smith49f906a2014-03-01 00:08:04 +00003151 emitMacroOverrides(Out, Overridden);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003152 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003153 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003154 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003155
Douglas Gregora868bbd2009-04-21 22:25:48 +00003156 // Emit the declaration IDs in reverse order, because the
3157 // IdentifierResolver provides the declarations as they would be
3158 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003159 // "stat"), but the ASTReader adds declarations to the end of the list
3160 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003161 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003162 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3163 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003164 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003165 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003166 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003167 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3168 }
3169
3170 /// \brief Returns the most recent local decl or the given decl if there are
3171 /// no local ones. The given decl is assumed to be the most recent one.
3172 Decl *getMostRecentLocalDecl(Decl *Orig) {
3173 // The only way a "from AST file" decl would be more recent from a local one
3174 // is if it came from a module.
3175 if (!PP.getLangOpts().Modules)
3176 return Orig;
3177
3178 // Look for a local in the decl chain.
3179 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3180 if (!D->isFromASTFile())
3181 return D;
3182 // If we come up a decl from a (chained-)PCH stop since we won't find a
3183 // local one.
3184 if (D->getOwningModuleID() == 0)
3185 break;
3186 }
3187
3188 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003189 }
3190};
3191} // end anonymous namespace
3192
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003193/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003194///
3195/// The identifier table consists of a blob containing string data
3196/// (the actual identifiers themselves) and a separate "offsets" index
3197/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003198void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3199 IdentifierResolver &IdResolver,
3200 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003201 using namespace llvm;
3202
3203 // Create and write out the blob that contains the identifier
3204 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003205 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003206 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003207 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003208
Douglas Gregore6648fb2009-04-28 20:33:11 +00003209 // Look for any identifiers that were named while processing the
3210 // headers, but are otherwise not needed. We add these to the hash
3211 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003212 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003213 // file.
3214 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3215 IDEnd = PP.getIdentifierTable().end();
3216 ID != IDEnd; ++ID)
3217 getIdentifierRef(ID->second);
3218
Sebastian Redlff4a2952010-07-23 23:49:55 +00003219 // Create the on-disk hash table representation. We only store offsets
3220 // for identifiers that appear here for the first time.
3221 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003222 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003223 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3224 ID != IDEnd; ++ID) {
3225 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003226 if (!Chain || !ID->first->isFromAST() ||
3227 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003228 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003229 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003230 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003231
Douglas Gregore84a9da2009-04-20 20:36:09 +00003232 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003233 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003234 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003235 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003236 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003237 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003238 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003239 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003240 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003241 }
3242
3243 // Create a blob abbreviation
3244 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003245 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003248 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003249
3250 // Write the identifier table
3251 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003252 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003253 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003254 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003255 }
3256
3257 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003258 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003259 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3263 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3264
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003265#ifndef NDEBUG
3266 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3267 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3268#endif
3269
Douglas Gregor0e149972009-04-25 19:10:14 +00003270 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003271 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003272 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003273 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003274 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003275 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003276}
3277
Douglas Gregorc5046832009-04-27 18:38:38 +00003278//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003279// DeclContext's Name Lookup Table Serialization
3280//===----------------------------------------------------------------------===//
3281
3282namespace {
3283// Trait used for the on-disk hash table used in the method pool.
3284class ASTDeclContextNameLookupTrait {
3285 ASTWriter &Writer;
3286
3287public:
3288 typedef DeclarationName key_type;
3289 typedef key_type key_type_ref;
3290
3291 typedef DeclContext::lookup_result data_type;
3292 typedef const data_type& data_type_ref;
3293
3294 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3295
3296 unsigned ComputeHash(DeclarationName Name) {
3297 llvm::FoldingSetNodeID ID;
3298 ID.AddInteger(Name.getNameKind());
3299
3300 switch (Name.getNameKind()) {
3301 case DeclarationName::Identifier:
3302 ID.AddString(Name.getAsIdentifierInfo()->getName());
3303 break;
3304 case DeclarationName::ObjCZeroArgSelector:
3305 case DeclarationName::ObjCOneArgSelector:
3306 case DeclarationName::ObjCMultiArgSelector:
3307 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3308 break;
3309 case DeclarationName::CXXConstructorName:
3310 case DeclarationName::CXXDestructorName:
3311 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003312 break;
3313 case DeclarationName::CXXOperatorName:
3314 ID.AddInteger(Name.getCXXOverloadedOperator());
3315 break;
3316 case DeclarationName::CXXLiteralOperatorName:
3317 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3318 case DeclarationName::CXXUsingDirective:
3319 break;
3320 }
3321
3322 return ID.ComputeHash();
3323 }
3324
3325 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003326 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003327 data_type_ref Lookup) {
3328 unsigned KeyLen = 1;
3329 switch (Name.getNameKind()) {
3330 case DeclarationName::Identifier:
3331 case DeclarationName::ObjCZeroArgSelector:
3332 case DeclarationName::ObjCOneArgSelector:
3333 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003334 case DeclarationName::CXXLiteralOperatorName:
3335 KeyLen += 4;
3336 break;
3337 case DeclarationName::CXXOperatorName:
3338 KeyLen += 1;
3339 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003340 case DeclarationName::CXXConstructorName:
3341 case DeclarationName::CXXDestructorName:
3342 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003343 case DeclarationName::CXXUsingDirective:
3344 break;
3345 }
3346 clang::io::Emit16(Out, KeyLen);
3347
3348 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003349 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003350 clang::io::Emit16(Out, DataLen);
3351
3352 return std::make_pair(KeyLen, DataLen);
3353 }
3354
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003355 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003356 using namespace clang::io;
3357
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003358 Emit8(Out, Name.getNameKind());
3359 switch (Name.getNameKind()) {
3360 case DeclarationName::Identifier:
3361 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003362 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003363 case DeclarationName::ObjCZeroArgSelector:
3364 case DeclarationName::ObjCOneArgSelector:
3365 case DeclarationName::ObjCMultiArgSelector:
3366 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003367 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003368 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003369 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3370 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003371 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003372 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003373 case DeclarationName::CXXLiteralOperatorName:
3374 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003375 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003376 case DeclarationName::CXXConstructorName:
3377 case DeclarationName::CXXDestructorName:
3378 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003379 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003380 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003381 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003382
3383 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003384 }
3385
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003386 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003387 data_type Lookup, unsigned DataLen) {
3388 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003389 clang::io::Emit16(Out, Lookup.size());
3390 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3391 I != E; ++I)
3392 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003393
3394 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3395 }
3396};
3397} // end anonymous namespace
3398
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003399/// \brief Write the block containing all of the declaration IDs
3400/// visible from the given DeclContext.
3401///
3402/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003403/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003404uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3405 DeclContext *DC) {
3406 if (DC->getPrimaryContext() != DC)
3407 return 0;
3408
3409 // Since there is no name lookup into functions or methods, don't bother to
3410 // build a visible-declarations table for these entities.
3411 if (DC->isFunctionOrMethod())
3412 return 0;
3413
3414 // If not in C++, we perform name lookup for the translation unit via the
3415 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003416 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003417 return 0;
3418
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003419 // Serialize the contents of the mapping used for lookup. Note that,
3420 // although we have two very different code paths, the serialized
3421 // representation is the same for both cases: a declaration name,
3422 // followed by a size, followed by references to the visible
3423 // declarations that have that name.
3424 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003425 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003426 if (!Map || Map->empty())
3427 return 0;
3428
3429 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3430 ASTDeclContextNameLookupTrait Trait(*this);
3431
3432 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003433 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003434 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003435 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3436 D != DEnd; ++D) {
3437 DeclarationName Name = D->first;
3438 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003439 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003440 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3441 // Hash all conversion function names to the same name. The actual
3442 // type information in conversion function name is not used in the
3443 // key (since such type information is not stable across different
3444 // modules), so the intended effect is to coalesce all of the conversion
3445 // functions under a single key.
3446 if (!ConversionName)
3447 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003448 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003449 continue;
3450 }
3451
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003452 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003453 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003454 }
3455
Douglas Gregor05ef9312011-08-30 20:49:19 +00003456 // Add the conversion functions
3457 if (!ConversionDecls.empty()) {
3458 Generator.insert(ConversionName,
3459 DeclContext::lookup_result(ConversionDecls.begin(),
3460 ConversionDecls.end()),
3461 Trait);
3462 }
3463
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003464 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003465 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003466 uint32_t BucketOffset;
3467 {
3468 llvm::raw_svector_ostream Out(LookupTable);
3469 // Make sure that no bucket is at offset 0
3470 clang::io::Emit32(Out, 0);
3471 BucketOffset = Generator.Emit(Out, Trait);
3472 }
3473
3474 // Write the lookup table
3475 RecordData Record;
3476 Record.push_back(DECL_CONTEXT_VISIBLE);
3477 Record.push_back(BucketOffset);
3478 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3479 LookupTable.str());
3480
3481 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3482 ++NumVisibleDeclContexts;
3483 return Offset;
3484}
3485
Sebastian Redla4071b42010-08-24 00:50:09 +00003486/// \brief Write an UPDATE_VISIBLE block for the given context.
3487///
3488/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3489/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003490/// (in C++), for namespaces, and for classes with forward-declared unscoped
3491/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003492void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003493 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3494 if (!Map || Map->empty())
3495 return;
3496
3497 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3498 ASTDeclContextNameLookupTrait Trait(*this);
3499
3500 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003501 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3502 D != DEnd; ++D) {
3503 DeclarationName Name = D->first;
3504 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003505 // For any name that appears in this table, the results are complete, i.e.
3506 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003507 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003508 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003509 }
3510
3511 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003512 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003513 uint32_t BucketOffset;
3514 {
3515 llvm::raw_svector_ostream Out(LookupTable);
3516 // Make sure that no bucket is at offset 0
3517 clang::io::Emit32(Out, 0);
3518 BucketOffset = Generator.Emit(Out, Trait);
3519 }
3520
3521 // Write the lookup table
3522 RecordData Record;
3523 Record.push_back(UPDATE_VISIBLE);
3524 Record.push_back(getDeclID(cast<Decl>(DC)));
3525 Record.push_back(BucketOffset);
3526 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3527}
3528
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003529/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3530void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3531 RecordData Record;
3532 Record.push_back(Opts.fp_contract);
3533 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3534}
3535
3536/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3537void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003538 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003539 return;
3540
3541 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3542 RecordData Record;
3543#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3544#include "clang/Basic/OpenCLExtensions.def"
3545 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3546}
3547
Douglas Gregor358cd442012-01-15 16:58:34 +00003548void ASTWriter::WriteRedeclarations() {
3549 RecordData LocalRedeclChains;
3550 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3551
3552 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3553 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003554 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003555
3556 Decl *MostRecent = First->getMostRecentDecl();
3557
3558 // If we only have a single declaration, there is no point in storing
3559 // a redeclaration chain.
3560 if (First == MostRecent)
3561 continue;
3562
3563 unsigned Offset = LocalRedeclChains.size();
3564 unsigned Size = 0;
3565 LocalRedeclChains.push_back(0); // Placeholder for the size.
3566
3567 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003568 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003569 Prev = Prev->getPreviousDecl()) {
3570 if (!Prev->isFromASTFile()) {
3571 AddDeclRef(Prev, LocalRedeclChains);
3572 ++Size;
3573 }
3574 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003575
3576 if (!First->isFromASTFile() && Chain) {
3577 Decl *FirstFromAST = MostRecent;
3578 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3579 if (Prev->isFromASTFile())
3580 FirstFromAST = Prev;
3581 }
3582
3583 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3584 }
3585
Douglas Gregor358cd442012-01-15 16:58:34 +00003586 LocalRedeclChains[Offset] = Size;
3587
3588 // Reverse the set of local redeclarations, so that we store them in
3589 // order (since we found them in reverse order).
3590 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3591
Douglas Gregor6168bd22013-02-18 15:53:43 +00003592 // Add the mapping from the first ID from the AST to the set of local
3593 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003594 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3595 LocalRedeclsMap.push_back(Info);
3596
3597 assert(N == Redeclarations.size() &&
3598 "Deserialized a declaration we shouldn't have");
3599 }
3600
3601 if (LocalRedeclChains.empty())
3602 return;
3603
3604 // Sort the local redeclarations map by the first declaration ID,
3605 // since the reader will be performing binary searches on this information.
3606 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3607
3608 // Emit the local redeclarations map.
3609 using namespace llvm;
3610 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3611 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3612 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3614 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3615
3616 RecordData Record;
3617 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3618 Record.push_back(LocalRedeclsMap.size());
3619 Stream.EmitRecordWithBlob(AbbrevID, Record,
3620 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3621 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3622
3623 // Emit the redeclaration chains.
3624 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3625}
3626
Douglas Gregor404cdde2012-01-27 01:47:08 +00003627void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003628 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003629 RecordData Categories;
3630
3631 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3632 unsigned Size = 0;
3633 unsigned StartIndex = Categories.size();
3634
3635 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3636
3637 // Allocate space for the size.
3638 Categories.push_back(0);
3639
3640 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003641 for (ObjCInterfaceDecl::known_categories_iterator
3642 Cat = Class->known_categories_begin(),
3643 CatEnd = Class->known_categories_end();
3644 Cat != CatEnd; ++Cat, ++Size) {
3645 assert(getDeclID(*Cat) != 0 && "Bogus category");
3646 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003647 }
3648
3649 // Update the size.
3650 Categories[StartIndex] = Size;
3651
3652 // Record this interface -> category map.
3653 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3654 CategoriesMap.push_back(CatInfo);
3655 }
3656
3657 // Sort the categories map by the definition ID, since the reader will be
3658 // performing binary searches on this information.
3659 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3660
3661 // Emit the categories map.
3662 using namespace llvm;
3663 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3664 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3667 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3668
3669 RecordData Record;
3670 Record.push_back(OBJC_CATEGORIES_MAP);
3671 Record.push_back(CategoriesMap.size());
3672 Stream.EmitRecordWithBlob(AbbrevID, Record,
3673 reinterpret_cast<char*>(CategoriesMap.data()),
3674 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3675
3676 // Emit the category lists.
3677 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3678}
3679
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003680void ASTWriter::WriteMergedDecls() {
3681 if (!Chain || Chain->MergedDecls.empty())
3682 return;
3683
3684 RecordData Record;
3685 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3686 IEnd = Chain->MergedDecls.end();
3687 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003688 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003689 : getDeclID(I->first);
3690 assert(CanonID && "Merged declaration not known?");
3691
3692 Record.push_back(CanonID);
3693 Record.push_back(I->second.size());
3694 Record.append(I->second.begin(), I->second.end());
3695 }
3696 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3697}
3698
Richard Smithe40f2ba2013-08-07 21:41:30 +00003699void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3700 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3701
3702 if (LPTMap.empty())
3703 return;
3704
3705 RecordData Record;
3706 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3707 ItEnd = LPTMap.end();
3708 It != ItEnd; ++It) {
3709 LateParsedTemplate *LPT = It->second;
3710 AddDeclRef(It->first, Record);
3711 AddDeclRef(LPT->D, Record);
3712 Record.push_back(LPT->Toks.size());
3713
3714 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3715 TokEnd = LPT->Toks.end();
3716 TokIt != TokEnd; ++TokIt) {
3717 AddToken(*TokIt, Record);
3718 }
3719 }
3720 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3721}
3722
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003723//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003724// General Serialization Routines
3725//===----------------------------------------------------------------------===//
3726
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003727/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003728void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3729 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003730 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003731 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3732 e = Attrs.end(); i != e; ++i){
3733 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003734 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003735 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003736
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003737#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003738
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003739 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003740}
3741
John McCallf413f5e2013-05-03 00:10:13 +00003742void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3743 AddSourceLocation(Tok.getLocation(), Record);
3744 Record.push_back(Tok.getLength());
3745
3746 // FIXME: When reading literal tokens, reconstruct the literal pointer
3747 // if it is needed.
3748 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3749 // FIXME: Should translate token kind to a stable encoding.
3750 Record.push_back(Tok.getKind());
3751 // FIXME: Should translate token flags to a stable encoding.
3752 Record.push_back(Tok.getFlags());
3753}
3754
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003755void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003756 Record.push_back(Str.size());
3757 Record.insert(Record.end(), Str.begin(), Str.end());
3758}
3759
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003760void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3761 RecordDataImpl &Record) {
3762 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003763 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003764 Record.push_back(*Minor + 1);
3765 else
3766 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003767 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003768 Record.push_back(*Subminor + 1);
3769 else
3770 Record.push_back(0);
3771}
3772
Douglas Gregore84a9da2009-04-20 20:36:09 +00003773/// \brief Note that the identifier II occurs at the given offset
3774/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003775void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003776 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003777 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003778 // up earlier in the chain and thus don't need an offset.
3779 if (ID >= FirstIdentID)
3780 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003781}
3782
Douglas Gregor95c13f52009-04-25 17:48:32 +00003783/// \brief Note that the selector Sel occurs at the given offset
3784/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003785void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003786 unsigned ID = SelectorIDs[Sel];
3787 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003788 // Don't record offsets for selectors that are also available in a different
3789 // file.
3790 if (ID < FirstSelectorID)
3791 return;
3792 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003793}
3794
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003795ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003796 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003797 WritingAST(false), DoneWritingDeclsAndTypes(false),
3798 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003799 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003800 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003801 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3802 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003803 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3804 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003805 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003806 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003807 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003808 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003809 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003810 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003811 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3812 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3813 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003814 DeclTypedefAbbrev(0),
3815 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3816 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003817{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003818}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003819
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003820ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003821 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003822}
3823
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003824void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003825 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003826 Module *WritingModule, StringRef isysroot,
3827 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003828 WritingAST = true;
3829
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003830 ASTHasCompilerErrors = hasErrors;
3831
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003832 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003833 Stream.Emit((unsigned)'C', 8);
3834 Stream.Emit((unsigned)'P', 8);
3835 Stream.Emit((unsigned)'C', 8);
3836 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003837
Chris Lattner28fa4e62009-04-26 22:26:21 +00003838 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003839
Douglas Gregoreda8e122011-08-09 15:13:55 +00003840 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003841 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003842 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003843 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003844 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003845 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003846 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003847
3848 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003849}
3850
Douglas Gregora94a1542011-07-27 21:45:57 +00003851template<typename Vector>
3852static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3853 ASTWriter::RecordData &Record) {
3854 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3855 I != E; ++I) {
3856 Writer.AddDeclRef(*I, Record);
3857 }
3858}
3859
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003860void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003861 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003862 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003863 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003864 using namespace llvm;
3865
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003866 bool isModule = WritingModule != 0;
3867
Douglas Gregorcf68c582011-12-01 22:20:10 +00003868 // Make sure that the AST reader knows to finalize itself.
3869 if (Chain)
3870 Chain->finalizeForWriting();
3871
Sebastian Redl143413f2010-07-12 22:02:52 +00003872 ASTContext &Context = SemaRef.Context;
3873 Preprocessor &PP = SemaRef.PP;
3874
Douglas Gregordab42432011-08-12 00:15:20 +00003875 // Set up predefined declaration IDs.
3876 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003877 if (Context.ObjCIdDecl)
3878 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003879 if (Context.ObjCSelDecl)
3880 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003881 if (Context.ObjCClassDecl)
3882 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003883 if (Context.ObjCProtocolClassDecl)
3884 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003885 if (Context.Int128Decl)
3886 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3887 if (Context.UInt128Decl)
3888 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003889 if (Context.ObjCInstanceTypeDecl)
3890 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003891 if (Context.BuiltinVaListDecl)
3892 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3893
Douglas Gregor851443c2011-08-12 01:39:19 +00003894 if (!Chain) {
3895 // Make sure that we emit IdentifierInfos (and any attached
3896 // declarations) for builtins. We don't need to do this when we're
3897 // emitting chained PCH files, because all of the builtins will be
3898 // in the original PCH file.
3899 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003900 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003901 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003902 if (!Context.getLangOpts().NoBuiltin) {
3903 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3904 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003905 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3906 getIdentifierRef(&Table.get(BuiltinNames[I]));
3907 }
3908
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003909 // If there are any out-of-date identifiers, bring them up to date.
3910 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003911 // Find out-of-date identifiers.
3912 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003913 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3914 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003915 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003916 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003917 OutOfDate.push_back(ID->second);
3918 }
3919
3920 // Update the out-of-date identifiers.
3921 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3922 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3923 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003924 }
3925
Chris Lattner0c797362009-09-08 18:19:27 +00003926 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003927 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003928 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003929 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003930 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003931
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003932 // Build a record containing all of the file scoped decls in this file.
3933 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003934 if (!isModule)
3935 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3936 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003937
Douglas Gregor851443c2011-08-12 01:39:19 +00003938 // Build a record containing all of the delegating constructors we still need
3939 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003940 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003941 if (!isModule)
3942 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003943
Douglas Gregor851443c2011-08-12 01:39:19 +00003944 // Write the set of weak, undeclared identifiers. We always write the
3945 // entire table, since later PCH files in a PCH chain are only interested in
3946 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003947 RecordData WeakUndeclaredIdentifiers;
3948 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003949 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003950 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3951 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3952 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3953 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3954 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3955 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3956 }
3957 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003958
Richard Smith78165b52013-01-10 23:43:47 +00003959 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003960 // declarations in this header file. Generally, this record will be
3961 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003962 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003963 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003964 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003965 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003966 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3967 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003968 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003969 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003970 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003971 }
3972
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003973 // Build a record containing all of the ext_vector declarations.
3974 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003975 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003976
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003977 // Build a record containing all of the VTable uses information.
3978 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003979 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003980 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3981 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3982 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3983 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3984 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003985 }
3986
3987 // Build a record containing all of dynamic classes declarations.
3988 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003989 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003990
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003991 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003992 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003993 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003994 I = SemaRef.PendingInstantiations.begin(),
3995 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3996 AddDeclRef(I->first, PendingInstantiations);
3997 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003998 }
3999 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4000 "There are local ones at end of translation unit!");
4001
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004002 // Build a record containing some declaration references.
4003 RecordData SemaDeclRefs;
4004 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4005 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4006 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4007 }
4008
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004009 RecordData CUDASpecialDeclRefs;
4010 if (Context.getcudaConfigureCallDecl()) {
4011 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4012 }
4013
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004014 // Build a record containing all of the known namespaces.
4015 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004016 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004017 I = SemaRef.KnownNamespaces.begin(),
4018 IEnd = SemaRef.KnownNamespaces.end();
4019 I != IEnd; ++I) {
4020 if (!I->second)
4021 AddDeclRef(I->first, KnownNamespaces);
4022 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004023
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004024 // Build a record of all used, undefined objects that require definitions.
4025 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004026
4027 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004028 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004029 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4030 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004031 AddDeclRef(I->first, UndefinedButUsed);
4032 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004033 }
4034
Douglas Gregor112b9072012-10-18 05:31:06 +00004035 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004036 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004037
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004038 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004039 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004040 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004041
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004042 // This is so that older clang versions, before the introduction
4043 // of the control block, can read and reject the newer PCH format.
4044 Record.clear();
4045 Record.push_back(VERSION_MAJOR);
4046 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4047
Douglas Gregor851443c2011-08-12 01:39:19 +00004048 // Create a lexical update block containing all of the declarations in the
4049 // translation unit that do not come from other AST files.
4050 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4051 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004052 for (const auto *I : TU->noload_decls()) {
4053 if (!I->isFromASTFile())
4054 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004055 }
4056
4057 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4058 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4059 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4060 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4061 Record.clear();
4062 Record.push_back(TU_UPDATE_LEXICAL);
4063 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4064 data(NewGlobalDecls));
4065
4066 // And a visible updates block for the translation unit.
4067 Abv = new llvm::BitCodeAbbrev();
4068 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4069 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4070 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4071 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4072 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4073 WriteDeclContextVisibleUpdate(TU);
4074
4075 // If the translation unit has an anonymous namespace, and we don't already
4076 // have an update block for it, write it as an update block.
4077 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4078 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4079 if (Record.empty()) {
4080 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004081 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004082 }
4083 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004084
4085 // Make sure visible decls, added to DeclContexts previously loaded from
4086 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004087 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004088 I = UpdatingVisibleDecls.begin(),
4089 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4090 GetDeclRef(*I);
4091 }
4092
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004093 // Make sure all decls associated with an identifier are registered for
4094 // serialization.
4095 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4096 IDEnd = PP.getIdentifierTable().end();
4097 ID != IDEnd; ++ID) {
4098 const IdentifierInfo *II = ID->second;
4099 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4100 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4101 DEnd = SemaRef.IdResolver.end();
4102 D != DEnd; ++D) {
4103 GetDeclRef(*D);
4104 }
4105 }
4106 }
4107
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00004108 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004109 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004110
Douglas Gregor5204bde2011-08-02 16:26:37 +00004111 // Form the record of special types.
4112 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004113 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004114 AddTypeRef(Context.getFILEType(), SpecialTypes);
4115 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4116 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4117 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4118 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004119 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004120 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004121
Douglas Gregor1970d882009-04-26 03:49:13 +00004122 // Keep writing types and declarations until all types and
4123 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00004124 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004125 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00004126 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4127 E = DeclsToRewrite.end();
4128 I != E; ++I)
4129 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004130 while (!DeclTypesToEmit.empty()) {
4131 DeclOrType DOT = DeclTypesToEmit.front();
4132 DeclTypesToEmit.pop();
4133 if (DOT.isType())
4134 WriteType(DOT.getType());
4135 else
4136 WriteDecl(Context, DOT.getDecl());
4137 }
4138 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004139
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004140 DoneWritingDeclsAndTypes = true;
4141
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004142 WriteFileDeclIDsMap();
4143 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00004144 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004145
4146 if (Chain) {
4147 // Write the mapping information describing our module dependencies and how
4148 // each of those modules were mapped into our own offset/ID space, so that
4149 // the reader can build the appropriate mapping to its own offset/ID space.
4150 // The map consists solely of a blob with the following format:
4151 // *(module-name-len:i16 module-name:len*i8
4152 // source-location-offset:i32
4153 // identifier-id:i32
4154 // preprocessed-entity-id:i32
4155 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004156 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004157 // selector-id:i32
4158 // declaration-id:i32
4159 // c++-base-specifiers-id:i32
4160 // type-id:i32)
4161 //
4162 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4163 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4165 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004166 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004167 {
4168 llvm::raw_svector_ostream Out(Buffer);
4169 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004170 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004171 M != MEnd; ++M) {
4172 StringRef FileName = (*M)->FileName;
4173 io::Emit16(Out, FileName.size());
4174 Out.write(FileName.data(), FileName.size());
4175 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4176 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004177 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004178 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004179 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004180 io::Emit32(Out, (*M)->BaseSelectorID);
4181 io::Emit32(Out, (*M)->BaseDeclID);
4182 io::Emit32(Out, (*M)->BaseTypeIndex);
4183 }
4184 }
4185 Record.clear();
4186 Record.push_back(MODULE_OFFSET_MAP);
4187 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4188 Buffer.data(), Buffer.size());
4189 }
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004190 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004191 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004192 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004193 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004194 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004195 WriteFPPragmaOptions(SemaRef.getFPOptions());
4196 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00004197
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004198 WriteTypeDeclOffsets();
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004199 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004200
Anders Carlsson9bb83e82011-03-06 18:41:18 +00004201 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004202
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004203 // If we're emitting a module, write out the submodule information.
4204 if (WritingModule)
4205 WriteSubmodules(WritingModule);
4206
Douglas Gregor5204bde2011-08-02 16:26:37 +00004207 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4208
Douglas Gregord4df8652009-04-22 22:02:47 +00004209 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004210 if (!EagerlyDeserializedDecls.empty())
4211 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004212
4213 // Write the record containing tentative definitions.
4214 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004215 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004216
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004217 // Write the record containing unused file scoped decls.
4218 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004219 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004220
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004221 // Write the record containing weak undeclared identifiers.
4222 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004223 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004224 WeakUndeclaredIdentifiers);
4225
Richard Smith78165b52013-01-10 23:43:47 +00004226 // Write the record containing locally-scoped extern "C" definitions.
4227 if (!LocallyScopedExternCDecls.empty())
4228 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4229 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004230
4231 // Write the record containing ext_vector type names.
4232 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004233 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004234
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004235 // Write the record containing VTable uses information.
4236 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004237 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004238
4239 // Write the record containing dynamic classes declarations.
4240 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004241 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004242
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004243 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004244 if (!PendingInstantiations.empty())
4245 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004246
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004247 // Write the record containing declaration references of Sema.
4248 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004249 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004250
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004251 // Write the record containing CUDA-specific declaration references.
4252 if (!CUDASpecialDeclRefs.empty())
4253 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004254
4255 // Write the delegating constructors.
4256 if (!DelegatingCtorDecls.empty())
4257 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004258
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004259 // Write the known namespaces.
4260 if (!KnownNamespaces.empty())
4261 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004262
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004263 // Write the undefined internal functions and variables, and inline functions.
4264 if (!UndefinedButUsed.empty())
4265 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004266
Douglas Gregor851443c2011-08-12 01:39:19 +00004267 // Write the visible updates to DeclContexts.
4268 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4269 I = UpdatedDeclContexts.begin(),
4270 E = UpdatedDeclContexts.end();
4271 I != E; ++I)
4272 WriteDeclContextVisibleUpdate(*I);
4273
Douglas Gregor959bb062011-12-03 01:15:29 +00004274 if (!WritingModule) {
4275 // Write the submodules that were imported, if any.
4276 RecordData ImportedModules;
4277 for (ASTContext::import_iterator I = Context.local_import_begin(),
4278 IEnd = Context.local_import_end();
4279 I != IEnd; ++I) {
4280 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4281 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4282 }
4283 if (!ImportedModules.empty()) {
4284 // Sort module IDs.
4285 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4286
4287 // Unique module IDs.
4288 ImportedModules.erase(std::unique(ImportedModules.begin(),
4289 ImportedModules.end()),
4290 ImportedModules.end());
4291
4292 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4293 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004294 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004295
Douglas Gregordab42432011-08-12 00:15:20 +00004296 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00004297 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004298 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004299 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004300 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004301 WriteLateParsedTemplates(SemaRef);
4302
Douglas Gregor08f01292009-04-17 22:13:46 +00004303 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004304 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004305 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004306 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004307 Record.push_back(NumLexicalDeclContexts);
4308 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004309 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004310 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004311}
4312
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004313/// \brief Go through the declaration update blocks and resolve declaration
4314/// pointers into declaration IDs.
4315void ASTWriter::ResolveDeclUpdatesBlocks() {
4316 for (DeclUpdateMap::iterator
4317 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4318 const Decl *D = I->first;
4319 UpdateRecord &URec = I->second;
4320
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004321 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004322 continue; // The decl will be written completely
4323
4324 unsigned Idx = 0, N = URec.size();
4325 while (Idx < N) {
4326 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004327 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4328 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4329 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4330 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4331 ++Idx;
4332 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004333
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004334 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman276dd182013-09-05 00:02:25 +00004335 case UPD_DECL_MARKED_USED:
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004336 ++Idx;
4337 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004338
4339 case UPD_CXX_DEDUCED_RETURN_TYPE:
4340 URec[Idx] = GetOrCreateTypeID(
4341 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4342 ++Idx;
4343 break;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004344 }
4345 }
4346 }
4347}
4348
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004349void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004350 if (DeclUpdates.empty())
4351 return;
4352
4353 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00004354 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004355 for (DeclUpdateMap::iterator
4356 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4357 const Decl *D = I->first;
4358 UpdateRecord &URec = I->second;
4359
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004360 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004361 continue; // The decl will be written completely,no need to store updates.
4362
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004363 uint64_t Offset = Stream.GetCurrentBitNo();
4364 Stream.EmitRecord(DECL_UPDATES, URec);
4365
4366 OffsetsRecord.push_back(GetDeclRef(D));
4367 OffsetsRecord.push_back(Offset);
4368 }
4369 Stream.ExitBlock();
4370 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4371}
4372
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004373void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004374 if (ReplacedDecls.empty())
4375 return;
4376
4377 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004378 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4379 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004380 Record.push_back(I->ID);
4381 Record.push_back(I->Offset);
4382 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004383 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004384 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004385}
4386
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004387void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004388 Record.push_back(Loc.getRawEncoding());
4389}
4390
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004391void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004392 AddSourceLocation(Range.getBegin(), Record);
4393 AddSourceLocation(Range.getEnd(), Record);
4394}
4395
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004396void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004397 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004398 const uint64_t *Words = Value.getRawData();
4399 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004400}
4401
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004402void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004403 Record.push_back(Value.isUnsigned());
4404 AddAPInt(Value, Record);
4405}
4406
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004407void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004408 AddAPInt(Value.bitcastToAPInt(), Record);
4409}
4410
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004411void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004412 Record.push_back(getIdentifierRef(II));
4413}
4414
Sebastian Redl539c5062010-08-18 23:57:32 +00004415IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004416 if (II == 0)
4417 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004418
Sebastian Redl539c5062010-08-18 23:57:32 +00004419 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004420 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004421 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004422 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004423}
4424
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004425MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004426 // Don't emit builtin macros like __LINE__ to the AST file unless they
4427 // have been redefined by the header (in which case they are not
4428 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004429 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004430 return 0;
4431
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004432 MacroID &ID = MacroIDs[MI];
4433 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004434 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004435 MacroInfoToEmitData Info = { Name, MI, ID };
4436 MacroInfosToEmit.push_back(Info);
4437 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004438 return ID;
4439}
4440
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004441MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4442 if (MI == 0 || MI->isBuiltinMacro())
4443 return 0;
4444
4445 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4446 return MacroIDs[MI];
4447}
4448
4449uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4450 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4451 return IdentMacroDirectivesOffsetMap[Name];
4452}
4453
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004454void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004455 Record.push_back(getSelectorRef(SelRef));
4456}
4457
Sebastian Redl539c5062010-08-18 23:57:32 +00004458SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004459 if (Sel.getAsOpaquePtr() == 0) {
4460 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004461 }
4462
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004463 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004464 if (SID == 0 && Chain) {
4465 // This might trigger a ReadSelector callback, which will set the ID for
4466 // this selector.
4467 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004468 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004469 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004470 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004471 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004472 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004473 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004474 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004475}
4476
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004477void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004478 AddDeclRef(Temp->getDestructor(), Record);
4479}
4480
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004481void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4482 CXXBaseSpecifier const *BasesEnd,
4483 RecordDataImpl &Record) {
4484 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4485 CXXBaseSpecifiersToWrite.push_back(
4486 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4487 Bases, BasesEnd));
4488 Record.push_back(NextCXXBaseSpecifiersID++);
4489}
4490
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004491void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004492 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004493 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004494 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004495 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004496 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004497 break;
4498 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004499 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004500 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004501 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004502 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004503 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004504 break;
4505 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004506 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004507 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004508 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004509 break;
John McCall0ad16662009-10-29 08:12:44 +00004510 case TemplateArgument::Null:
4511 case TemplateArgument::Integral:
4512 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004513 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004514 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004515 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004516 break;
4517 }
4518}
4519
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004520void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004521 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004522 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004523
4524 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4525 bool InfoHasSameExpr
4526 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4527 Record.push_back(InfoHasSameExpr);
4528 if (InfoHasSameExpr)
4529 return; // Avoid storing the same expr twice.
4530 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004531 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4532 Record);
4533}
4534
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004535void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4536 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004537 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004538 AddTypeRef(QualType(), Record);
4539 return;
4540 }
4541
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004542 AddTypeLoc(TInfo->getTypeLoc(), Record);
4543}
4544
4545void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4546 AddTypeRef(TL.getType(), Record);
4547
John McCall8f115c62009-10-16 21:56:05 +00004548 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004549 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004550 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004551}
4552
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004553void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004554 Record.push_back(GetOrCreateTypeID(T));
4555}
4556
Douglas Gregoreda8e122011-08-09 15:13:55 +00004557TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004558 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004559 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004560 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4561}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004562
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004563TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004564 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004565 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004566 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004567}
4568
4569TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4570 if (T.isNull())
4571 return TypeIdx();
4572 assert(!T.getLocalFastQualifiers());
4573
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004574 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004575 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004576 if (DoneWritingDeclsAndTypes) {
4577 assert(0 && "New type seen after serializing all the types to emit!");
4578 return TypeIdx();
4579 }
4580
Douglas Gregor1970d882009-04-26 03:49:13 +00004581 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004582 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004583 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004584 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004585 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004586 return Idx;
4587}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004588
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004589TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004590 if (T.isNull())
4591 return TypeIdx();
4592 assert(!T.getLocalFastQualifiers());
4593
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004594 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4595 assert(I != TypeIdxs.end() && "Type not emitted!");
4596 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004597}
4598
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004599void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004600 Record.push_back(GetDeclRef(D));
4601}
4602
Sebastian Redl539c5062010-08-18 23:57:32 +00004603DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004604 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4605
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004606 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004607 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004608 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004609
4610 // If D comes from an AST file, its declaration ID is already known and
4611 // fixed.
4612 if (D->isFromASTFile())
4613 return D->getGlobalID();
4614
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004615 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004616 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004617 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004618 if (DoneWritingDeclsAndTypes) {
4619 assert(0 && "New decl seen after serializing all the decls to emit!");
4620 return 0;
4621 }
4622
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004623 // We haven't seen this declaration before. Give it a new ID and
4624 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004625 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004626 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004627 }
4628
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004629 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004630}
4631
Sebastian Redl539c5062010-08-18 23:57:32 +00004632DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004633 if (D == 0)
4634 return 0;
4635
Douglas Gregorb3163e52012-01-05 22:33:30 +00004636 // If D comes from an AST file, its declaration ID is already known and
4637 // fixed.
4638 if (D->isFromASTFile())
4639 return D->getGlobalID();
4640
Douglas Gregore84a9da2009-04-20 20:36:09 +00004641 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4642 return DeclIDs[D];
4643}
4644
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004645void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004646 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004647 assert(D);
4648
4649 SourceLocation Loc = D->getLocation();
4650 if (Loc.isInvalid())
4651 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004652
4653 // We only keep track of the file-level declarations of each file.
4654 if (!D->getLexicalDeclContext()->isFileContext())
4655 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004656 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4657 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004658 if (isa<ParmVarDecl>(D))
4659 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004660
4661 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004662 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004663 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004664 FileID FID;
4665 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004666 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004667 if (FID.isInvalid())
4668 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004669 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004670
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004671 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004672 if (!Info)
4673 Info = new DeclIDInFileInfo();
4674
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004675 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004676 LocDeclIDsTy &Decls = Info->DeclIDs;
4677
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004678 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004679 Decls.push_back(LocDecl);
4680 return;
4681 }
4682
Benjamin Kramer45025c02013-08-24 13:22:59 +00004683 LocDeclIDsTy::iterator I =
4684 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004685
4686 Decls.insert(I, LocDecl);
4687}
4688
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004689void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004690 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004691 Record.push_back(Name.getNameKind());
4692 switch (Name.getNameKind()) {
4693 case DeclarationName::Identifier:
4694 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4695 break;
4696
4697 case DeclarationName::ObjCZeroArgSelector:
4698 case DeclarationName::ObjCOneArgSelector:
4699 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004700 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004701 break;
4702
4703 case DeclarationName::CXXConstructorName:
4704 case DeclarationName::CXXDestructorName:
4705 case DeclarationName::CXXConversionFunctionName:
4706 AddTypeRef(Name.getCXXNameType(), Record);
4707 break;
4708
4709 case DeclarationName::CXXOperatorName:
4710 Record.push_back(Name.getCXXOverloadedOperator());
4711 break;
4712
Alexis Hunt3d221f22009-11-29 07:34:05 +00004713 case DeclarationName::CXXLiteralOperatorName:
4714 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4715 break;
4716
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004717 case DeclarationName::CXXUsingDirective:
4718 // No extra data to emit
4719 break;
4720 }
4721}
Chris Lattnerca025db2010-05-07 21:43:38 +00004722
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004723void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004724 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004725 switch (Name.getNameKind()) {
4726 case DeclarationName::CXXConstructorName:
4727 case DeclarationName::CXXDestructorName:
4728 case DeclarationName::CXXConversionFunctionName:
4729 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4730 break;
4731
4732 case DeclarationName::CXXOperatorName:
4733 AddSourceLocation(
4734 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4735 Record);
4736 AddSourceLocation(
4737 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4738 Record);
4739 break;
4740
4741 case DeclarationName::CXXLiteralOperatorName:
4742 AddSourceLocation(
4743 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4744 Record);
4745 break;
4746
4747 case DeclarationName::Identifier:
4748 case DeclarationName::ObjCZeroArgSelector:
4749 case DeclarationName::ObjCOneArgSelector:
4750 case DeclarationName::ObjCMultiArgSelector:
4751 case DeclarationName::CXXUsingDirective:
4752 break;
4753 }
4754}
4755
4756void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004757 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004758 AddDeclarationName(NameInfo.getName(), Record);
4759 AddSourceLocation(NameInfo.getLoc(), Record);
4760 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4761}
4762
4763void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004764 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004765 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004766 Record.push_back(Info.NumTemplParamLists);
4767 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4768 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4769}
4770
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004771void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004772 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004773 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004774 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004775 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004776
4777 // Push each of the NNS's onto a stack for serialization in reverse order.
4778 while (NNS) {
4779 NestedNames.push_back(NNS);
4780 NNS = NNS->getPrefix();
4781 }
4782
4783 Record.push_back(NestedNames.size());
4784 while(!NestedNames.empty()) {
4785 NNS = NestedNames.pop_back_val();
4786 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4787 Record.push_back(Kind);
4788 switch (Kind) {
4789 case NestedNameSpecifier::Identifier:
4790 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4791 break;
4792
4793 case NestedNameSpecifier::Namespace:
4794 AddDeclRef(NNS->getAsNamespace(), Record);
4795 break;
4796
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004797 case NestedNameSpecifier::NamespaceAlias:
4798 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4799 break;
4800
Chris Lattnerca025db2010-05-07 21:43:38 +00004801 case NestedNameSpecifier::TypeSpec:
4802 case NestedNameSpecifier::TypeSpecWithTemplate:
4803 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4804 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4805 break;
4806
4807 case NestedNameSpecifier::Global:
4808 // Don't need to write an associated value.
4809 break;
4810 }
4811 }
4812}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004813
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004814void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4815 RecordDataImpl &Record) {
4816 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004817 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004818 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004819
4820 // Push each of the nested-name-specifiers's onto a stack for
4821 // serialization in reverse order.
4822 while (NNS) {
4823 NestedNames.push_back(NNS);
4824 NNS = NNS.getPrefix();
4825 }
4826
4827 Record.push_back(NestedNames.size());
4828 while(!NestedNames.empty()) {
4829 NNS = NestedNames.pop_back_val();
4830 NestedNameSpecifier::SpecifierKind Kind
4831 = NNS.getNestedNameSpecifier()->getKind();
4832 Record.push_back(Kind);
4833 switch (Kind) {
4834 case NestedNameSpecifier::Identifier:
4835 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4836 AddSourceRange(NNS.getLocalSourceRange(), Record);
4837 break;
4838
4839 case NestedNameSpecifier::Namespace:
4840 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4841 AddSourceRange(NNS.getLocalSourceRange(), Record);
4842 break;
4843
4844 case NestedNameSpecifier::NamespaceAlias:
4845 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4846 AddSourceRange(NNS.getLocalSourceRange(), Record);
4847 break;
4848
4849 case NestedNameSpecifier::TypeSpec:
4850 case NestedNameSpecifier::TypeSpecWithTemplate:
4851 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4852 AddTypeLoc(NNS.getTypeLoc(), Record);
4853 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4854 break;
4855
4856 case NestedNameSpecifier::Global:
4857 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4858 break;
4859 }
4860 }
4861}
4862
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004863void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004864 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004865 Record.push_back(Kind);
4866 switch (Kind) {
4867 case TemplateName::Template:
4868 AddDeclRef(Name.getAsTemplateDecl(), Record);
4869 break;
4870
4871 case TemplateName::OverloadedTemplate: {
4872 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4873 Record.push_back(OvT->size());
4874 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4875 I != E; ++I)
4876 AddDeclRef(*I, Record);
4877 break;
4878 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004879
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004880 case TemplateName::QualifiedTemplate: {
4881 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4882 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4883 Record.push_back(QualT->hasTemplateKeyword());
4884 AddDeclRef(QualT->getTemplateDecl(), Record);
4885 break;
4886 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004887
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004888 case TemplateName::DependentTemplate: {
4889 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4890 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4891 Record.push_back(DepT->isIdentifier());
4892 if (DepT->isIdentifier())
4893 AddIdentifierRef(DepT->getIdentifier(), Record);
4894 else
4895 Record.push_back(DepT->getOperator());
4896 break;
4897 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004898
4899 case TemplateName::SubstTemplateTemplateParm: {
4900 SubstTemplateTemplateParmStorage *subst
4901 = Name.getAsSubstTemplateTemplateParm();
4902 AddDeclRef(subst->getParameter(), Record);
4903 AddTemplateName(subst->getReplacement(), Record);
4904 break;
4905 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004906
4907 case TemplateName::SubstTemplateTemplateParmPack: {
4908 SubstTemplateTemplateParmPackStorage *SubstPack
4909 = Name.getAsSubstTemplateTemplateParmPack();
4910 AddDeclRef(SubstPack->getParameterPack(), Record);
4911 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4912 break;
4913 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004914 }
4915}
4916
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004917void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004918 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004919 Record.push_back(Arg.getKind());
4920 switch (Arg.getKind()) {
4921 case TemplateArgument::Null:
4922 break;
4923 case TemplateArgument::Type:
4924 AddTypeRef(Arg.getAsType(), Record);
4925 break;
4926 case TemplateArgument::Declaration:
4927 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004928 Record.push_back(Arg.isDeclForReferenceParam());
4929 break;
4930 case TemplateArgument::NullPtr:
4931 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004932 break;
4933 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004934 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004935 AddTypeRef(Arg.getIntegralType(), Record);
4936 break;
4937 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004938 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4939 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004940 case TemplateArgument::TemplateExpansion:
4941 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004942 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004943 Record.push_back(*NumExpansions + 1);
4944 else
4945 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004946 break;
4947 case TemplateArgument::Expression:
4948 AddStmt(Arg.getAsExpr());
4949 break;
4950 case TemplateArgument::Pack:
4951 Record.push_back(Arg.pack_size());
4952 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4953 I != E; ++I)
4954 AddTemplateArgument(*I, Record);
4955 break;
4956 }
4957}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004958
4959void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004960ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004961 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004962 assert(TemplateParams && "No TemplateParams!");
4963 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4964 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4965 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4966 Record.push_back(TemplateParams->size());
4967 for (TemplateParameterList::const_iterator
4968 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4969 P != PEnd; ++P)
4970 AddDeclRef(*P, Record);
4971}
4972
4973/// \brief Emit a template argument list.
4974void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004975ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004976 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004977 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004978 Record.push_back(TemplateArgs->size());
4979 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004980 AddTemplateArgument(TemplateArgs->get(i), Record);
4981}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004982
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00004983void
4984ASTWriter::AddASTTemplateArgumentListInfo
4985(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4986 assert(ASTTemplArgList && "No ASTTemplArgList!");
4987 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4988 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4989 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4990 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4991 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4992 AddTemplateArgumentLoc(TemplArgs[i], Record);
4993}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004994
4995void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004996ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004997 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004998 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004999 I = Set.begin(), E = Set.end(); I != E; ++I) {
5000 AddDeclRef(I.getDecl(), Record);
5001 Record.push_back(I.getAccess());
5002 }
5003}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005004
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005005void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005006 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005007 Record.push_back(Base.isVirtual());
5008 Record.push_back(Base.isBaseOfClass());
5009 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005010 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005011 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005012 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005013 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5014 : SourceLocation(),
5015 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005016}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005017
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005018void ASTWriter::FlushCXXBaseSpecifiers() {
5019 RecordData Record;
5020 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5021 Record.clear();
5022
5023 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005024 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005025 if (Index == CXXBaseSpecifiersOffsets.size())
5026 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5027 else {
5028 if (Index > CXXBaseSpecifiersOffsets.size())
5029 CXXBaseSpecifiersOffsets.resize(Index + 1);
5030 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5031 }
5032
5033 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5034 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5035 Record.push_back(BEnd - B);
5036 for (; B != BEnd; ++B)
5037 AddCXXBaseSpecifier(*B, Record);
5038 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005039
5040 // Flush any expressions that were written as part of the base specifiers.
5041 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005042 }
5043
5044 CXXBaseSpecifiersToWrite.clear();
5045}
5046
Alexis Hunt1d792652011-01-08 20:30:50 +00005047void ASTWriter::AddCXXCtorInitializers(
5048 const CXXCtorInitializer * const *CtorInitializers,
5049 unsigned NumCtorInitializers,
5050 RecordDataImpl &Record) {
5051 Record.push_back(NumCtorInitializers);
5052 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5053 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005054
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005055 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005056 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005057 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005058 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005059 } else if (Init->isDelegatingInitializer()) {
5060 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005061 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005062 } else if (Init->isMemberInitializer()){
5063 Record.push_back(CTOR_INITIALIZER_MEMBER);
5064 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005065 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005066 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5067 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005068 }
Francois Pichetd583da02010-12-04 09:14:42 +00005069
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005070 AddSourceLocation(Init->getMemberLocation(), Record);
5071 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005072 AddSourceLocation(Init->getLParenLoc(), Record);
5073 AddSourceLocation(Init->getRParenLoc(), Record);
5074 Record.push_back(Init->isWritten());
5075 if (Init->isWritten()) {
5076 Record.push_back(Init->getSourceOrder());
5077 } else {
5078 Record.push_back(Init->getNumArrayIndices());
5079 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5080 AddDeclRef(Init->getArrayIndex(i), Record);
5081 }
5082 }
5083}
5084
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005085void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5086 assert(D->DefinitionData);
5087 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005088 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005089 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005090 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005091 Record.push_back(Data.Aggregate);
5092 Record.push_back(Data.PlainOldData);
5093 Record.push_back(Data.Empty);
5094 Record.push_back(Data.Polymorphic);
5095 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005096 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005097 Record.push_back(Data.HasNoNonEmptyBases);
5098 Record.push_back(Data.HasPrivateFields);
5099 Record.push_back(Data.HasProtectedFields);
5100 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005101 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005102 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005103 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005104 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005105 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005106 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5107 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5108 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5109 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5110 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5111 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005112 Record.push_back(Data.HasTrivialSpecialMembers);
5113 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005114 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005115 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005116 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005117 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005118 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005119 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005120 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005121 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5122 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5123 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5124 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005125 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005126
5127 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005128 if (Data.NumBases > 0)
5129 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5130 Record);
5131
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005132 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5133 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005134 if (Data.NumVBases > 0)
5135 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5136 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005137
Richard Smitha4ba74c2013-08-30 04:46:40 +00005138 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5139 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005140 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005141 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005142
5143 // Add lambda-specific data.
5144 if (Data.IsLambda) {
5145 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005146 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005147 Record.push_back(Lambda.IsGenericLambda);
5148 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005149 Record.push_back(Lambda.NumCaptures);
5150 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005151 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005152 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005153 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005154 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5155 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5156 AddSourceLocation(Capture.getLocation(), Record);
5157 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005158 Record.push_back(Capture.getCaptureKind());
5159 switch (Capture.getCaptureKind()) {
5160 case LCK_This:
5161 break;
5162 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005163 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005164 VarDecl *Var =
5165 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5166 AddDeclRef(Var, Record);
5167 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5168 : SourceLocation(),
5169 Record);
5170 break;
5171 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005172 }
5173 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005174}
5175
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005176void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005177 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005178 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005179 assert(FirstDeclID == NextDeclID &&
5180 FirstTypeID == NextTypeID &&
5181 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005182 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005183 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005184 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005185 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005186
Sebastian Redl07a89a82010-07-30 00:29:29 +00005187 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005188
Douglas Gregordf0c1512011-08-18 04:12:04 +00005189 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5190 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5191 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005192 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005193 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005194 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005195 NextDeclID = FirstDeclID;
5196 NextTypeID = FirstTypeID;
5197 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005198 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005199 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005200 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005201}
5202
Sebastian Redl539c5062010-08-18 23:57:32 +00005203void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005204 // Always keep the highest ID. See \p TypeRead() for more information.
5205 IdentID &StoredID = IdentifierIDs[II];
5206 if (ID > StoredID)
5207 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005208}
5209
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005210void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005211 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005212 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005213 if (ID > StoredID)
5214 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005215}
5216
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005217void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005218 // Always take the highest-numbered type index. This copes with an interesting
5219 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005220 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005221 // keep the higher-numbered entry so that we can properly write it out to
5222 // the AST file.
5223 TypeIdx &StoredIdx = TypeIdxs[T];
5224 if (Idx.getIndex() >= StoredIdx.getIndex())
5225 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005226}
5227
Sebastian Redl539c5062010-08-18 23:57:32 +00005228void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005229 // Always keep the highest ID. See \p TypeRead() for more information.
5230 SelectorID &StoredID = SelectorIDs[S];
5231 if (ID > StoredID)
5232 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005233}
Douglas Gregor91096292010-10-02 19:29:26 +00005234
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005235void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005236 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005237 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005238 MacroDefinitions[MD] = ID;
5239}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005240
Douglas Gregore37a85a2011-12-02 17:30:13 +00005241void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5242 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5243 SubmoduleIDs[Mod] = ID;
5244}
5245
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005246void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005247 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005248 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005249 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5250 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005251 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005252 // A forward reference was mutated into a definition. Rewrite it.
5253 // FIXME: This happens during template instantiation, should we
5254 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005255 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005256 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005257 }
5258}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005259
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005260void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005261 assert(!WritingAST && "Already writing the AST!");
5262
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005263 // TU and namespaces are handled elsewhere.
5264 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5265 return;
5266
Douglas Gregorb3722e22011-09-09 23:01:35 +00005267 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005268 return; // Not a source decl added to a DeclContext from PCH.
5269
Douglas Gregor9f782892013-01-21 15:25:38 +00005270 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005271 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005272 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005273}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005274
5275void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005276 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005277 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005278 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005279 return; // Not a source member added to a class from PCH.
5280 if (!isa<CXXMethodDecl>(D))
5281 return; // We are interested in lazily declared implicit methods.
5282
5283 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005284 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005285 UpdateRecord &Record = DeclUpdates[RD];
5286 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005287 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005288}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005289
5290void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5291 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005292 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005293 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005294 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005295 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005296 return; // Not a source specialization added to a template from PCH.
5297
5298 UpdateRecord &Record = DeclUpdates[TD];
5299 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005300 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005301}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005302
Larisse Voufo39a1e502013-08-06 01:03:05 +00005303void ASTWriter::AddedCXXTemplateSpecialization(
5304 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5305 // The specializations set is kept in the canonical template.
5306 assert(!WritingAST && "Already writing the AST!");
5307 TD = TD->getCanonicalDecl();
5308 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5309 return; // Not a source specialization added to a template from PCH.
5310
5311 UpdateRecord &Record = DeclUpdates[TD];
5312 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5313 Record.push_back(reinterpret_cast<uint64_t>(D));
5314}
5315
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005316void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5317 const FunctionDecl *D) {
5318 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005319 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005320 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005321 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005322 return; // Not a source specialization added to a template from PCH.
5323
5324 UpdateRecord &Record = DeclUpdates[TD];
5325 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005326 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005327}
5328
Richard Smith1fa5d642013-05-11 05:45:24 +00005329void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5330 assert(!WritingAST && "Already writing the AST!");
5331 FD = FD->getCanonicalDecl();
5332 if (!FD->isFromASTFile())
5333 return; // Not a function declared in PCH and defined outside.
5334
5335 UpdateRecord &Record = DeclUpdates[FD];
5336 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5337 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5338}
5339
Sebastian Redlab238a72011-04-24 16:28:06 +00005340void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *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 Redlab238a72011-04-24 16:28:06 +00005343 return; // Declaration not imported from PCH.
5344
5345 // Implicit decl from a PCH was defined.
5346 // FIXME: Should implicit definition be a separate FunctionDecl?
5347 RewriteDecl(D);
5348}
5349
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005350void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005351 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005352 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005353 return;
5354
5355 // Since the actual instantiation is delayed, this really means that we need
5356 // to update the instantiation location.
5357 UpdateRecord &Record = DeclUpdates[D];
5358 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5359 AddSourceLocation(
5360 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5361}
5362
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005363void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5364 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005365 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005366 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005367 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005368
5369 assert(IFD->getDefinition() && "Category on a class without a definition?");
5370 ObjCClassesWithCategories.insert(
5371 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005372}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005373
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005374
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005375void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5376 const ObjCPropertyDecl *OrigProp,
5377 const ObjCCategoryDecl *ClassExt) {
5378 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5379 if (!D)
5380 return;
5381
5382 assert(!WritingAST && "Already writing the AST!");
5383 if (!D->isFromASTFile())
5384 return; // Declaration not imported from PCH.
5385
5386 RewriteDecl(D);
5387}
Eli Friedman276dd182013-09-05 00:02:25 +00005388
5389void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5390 assert(!WritingAST && "Already writing the AST!");
5391 if (!D->isFromASTFile())
5392 return;
5393
5394 UpdateRecord &Record = DeclUpdates[D];
5395 Record.push_back(UPD_DECL_MARKED_USED);
5396}