blob: 3e6b719bc6e225ec7d972884a3f1a1eca924f536 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregorcb177f12012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000034#include "clang/Lex/HeaderSearch.h"
35#include "clang/Lex/HeaderSearchOptions.h"
36#include "clang/Lex/MacroInfo.h"
37#include "clang/Lex/PreprocessingRecord.h"
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/PreprocessorOptions.h"
40#include "clang/Sema/IdentifierResolver.h"
41#include "clang/Sema/Sema.h"
42#include "clang/Serialization/ASTReader.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000057
Sebastian Redl3df5a082010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000059static StringRef data(const std::vector<T, Allocator> &v) {
60 if (v.empty()) return StringRef();
61 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000063}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000069}
70
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000074
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000086
87 void VisitArrayType(const ArrayType *T);
88 void VisitFunctionType(const FunctionType *T);
89 void VisitTagType(const TagType *T);
90
91#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
92#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Reid Kleckner8a365022013-06-24 17:51:48 +0000111void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
112 Writer.AddTypeRef(T->getOriginalType(), Record);
113 Code = TYPE_DECAYED;
114}
115
Reid Kleckner0503a872013-12-05 01:23:43 +0000116void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
117 Writer.AddTypeRef(T->getOriginalType(), Record);
118 Writer.AddTypeRef(T->getAdjustedType(), Record);
119 Code = TYPE_ADJUSTED;
120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000124 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000128 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
129 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000134 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000139 Writer.AddTypeRef(T->getPointeeType(), Record);
140 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000141 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142}
143
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145 Writer.AddTypeRef(T->getElementType(), Record);
146 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000147 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148}
149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151 VisitArrayType(T);
152 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000153 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154}
155
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000156void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000163 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
164 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000165 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000166 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167}
168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getElementType(), Record);
171 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000172 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000173 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174}
175
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000176void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000177 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000178 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179}
180
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Alp Toker314cc812014-01-25 16:55:45 +0000182 Writer.AddTypeRef(T->getReturnType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000183 FunctionType::ExtInfo C = T->getExtInfo();
184 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000185 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000186 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000187 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000188 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000189 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190}
191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000194 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195}
196
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000197void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000198 VisitFunctionType(T);
Alp Toker9cacbab2014-01-20 20:26:09 +0000199 Record.push_back(T->getNumParams());
200 for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
201 Writer.AddTypeRef(T->getParamType(I), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000203 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000205 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000206 Record.push_back(T->getExceptionSpecType());
207 if (T->getExceptionSpecType() == EST_Dynamic) {
208 Record.push_back(T->getNumExceptions());
209 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
210 Writer.AddTypeRef(T->getExceptionType(I), Record);
211 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
212 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000213 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
214 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
215 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000216 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
217 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000218 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000225}
John McCallb96ec562009-12-04 22:46:56 +0000226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000229 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
230 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000231 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000232}
233
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000234void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000237}
238
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000241 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242}
243
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000244void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000245 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000246 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000248}
249
Alexis Hunte852b102011-05-24 22:41:36 +0000250void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
251 Writer.AddTypeRef(T->getBaseType(), Record);
252 Writer.AddTypeRef(T->getUnderlyingType(), Record);
253 Record.push_back(T->getUTTKind());
254 Code = TYPE_UNARY_TRANSFORM;
255}
256
Richard Smith30482bc2011-02-20 03:19:35 +0000257void ASTTypeWriter::VisitAutoType(const AutoType *T) {
258 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smith74aeef52013-04-26 16:15:35 +0000259 Record.push_back(T->isDecltypeAuto());
Richard Smith27d807c2013-04-30 13:56:41 +0000260 if (T->getDeducedType().isNull())
261 Record.push_back(T->isDependentType());
Richard Smith30482bc2011-02-20 03:19:35 +0000262 Code = TYPE_AUTO;
263}
264
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000265void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000266 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000267 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000268 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000269 "Cannot serialize in the middle of a type definition");
270}
271
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000273 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000274 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000275}
276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000278 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000279 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280}
281
John McCall81904512011-01-06 01:58:22 +0000282void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
283 Writer.AddTypeRef(T->getModifiedType(), Record);
284 Writer.AddTypeRef(T->getEquivalentType(), Record);
285 Record.push_back(T->getAttrKind());
286 Code = TYPE_ATTRIBUTED;
287}
288
Mike Stump11289f42009-09-09 15:08:12 +0000289void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000290ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000291 const SubstTemplateTypeParmType *T) {
292 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
293 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000294 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000295}
296
297void
Douglas Gregorada4b792011-01-14 02:55:32 +0000298ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
299 const SubstTemplateTypeParmPackType *T) {
300 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
301 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
302 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
303}
304
305void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000306ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000307 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000308 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309 Writer.AddTemplateName(T->getTemplateName(), Record);
310 Record.push_back(T->getNumArgs());
311 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
312 ArgI != ArgE; ++ArgI)
313 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000314 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
315 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000316 : T->getCanonicalTypeInternal(),
317 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000323 VisitArrayType(T);
324 Writer.AddStmt(T->getSizeExpr());
325 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331 const DependentSizedExtVectorType *T) {
332 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000333 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000334}
335
336void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000337ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000338 Record.push_back(T->getDepth());
339 Record.push_back(T->getIndex());
340 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000341 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000343}
344
345void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000346ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000347 Record.push_back(T->getKeyword());
348 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
349 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000350 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
351 : T->getCanonicalTypeInternal(),
352 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000354}
355
356void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000357ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000358 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000359 Record.push_back(T->getKeyword());
360 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
361 Writer.AddIdentifierRef(T->getIdentifier(), Record);
362 Record.push_back(T->getNumArgs());
363 for (DependentTemplateSpecializationType::iterator
364 I = T->begin(), E = T->end(); I != E; ++I)
365 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000367}
368
Douglas Gregord2fa7662010-12-20 02:24:11 +0000369void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
370 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +0000371 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000372 Record.push_back(*NumExpansions + 1);
373 else
374 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000375 Code = TYPE_PACK_EXPANSION;
376}
377
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000378void ASTTypeWriter::VisitParenType(const ParenType *T) {
379 Writer.AddTypeRef(T->getInnerType(), Record);
380 Code = TYPE_PAREN;
381}
382
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000384 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000385 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
386 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000391 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000392 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000393 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000394}
395
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000396void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000397 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000399}
400
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000401void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000402 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000403 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000404 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000405 E = T->qual_end(); I != E; ++I)
406 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000407 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000408}
409
Steve Narofffb4330f2009-06-17 22:40:22 +0000410void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000411ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000412 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000413 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000414}
415
Eli Friedman0dfb8892011-10-06 23:00:33 +0000416void
417ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
418 Writer.AddTypeRef(T->getValueType(), Record);
419 Code = TYPE_ATOMIC;
420}
421
John McCall8f115c62009-10-16 21:56:05 +0000422namespace {
423
424class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000425 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000426 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000427
428public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000429 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000430 : Writer(Writer), Record(Record) { }
431
John McCall17001972009-10-18 01:05:36 +0000432#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000433#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000434 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000435#include "clang/AST/TypeLocNodes.def"
436
John McCall17001972009-10-18 01:05:36 +0000437 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
438 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000439};
440
441}
442
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
444 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000447 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
448 if (TL.needsExtraLocalData()) {
449 Record.push_back(TL.getWrittenTypeSpec());
450 Record.push_back(TL.getWrittenSignSpec());
451 Record.push_back(TL.getWrittenWidthSpec());
452 Record.push_back(TL.hasModeAttr());
453 }
John McCall8f115c62009-10-16 21:56:05 +0000454}
John McCall17001972009-10-18 01:05:36 +0000455void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000457}
John McCall17001972009-10-18 01:05:36 +0000458void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000460}
Reid Kleckner8a365022013-06-24 17:51:48 +0000461void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
462 // nothing to do
463}
Reid Kleckner0503a872013-12-05 01:23:43 +0000464void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
465 // nothing to do
466}
John McCall17001972009-10-18 01:05:36 +0000467void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
468 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000469}
John McCall17001972009-10-18 01:05:36 +0000470void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000472}
John McCall17001972009-10-18 01:05:36 +0000473void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
474 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000475}
John McCall17001972009-10-18 01:05:36 +0000476void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000478 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
482 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
483 Record.push_back(TL.getSizeExpr() ? 1 : 0);
484 if (TL.getSizeExpr())
485 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
488 VisitArrayTypeLoc(TL);
489}
490void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
491 VisitArrayTypeLoc(TL);
492}
493void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
494 VisitArrayTypeLoc(TL);
495}
496void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
497 DependentSizedArrayTypeLoc TL) {
498 VisitArrayTypeLoc(TL);
499}
500void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
501 DependentSizedExtVectorTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
504void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
510void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000511 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000512 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
513 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000514 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Alp Tokerb3fd5cf2014-01-21 00:32:38 +0000515 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
516 Writer.AddDeclRef(TL.getParam(i), Record);
John McCall17001972009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
519 VisitFunctionTypeLoc(TL);
520}
521void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
522 VisitFunctionTypeLoc(TL);
523}
John McCallb96ec562009-12-04 22:46:56 +0000524void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall17001972009-10-18 01:05:36 +0000527void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000531 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
532 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
533 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000534}
535void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000536 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
537 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
538 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
539 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000540}
541void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Alexis Hunte852b102011-05-24 22:41:36 +0000544void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getKWLoc(), Record);
546 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
547 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
548 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
549}
Richard Smith30482bc2011-02-20 03:19:35 +0000550void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall17001972009-10-18 01:05:36 +0000553void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
556void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall81904512011-01-06 01:58:22 +0000559void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
561 if (TL.hasAttrOperand()) {
562 SourceRange range = TL.getAttrOperandParensRange();
563 Writer.AddSourceLocation(range.getBegin(), Record);
564 Writer.AddSourceLocation(range.getEnd(), Record);
565 }
566 if (TL.hasAttrExprOperand()) {
567 Expr *operand = TL.getAttrExprOperand();
568 Record.push_back(operand ? 1 : 0);
569 if (operand) Writer.AddStmt(operand);
570 } else if (TL.hasAttrEnumOperand()) {
571 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
572 }
573}
John McCall17001972009-10-18 01:05:36 +0000574void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getNameLoc(), Record);
576}
John McCallcebee162009-10-18 09:09:24 +0000577void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
578 SubstTemplateTypeParmTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
Douglas Gregorada4b792011-01-14 02:55:32 +0000581void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
582 SubstTemplateTypeParmPackTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
John McCall17001972009-10-18 01:05:36 +0000585void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
586 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000587 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000588 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
589 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
590 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
591 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000592 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
593 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000594}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000595void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
596 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
597 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
598}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000599void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000600 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000601 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000602}
John McCalle78aac42010-03-10 03:28:59 +0000603void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
605}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000606void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000607 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000608 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000609 Writer.AddSourceLocation(TL.getNameLoc(), Record);
610}
John McCallc392f372010-06-11 00:33:02 +0000611void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
612 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000613 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000614 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000615 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000616 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000617 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
618 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
619 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000620 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
621 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000622}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000623void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
624 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
625}
John McCall17001972009-10-18 01:05:36 +0000626void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
627 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000628}
629void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
630 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000631 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
632 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
633 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
634 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000635}
John McCallfc93cf92009-10-22 22:37:11 +0000636void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
637 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000638}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000639void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
640 Writer.AddSourceLocation(TL.getKWLoc(), Record);
641 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
642 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
643}
John McCall8f115c62009-10-16 21:56:05 +0000644
Chris Lattner19cea4e2009-04-22 05:57:30 +0000645//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000646// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000647//===----------------------------------------------------------------------===//
648
Chris Lattner28fa4e62009-04-26 22:26:21 +0000649static void EmitBlockID(unsigned ID, const char *Name,
650 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000651 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000652 Record.clear();
653 Record.push_back(ID);
654 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
655
656 // Emit the block name if present.
657 if (Name == 0 || Name[0] == 0) return;
658 Record.clear();
659 while (*Name)
660 Record.push_back(*Name++);
661 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
662}
663
664static void EmitRecordID(unsigned ID, const char *Name,
665 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000666 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 Record.clear();
668 Record.push_back(ID);
669 while (*Name)
670 Record.push_back(*Name++);
671 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000672}
673
674static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000675 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000676#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000677 RECORD(STMT_STOP);
678 RECORD(STMT_NULL_PTR);
679 RECORD(STMT_NULL);
680 RECORD(STMT_COMPOUND);
681 RECORD(STMT_CASE);
682 RECORD(STMT_DEFAULT);
683 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000684 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000685 RECORD(STMT_IF);
686 RECORD(STMT_SWITCH);
687 RECORD(STMT_WHILE);
688 RECORD(STMT_DO);
689 RECORD(STMT_FOR);
690 RECORD(STMT_GOTO);
691 RECORD(STMT_INDIRECT_GOTO);
692 RECORD(STMT_CONTINUE);
693 RECORD(STMT_BREAK);
694 RECORD(STMT_RETURN);
695 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000696 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000697 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000698 RECORD(EXPR_PREDEFINED);
699 RECORD(EXPR_DECL_REF);
700 RECORD(EXPR_INTEGER_LITERAL);
701 RECORD(EXPR_FLOATING_LITERAL);
702 RECORD(EXPR_IMAGINARY_LITERAL);
703 RECORD(EXPR_STRING_LITERAL);
704 RECORD(EXPR_CHARACTER_LITERAL);
705 RECORD(EXPR_PAREN);
706 RECORD(EXPR_UNARY_OPERATOR);
707 RECORD(EXPR_SIZEOF_ALIGN_OF);
708 RECORD(EXPR_ARRAY_SUBSCRIPT);
709 RECORD(EXPR_CALL);
710 RECORD(EXPR_MEMBER);
711 RECORD(EXPR_BINARY_OPERATOR);
712 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
713 RECORD(EXPR_CONDITIONAL_OPERATOR);
714 RECORD(EXPR_IMPLICIT_CAST);
715 RECORD(EXPR_CSTYLE_CAST);
716 RECORD(EXPR_COMPOUND_LITERAL);
717 RECORD(EXPR_EXT_VECTOR_ELEMENT);
718 RECORD(EXPR_INIT_LIST);
719 RECORD(EXPR_DESIGNATED_INIT);
720 RECORD(EXPR_IMPLICIT_VALUE_INIT);
721 RECORD(EXPR_VA_ARG);
722 RECORD(EXPR_ADDR_LABEL);
723 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000724 RECORD(EXPR_CHOOSE);
725 RECORD(EXPR_GNU_NULL);
726 RECORD(EXPR_SHUFFLE_VECTOR);
727 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000728 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000729 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000730 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000731 RECORD(EXPR_OBJC_ARRAY_LITERAL);
732 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000733 RECORD(EXPR_OBJC_ENCODE);
734 RECORD(EXPR_OBJC_SELECTOR_EXPR);
735 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
736 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
737 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
738 RECORD(EXPR_OBJC_KVC_REF_EXPR);
739 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000740 RECORD(STMT_OBJC_FOR_COLLECTION);
741 RECORD(STMT_OBJC_CATCH);
742 RECORD(STMT_OBJC_FINALLY);
743 RECORD(STMT_OBJC_AT_TRY);
744 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
745 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000746 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000747 RECORD(EXPR_CXX_OPERATOR_CALL);
748 RECORD(EXPR_CXX_CONSTRUCT);
749 RECORD(EXPR_CXX_STATIC_CAST);
750 RECORD(EXPR_CXX_DYNAMIC_CAST);
751 RECORD(EXPR_CXX_REINTERPRET_CAST);
752 RECORD(EXPR_CXX_CONST_CAST);
753 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000754 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000755 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000756 RECORD(EXPR_CXX_BOOL_LITERAL);
757 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000758 RECORD(EXPR_CXX_TYPEID_EXPR);
759 RECORD(EXPR_CXX_TYPEID_TYPE);
760 RECORD(EXPR_CXX_UUIDOF_EXPR);
761 RECORD(EXPR_CXX_UUIDOF_TYPE);
762 RECORD(EXPR_CXX_THIS);
763 RECORD(EXPR_CXX_THROW);
764 RECORD(EXPR_CXX_DEFAULT_ARG);
765 RECORD(EXPR_CXX_BIND_TEMPORARY);
766 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
767 RECORD(EXPR_CXX_NEW);
768 RECORD(EXPR_CXX_DELETE);
769 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
770 RECORD(EXPR_EXPR_WITH_CLEANUPS);
771 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
772 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
773 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
774 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
775 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000776 RECORD(EXPR_CXX_NOEXCEPT);
777 RECORD(EXPR_OPAQUE_VALUE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000778 RECORD(EXPR_PACK_EXPANSION);
779 RECORD(EXPR_SIZEOF_PACK);
780 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000781 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000782#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000783}
Mike Stump11289f42009-09-09 15:08:12 +0000784
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000785void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RecordData Record;
787 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000788
Sebastian Redl539c5062010-08-18 23:57:32 +0000789#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
790#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000791
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000792 // Control Block.
793 BLOCK(CONTROL_BLOCK);
794 RECORD(METADATA);
795 RECORD(IMPORTS);
796 RECORD(LANGUAGE_OPTIONS);
797 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000798 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000799 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000800 RECORD(ORIGINAL_FILE_ID);
Douglas Gregor3120d2c2012-10-22 18:42:04 +0000801 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor8263ffb2012-10-24 15:17:15 +0000802 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregorc6317db2012-10-24 15:49:58 +0000803 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregor2d302362012-10-24 16:50:34 +0000804 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregorb6af6c22012-10-24 20:05:57 +0000805 RECORD(PREPROCESSOR_OPTIONS);
806
Douglas Gregor108cb222012-10-19 00:45:00 +0000807 BLOCK(INPUT_FILES_BLOCK);
808 RECORD(INPUT_FILE);
809
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000810 // AST Top-Level Block.
811 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000812 RECORD(TYPE_OFFSET);
813 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000814 RECORD(IDENTIFIER_OFFSET);
815 RECORD(IDENTIFIER_TABLE);
Ben Langmuir332aafe2014-01-31 01:06:56 +0000816 RECORD(EAGERLY_DESERIALIZED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000817 RECORD(SPECIAL_TYPES);
818 RECORD(STATISTICS);
819 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000820 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith78165b52013-01-10 23:43:47 +0000821 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 RECORD(SELECTOR_OFFSETS);
823 RECORD(METHOD_POOL);
824 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000825 RECORD(SOURCE_LOCATION_OFFSETS);
826 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000827 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000828 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000829 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000830 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000831 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000832 RECORD(SEMA_DECL_REFS);
833 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
834 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
835 RECORD(DECL_REPLACEMENTS);
836 RECORD(UPDATE_VISIBLE);
837 RECORD(DECL_UPDATE_OFFSETS);
838 RECORD(DECL_UPDATES);
839 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
840 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000841 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000842 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000843 RECORD(FP_PRAGMA_OPTIONS);
844 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000845 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000846 RECORD(KNOWN_NAMESPACES);
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +0000847 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000848 RECORD(MODULE_OFFSET_MAP);
849 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000850 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000851 RECORD(FILE_SORTED_DECLS);
852 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000853 RECORD(MERGED_DECLARATIONS);
854 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000855 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000856 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +0000857 RECORD(MACRO_TABLE);
Richard Smithe40f2ba2013-08-07 21:41:30 +0000858 RECORD(LATE_PARSED_TEMPLATE);
Douglas Gregor358cd442012-01-15 16:58:34 +0000859
Chris Lattner28fa4e62009-04-26 22:26:21 +0000860 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000861 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000862 RECORD(SM_SLOC_FILE_ENTRY);
863 RECORD(SM_SLOC_BUFFER_ENTRY);
864 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000865 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000866
Chris Lattner28fa4e62009-04-26 22:26:21 +0000867 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000868 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000869 RECORD(PP_MACRO_OBJECT_LIKE);
870 RECORD(PP_MACRO_FUNCTION_LIKE);
871 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000872
Douglas Gregor12bfa382009-10-17 00:13:19 +0000873 // Decls and Types block.
874 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000875 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000876 RECORD(TYPE_COMPLEX);
877 RECORD(TYPE_POINTER);
878 RECORD(TYPE_BLOCK_POINTER);
879 RECORD(TYPE_LVALUE_REFERENCE);
880 RECORD(TYPE_RVALUE_REFERENCE);
881 RECORD(TYPE_MEMBER_POINTER);
882 RECORD(TYPE_CONSTANT_ARRAY);
883 RECORD(TYPE_INCOMPLETE_ARRAY);
884 RECORD(TYPE_VARIABLE_ARRAY);
885 RECORD(TYPE_VECTOR);
886 RECORD(TYPE_EXT_VECTOR);
887 RECORD(TYPE_FUNCTION_PROTO);
888 RECORD(TYPE_FUNCTION_NO_PROTO);
889 RECORD(TYPE_TYPEDEF);
890 RECORD(TYPE_TYPEOF_EXPR);
891 RECORD(TYPE_TYPEOF);
892 RECORD(TYPE_RECORD);
893 RECORD(TYPE_ENUM);
894 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000895 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000896 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000897 RECORD(TYPE_DECLTYPE);
898 RECORD(TYPE_ELABORATED);
899 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
900 RECORD(TYPE_UNRESOLVED_USING);
901 RECORD(TYPE_INJECTED_CLASS_NAME);
902 RECORD(TYPE_OBJC_OBJECT);
903 RECORD(TYPE_TEMPLATE_TYPE_PARM);
904 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
905 RECORD(TYPE_DEPENDENT_NAME);
906 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
907 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
908 RECORD(TYPE_PAREN);
909 RECORD(TYPE_PACK_EXPANSION);
910 RECORD(TYPE_ATTRIBUTED);
911 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000912 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000913 RECORD(DECL_TYPEDEF);
914 RECORD(DECL_ENUM);
915 RECORD(DECL_RECORD);
916 RECORD(DECL_ENUM_CONSTANT);
917 RECORD(DECL_FUNCTION);
918 RECORD(DECL_OBJC_METHOD);
919 RECORD(DECL_OBJC_INTERFACE);
920 RECORD(DECL_OBJC_PROTOCOL);
921 RECORD(DECL_OBJC_IVAR);
922 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000923 RECORD(DECL_OBJC_CATEGORY);
924 RECORD(DECL_OBJC_CATEGORY_IMPL);
925 RECORD(DECL_OBJC_IMPLEMENTATION);
926 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
927 RECORD(DECL_OBJC_PROPERTY);
928 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000929 RECORD(DECL_FIELD);
John McCall5e77d762013-04-16 07:28:30 +0000930 RECORD(DECL_MS_PROPERTY);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000931 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000932 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000933 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000934 RECORD(DECL_FILE_SCOPE_ASM);
935 RECORD(DECL_BLOCK);
936 RECORD(DECL_CONTEXT_LEXICAL);
937 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000938 RECORD(DECL_NAMESPACE);
939 RECORD(DECL_NAMESPACE_ALIAS);
940 RECORD(DECL_USING);
941 RECORD(DECL_USING_SHADOW);
942 RECORD(DECL_USING_DIRECTIVE);
943 RECORD(DECL_UNRESOLVED_USING_VALUE);
944 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
945 RECORD(DECL_LINKAGE_SPEC);
946 RECORD(DECL_CXX_RECORD);
947 RECORD(DECL_CXX_METHOD);
948 RECORD(DECL_CXX_CONSTRUCTOR);
949 RECORD(DECL_CXX_DESTRUCTOR);
950 RECORD(DECL_CXX_CONVERSION);
951 RECORD(DECL_ACCESS_SPEC);
952 RECORD(DECL_FRIEND);
953 RECORD(DECL_FRIEND_TEMPLATE);
954 RECORD(DECL_CLASS_TEMPLATE);
955 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
956 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000957 RECORD(DECL_VAR_TEMPLATE);
958 RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
959 RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000960 RECORD(DECL_FUNCTION_TEMPLATE);
961 RECORD(DECL_TEMPLATE_TYPE_PARM);
962 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
963 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
964 RECORD(DECL_STATIC_ASSERT);
965 RECORD(DECL_CXX_BASE_SPECIFIERS);
966 RECORD(DECL_INDIRECTFIELD);
967 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
968
Douglas Gregor03412ba2011-06-03 02:27:19 +0000969 // Statements and Exprs can occur in the Decls and Types block.
970 AddStmtsExprs(Stream, Record);
971
Douglas Gregor92a96f52011-02-08 21:58:10 +0000972 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000973 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000974 RECORD(PPD_MACRO_DEFINITION);
975 RECORD(PPD_INCLUSION_DIRECTIVE);
976
Chris Lattner28fa4e62009-04-26 22:26:21 +0000977#undef RECORD
978#undef BLOCK
979 Stream.ExitBlock();
980}
981
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000982/// \brief Adjusts the given filename to only write out the portion of the
983/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000984///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985/// \param Filename the file name to adjust.
986///
987/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
988/// the returned filename will be adjusted by this system root.
989///
990/// \returns either the original filename (if it needs no adjustment) or the
991/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000992static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000993adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000994 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000995
Douglas Gregorc567ba22011-07-22 16:35:34 +0000996 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000997 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000998
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000999 // Verify that the filename and the system root have the same prefix.
1000 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +00001001 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001002 if (Filename[Pos] != isysroot[Pos])
1003 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +00001004
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001005 // We hit the end of the filename before we hit the end of the system root.
1006 if (!Filename[Pos])
1007 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +00001008
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001009 // If the file name has a '/' at the current position, skip over the '/'.
1010 // We distinguish sysroot-based includes from absolute includes by the
1011 // absence of '/' at the beginning of sysroot-based includes.
1012 if (Filename[Pos] == '/')
1013 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001015 return Filename + Pos;
1016}
Chris Lattner28fa4e62009-04-26 22:26:21 +00001017
Douglas Gregor112b9072012-10-18 05:31:06 +00001018/// \brief Write the control block.
Douglas Gregor2d302362012-10-24 16:50:34 +00001019void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1020 StringRef isysroot,
Douglas Gregor112b9072012-10-18 05:31:06 +00001021 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +00001022 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001023 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1024 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +00001025
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001026 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001027 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1028 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1029 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1030 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1031 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1032 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1033 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1034 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1035 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1036 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1037 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001038 Record.push_back(VERSION_MAJOR);
1039 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001040 Record.push_back(CLANG_VERSION_MAJOR);
1041 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001042 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001043 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001044 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1045 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001046
Douglas Gregor112b9072012-10-18 05:31:06 +00001047 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001048 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001049 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Douglas Gregor29cc6422011-08-17 21:07:30 +00001050 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001051
1052 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1053 M != MEnd; ++M) {
1054 // Skip modules that weren't directly imported.
1055 if (!(*M)->isDirectlyImported())
1056 continue;
1057
1058 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis2ec29362012-11-15 18:57:22 +00001059 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor7029ce12013-03-19 00:28:20 +00001060 Record.push_back((*M)->File->getSize());
1061 Record.push_back((*M)->File->getModificationTime());
Douglas Gregordf0c1512011-08-18 04:12:04 +00001062 // FIXME: This writes the absolute path for AST files we depend on.
1063 const std::string &FileName = (*M)->FileName;
1064 Record.push_back(FileName.size());
1065 Record.append(FileName.begin(), FileName.end());
1066 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001067 Stream.EmitRecord(IMPORTS, Record);
1068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
Douglas Gregor112b9072012-10-18 05:31:06 +00001070 // Language options.
1071 Record.clear();
1072 const LangOptions &LangOpts = Context.getLangOpts();
1073#define LANGOPT(Name, Bits, Default, Description) \
1074 Record.push_back(LangOpts.Name);
1075#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1076 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1077#include "clang/Basic/LangOptions.def"
Will Dietzf54319c2013-01-18 11:30:38 +00001078#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1079#include "clang/Basic/Sanitizers.def"
Douglas Gregor112b9072012-10-18 05:31:06 +00001080
1081 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1082 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1083
1084 Record.push_back(LangOpts.CurrentModule.size());
1085 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001086
1087 // Comment options.
1088 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1089 for (CommentOptions::BlockCommandNamesTy::const_iterator
1090 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1091 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1092 I != IEnd; ++I) {
1093 AddString(*I, Record);
1094 }
Dmitri Gribenkoa7d16ce2013-04-10 15:35:17 +00001095 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenkoacf2e782013-02-22 14:21:27 +00001096
Douglas Gregor112b9072012-10-18 05:31:06 +00001097 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1098
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001099 // Target options.
1100 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001101 const TargetInfo &Target = Context.getTargetInfo();
1102 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001103 AddString(TargetOpts.Triple, Record);
1104 AddString(TargetOpts.CPU, Record);
1105 AddString(TargetOpts.ABI, Record);
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001106 AddString(TargetOpts.LinkerVersion, Record);
1107 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1108 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1109 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1110 }
1111 Record.push_back(TargetOpts.Features.size());
1112 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1113 AddString(TargetOpts.Features[I], Record);
1114 }
1115 Stream.EmitRecord(TARGET_OPTIONS, Record);
1116
Douglas Gregor8263ffb2012-10-24 15:17:15 +00001117 // Diagnostic options.
1118 Record.clear();
1119 const DiagnosticOptions &DiagOpts
1120 = Context.getDiagnostics().getDiagnosticOptions();
1121#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1122#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1123 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1124#include "clang/Basic/DiagnosticOptions.def"
1125 Record.push_back(DiagOpts.Warnings.size());
1126 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1127 AddString(DiagOpts.Warnings[I], Record);
1128 // Note: we don't serialize the log or serialization file names, because they
1129 // are generally transient files and will almost always be overridden.
1130 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1131
Douglas Gregorc6317db2012-10-24 15:49:58 +00001132 // File system options.
1133 Record.clear();
1134 const FileSystemOptions &FSOpts
1135 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1136 AddString(FSOpts.WorkingDir, Record);
1137 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1138
Douglas Gregor2d302362012-10-24 16:50:34 +00001139 // Header search options.
1140 Record.clear();
1141 const HeaderSearchOptions &HSOpts
1142 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1143 AddString(HSOpts.Sysroot, Record);
1144
1145 // Include entries.
1146 Record.push_back(HSOpts.UserEntries.size());
1147 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1148 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1149 AddString(Entry.Path, Record);
1150 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregor2d302362012-10-24 16:50:34 +00001151 Record.push_back(Entry.IsFramework);
1152 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregor2d302362012-10-24 16:50:34 +00001153 }
1154
1155 // System header prefixes.
1156 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1157 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1158 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1159 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1160 }
1161
1162 AddString(HSOpts.ResourceDir, Record);
1163 AddString(HSOpts.ModuleCachePath, Record);
Argyrios Kyrtzidis1594c152014-03-03 08:12:05 +00001164 AddString(HSOpts.ModuleUserBuildPath, Record);
Douglas Gregor2d302362012-10-24 16:50:34 +00001165 Record.push_back(HSOpts.DisableModuleHash);
1166 Record.push_back(HSOpts.UseBuiltinIncludes);
1167 Record.push_back(HSOpts.UseStandardSystemIncludes);
1168 Record.push_back(HSOpts.UseStandardCXXIncludes);
1169 Record.push_back(HSOpts.UseLibcxx);
1170 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1171
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001172 // Preprocessor options.
1173 Record.clear();
1174 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1175
1176 // Macro definitions.
1177 Record.push_back(PPOpts.Macros.size());
1178 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1179 AddString(PPOpts.Macros[I].first, Record);
1180 Record.push_back(PPOpts.Macros[I].second);
1181 }
1182
1183 // Includes
1184 Record.push_back(PPOpts.Includes.size());
1185 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1186 AddString(PPOpts.Includes[I], Record);
1187
1188 // Macro includes
1189 Record.push_back(PPOpts.MacroIncludes.size());
1190 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1191 AddString(PPOpts.MacroIncludes[I], Record);
1192
Douglas Gregorb6368752012-10-24 23:41:50 +00001193 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00001194 // Detailed record is important since it is used for the module cache hash.
1195 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001196 AddString(PPOpts.ImplicitPCHInclude, Record);
1197 AddString(PPOpts.ImplicitPTHInclude, Record);
1198 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1199 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1200
Douglas Gregora3b20262011-05-06 21:43:30 +00001201 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001202 SourceManager &SM = Context.getSourceManager();
1203 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1204 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001205 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1206 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001207 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1208 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1209
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001210 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001211
Michael J. Spencer740857f2010-12-21 16:45:57 +00001212 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001213
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001214 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001215 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001216 isysroot);
Douglas Gregorb6af6c22012-10-24 20:05:57 +00001217 Record.clear();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001218 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001219 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001220 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001221 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001222
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +00001223 Record.clear();
1224 Record.push_back(SM.getMainFileID().getOpaqueValue());
1225 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1226
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001227 // Original PCH directory
1228 if (!OutputFile.empty() && OutputFile != "-") {
1229 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1230 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1232 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1233
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001234 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001235
1236 llvm::sys::fs::make_absolute(OutputPath);
1237 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1238
1239 RecordData Record;
1240 Record.push_back(ORIGINAL_PCH_DIR);
1241 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1242 }
1243
Douglas Gregor49491f72013-03-15 22:15:07 +00001244 WriteInputFiles(Context.SourceMgr,
1245 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
Douglas Gregora3dd9002013-07-22 20:48:33 +00001246 isysroot,
1247 PP.getLangOpts().Modules);
Douglas Gregor72be3902012-10-19 00:38:02 +00001248 Stream.ExitBlock();
1249}
1250
Douglas Gregor49491f72013-03-15 22:15:07 +00001251namespace {
1252 /// \brief An input file.
1253 struct InputFileEntry {
1254 const FileEntry *File;
1255 bool IsSystemFile;
1256 bool BufferOverridden;
1257 };
1258}
1259
1260void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1261 HeaderSearchOptions &HSOpts,
Douglas Gregora3dd9002013-07-22 20:48:33 +00001262 StringRef isysroot,
1263 bool Modules) {
Douglas Gregor72be3902012-10-19 00:38:02 +00001264 using namespace llvm;
1265 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1266 RecordData Record;
1267
1268 // Create input-file abbreviation.
1269 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1270 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001271 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor72be3902012-10-19 00:38:02 +00001272 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1273 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001274 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor72be3902012-10-19 00:38:02 +00001275 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1276 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1277
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001278 // Get all ContentCache objects for files, sorted by whether the file is a
1279 // system one or not. System files go at the back, users files at the front.
Douglas Gregor49491f72013-03-15 22:15:07 +00001280 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor72be3902012-10-19 00:38:02 +00001281 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1282 // Get this source location entry.
1283 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumideca50f2012-10-19 01:53:57 +00001284 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor72be3902012-10-19 00:38:02 +00001285
1286 // We only care about file entries that were not overridden.
1287 if (!SLoc->isFile())
1288 continue;
1289 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001290 if (!Cache->OrigEntry)
Douglas Gregor72be3902012-10-19 00:38:02 +00001291 continue;
1292
Douglas Gregor49491f72013-03-15 22:15:07 +00001293 InputFileEntry Entry;
1294 Entry.File = Cache->OrigEntry;
1295 Entry.IsSystemFile = Cache->IsSystemFile;
1296 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001297 if (Cache->IsSystemFile)
Douglas Gregor49491f72013-03-15 22:15:07 +00001298 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001299 else
Douglas Gregor49491f72013-03-15 22:15:07 +00001300 SortedFiles.push_front(Entry);
1301 }
1302
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001303 unsigned UserFilesNum = 0;
1304 // Write out all of the input files.
1305 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001306 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001307 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001308 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001309
Douglas Gregor49491f72013-03-15 22:15:07 +00001310 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001311 if (InputFileID != 0)
1312 continue; // already recorded this file.
1313
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001314 // Record this entry's offset.
1315 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001316
1317 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001318
Douglas Gregor49491f72013-03-15 22:15:07 +00001319 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001320 ++UserFilesNum;
1321
Douglas Gregor72be3902012-10-19 00:38:02 +00001322 Record.clear();
1323 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001324 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001325
1326 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001327 Record.push_back(Entry.File->getSize());
1328 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001329
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001330 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001331 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001332
Douglas Gregor72be3902012-10-19 00:38:02 +00001333 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001334 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001335 SmallString<128> FilePath(Filename);
1336
1337 // Ask the file manager to fixup the relative path for us. This will
1338 // honor the working directory.
Ben Langmuircb69b572014-03-07 06:40:32 +00001339 SourceMgr.getFileManager().FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001340
1341 // FIXME: This call to make_absolute shouldn't be necessary, the
1342 // call to FixupRelativePath should always return an absolute path.
1343 llvm::sys::fs::make_absolute(FilePath);
1344 Filename = FilePath.c_str();
1345
1346 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1347
1348 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1349 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001350
Douglas Gregor112b9072012-10-18 05:31:06 +00001351 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001352
1353 // Create input file offsets abbreviation.
1354 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1355 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1356 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001357 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1358 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001359 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1360 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1361
1362 // Write input file offsets.
1363 Record.clear();
1364 Record.push_back(INPUT_FILE_OFFSETS);
1365 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001366 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001367 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001368}
1369
Douglas Gregora7f71a92009-04-10 03:52:48 +00001370//===----------------------------------------------------------------------===//
1371// Source Manager Serialization
1372//===----------------------------------------------------------------------===//
1373
1374/// \brief Create an abbreviation for the SLocEntry that refers to a
1375/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001376static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001377 using namespace llvm;
1378 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001379 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001384 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001389 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001390}
1391
1392/// \brief Create an abbreviation for the SLocEntry that refers to a
1393/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001394static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001395 using namespace llvm;
1396 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001397 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001398 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001403 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001404}
1405
1406/// \brief Create an abbreviation for the SLocEntry that refers to a
1407/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001408static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001409 using namespace llvm;
1410 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001411 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001413 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001414}
1415
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001416/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1417/// expansion.
1418static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001419 using namespace llvm;
1420 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001421 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001427 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001428}
1429
Douglas Gregor09b69892011-02-10 17:09:37 +00001430namespace {
1431 // Trait used for the on-disk hash table of header search information.
1432 class HeaderFileInfoTrait {
1433 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001434 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001435
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001436 // Keep track of the framework names we've used during serialization.
1437 SmallVector<char, 128> FrameworkStringData;
1438 llvm::StringMap<unsigned> FrameworkNameOffset;
1439
Douglas Gregor09b69892011-02-10 17:09:37 +00001440 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001441 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1442 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001443
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001444 struct key_type {
1445 const FileEntry *FE;
1446 const char *Filename;
1447 };
1448 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001449
1450 typedef HeaderFileInfo data_type;
1451 typedef const data_type &data_type_ref;
1452
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001453 static unsigned ComputeHash(key_type_ref key) {
1454 // The hash is based only on size/time of the file, so that the reader can
1455 // match even when symlinking or excess path elements ("foo/../", "../")
1456 // change the form of the name. However, complete path is still the key.
1457 return llvm::hash_combine(key.FE->getSize(),
1458 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001459 }
1460
1461 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001462 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1463 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1464 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001465 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001466 if (Data.isModuleHeader)
1467 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001468 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001469 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001470 }
1471
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001472 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1473 clang::io::Emit64(Out, key.FE->getSize());
1474 KeyLen -= 8;
1475 clang::io::Emit64(Out, key.FE->getModificationTime());
1476 KeyLen -= 8;
1477 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001478 }
1479
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001480 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001481 data_type_ref Data, unsigned DataLen) {
1482 using namespace clang::io;
1483 uint64_t Start = Out.tell(); (void)Start;
1484
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001485 unsigned char Flags = (Data.HeaderRole << 6)
1486 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001487 | (Data.isPragmaOnce << 4)
1488 | (Data.DirInfo << 2)
1489 | (Data.Resolved << 1)
1490 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001491 Emit8(Out, (uint8_t)Flags);
1492 Emit16(Out, (uint16_t) Data.NumIncludes);
1493
1494 if (!Data.ControllingMacro)
1495 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1496 else
1497 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001498
1499 unsigned Offset = 0;
1500 if (!Data.Framework.empty()) {
1501 // If this header refers into a framework, save the framework name.
1502 llvm::StringMap<unsigned>::iterator Pos
1503 = FrameworkNameOffset.find(Data.Framework);
1504 if (Pos == FrameworkNameOffset.end()) {
1505 Offset = FrameworkStringData.size() + 1;
1506 FrameworkStringData.append(Data.Framework.begin(),
1507 Data.Framework.end());
1508 FrameworkStringData.push_back(0);
1509
1510 FrameworkNameOffset[Data.Framework] = Offset;
1511 } else
1512 Offset = Pos->second;
1513 }
1514 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001515
1516 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001517 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001518 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1519 }
1520
Douglas Gregor09b69892011-02-10 17:09:37 +00001521 assert(Out.tell() - Start == DataLen && "Wrong data length");
1522 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001523
1524 const char *strings_begin() const { return FrameworkStringData.begin(); }
1525 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001526 };
1527} // end anonymous namespace
1528
1529/// \brief Write the header search block for the list of files that
1530///
1531/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001532void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001533 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001534 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1535
1536 if (FilesByUID.size() > HS.header_file_size())
1537 FilesByUID.resize(HS.header_file_size());
1538
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001539 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001540 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001541 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001542 unsigned NumHeaderSearchEntries = 0;
1543 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1544 const FileEntry *File = FilesByUID[UID];
1545 if (!File)
1546 continue;
1547
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001548 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1549 // from the external source if it was not provided already.
1550 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001551 if (HFI.External && Chain)
1552 continue;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001553 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1554 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001555
1556 // Turn the file name into an absolute path, if it isn't already.
1557 const char *Filename = File->getName();
1558 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1559
1560 // If we performed any translation on the file name at all, we need to
1561 // save this string, since the generator will refer to it later.
1562 if (Filename != File->getName()) {
1563 Filename = strdup(Filename);
1564 SavedStrings.push_back(Filename);
1565 }
1566
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001567 HeaderFileInfoTrait::key_type key = { File, Filename };
1568 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001569 ++NumHeaderSearchEntries;
1570 }
1571
1572 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001573 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001574 uint32_t BucketOffset;
1575 {
1576 llvm::raw_svector_ostream Out(TableData);
1577 // Make sure that no bucket is at offset 0
1578 clang::io::Emit32(Out, 0);
1579 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1580 }
1581
1582 // Create a blob abbreviation
1583 using namespace llvm;
1584 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1585 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001588 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1590 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1591
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001592 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001593 RecordData Record;
1594 Record.push_back(HEADER_SEARCH_TABLE);
1595 Record.push_back(BucketOffset);
1596 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001597 Record.push_back(TableData.size());
1598 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001599 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1600
1601 // Free all of the strings we had to duplicate.
1602 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001603 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001604}
1605
Douglas Gregora7f71a92009-04-10 03:52:48 +00001606/// \brief Writes the block containing the serialized form of the
1607/// source manager.
1608///
1609/// TODO: We should probably use an on-disk hash table (stored in a
1610/// blob), indexed based on the file name, so that we only create
1611/// entries for files that we actually need. In the common case (no
1612/// errors), we probably won't have to create file entries for any of
1613/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001614void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001615 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001616 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001617 RecordData Record;
1618
Chris Lattner0910e3b2009-04-10 17:16:57 +00001619 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001620 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001621
1622 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001623 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1624 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1625 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001626 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001627
Douglas Gregor258ae542009-04-27 06:38:32 +00001628 // Write out the source location entry table. We skip the first
1629 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001630 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001631 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001632 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1633 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001634 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001635 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001636 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001637 FileID FID = FileID::get(I);
1638 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001639
Douglas Gregor258ae542009-04-27 06:38:32 +00001640 // Record the offset of this source-location entry.
1641 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1642
1643 // Figure out which record code to use.
1644 unsigned Code;
1645 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001646 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1647 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001648 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001649 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001650 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001651 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001652 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001653 Record.clear();
1654 Record.push_back(Code);
1655
Douglas Gregor925296b2011-07-19 16:10:42 +00001656 // Starting offset of this entry within this module, so skip the dummy.
1657 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001658 if (SLoc->isFile()) {
1659 const SrcMgr::FileInfo &File = SLoc->getFile();
1660 Record.push_back(File.getIncludeLoc().getRawEncoding());
1661 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1662 Record.push_back(File.hasLineDirectives());
1663
1664 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001665 if (Content->OrigEntry) {
1666 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001667 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001668
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001669 // The source location entry is a file. Emit input file ID.
1670 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1671 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001672
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001673 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001674
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001675 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001676 if (FDI != FileDeclIDs.end()) {
1677 Record.push_back(FDI->second->FirstDeclIndex);
1678 Record.push_back(FDI->second->DeclIDs.size());
1679 } else {
1680 Record.push_back(0);
1681 Record.push_back(0);
1682 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001683
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001684 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001685
1686 if (Content->BufferOverridden) {
1687 Record.clear();
1688 Record.push_back(SM_SLOC_BUFFER_BLOB);
1689 const llvm::MemoryBuffer *Buffer
1690 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1691 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1692 StringRef(Buffer->getBufferStart(),
1693 Buffer->getBufferSize() + 1));
1694 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001695 } else {
1696 // The source location entry is a buffer. The blob associated
1697 // with this entry contains the contents of the buffer.
1698
1699 // We add one to the size so that we capture the trailing NULL
1700 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1701 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001702 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001703 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001704 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001705 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001706 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001707 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001708 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001709 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001710 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001711 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001712
Douglas Gregor925296b2011-07-19 16:10:42 +00001713 if (strcmp(Name, "<built-in>") == 0) {
1714 PreloadSLocs.push_back(SLocEntryOffsets.size());
1715 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001716 }
1717 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001718 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001719 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001720 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1721 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001722 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1723 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001724
1725 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001726 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001727 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001728 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001729 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001730 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001731 }
1732 }
1733
Douglas Gregor8f45df52009-04-16 22:23:12 +00001734 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001735
1736 if (SLocEntryOffsets.empty())
1737 return;
1738
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001739 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001740 // table is used for lazily loading source-location information.
1741 using namespace llvm;
1742 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001743 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1747 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001748
Douglas Gregor258ae542009-04-27 06:38:32 +00001749 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001750 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001751 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001752 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001753 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001754
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001755 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001756 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001757 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001758
1759 // Write the line table. It depends on remapping working, so it must come
1760 // after the source location offsets.
1761 if (SourceMgr.hasLineTable()) {
1762 LineTableInfo &LineTable = SourceMgr.getLineTable();
1763
1764 Record.clear();
1765 // Emit the file names
1766 Record.push_back(LineTable.getNumFilenames());
1767 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1768 // Emit the file name
1769 const char *Filename = LineTable.getFilename(I);
1770 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1771 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1772 Record.push_back(FilenameLen);
1773 if (FilenameLen)
1774 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1775 }
1776
1777 // Emit the line entries
1778 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1779 L != LEnd; ++L) {
1780 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001781 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001782 continue;
1783
1784 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001785 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001786
1787 // Emit the line entries
1788 Record.push_back(L->second.size());
1789 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1790 LEEnd = L->second.end();
1791 LE != LEEnd; ++LE) {
1792 Record.push_back(LE->FileOffset);
1793 Record.push_back(LE->LineNo);
1794 Record.push_back(LE->FilenameID);
1795 Record.push_back((unsigned)LE->FileKind);
1796 Record.push_back(LE->IncludeOffset);
1797 }
1798 }
1799 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1800 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001801}
1802
Douglas Gregorc5046832009-04-27 18:38:38 +00001803//===----------------------------------------------------------------------===//
1804// Preprocessor Serialization
1805//===----------------------------------------------------------------------===//
1806
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001807namespace {
1808class ASTMacroTableTrait {
1809public:
1810 typedef IdentID key_type;
1811 typedef key_type key_type_ref;
1812
1813 struct Data {
1814 uint32_t MacroDirectivesOffset;
1815 };
1816
1817 typedef Data data_type;
1818 typedef const data_type &data_type_ref;
1819
1820 static unsigned ComputeHash(IdentID IdID) {
1821 return llvm::hash_value(IdID);
1822 }
1823
1824 std::pair<unsigned,unsigned>
1825 static EmitKeyDataLength(raw_ostream& Out,
1826 key_type_ref Key, data_type_ref Data) {
1827 unsigned KeyLen = 4; // IdentID.
1828 unsigned DataLen = 4; // MacroDirectivesOffset.
1829 return std::make_pair(KeyLen, DataLen);
1830 }
1831
1832 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1833 clang::io::Emit32(Out, Key);
1834 }
1835
1836 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1837 unsigned) {
1838 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1839 }
1840};
1841} // end anonymous namespace
1842
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001843static int compareMacroDirectives(
1844 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1845 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1846 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001847}
1848
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001849static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1850 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001851 if (MacroInfo *MI = MD->getMacroInfo())
1852 if (MI->isBuiltinMacro())
1853 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001854
1855 if (IsModule) {
1856 SourceLocation Loc = MD->getLocation();
1857 if (Loc.isInvalid())
1858 return true;
1859 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1860 return true;
1861 }
1862
1863 return false;
1864}
1865
Chris Lattnereeffaef2009-04-10 17:15:23 +00001866/// \brief Writes the block containing the serialized form of the
1867/// preprocessor.
1868///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001869void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001870 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1871 if (PPRec)
1872 WritePreprocessorDetail(*PPRec);
1873
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001874 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001875
Chris Lattner0af3ba12009-04-13 01:29:17 +00001876 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1877 if (PP.getCounterValue() != 0) {
1878 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001879 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001880 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001881 }
1882
1883 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001884 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001885
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001886 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001887 // FIXME: use diagnostics subsystem for localization etc.
1888 if (PP.SawDateOrTime())
1889 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001890
Douglas Gregor796d76a2010-10-20 22:00:55 +00001891
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001892 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001893 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001894
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001895 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001896 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001897 MacroDirectives;
1898 for (Preprocessor::macro_iterator
1899 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1900 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001901 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001902 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001903 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001904
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001905 // Sort the set of macro definitions that need to be serialized by the
1906 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001907 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1908 &compareMacroDirectives);
1909
1910 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1911
1912 // Emit the macro directives as a list and associate the offset with the
1913 // identifier they belong to.
1914 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1915 const IdentifierInfo *Name = MacroDirectives[I].first;
1916 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1917 MacroDirective *MD = MacroDirectives[I].second;
1918
1919 // If the macro or identifier need no updates, don't write the macro history
1920 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001921 // FIXME: Chain the macro history instead of re-writing it.
1922 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001923 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1924 continue;
1925
1926 // Emit the macro directives in reverse source order.
1927 for (; MD; MD = MD->getPrevious()) {
1928 if (shouldIgnoreMacro(MD, IsModule, PP))
1929 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001930
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001931 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001932 Record.push_back(MD->getKind());
1933 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1934 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1935 Record.push_back(InfoID);
1936 Record.push_back(DefMD->isImported());
1937 Record.push_back(DefMD->isAmbiguous());
1938
1939 } else if (VisibilityMacroDirective *
1940 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1941 Record.push_back(VisMD->isPublic());
1942 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001943 }
1944 if (Record.empty())
1945 continue;
1946
1947 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1948 Record.clear();
1949
1950 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1951
1952 IdentID NameID = getIdentifierRef(Name);
1953 ASTMacroTableTrait::Data data;
1954 data.MacroDirectivesOffset = MacroDirectiveOffset;
1955 Generator.insert(NameID, data);
1956 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001957
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001958 /// \brief Offsets of each of the macros into the bitstream, indexed by
1959 /// the local macro ID
1960 ///
1961 /// For each identifier that is associated with a macro, this map
1962 /// provides the offset into the bitstream where that macro is
1963 /// defined.
1964 std::vector<uint32_t> MacroOffsets;
1965
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001966 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1967 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1968 MacroInfo *MI = MacroInfosToEmit[I].MI;
1969 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001970
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001971 if (ID < FirstMacroID) {
1972 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1973 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001974 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001975
1976 // Record the local offset of this macro.
1977 unsigned Index = ID - FirstMacroID;
1978 if (Index == MacroOffsets.size())
1979 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1980 else {
1981 if (Index > MacroOffsets.size())
1982 MacroOffsets.resize(Index + 1);
1983
1984 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1985 }
1986
1987 AddIdentifierRef(Name, Record);
1988 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1989 AddSourceLocation(MI->getDefinitionLoc(), Record);
1990 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1991 Record.push_back(MI->isUsed());
1992 unsigned Code;
1993 if (MI->isObjectLike()) {
1994 Code = PP_MACRO_OBJECT_LIKE;
1995 } else {
1996 Code = PP_MACRO_FUNCTION_LIKE;
1997
1998 Record.push_back(MI->isC99Varargs());
1999 Record.push_back(MI->isGNUVarargs());
2000 Record.push_back(MI->hasCommaPasting());
2001 Record.push_back(MI->getNumArgs());
2002 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2003 I != E; ++I)
2004 AddIdentifierRef(*I, Record);
2005 }
2006
2007 // If we have a detailed preprocessing record, record the macro definition
2008 // ID that corresponds to this macro.
2009 if (PPRec)
2010 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2011
2012 Stream.EmitRecord(Code, Record);
2013 Record.clear();
2014
2015 // Emit the tokens array.
2016 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2017 // Note that we know that the preprocessor does not have any annotation
2018 // tokens in it because they are created by the parser, and thus can't
2019 // be in a macro definition.
2020 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002021 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002022 Stream.EmitRecord(PP_TOKEN, Record);
2023 Record.clear();
2024 }
2025 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002026 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002027
Douglas Gregor92a96f52011-02-08 21:58:10 +00002028 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002029
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002030 // Create the on-disk hash table in a buffer.
2031 SmallString<4096> MacroTable;
2032 uint32_t BucketOffset;
2033 {
2034 llvm::raw_svector_ostream Out(MacroTable);
2035 // Make sure that no bucket is at offset 0
2036 clang::io::Emit32(Out, 0);
2037 BucketOffset = Generator.Emit(Out);
2038 }
2039
2040 // Write the macro table
2041 using namespace llvm;
2042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2043 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2046 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2047
2048 Record.push_back(MACRO_TABLE);
2049 Record.push_back(BucketOffset);
2050 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2051 Record.clear();
2052
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002053 // Write the offsets table for macro IDs.
2054 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002055 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002056 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2060
2061 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2062 Record.clear();
2063 Record.push_back(MACRO_OFFSET);
2064 Record.push_back(MacroOffsets.size());
2065 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2066 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2067 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002068}
2069
2070void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002071 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002072 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002073
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002074 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002075
Douglas Gregor92a96f52011-02-08 21:58:10 +00002076 // Enter the preprocessor block.
2077 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002078
Douglas Gregoraae92242010-03-19 21:51:54 +00002079 // If the preprocessor has a preprocessing record, emit it.
2080 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002081 using namespace llvm;
2082
2083 // Set up the abbreviation for
2084 unsigned InclusionAbbrev = 0;
2085 {
2086 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2087 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002091 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002092 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2093 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2094 }
2095
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002096 unsigned FirstPreprocessorEntityID
2097 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2098 + NUM_PREDEF_PP_ENTITY_IDS;
2099 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002100 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002101 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2102 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002103 E != EEnd;
2104 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002105 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002106
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002107 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2108 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002109
Douglas Gregor92a96f52011-02-08 21:58:10 +00002110 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002111 // Record this macro definition's ID.
2112 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002113
Douglas Gregor92a96f52011-02-08 21:58:10 +00002114 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002115 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2116 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002117 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002118
Chandler Carrutha88a22182011-07-14 08:20:46 +00002119 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002120 Record.push_back(ME->isBuiltinMacro());
2121 if (ME->isBuiltinMacro())
2122 AddIdentifierRef(ME->getName(), Record);
2123 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002124 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002125 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002126 continue;
2127 }
2128
2129 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2130 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002131 Record.push_back(ID->getFileName().size());
2132 Record.push_back(ID->wasInQuotes());
2133 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002134 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002135 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002136 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002137 // Check that the FileEntry is not null because it was not resolved and
2138 // we create a PCH even with compiler errors.
2139 if (ID->getFile())
2140 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002141 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2142 continue;
2143 }
2144
2145 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2146 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002147 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002148
Douglas Gregoraae92242010-03-19 21:51:54 +00002149 // Write the offsets table for the preprocessing record.
2150 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002151 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2152
Douglas Gregoraae92242010-03-19 21:51:54 +00002153 // Write the offsets table for identifier IDs.
2154 using namespace llvm;
2155 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002156 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002158 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002159 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002160
Douglas Gregoraae92242010-03-19 21:51:54 +00002161 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002162 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002163 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002164 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2165 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002166 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002167}
2168
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002169unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2170 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2171 if (Known != SubmoduleIDs.end())
2172 return Known->second;
2173
2174 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2175}
2176
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002177unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2178 if (!Mod)
2179 return 0;
2180
2181 llvm::DenseMap<Module *, unsigned>::const_iterator
2182 Known = SubmoduleIDs.find(Mod);
2183 if (Known != SubmoduleIDs.end())
2184 return Known->second;
2185
2186 return 0;
2187}
2188
Douglas Gregor253eefe2011-12-01 00:59:36 +00002189/// \brief Compute the number of modules within the given tree (including the
2190/// given module).
2191static unsigned getNumberOfModules(Module *Mod) {
2192 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002193 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2194 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002195 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002196 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002197
2198 return ChildModules + 1;
2199}
2200
Douglas Gregorde3ef502011-11-30 23:21:26 +00002201void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002202 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002203 // FIXME: This feels like it belongs somewhere else, but there are no
2204 // other consumers of this information.
2205 SourceManager &SrcMgr = PP->getSourceManager();
2206 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2207 for (ASTContext::import_iterator I = Context->local_import_begin(),
2208 IEnd = Context->local_import_end();
2209 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002210 if (Module *ImportedFrom
2211 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2212 SrcMgr))) {
2213 ImportedFrom->Imports.push_back(I->getImportedModule());
2214 }
2215 }
2216
Douglas Gregor69021972011-11-30 17:33:56 +00002217 // Enter the submodule description block.
2218 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2219
2220 // Write the abbreviations needed for the submodules block.
2221 using namespace llvm;
2222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2223 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2234 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2235
2236 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002237 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2239 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2240
2241 Abbrev = new BitCodeAbbrev();
2242 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2244 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002245
2246 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002247 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2249 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2250
2251 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002252 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2254 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2255
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002256 Abbrev = new BitCodeAbbrev();
2257 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002260 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2261
Douglas Gregor59527662012-10-15 06:28:11 +00002262 Abbrev = new BitCodeAbbrev();
2263 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2265 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2266
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002267 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002268 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2270 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2271
2272 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002273 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2276 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2277
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002278 Abbrev = new BitCodeAbbrev();
2279 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2281 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2282
Douglas Gregorfb912652013-03-20 21:10:35 +00002283 Abbrev = new BitCodeAbbrev();
2284 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2287 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2288
Douglas Gregor253eefe2011-12-01 00:59:36 +00002289 // Write the submodule metadata block.
2290 RecordData Record;
2291 Record.push_back(getNumberOfModules(WritingModule));
2292 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2293 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2294
Douglas Gregor69021972011-11-30 17:33:56 +00002295 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002296 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002297 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002298 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002299 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002300 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002301 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002302
2303 // Emit the definition of the block.
2304 Record.clear();
2305 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002306 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002307 if (Mod->Parent) {
2308 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2309 Record.push_back(SubmoduleIDs[Mod->Parent]);
2310 } else {
2311 Record.push_back(0);
2312 }
2313 Record.push_back(Mod->IsFramework);
2314 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002315 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002316 Record.push_back(Mod->InferSubmodules);
2317 Record.push_back(Mod->InferExplicitSubmodules);
2318 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002319 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002320 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2321
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002322 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002323 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002324 Record.clear();
2325 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002326 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002327 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002328 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002329 }
2330
Douglas Gregor69021972011-11-30 17:33:56 +00002331 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002332 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002333 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002334 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002335 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002336 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002337 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2338 Record.clear();
2339 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2340 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2341 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002342 }
2343
2344 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002345 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002346 Record.clear();
2347 Record.push_back(SUBMODULE_HEADER);
2348 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002349 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002350 }
Douglas Gregor59527662012-10-15 06:28:11 +00002351 // Emit the excluded headers.
2352 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2353 Record.clear();
2354 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2355 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2356 Mod->ExcludedHeaders[I]->getName());
2357 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002358 // Emit the private headers.
2359 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2360 Record.clear();
2361 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2362 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2363 Mod->PrivateHeaders[I]->getName());
2364 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002365 ArrayRef<const FileEntry *>
2366 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2367 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002368 Record.clear();
2369 Record.push_back(SUBMODULE_TOPHEADER);
2370 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002371 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002372 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002373
2374 // Emit the imports.
2375 if (!Mod->Imports.empty()) {
2376 Record.clear();
2377 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002378 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002379 assert(ImportedID && "Unknown submodule!");
2380 Record.push_back(ImportedID);
2381 }
2382 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2383 }
2384
Douglas Gregor24bb9232011-12-02 18:58:38 +00002385 // Emit the exports.
2386 if (!Mod->Exports.empty()) {
2387 Record.clear();
2388 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002389 if (Module *Exported = Mod->Exports[I].getPointer()) {
2390 unsigned ExportedID = SubmoduleIDs[Exported];
2391 assert(ExportedID > 0 && "Unknown submodule ID?");
2392 Record.push_back(ExportedID);
2393 } else {
2394 Record.push_back(0);
2395 }
2396
Douglas Gregor24bb9232011-12-02 18:58:38 +00002397 Record.push_back(Mod->Exports[I].getInt());
2398 }
2399 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2400 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002401
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002402 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2403 // Might be unnecessary as use declarations are only used to build the
2404 // module itself.
2405
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002406 // Emit the link libraries.
2407 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2408 Record.clear();
2409 Record.push_back(SUBMODULE_LINK_LIBRARY);
2410 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2411 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2412 Mod->LinkLibraries[I].Library);
2413 }
2414
Douglas Gregorfb912652013-03-20 21:10:35 +00002415 // Emit the conflicts.
2416 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2417 Record.clear();
2418 Record.push_back(SUBMODULE_CONFLICT);
2419 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2420 assert(OtherID && "Unknown submodule!");
2421 Record.push_back(OtherID);
2422 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2423 Mod->Conflicts[I].Message);
2424 }
2425
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002426 // Emit the configuration macros.
2427 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2428 Record.clear();
2429 Record.push_back(SUBMODULE_CONFIG_MACRO);
2430 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2431 Mod->ConfigMacros[I]);
2432 }
2433
Douglas Gregor69021972011-11-30 17:33:56 +00002434 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002435 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2436 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002437 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002438 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002439 }
2440
2441 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002442
2443 assert((NextSubmoduleID - FirstSubmoduleID
2444 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002445}
2446
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002447serialization::SubmoduleID
2448ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002449 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002450 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002451
2452 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002453 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002454 Module *OwningMod
2455 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002456 if (!OwningMod)
2457 return 0;
2458
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002459 // Check whether this submodule is part of our own module.
2460 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002461 return 0;
2462
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002463 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002464}
2465
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002466void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2467 bool isModule) {
2468 // Make sure set diagnostic pragmas don't affect the translation unit that
2469 // imports the module.
2470 // FIXME: Make diagnostic pragma sections work properly with modules.
2471 if (isModule)
2472 return;
2473
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002474 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2475 DiagStateIDMap;
2476 unsigned CurrID = 0;
2477 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002478 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002479 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002480 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2481 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002482 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002483 if (point.Loc.isInvalid())
2484 continue;
2485
2486 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002487 unsigned &DiagStateID = DiagStateIDMap[point.State];
2488 Record.push_back(DiagStateID);
2489
2490 if (DiagStateID == 0) {
2491 DiagStateID = ++CurrID;
2492 for (DiagnosticsEngine::DiagState::const_iterator
2493 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2494 if (I->second.isPragma()) {
2495 Record.push_back(I->first);
2496 Record.push_back(I->second.getMapping());
2497 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002498 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002499 Record.push_back(-1); // mark the end of the diag/map pairs for this
2500 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002501 }
2502 }
2503
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002504 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002505 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002506}
2507
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002508void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2509 if (CXXBaseSpecifiersOffsets.empty())
2510 return;
2511
2512 RecordData Record;
2513
2514 // Create a blob abbreviation for the C++ base specifiers offsets.
2515 using namespace llvm;
2516
2517 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2518 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2519 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2520 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2521 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2522
Douglas Gregorc27b2872011-08-04 00:01:48 +00002523 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002524 Record.clear();
2525 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2526 Record.push_back(CXXBaseSpecifiersOffsets.size());
2527 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002528 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002529}
2530
Douglas Gregorc5046832009-04-27 18:38:38 +00002531//===----------------------------------------------------------------------===//
2532// Type Serialization
2533//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002534
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002535/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002536void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002537 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002538 if (Idx.getIndex() == 0) // we haven't seen this type before.
2539 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002540
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002541 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002542
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002543 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002544 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002545 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002546 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002547 else if (TypeOffsets.size() < Index) {
2548 TypeOffsets.resize(Index + 1);
2549 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002550 }
2551
2552 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002553
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002554 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002555 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002556
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002557 if (T.hasLocalNonFastQualifiers()) {
2558 Qualifiers Qs = T.getLocalQualifiers();
2559 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002560 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002561 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002562 } else {
2563 switch (T->getTypeClass()) {
2564 // For all of the concrete, non-dependent types, call the
2565 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002566#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002567 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002568#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002569#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002570 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002571 }
2572
2573 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002574 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002575
2576 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002577 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002578}
2579
Douglas Gregorc5046832009-04-27 18:38:38 +00002580//===----------------------------------------------------------------------===//
2581// Declaration Serialization
2582//===----------------------------------------------------------------------===//
2583
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002584/// \brief Write the block containing all of the declaration IDs
2585/// lexically declared within the given DeclContext.
2586///
2587/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2588/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002589uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002590 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002591 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002592 return 0;
2593
Douglas Gregor8f45df52009-04-16 22:23:12 +00002594 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002595 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002596 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002597 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002598 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2599 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002600 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002601
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002602 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002603 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002604 return Offset;
2605}
2606
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002607void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002608 using namespace llvm;
2609 RecordData Record;
2610
2611 // Write the type offsets array
2612 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002613 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2617 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2618 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002619 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002620 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002621 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002622 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002623
2624 // Write the declaration offsets array
2625 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002626 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002627 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002628 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2630 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2631 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002632 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002633 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002634 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002635 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002636}
2637
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002638void ASTWriter::WriteFileDeclIDsMap() {
2639 using namespace llvm;
2640 RecordData Record;
2641
2642 // Join the vectors of DeclIDs from all files.
2643 SmallVector<DeclID, 256> FileSortedIDs;
2644 for (FileDeclIDsTy::iterator
2645 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2646 DeclIDInFileInfo &Info = *FI->second;
2647 Info.FirstDeclIndex = FileSortedIDs.size();
2648 for (LocDeclIDsTy::iterator
2649 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2650 FileSortedIDs.push_back(DI->second);
2651 }
2652
2653 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2654 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2657 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2658 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002659 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002660 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2661}
2662
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002663void ASTWriter::WriteComments() {
2664 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002665 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002666 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002667 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2668 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002669 I != E; ++I) {
2670 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002671 AddSourceRange((*I)->getSourceRange(), Record);
2672 Record.push_back((*I)->getKind());
2673 Record.push_back((*I)->isTrailingComment());
2674 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002675 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2676 }
2677 Stream.ExitBlock();
2678}
2679
Douglas Gregorc5046832009-04-27 18:38:38 +00002680//===----------------------------------------------------------------------===//
2681// Global Method Pool and Selector Serialization
2682//===----------------------------------------------------------------------===//
2683
Douglas Gregore84a9da2009-04-20 20:36:09 +00002684namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002685// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002686class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002687 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002688
2689public:
2690 typedef Selector key_type;
2691 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002692
Sebastian Redl834bb972010-08-04 17:20:04 +00002693 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002694 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002695 ObjCMethodList Instance, Factory;
2696 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002697 typedef const data_type& data_type_ref;
2698
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002699 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002700
Douglas Gregorc78d3462009-04-24 21:10:55 +00002701 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002702 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002703 }
Mike Stump11289f42009-09-09 15:08:12 +00002704
2705 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002706 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002707 data_type_ref Methods) {
2708 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2709 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002710 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2711 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002712 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002713 if (Method->Method)
2714 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002715 for (const ObjCMethodList *Method = &Methods.Factory; 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;
2719 clang::io::Emit16(Out, DataLen);
2720 return std::make_pair(KeyLen, DataLen);
2721 }
Mike Stump11289f42009-09-09 15:08:12 +00002722
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002723 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002724 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002725 assert((Start >> 32) == 0 && "Selector key offset too large");
2726 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002727 unsigned N = Sel.getNumArgs();
2728 clang::io::Emit16(Out, N);
2729 if (N == 0)
2730 N = 1;
2731 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002732 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002733 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2734 }
Mike Stump11289f42009-09-09 15:08:12 +00002735
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002736 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002737 data_type_ref Methods, unsigned DataLen) {
2738 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002739 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002740 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002741 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002742 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002743 if (Method->Method)
2744 ++NumInstanceMethods;
2745
2746 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002747 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002748 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002749 if (Method->Method)
2750 ++NumFactoryMethods;
2751
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002752 unsigned InstanceBits = Methods.Instance.getBits();
2753 assert(InstanceBits < 4);
2754 unsigned NumInstanceMethodsAndBits =
2755 (NumInstanceMethods << 2) | InstanceBits;
2756 unsigned FactoryBits = Methods.Factory.getBits();
2757 assert(FactoryBits < 4);
2758 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2759 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2760 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002761 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002762 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002763 if (Method->Method)
2764 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002765 for (const ObjCMethodList *Method = &Methods.Factory; 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));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002769
2770 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002771 }
2772};
2773} // end anonymous namespace
2774
Sebastian Redla19a67f2010-08-03 21:58:15 +00002775/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002776///
2777/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002778/// in an on-disk hash table indexed by the selector. The hash table also
2779/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002780void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002781 using namespace llvm;
2782
Sebastian Redla19a67f2010-08-03 21:58:15 +00002783 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002784 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002785 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002786 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002787 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002788 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002789 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002790 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002791
Sebastian Redla19a67f2010-08-03 21:58:15 +00002792 // Create the on-disk hash table representation. We walk through every
2793 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002794 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002795 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002796 I = SelectorIDs.begin(), E = SelectorIDs.end();
2797 I != E; ++I) {
2798 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002799 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002800 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002801 I->second,
2802 ObjCMethodList(),
2803 ObjCMethodList()
2804 };
2805 if (F != SemaRef.MethodPool.end()) {
2806 Data.Instance = F->second.first;
2807 Data.Factory = F->second.second;
2808 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002809 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002810 // changed.
2811 if (Chain && I->second < FirstSelectorID) {
2812 // Selector already exists. Did it change?
2813 bool changed = false;
2814 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002815 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002816 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002817 changed = true;
2818 }
2819 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002820 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002821 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002822 changed = true;
2823 }
2824 if (!changed)
2825 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002826 } else if (Data.Instance.Method || Data.Factory.Method) {
2827 // A new method pool entry.
2828 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002829 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002830 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002831 }
2832
Douglas Gregorc78d3462009-04-24 21:10:55 +00002833 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002834 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002835 uint32_t BucketOffset;
2836 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002837 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002838 llvm::raw_svector_ostream Out(MethodPool);
2839 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002840 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002841 BucketOffset = Generator.Emit(Out, Trait);
2842 }
2843
2844 // Create a blob abbreviation
2845 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002846 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2850 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2851
Douglas Gregor95c13f52009-04-25 17:48:32 +00002852 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002853 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002854 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002855 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002856 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002857 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002858
2859 // Create a blob abbreviation for the selector table offsets.
2860 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002861 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002862 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002863 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002864 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2865 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2866
2867 // Write the selector offsets table.
2868 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002869 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002870 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002871 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002872 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002873 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002874 }
2875}
2876
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002877/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002878void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002879 using namespace llvm;
2880 if (SemaRef.ReferencedSelectors.empty())
2881 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002882
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002883 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002884
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002885 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002886 // very tricky to fix, and given that @selector shouldn't really appear in
2887 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002888 for (DenseMap<Selector, SourceLocation>::iterator S =
2889 SemaRef.ReferencedSelectors.begin(),
2890 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2891 Selector Sel = (*S).first;
2892 SourceLocation Loc = (*S).second;
2893 AddSelectorRef(Sel, Record);
2894 AddSourceLocation(Loc, Record);
2895 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002896 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002897}
2898
Douglas Gregorc5046832009-04-27 18:38:38 +00002899//===----------------------------------------------------------------------===//
2900// Identifier Table Serialization
2901//===----------------------------------------------------------------------===//
2902
Douglas Gregorc78d3462009-04-24 21:10:55 +00002903namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002904class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002905 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002906 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002907 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002908 bool IsModule;
2909
Douglas Gregor1d583f22009-04-28 21:18:29 +00002910 /// \brief Determines whether this is an "interesting" identifier
2911 /// that needs a full IdentifierInfo structure written into the hash
2912 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002913 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002914 if (II->isPoisoned() ||
2915 II->isExtensionToken() ||
2916 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002917 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002918 II->getFETokenInfo<void>())
2919 return true;
2920
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002921 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002922 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002923
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002924 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002925 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002926 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002927
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002928 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2929 if (!IsModule)
2930 return !shouldIgnoreMacro(Macro, IsModule, PP);
2931 SubmoduleID ModID;
2932 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2933 return true;
2934 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002935
2936 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002937 }
2938
Richard Smith49f906a2014-03-01 00:08:04 +00002939 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2940
2941 MacroDirective *
2942 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002943 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002944 llvm::SmallVector<SubmoduleID, 1> Overridden;
2945 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2946 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2947 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002948 return 0;
2949 }
2950
Richard Smith49f906a2014-03-01 00:08:04 +00002951 MacroDirective *
2952 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2953 OverriddenList &Overridden) {
2954 if (MacroDirective *NextMD =
2955 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2956 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2957 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002958 return 0;
2959 }
2960
2961 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00002962 /// public macro definition or undefinition that is not in ModID.
2963 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002964 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00002965 /// ModID is updated to the module containing the returned directive.
2966 ///
2967 /// FIXME: This process breaks down if a module defines a macro, imports
2968 /// another submodule that changes the macro, then changes the
2969 /// macro again itself.
2970 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2971 SubmoduleID &ModID,
2972 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002973 if (!MD)
2974 return 0;
2975
Richard Smith49f906a2014-03-01 00:08:04 +00002976 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00002977 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002978 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002979 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002980 SubmoduleID ThisModID = getSubmoduleID(MD);
2981 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00002982 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002983 continue;
2984 }
Richard Smith49f906a2014-03-01 00:08:04 +00002985 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002986 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00002987 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002988 }
Richard Smith49f906a2014-03-01 00:08:04 +00002989
2990 // If this is a definition from a submodule import, that submodule's
2991 // definition is overridden by the definition or undefinition that we
2992 // started with.
2993 // FIXME: This should only apply to macros defined in OrigModID.
2994 // We can't do that currently, because a #include of a different submodule
2995 // of the same module just leaks through macros instead of providing new
2996 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00002997 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2998 // Figure out which submodule the macro was originally defined within.
2999 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3000 if (!SourceID) {
3001 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3002 if (DefLoc == MD->getLocation())
3003 SourceID = ThisModID;
3004 else
3005 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3006 }
3007 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003008 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003009 }
Richard Smith49f906a2014-03-01 00:08:04 +00003010
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003011 // We are looking for a definition in a different submodule than the one
3012 // that we started with. If a submodule has re-definitions of the same
3013 // macro, only the last definition will be used as the "exported" one.
3014 if (ModID == OrigModID)
3015 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003016
Richard Smith49f906a2014-03-01 00:08:04 +00003017 // The latest visibility directive for a name in a submodule affects all
3018 // the directives that come before it.
3019 if (VisibilityMacroDirective *VisMD =
3020 dyn_cast<VisibilityMacroDirective>(MD)) {
3021 if (!IsPublic.hasValue())
3022 IsPublic = VisMD->isPublic();
3023 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3024 // FIXME: If we find an imported macro, we should include its list of
3025 // overrides in our export.
3026 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003027 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003028 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003029
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003030 return 0;
3031 }
3032
3033 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003034 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003035 }
3036
Douglas Gregore84a9da2009-04-20 20:36:09 +00003037public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003038 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003039 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003040
Sebastian Redl539c5062010-08-18 23:57:32 +00003041 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003042 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003043
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003044 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3045 IdentifierResolver &IdResolver, bool IsModule)
3046 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003047
3048 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003049 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003050 }
Mike Stump11289f42009-09-09 15:08:12 +00003051
3052 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003053 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003054 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003055 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003056 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003057 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003058 DataLen += 2; // 2 bytes for builtin ID
3059 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003060 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003061 DataLen += 4; // MacroDirectives offset.
3062 if (IsModule) {
3063 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003064 llvm::SmallVector<SubmoduleID, 4> Overridden;
3065 for (MacroDirective *
3066 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3067 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3068 // Previous macro's overrides.
3069 if (!Overridden.empty())
3070 DataLen += 4 * (1 + Overridden.size());
3071 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003072 }
Richard Smith49f906a2014-03-01 00:08:04 +00003073 // Previous macro's overrides.
3074 if (!Overridden.empty())
3075 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003076 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003077 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003078 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003079
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003080 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3081 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003082 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003083 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003084 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003085 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003086 // We emit the key length after the data length so that every
3087 // string is preceded by a 16-bit length. This matches the PTH
3088 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003089 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003090 return std::make_pair(KeyLen, DataLen);
3091 }
Mike Stump11289f42009-09-09 15:08:12 +00003092
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003093 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003094 unsigned KeyLen) {
3095 // Record the location of the key data. This is used when generating
3096 // the mapping from persistent IDs to strings.
3097 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003098 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003099 }
Mike Stump11289f42009-09-09 15:08:12 +00003100
Richard Smith49f906a2014-03-01 00:08:04 +00003101 static void emitMacroOverrides(raw_ostream &Out,
3102 llvm::ArrayRef<SubmoduleID> Overridden) {
3103 if (!Overridden.empty()) {
3104 clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
3105 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3106 clang::io::Emit32(Out, Overridden[I]);
3107 }
3108 }
3109
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003110 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003111 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003112 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003113 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003114 clang::io::Emit32(Out, ID << 1);
3115 return;
3116 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003117
Douglas Gregor1d583f22009-04-28 21:18:29 +00003118 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003119 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3120 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3121 clang::io::Emit16(Out, Bits);
3122 Bits = 0;
3123 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003124 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003125 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003126 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3127 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003128 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003129 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003130 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003131
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003132 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003133 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3134 if (IsModule) {
3135 // Write the IDs of macros coming from different submodules.
3136 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003137 llvm::SmallVector<SubmoduleID, 4> Overridden;
3138 for (MacroDirective *
3139 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3140 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3141 MacroID InfoID = 0;
3142 emitMacroOverrides(Out, Overridden);
3143 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3144 InfoID = Writer.getMacroID(DefMD->getInfo());
3145 assert(InfoID);
3146 clang::io::Emit32(Out, InfoID << 1);
3147 } else {
3148 assert(isa<UndefMacroDirective>(MD));
3149 clang::io::Emit32(Out, (ModID << 1) | 1);
3150 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003151 }
Richard Smith49f906a2014-03-01 00:08:04 +00003152 emitMacroOverrides(Out, Overridden);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003153 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003154 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003155 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003156
Douglas Gregora868bbd2009-04-21 22:25:48 +00003157 // Emit the declaration IDs in reverse order, because the
3158 // IdentifierResolver provides the declarations as they would be
3159 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003160 // "stat"), but the ASTReader adds declarations to the end of the list
3161 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003162 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003163 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3164 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003165 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003166 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003167 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003168 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3169 }
3170
3171 /// \brief Returns the most recent local decl or the given decl if there are
3172 /// no local ones. The given decl is assumed to be the most recent one.
3173 Decl *getMostRecentLocalDecl(Decl *Orig) {
3174 // The only way a "from AST file" decl would be more recent from a local one
3175 // is if it came from a module.
3176 if (!PP.getLangOpts().Modules)
3177 return Orig;
3178
3179 // Look for a local in the decl chain.
3180 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3181 if (!D->isFromASTFile())
3182 return D;
3183 // If we come up a decl from a (chained-)PCH stop since we won't find a
3184 // local one.
3185 if (D->getOwningModuleID() == 0)
3186 break;
3187 }
3188
3189 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003190 }
3191};
3192} // end anonymous namespace
3193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003194/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003195///
3196/// The identifier table consists of a blob containing string data
3197/// (the actual identifiers themselves) and a separate "offsets" index
3198/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003199void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3200 IdentifierResolver &IdResolver,
3201 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003202 using namespace llvm;
3203
3204 // Create and write out the blob that contains the identifier
3205 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003206 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003207 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003208 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003209
Douglas Gregore6648fb2009-04-28 20:33:11 +00003210 // Look for any identifiers that were named while processing the
3211 // headers, but are otherwise not needed. We add these to the hash
3212 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003213 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003214 // file.
3215 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3216 IDEnd = PP.getIdentifierTable().end();
3217 ID != IDEnd; ++ID)
3218 getIdentifierRef(ID->second);
3219
Sebastian Redlff4a2952010-07-23 23:49:55 +00003220 // Create the on-disk hash table representation. We only store offsets
3221 // for identifiers that appear here for the first time.
3222 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003223 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003224 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3225 ID != IDEnd; ++ID) {
3226 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003227 if (!Chain || !ID->first->isFromAST() ||
3228 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003229 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003230 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003231 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003232
Douglas Gregore84a9da2009-04-20 20:36:09 +00003233 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003234 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003235 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003236 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003237 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003238 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003239 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003240 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003241 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003242 }
3243
3244 // Create a blob abbreviation
3245 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003246 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003249 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003250
3251 // Write the identifier table
3252 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003253 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003254 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003255 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003256 }
3257
3258 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003259 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003260 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3264 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3265
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003266#ifndef NDEBUG
3267 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3268 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3269#endif
3270
Douglas Gregor0e149972009-04-25 19:10:14 +00003271 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003272 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003273 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003274 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003275 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003276 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003277}
3278
Douglas Gregorc5046832009-04-27 18:38:38 +00003279//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003280// DeclContext's Name Lookup Table Serialization
3281//===----------------------------------------------------------------------===//
3282
3283namespace {
3284// Trait used for the on-disk hash table used in the method pool.
3285class ASTDeclContextNameLookupTrait {
3286 ASTWriter &Writer;
3287
3288public:
3289 typedef DeclarationName key_type;
3290 typedef key_type key_type_ref;
3291
3292 typedef DeclContext::lookup_result data_type;
3293 typedef const data_type& data_type_ref;
3294
3295 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3296
3297 unsigned ComputeHash(DeclarationName Name) {
3298 llvm::FoldingSetNodeID ID;
3299 ID.AddInteger(Name.getNameKind());
3300
3301 switch (Name.getNameKind()) {
3302 case DeclarationName::Identifier:
3303 ID.AddString(Name.getAsIdentifierInfo()->getName());
3304 break;
3305 case DeclarationName::ObjCZeroArgSelector:
3306 case DeclarationName::ObjCOneArgSelector:
3307 case DeclarationName::ObjCMultiArgSelector:
3308 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3309 break;
3310 case DeclarationName::CXXConstructorName:
3311 case DeclarationName::CXXDestructorName:
3312 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003313 break;
3314 case DeclarationName::CXXOperatorName:
3315 ID.AddInteger(Name.getCXXOverloadedOperator());
3316 break;
3317 case DeclarationName::CXXLiteralOperatorName:
3318 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3319 case DeclarationName::CXXUsingDirective:
3320 break;
3321 }
3322
3323 return ID.ComputeHash();
3324 }
3325
3326 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003327 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003328 data_type_ref Lookup) {
3329 unsigned KeyLen = 1;
3330 switch (Name.getNameKind()) {
3331 case DeclarationName::Identifier:
3332 case DeclarationName::ObjCZeroArgSelector:
3333 case DeclarationName::ObjCOneArgSelector:
3334 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003335 case DeclarationName::CXXLiteralOperatorName:
3336 KeyLen += 4;
3337 break;
3338 case DeclarationName::CXXOperatorName:
3339 KeyLen += 1;
3340 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003341 case DeclarationName::CXXConstructorName:
3342 case DeclarationName::CXXDestructorName:
3343 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003344 case DeclarationName::CXXUsingDirective:
3345 break;
3346 }
3347 clang::io::Emit16(Out, KeyLen);
3348
3349 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003350 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003351 clang::io::Emit16(Out, DataLen);
3352
3353 return std::make_pair(KeyLen, DataLen);
3354 }
3355
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003356 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003357 using namespace clang::io;
3358
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003359 Emit8(Out, Name.getNameKind());
3360 switch (Name.getNameKind()) {
3361 case DeclarationName::Identifier:
3362 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003363 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003364 case DeclarationName::ObjCZeroArgSelector:
3365 case DeclarationName::ObjCOneArgSelector:
3366 case DeclarationName::ObjCMultiArgSelector:
3367 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003368 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003369 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003370 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3371 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003372 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003373 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003374 case DeclarationName::CXXLiteralOperatorName:
3375 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003376 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003377 case DeclarationName::CXXConstructorName:
3378 case DeclarationName::CXXDestructorName:
3379 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003380 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003381 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003382 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003383
3384 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003385 }
3386
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003387 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003388 data_type Lookup, unsigned DataLen) {
3389 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003390 clang::io::Emit16(Out, Lookup.size());
3391 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3392 I != E; ++I)
3393 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003394
3395 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3396 }
3397};
3398} // end anonymous namespace
3399
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003400/// \brief Write the block containing all of the declaration IDs
3401/// visible from the given DeclContext.
3402///
3403/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003404/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003405uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3406 DeclContext *DC) {
3407 if (DC->getPrimaryContext() != DC)
3408 return 0;
3409
3410 // Since there is no name lookup into functions or methods, don't bother to
3411 // build a visible-declarations table for these entities.
3412 if (DC->isFunctionOrMethod())
3413 return 0;
3414
3415 // If not in C++, we perform name lookup for the translation unit via the
3416 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003417 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003418 return 0;
3419
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003420 // Serialize the contents of the mapping used for lookup. Note that,
3421 // although we have two very different code paths, the serialized
3422 // representation is the same for both cases: a declaration name,
3423 // followed by a size, followed by references to the visible
3424 // declarations that have that name.
3425 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003426 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003427 if (!Map || Map->empty())
3428 return 0;
3429
3430 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3431 ASTDeclContextNameLookupTrait Trait(*this);
3432
3433 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003434 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003435 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003436 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3437 D != DEnd; ++D) {
3438 DeclarationName Name = D->first;
3439 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003440 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003441 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3442 // Hash all conversion function names to the same name. The actual
3443 // type information in conversion function name is not used in the
3444 // key (since such type information is not stable across different
3445 // modules), so the intended effect is to coalesce all of the conversion
3446 // functions under a single key.
3447 if (!ConversionName)
3448 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003449 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003450 continue;
3451 }
3452
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003453 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003454 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003455 }
3456
Douglas Gregor05ef9312011-08-30 20:49:19 +00003457 // Add the conversion functions
3458 if (!ConversionDecls.empty()) {
3459 Generator.insert(ConversionName,
3460 DeclContext::lookup_result(ConversionDecls.begin(),
3461 ConversionDecls.end()),
3462 Trait);
3463 }
3464
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003465 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003466 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003467 uint32_t BucketOffset;
3468 {
3469 llvm::raw_svector_ostream Out(LookupTable);
3470 // Make sure that no bucket is at offset 0
3471 clang::io::Emit32(Out, 0);
3472 BucketOffset = Generator.Emit(Out, Trait);
3473 }
3474
3475 // Write the lookup table
3476 RecordData Record;
3477 Record.push_back(DECL_CONTEXT_VISIBLE);
3478 Record.push_back(BucketOffset);
3479 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3480 LookupTable.str());
3481
3482 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3483 ++NumVisibleDeclContexts;
3484 return Offset;
3485}
3486
Sebastian Redla4071b42010-08-24 00:50:09 +00003487/// \brief Write an UPDATE_VISIBLE block for the given context.
3488///
3489/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3490/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003491/// (in C++), for namespaces, and for classes with forward-declared unscoped
3492/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003493void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003494 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3495 if (!Map || Map->empty())
3496 return;
3497
3498 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3499 ASTDeclContextNameLookupTrait Trait(*this);
3500
3501 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003502 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3503 D != DEnd; ++D) {
3504 DeclarationName Name = D->first;
3505 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003506 // For any name that appears in this table, the results are complete, i.e.
3507 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003508 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003509 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003510 }
3511
3512 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003513 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003514 uint32_t BucketOffset;
3515 {
3516 llvm::raw_svector_ostream Out(LookupTable);
3517 // Make sure that no bucket is at offset 0
3518 clang::io::Emit32(Out, 0);
3519 BucketOffset = Generator.Emit(Out, Trait);
3520 }
3521
3522 // Write the lookup table
3523 RecordData Record;
3524 Record.push_back(UPDATE_VISIBLE);
3525 Record.push_back(getDeclID(cast<Decl>(DC)));
3526 Record.push_back(BucketOffset);
3527 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3528}
3529
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003530/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3531void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3532 RecordData Record;
3533 Record.push_back(Opts.fp_contract);
3534 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3535}
3536
3537/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3538void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003539 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003540 return;
3541
3542 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3543 RecordData Record;
3544#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3545#include "clang/Basic/OpenCLExtensions.def"
3546 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3547}
3548
Douglas Gregor358cd442012-01-15 16:58:34 +00003549void ASTWriter::WriteRedeclarations() {
3550 RecordData LocalRedeclChains;
3551 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3552
3553 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3554 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003555 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003556
3557 Decl *MostRecent = First->getMostRecentDecl();
3558
3559 // If we only have a single declaration, there is no point in storing
3560 // a redeclaration chain.
3561 if (First == MostRecent)
3562 continue;
3563
3564 unsigned Offset = LocalRedeclChains.size();
3565 unsigned Size = 0;
3566 LocalRedeclChains.push_back(0); // Placeholder for the size.
3567
3568 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003569 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003570 Prev = Prev->getPreviousDecl()) {
3571 if (!Prev->isFromASTFile()) {
3572 AddDeclRef(Prev, LocalRedeclChains);
3573 ++Size;
3574 }
3575 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003576
3577 if (!First->isFromASTFile() && Chain) {
3578 Decl *FirstFromAST = MostRecent;
3579 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3580 if (Prev->isFromASTFile())
3581 FirstFromAST = Prev;
3582 }
3583
3584 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3585 }
3586
Douglas Gregor358cd442012-01-15 16:58:34 +00003587 LocalRedeclChains[Offset] = Size;
3588
3589 // Reverse the set of local redeclarations, so that we store them in
3590 // order (since we found them in reverse order).
3591 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3592
Douglas Gregor6168bd22013-02-18 15:53:43 +00003593 // Add the mapping from the first ID from the AST to the set of local
3594 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003595 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3596 LocalRedeclsMap.push_back(Info);
3597
3598 assert(N == Redeclarations.size() &&
3599 "Deserialized a declaration we shouldn't have");
3600 }
3601
3602 if (LocalRedeclChains.empty())
3603 return;
3604
3605 // Sort the local redeclarations map by the first declaration ID,
3606 // since the reader will be performing binary searches on this information.
3607 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3608
3609 // Emit the local redeclarations map.
3610 using namespace llvm;
3611 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3612 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3615 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3616
3617 RecordData Record;
3618 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3619 Record.push_back(LocalRedeclsMap.size());
3620 Stream.EmitRecordWithBlob(AbbrevID, Record,
3621 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3622 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3623
3624 // Emit the redeclaration chains.
3625 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3626}
3627
Douglas Gregor404cdde2012-01-27 01:47:08 +00003628void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003629 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003630 RecordData Categories;
3631
3632 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3633 unsigned Size = 0;
3634 unsigned StartIndex = Categories.size();
3635
3636 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3637
3638 // Allocate space for the size.
3639 Categories.push_back(0);
3640
3641 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003642 for (ObjCInterfaceDecl::known_categories_iterator
3643 Cat = Class->known_categories_begin(),
3644 CatEnd = Class->known_categories_end();
3645 Cat != CatEnd; ++Cat, ++Size) {
3646 assert(getDeclID(*Cat) != 0 && "Bogus category");
3647 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003648 }
3649
3650 // Update the size.
3651 Categories[StartIndex] = Size;
3652
3653 // Record this interface -> category map.
3654 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3655 CategoriesMap.push_back(CatInfo);
3656 }
3657
3658 // Sort the categories map by the definition ID, since the reader will be
3659 // performing binary searches on this information.
3660 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3661
3662 // Emit the categories map.
3663 using namespace llvm;
3664 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3665 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3668 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3669
3670 RecordData Record;
3671 Record.push_back(OBJC_CATEGORIES_MAP);
3672 Record.push_back(CategoriesMap.size());
3673 Stream.EmitRecordWithBlob(AbbrevID, Record,
3674 reinterpret_cast<char*>(CategoriesMap.data()),
3675 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3676
3677 // Emit the category lists.
3678 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3679}
3680
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003681void ASTWriter::WriteMergedDecls() {
3682 if (!Chain || Chain->MergedDecls.empty())
3683 return;
3684
3685 RecordData Record;
3686 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3687 IEnd = Chain->MergedDecls.end();
3688 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003689 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003690 : getDeclID(I->first);
3691 assert(CanonID && "Merged declaration not known?");
3692
3693 Record.push_back(CanonID);
3694 Record.push_back(I->second.size());
3695 Record.append(I->second.begin(), I->second.end());
3696 }
3697 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3698}
3699
Richard Smithe40f2ba2013-08-07 21:41:30 +00003700void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3701 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3702
3703 if (LPTMap.empty())
3704 return;
3705
3706 RecordData Record;
3707 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3708 ItEnd = LPTMap.end();
3709 It != ItEnd; ++It) {
3710 LateParsedTemplate *LPT = It->second;
3711 AddDeclRef(It->first, Record);
3712 AddDeclRef(LPT->D, Record);
3713 Record.push_back(LPT->Toks.size());
3714
3715 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3716 TokEnd = LPT->Toks.end();
3717 TokIt != TokEnd; ++TokIt) {
3718 AddToken(*TokIt, Record);
3719 }
3720 }
3721 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3722}
3723
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003724//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003725// General Serialization Routines
3726//===----------------------------------------------------------------------===//
3727
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003728/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003729void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3730 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003731 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003732 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3733 e = Attrs.end(); i != e; ++i){
3734 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003735 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003736 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003737
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003738#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003739
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003740 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003741}
3742
John McCallf413f5e2013-05-03 00:10:13 +00003743void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3744 AddSourceLocation(Tok.getLocation(), Record);
3745 Record.push_back(Tok.getLength());
3746
3747 // FIXME: When reading literal tokens, reconstruct the literal pointer
3748 // if it is needed.
3749 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3750 // FIXME: Should translate token kind to a stable encoding.
3751 Record.push_back(Tok.getKind());
3752 // FIXME: Should translate token flags to a stable encoding.
3753 Record.push_back(Tok.getFlags());
3754}
3755
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003756void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003757 Record.push_back(Str.size());
3758 Record.insert(Record.end(), Str.begin(), Str.end());
3759}
3760
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003761void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3762 RecordDataImpl &Record) {
3763 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003764 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003765 Record.push_back(*Minor + 1);
3766 else
3767 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003768 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003769 Record.push_back(*Subminor + 1);
3770 else
3771 Record.push_back(0);
3772}
3773
Douglas Gregore84a9da2009-04-20 20:36:09 +00003774/// \brief Note that the identifier II occurs at the given offset
3775/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003776void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003777 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003778 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003779 // up earlier in the chain and thus don't need an offset.
3780 if (ID >= FirstIdentID)
3781 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003782}
3783
Douglas Gregor95c13f52009-04-25 17:48:32 +00003784/// \brief Note that the selector Sel occurs at the given offset
3785/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003786void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003787 unsigned ID = SelectorIDs[Sel];
3788 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003789 // Don't record offsets for selectors that are also available in a different
3790 // file.
3791 if (ID < FirstSelectorID)
3792 return;
3793 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003794}
3795
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003796ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003797 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003798 WritingAST(false), DoneWritingDeclsAndTypes(false),
3799 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003800 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003801 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003802 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3803 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003804 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3805 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003806 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003807 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003808 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003809 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003810 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003811 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003812 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3813 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3814 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003815 DeclTypedefAbbrev(0),
3816 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3817 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003818{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003819}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003820
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003821ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003822 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003823}
3824
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003825void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003826 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003827 Module *WritingModule, StringRef isysroot,
3828 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003829 WritingAST = true;
3830
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003831 ASTHasCompilerErrors = hasErrors;
3832
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003833 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003834 Stream.Emit((unsigned)'C', 8);
3835 Stream.Emit((unsigned)'P', 8);
3836 Stream.Emit((unsigned)'C', 8);
3837 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003838
Chris Lattner28fa4e62009-04-26 22:26:21 +00003839 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003840
Douglas Gregoreda8e122011-08-09 15:13:55 +00003841 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003842 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003843 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003844 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003845 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003846 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003847 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003848
3849 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003850}
3851
Douglas Gregora94a1542011-07-27 21:45:57 +00003852template<typename Vector>
3853static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3854 ASTWriter::RecordData &Record) {
3855 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3856 I != E; ++I) {
3857 Writer.AddDeclRef(*I, Record);
3858 }
3859}
3860
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003861void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003862 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003863 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003864 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003865 using namespace llvm;
3866
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003867 bool isModule = WritingModule != 0;
3868
Douglas Gregorcf68c582011-12-01 22:20:10 +00003869 // Make sure that the AST reader knows to finalize itself.
3870 if (Chain)
3871 Chain->finalizeForWriting();
3872
Sebastian Redl143413f2010-07-12 22:02:52 +00003873 ASTContext &Context = SemaRef.Context;
3874 Preprocessor &PP = SemaRef.PP;
3875
Douglas Gregordab42432011-08-12 00:15:20 +00003876 // Set up predefined declaration IDs.
3877 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003878 if (Context.ObjCIdDecl)
3879 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003880 if (Context.ObjCSelDecl)
3881 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003882 if (Context.ObjCClassDecl)
3883 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003884 if (Context.ObjCProtocolClassDecl)
3885 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003886 if (Context.Int128Decl)
3887 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3888 if (Context.UInt128Decl)
3889 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003890 if (Context.ObjCInstanceTypeDecl)
3891 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003892 if (Context.BuiltinVaListDecl)
3893 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3894
Douglas Gregor851443c2011-08-12 01:39:19 +00003895 if (!Chain) {
3896 // Make sure that we emit IdentifierInfos (and any attached
3897 // declarations) for builtins. We don't need to do this when we're
3898 // emitting chained PCH files, because all of the builtins will be
3899 // in the original PCH file.
3900 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003901 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003902 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003903 if (!Context.getLangOpts().NoBuiltin) {
3904 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3905 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003906 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3907 getIdentifierRef(&Table.get(BuiltinNames[I]));
3908 }
3909
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003910 // If there are any out-of-date identifiers, bring them up to date.
3911 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003912 // Find out-of-date identifiers.
3913 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003914 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3915 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003916 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003917 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003918 OutOfDate.push_back(ID->second);
3919 }
3920
3921 // Update the out-of-date identifiers.
3922 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3923 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3924 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003925 }
3926
Chris Lattner0c797362009-09-08 18:19:27 +00003927 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003928 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003929 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003930 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003931 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003932
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003933 // Build a record containing all of the file scoped decls in this file.
3934 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003935 if (!isModule)
3936 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3937 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003938
Douglas Gregor851443c2011-08-12 01:39:19 +00003939 // Build a record containing all of the delegating constructors we still need
3940 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003941 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003942 if (!isModule)
3943 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003944
Douglas Gregor851443c2011-08-12 01:39:19 +00003945 // Write the set of weak, undeclared identifiers. We always write the
3946 // entire table, since later PCH files in a PCH chain are only interested in
3947 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003948 RecordData WeakUndeclaredIdentifiers;
3949 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003950 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003951 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3952 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3953 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3954 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3955 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3956 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3957 }
3958 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003959
Richard Smith78165b52013-01-10 23:43:47 +00003960 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003961 // declarations in this header file. Generally, this record will be
3962 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003963 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003964 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003965 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003966 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003967 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3968 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003969 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003970 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003971 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003972 }
3973
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003974 // Build a record containing all of the ext_vector declarations.
3975 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003976 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003977
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003978 // Build a record containing all of the VTable uses information.
3979 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003980 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003981 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3982 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3983 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3984 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3985 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003986 }
3987
3988 // Build a record containing all of dynamic classes declarations.
3989 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003990 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003991
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003992 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003993 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003994 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003995 I = SemaRef.PendingInstantiations.begin(),
3996 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3997 AddDeclRef(I->first, PendingInstantiations);
3998 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003999 }
4000 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4001 "There are local ones at end of translation unit!");
4002
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004003 // Build a record containing some declaration references.
4004 RecordData SemaDeclRefs;
4005 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4006 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4007 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4008 }
4009
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004010 RecordData CUDASpecialDeclRefs;
4011 if (Context.getcudaConfigureCallDecl()) {
4012 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4013 }
4014
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004015 // Build a record containing all of the known namespaces.
4016 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004017 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004018 I = SemaRef.KnownNamespaces.begin(),
4019 IEnd = SemaRef.KnownNamespaces.end();
4020 I != IEnd; ++I) {
4021 if (!I->second)
4022 AddDeclRef(I->first, KnownNamespaces);
4023 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004024
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004025 // Build a record of all used, undefined objects that require definitions.
4026 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004027
4028 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004029 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004030 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4031 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004032 AddDeclRef(I->first, UndefinedButUsed);
4033 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004034 }
4035
Douglas Gregor112b9072012-10-18 05:31:06 +00004036 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004037 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004038
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004039 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004040 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004041 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004042
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004043 // This is so that older clang versions, before the introduction
4044 // of the control block, can read and reject the newer PCH format.
4045 Record.clear();
4046 Record.push_back(VERSION_MAJOR);
4047 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4048
Douglas Gregor851443c2011-08-12 01:39:19 +00004049 // Create a lexical update block containing all of the declarations in the
4050 // translation unit that do not come from other AST files.
4051 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4052 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4053 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
4054 E = TU->noload_decls_end();
4055 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004056 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00004057 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004058 }
4059
4060 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4061 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4062 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4063 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4064 Record.clear();
4065 Record.push_back(TU_UPDATE_LEXICAL);
4066 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4067 data(NewGlobalDecls));
4068
4069 // And a visible updates block for the translation unit.
4070 Abv = new llvm::BitCodeAbbrev();
4071 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4072 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4073 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4074 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4075 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4076 WriteDeclContextVisibleUpdate(TU);
4077
4078 // If the translation unit has an anonymous namespace, and we don't already
4079 // have an update block for it, write it as an update block.
4080 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4081 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4082 if (Record.empty()) {
4083 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004084 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004085 }
4086 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004087
4088 // Make sure visible decls, added to DeclContexts previously loaded from
4089 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004090 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004091 I = UpdatingVisibleDecls.begin(),
4092 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4093 GetDeclRef(*I);
4094 }
4095
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004096 // Make sure all decls associated with an identifier are registered for
4097 // serialization.
4098 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4099 IDEnd = PP.getIdentifierTable().end();
4100 ID != IDEnd; ++ID) {
4101 const IdentifierInfo *II = ID->second;
4102 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4103 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4104 DEnd = SemaRef.IdResolver.end();
4105 D != DEnd; ++D) {
4106 GetDeclRef(*D);
4107 }
4108 }
4109 }
4110
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00004111 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004112 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004113
Douglas Gregor5204bde2011-08-02 16:26:37 +00004114 // Form the record of special types.
4115 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004116 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004117 AddTypeRef(Context.getFILEType(), SpecialTypes);
4118 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4119 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4120 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4121 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004122 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004123 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004124
Douglas Gregor1970d882009-04-26 03:49:13 +00004125 // Keep writing types and declarations until all types and
4126 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00004127 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004128 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00004129 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4130 E = DeclsToRewrite.end();
4131 I != E; ++I)
4132 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004133 while (!DeclTypesToEmit.empty()) {
4134 DeclOrType DOT = DeclTypesToEmit.front();
4135 DeclTypesToEmit.pop();
4136 if (DOT.isType())
4137 WriteType(DOT.getType());
4138 else
4139 WriteDecl(Context, DOT.getDecl());
4140 }
4141 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004142
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004143 DoneWritingDeclsAndTypes = true;
4144
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004145 WriteFileDeclIDsMap();
4146 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00004147 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004148
4149 if (Chain) {
4150 // Write the mapping information describing our module dependencies and how
4151 // each of those modules were mapped into our own offset/ID space, so that
4152 // the reader can build the appropriate mapping to its own offset/ID space.
4153 // The map consists solely of a blob with the following format:
4154 // *(module-name-len:i16 module-name:len*i8
4155 // source-location-offset:i32
4156 // identifier-id:i32
4157 // preprocessed-entity-id:i32
4158 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004159 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004160 // selector-id:i32
4161 // declaration-id:i32
4162 // c++-base-specifiers-id:i32
4163 // type-id:i32)
4164 //
4165 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4166 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4168 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004169 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004170 {
4171 llvm::raw_svector_ostream Out(Buffer);
4172 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004173 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004174 M != MEnd; ++M) {
4175 StringRef FileName = (*M)->FileName;
4176 io::Emit16(Out, FileName.size());
4177 Out.write(FileName.data(), FileName.size());
4178 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4179 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004180 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004181 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004182 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004183 io::Emit32(Out, (*M)->BaseSelectorID);
4184 io::Emit32(Out, (*M)->BaseDeclID);
4185 io::Emit32(Out, (*M)->BaseTypeIndex);
4186 }
4187 }
4188 Record.clear();
4189 Record.push_back(MODULE_OFFSET_MAP);
4190 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4191 Buffer.data(), Buffer.size());
4192 }
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004193 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004194 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004195 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004196 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004197 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004198 WriteFPPragmaOptions(SemaRef.getFPOptions());
4199 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00004200
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004201 WriteTypeDeclOffsets();
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004202 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004203
Anders Carlsson9bb83e82011-03-06 18:41:18 +00004204 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004205
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004206 // If we're emitting a module, write out the submodule information.
4207 if (WritingModule)
4208 WriteSubmodules(WritingModule);
4209
Douglas Gregor5204bde2011-08-02 16:26:37 +00004210 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4211
Douglas Gregord4df8652009-04-22 22:02:47 +00004212 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004213 if (!EagerlyDeserializedDecls.empty())
4214 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004215
4216 // Write the record containing tentative definitions.
4217 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004218 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004219
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004220 // Write the record containing unused file scoped decls.
4221 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004222 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004223
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004224 // Write the record containing weak undeclared identifiers.
4225 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004226 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004227 WeakUndeclaredIdentifiers);
4228
Richard Smith78165b52013-01-10 23:43:47 +00004229 // Write the record containing locally-scoped extern "C" definitions.
4230 if (!LocallyScopedExternCDecls.empty())
4231 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4232 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004233
4234 // Write the record containing ext_vector type names.
4235 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004236 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004237
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004238 // Write the record containing VTable uses information.
4239 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004240 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004241
4242 // Write the record containing dynamic classes declarations.
4243 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004244 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004245
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004246 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004247 if (!PendingInstantiations.empty())
4248 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004249
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004250 // Write the record containing declaration references of Sema.
4251 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004252 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004253
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004254 // Write the record containing CUDA-specific declaration references.
4255 if (!CUDASpecialDeclRefs.empty())
4256 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004257
4258 // Write the delegating constructors.
4259 if (!DelegatingCtorDecls.empty())
4260 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004261
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004262 // Write the known namespaces.
4263 if (!KnownNamespaces.empty())
4264 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004265
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004266 // Write the undefined internal functions and variables, and inline functions.
4267 if (!UndefinedButUsed.empty())
4268 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004269
Douglas Gregor851443c2011-08-12 01:39:19 +00004270 // Write the visible updates to DeclContexts.
4271 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4272 I = UpdatedDeclContexts.begin(),
4273 E = UpdatedDeclContexts.end();
4274 I != E; ++I)
4275 WriteDeclContextVisibleUpdate(*I);
4276
Douglas Gregor959bb062011-12-03 01:15:29 +00004277 if (!WritingModule) {
4278 // Write the submodules that were imported, if any.
4279 RecordData ImportedModules;
4280 for (ASTContext::import_iterator I = Context.local_import_begin(),
4281 IEnd = Context.local_import_end();
4282 I != IEnd; ++I) {
4283 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4284 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4285 }
4286 if (!ImportedModules.empty()) {
4287 // Sort module IDs.
4288 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4289
4290 // Unique module IDs.
4291 ImportedModules.erase(std::unique(ImportedModules.begin(),
4292 ImportedModules.end()),
4293 ImportedModules.end());
4294
4295 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4296 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004297 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004298
Douglas Gregordab42432011-08-12 00:15:20 +00004299 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00004300 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004301 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004302 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004303 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004304 WriteLateParsedTemplates(SemaRef);
4305
Douglas Gregor08f01292009-04-17 22:13:46 +00004306 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004307 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004308 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004309 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004310 Record.push_back(NumLexicalDeclContexts);
4311 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004312 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004313 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004314}
4315
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004316/// \brief Go through the declaration update blocks and resolve declaration
4317/// pointers into declaration IDs.
4318void ASTWriter::ResolveDeclUpdatesBlocks() {
4319 for (DeclUpdateMap::iterator
4320 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4321 const Decl *D = I->first;
4322 UpdateRecord &URec = I->second;
4323
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004324 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004325 continue; // The decl will be written completely
4326
4327 unsigned Idx = 0, N = URec.size();
4328 while (Idx < N) {
4329 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004330 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4331 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4332 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4333 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4334 ++Idx;
4335 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004336
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004337 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman276dd182013-09-05 00:02:25 +00004338 case UPD_DECL_MARKED_USED:
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004339 ++Idx;
4340 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004341
4342 case UPD_CXX_DEDUCED_RETURN_TYPE:
4343 URec[Idx] = GetOrCreateTypeID(
4344 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4345 ++Idx;
4346 break;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004347 }
4348 }
4349 }
4350}
4351
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004352void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004353 if (DeclUpdates.empty())
4354 return;
4355
4356 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00004357 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004358 for (DeclUpdateMap::iterator
4359 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4360 const Decl *D = I->first;
4361 UpdateRecord &URec = I->second;
4362
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004363 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004364 continue; // The decl will be written completely,no need to store updates.
4365
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004366 uint64_t Offset = Stream.GetCurrentBitNo();
4367 Stream.EmitRecord(DECL_UPDATES, URec);
4368
4369 OffsetsRecord.push_back(GetDeclRef(D));
4370 OffsetsRecord.push_back(Offset);
4371 }
4372 Stream.ExitBlock();
4373 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4374}
4375
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004376void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004377 if (ReplacedDecls.empty())
4378 return;
4379
4380 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004381 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4382 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004383 Record.push_back(I->ID);
4384 Record.push_back(I->Offset);
4385 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004386 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004387 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004388}
4389
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004390void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004391 Record.push_back(Loc.getRawEncoding());
4392}
4393
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004394void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004395 AddSourceLocation(Range.getBegin(), Record);
4396 AddSourceLocation(Range.getEnd(), Record);
4397}
4398
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004399void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004400 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004401 const uint64_t *Words = Value.getRawData();
4402 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004403}
4404
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004405void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004406 Record.push_back(Value.isUnsigned());
4407 AddAPInt(Value, Record);
4408}
4409
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004410void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004411 AddAPInt(Value.bitcastToAPInt(), Record);
4412}
4413
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004414void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004415 Record.push_back(getIdentifierRef(II));
4416}
4417
Sebastian Redl539c5062010-08-18 23:57:32 +00004418IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004419 if (II == 0)
4420 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004421
Sebastian Redl539c5062010-08-18 23:57:32 +00004422 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004423 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004424 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004425 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004426}
4427
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004428MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004429 // Don't emit builtin macros like __LINE__ to the AST file unless they
4430 // have been redefined by the header (in which case they are not
4431 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004432 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004433 return 0;
4434
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004435 MacroID &ID = MacroIDs[MI];
4436 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004437 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004438 MacroInfoToEmitData Info = { Name, MI, ID };
4439 MacroInfosToEmit.push_back(Info);
4440 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004441 return ID;
4442}
4443
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004444MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4445 if (MI == 0 || MI->isBuiltinMacro())
4446 return 0;
4447
4448 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4449 return MacroIDs[MI];
4450}
4451
4452uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4453 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4454 return IdentMacroDirectivesOffsetMap[Name];
4455}
4456
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004457void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004458 Record.push_back(getSelectorRef(SelRef));
4459}
4460
Sebastian Redl539c5062010-08-18 23:57:32 +00004461SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004462 if (Sel.getAsOpaquePtr() == 0) {
4463 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004464 }
4465
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004466 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004467 if (SID == 0 && Chain) {
4468 // This might trigger a ReadSelector callback, which will set the ID for
4469 // this selector.
4470 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004471 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004472 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004473 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004474 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004475 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004476 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004477 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004478}
4479
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004480void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004481 AddDeclRef(Temp->getDestructor(), Record);
4482}
4483
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004484void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4485 CXXBaseSpecifier const *BasesEnd,
4486 RecordDataImpl &Record) {
4487 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4488 CXXBaseSpecifiersToWrite.push_back(
4489 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4490 Bases, BasesEnd));
4491 Record.push_back(NextCXXBaseSpecifiersID++);
4492}
4493
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004494void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004495 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004496 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004497 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004498 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004499 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004500 break;
4501 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004502 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004503 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004504 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004505 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004506 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004507 break;
4508 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004509 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004510 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004511 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004512 break;
John McCall0ad16662009-10-29 08:12:44 +00004513 case TemplateArgument::Null:
4514 case TemplateArgument::Integral:
4515 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004516 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004517 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004518 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004519 break;
4520 }
4521}
4522
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004523void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004524 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004525 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004526
4527 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4528 bool InfoHasSameExpr
4529 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4530 Record.push_back(InfoHasSameExpr);
4531 if (InfoHasSameExpr)
4532 return; // Avoid storing the same expr twice.
4533 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004534 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4535 Record);
4536}
4537
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004538void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4539 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004540 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004541 AddTypeRef(QualType(), Record);
4542 return;
4543 }
4544
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004545 AddTypeLoc(TInfo->getTypeLoc(), Record);
4546}
4547
4548void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4549 AddTypeRef(TL.getType(), Record);
4550
John McCall8f115c62009-10-16 21:56:05 +00004551 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004552 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004553 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004554}
4555
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004556void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004557 Record.push_back(GetOrCreateTypeID(T));
4558}
4559
Douglas Gregoreda8e122011-08-09 15:13:55 +00004560TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004561 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004562 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004563 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4564}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004565
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004566TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004567 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004568 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004569 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004570}
4571
4572TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4573 if (T.isNull())
4574 return TypeIdx();
4575 assert(!T.getLocalFastQualifiers());
4576
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004577 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004578 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004579 if (DoneWritingDeclsAndTypes) {
4580 assert(0 && "New type seen after serializing all the types to emit!");
4581 return TypeIdx();
4582 }
4583
Douglas Gregor1970d882009-04-26 03:49:13 +00004584 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004585 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004586 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004587 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004588 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004589 return Idx;
4590}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004591
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004592TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004593 if (T.isNull())
4594 return TypeIdx();
4595 assert(!T.getLocalFastQualifiers());
4596
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004597 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4598 assert(I != TypeIdxs.end() && "Type not emitted!");
4599 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004600}
4601
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004602void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004603 Record.push_back(GetDeclRef(D));
4604}
4605
Sebastian Redl539c5062010-08-18 23:57:32 +00004606DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004607 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4608
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004609 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004610 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004611 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004612
4613 // If D comes from an AST file, its declaration ID is already known and
4614 // fixed.
4615 if (D->isFromASTFile())
4616 return D->getGlobalID();
4617
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004618 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004619 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004620 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004621 if (DoneWritingDeclsAndTypes) {
4622 assert(0 && "New decl seen after serializing all the decls to emit!");
4623 return 0;
4624 }
4625
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004626 // We haven't seen this declaration before. Give it a new ID and
4627 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004628 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004629 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004630 }
4631
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004632 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004633}
4634
Sebastian Redl539c5062010-08-18 23:57:32 +00004635DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004636 if (D == 0)
4637 return 0;
4638
Douglas Gregorb3163e52012-01-05 22:33:30 +00004639 // If D comes from an AST file, its declaration ID is already known and
4640 // fixed.
4641 if (D->isFromASTFile())
4642 return D->getGlobalID();
4643
Douglas Gregore84a9da2009-04-20 20:36:09 +00004644 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4645 return DeclIDs[D];
4646}
4647
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004648void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004649 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004650 assert(D);
4651
4652 SourceLocation Loc = D->getLocation();
4653 if (Loc.isInvalid())
4654 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004655
4656 // We only keep track of the file-level declarations of each file.
4657 if (!D->getLexicalDeclContext()->isFileContext())
4658 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004659 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4660 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004661 if (isa<ParmVarDecl>(D))
4662 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004663
4664 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004665 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004666 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004667 FileID FID;
4668 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004669 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004670 if (FID.isInvalid())
4671 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004672 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004673
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004674 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004675 if (!Info)
4676 Info = new DeclIDInFileInfo();
4677
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004678 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004679 LocDeclIDsTy &Decls = Info->DeclIDs;
4680
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004681 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004682 Decls.push_back(LocDecl);
4683 return;
4684 }
4685
Benjamin Kramer45025c02013-08-24 13:22:59 +00004686 LocDeclIDsTy::iterator I =
4687 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004688
4689 Decls.insert(I, LocDecl);
4690}
4691
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004692void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004693 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004694 Record.push_back(Name.getNameKind());
4695 switch (Name.getNameKind()) {
4696 case DeclarationName::Identifier:
4697 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4698 break;
4699
4700 case DeclarationName::ObjCZeroArgSelector:
4701 case DeclarationName::ObjCOneArgSelector:
4702 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004703 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004704 break;
4705
4706 case DeclarationName::CXXConstructorName:
4707 case DeclarationName::CXXDestructorName:
4708 case DeclarationName::CXXConversionFunctionName:
4709 AddTypeRef(Name.getCXXNameType(), Record);
4710 break;
4711
4712 case DeclarationName::CXXOperatorName:
4713 Record.push_back(Name.getCXXOverloadedOperator());
4714 break;
4715
Alexis Hunt3d221f22009-11-29 07:34:05 +00004716 case DeclarationName::CXXLiteralOperatorName:
4717 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4718 break;
4719
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004720 case DeclarationName::CXXUsingDirective:
4721 // No extra data to emit
4722 break;
4723 }
4724}
Chris Lattnerca025db2010-05-07 21:43:38 +00004725
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004726void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004727 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004728 switch (Name.getNameKind()) {
4729 case DeclarationName::CXXConstructorName:
4730 case DeclarationName::CXXDestructorName:
4731 case DeclarationName::CXXConversionFunctionName:
4732 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4733 break;
4734
4735 case DeclarationName::CXXOperatorName:
4736 AddSourceLocation(
4737 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4738 Record);
4739 AddSourceLocation(
4740 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4741 Record);
4742 break;
4743
4744 case DeclarationName::CXXLiteralOperatorName:
4745 AddSourceLocation(
4746 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4747 Record);
4748 break;
4749
4750 case DeclarationName::Identifier:
4751 case DeclarationName::ObjCZeroArgSelector:
4752 case DeclarationName::ObjCOneArgSelector:
4753 case DeclarationName::ObjCMultiArgSelector:
4754 case DeclarationName::CXXUsingDirective:
4755 break;
4756 }
4757}
4758
4759void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004760 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004761 AddDeclarationName(NameInfo.getName(), Record);
4762 AddSourceLocation(NameInfo.getLoc(), Record);
4763 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4764}
4765
4766void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004767 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004768 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004769 Record.push_back(Info.NumTemplParamLists);
4770 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4771 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4772}
4773
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004774void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004775 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004776 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004777 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004778 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004779
4780 // Push each of the NNS's onto a stack for serialization in reverse order.
4781 while (NNS) {
4782 NestedNames.push_back(NNS);
4783 NNS = NNS->getPrefix();
4784 }
4785
4786 Record.push_back(NestedNames.size());
4787 while(!NestedNames.empty()) {
4788 NNS = NestedNames.pop_back_val();
4789 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4790 Record.push_back(Kind);
4791 switch (Kind) {
4792 case NestedNameSpecifier::Identifier:
4793 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4794 break;
4795
4796 case NestedNameSpecifier::Namespace:
4797 AddDeclRef(NNS->getAsNamespace(), Record);
4798 break;
4799
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004800 case NestedNameSpecifier::NamespaceAlias:
4801 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4802 break;
4803
Chris Lattnerca025db2010-05-07 21:43:38 +00004804 case NestedNameSpecifier::TypeSpec:
4805 case NestedNameSpecifier::TypeSpecWithTemplate:
4806 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4807 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4808 break;
4809
4810 case NestedNameSpecifier::Global:
4811 // Don't need to write an associated value.
4812 break;
4813 }
4814 }
4815}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004816
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004817void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4818 RecordDataImpl &Record) {
4819 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004820 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004821 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004822
4823 // Push each of the nested-name-specifiers's onto a stack for
4824 // serialization in reverse order.
4825 while (NNS) {
4826 NestedNames.push_back(NNS);
4827 NNS = NNS.getPrefix();
4828 }
4829
4830 Record.push_back(NestedNames.size());
4831 while(!NestedNames.empty()) {
4832 NNS = NestedNames.pop_back_val();
4833 NestedNameSpecifier::SpecifierKind Kind
4834 = NNS.getNestedNameSpecifier()->getKind();
4835 Record.push_back(Kind);
4836 switch (Kind) {
4837 case NestedNameSpecifier::Identifier:
4838 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4839 AddSourceRange(NNS.getLocalSourceRange(), Record);
4840 break;
4841
4842 case NestedNameSpecifier::Namespace:
4843 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4844 AddSourceRange(NNS.getLocalSourceRange(), Record);
4845 break;
4846
4847 case NestedNameSpecifier::NamespaceAlias:
4848 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4849 AddSourceRange(NNS.getLocalSourceRange(), Record);
4850 break;
4851
4852 case NestedNameSpecifier::TypeSpec:
4853 case NestedNameSpecifier::TypeSpecWithTemplate:
4854 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4855 AddTypeLoc(NNS.getTypeLoc(), Record);
4856 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4857 break;
4858
4859 case NestedNameSpecifier::Global:
4860 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4861 break;
4862 }
4863 }
4864}
4865
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004866void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004867 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004868 Record.push_back(Kind);
4869 switch (Kind) {
4870 case TemplateName::Template:
4871 AddDeclRef(Name.getAsTemplateDecl(), Record);
4872 break;
4873
4874 case TemplateName::OverloadedTemplate: {
4875 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4876 Record.push_back(OvT->size());
4877 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4878 I != E; ++I)
4879 AddDeclRef(*I, Record);
4880 break;
4881 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004882
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004883 case TemplateName::QualifiedTemplate: {
4884 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4885 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4886 Record.push_back(QualT->hasTemplateKeyword());
4887 AddDeclRef(QualT->getTemplateDecl(), Record);
4888 break;
4889 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004890
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004891 case TemplateName::DependentTemplate: {
4892 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4893 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4894 Record.push_back(DepT->isIdentifier());
4895 if (DepT->isIdentifier())
4896 AddIdentifierRef(DepT->getIdentifier(), Record);
4897 else
4898 Record.push_back(DepT->getOperator());
4899 break;
4900 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004901
4902 case TemplateName::SubstTemplateTemplateParm: {
4903 SubstTemplateTemplateParmStorage *subst
4904 = Name.getAsSubstTemplateTemplateParm();
4905 AddDeclRef(subst->getParameter(), Record);
4906 AddTemplateName(subst->getReplacement(), Record);
4907 break;
4908 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004909
4910 case TemplateName::SubstTemplateTemplateParmPack: {
4911 SubstTemplateTemplateParmPackStorage *SubstPack
4912 = Name.getAsSubstTemplateTemplateParmPack();
4913 AddDeclRef(SubstPack->getParameterPack(), Record);
4914 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4915 break;
4916 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004917 }
4918}
4919
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004920void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004921 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004922 Record.push_back(Arg.getKind());
4923 switch (Arg.getKind()) {
4924 case TemplateArgument::Null:
4925 break;
4926 case TemplateArgument::Type:
4927 AddTypeRef(Arg.getAsType(), Record);
4928 break;
4929 case TemplateArgument::Declaration:
4930 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004931 Record.push_back(Arg.isDeclForReferenceParam());
4932 break;
4933 case TemplateArgument::NullPtr:
4934 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004935 break;
4936 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004937 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004938 AddTypeRef(Arg.getIntegralType(), Record);
4939 break;
4940 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004941 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4942 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004943 case TemplateArgument::TemplateExpansion:
4944 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004945 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004946 Record.push_back(*NumExpansions + 1);
4947 else
4948 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004949 break;
4950 case TemplateArgument::Expression:
4951 AddStmt(Arg.getAsExpr());
4952 break;
4953 case TemplateArgument::Pack:
4954 Record.push_back(Arg.pack_size());
4955 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4956 I != E; ++I)
4957 AddTemplateArgument(*I, Record);
4958 break;
4959 }
4960}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004961
4962void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004963ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004964 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004965 assert(TemplateParams && "No TemplateParams!");
4966 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4967 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4968 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4969 Record.push_back(TemplateParams->size());
4970 for (TemplateParameterList::const_iterator
4971 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4972 P != PEnd; ++P)
4973 AddDeclRef(*P, Record);
4974}
4975
4976/// \brief Emit a template argument list.
4977void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004978ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004979 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004980 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004981 Record.push_back(TemplateArgs->size());
4982 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004983 AddTemplateArgument(TemplateArgs->get(i), Record);
4984}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004985
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00004986void
4987ASTWriter::AddASTTemplateArgumentListInfo
4988(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
4989 assert(ASTTemplArgList && "No ASTTemplArgList!");
4990 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
4991 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
4992 Record.push_back(ASTTemplArgList->NumTemplateArgs);
4993 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
4994 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
4995 AddTemplateArgumentLoc(TemplArgs[i], Record);
4996}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004997
4998void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00004999ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005000 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005001 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005002 I = Set.begin(), E = Set.end(); I != E; ++I) {
5003 AddDeclRef(I.getDecl(), Record);
5004 Record.push_back(I.getAccess());
5005 }
5006}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005007
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005008void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005009 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005010 Record.push_back(Base.isVirtual());
5011 Record.push_back(Base.isBaseOfClass());
5012 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005013 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005014 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005015 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005016 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5017 : SourceLocation(),
5018 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005019}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005020
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005021void ASTWriter::FlushCXXBaseSpecifiers() {
5022 RecordData Record;
5023 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5024 Record.clear();
5025
5026 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005027 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005028 if (Index == CXXBaseSpecifiersOffsets.size())
5029 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5030 else {
5031 if (Index > CXXBaseSpecifiersOffsets.size())
5032 CXXBaseSpecifiersOffsets.resize(Index + 1);
5033 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5034 }
5035
5036 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5037 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5038 Record.push_back(BEnd - B);
5039 for (; B != BEnd; ++B)
5040 AddCXXBaseSpecifier(*B, Record);
5041 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005042
5043 // Flush any expressions that were written as part of the base specifiers.
5044 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005045 }
5046
5047 CXXBaseSpecifiersToWrite.clear();
5048}
5049
Alexis Hunt1d792652011-01-08 20:30:50 +00005050void ASTWriter::AddCXXCtorInitializers(
5051 const CXXCtorInitializer * const *CtorInitializers,
5052 unsigned NumCtorInitializers,
5053 RecordDataImpl &Record) {
5054 Record.push_back(NumCtorInitializers);
5055 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5056 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005057
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005058 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005059 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005060 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005061 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005062 } else if (Init->isDelegatingInitializer()) {
5063 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005064 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005065 } else if (Init->isMemberInitializer()){
5066 Record.push_back(CTOR_INITIALIZER_MEMBER);
5067 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005068 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005069 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5070 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005071 }
Francois Pichetd583da02010-12-04 09:14:42 +00005072
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005073 AddSourceLocation(Init->getMemberLocation(), Record);
5074 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005075 AddSourceLocation(Init->getLParenLoc(), Record);
5076 AddSourceLocation(Init->getRParenLoc(), Record);
5077 Record.push_back(Init->isWritten());
5078 if (Init->isWritten()) {
5079 Record.push_back(Init->getSourceOrder());
5080 } else {
5081 Record.push_back(Init->getNumArrayIndices());
5082 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5083 AddDeclRef(Init->getArrayIndex(i), Record);
5084 }
5085 }
5086}
5087
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005088void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5089 assert(D->DefinitionData);
5090 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005091 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005092 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005093 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005094 Record.push_back(Data.Aggregate);
5095 Record.push_back(Data.PlainOldData);
5096 Record.push_back(Data.Empty);
5097 Record.push_back(Data.Polymorphic);
5098 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005099 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005100 Record.push_back(Data.HasNoNonEmptyBases);
5101 Record.push_back(Data.HasPrivateFields);
5102 Record.push_back(Data.HasProtectedFields);
5103 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005104 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005105 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005106 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005107 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005108 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005109 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5110 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5111 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5112 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5113 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5114 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005115 Record.push_back(Data.HasTrivialSpecialMembers);
5116 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005117 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005118 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005119 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005120 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005121 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005122 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005123 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005124 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5125 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5126 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5127 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005128 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005129
5130 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005131 if (Data.NumBases > 0)
5132 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5133 Record);
5134
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005135 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5136 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005137 if (Data.NumVBases > 0)
5138 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5139 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005140
Richard Smitha4ba74c2013-08-30 04:46:40 +00005141 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5142 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005143 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005144 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005145
5146 // Add lambda-specific data.
5147 if (Data.IsLambda) {
5148 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005149 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005150 Record.push_back(Lambda.IsGenericLambda);
5151 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005152 Record.push_back(Lambda.NumCaptures);
5153 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005154 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005155 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005156 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005157 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5158 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5159 AddSourceLocation(Capture.getLocation(), Record);
5160 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005161 Record.push_back(Capture.getCaptureKind());
5162 switch (Capture.getCaptureKind()) {
5163 case LCK_This:
5164 break;
5165 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005166 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005167 VarDecl *Var =
5168 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5169 AddDeclRef(Var, Record);
5170 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5171 : SourceLocation(),
5172 Record);
5173 break;
5174 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005175 }
5176 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005177}
5178
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005179void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005180 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005181 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005182 assert(FirstDeclID == NextDeclID &&
5183 FirstTypeID == NextTypeID &&
5184 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005185 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005186 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005187 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005188 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005189
Sebastian Redl07a89a82010-07-30 00:29:29 +00005190 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005191
Douglas Gregordf0c1512011-08-18 04:12:04 +00005192 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5193 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5194 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005195 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005196 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005197 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005198 NextDeclID = FirstDeclID;
5199 NextTypeID = FirstTypeID;
5200 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005201 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005202 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005203 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005204}
5205
Sebastian Redl539c5062010-08-18 23:57:32 +00005206void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005207 // Always keep the highest ID. See \p TypeRead() for more information.
5208 IdentID &StoredID = IdentifierIDs[II];
5209 if (ID > StoredID)
5210 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005211}
5212
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005213void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005214 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005215 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005216 if (ID > StoredID)
5217 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005218}
5219
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005220void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005221 // Always take the highest-numbered type index. This copes with an interesting
5222 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005223 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005224 // keep the higher-numbered entry so that we can properly write it out to
5225 // the AST file.
5226 TypeIdx &StoredIdx = TypeIdxs[T];
5227 if (Idx.getIndex() >= StoredIdx.getIndex())
5228 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005229}
5230
Sebastian Redl539c5062010-08-18 23:57:32 +00005231void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005232 // Always keep the highest ID. See \p TypeRead() for more information.
5233 SelectorID &StoredID = SelectorIDs[S];
5234 if (ID > StoredID)
5235 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005236}
Douglas Gregor91096292010-10-02 19:29:26 +00005237
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005238void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005239 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005240 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005241 MacroDefinitions[MD] = ID;
5242}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005243
Douglas Gregore37a85a2011-12-02 17:30:13 +00005244void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5245 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5246 SubmoduleIDs[Mod] = ID;
5247}
5248
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005249void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005250 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005251 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005252 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5253 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005254 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005255 // A forward reference was mutated into a definition. Rewrite it.
5256 // FIXME: This happens during template instantiation, should we
5257 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005258 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005259 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005260 }
5261}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005262
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005263void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005264 assert(!WritingAST && "Already writing the AST!");
5265
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005266 // TU and namespaces are handled elsewhere.
5267 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5268 return;
5269
Douglas Gregorb3722e22011-09-09 23:01:35 +00005270 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005271 return; // Not a source decl added to a DeclContext from PCH.
5272
Douglas Gregor9f782892013-01-21 15:25:38 +00005273 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005274 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005275 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005276}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005277
5278void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005279 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005280 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005281 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005282 return; // Not a source member added to a class from PCH.
5283 if (!isa<CXXMethodDecl>(D))
5284 return; // We are interested in lazily declared implicit methods.
5285
5286 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005287 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005288 UpdateRecord &Record = DeclUpdates[RD];
5289 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005290 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005291}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005292
5293void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5294 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005295 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005296 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005297 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005298 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005299 return; // Not a source specialization added to a template from PCH.
5300
5301 UpdateRecord &Record = DeclUpdates[TD];
5302 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005303 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005304}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005305
Larisse Voufo39a1e502013-08-06 01:03:05 +00005306void ASTWriter::AddedCXXTemplateSpecialization(
5307 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5308 // The specializations set is kept in the canonical template.
5309 assert(!WritingAST && "Already writing the AST!");
5310 TD = TD->getCanonicalDecl();
5311 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5312 return; // Not a source specialization added to a template from PCH.
5313
5314 UpdateRecord &Record = DeclUpdates[TD];
5315 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5316 Record.push_back(reinterpret_cast<uint64_t>(D));
5317}
5318
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005319void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5320 const FunctionDecl *D) {
5321 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005322 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005323 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005324 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005325 return; // Not a source specialization added to a template from PCH.
5326
5327 UpdateRecord &Record = DeclUpdates[TD];
5328 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005329 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005330}
5331
Richard Smith1fa5d642013-05-11 05:45:24 +00005332void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5333 assert(!WritingAST && "Already writing the AST!");
5334 FD = FD->getCanonicalDecl();
5335 if (!FD->isFromASTFile())
5336 return; // Not a function declared in PCH and defined outside.
5337
5338 UpdateRecord &Record = DeclUpdates[FD];
5339 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5340 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5341}
5342
Sebastian Redlab238a72011-04-24 16:28:06 +00005343void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005344 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005345 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005346 return; // Declaration not imported from PCH.
5347
5348 // Implicit decl from a PCH was defined.
5349 // FIXME: Should implicit definition be a separate FunctionDecl?
5350 RewriteDecl(D);
5351}
5352
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005353void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005354 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005355 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005356 return;
5357
5358 // Since the actual instantiation is delayed, this really means that we need
5359 // to update the instantiation location.
5360 UpdateRecord &Record = DeclUpdates[D];
5361 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5362 AddSourceLocation(
5363 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5364}
5365
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005366void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5367 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005368 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005369 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005370 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005371
5372 assert(IFD->getDefinition() && "Category on a class without a definition?");
5373 ObjCClassesWithCategories.insert(
5374 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005375}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005376
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005377
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005378void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5379 const ObjCPropertyDecl *OrigProp,
5380 const ObjCCategoryDecl *ClassExt) {
5381 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5382 if (!D)
5383 return;
5384
5385 assert(!WritingAST && "Already writing the AST!");
5386 if (!D->isFromASTFile())
5387 return; // Declaration not imported from PCH.
5388
5389 RewriteDecl(D);
5390}
Eli Friedman276dd182013-09-05 00:02:25 +00005391
5392void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5393 assert(!WritingAST && "Already writing the AST!");
5394 if (!D->isFromASTFile())
5395 return;
5396
5397 UpdateRecord &Record = DeclUpdates[D];
5398 Record.push_back(UPD_DECL_MARKED_USED);
5399}