blob: 30eaa3a431639be00b11fe33295f12b62d709a34 [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"
Richard Smith961eae52014-03-25 01:14:22 +000020#include "clang/AST/DeclLookups.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000027#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000028#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000029#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000030#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000031#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000032#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000034#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000035#include "clang/Lex/HeaderSearch.h"
36#include "clang/Lex/HeaderSearchOptions.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Lex/PreprocessingRecord.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Lex/PreprocessorOptions.h"
41#include "clang/Sema/IdentifierResolver.h"
42#include "clang/Sema/Sema.h"
43#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000044#include "llvm/ADT/APFloat.h"
45#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000046#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000047#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000049#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000050#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000051#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000052#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000053#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000054#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000055#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000056using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000057using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000058
Sebastian Redl3df5a082010-07-30 17:03:48 +000059template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000060static StringRef data(const std::vector<T, Allocator> &v) {
61 if (v.empty()) return StringRef();
62 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000063 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000064}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000065
66template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067static StringRef data(const SmallVectorImpl<T> &v) {
68 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000069 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000070}
71
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072//===----------------------------------------------------------------------===//
73// Type serialization
74//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000075
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000077 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000078 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000079 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080
81 public:
82 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000083 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000084
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000085 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000086 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087
88 void VisitArrayType(const ArrayType *T);
89 void VisitFunctionType(const FunctionType *T);
90 void VisitTagType(const TagType *T);
91
92#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
93#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094#include "clang/AST/TypeNodes.def"
95 };
96}
97
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000098void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000099 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Reid Kleckner8a365022013-06-24 17:51:48 +0000112void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
113 Writer.AddTypeRef(T->getOriginalType(), Record);
114 Code = TYPE_DECAYED;
115}
116
Reid Kleckner0503a872013-12-05 01:23:43 +0000117void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
118 Writer.AddTypeRef(T->getOriginalType(), Record);
119 Writer.AddTypeRef(T->getAdjustedType(), Record);
120 Code = TYPE_ADJUSTED;
121}
122
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000125 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000129 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
130 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000135 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000140 Writer.AddTypeRef(T->getPointeeType(), Record);
141 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000142 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143}
144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146 Writer.AddTypeRef(T->getElementType(), Record);
147 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000148 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000149}
150
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000151void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152 VisitArrayType(T);
153 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000154 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155}
156
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000164 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
165 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000166 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000167 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168}
169
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171 Writer.AddTypeRef(T->getElementType(), Record);
172 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000173 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000174 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000183 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000184 FunctionType::ExtInfo C = T->getExtInfo();
185 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000186 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000187 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000188 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000189 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000190 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000191}
192
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000193void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000194 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000195 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000196}
197
Richard Smith564417a2014-03-20 21:47:22 +0000198static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
199 ASTWriter::RecordDataImpl &Record) {
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000200 Record.push_back(T->getExceptionSpecType());
201 if (T->getExceptionSpecType() == EST_Dynamic) {
202 Record.push_back(T->getNumExceptions());
203 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
204 Writer.AddTypeRef(T->getExceptionType(I), Record);
205 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
206 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000207 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
208 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
209 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000210 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
211 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000212 }
Richard Smith564417a2014-03-20 21:47:22 +0000213}
214
215void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
216 VisitFunctionType(T);
217 Record.push_back(T->getNumParams());
218 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
219 Writer.AddTypeRef(T->getParamType(I), Record);
220 Record.push_back(T->isVariadic());
221 Record.push_back(T->hasTrailingReturn());
222 Record.push_back(T->getTypeQuals());
223 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
224 addExceptionSpec(Writer, T, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000225 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226}
227
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000229 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000231}
John McCallb96ec562009-12-04 22:46:56 +0000232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000235 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
236 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000237 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000238}
239
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000240void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000241 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000242 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243}
244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000245void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000251 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000252 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000253 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000254}
255
Alexis Hunte852b102011-05-24 22:41:36 +0000256void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
257 Writer.AddTypeRef(T->getBaseType(), Record);
258 Writer.AddTypeRef(T->getUnderlyingType(), Record);
259 Record.push_back(T->getUTTKind());
260 Code = TYPE_UNARY_TRANSFORM;
261}
262
Richard Smith30482bc2011-02-20 03:19:35 +0000263void ASTTypeWriter::VisitAutoType(const AutoType *T) {
264 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000265 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000266 if (T->getDeducedType().isNull())
267 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000268 Code = TYPE_AUTO;
269}
270
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000271void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000272 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000273 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000274 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000275 "Cannot serialize in the middle of a type definition");
276}
277
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000278void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000279 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000280 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000281}
282
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000283void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000284 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000285 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000286}
287
John McCall81904512011-01-06 01:58:22 +0000288void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
289 Writer.AddTypeRef(T->getModifiedType(), Record);
290 Writer.AddTypeRef(T->getEquivalentType(), Record);
291 Record.push_back(T->getAttrKind());
292 Code = TYPE_ATTRIBUTED;
293}
294
Mike Stump11289f42009-09-09 15:08:12 +0000295void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000296ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000297 const SubstTemplateTypeParmType *T) {
298 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
299 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000301}
302
303void
Douglas Gregorada4b792011-01-14 02:55:32 +0000304ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
305 const SubstTemplateTypeParmPackType *T) {
306 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
307 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
308 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000313 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000314 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000315 Writer.AddTemplateName(T->getTemplateName(), Record);
316 Record.push_back(T->getNumArgs());
317 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
318 ArgI != ArgE; ++ArgI)
319 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000320 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
321 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000322 : T->getCanonicalTypeInternal(),
323 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000324 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000325}
326
327void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000328ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000329 VisitArrayType(T);
330 Writer.AddStmt(T->getSizeExpr());
331 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000332 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000333}
334
335void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000336ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000337 const DependentSizedExtVectorType *T) {
338 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000339 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000340}
341
342void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000343ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000344 Record.push_back(T->getDepth());
345 Record.push_back(T->getIndex());
346 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000347 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000349}
350
351void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000352ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000353 Record.push_back(T->getKeyword());
354 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
355 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000356 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
357 : T->getCanonicalTypeInternal(),
358 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000359 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000360}
361
362void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000364 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000365 Record.push_back(T->getKeyword());
366 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
367 Writer.AddIdentifierRef(T->getIdentifier(), Record);
368 Record.push_back(T->getNumArgs());
369 for (DependentTemplateSpecializationType::iterator
370 I = T->begin(), E = T->end(); I != E; ++I)
371 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000372 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000373}
374
Douglas Gregord2fa7662010-12-20 02:24:11 +0000375void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
376 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000377 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000378 Record.push_back(*NumExpansions + 1);
379 else
380 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000381 Code = TYPE_PACK_EXPANSION;
382}
383
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000384void ASTTypeWriter::VisitParenType(const ParenType *T) {
385 Writer.AddTypeRef(T->getInnerType(), Record);
386 Code = TYPE_PAREN;
387}
388
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000389void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000390 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000391 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
392 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000393 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000394}
395
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000396void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000397 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000398 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000399 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000400}
401
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000402void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000403 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000404 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000405}
406
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000407void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000408 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000409 Record.push_back(T->getNumProtocols());
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000410 for (const auto *I : T->quals())
411 Writer.AddDeclRef(I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000412 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000413}
414
Steve Narofffb4330f2009-06-17 22:40:22 +0000415void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000416ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000417 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000418 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000419}
420
Eli Friedman0dfb8892011-10-06 23:00:33 +0000421void
422ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
423 Writer.AddTypeRef(T->getValueType(), Record);
424 Code = TYPE_ATOMIC;
425}
426
John McCall8f115c62009-10-16 21:56:05 +0000427namespace {
428
429class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000430 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000431 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000432
433public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000434 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000435 : Writer(Writer), Record(Record) { }
436
John McCall17001972009-10-18 01:05:36 +0000437#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000438#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000439 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000440#include "clang/AST/TypeLocNodes.def"
441
John McCall17001972009-10-18 01:05:36 +0000442 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
443 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000444};
445
446}
447
John McCall17001972009-10-18 01:05:36 +0000448void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
449 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000450}
John McCall17001972009-10-18 01:05:36 +0000451void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000452 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
453 if (TL.needsExtraLocalData()) {
454 Record.push_back(TL.getWrittenTypeSpec());
455 Record.push_back(TL.getWrittenSignSpec());
456 Record.push_back(TL.getWrittenWidthSpec());
457 Record.push_back(TL.hasModeAttr());
458 }
John McCall8f115c62009-10-16 21:56:05 +0000459}
John McCall17001972009-10-18 01:05:36 +0000460void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000462}
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000465}
Reid Kleckner8a365022013-06-24 17:51:48 +0000466void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
467 // nothing to do
468}
Reid Kleckner0503a872013-12-05 01:23:43 +0000469void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
470 // nothing to do
471}
John McCall17001972009-10-18 01:05:36 +0000472void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000474}
John McCall17001972009-10-18 01:05:36 +0000475void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000477}
John McCall17001972009-10-18 01:05:36 +0000478void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000480}
John McCall17001972009-10-18 01:05:36 +0000481void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000483 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000484}
John McCall17001972009-10-18 01:05:36 +0000485void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
487 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
488 Record.push_back(TL.getSizeExpr() ? 1 : 0);
489 if (TL.getSizeExpr())
490 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000491}
John McCall17001972009-10-18 01:05:36 +0000492void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
493 VisitArrayTypeLoc(TL);
494}
495void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
496 VisitArrayTypeLoc(TL);
497}
498void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
499 VisitArrayTypeLoc(TL);
500}
501void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
502 DependentSizedArrayTypeLoc TL) {
503 VisitArrayTypeLoc(TL);
504}
505void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
506 DependentSizedExtVectorTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getNameLoc(), Record);
508}
509void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
512void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getNameLoc(), Record);
514}
515void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000516 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000517 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
518 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000519 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000520 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
521 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000522}
523void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
524 VisitFunctionTypeLoc(TL);
525}
526void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
527 VisitFunctionTypeLoc(TL);
528}
John McCallb96ec562009-12-04 22:46:56 +0000529void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall17001972009-10-18 01:05:36 +0000532void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
535void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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);
John McCall17001972009-10-18 01:05:36 +0000539}
540void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000541 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
542 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
543 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
544 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000545}
546void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getNameLoc(), Record);
548}
Alexis Hunte852b102011-05-24 22:41:36 +0000549void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
550 Writer.AddSourceLocation(TL.getKWLoc(), Record);
551 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
552 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
553 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
554}
Richard Smith30482bc2011-02-20 03:19:35 +0000555void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
John McCall17001972009-10-18 01:05:36 +0000558void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
561void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
John McCall81904512011-01-06 01:58:22 +0000564void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
565 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
566 if (TL.hasAttrOperand()) {
567 SourceRange range = TL.getAttrOperandParensRange();
568 Writer.AddSourceLocation(range.getBegin(), Record);
569 Writer.AddSourceLocation(range.getEnd(), Record);
570 }
571 if (TL.hasAttrExprOperand()) {
572 Expr *operand = TL.getAttrExprOperand();
573 Record.push_back(operand ? 1 : 0);
574 if (operand) Writer.AddStmt(operand);
575 } else if (TL.hasAttrEnumOperand()) {
576 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
577 }
578}
John McCall17001972009-10-18 01:05:36 +0000579void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getNameLoc(), Record);
581}
John McCallcebee162009-10-18 09:09:24 +0000582void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
583 SubstTemplateTypeParmTypeLoc TL) {
584 Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
Douglas Gregorada4b792011-01-14 02:55:32 +0000586void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
587 SubstTemplateTypeParmPackTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
589}
John McCall17001972009-10-18 01:05:36 +0000590void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
591 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000593 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
594 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
595 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
596 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000597 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
598 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000599}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000600void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
602 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
603}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000604void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000605 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000606 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000607}
John McCalle78aac42010-03-10 03:28:59 +0000608void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
609 Writer.AddSourceLocation(TL.getNameLoc(), Record);
610}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000611void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000612 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000613 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000614 Writer.AddSourceLocation(TL.getNameLoc(), Record);
615}
John McCallc392f372010-06-11 00:33:02 +0000616void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
617 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000618 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000619 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000620 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000621 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000622 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
623 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
624 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000625 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
626 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000627}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000628void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
629 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
630}
John McCall17001972009-10-18 01:05:36 +0000631void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
632 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000633}
634void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
635 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000636 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
637 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
638 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
639 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000640}
John McCallfc93cf92009-10-22 22:37:11 +0000641void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
642 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000643}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000644void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
645 Writer.AddSourceLocation(TL.getKWLoc(), Record);
646 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
647 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
648}
John McCall8f115c62009-10-16 21:56:05 +0000649
Chris Lattner19cea4e2009-04-22 05:57:30 +0000650//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000651// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000652//===----------------------------------------------------------------------===//
653
Chris Lattner28fa4e62009-04-26 22:26:21 +0000654static void EmitBlockID(unsigned ID, const char *Name,
655 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000656 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000657 Record.clear();
658 Record.push_back(ID);
659 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
660
661 // Emit the block name if present.
662 if (Name == 0 || Name[0] == 0) return;
663 Record.clear();
664 while (*Name)
665 Record.push_back(*Name++);
666 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
667}
668
669static void EmitRecordID(unsigned ID, const char *Name,
670 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000671 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000672 Record.clear();
673 Record.push_back(ID);
674 while (*Name)
675 Record.push_back(*Name++);
676 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000677}
678
679static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000680 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000681#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000682 RECORD(STMT_STOP);
683 RECORD(STMT_NULL_PTR);
684 RECORD(STMT_NULL);
685 RECORD(STMT_COMPOUND);
686 RECORD(STMT_CASE);
687 RECORD(STMT_DEFAULT);
688 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000689 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000690 RECORD(STMT_IF);
691 RECORD(STMT_SWITCH);
692 RECORD(STMT_WHILE);
693 RECORD(STMT_DO);
694 RECORD(STMT_FOR);
695 RECORD(STMT_GOTO);
696 RECORD(STMT_INDIRECT_GOTO);
697 RECORD(STMT_CONTINUE);
698 RECORD(STMT_BREAK);
699 RECORD(STMT_RETURN);
700 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000701 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000702 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000703 RECORD(EXPR_PREDEFINED);
704 RECORD(EXPR_DECL_REF);
705 RECORD(EXPR_INTEGER_LITERAL);
706 RECORD(EXPR_FLOATING_LITERAL);
707 RECORD(EXPR_IMAGINARY_LITERAL);
708 RECORD(EXPR_STRING_LITERAL);
709 RECORD(EXPR_CHARACTER_LITERAL);
710 RECORD(EXPR_PAREN);
711 RECORD(EXPR_UNARY_OPERATOR);
712 RECORD(EXPR_SIZEOF_ALIGN_OF);
713 RECORD(EXPR_ARRAY_SUBSCRIPT);
714 RECORD(EXPR_CALL);
715 RECORD(EXPR_MEMBER);
716 RECORD(EXPR_BINARY_OPERATOR);
717 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
718 RECORD(EXPR_CONDITIONAL_OPERATOR);
719 RECORD(EXPR_IMPLICIT_CAST);
720 RECORD(EXPR_CSTYLE_CAST);
721 RECORD(EXPR_COMPOUND_LITERAL);
722 RECORD(EXPR_EXT_VECTOR_ELEMENT);
723 RECORD(EXPR_INIT_LIST);
724 RECORD(EXPR_DESIGNATED_INIT);
725 RECORD(EXPR_IMPLICIT_VALUE_INIT);
726 RECORD(EXPR_VA_ARG);
727 RECORD(EXPR_ADDR_LABEL);
728 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000729 RECORD(EXPR_CHOOSE);
730 RECORD(EXPR_GNU_NULL);
731 RECORD(EXPR_SHUFFLE_VECTOR);
732 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000733 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000734 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000735 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000736 RECORD(EXPR_OBJC_ARRAY_LITERAL);
737 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000738 RECORD(EXPR_OBJC_ENCODE);
739 RECORD(EXPR_OBJC_SELECTOR_EXPR);
740 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
741 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
742 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
743 RECORD(EXPR_OBJC_KVC_REF_EXPR);
744 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000745 RECORD(STMT_OBJC_FOR_COLLECTION);
746 RECORD(STMT_OBJC_CATCH);
747 RECORD(STMT_OBJC_FINALLY);
748 RECORD(STMT_OBJC_AT_TRY);
749 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
750 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000751 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000752 RECORD(EXPR_CXX_OPERATOR_CALL);
753 RECORD(EXPR_CXX_CONSTRUCT);
754 RECORD(EXPR_CXX_STATIC_CAST);
755 RECORD(EXPR_CXX_DYNAMIC_CAST);
756 RECORD(EXPR_CXX_REINTERPRET_CAST);
757 RECORD(EXPR_CXX_CONST_CAST);
758 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000759 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000760 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000761 RECORD(EXPR_CXX_BOOL_LITERAL);
762 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000763 RECORD(EXPR_CXX_TYPEID_EXPR);
764 RECORD(EXPR_CXX_TYPEID_TYPE);
765 RECORD(EXPR_CXX_UUIDOF_EXPR);
766 RECORD(EXPR_CXX_UUIDOF_TYPE);
767 RECORD(EXPR_CXX_THIS);
768 RECORD(EXPR_CXX_THROW);
769 RECORD(EXPR_CXX_DEFAULT_ARG);
770 RECORD(EXPR_CXX_BIND_TEMPORARY);
771 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
772 RECORD(EXPR_CXX_NEW);
773 RECORD(EXPR_CXX_DELETE);
774 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
775 RECORD(EXPR_EXPR_WITH_CLEANUPS);
776 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
777 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
778 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
779 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
780 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000781 RECORD(EXPR_CXX_NOEXCEPT);
782 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000783 RECORD(EXPR_PACK_EXPANSION);
784 RECORD(EXPR_SIZEOF_PACK);
785 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000786 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000787#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000788}
Mike Stump11289f42009-09-09 15:08:12 +0000789
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000790void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000791 RecordData Record;
792 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Sebastian Redl539c5062010-08-18 23:57:32 +0000794#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
795#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000796
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000797 // Control Block.
798 BLOCK(CONTROL_BLOCK);
799 RECORD(METADATA);
800 RECORD(IMPORTS);
801 RECORD(LANGUAGE_OPTIONS);
802 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000803 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000804 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000805 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000806 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000807 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000808 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000809 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000810 RECORD(PREPROCESSOR_OPTIONS);
811
Douglas Gregor108cb222012-10-19 00:45:00 +0000812 BLOCK(INPUT_FILES_BLOCK);
813 RECORD(INPUT_FILE);
814
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000815 // AST Top-Level Block.
816 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 RECORD(TYPE_OFFSET);
818 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000819 RECORD(IDENTIFIER_OFFSET);
820 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000821 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 RECORD(SPECIAL_TYPES);
823 RECORD(STATISTICS);
824 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000825 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000826 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000827 RECORD(SELECTOR_OFFSETS);
828 RECORD(METHOD_POOL);
829 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000830 RECORD(SOURCE_LOCATION_OFFSETS);
831 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000832 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000833 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000834 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000835 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000836 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000837 RECORD(SEMA_DECL_REFS);
838 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
839 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
840 RECORD(DECL_REPLACEMENTS);
841 RECORD(UPDATE_VISIBLE);
842 RECORD(DECL_UPDATE_OFFSETS);
843 RECORD(DECL_UPDATES);
844 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
845 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000846 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000847 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000848 RECORD(FP_PRAGMA_OPTIONS);
849 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000850 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000851 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000852 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000853 RECORD(MODULE_OFFSET_MAP);
854 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000855 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000856 RECORD(FILE_SORTED_DECLS);
857 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000858 RECORD(MERGED_DECLARATIONS);
859 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000860 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000861 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000862 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000863 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000864
Chris Lattner28fa4e62009-04-26 22:26:21 +0000865 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000866 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000867 RECORD(SM_SLOC_FILE_ENTRY);
868 RECORD(SM_SLOC_BUFFER_ENTRY);
869 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000870 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner28fa4e62009-04-26 22:26:21 +0000872 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000873 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000874 RECORD(PP_MACRO_OBJECT_LIKE);
875 RECORD(PP_MACRO_FUNCTION_LIKE);
876 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000877
Douglas Gregor12bfa382009-10-17 00:13:19 +0000878 // Decls and Types block.
879 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000880 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000881 RECORD(TYPE_COMPLEX);
882 RECORD(TYPE_POINTER);
883 RECORD(TYPE_BLOCK_POINTER);
884 RECORD(TYPE_LVALUE_REFERENCE);
885 RECORD(TYPE_RVALUE_REFERENCE);
886 RECORD(TYPE_MEMBER_POINTER);
887 RECORD(TYPE_CONSTANT_ARRAY);
888 RECORD(TYPE_INCOMPLETE_ARRAY);
889 RECORD(TYPE_VARIABLE_ARRAY);
890 RECORD(TYPE_VECTOR);
891 RECORD(TYPE_EXT_VECTOR);
892 RECORD(TYPE_FUNCTION_PROTO);
893 RECORD(TYPE_FUNCTION_NO_PROTO);
894 RECORD(TYPE_TYPEDEF);
895 RECORD(TYPE_TYPEOF_EXPR);
896 RECORD(TYPE_TYPEOF);
897 RECORD(TYPE_RECORD);
898 RECORD(TYPE_ENUM);
899 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000900 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000901 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000902 RECORD(TYPE_DECLTYPE);
903 RECORD(TYPE_ELABORATED);
904 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
905 RECORD(TYPE_UNRESOLVED_USING);
906 RECORD(TYPE_INJECTED_CLASS_NAME);
907 RECORD(TYPE_OBJC_OBJECT);
908 RECORD(TYPE_TEMPLATE_TYPE_PARM);
909 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
910 RECORD(TYPE_DEPENDENT_NAME);
911 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
912 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
913 RECORD(TYPE_PAREN);
914 RECORD(TYPE_PACK_EXPANSION);
915 RECORD(TYPE_ATTRIBUTED);
916 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000917 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000918 RECORD(DECL_TYPEDEF);
919 RECORD(DECL_ENUM);
920 RECORD(DECL_RECORD);
921 RECORD(DECL_ENUM_CONSTANT);
922 RECORD(DECL_FUNCTION);
923 RECORD(DECL_OBJC_METHOD);
924 RECORD(DECL_OBJC_INTERFACE);
925 RECORD(DECL_OBJC_PROTOCOL);
926 RECORD(DECL_OBJC_IVAR);
927 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000928 RECORD(DECL_OBJC_CATEGORY);
929 RECORD(DECL_OBJC_CATEGORY_IMPL);
930 RECORD(DECL_OBJC_IMPLEMENTATION);
931 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
932 RECORD(DECL_OBJC_PROPERTY);
933 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000934 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000935 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000936 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000937 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000938 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000939 RECORD(DECL_FILE_SCOPE_ASM);
940 RECORD(DECL_BLOCK);
941 RECORD(DECL_CONTEXT_LEXICAL);
942 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000943 RECORD(DECL_NAMESPACE);
944 RECORD(DECL_NAMESPACE_ALIAS);
945 RECORD(DECL_USING);
946 RECORD(DECL_USING_SHADOW);
947 RECORD(DECL_USING_DIRECTIVE);
948 RECORD(DECL_UNRESOLVED_USING_VALUE);
949 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
950 RECORD(DECL_LINKAGE_SPEC);
951 RECORD(DECL_CXX_RECORD);
952 RECORD(DECL_CXX_METHOD);
953 RECORD(DECL_CXX_CONSTRUCTOR);
954 RECORD(DECL_CXX_DESTRUCTOR);
955 RECORD(DECL_CXX_CONVERSION);
956 RECORD(DECL_ACCESS_SPEC);
957 RECORD(DECL_FRIEND);
958 RECORD(DECL_FRIEND_TEMPLATE);
959 RECORD(DECL_CLASS_TEMPLATE);
960 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
961 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000962 RECORD(DECL_VAR_TEMPLATE);
963 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
964 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000965 RECORD(DECL_FUNCTION_TEMPLATE);
966 RECORD(DECL_TEMPLATE_TYPE_PARM);
967 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
968 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
969 RECORD(DECL_STATIC_ASSERT);
970 RECORD(DECL_CXX_BASE_SPECIFIERS);
971 RECORD(DECL_INDIRECTFIELD);
972 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
973
Douglas Gregor03412ba2011-06-03 02:27:19 +0000974 // Statements and Exprs can occur in the Decls and Types block.
975 AddStmtsExprs(Stream, Record);
976
Douglas Gregor92a96f52011-02-08 21:58:10 +0000977 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000978 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000979 RECORD(PPD_MACRO_DEFINITION);
980 RECORD(PPD_INCLUSION_DIRECTIVE);
981
Chris Lattner28fa4e62009-04-26 22:26:21 +0000982#undef RECORD
983#undef BLOCK
984 Stream.ExitBlock();
985}
986
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000987/// \brief Adjusts the given filename to only write out the portion of the
988/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000989///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000990/// \param Filename the file name to adjust.
991///
992/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
993/// the returned filename will be adjusted by this system root.
994///
995/// \returns either the original filename (if it needs no adjustment) or the
996/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000997static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000998adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000999 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregorc567ba22011-07-22 16:35:34 +00001001 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001002 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001004 // Verify that the filename and the system root have the same prefix.
1005 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001006 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001007 if (Filename[Pos] != isysroot[Pos])
1008 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001009
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001010 // We hit the end of the filename before we hit the end of the system root.
1011 if (!Filename[Pos])
1012 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001013
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001014 // If the file name has a '/' at the current position, skip over the '/'.
1015 // We distinguish sysroot-based includes from absolute includes by the
1016 // absence of '/' at the beginning of sysroot-based includes.
1017 if (Filename[Pos] == '/')
1018 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001019
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001020 return Filename + Pos;
1021}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001022
Douglas Gregor112b9072012-10-18 05:31:06 +00001023/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001024void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1025 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001026 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001027 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001028 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1029 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001030
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001031 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001032 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1033 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1035 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1036 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1037 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1038 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1039 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1040 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1041 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1042 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001043 Record.push_back(VERSION_MAJOR);
1044 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001045 Record.push_back(CLANG_VERSION_MAJOR);
1046 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001047 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001048 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001049 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1050 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001051
Douglas Gregor112b9072012-10-18 05:31:06 +00001052 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001053 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001054 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001055 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001056
1057 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1058 M != MEnd; ++M) {
1059 // Skip modules that weren't directly imported.
1060 if (!(*M)->isDirectlyImported())
1061 continue;
1062
1063 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001064 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001065 Record.push_back((*M)->File->getSize());
1066 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001067 // FIXME: This writes the absolute path for AST files we depend on.
1068 const std::string &FileName = (*M)->FileName;
1069 Record.push_back(FileName.size());
1070 Record.append(FileName.begin(), FileName.end());
1071 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001072 Stream.EmitRecord(IMPORTS, Record);
1073 }
Mike Stump11289f42009-09-09 15:08:12 +00001074
Douglas Gregor112b9072012-10-18 05:31:06 +00001075 // Language options.
1076 Record.clear();
1077 const LangOptions &LangOpts = Context.getLangOpts();
1078#define LANGOPT(Name, Bits, Default, Description) \
1079 Record.push_back(LangOpts.Name);
1080#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1081 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1082#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001083#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1084#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001085
1086 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1087 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1088
1089 Record.push_back(LangOpts.CurrentModule.size());
1090 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001091
1092 // Comment options.
1093 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1094 for (CommentOptions::BlockCommandNamesTy::const_iterator
1095 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1096 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1097 I != IEnd; ++I) {
1098 AddString(*I, Record);
1099 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001100 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001101
Douglas Gregor112b9072012-10-18 05:31:06 +00001102 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1103
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001104 // Target options.
1105 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001106 const TargetInfo &Target = Context.getTargetInfo();
1107 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001108 AddString(TargetOpts.Triple, Record);
1109 AddString(TargetOpts.CPU, Record);
1110 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001111 AddString(TargetOpts.LinkerVersion, Record);
1112 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1113 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1114 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1115 }
1116 Record.push_back(TargetOpts.Features.size());
1117 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1118 AddString(TargetOpts.Features[I], Record);
1119 }
1120 Stream.EmitRecord(TARGET_OPTIONS, Record);
1121
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001122 // Diagnostic options.
1123 Record.clear();
1124 const DiagnosticOptions &DiagOpts
1125 = Context.getDiagnostics().getDiagnosticOptions();
1126#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1127#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1128 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1129#include "clang/Basic/DiagnosticOptions.def"
1130 Record.push_back(DiagOpts.Warnings.size());
1131 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1132 AddString(DiagOpts.Warnings[I], Record);
1133 // Note: we don't serialize the log or serialization file names, because they
1134 // are generally transient files and will almost always be overridden.
1135 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1136
Douglas Gregorc6317db2012-10-24 15:49:58 +00001137 // File system options.
1138 Record.clear();
1139 const FileSystemOptions &FSOpts
1140 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1141 AddString(FSOpts.WorkingDir, Record);
1142 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1143
Douglas Gregor2d302362012-10-24 16:50:34 +00001144 // Header search options.
1145 Record.clear();
1146 const HeaderSearchOptions &HSOpts
1147 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1148 AddString(HSOpts.Sysroot, Record);
1149
1150 // Include entries.
1151 Record.push_back(HSOpts.UserEntries.size());
1152 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1153 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1154 AddString(Entry.Path, Record);
1155 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001156 Record.push_back(Entry.IsFramework);
1157 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001158 }
1159
1160 // System header prefixes.
1161 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1162 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1163 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1164 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1165 }
1166
1167 AddString(HSOpts.ResourceDir, Record);
1168 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001169 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001170 Record.push_back(HSOpts.DisableModuleHash);
1171 Record.push_back(HSOpts.UseBuiltinIncludes);
1172 Record.push_back(HSOpts.UseStandardSystemIncludes);
1173 Record.push_back(HSOpts.UseStandardCXXIncludes);
1174 Record.push_back(HSOpts.UseLibcxx);
1175 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1176
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001177 // Preprocessor options.
1178 Record.clear();
1179 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1180
1181 // Macro definitions.
1182 Record.push_back(PPOpts.Macros.size());
1183 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1184 AddString(PPOpts.Macros[I].first, Record);
1185 Record.push_back(PPOpts.Macros[I].second);
1186 }
1187
1188 // Includes
1189 Record.push_back(PPOpts.Includes.size());
1190 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1191 AddString(PPOpts.Includes[I], Record);
1192
1193 // Macro includes
1194 Record.push_back(PPOpts.MacroIncludes.size());
1195 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1196 AddString(PPOpts.MacroIncludes[I], Record);
1197
Douglas Gregorb6368752012-10-24 23:41:50 +00001198 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001199 // Detailed record is important since it is used for the module cache hash.
1200 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001201 AddString(PPOpts.ImplicitPCHInclude, Record);
1202 AddString(PPOpts.ImplicitPTHInclude, Record);
1203 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1204 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1205
Douglas Gregora3b20262011-05-06 21:43:30 +00001206 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001207 SourceManager &SM = Context.getSourceManager();
1208 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1209 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001210 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1211 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001212 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1213 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1214
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001215 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001216
Michael J. Spencer740857f2010-12-21 16:45:57 +00001217 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001218
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001219 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001220 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001221 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001222 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001223 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001224 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001225 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001226 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001227
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001228 Record.clear();
1229 Record.push_back(SM.getMainFileID().getOpaqueValue());
1230 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1231
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001232 // Original PCH directory
1233 if (!OutputFile.empty() && OutputFile != "-") {
1234 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1235 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1237 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1238
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001239 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001240
1241 llvm::sys::fs::make_absolute(OutputPath);
1242 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1243
1244 RecordData Record;
1245 Record.push_back(ORIGINAL_PCH_DIR);
1246 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1247 }
1248
Douglas Gregor49491f72013-03-15 22:15:07 +00001249 WriteInputFiles(Context.SourceMgr,
1250 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001251 isysroot,
1252 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001253 Stream.ExitBlock();
1254}
1255
Douglas Gregor49491f72013-03-15 22:15:07 +00001256namespace {
1257 /// \brief An input file.
1258 struct InputFileEntry {
1259 const FileEntry *File;
1260 bool IsSystemFile;
1261 bool BufferOverridden;
1262 };
1263}
1264
1265void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1266 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001267 StringRef isysroot,
1268 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001269 using namespace llvm;
1270 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1271 RecordData Record;
1272
1273 // Create input-file abbreviation.
1274 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1275 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001276 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001277 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1278 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001279 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001280 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1281 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1282
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001283 // Get all ContentCache objects for files, sorted by whether the file is a
1284 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001285 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001286 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1287 // Get this source location entry.
1288 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001289 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001290
1291 // We only care about file entries that were not overridden.
1292 if (!SLoc->isFile())
1293 continue;
1294 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001295 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001296 continue;
1297
Douglas Gregor49491f72013-03-15 22:15:07 +00001298 InputFileEntry Entry;
1299 Entry.File = Cache->OrigEntry;
1300 Entry.IsSystemFile = Cache->IsSystemFile;
1301 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001302 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001303 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001304 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001305 SortedFiles.push_front(Entry);
1306 }
1307
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001308 unsigned UserFilesNum = 0;
1309 // Write out all of the input files.
1310 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001311 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001312 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001313 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001314
Douglas Gregor49491f72013-03-15 22:15:07 +00001315 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001316 if (InputFileID != 0)
1317 continue; // already recorded this file.
1318
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001319 // Record this entry's offset.
1320 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001321
1322 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001323
Douglas Gregor49491f72013-03-15 22:15:07 +00001324 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001325 ++UserFilesNum;
1326
Douglas Gregor72be3902012-10-19 00:38:02 +00001327 Record.clear();
1328 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001329 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001330
1331 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001332 Record.push_back(Entry.File->getSize());
1333 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001334
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001335 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001336 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001337
Douglas Gregor72be3902012-10-19 00:38:02 +00001338 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001339 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001340 SmallString<128> FilePath(Filename);
1341
1342 // Ask the file manager to fixup the relative path for us. This will
1343 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001344 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001345
1346 // FIXME: This call to make_absolute shouldn't be necessary, the
1347 // call to FixupRelativePath should always return an absolute path.
1348 llvm::sys::fs::make_absolute(FilePath);
1349 Filename = FilePath.c_str();
1350
1351 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1352
1353 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1354 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001355
Douglas Gregor112b9072012-10-18 05:31:06 +00001356 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001357
1358 // Create input file offsets abbreviation.
1359 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1360 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1361 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001362 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1363 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001364 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1365 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1366
1367 // Write input file offsets.
1368 Record.clear();
1369 Record.push_back(INPUT_FILE_OFFSETS);
1370 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001371 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001372 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001373}
1374
Douglas Gregora7f71a92009-04-10 03:52:48 +00001375//===----------------------------------------------------------------------===//
1376// Source Manager Serialization
1377//===----------------------------------------------------------------------===//
1378
1379/// \brief Create an abbreviation for the SLocEntry that refers to a
1380/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001381static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001382 using namespace llvm;
1383 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001384 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001389 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001394 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001395}
1396
1397/// \brief Create an abbreviation for the SLocEntry that refers to a
1398/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001399static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001400 using namespace llvm;
1401 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001402 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001408 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001409}
1410
1411/// \brief Create an abbreviation for the SLocEntry that refers to a
1412/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001413static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001414 using namespace llvm;
1415 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001416 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001418 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001419}
1420
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001421/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1422/// expansion.
1423static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001424 using namespace llvm;
1425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001426 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001432 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001433}
1434
Douglas Gregor09b69892011-02-10 17:09:37 +00001435namespace {
1436 // Trait used for the on-disk hash table of header search information.
1437 class HeaderFileInfoTrait {
1438 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001439 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001440
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001441 // Keep track of the framework names we've used during serialization.
1442 SmallVector<char, 128> FrameworkStringData;
1443 llvm::StringMap<unsigned> FrameworkNameOffset;
1444
Douglas Gregor09b69892011-02-10 17:09:37 +00001445 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001446 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1447 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001448
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001449 struct key_type {
1450 const FileEntry *FE;
1451 const char *Filename;
1452 };
1453 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001454
1455 typedef HeaderFileInfo data_type;
1456 typedef const data_type &data_type_ref;
1457
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001458 static unsigned ComputeHash(key_type_ref key) {
1459 // The hash is based only on size/time of the file, so that the reader can
1460 // match even when symlinking or excess path elements ("foo/../", "../")
1461 // change the form of the name. However, complete path is still the key.
1462 return llvm::hash_combine(key.FE->getSize(),
1463 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001464 }
1465
1466 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001467 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1468 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1469 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001470 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001471 if (Data.isModuleHeader)
1472 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001473 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001474 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001475 }
1476
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001477 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1478 clang::io::Emit64(Out, key.FE->getSize());
1479 KeyLen -= 8;
1480 clang::io::Emit64(Out, key.FE->getModificationTime());
1481 KeyLen -= 8;
1482 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001483 }
1484
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001485 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001486 data_type_ref Data, unsigned DataLen) {
1487 using namespace clang::io;
1488 uint64_t Start = Out.tell(); (void)Start;
1489
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001490 unsigned char Flags = (Data.HeaderRole << 6)
1491 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001492 | (Data.isPragmaOnce << 4)
1493 | (Data.DirInfo << 2)
1494 | (Data.Resolved << 1)
1495 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001496 Emit8(Out, (uint8_t)Flags);
1497 Emit16(Out, (uint16_t) Data.NumIncludes);
1498
1499 if (!Data.ControllingMacro)
1500 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1501 else
1502 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001503
1504 unsigned Offset = 0;
1505 if (!Data.Framework.empty()) {
1506 // If this header refers into a framework, save the framework name.
1507 llvm::StringMap<unsigned>::iterator Pos
1508 = FrameworkNameOffset.find(Data.Framework);
1509 if (Pos == FrameworkNameOffset.end()) {
1510 Offset = FrameworkStringData.size() + 1;
1511 FrameworkStringData.append(Data.Framework.begin(),
1512 Data.Framework.end());
1513 FrameworkStringData.push_back(0);
1514
1515 FrameworkNameOffset[Data.Framework] = Offset;
1516 } else
1517 Offset = Pos->second;
1518 }
1519 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001520
1521 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001522 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001523 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1524 }
1525
Douglas Gregor09b69892011-02-10 17:09:37 +00001526 assert(Out.tell() - Start == DataLen && "Wrong data length");
1527 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001528
1529 const char *strings_begin() const { return FrameworkStringData.begin(); }
1530 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001531 };
1532} // end anonymous namespace
1533
1534/// \brief Write the header search block for the list of files that
1535///
1536/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001537void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001538 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001539 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1540
1541 if (FilesByUID.size() > HS.header_file_size())
1542 FilesByUID.resize(HS.header_file_size());
1543
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001544 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001545 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001546 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001547 unsigned NumHeaderSearchEntries = 0;
1548 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1549 const FileEntry *File = FilesByUID[UID];
1550 if (!File)
1551 continue;
1552
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001553 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1554 // from the external source if it was not provided already.
Ben Langmuird285c502014-03-13 16:46:36 +00001555 HeaderFileInfo HFI;
1556 if (!HS.tryGetFileInfo(File, HFI) ||
1557 (HFI.External && Chain) ||
1558 (HFI.isModuleHeader && !HFI.isCompilingModuleHeader))
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001559 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001560
1561 // Turn the file name into an absolute path, if it isn't already.
1562 const char *Filename = File->getName();
1563 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1564
1565 // If we performed any translation on the file name at all, we need to
1566 // save this string, since the generator will refer to it later.
1567 if (Filename != File->getName()) {
1568 Filename = strdup(Filename);
1569 SavedStrings.push_back(Filename);
1570 }
1571
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001572 HeaderFileInfoTrait::key_type key = { File, Filename };
1573 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001574 ++NumHeaderSearchEntries;
1575 }
1576
1577 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001578 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001579 uint32_t BucketOffset;
1580 {
1581 llvm::raw_svector_ostream Out(TableData);
1582 // Make sure that no bucket is at offset 0
1583 clang::io::Emit32(Out, 0);
1584 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1585 }
1586
1587 // Create a blob abbreviation
1588 using namespace llvm;
1589 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1590 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1595 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1596
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001597 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001598 RecordData Record;
1599 Record.push_back(HEADER_SEARCH_TABLE);
1600 Record.push_back(BucketOffset);
1601 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001602 Record.push_back(TableData.size());
1603 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001604 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1605
1606 // Free all of the strings we had to duplicate.
1607 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001608 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001609}
1610
Douglas Gregora7f71a92009-04-10 03:52:48 +00001611/// \brief Writes the block containing the serialized form of the
1612/// source manager.
1613///
1614/// TODO: We should probably use an on-disk hash table (stored in a
1615/// blob), indexed based on the file name, so that we only create
1616/// entries for files that we actually need. In the common case (no
1617/// errors), we probably won't have to create file entries for any of
1618/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001619void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001620 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001621 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001622 RecordData Record;
1623
Chris Lattner0910e3b2009-04-10 17:16:57 +00001624 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001625 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001626
1627 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001628 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1629 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1630 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001631 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001632
Douglas Gregor258ae542009-04-27 06:38:32 +00001633 // Write out the source location entry table. We skip the first
1634 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001635 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001636 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001637 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1638 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001639 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001640 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001641 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001642 FileID FID = FileID::get(I);
1643 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001644
Douglas Gregor258ae542009-04-27 06:38:32 +00001645 // Record the offset of this source-location entry.
1646 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1647
1648 // Figure out which record code to use.
1649 unsigned Code;
1650 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001651 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1652 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001653 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001654 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001655 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001656 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001657 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001658 Record.clear();
1659 Record.push_back(Code);
1660
Douglas Gregor925296b2011-07-19 16:10:42 +00001661 // Starting offset of this entry within this module, so skip the dummy.
1662 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001663 if (SLoc->isFile()) {
1664 const SrcMgr::FileInfo &File = SLoc->getFile();
1665 Record.push_back(File.getIncludeLoc().getRawEncoding());
1666 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1667 Record.push_back(File.hasLineDirectives());
1668
1669 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001670 if (Content->OrigEntry) {
1671 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001672 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001673
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001674 // The source location entry is a file. Emit input file ID.
1675 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1676 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001677
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001678 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001679
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001680 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001681 if (FDI != FileDeclIDs.end()) {
1682 Record.push_back(FDI->second->FirstDeclIndex);
1683 Record.push_back(FDI->second->DeclIDs.size());
1684 } else {
1685 Record.push_back(0);
1686 Record.push_back(0);
1687 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001688
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001689 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001690
1691 if (Content->BufferOverridden) {
1692 Record.clear();
1693 Record.push_back(SM_SLOC_BUFFER_BLOB);
1694 const llvm::MemoryBuffer *Buffer
1695 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1696 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1697 StringRef(Buffer->getBufferStart(),
1698 Buffer->getBufferSize() + 1));
1699 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001700 } else {
1701 // The source location entry is a buffer. The blob associated
1702 // with this entry contains the contents of the buffer.
1703
1704 // We add one to the size so that we capture the trailing NULL
1705 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1706 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001707 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001708 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001709 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001710 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001711 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001712 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001713 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001714 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001715 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001716 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001717
Douglas Gregor925296b2011-07-19 16:10:42 +00001718 if (strcmp(Name, "<built-in>") == 0) {
1719 PreloadSLocs.push_back(SLocEntryOffsets.size());
1720 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001721 }
1722 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001723 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001724 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001725 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1726 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001727 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1728 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001729
1730 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001731 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001732 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001733 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001734 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001735 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001736 }
1737 }
1738
Douglas Gregor8f45df52009-04-16 22:23:12 +00001739 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001740
1741 if (SLocEntryOffsets.empty())
1742 return;
1743
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001744 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001745 // table is used for lazily loading source-location information.
1746 using namespace llvm;
1747 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001748 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1752 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001753
Douglas Gregor258ae542009-04-27 06:38:32 +00001754 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001755 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001756 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001757 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001758 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001759
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001760 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001761 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001762 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001763
1764 // Write the line table. It depends on remapping working, so it must come
1765 // after the source location offsets.
1766 if (SourceMgr.hasLineTable()) {
1767 LineTableInfo &LineTable = SourceMgr.getLineTable();
1768
1769 Record.clear();
1770 // Emit the file names
1771 Record.push_back(LineTable.getNumFilenames());
1772 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1773 // Emit the file name
1774 const char *Filename = LineTable.getFilename(I);
1775 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1776 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1777 Record.push_back(FilenameLen);
1778 if (FilenameLen)
1779 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1780 }
1781
1782 // Emit the line entries
1783 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1784 L != LEnd; ++L) {
1785 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001786 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001787 continue;
1788
1789 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001790 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001791
1792 // Emit the line entries
1793 Record.push_back(L->second.size());
1794 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1795 LEEnd = L->second.end();
1796 LE != LEEnd; ++LE) {
1797 Record.push_back(LE->FileOffset);
1798 Record.push_back(LE->LineNo);
1799 Record.push_back(LE->FilenameID);
1800 Record.push_back((unsigned)LE->FileKind);
1801 Record.push_back(LE->IncludeOffset);
1802 }
1803 }
1804 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1805 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001806}
1807
Douglas Gregorc5046832009-04-27 18:38:38 +00001808//===----------------------------------------------------------------------===//
1809// Preprocessor Serialization
1810//===----------------------------------------------------------------------===//
1811
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001812namespace {
1813class ASTMacroTableTrait {
1814public:
1815 typedef IdentID key_type;
1816 typedef key_type key_type_ref;
1817
1818 struct Data {
1819 uint32_t MacroDirectivesOffset;
1820 };
1821
1822 typedef Data data_type;
1823 typedef const data_type &data_type_ref;
1824
1825 static unsigned ComputeHash(IdentID IdID) {
1826 return llvm::hash_value(IdID);
1827 }
1828
1829 std::pair<unsigned,unsigned>
1830 static EmitKeyDataLength(raw_ostream& Out,
1831 key_type_ref Key, data_type_ref Data) {
1832 unsigned KeyLen = 4; // IdentID.
1833 unsigned DataLen = 4; // MacroDirectivesOffset.
1834 return std::make_pair(KeyLen, DataLen);
1835 }
1836
1837 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1838 clang::io::Emit32(Out, Key);
1839 }
1840
1841 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1842 unsigned) {
1843 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1844 }
1845};
1846} // end anonymous namespace
1847
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001848static int compareMacroDirectives(
1849 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1850 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1851 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001852}
1853
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001854static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1855 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001856 if (MacroInfo *MI = MD->getMacroInfo())
1857 if (MI->isBuiltinMacro())
1858 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001859
1860 if (IsModule) {
1861 SourceLocation Loc = MD->getLocation();
1862 if (Loc.isInvalid())
1863 return true;
1864 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1865 return true;
1866 }
1867
1868 return false;
1869}
1870
Chris Lattnereeffaef2009-04-10 17:15:23 +00001871/// \brief Writes the block containing the serialized form of the
1872/// preprocessor.
1873///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001874void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001875 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1876 if (PPRec)
1877 WritePreprocessorDetail(*PPRec);
1878
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001879 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001880
Chris Lattner0af3ba12009-04-13 01:29:17 +00001881 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1882 if (PP.getCounterValue() != 0) {
1883 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001884 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001885 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001886 }
1887
1888 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001889 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001890
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001891 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001892 // FIXME: use diagnostics subsystem for localization etc.
1893 if (PP.SawDateOrTime())
1894 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001895
Douglas Gregor796d76a2010-10-20 22:00:55 +00001896
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001897 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001898 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001899
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001900 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001901 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001902 MacroDirectives;
1903 for (Preprocessor::macro_iterator
1904 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1905 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001906 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001907 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001908 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001909
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001910 // Sort the set of macro definitions that need to be serialized by the
1911 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001912 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1913 &compareMacroDirectives);
1914
1915 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1916
1917 // Emit the macro directives as a list and associate the offset with the
1918 // identifier they belong to.
1919 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1920 const IdentifierInfo *Name = MacroDirectives[I].first;
1921 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1922 MacroDirective *MD = MacroDirectives[I].second;
1923
1924 // If the macro or identifier need no updates, don't write the macro history
1925 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001926 // FIXME: Chain the macro history instead of re-writing it.
1927 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001928 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1929 continue;
1930
1931 // Emit the macro directives in reverse source order.
1932 for (; MD; MD = MD->getPrevious()) {
1933 if (shouldIgnoreMacro(MD, IsModule, PP))
1934 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001935
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001936 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001937 Record.push_back(MD->getKind());
1938 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1939 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1940 Record.push_back(InfoID);
1941 Record.push_back(DefMD->isImported());
1942 Record.push_back(DefMD->isAmbiguous());
1943
1944 } else if (VisibilityMacroDirective *
1945 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1946 Record.push_back(VisMD->isPublic());
1947 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001948 }
1949 if (Record.empty())
1950 continue;
1951
1952 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1953 Record.clear();
1954
1955 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1956
1957 IdentID NameID = getIdentifierRef(Name);
1958 ASTMacroTableTrait::Data data;
1959 data.MacroDirectivesOffset = MacroDirectiveOffset;
1960 Generator.insert(NameID, data);
1961 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001962
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001963 /// \brief Offsets of each of the macros into the bitstream, indexed by
1964 /// the local macro ID
1965 ///
1966 /// For each identifier that is associated with a macro, this map
1967 /// provides the offset into the bitstream where that macro is
1968 /// defined.
1969 std::vector<uint32_t> MacroOffsets;
1970
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001971 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1972 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1973 MacroInfo *MI = MacroInfosToEmit[I].MI;
1974 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001975
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001976 if (ID < FirstMacroID) {
1977 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1978 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001979 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001980
1981 // Record the local offset of this macro.
1982 unsigned Index = ID - FirstMacroID;
1983 if (Index == MacroOffsets.size())
1984 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1985 else {
1986 if (Index > MacroOffsets.size())
1987 MacroOffsets.resize(Index + 1);
1988
1989 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1990 }
1991
1992 AddIdentifierRef(Name, Record);
1993 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1994 AddSourceLocation(MI->getDefinitionLoc(), Record);
1995 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1996 Record.push_back(MI->isUsed());
1997 unsigned Code;
1998 if (MI->isObjectLike()) {
1999 Code = PP_MACRO_OBJECT_LIKE;
2000 } else {
2001 Code = PP_MACRO_FUNCTION_LIKE;
2002
2003 Record.push_back(MI->isC99Varargs());
2004 Record.push_back(MI->isGNUVarargs());
2005 Record.push_back(MI->hasCommaPasting());
2006 Record.push_back(MI->getNumArgs());
2007 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2008 I != E; ++I)
2009 AddIdentifierRef(*I, Record);
2010 }
2011
2012 // If we have a detailed preprocessing record, record the macro definition
2013 // ID that corresponds to this macro.
2014 if (PPRec)
2015 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2016
2017 Stream.EmitRecord(Code, Record);
2018 Record.clear();
2019
2020 // Emit the tokens array.
2021 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2022 // Note that we know that the preprocessor does not have any annotation
2023 // tokens in it because they are created by the parser, and thus can't
2024 // be in a macro definition.
2025 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002026 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002027 Stream.EmitRecord(PP_TOKEN, Record);
2028 Record.clear();
2029 }
2030 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002031 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002032
Douglas Gregor92a96f52011-02-08 21:58:10 +00002033 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002034
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002035 // Create the on-disk hash table in a buffer.
2036 SmallString<4096> MacroTable;
2037 uint32_t BucketOffset;
2038 {
2039 llvm::raw_svector_ostream Out(MacroTable);
2040 // Make sure that no bucket is at offset 0
2041 clang::io::Emit32(Out, 0);
2042 BucketOffset = Generator.Emit(Out);
2043 }
2044
2045 // Write the macro table
2046 using namespace llvm;
2047 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2048 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2051 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2052
2053 Record.push_back(MACRO_TABLE);
2054 Record.push_back(BucketOffset);
2055 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2056 Record.clear();
2057
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002058 // Write the offsets table for macro IDs.
2059 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002060 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002061 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2065
2066 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2067 Record.clear();
2068 Record.push_back(MACRO_OFFSET);
2069 Record.push_back(MacroOffsets.size());
2070 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2071 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2072 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002073}
2074
2075void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002076 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002077 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002078
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002079 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002080
Douglas Gregor92a96f52011-02-08 21:58:10 +00002081 // Enter the preprocessor block.
2082 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002083
Douglas Gregoraae92242010-03-19 21:51:54 +00002084 // If the preprocessor has a preprocessing record, emit it.
2085 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002086 using namespace llvm;
2087
2088 // Set up the abbreviation for
2089 unsigned InclusionAbbrev = 0;
2090 {
2091 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2092 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2098 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2099 }
2100
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002101 unsigned FirstPreprocessorEntityID
2102 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2103 + NUM_PREDEF_PP_ENTITY_IDS;
2104 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002105 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002106 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2107 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002108 E != EEnd;
2109 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002110 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002111
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002112 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2113 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002114
Douglas Gregor92a96f52011-02-08 21:58:10 +00002115 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002116 // Record this macro definition's ID.
2117 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002118
Douglas Gregor92a96f52011-02-08 21:58:10 +00002119 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002120 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2121 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002122 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002123
Chandler Carrutha88a22182011-07-14 08:20:46 +00002124 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002125 Record.push_back(ME->isBuiltinMacro());
2126 if (ME->isBuiltinMacro())
2127 AddIdentifierRef(ME->getName(), Record);
2128 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002129 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002130 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002131 continue;
2132 }
2133
2134 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2135 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002136 Record.push_back(ID->getFileName().size());
2137 Record.push_back(ID->wasInQuotes());
2138 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002139 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002140 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002141 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002142 // Check that the FileEntry is not null because it was not resolved and
2143 // we create a PCH even with compiler errors.
2144 if (ID->getFile())
2145 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002146 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2147 continue;
2148 }
2149
2150 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2151 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002152 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002153
Douglas Gregoraae92242010-03-19 21:51:54 +00002154 // Write the offsets table for the preprocessing record.
2155 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002156 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2157
Douglas Gregoraae92242010-03-19 21:51:54 +00002158 // Write the offsets table for identifier IDs.
2159 using namespace llvm;
2160 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002161 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002164 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002165
Douglas Gregoraae92242010-03-19 21:51:54 +00002166 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002167 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002168 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002169 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2170 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002171 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002172}
2173
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002174unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2175 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2176 if (Known != SubmoduleIDs.end())
2177 return Known->second;
2178
2179 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2180}
2181
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002182unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2183 if (!Mod)
2184 return 0;
2185
2186 llvm::DenseMap<Module *, unsigned>::const_iterator
2187 Known = SubmoduleIDs.find(Mod);
2188 if (Known != SubmoduleIDs.end())
2189 return Known->second;
2190
2191 return 0;
2192}
2193
Douglas Gregor253eefe2011-12-01 00:59:36 +00002194/// \brief Compute the number of modules within the given tree (including the
2195/// given module).
2196static unsigned getNumberOfModules(Module *Mod) {
2197 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002198 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2199 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002200 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002201 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002202
2203 return ChildModules + 1;
2204}
2205
Douglas Gregorde3ef502011-11-30 23:21:26 +00002206void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002207 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002208 // FIXME: This feels like it belongs somewhere else, but there are no
2209 // other consumers of this information.
2210 SourceManager &SrcMgr = PP->getSourceManager();
2211 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Aaron Ballmanbbc31212014-03-14 20:59:21 +00002212 for (const auto *I : Context->local_imports()) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002213 if (Module *ImportedFrom
2214 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2215 SrcMgr))) {
2216 ImportedFrom->Imports.push_back(I->getImportedModule());
2217 }
2218 }
2219
Douglas Gregor69021972011-11-30 17:33:56 +00002220 // Enter the submodule description block.
2221 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2222
2223 // Write the abbreviations needed for the submodules block.
2224 using namespace llvm;
2225 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2226 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Richard Smith9bca2982014-03-08 00:03:56 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
Douglas Gregora686e1b2012-01-27 19:52:33 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2238 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2239
2240 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002241 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2243 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2244
2245 Abbrev = new BitCodeAbbrev();
2246 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2248 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002249
2250 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002251 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2253 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2254
2255 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002256 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2258 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2259
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002260 Abbrev = new BitCodeAbbrev();
2261 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002264 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2265
Douglas Gregor59527662012-10-15 06:28:11 +00002266 Abbrev = new BitCodeAbbrev();
2267 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2269 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2270
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002271 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002272 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2274 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2275
2276 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002277 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2280 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2281
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002282 Abbrev = new BitCodeAbbrev();
2283 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2285 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2286
Douglas Gregorfb912652013-03-20 21:10:35 +00002287 Abbrev = new BitCodeAbbrev();
2288 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2291 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2292
Douglas Gregor253eefe2011-12-01 00:59:36 +00002293 // Write the submodule metadata block.
2294 RecordData Record;
2295 Record.push_back(getNumberOfModules(WritingModule));
2296 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2297 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2298
Douglas Gregor69021972011-11-30 17:33:56 +00002299 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002300 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002301 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002302 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002303 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002304 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002305 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002306
2307 // Emit the definition of the block.
2308 Record.clear();
2309 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002310 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002311 if (Mod->Parent) {
2312 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2313 Record.push_back(SubmoduleIDs[Mod->Parent]);
2314 } else {
2315 Record.push_back(0);
2316 }
2317 Record.push_back(Mod->IsFramework);
2318 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002319 Record.push_back(Mod->IsSystem);
Richard Smith9bca2982014-03-08 00:03:56 +00002320 Record.push_back(Mod->IsExternC);
Douglas Gregor73441092011-12-05 22:27:44 +00002321 Record.push_back(Mod->InferSubmodules);
2322 Record.push_back(Mod->InferExplicitSubmodules);
2323 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002324 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002325 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2326
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002327 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002328 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002329 Record.clear();
2330 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002331 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002332 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002333 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002334 }
2335
Douglas Gregor69021972011-11-30 17:33:56 +00002336 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002337 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002338 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002339 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002340 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002341 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002342 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2343 Record.clear();
2344 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2345 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2346 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002347 }
2348
2349 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002350 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002351 Record.clear();
2352 Record.push_back(SUBMODULE_HEADER);
2353 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002354 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002355 }
Douglas Gregor59527662012-10-15 06:28:11 +00002356 // Emit the excluded headers.
2357 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2358 Record.clear();
2359 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2360 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2361 Mod->ExcludedHeaders[I]->getName());
2362 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002363 // Emit the private headers.
2364 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2365 Record.clear();
2366 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2367 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2368 Mod->PrivateHeaders[I]->getName());
2369 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002370 ArrayRef<const FileEntry *>
2371 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2372 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002373 Record.clear();
2374 Record.push_back(SUBMODULE_TOPHEADER);
2375 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002376 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002377 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002378
2379 // Emit the imports.
2380 if (!Mod->Imports.empty()) {
2381 Record.clear();
2382 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002383 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002384 assert(ImportedID && "Unknown submodule!");
2385 Record.push_back(ImportedID);
2386 }
2387 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2388 }
2389
Douglas Gregor24bb9232011-12-02 18:58:38 +00002390 // Emit the exports.
2391 if (!Mod->Exports.empty()) {
2392 Record.clear();
2393 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002394 if (Module *Exported = Mod->Exports[I].getPointer()) {
2395 unsigned ExportedID = SubmoduleIDs[Exported];
2396 assert(ExportedID > 0 && "Unknown submodule ID?");
2397 Record.push_back(ExportedID);
2398 } else {
2399 Record.push_back(0);
2400 }
2401
Douglas Gregor24bb9232011-12-02 18:58:38 +00002402 Record.push_back(Mod->Exports[I].getInt());
2403 }
2404 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2405 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002406
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002407 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2408 // Might be unnecessary as use declarations are only used to build the
2409 // module itself.
2410
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002411 // Emit the link libraries.
2412 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2413 Record.clear();
2414 Record.push_back(SUBMODULE_LINK_LIBRARY);
2415 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2416 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2417 Mod->LinkLibraries[I].Library);
2418 }
2419
Douglas Gregorfb912652013-03-20 21:10:35 +00002420 // Emit the conflicts.
2421 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2422 Record.clear();
2423 Record.push_back(SUBMODULE_CONFLICT);
2424 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2425 assert(OtherID && "Unknown submodule!");
2426 Record.push_back(OtherID);
2427 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2428 Mod->Conflicts[I].Message);
2429 }
2430
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002431 // Emit the configuration macros.
2432 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2433 Record.clear();
2434 Record.push_back(SUBMODULE_CONFIG_MACRO);
2435 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2436 Mod->ConfigMacros[I]);
2437 }
2438
Douglas Gregor69021972011-11-30 17:33:56 +00002439 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002440 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2441 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002442 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002443 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002444 }
2445
2446 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002447
2448 assert((NextSubmoduleID - FirstSubmoduleID
2449 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002450}
2451
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002452serialization::SubmoduleID
2453ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002454 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002455 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002456
2457 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002458 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002459 Module *OwningMod
2460 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002461 if (!OwningMod)
2462 return 0;
2463
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002464 // Check whether this submodule is part of our own module.
2465 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002466 return 0;
2467
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002468 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002469}
2470
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002471void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2472 bool isModule) {
2473 // Make sure set diagnostic pragmas don't affect the translation unit that
2474 // imports the module.
2475 // FIXME: Make diagnostic pragma sections work properly with modules.
2476 if (isModule)
2477 return;
2478
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002479 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2480 DiagStateIDMap;
2481 unsigned CurrID = 0;
2482 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002483 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002484 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002485 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2486 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002487 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002488 if (point.Loc.isInvalid())
2489 continue;
2490
2491 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002492 unsigned &DiagStateID = DiagStateIDMap[point.State];
2493 Record.push_back(DiagStateID);
2494
2495 if (DiagStateID == 0) {
2496 DiagStateID = ++CurrID;
2497 for (DiagnosticsEngine::DiagState::const_iterator
2498 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2499 if (I->second.isPragma()) {
2500 Record.push_back(I->first);
2501 Record.push_back(I->second.getMapping());
2502 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002503 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002504 Record.push_back(-1); // mark the end of the diag/map pairs for this
2505 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002506 }
2507 }
2508
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002509 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002510 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002511}
2512
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002513void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2514 if (CXXBaseSpecifiersOffsets.empty())
2515 return;
2516
2517 RecordData Record;
2518
2519 // Create a blob abbreviation for the C++ base specifiers offsets.
2520 using namespace llvm;
2521
2522 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2523 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2525 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2526 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2527
Douglas Gregorc27b2872011-08-04 00:01:48 +00002528 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002529 Record.clear();
2530 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2531 Record.push_back(CXXBaseSpecifiersOffsets.size());
2532 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002533 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002534}
2535
Douglas Gregorc5046832009-04-27 18:38:38 +00002536//===----------------------------------------------------------------------===//
2537// Type Serialization
2538//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002539
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002540/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002541void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002542 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002543 if (Idx.getIndex() == 0) // we haven't seen this type before.
2544 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002545
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002546 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002547
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002548 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002549 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002550 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002551 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002552 else if (TypeOffsets.size() < Index) {
2553 TypeOffsets.resize(Index + 1);
2554 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002555 }
2556
2557 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002558
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002559 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002560 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002561
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002562 if (T.hasLocalNonFastQualifiers()) {
2563 Qualifiers Qs = T.getLocalQualifiers();
2564 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002565 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002566 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002567 } else {
2568 switch (T->getTypeClass()) {
2569 // For all of the concrete, non-dependent types, call the
2570 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002571#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002572 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002573#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002574#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002575 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002576 }
2577
2578 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002579 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002580
2581 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002582 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002583}
2584
Douglas Gregorc5046832009-04-27 18:38:38 +00002585//===----------------------------------------------------------------------===//
2586// Declaration Serialization
2587//===----------------------------------------------------------------------===//
2588
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002589/// \brief Write the block containing all of the declaration IDs
2590/// lexically declared within the given DeclContext.
2591///
2592/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2593/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002594uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002595 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002596 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002597 return 0;
2598
Douglas Gregor8f45df52009-04-16 22:23:12 +00002599 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002600 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002601 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002602 SmallVector<KindDeclIDPair, 64> Decls;
Aaron Ballman629afae2014-03-07 19:56:05 +00002603 for (const auto *D : DC->decls())
2604 Decls.push_back(std::make_pair(D->getKind(), GetDeclRef(D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002605
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002606 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002607 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002608 return Offset;
2609}
2610
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002611void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002612 using namespace llvm;
2613 RecordData Record;
2614
2615 // Write the type offsets array
2616 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002617 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002619 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2621 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2622 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002623 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002624 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002625 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002626 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002627
2628 // Write the declaration offsets array
2629 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002630 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2634 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2635 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002636 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002637 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002638 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002639 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002640}
2641
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002642void ASTWriter::WriteFileDeclIDsMap() {
2643 using namespace llvm;
2644 RecordData Record;
2645
2646 // Join the vectors of DeclIDs from all files.
2647 SmallVector<DeclID, 256> FileSortedIDs;
2648 for (FileDeclIDsTy::iterator
2649 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2650 DeclIDInFileInfo &Info = *FI->second;
2651 Info.FirstDeclIndex = FileSortedIDs.size();
2652 for (LocDeclIDsTy::iterator
2653 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2654 FileSortedIDs.push_back(DI->second);
2655 }
2656
2657 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2658 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2661 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2662 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002663 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002664 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2665}
2666
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002667void ASTWriter::WriteComments() {
2668 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002669 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002670 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002671 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2672 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002673 I != E; ++I) {
2674 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002675 AddSourceRange((*I)->getSourceRange(), Record);
2676 Record.push_back((*I)->getKind());
2677 Record.push_back((*I)->isTrailingComment());
2678 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002679 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2680 }
2681 Stream.ExitBlock();
2682}
2683
Douglas Gregorc5046832009-04-27 18:38:38 +00002684//===----------------------------------------------------------------------===//
2685// Global Method Pool and Selector Serialization
2686//===----------------------------------------------------------------------===//
2687
Douglas Gregore84a9da2009-04-20 20:36:09 +00002688namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002689// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002690class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002691 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002692
2693public:
2694 typedef Selector key_type;
2695 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002696
Sebastian Redl834bb972010-08-04 17:20:04 +00002697 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002698 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002699 ObjCMethodList Instance, Factory;
2700 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002701 typedef const data_type& data_type_ref;
2702
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002703 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002704
Douglas Gregorc78d3462009-04-24 21:10:55 +00002705 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002706 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002707 }
Mike Stump11289f42009-09-09 15:08:12 +00002708
2709 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002710 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002711 data_type_ref Methods) {
2712 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2713 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002714 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2715 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002716 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002717 if (Method->Method)
2718 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002719 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002720 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002721 if (Method->Method)
2722 DataLen += 4;
2723 clang::io::Emit16(Out, DataLen);
2724 return std::make_pair(KeyLen, DataLen);
2725 }
Mike Stump11289f42009-09-09 15:08:12 +00002726
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002727 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002728 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002729 assert((Start >> 32) == 0 && "Selector key offset too large");
2730 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002731 unsigned N = Sel.getNumArgs();
2732 clang::io::Emit16(Out, N);
2733 if (N == 0)
2734 N = 1;
2735 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002736 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002737 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2738 }
Mike Stump11289f42009-09-09 15:08:12 +00002739
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002740 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002741 data_type_ref Methods, unsigned DataLen) {
2742 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002743 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002744 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002745 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002746 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002747 if (Method->Method)
2748 ++NumInstanceMethods;
2749
2750 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002751 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002752 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002753 if (Method->Method)
2754 ++NumFactoryMethods;
2755
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002756 unsigned InstanceBits = Methods.Instance.getBits();
2757 assert(InstanceBits < 4);
2758 unsigned NumInstanceMethodsAndBits =
2759 (NumInstanceMethods << 2) | InstanceBits;
2760 unsigned FactoryBits = Methods.Factory.getBits();
2761 assert(FactoryBits < 4);
2762 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2763 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2764 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002765 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002766 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002767 if (Method->Method)
2768 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002769 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002770 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002771 if (Method->Method)
2772 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002773
2774 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002775 }
2776};
2777} // end anonymous namespace
2778
Sebastian Redla19a67f2010-08-03 21:58:15 +00002779/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002780///
2781/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002782/// in an on-disk hash table indexed by the selector. The hash table also
2783/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002784void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002785 using namespace llvm;
2786
Sebastian Redla19a67f2010-08-03 21:58:15 +00002787 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002788 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002789 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002790 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002791 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002792 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002793 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002794 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002795
Sebastian Redla19a67f2010-08-03 21:58:15 +00002796 // Create the on-disk hash table representation. We walk through every
2797 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002798 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002799 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002800 I = SelectorIDs.begin(), E = SelectorIDs.end();
2801 I != E; ++I) {
2802 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002803 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002804 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002805 I->second,
2806 ObjCMethodList(),
2807 ObjCMethodList()
2808 };
2809 if (F != SemaRef.MethodPool.end()) {
2810 Data.Instance = F->second.first;
2811 Data.Factory = F->second.second;
2812 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002813 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002814 // changed.
2815 if (Chain && I->second < FirstSelectorID) {
2816 // Selector already exists. Did it change?
2817 bool changed = false;
2818 for (ObjCMethodList *M = &Data.Instance; !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 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002824 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002825 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002826 changed = true;
2827 }
2828 if (!changed)
2829 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002830 } else if (Data.Instance.Method || Data.Factory.Method) {
2831 // A new method pool entry.
2832 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002833 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002834 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002835 }
2836
Douglas Gregorc78d3462009-04-24 21:10:55 +00002837 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002838 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002839 uint32_t BucketOffset;
2840 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002841 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002842 llvm::raw_svector_ostream Out(MethodPool);
2843 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002844 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002845 BucketOffset = Generator.Emit(Out, Trait);
2846 }
2847
2848 // Create a blob abbreviation
2849 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002850 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002851 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002852 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002853 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2854 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2855
Douglas Gregor95c13f52009-04-25 17:48:32 +00002856 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002857 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002858 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002859 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002860 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002861 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002862
2863 // Create a blob abbreviation for the selector table offsets.
2864 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002865 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002868 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2869 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2870
2871 // Write the selector offsets table.
2872 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002873 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002874 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002875 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002876 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002877 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002878 }
2879}
2880
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002881/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002882void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002883 using namespace llvm;
2884 if (SemaRef.ReferencedSelectors.empty())
2885 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002886
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002887 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002888
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002889 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002890 // very tricky to fix, and given that @selector shouldn't really appear in
2891 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002892 for (DenseMap<Selector, SourceLocation>::iterator S =
2893 SemaRef.ReferencedSelectors.begin(),
2894 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2895 Selector Sel = (*S).first;
2896 SourceLocation Loc = (*S).second;
2897 AddSelectorRef(Sel, Record);
2898 AddSourceLocation(Loc, Record);
2899 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002900 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002901}
2902
Douglas Gregorc5046832009-04-27 18:38:38 +00002903//===----------------------------------------------------------------------===//
2904// Identifier Table Serialization
2905//===----------------------------------------------------------------------===//
2906
Douglas Gregorc78d3462009-04-24 21:10:55 +00002907namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002908class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002909 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002910 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002911 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002912 bool IsModule;
2913
Douglas Gregor1d583f22009-04-28 21:18:29 +00002914 /// \brief Determines whether this is an "interesting" identifier
2915 /// that needs a full IdentifierInfo structure written into the hash
2916 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002917 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002918 if (II->isPoisoned() ||
2919 II->isExtensionToken() ||
2920 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002921 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002922 II->getFETokenInfo<void>())
2923 return true;
2924
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002925 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002926 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002927
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002928 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002929 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002930 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002931
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002932 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2933 if (!IsModule)
2934 return !shouldIgnoreMacro(Macro, IsModule, PP);
2935 SubmoduleID ModID;
2936 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2937 return true;
2938 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002939
2940 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002941 }
2942
Richard Smith49f906a2014-03-01 00:08:04 +00002943 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2944
2945 MacroDirective *
2946 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002947 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002948 llvm::SmallVector<SubmoduleID, 1> Overridden;
2949 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2950 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2951 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002952 return 0;
2953 }
2954
Richard Smith49f906a2014-03-01 00:08:04 +00002955 MacroDirective *
2956 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2957 OverriddenList &Overridden) {
2958 if (MacroDirective *NextMD =
2959 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2960 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2961 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002962 return 0;
2963 }
2964
2965 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00002966 /// public macro definition or undefinition that is not in ModID.
2967 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002968 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00002969 /// ModID is updated to the module containing the returned directive.
2970 ///
2971 /// FIXME: This process breaks down if a module defines a macro, imports
2972 /// another submodule that changes the macro, then changes the
2973 /// macro again itself.
2974 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2975 SubmoduleID &ModID,
2976 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002977 if (!MD)
2978 return 0;
2979
Richard Smith49f906a2014-03-01 00:08:04 +00002980 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002981 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002982 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002983 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002984 SubmoduleID ThisModID = getSubmoduleID(MD);
2985 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00002986 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002987 continue;
2988 }
Richard Smith49f906a2014-03-01 00:08:04 +00002989 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002990 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002991 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002992 }
Richard Smith49f906a2014-03-01 00:08:04 +00002993
2994 // If this is a definition from a submodule import, that submodule's
2995 // definition is overridden by the definition or undefinition that we
2996 // started with.
2997 // FIXME: This should only apply to macros defined in OrigModID.
2998 // We can't do that currently, because a #include of a different submodule
2999 // of the same module just leaks through macros instead of providing new
3000 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00003001 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3002 // Figure out which submodule the macro was originally defined within.
3003 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3004 if (!SourceID) {
3005 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3006 if (DefLoc == MD->getLocation())
3007 SourceID = ThisModID;
3008 else
3009 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3010 }
3011 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003012 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003013 }
Richard Smith49f906a2014-03-01 00:08:04 +00003014
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003015 // We are looking for a definition in a different submodule than the one
3016 // that we started with. If a submodule has re-definitions of the same
3017 // macro, only the last definition will be used as the "exported" one.
3018 if (ModID == OrigModID)
3019 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003020
Richard Smith49f906a2014-03-01 00:08:04 +00003021 // The latest visibility directive for a name in a submodule affects all
3022 // the directives that come before it.
3023 if (VisibilityMacroDirective *VisMD =
3024 dyn_cast<VisibilityMacroDirective>(MD)) {
3025 if (!IsPublic.hasValue())
3026 IsPublic = VisMD->isPublic();
3027 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3028 // FIXME: If we find an imported macro, we should include its list of
3029 // overrides in our export.
3030 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003031 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003032 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003033
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003034 return 0;
3035 }
3036
3037 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003038 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003039 }
3040
Douglas Gregore84a9da2009-04-20 20:36:09 +00003041public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003042 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003043 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003044
Sebastian Redl539c5062010-08-18 23:57:32 +00003045 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003046 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003047
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003048 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3049 IdentifierResolver &IdResolver, bool IsModule)
3050 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003051
3052 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003053 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003054 }
Mike Stump11289f42009-09-09 15:08:12 +00003055
3056 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003057 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003058 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003059 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003060 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003061 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003062 DataLen += 2; // 2 bytes for builtin ID
3063 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003064 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003065 DataLen += 4; // MacroDirectives offset.
3066 if (IsModule) {
3067 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003068 llvm::SmallVector<SubmoduleID, 4> Overridden;
3069 for (MacroDirective *
3070 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3071 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3072 // Previous macro's overrides.
3073 if (!Overridden.empty())
3074 DataLen += 4 * (1 + Overridden.size());
3075 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003076 }
Richard Smith49f906a2014-03-01 00:08:04 +00003077 // Previous macro's overrides.
3078 if (!Overridden.empty())
3079 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003080 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003081 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003082 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003083
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003084 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3085 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003086 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003087 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003088 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003089 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003090 // We emit the key length after the data length so that every
3091 // string is preceded by a 16-bit length. This matches the PTH
3092 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003093 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003094 return std::make_pair(KeyLen, DataLen);
3095 }
Mike Stump11289f42009-09-09 15:08:12 +00003096
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003097 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003098 unsigned KeyLen) {
3099 // Record the location of the key data. This is used when generating
3100 // the mapping from persistent IDs to strings.
3101 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003102 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003103 }
Mike Stump11289f42009-09-09 15:08:12 +00003104
Richard Smith49f906a2014-03-01 00:08:04 +00003105 static void emitMacroOverrides(raw_ostream &Out,
3106 llvm::ArrayRef<SubmoduleID> Overridden) {
3107 if (!Overridden.empty()) {
3108 clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
3109 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3110 clang::io::Emit32(Out, Overridden[I]);
3111 }
3112 }
3113
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003114 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003115 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003116 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003117 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003118 clang::io::Emit32(Out, ID << 1);
3119 return;
3120 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003121
Douglas Gregor1d583f22009-04-28 21:18:29 +00003122 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003123 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3124 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3125 clang::io::Emit16(Out, Bits);
3126 Bits = 0;
3127 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003128 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003129 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003130 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3131 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003132 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003133 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003134 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003135
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003136 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003137 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3138 if (IsModule) {
3139 // Write the IDs of macros coming from different submodules.
3140 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003141 llvm::SmallVector<SubmoduleID, 4> Overridden;
3142 for (MacroDirective *
3143 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3144 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3145 MacroID InfoID = 0;
3146 emitMacroOverrides(Out, Overridden);
3147 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3148 InfoID = Writer.getMacroID(DefMD->getInfo());
3149 assert(InfoID);
3150 clang::io::Emit32(Out, InfoID << 1);
3151 } else {
3152 assert(isa<UndefMacroDirective>(MD));
3153 clang::io::Emit32(Out, (ModID << 1) | 1);
3154 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003155 }
Richard Smith49f906a2014-03-01 00:08:04 +00003156 emitMacroOverrides(Out, Overridden);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003157 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003158 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003159 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003160
Douglas Gregora868bbd2009-04-21 22:25:48 +00003161 // Emit the declaration IDs in reverse order, because the
3162 // IdentifierResolver provides the declarations as they would be
3163 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003164 // "stat"), but the ASTReader adds declarations to the end of the list
3165 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003166 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003167 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3168 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003169 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003170 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003171 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003172 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3173 }
3174
3175 /// \brief Returns the most recent local decl or the given decl if there are
3176 /// no local ones. The given decl is assumed to be the most recent one.
3177 Decl *getMostRecentLocalDecl(Decl *Orig) {
3178 // The only way a "from AST file" decl would be more recent from a local one
3179 // is if it came from a module.
3180 if (!PP.getLangOpts().Modules)
3181 return Orig;
3182
3183 // Look for a local in the decl chain.
3184 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3185 if (!D->isFromASTFile())
3186 return D;
3187 // If we come up a decl from a (chained-)PCH stop since we won't find a
3188 // local one.
3189 if (D->getOwningModuleID() == 0)
3190 break;
3191 }
3192
3193 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003194 }
3195};
3196} // end anonymous namespace
3197
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003198/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003199///
3200/// The identifier table consists of a blob containing string data
3201/// (the actual identifiers themselves) and a separate "offsets" index
3202/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003203void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3204 IdentifierResolver &IdResolver,
3205 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003206 using namespace llvm;
3207
3208 // Create and write out the blob that contains the identifier
3209 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003210 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003211 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003212 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003213
Douglas Gregore6648fb2009-04-28 20:33:11 +00003214 // Look for any identifiers that were named while processing the
3215 // headers, but are otherwise not needed. We add these to the hash
3216 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003217 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003218 // file.
3219 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3220 IDEnd = PP.getIdentifierTable().end();
3221 ID != IDEnd; ++ID)
3222 getIdentifierRef(ID->second);
3223
Sebastian Redlff4a2952010-07-23 23:49:55 +00003224 // Create the on-disk hash table representation. We only store offsets
3225 // for identifiers that appear here for the first time.
3226 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003227 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003228 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3229 ID != IDEnd; ++ID) {
3230 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003231 if (!Chain || !ID->first->isFromAST() ||
3232 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003233 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003234 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003235 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003236
Douglas Gregore84a9da2009-04-20 20:36:09 +00003237 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003238 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003239 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003240 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003241 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003242 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003243 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003244 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003245 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003246 }
3247
3248 // Create a blob abbreviation
3249 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003250 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003253 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003254
3255 // Write the identifier table
3256 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003257 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003258 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003259 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003260 }
3261
3262 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003263 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003264 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3268 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3269
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003270#ifndef NDEBUG
3271 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3272 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3273#endif
3274
Douglas Gregor0e149972009-04-25 19:10:14 +00003275 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003276 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003277 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003278 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003279 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003280 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003281}
3282
Douglas Gregorc5046832009-04-27 18:38:38 +00003283//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003284// DeclContext's Name Lookup Table Serialization
3285//===----------------------------------------------------------------------===//
3286
3287namespace {
3288// Trait used for the on-disk hash table used in the method pool.
3289class ASTDeclContextNameLookupTrait {
3290 ASTWriter &Writer;
3291
3292public:
3293 typedef DeclarationName key_type;
3294 typedef key_type key_type_ref;
3295
3296 typedef DeclContext::lookup_result data_type;
3297 typedef const data_type& data_type_ref;
3298
3299 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3300
3301 unsigned ComputeHash(DeclarationName Name) {
3302 llvm::FoldingSetNodeID ID;
3303 ID.AddInteger(Name.getNameKind());
3304
3305 switch (Name.getNameKind()) {
3306 case DeclarationName::Identifier:
3307 ID.AddString(Name.getAsIdentifierInfo()->getName());
3308 break;
3309 case DeclarationName::ObjCZeroArgSelector:
3310 case DeclarationName::ObjCOneArgSelector:
3311 case DeclarationName::ObjCMultiArgSelector:
3312 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3313 break;
3314 case DeclarationName::CXXConstructorName:
3315 case DeclarationName::CXXDestructorName:
3316 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003317 break;
3318 case DeclarationName::CXXOperatorName:
3319 ID.AddInteger(Name.getCXXOverloadedOperator());
3320 break;
3321 case DeclarationName::CXXLiteralOperatorName:
3322 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3323 case DeclarationName::CXXUsingDirective:
3324 break;
3325 }
3326
3327 return ID.ComputeHash();
3328 }
3329
3330 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003331 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003332 data_type_ref Lookup) {
3333 unsigned KeyLen = 1;
3334 switch (Name.getNameKind()) {
3335 case DeclarationName::Identifier:
3336 case DeclarationName::ObjCZeroArgSelector:
3337 case DeclarationName::ObjCOneArgSelector:
3338 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003339 case DeclarationName::CXXLiteralOperatorName:
3340 KeyLen += 4;
3341 break;
3342 case DeclarationName::CXXOperatorName:
3343 KeyLen += 1;
3344 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003345 case DeclarationName::CXXConstructorName:
3346 case DeclarationName::CXXDestructorName:
3347 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003348 case DeclarationName::CXXUsingDirective:
3349 break;
3350 }
3351 clang::io::Emit16(Out, KeyLen);
3352
3353 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003354 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003355 clang::io::Emit16(Out, DataLen);
3356
3357 return std::make_pair(KeyLen, DataLen);
3358 }
3359
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003360 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003361 using namespace clang::io;
3362
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003363 Emit8(Out, Name.getNameKind());
3364 switch (Name.getNameKind()) {
3365 case DeclarationName::Identifier:
3366 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003367 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003368 case DeclarationName::ObjCZeroArgSelector:
3369 case DeclarationName::ObjCOneArgSelector:
3370 case DeclarationName::ObjCMultiArgSelector:
3371 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003372 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003373 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003374 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3375 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003376 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003377 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003378 case DeclarationName::CXXLiteralOperatorName:
3379 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003380 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003381 case DeclarationName::CXXConstructorName:
3382 case DeclarationName::CXXDestructorName:
3383 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003384 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003385 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003386 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003387
3388 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003389 }
3390
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003391 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003392 data_type Lookup, unsigned DataLen) {
3393 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003394 clang::io::Emit16(Out, Lookup.size());
3395 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3396 I != E; ++I)
3397 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003398
3399 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3400 }
3401};
3402} // end anonymous namespace
3403
Richard Smith961eae52014-03-25 01:14:22 +00003404uint32_t
3405ASTWriter::GenerateNameLookupTable(const DeclContext *DC,
3406 llvm::SmallVectorImpl<char> &LookupTable) {
3407 assert(!DC->LookupPtr.getInt() && "must call buildLookups first");
3408 assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3409
3410 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3411 ASTDeclContextNameLookupTrait Trait(*this);
3412
3413 // Create the on-disk hash table representation.
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003414 DeclarationName ConstructorName;
Richard Smith961eae52014-03-25 01:14:22 +00003415 DeclarationName ConversionName;
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003416 SmallVector<NamedDecl *, 8> ConstructorDecls;
Richard Smith961eae52014-03-25 01:14:22 +00003417 SmallVector<NamedDecl *, 4> ConversionDecls;
3418
3419 auto AddLookupResult = [&](DeclarationName Name,
3420 DeclContext::lookup_result Result) {
3421 if (Result.empty())
3422 return;
3423
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003424 // Different DeclarationName values of certain kinds are mapped to
3425 // identical serialized keys, because we don't want to use type
3426 // identifiers in the keys (since type ids are local to the module).
3427 switch (Name.getNameKind()) {
3428 case DeclarationName::CXXConstructorName:
3429 // There may be different CXXConstructorName DeclarationName values
3430 // in a DeclContext because a UsingDecl that inherits constructors
3431 // has the DeclarationName of the inherited constructors.
3432 if (!ConstructorName)
3433 ConstructorName = Name;
3434 ConstructorDecls.append(Result.begin(), Result.end());
3435 return;
3436 case DeclarationName::CXXConversionFunctionName:
Richard Smith961eae52014-03-25 01:14:22 +00003437 if (!ConversionName)
3438 ConversionName = Name;
3439 ConversionDecls.append(Result.begin(), Result.end());
3440 return;
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003441 default:
3442 break;
Richard Smith961eae52014-03-25 01:14:22 +00003443 }
3444
3445 Generator.insert(Name, Result, Trait);
3446 };
3447
3448 SmallVector<DeclarationName, 16> ExternalNames;
3449 for (auto &Lookup : *DC->getLookupPtr()) {
3450 if (Lookup.second.hasExternalDecls() ||
3451 DC->NeedToReconcileExternalVisibleStorage) {
3452 // We don't know for sure what declarations are found by this name,
3453 // because the external source might have a different set from the set
3454 // that are in the lookup map, and we can't update it now without
3455 // risking invalidating our lookup iterator. So add it to a queue to
3456 // deal with later.
3457 ExternalNames.push_back(Lookup.first);
3458 continue;
3459 }
3460
3461 AddLookupResult(Lookup.first, Lookup.second.getLookupResult());
3462 }
3463
3464 // Add the names we needed to defer. Note, this shouldn't add any new decls
3465 // to the list we need to serialize: any new declarations we find here should
3466 // be imported from an external source.
3467 // FIXME: What if the external source isn't an ASTReader?
3468 for (const auto &Name : ExternalNames)
3469 // FIXME: const_cast since OnDiskHashTable wants a non-const lookup result.
3470 AddLookupResult(Name, const_cast<DeclContext*>(DC)->lookup(Name));
3471
Stephan Tolksdorfa6a08632014-03-27 19:22:19 +00003472 // Add the constructors.
3473 if (!ConstructorDecls.empty()) {
3474 Generator.insert(ConstructorName,
3475 DeclContext::lookup_result(ConstructorDecls.begin(),
3476 ConstructorDecls.end()),
3477 Trait);
3478 }
3479 // Add the conversion functions.
Richard Smith961eae52014-03-25 01:14:22 +00003480 if (!ConversionDecls.empty()) {
3481 Generator.insert(ConversionName,
3482 DeclContext::lookup_result(ConversionDecls.begin(),
3483 ConversionDecls.end()),
3484 Trait);
3485 }
3486
3487 // Create the on-disk hash table in a buffer.
3488 llvm::raw_svector_ostream Out(LookupTable);
3489 // Make sure that no bucket is at offset 0
3490 clang::io::Emit32(Out, 0);
3491 return Generator.Emit(Out, Trait);
3492}
3493
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003494/// \brief Write the block containing all of the declaration IDs
3495/// visible from the given DeclContext.
3496///
3497/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003498/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003499uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3500 DeclContext *DC) {
3501 if (DC->getPrimaryContext() != DC)
3502 return 0;
3503
3504 // Since there is no name lookup into functions or methods, don't bother to
3505 // build a visible-declarations table for these entities.
3506 if (DC->isFunctionOrMethod())
3507 return 0;
3508
3509 // If not in C++, we perform name lookup for the translation unit via the
3510 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003511 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003512 return 0;
3513
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003514 // Serialize the contents of the mapping used for lookup. Note that,
3515 // although we have two very different code paths, the serialized
3516 // representation is the same for both cases: a declaration name,
3517 // followed by a size, followed by references to the visible
3518 // declarations that have that name.
3519 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003520 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003521 if (!Map || Map->empty())
3522 return 0;
3523
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003524 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003525 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003526 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003527
3528 // Write the lookup table
3529 RecordData Record;
3530 Record.push_back(DECL_CONTEXT_VISIBLE);
3531 Record.push_back(BucketOffset);
3532 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3533 LookupTable.str());
3534
3535 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3536 ++NumVisibleDeclContexts;
3537 return Offset;
3538}
3539
Sebastian Redla4071b42010-08-24 00:50:09 +00003540/// \brief Write an UPDATE_VISIBLE block for the given context.
3541///
3542/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3543/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003544/// (in C++), for namespaces, and for classes with forward-declared unscoped
3545/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003546void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Richard Smith961eae52014-03-25 01:14:22 +00003547 StoredDeclsMap *Map = DC->getLookupPtr();
Sebastian Redla4071b42010-08-24 00:50:09 +00003548 if (!Map || Map->empty())
3549 return;
3550
Sebastian Redla4071b42010-08-24 00:50:09 +00003551 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003552 SmallString<4096> LookupTable;
Richard Smith961eae52014-03-25 01:14:22 +00003553 uint32_t BucketOffset = GenerateNameLookupTable(DC, LookupTable);
Sebastian Redla4071b42010-08-24 00:50:09 +00003554
3555 // Write the lookup table
3556 RecordData Record;
3557 Record.push_back(UPDATE_VISIBLE);
3558 Record.push_back(getDeclID(cast<Decl>(DC)));
3559 Record.push_back(BucketOffset);
3560 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3561}
3562
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003563/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3564void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3565 RecordData Record;
3566 Record.push_back(Opts.fp_contract);
3567 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3568}
3569
3570/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3571void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003572 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003573 return;
3574
3575 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3576 RecordData Record;
3577#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3578#include "clang/Basic/OpenCLExtensions.def"
3579 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3580}
3581
Douglas Gregor358cd442012-01-15 16:58:34 +00003582void ASTWriter::WriteRedeclarations() {
3583 RecordData LocalRedeclChains;
3584 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3585
3586 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3587 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003588 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003589
3590 Decl *MostRecent = First->getMostRecentDecl();
3591
3592 // If we only have a single declaration, there is no point in storing
3593 // a redeclaration chain.
3594 if (First == MostRecent)
3595 continue;
3596
3597 unsigned Offset = LocalRedeclChains.size();
3598 unsigned Size = 0;
3599 LocalRedeclChains.push_back(0); // Placeholder for the size.
3600
3601 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003602 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003603 Prev = Prev->getPreviousDecl()) {
3604 if (!Prev->isFromASTFile()) {
3605 AddDeclRef(Prev, LocalRedeclChains);
3606 ++Size;
3607 }
3608 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003609
3610 if (!First->isFromASTFile() && Chain) {
3611 Decl *FirstFromAST = MostRecent;
3612 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3613 if (Prev->isFromASTFile())
3614 FirstFromAST = Prev;
3615 }
3616
3617 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3618 }
3619
Douglas Gregor358cd442012-01-15 16:58:34 +00003620 LocalRedeclChains[Offset] = Size;
3621
3622 // Reverse the set of local redeclarations, so that we store them in
3623 // order (since we found them in reverse order).
3624 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3625
Douglas Gregor6168bd22013-02-18 15:53:43 +00003626 // Add the mapping from the first ID from the AST to the set of local
3627 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003628 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3629 LocalRedeclsMap.push_back(Info);
3630
3631 assert(N == Redeclarations.size() &&
3632 "Deserialized a declaration we shouldn't have");
3633 }
3634
3635 if (LocalRedeclChains.empty())
3636 return;
3637
3638 // Sort the local redeclarations map by the first declaration ID,
3639 // since the reader will be performing binary searches on this information.
3640 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3641
3642 // Emit the local redeclarations map.
3643 using namespace llvm;
3644 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3645 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3647 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3648 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3649
3650 RecordData Record;
3651 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3652 Record.push_back(LocalRedeclsMap.size());
3653 Stream.EmitRecordWithBlob(AbbrevID, Record,
3654 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3655 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3656
3657 // Emit the redeclaration chains.
3658 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3659}
3660
Douglas Gregor404cdde2012-01-27 01:47:08 +00003661void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003662 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003663 RecordData Categories;
3664
3665 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3666 unsigned Size = 0;
3667 unsigned StartIndex = Categories.size();
3668
3669 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3670
3671 // Allocate space for the size.
3672 Categories.push_back(0);
3673
3674 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003675 for (ObjCInterfaceDecl::known_categories_iterator
3676 Cat = Class->known_categories_begin(),
3677 CatEnd = Class->known_categories_end();
3678 Cat != CatEnd; ++Cat, ++Size) {
3679 assert(getDeclID(*Cat) != 0 && "Bogus category");
3680 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003681 }
3682
3683 // Update the size.
3684 Categories[StartIndex] = Size;
3685
3686 // Record this interface -> category map.
3687 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3688 CategoriesMap.push_back(CatInfo);
3689 }
3690
3691 // Sort the categories map by the definition ID, since the reader will be
3692 // performing binary searches on this information.
3693 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3694
3695 // Emit the categories map.
3696 using namespace llvm;
3697 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3698 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3701 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3702
3703 RecordData Record;
3704 Record.push_back(OBJC_CATEGORIES_MAP);
3705 Record.push_back(CategoriesMap.size());
3706 Stream.EmitRecordWithBlob(AbbrevID, Record,
3707 reinterpret_cast<char*>(CategoriesMap.data()),
3708 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3709
3710 // Emit the category lists.
3711 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3712}
3713
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003714void ASTWriter::WriteMergedDecls() {
3715 if (!Chain || Chain->MergedDecls.empty())
3716 return;
3717
3718 RecordData Record;
3719 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3720 IEnd = Chain->MergedDecls.end();
3721 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003722 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003723 : getDeclID(I->first);
3724 assert(CanonID && "Merged declaration not known?");
3725
3726 Record.push_back(CanonID);
3727 Record.push_back(I->second.size());
3728 Record.append(I->second.begin(), I->second.end());
3729 }
3730 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3731}
3732
Richard Smithe40f2ba2013-08-07 21:41:30 +00003733void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3734 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3735
3736 if (LPTMap.empty())
3737 return;
3738
3739 RecordData Record;
3740 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3741 ItEnd = LPTMap.end();
3742 It != ItEnd; ++It) {
3743 LateParsedTemplate *LPT = It->second;
3744 AddDeclRef(It->first, Record);
3745 AddDeclRef(LPT->D, Record);
3746 Record.push_back(LPT->Toks.size());
3747
3748 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3749 TokEnd = LPT->Toks.end();
3750 TokIt != TokEnd; ++TokIt) {
3751 AddToken(*TokIt, Record);
3752 }
3753 }
3754 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3755}
3756
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003757//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003758// General Serialization Routines
3759//===----------------------------------------------------------------------===//
3760
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003761/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003762void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3763 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003764 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003765 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3766 e = Attrs.end(); i != e; ++i){
3767 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003768 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003769 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003770
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003771#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003772
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003773 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003774}
3775
John McCallf413f5e2013-05-03 00:10:13 +00003776void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3777 AddSourceLocation(Tok.getLocation(), Record);
3778 Record.push_back(Tok.getLength());
3779
3780 // FIXME: When reading literal tokens, reconstruct the literal pointer
3781 // if it is needed.
3782 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3783 // FIXME: Should translate token kind to a stable encoding.
3784 Record.push_back(Tok.getKind());
3785 // FIXME: Should translate token flags to a stable encoding.
3786 Record.push_back(Tok.getFlags());
3787}
3788
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003789void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003790 Record.push_back(Str.size());
3791 Record.insert(Record.end(), Str.begin(), Str.end());
3792}
3793
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003794void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3795 RecordDataImpl &Record) {
3796 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003797 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003798 Record.push_back(*Minor + 1);
3799 else
3800 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003801 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003802 Record.push_back(*Subminor + 1);
3803 else
3804 Record.push_back(0);
3805}
3806
Douglas Gregore84a9da2009-04-20 20:36:09 +00003807/// \brief Note that the identifier II occurs at the given offset
3808/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003809void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003810 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003811 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003812 // up earlier in the chain and thus don't need an offset.
3813 if (ID >= FirstIdentID)
3814 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003815}
3816
Douglas Gregor95c13f52009-04-25 17:48:32 +00003817/// \brief Note that the selector Sel occurs at the given offset
3818/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003819void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003820 unsigned ID = SelectorIDs[Sel];
3821 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003822 // Don't record offsets for selectors that are also available in a different
3823 // file.
3824 if (ID < FirstSelectorID)
3825 return;
3826 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003827}
3828
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003829ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003830 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003831 WritingAST(false), DoneWritingDeclsAndTypes(false),
3832 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003833 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003834 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003835 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3836 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003837 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3838 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003839 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003840 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003841 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003842 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003843 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003844 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003845 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3846 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3847 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003848 DeclTypedefAbbrev(0),
3849 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3850 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003851{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003852}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003853
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003854ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003855 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003856}
3857
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003858void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003859 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003860 Module *WritingModule, StringRef isysroot,
3861 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003862 WritingAST = true;
3863
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003864 ASTHasCompilerErrors = hasErrors;
3865
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003866 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003867 Stream.Emit((unsigned)'C', 8);
3868 Stream.Emit((unsigned)'P', 8);
3869 Stream.Emit((unsigned)'C', 8);
3870 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003871
Chris Lattner28fa4e62009-04-26 22:26:21 +00003872 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003873
Douglas Gregoreda8e122011-08-09 15:13:55 +00003874 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003875 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003876 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003877 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003878 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003879 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003880 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003881
3882 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003883}
3884
Douglas Gregora94a1542011-07-27 21:45:57 +00003885template<typename Vector>
3886static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3887 ASTWriter::RecordData &Record) {
3888 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3889 I != E; ++I) {
3890 Writer.AddDeclRef(*I, Record);
3891 }
3892}
3893
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003894void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003895 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003896 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003897 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003898 using namespace llvm;
3899
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003900 bool isModule = WritingModule != 0;
3901
Douglas Gregorcf68c582011-12-01 22:20:10 +00003902 // Make sure that the AST reader knows to finalize itself.
3903 if (Chain)
3904 Chain->finalizeForWriting();
3905
Sebastian Redl143413f2010-07-12 22:02:52 +00003906 ASTContext &Context = SemaRef.Context;
3907 Preprocessor &PP = SemaRef.PP;
3908
Douglas Gregordab42432011-08-12 00:15:20 +00003909 // Set up predefined declaration IDs.
3910 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003911 if (Context.ObjCIdDecl)
3912 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003913 if (Context.ObjCSelDecl)
3914 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003915 if (Context.ObjCClassDecl)
3916 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003917 if (Context.ObjCProtocolClassDecl)
3918 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003919 if (Context.Int128Decl)
3920 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3921 if (Context.UInt128Decl)
3922 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003923 if (Context.ObjCInstanceTypeDecl)
3924 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003925 if (Context.BuiltinVaListDecl)
3926 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3927
Douglas Gregor851443c2011-08-12 01:39:19 +00003928 if (!Chain) {
3929 // Make sure that we emit IdentifierInfos (and any attached
3930 // declarations) for builtins. We don't need to do this when we're
3931 // emitting chained PCH files, because all of the builtins will be
3932 // in the original PCH file.
3933 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003934 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003935 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003936 if (!Context.getLangOpts().NoBuiltin) {
3937 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3938 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003939 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3940 getIdentifierRef(&Table.get(BuiltinNames[I]));
3941 }
3942
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003943 // If there are any out-of-date identifiers, bring them up to date.
3944 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003945 // Find out-of-date identifiers.
3946 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003947 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3948 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003949 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003950 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003951 OutOfDate.push_back(ID->second);
3952 }
3953
3954 // Update the out-of-date identifiers.
3955 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3956 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3957 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003958 }
3959
Chris Lattner0c797362009-09-08 18:19:27 +00003960 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003961 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003962 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003963 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003964 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003965
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003966 // Build a record containing all of the file scoped decls in this file.
3967 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003968 if (!isModule)
3969 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3970 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003971
Douglas Gregor851443c2011-08-12 01:39:19 +00003972 // Build a record containing all of the delegating constructors we still need
3973 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003974 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003975 if (!isModule)
3976 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003977
Douglas Gregor851443c2011-08-12 01:39:19 +00003978 // Write the set of weak, undeclared identifiers. We always write the
3979 // entire table, since later PCH files in a PCH chain are only interested in
3980 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003981 RecordData WeakUndeclaredIdentifiers;
3982 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003983 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003984 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3985 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3986 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3987 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3988 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3989 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3990 }
3991 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003992
Richard Smith78165b52013-01-10 23:43:47 +00003993 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003994 // declarations in this header file. Generally, this record will be
3995 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003996 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003997 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003998 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003999 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00004000 TD = SemaRef.LocallyScopedExternCDecls.begin(),
4001 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00004002 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004003 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00004004 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00004005 }
4006
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004007 // Build a record containing all of the ext_vector declarations.
4008 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004009 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004010
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004011 // Build a record containing all of the VTable uses information.
4012 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004013 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004014 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4015 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4016 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4017 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4018 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004019 }
4020
4021 // Build a record containing all of dynamic classes declarations.
4022 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004023 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004024
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004025 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004026 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004027 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004028 I = SemaRef.PendingInstantiations.begin(),
4029 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4030 AddDeclRef(I->first, PendingInstantiations);
4031 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004032 }
4033 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4034 "There are local ones at end of translation unit!");
4035
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004036 // Build a record containing some declaration references.
4037 RecordData SemaDeclRefs;
4038 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4039 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4040 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4041 }
4042
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004043 RecordData CUDASpecialDeclRefs;
4044 if (Context.getcudaConfigureCallDecl()) {
4045 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4046 }
4047
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004048 // Build a record containing all of the known namespaces.
4049 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004050 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004051 I = SemaRef.KnownNamespaces.begin(),
4052 IEnd = SemaRef.KnownNamespaces.end();
4053 I != IEnd; ++I) {
4054 if (!I->second)
4055 AddDeclRef(I->first, KnownNamespaces);
4056 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004057
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004058 // Build a record of all used, undefined objects that require definitions.
4059 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004060
4061 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004062 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004063 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4064 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004065 AddDeclRef(I->first, UndefinedButUsed);
4066 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004067 }
4068
Douglas Gregor112b9072012-10-18 05:31:06 +00004069 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004070 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004071
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004072 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004073 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004074 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004075
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004076 // This is so that older clang versions, before the introduction
4077 // of the control block, can read and reject the newer PCH format.
4078 Record.clear();
4079 Record.push_back(VERSION_MAJOR);
4080 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4081
Douglas Gregor851443c2011-08-12 01:39:19 +00004082 // Create a lexical update block containing all of the declarations in the
4083 // translation unit that do not come from other AST files.
4084 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4085 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Aaron Ballman629afae2014-03-07 19:56:05 +00004086 for (const auto *I : TU->noload_decls()) {
4087 if (!I->isFromASTFile())
4088 NewGlobalDecls.push_back(std::make_pair(I->getKind(), GetDeclRef(I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004089 }
4090
4091 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4092 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4093 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4094 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4095 Record.clear();
4096 Record.push_back(TU_UPDATE_LEXICAL);
4097 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4098 data(NewGlobalDecls));
4099
4100 // And a visible updates block for the translation unit.
4101 Abv = new llvm::BitCodeAbbrev();
4102 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4103 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4104 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4105 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4106 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4107 WriteDeclContextVisibleUpdate(TU);
4108
4109 // If the translation unit has an anonymous namespace, and we don't already
4110 // have an update block for it, write it as an update block.
Richard Smith6ef42932014-03-20 21:02:00 +00004111 // FIXME: Why do we not do this if there's already an update block?
Douglas Gregor851443c2011-08-12 01:39:19 +00004112 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4113 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
Richard Smith6ef42932014-03-20 21:02:00 +00004114 if (Record.empty())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004115 Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004116 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004117
Richard Smith5652c0f2014-03-21 01:48:23 +00004118 // Add update records for all mangling numbers and static local numbers.
4119 // These aren't really update records, but this is a convenient way of
4120 // tagging this rare extra data onto the declarations.
4121 for (const auto &Number : Context.MangleNumbers)
4122 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004123 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4124 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004125 for (const auto &Number : Context.StaticLocalNumbers)
4126 if (!Number.first->isFromASTFile())
Aaron Ballman4f45b712014-03-21 15:22:56 +00004127 DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4128 Number.second));
Richard Smith5652c0f2014-03-21 01:48:23 +00004129
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004130 // Make sure visible decls, added to DeclContexts previously loaded from
4131 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004132 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004133 I = UpdatingVisibleDecls.begin(),
4134 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4135 GetDeclRef(*I);
4136 }
4137
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004138 // Make sure all decls associated with an identifier are registered for
4139 // serialization.
4140 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4141 IDEnd = PP.getIdentifierTable().end();
4142 ID != IDEnd; ++ID) {
4143 const IdentifierInfo *II = ID->second;
4144 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4145 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4146 DEnd = SemaRef.IdResolver.end();
4147 D != DEnd; ++D) {
4148 GetDeclRef(*D);
4149 }
4150 }
4151 }
4152
Douglas Gregor5204bde2011-08-02 16:26:37 +00004153 // Form the record of special types.
4154 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004155 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004156 AddTypeRef(Context.getFILEType(), SpecialTypes);
4157 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4158 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4159 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4160 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004161 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004162 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004163
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004164 if (Chain) {
4165 // Write the mapping information describing our module dependencies and how
4166 // each of those modules were mapped into our own offset/ID space, so that
4167 // the reader can build the appropriate mapping to its own offset/ID space.
4168 // The map consists solely of a blob with the following format:
4169 // *(module-name-len:i16 module-name:len*i8
4170 // source-location-offset:i32
4171 // identifier-id:i32
4172 // preprocessed-entity-id:i32
4173 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004174 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004175 // selector-id:i32
4176 // declaration-id:i32
4177 // c++-base-specifiers-id:i32
4178 // type-id:i32)
4179 //
4180 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4181 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4183 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004184 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004185 {
4186 llvm::raw_svector_ostream Out(Buffer);
4187 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004188 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004189 M != MEnd; ++M) {
4190 StringRef FileName = (*M)->FileName;
4191 io::Emit16(Out, FileName.size());
4192 Out.write(FileName.data(), FileName.size());
4193 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4194 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004195 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004196 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004197 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004198 io::Emit32(Out, (*M)->BaseSelectorID);
4199 io::Emit32(Out, (*M)->BaseDeclID);
4200 io::Emit32(Out, (*M)->BaseTypeIndex);
4201 }
4202 }
4203 Record.clear();
4204 Record.push_back(MODULE_OFFSET_MAP);
4205 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4206 Buffer.data(), Buffer.size());
4207 }
Richard Smithb9eab6d2014-03-20 19:44:17 +00004208
Richard Smithb9eab6d2014-03-20 19:44:17 +00004209 RecordData DeclUpdatesOffsetsRecord;
4210
Richard Smith59442b42014-03-20 20:07:19 +00004211 // Keep writing types, declarations, and declaration update records
4212 // until we've emitted all of them.
Richard Smithb9eab6d2014-03-20 19:44:17 +00004213 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
4214 WriteDeclsBlockAbbrevs();
4215 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4216 E = DeclsToRewrite.end();
4217 I != E; ++I)
4218 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Richard Smith59442b42014-03-20 20:07:19 +00004219 do {
4220 WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4221 while (!DeclTypesToEmit.empty()) {
4222 DeclOrType DOT = DeclTypesToEmit.front();
4223 DeclTypesToEmit.pop();
4224 if (DOT.isType())
4225 WriteType(DOT.getType());
4226 else
4227 WriteDecl(Context, DOT.getDecl());
4228 }
4229 } while (!DeclUpdates.empty());
Richard Smithb9eab6d2014-03-20 19:44:17 +00004230 Stream.ExitBlock();
4231
Richard Smithb9eab6d2014-03-20 19:44:17 +00004232 DoneWritingDeclsAndTypes = true;
4233
4234 // These things can only be done once we've written out decls and types.
4235 WriteTypeDeclOffsets();
Richard Smith5652c0f2014-03-21 01:48:23 +00004236 if (!DeclUpdatesOffsetsRecord.empty())
4237 Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
Richard Smithb9eab6d2014-03-20 19:44:17 +00004238 WriteCXXBaseSpecifiersOffsets();
4239 WriteFileDeclIDsMap();
4240 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
4241
4242 WriteComments();
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004243 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004244 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004245 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004246 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004247 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004248 WriteFPPragmaOptions(SemaRef.getFPOptions());
4249 WriteOpenCLExtensions(SemaRef);
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004250 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004251
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004252 // If we're emitting a module, write out the submodule information.
4253 if (WritingModule)
4254 WriteSubmodules(WritingModule);
4255
Douglas Gregor5204bde2011-08-02 16:26:37 +00004256 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4257
Douglas Gregord4df8652009-04-22 22:02:47 +00004258 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004259 if (!EagerlyDeserializedDecls.empty())
4260 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004261
4262 // Write the record containing tentative definitions.
4263 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004264 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004265
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004266 // Write the record containing unused file scoped decls.
4267 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004268 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004269
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004270 // Write the record containing weak undeclared identifiers.
4271 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004272 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004273 WeakUndeclaredIdentifiers);
4274
Richard Smith78165b52013-01-10 23:43:47 +00004275 // Write the record containing locally-scoped extern "C" definitions.
4276 if (!LocallyScopedExternCDecls.empty())
4277 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4278 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004279
4280 // Write the record containing ext_vector type names.
4281 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004282 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004283
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004284 // Write the record containing VTable uses information.
4285 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004286 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004287
4288 // Write the record containing dynamic classes declarations.
4289 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004290 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004291
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004292 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004293 if (!PendingInstantiations.empty())
4294 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004295
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004296 // Write the record containing declaration references of Sema.
4297 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004298 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004299
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004300 // Write the record containing CUDA-specific declaration references.
4301 if (!CUDASpecialDeclRefs.empty())
4302 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004303
4304 // Write the delegating constructors.
4305 if (!DelegatingCtorDecls.empty())
4306 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004307
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004308 // Write the known namespaces.
4309 if (!KnownNamespaces.empty())
4310 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004311
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004312 // Write the undefined internal functions and variables, and inline functions.
4313 if (!UndefinedButUsed.empty())
4314 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004315
Douglas Gregor851443c2011-08-12 01:39:19 +00004316 // Write the visible updates to DeclContexts.
4317 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4318 I = UpdatedDeclContexts.begin(),
4319 E = UpdatedDeclContexts.end();
4320 I != E; ++I)
4321 WriteDeclContextVisibleUpdate(*I);
4322
Douglas Gregor959bb062011-12-03 01:15:29 +00004323 if (!WritingModule) {
4324 // Write the submodules that were imported, if any.
Aaron Ballman4f45b712014-03-21 15:22:56 +00004325 struct ModuleInfo {
4326 uint64_t ID;
4327 Module *M;
4328 ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4329 };
Richard Smith56be7542014-03-21 00:33:59 +00004330 llvm::SmallVector<ModuleInfo, 64> Imports;
Aaron Ballmanbbc31212014-03-14 20:59:21 +00004331 for (const auto *I : Context.local_imports()) {
Douglas Gregor959bb062011-12-03 01:15:29 +00004332 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
Aaron Ballman4f45b712014-03-21 15:22:56 +00004333 Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4334 I->getImportedModule()));
Douglas Gregor959bb062011-12-03 01:15:29 +00004335 }
Richard Smith56be7542014-03-21 00:33:59 +00004336
4337 if (!Imports.empty()) {
4338 auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4339 return A.ID < B.ID;
4340 };
4341
4342 // Sort and deduplicate module IDs.
4343 std::sort(Imports.begin(), Imports.end(), Cmp);
4344 Imports.erase(std::unique(Imports.begin(), Imports.end(), Cmp),
4345 Imports.end());
4346
4347 RecordData ImportedModules;
4348 for (const auto &Import : Imports) {
4349 ImportedModules.push_back(Import.ID);
4350 // FIXME: If the module has macros imported then later has declarations
4351 // imported, this location won't be the right one as a location for the
4352 // declaration imports.
4353 AddSourceLocation(Import.M->MacroVisibilityLoc, ImportedModules);
4354 }
4355
Douglas Gregor959bb062011-12-03 01:15:29 +00004356 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4357 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004358 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004359
Douglas Gregor851443c2011-08-12 01:39:19 +00004360 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004361 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004362 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004363 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004364 WriteLateParsedTemplates(SemaRef);
4365
Douglas Gregor08f01292009-04-17 22:13:46 +00004366 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004367 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004368 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004369 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004370 Record.push_back(NumLexicalDeclContexts);
4371 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004372 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004373 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004374}
4375
Richard Smithb9eab6d2014-03-20 19:44:17 +00004376void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004377 if (DeclUpdates.empty())
4378 return;
4379
Richard Smith59442b42014-03-20 20:07:19 +00004380 DeclUpdateMap LocalUpdates;
4381 LocalUpdates.swap(DeclUpdates);
4382
Richard Smith6ef42932014-03-20 21:02:00 +00004383 for (auto &DeclUpdate : LocalUpdates) {
4384 const Decl *D = DeclUpdate.first;
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004385 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004386 continue; // The decl will be written completely,no need to store updates.
4387
Richard Smith6ef42932014-03-20 21:02:00 +00004388 OffsetsRecord.push_back(GetDeclRef(D));
4389 OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4390
Richard Smithd28ac5b2014-03-22 23:33:22 +00004391 bool HasUpdatedBody = false;
Richard Smith6ef42932014-03-20 21:02:00 +00004392 RecordData Record;
4393 for (auto &Update : DeclUpdate.second) {
4394 DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4395
4396 Record.push_back(Kind);
4397 switch (Kind) {
4398 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4399 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4400 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4401 Record.push_back(GetDeclRef(Update.getDecl()));
4402 break;
4403
4404 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4405 AddSourceLocation(Update.getLoc(), Record);
4406 break;
4407
Richard Smithd28ac5b2014-03-22 23:33:22 +00004408 case UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION:
4409 // An updated body is emitted last, so that the reader doesn't need
4410 // to skip over the lazy body to reach statements for other records.
4411 Record.pop_back();
4412 HasUpdatedBody = true;
4413 break;
4414
Richard Smith564417a2014-03-20 21:47:22 +00004415 case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4416 addExceptionSpec(
4417 *this,
4418 cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4419 Record);
4420 break;
4421
Richard Smith6ef42932014-03-20 21:02:00 +00004422 case UPD_CXX_DEDUCED_RETURN_TYPE:
4423 Record.push_back(GetOrCreateTypeID(Update.getType()));
4424 break;
4425
4426 case UPD_DECL_MARKED_USED:
4427 break;
Richard Smith5652c0f2014-03-21 01:48:23 +00004428
4429 case UPD_MANGLING_NUMBER:
4430 case UPD_STATIC_LOCAL_NUMBER:
4431 Record.push_back(Update.getNumber());
4432 break;
Richard Smith6ef42932014-03-20 21:02:00 +00004433 }
4434 }
4435
Richard Smithd28ac5b2014-03-22 23:33:22 +00004436 if (HasUpdatedBody) {
4437 const FunctionDecl *Def = cast<FunctionDecl>(D);
4438 Record.push_back(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION);
4439 Record.push_back(Def->isInlined());
4440 AddSourceLocation(Def->getInnerLocStart(), Record);
4441 AddFunctionDefinition(Def, Record);
4442 }
4443
Richard Smith6ef42932014-03-20 21:02:00 +00004444 Stream.EmitRecord(DECL_UPDATES, Record);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004445
Richard Smithb9eab6d2014-03-20 19:44:17 +00004446 // Flush any statements that were written as part of this update record.
4447 FlushStmts();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004448 }
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004449}
4450
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004451void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004452 if (ReplacedDecls.empty())
4453 return;
4454
4455 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004456 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4457 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004458 Record.push_back(I->ID);
4459 Record.push_back(I->Offset);
4460 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004461 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004462 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004463}
4464
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004465void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004466 Record.push_back(Loc.getRawEncoding());
4467}
4468
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004469void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004470 AddSourceLocation(Range.getBegin(), Record);
4471 AddSourceLocation(Range.getEnd(), Record);
4472}
4473
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004474void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004475 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004476 const uint64_t *Words = Value.getRawData();
4477 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004478}
4479
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004480void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004481 Record.push_back(Value.isUnsigned());
4482 AddAPInt(Value, Record);
4483}
4484
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004485void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004486 AddAPInt(Value.bitcastToAPInt(), Record);
4487}
4488
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004489void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004490 Record.push_back(getIdentifierRef(II));
4491}
4492
Sebastian Redl539c5062010-08-18 23:57:32 +00004493IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004494 if (II == 0)
4495 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004496
Sebastian Redl539c5062010-08-18 23:57:32 +00004497 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004498 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004499 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004500 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004501}
4502
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004503MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004504 // Don't emit builtin macros like __LINE__ to the AST file unless they
4505 // have been redefined by the header (in which case they are not
4506 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004507 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004508 return 0;
4509
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004510 MacroID &ID = MacroIDs[MI];
4511 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004512 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004513 MacroInfoToEmitData Info = { Name, MI, ID };
4514 MacroInfosToEmit.push_back(Info);
4515 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004516 return ID;
4517}
4518
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004519MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4520 if (MI == 0 || MI->isBuiltinMacro())
4521 return 0;
4522
4523 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4524 return MacroIDs[MI];
4525}
4526
4527uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4528 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4529 return IdentMacroDirectivesOffsetMap[Name];
4530}
4531
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004532void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004533 Record.push_back(getSelectorRef(SelRef));
4534}
4535
Sebastian Redl539c5062010-08-18 23:57:32 +00004536SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004537 if (Sel.getAsOpaquePtr() == 0) {
4538 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004539 }
4540
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004541 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004542 if (SID == 0 && Chain) {
4543 // This might trigger a ReadSelector callback, which will set the ID for
4544 // this selector.
4545 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004546 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004547 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004548 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004549 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004550 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004551 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004552 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004553}
4554
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004555void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004556 AddDeclRef(Temp->getDestructor(), Record);
4557}
4558
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004559void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4560 CXXBaseSpecifier const *BasesEnd,
4561 RecordDataImpl &Record) {
4562 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4563 CXXBaseSpecifiersToWrite.push_back(
4564 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4565 Bases, BasesEnd));
4566 Record.push_back(NextCXXBaseSpecifiersID++);
4567}
4568
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004569void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004570 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004571 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004572 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004573 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004574 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004575 break;
4576 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004577 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004578 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004579 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004580 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004581 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004582 break;
4583 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004584 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004585 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004586 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004587 break;
John McCall0ad16662009-10-29 08:12:44 +00004588 case TemplateArgument::Null:
4589 case TemplateArgument::Integral:
4590 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004591 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004592 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004593 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004594 break;
4595 }
4596}
4597
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004598void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004599 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004600 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004601
4602 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4603 bool InfoHasSameExpr
4604 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4605 Record.push_back(InfoHasSameExpr);
4606 if (InfoHasSameExpr)
4607 return; // Avoid storing the same expr twice.
4608 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004609 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4610 Record);
4611}
4612
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004613void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4614 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004615 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004616 AddTypeRef(QualType(), Record);
4617 return;
4618 }
4619
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004620 AddTypeLoc(TInfo->getTypeLoc(), Record);
4621}
4622
4623void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4624 AddTypeRef(TL.getType(), Record);
4625
John McCall8f115c62009-10-16 21:56:05 +00004626 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004627 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004628 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004629}
4630
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004631void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004632 Record.push_back(GetOrCreateTypeID(T));
4633}
4634
Douglas Gregoreda8e122011-08-09 15:13:55 +00004635TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004636 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004637 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004638 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4639}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004640
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004641TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004642 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004643 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004644 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004645}
4646
4647TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4648 if (T.isNull())
4649 return TypeIdx();
4650 assert(!T.getLocalFastQualifiers());
4651
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004652 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004653 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004654 if (DoneWritingDeclsAndTypes) {
4655 assert(0 && "New type seen after serializing all the types to emit!");
4656 return TypeIdx();
4657 }
4658
Douglas Gregor1970d882009-04-26 03:49:13 +00004659 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004660 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004661 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004662 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004663 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004664 return Idx;
4665}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004666
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004667TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004668 if (T.isNull())
4669 return TypeIdx();
4670 assert(!T.getLocalFastQualifiers());
4671
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004672 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4673 assert(I != TypeIdxs.end() && "Type not emitted!");
4674 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004675}
4676
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004677void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004678 Record.push_back(GetDeclRef(D));
4679}
4680
Sebastian Redl539c5062010-08-18 23:57:32 +00004681DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004682 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4683
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004684 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004685 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004686 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004687
4688 // If D comes from an AST file, its declaration ID is already known and
4689 // fixed.
4690 if (D->isFromASTFile())
4691 return D->getGlobalID();
4692
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004693 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004694 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004695 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004696 if (DoneWritingDeclsAndTypes) {
4697 assert(0 && "New decl seen after serializing all the decls to emit!");
4698 return 0;
4699 }
4700
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004701 // We haven't seen this declaration before. Give it a new ID and
4702 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004703 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004704 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004705 }
4706
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004707 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004708}
4709
Sebastian Redl539c5062010-08-18 23:57:32 +00004710DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004711 if (D == 0)
4712 return 0;
4713
Douglas Gregorb3163e52012-01-05 22:33:30 +00004714 // If D comes from an AST file, its declaration ID is already known and
4715 // fixed.
4716 if (D->isFromASTFile())
4717 return D->getGlobalID();
4718
Douglas Gregore84a9da2009-04-20 20:36:09 +00004719 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4720 return DeclIDs[D];
4721}
4722
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004723void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004724 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004725 assert(D);
4726
4727 SourceLocation Loc = D->getLocation();
4728 if (Loc.isInvalid())
4729 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004730
4731 // We only keep track of the file-level declarations of each file.
4732 if (!D->getLexicalDeclContext()->isFileContext())
4733 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004734 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4735 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004736 if (isa<ParmVarDecl>(D))
4737 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004738
4739 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004740 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004741 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004742 FileID FID;
4743 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004744 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004745 if (FID.isInvalid())
4746 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004747 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004748
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004749 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004750 if (!Info)
4751 Info = new DeclIDInFileInfo();
4752
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004753 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004754 LocDeclIDsTy &Decls = Info->DeclIDs;
4755
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004756 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004757 Decls.push_back(LocDecl);
4758 return;
4759 }
4760
Benjamin Kramer45025c02013-08-24 13:22:59 +00004761 LocDeclIDsTy::iterator I =
4762 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004763
4764 Decls.insert(I, LocDecl);
4765}
4766
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004767void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004768 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004769 Record.push_back(Name.getNameKind());
4770 switch (Name.getNameKind()) {
4771 case DeclarationName::Identifier:
4772 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4773 break;
4774
4775 case DeclarationName::ObjCZeroArgSelector:
4776 case DeclarationName::ObjCOneArgSelector:
4777 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004778 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004779 break;
4780
4781 case DeclarationName::CXXConstructorName:
4782 case DeclarationName::CXXDestructorName:
4783 case DeclarationName::CXXConversionFunctionName:
4784 AddTypeRef(Name.getCXXNameType(), Record);
4785 break;
4786
4787 case DeclarationName::CXXOperatorName:
4788 Record.push_back(Name.getCXXOverloadedOperator());
4789 break;
4790
Alexis Hunt3d221f22009-11-29 07:34:05 +00004791 case DeclarationName::CXXLiteralOperatorName:
4792 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4793 break;
4794
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004795 case DeclarationName::CXXUsingDirective:
4796 // No extra data to emit
4797 break;
4798 }
4799}
Chris Lattnerca025db2010-05-07 21:43:38 +00004800
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004801void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004802 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004803 switch (Name.getNameKind()) {
4804 case DeclarationName::CXXConstructorName:
4805 case DeclarationName::CXXDestructorName:
4806 case DeclarationName::CXXConversionFunctionName:
4807 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4808 break;
4809
4810 case DeclarationName::CXXOperatorName:
4811 AddSourceLocation(
4812 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4813 Record);
4814 AddSourceLocation(
4815 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4816 Record);
4817 break;
4818
4819 case DeclarationName::CXXLiteralOperatorName:
4820 AddSourceLocation(
4821 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4822 Record);
4823 break;
4824
4825 case DeclarationName::Identifier:
4826 case DeclarationName::ObjCZeroArgSelector:
4827 case DeclarationName::ObjCOneArgSelector:
4828 case DeclarationName::ObjCMultiArgSelector:
4829 case DeclarationName::CXXUsingDirective:
4830 break;
4831 }
4832}
4833
4834void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004835 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004836 AddDeclarationName(NameInfo.getName(), Record);
4837 AddSourceLocation(NameInfo.getLoc(), Record);
4838 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4839}
4840
4841void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004842 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004843 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004844 Record.push_back(Info.NumTemplParamLists);
4845 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4846 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4847}
4848
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004849void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004850 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004851 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004852 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004853 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004854
4855 // Push each of the NNS's onto a stack for serialization in reverse order.
4856 while (NNS) {
4857 NestedNames.push_back(NNS);
4858 NNS = NNS->getPrefix();
4859 }
4860
4861 Record.push_back(NestedNames.size());
4862 while(!NestedNames.empty()) {
4863 NNS = NestedNames.pop_back_val();
4864 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4865 Record.push_back(Kind);
4866 switch (Kind) {
4867 case NestedNameSpecifier::Identifier:
4868 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4869 break;
4870
4871 case NestedNameSpecifier::Namespace:
4872 AddDeclRef(NNS->getAsNamespace(), Record);
4873 break;
4874
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004875 case NestedNameSpecifier::NamespaceAlias:
4876 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4877 break;
4878
Chris Lattnerca025db2010-05-07 21:43:38 +00004879 case NestedNameSpecifier::TypeSpec:
4880 case NestedNameSpecifier::TypeSpecWithTemplate:
4881 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4882 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4883 break;
4884
4885 case NestedNameSpecifier::Global:
4886 // Don't need to write an associated value.
4887 break;
4888 }
4889 }
4890}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004891
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004892void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4893 RecordDataImpl &Record) {
4894 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004895 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004896 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004897
4898 // Push each of the nested-name-specifiers's onto a stack for
4899 // serialization in reverse order.
4900 while (NNS) {
4901 NestedNames.push_back(NNS);
4902 NNS = NNS.getPrefix();
4903 }
4904
4905 Record.push_back(NestedNames.size());
4906 while(!NestedNames.empty()) {
4907 NNS = NestedNames.pop_back_val();
4908 NestedNameSpecifier::SpecifierKind Kind
4909 = NNS.getNestedNameSpecifier()->getKind();
4910 Record.push_back(Kind);
4911 switch (Kind) {
4912 case NestedNameSpecifier::Identifier:
4913 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4914 AddSourceRange(NNS.getLocalSourceRange(), Record);
4915 break;
4916
4917 case NestedNameSpecifier::Namespace:
4918 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4919 AddSourceRange(NNS.getLocalSourceRange(), Record);
4920 break;
4921
4922 case NestedNameSpecifier::NamespaceAlias:
4923 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4924 AddSourceRange(NNS.getLocalSourceRange(), Record);
4925 break;
4926
4927 case NestedNameSpecifier::TypeSpec:
4928 case NestedNameSpecifier::TypeSpecWithTemplate:
4929 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4930 AddTypeLoc(NNS.getTypeLoc(), Record);
4931 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4932 break;
4933
4934 case NestedNameSpecifier::Global:
4935 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4936 break;
4937 }
4938 }
4939}
4940
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004941void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004942 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004943 Record.push_back(Kind);
4944 switch (Kind) {
4945 case TemplateName::Template:
4946 AddDeclRef(Name.getAsTemplateDecl(), Record);
4947 break;
4948
4949 case TemplateName::OverloadedTemplate: {
4950 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4951 Record.push_back(OvT->size());
4952 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4953 I != E; ++I)
4954 AddDeclRef(*I, Record);
4955 break;
4956 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004957
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004958 case TemplateName::QualifiedTemplate: {
4959 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4960 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4961 Record.push_back(QualT->hasTemplateKeyword());
4962 AddDeclRef(QualT->getTemplateDecl(), Record);
4963 break;
4964 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004965
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004966 case TemplateName::DependentTemplate: {
4967 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4968 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4969 Record.push_back(DepT->isIdentifier());
4970 if (DepT->isIdentifier())
4971 AddIdentifierRef(DepT->getIdentifier(), Record);
4972 else
4973 Record.push_back(DepT->getOperator());
4974 break;
4975 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004976
4977 case TemplateName::SubstTemplateTemplateParm: {
4978 SubstTemplateTemplateParmStorage *subst
4979 = Name.getAsSubstTemplateTemplateParm();
4980 AddDeclRef(subst->getParameter(), Record);
4981 AddTemplateName(subst->getReplacement(), Record);
4982 break;
4983 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004984
4985 case TemplateName::SubstTemplateTemplateParmPack: {
4986 SubstTemplateTemplateParmPackStorage *SubstPack
4987 = Name.getAsSubstTemplateTemplateParmPack();
4988 AddDeclRef(SubstPack->getParameterPack(), Record);
4989 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4990 break;
4991 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004992 }
4993}
4994
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004995void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004996 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004997 Record.push_back(Arg.getKind());
4998 switch (Arg.getKind()) {
4999 case TemplateArgument::Null:
5000 break;
5001 case TemplateArgument::Type:
5002 AddTypeRef(Arg.getAsType(), Record);
5003 break;
5004 case TemplateArgument::Declaration:
5005 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00005006 Record.push_back(Arg.isDeclForReferenceParam());
5007 break;
5008 case TemplateArgument::NullPtr:
5009 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005010 break;
5011 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00005012 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005013 AddTypeRef(Arg.getIntegralType(), Record);
5014 break;
5015 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00005016 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5017 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005018 case TemplateArgument::TemplateExpansion:
5019 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00005020 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00005021 Record.push_back(*NumExpansions + 1);
5022 else
5023 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00005024 break;
5025 case TemplateArgument::Expression:
5026 AddStmt(Arg.getAsExpr());
5027 break;
5028 case TemplateArgument::Pack:
5029 Record.push_back(Arg.pack_size());
5030 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
5031 I != E; ++I)
5032 AddTemplateArgument(*I, Record);
5033 break;
5034 }
5035}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005036
5037void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005038ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005039 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005040 assert(TemplateParams && "No TemplateParams!");
5041 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5042 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5043 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5044 Record.push_back(TemplateParams->size());
5045 for (TemplateParameterList::const_iterator
5046 P = TemplateParams->begin(), PEnd = TemplateParams->end();
5047 P != PEnd; ++P)
5048 AddDeclRef(*P, Record);
5049}
5050
5051/// \brief Emit a template argument list.
5052void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005053ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005054 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005055 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005056 Record.push_back(TemplateArgs->size());
5057 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005058 AddTemplateArgument(TemplateArgs->get(i), Record);
5059}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005060
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005061void
5062ASTWriter::AddASTTemplateArgumentListInfo
5063(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5064 assert(ASTTemplArgList && "No ASTTemplArgList!");
5065 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5066 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5067 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5068 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5069 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5070 AddTemplateArgumentLoc(TemplArgs[i], Record);
5071}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005072
5073void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005074ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005075 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005076 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005077 I = Set.begin(), E = Set.end(); I != E; ++I) {
5078 AddDeclRef(I.getDecl(), Record);
5079 Record.push_back(I.getAccess());
5080 }
5081}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005082
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005083void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005084 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005085 Record.push_back(Base.isVirtual());
5086 Record.push_back(Base.isBaseOfClass());
5087 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005088 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005089 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005090 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005091 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5092 : SourceLocation(),
5093 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005094}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005095
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005096void ASTWriter::FlushCXXBaseSpecifiers() {
5097 RecordData Record;
5098 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5099 Record.clear();
5100
5101 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005102 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005103 if (Index == CXXBaseSpecifiersOffsets.size())
5104 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5105 else {
5106 if (Index > CXXBaseSpecifiersOffsets.size())
5107 CXXBaseSpecifiersOffsets.resize(Index + 1);
5108 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5109 }
5110
5111 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5112 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5113 Record.push_back(BEnd - B);
5114 for (; B != BEnd; ++B)
5115 AddCXXBaseSpecifier(*B, Record);
5116 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005117
5118 // Flush any expressions that were written as part of the base specifiers.
5119 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005120 }
5121
5122 CXXBaseSpecifiersToWrite.clear();
5123}
5124
Alexis Hunt1d792652011-01-08 20:30:50 +00005125void ASTWriter::AddCXXCtorInitializers(
5126 const CXXCtorInitializer * const *CtorInitializers,
5127 unsigned NumCtorInitializers,
5128 RecordDataImpl &Record) {
5129 Record.push_back(NumCtorInitializers);
5130 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5131 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005132
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005133 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005134 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005135 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005136 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005137 } else if (Init->isDelegatingInitializer()) {
5138 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005139 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005140 } else if (Init->isMemberInitializer()){
5141 Record.push_back(CTOR_INITIALIZER_MEMBER);
5142 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005143 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005144 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5145 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005146 }
Francois Pichetd583da02010-12-04 09:14:42 +00005147
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005148 AddSourceLocation(Init->getMemberLocation(), Record);
5149 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005150 AddSourceLocation(Init->getLParenLoc(), Record);
5151 AddSourceLocation(Init->getRParenLoc(), Record);
5152 Record.push_back(Init->isWritten());
5153 if (Init->isWritten()) {
5154 Record.push_back(Init->getSourceOrder());
5155 } else {
5156 Record.push_back(Init->getNumArrayIndices());
5157 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5158 AddDeclRef(Init->getArrayIndex(i), Record);
5159 }
5160 }
5161}
5162
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005163void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5164 assert(D->DefinitionData);
5165 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005166 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005167 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005168 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005169 Record.push_back(Data.Aggregate);
5170 Record.push_back(Data.PlainOldData);
5171 Record.push_back(Data.Empty);
5172 Record.push_back(Data.Polymorphic);
5173 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005174 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005175 Record.push_back(Data.HasNoNonEmptyBases);
5176 Record.push_back(Data.HasPrivateFields);
5177 Record.push_back(Data.HasProtectedFields);
5178 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005179 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005180 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005181 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005182 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005183 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005184 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5185 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5186 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5187 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5188 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5189 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005190 Record.push_back(Data.HasTrivialSpecialMembers);
5191 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005192 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005193 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005194 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005195 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005196 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005197 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005198 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005199 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5200 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5201 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5202 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005203 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005204
5205 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005206 if (Data.NumBases > 0)
5207 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5208 Record);
5209
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005210 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5211 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005212 if (Data.NumVBases > 0)
5213 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5214 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005215
Richard Smitha4ba74c2013-08-30 04:46:40 +00005216 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5217 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005218 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005219 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005220
5221 // Add lambda-specific data.
5222 if (Data.IsLambda) {
5223 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005224 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005225 Record.push_back(Lambda.IsGenericLambda);
5226 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005227 Record.push_back(Lambda.NumCaptures);
5228 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005229 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005230 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005231 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005232 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5233 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5234 AddSourceLocation(Capture.getLocation(), Record);
5235 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005236 Record.push_back(Capture.getCaptureKind());
5237 switch (Capture.getCaptureKind()) {
5238 case LCK_This:
5239 break;
5240 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005241 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005242 VarDecl *Var =
5243 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5244 AddDeclRef(Var, Record);
5245 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5246 : SourceLocation(),
5247 Record);
5248 break;
5249 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005250 }
5251 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005252}
5253
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005254void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005255 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005256 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005257 assert(FirstDeclID == NextDeclID &&
5258 FirstTypeID == NextTypeID &&
5259 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005260 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005261 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005262 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005263 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005264
Sebastian Redl07a89a82010-07-30 00:29:29 +00005265 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005266
Douglas Gregordf0c1512011-08-18 04:12:04 +00005267 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5268 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5269 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005270 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005271 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005272 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005273 NextDeclID = FirstDeclID;
5274 NextTypeID = FirstTypeID;
5275 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005276 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005277 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005278 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005279}
5280
Sebastian Redl539c5062010-08-18 23:57:32 +00005281void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005282 // Always keep the highest ID. See \p TypeRead() for more information.
5283 IdentID &StoredID = IdentifierIDs[II];
5284 if (ID > StoredID)
5285 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005286}
5287
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005288void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005289 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005290 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005291 if (ID > StoredID)
5292 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005293}
5294
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005295void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005296 // Always take the highest-numbered type index. This copes with an interesting
5297 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005298 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005299 // keep the higher-numbered entry so that we can properly write it out to
5300 // the AST file.
5301 TypeIdx &StoredIdx = TypeIdxs[T];
5302 if (Idx.getIndex() >= StoredIdx.getIndex())
5303 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005304}
5305
Sebastian Redl539c5062010-08-18 23:57:32 +00005306void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005307 // Always keep the highest ID. See \p TypeRead() for more information.
5308 SelectorID &StoredID = SelectorIDs[S];
5309 if (ID > StoredID)
5310 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005311}
Douglas Gregor91096292010-10-02 19:29:26 +00005312
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005313void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005314 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005315 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005316 MacroDefinitions[MD] = ID;
5317}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005318
Douglas Gregore37a85a2011-12-02 17:30:13 +00005319void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5320 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5321 SubmoduleIDs[Mod] = ID;
5322}
5323
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005324void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005325 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005326 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005327 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5328 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005329 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005330 // A forward reference was mutated into a definition. Rewrite it.
5331 // FIXME: This happens during template instantiation, should we
5332 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005333 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005334 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005335 }
5336}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005337
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005338void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005339 assert(!WritingAST && "Already writing the AST!");
5340
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005341 // TU and namespaces are handled elsewhere.
5342 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5343 return;
5344
Douglas Gregorb3722e22011-09-09 23:01:35 +00005345 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005346 return; // Not a source decl added to a DeclContext from PCH.
5347
Douglas Gregor9f782892013-01-21 15:25:38 +00005348 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005349 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005350 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005351}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005352
5353void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005354 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005355 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005356 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005357 return; // Not a source member added to a class from PCH.
5358 if (!isa<CXXMethodDecl>(D))
5359 return; // We are interested in lazily declared implicit methods.
5360
5361 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005362 assert(RD->isCompleteDefinition());
Aaron Ballman4f45b712014-03-21 15:22:56 +00005363 DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005364}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005365
5366void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5367 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005368 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005369 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005370 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005371 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005372 return; // Not a source specialization added to a template from PCH.
5373
Aaron Ballman4f45b712014-03-21 15:22:56 +00005374 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5375 D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005376}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005377
Larisse Voufo39a1e502013-08-06 01:03:05 +00005378void ASTWriter::AddedCXXTemplateSpecialization(
5379 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5380 // The specializations set is kept in the canonical template.
5381 assert(!WritingAST && "Already writing the AST!");
5382 TD = TD->getCanonicalDecl();
5383 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5384 return; // Not a source specialization added to a template from PCH.
5385
Aaron Ballman4f45b712014-03-21 15:22:56 +00005386 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5387 D));
Larisse Voufo39a1e502013-08-06 01:03:05 +00005388}
5389
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005390void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5391 const FunctionDecl *D) {
5392 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005393 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005394 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005395 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005396 return; // Not a source specialization added to a template from PCH.
5397
Aaron Ballman4f45b712014-03-21 15:22:56 +00005398 DeclUpdates[TD].push_back(DeclUpdate(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION,
5399 D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005400}
5401
Richard Smith564417a2014-03-20 21:47:22 +00005402void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5403 assert(!WritingAST && "Already writing the AST!");
5404 FD = FD->getCanonicalDecl();
5405 if (!FD->isFromASTFile())
5406 return; // Not a function declared in PCH and defined outside.
5407
5408 DeclUpdates[FD].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5409}
5410
Richard Smith1fa5d642013-05-11 05:45:24 +00005411void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5412 assert(!WritingAST && "Already writing the AST!");
5413 FD = FD->getCanonicalDecl();
5414 if (!FD->isFromASTFile())
5415 return; // Not a function declared in PCH and defined outside.
5416
Aaron Ballman4f45b712014-03-21 15:22:56 +00005417 DeclUpdates[FD].push_back(DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
Richard Smith1fa5d642013-05-11 05:45:24 +00005418}
5419
Sebastian Redlab238a72011-04-24 16:28:06 +00005420void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005421 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005422 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005423 return; // Declaration not imported from PCH.
5424
5425 // Implicit decl from a PCH was defined.
5426 // FIXME: Should implicit definition be a separate FunctionDecl?
5427 RewriteDecl(D);
5428}
5429
Richard Smithd28ac5b2014-03-22 23:33:22 +00005430void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5431 assert(!WritingAST && "Already writing the AST!");
5432 if (!D->isFromASTFile())
5433 return;
5434
5435 // Since the actual instantiation is delayed, this really means that we need
5436 // to update the instantiation location.
5437 DeclUpdates[D].push_back(
5438 DeclUpdate(UPD_CXX_INSTANTIATED_FUNCTION_DEFINITION));
5439}
5440
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005441void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005442 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005443 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005444 return;
5445
5446 // Since the actual instantiation is delayed, this really means that we need
5447 // to update the instantiation location.
Richard Smith6ef42932014-03-20 21:02:00 +00005448 DeclUpdates[D].push_back(
Aaron Ballman4f45b712014-03-21 15:22:56 +00005449 DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5450 D->getMemberSpecializationInfo()->getPointOfInstantiation()));
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005451}
5452
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005453void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5454 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005455 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005456 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005457 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005458
5459 assert(IFD->getDefinition() && "Category on a class without a definition?");
5460 ObjCClassesWithCategories.insert(
5461 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005462}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005463
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005464
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005465void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5466 const ObjCPropertyDecl *OrigProp,
5467 const ObjCCategoryDecl *ClassExt) {
5468 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5469 if (!D)
5470 return;
5471
5472 assert(!WritingAST && "Already writing the AST!");
5473 if (!D->isFromASTFile())
5474 return; // Declaration not imported from PCH.
5475
5476 RewriteDecl(D);
5477}
Eli Friedman276dd182013-09-05 00:02:25 +00005478
5479void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5480 assert(!WritingAST && "Already writing the AST!");
5481 if (!D->isFromASTFile())
5482 return;
5483
Aaron Ballman4f45b712014-03-21 15:22:56 +00005484 DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
Eli Friedman276dd182013-09-05 00:02:25 +00005485}