blob: 2769b9fda11775df9551bbed8650bfa9846d68c3 [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
1303 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1304 // the set of (non-system) input files. This is simple heuristic for
1305 // detecting whether the system headers may have changed, because it is too
1306 // expensive to stat() all of the system headers.
Richard Smith0b1f4762013-05-20 23:40:27 +00001307 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bb719f2013-03-20 16:59:53 +00001308 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001309 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1310 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1311 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1312 InputFileEntry Entry = { SDKSettingsFile, false, false };
1313 SortedFiles.push_front(Entry);
1314 }
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001315 }
1316
Douglas Gregora3dd9002013-07-22 20:48:33 +00001317 // Add the compiler's own module.map in the set of (non-system) input files.
1318 // This is a simple heuristic for detecting whether the compiler's headers
1319 // have changed, because we don't want to stat() all of them.
1320 if (Modules && !Chain) {
1321 SmallString<128> P = StringRef(HSOpts.ResourceDir);
1322 llvm::sys::path::append(P, "include");
1323 llvm::sys::path::append(P, "module.map");
1324 if (const FileEntry *ModuleMapFile = FileMgr.getFile(P)) {
1325 InputFileEntry Entry = { ModuleMapFile, false, false };
1326 SortedFiles.push_front(Entry);
1327 }
1328 }
1329
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001330 unsigned UserFilesNum = 0;
1331 // Write out all of the input files.
1332 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor49491f72013-03-15 22:15:07 +00001333 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001334 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor49491f72013-03-15 22:15:07 +00001335 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001336
Douglas Gregor49491f72013-03-15 22:15:07 +00001337 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001338 if (InputFileID != 0)
1339 continue; // already recorded this file.
1340
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001341 // Record this entry's offset.
1342 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidise65856f2012-12-11 07:48:08 +00001343
1344 InputFileID = InputFileOffsets.size();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001345
Douglas Gregor49491f72013-03-15 22:15:07 +00001346 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001347 ++UserFilesNum;
1348
Douglas Gregor72be3902012-10-19 00:38:02 +00001349 Record.clear();
1350 Record.push_back(INPUT_FILE);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001351 Record.push_back(InputFileOffsets.size());
Douglas Gregor72be3902012-10-19 00:38:02 +00001352
1353 // Emit size/modification time for this file.
Douglas Gregor49491f72013-03-15 22:15:07 +00001354 Record.push_back(Entry.File->getSize());
1355 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor72be3902012-10-19 00:38:02 +00001356
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001357 // Whether this file was overridden.
Douglas Gregor49491f72013-03-15 22:15:07 +00001358 Record.push_back(Entry.BufferOverridden);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001359
Douglas Gregor72be3902012-10-19 00:38:02 +00001360 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor49491f72013-03-15 22:15:07 +00001361 const char *Filename = Entry.File->getName();
Douglas Gregor72be3902012-10-19 00:38:02 +00001362 SmallString<128> FilePath(Filename);
1363
1364 // Ask the file manager to fixup the relative path for us. This will
1365 // honor the working directory.
Douglas Gregor49491f72013-03-15 22:15:07 +00001366 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor72be3902012-10-19 00:38:02 +00001367
1368 // FIXME: This call to make_absolute shouldn't be necessary, the
1369 // call to FixupRelativePath should always return an absolute path.
1370 llvm::sys::fs::make_absolute(FilePath);
1371 Filename = FilePath.c_str();
1372
1373 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1374
1375 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1376 }
Douglas Gregor49491f72013-03-15 22:15:07 +00001377
Douglas Gregor112b9072012-10-18 05:31:06 +00001378 Stream.ExitBlock();
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001379
1380 // Create input file offsets abbreviation.
1381 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1382 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1383 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001384 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1385 // input files
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001386 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1387 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1388
1389 // Write input file offsets.
1390 Record.clear();
1391 Record.push_back(INPUT_FILE_OFFSETS);
1392 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis7d238572013-03-06 18:12:50 +00001393 Record.push_back(UserFilesNum);
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001394 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor55abb232009-04-10 20:39:37 +00001395}
1396
Douglas Gregora7f71a92009-04-10 03:52:48 +00001397//===----------------------------------------------------------------------===//
1398// Source Manager Serialization
1399//===----------------------------------------------------------------------===//
1400
1401/// \brief Create an abbreviation for the SLocEntry that refers to a
1402/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001403static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001404 using namespace llvm;
1405 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001406 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1408 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1409 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001411 // FileEntry fields.
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor8f45df52009-04-16 22:23:12 +00001416 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001417}
1418
1419/// \brief Create an abbreviation for the SLocEntry that refers to a
1420/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001421static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001422 using namespace llvm;
1423 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001424 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001430 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001431}
1432
1433/// \brief Create an abbreviation for the SLocEntry that refers to a
1434/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001435static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001436 using namespace llvm;
1437 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001438 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001440 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001441}
1442
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001443/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1444/// expansion.
1445static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001446 using namespace llvm;
1447 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001448 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001449 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1450 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1451 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1452 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001454 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001455}
1456
Douglas Gregor09b69892011-02-10 17:09:37 +00001457namespace {
1458 // Trait used for the on-disk hash table of header search information.
1459 class HeaderFileInfoTrait {
1460 ASTWriter &Writer;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001461 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001462
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001463 // Keep track of the framework names we've used during serialization.
1464 SmallVector<char, 128> FrameworkStringData;
1465 llvm::StringMap<unsigned> FrameworkNameOffset;
1466
Douglas Gregor09b69892011-02-10 17:09:37 +00001467 public:
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001468 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1469 : Writer(Writer), HS(HS) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001470
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001471 struct key_type {
1472 const FileEntry *FE;
1473 const char *Filename;
1474 };
1475 typedef const key_type &key_type_ref;
Douglas Gregor09b69892011-02-10 17:09:37 +00001476
1477 typedef HeaderFileInfo data_type;
1478 typedef const data_type &data_type_ref;
1479
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001480 static unsigned ComputeHash(key_type_ref key) {
1481 // The hash is based only on size/time of the file, so that the reader can
1482 // match even when symlinking or excess path elements ("foo/../", "../")
1483 // change the form of the name. However, complete path is still the key.
1484 return llvm::hash_combine(key.FE->getSize(),
1485 key.FE->getModificationTime());
Douglas Gregor09b69892011-02-10 17:09:37 +00001486 }
1487
1488 std::pair<unsigned,unsigned>
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001489 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1490 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1491 clang::io::Emit16(Out, KeyLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001492 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001493 if (Data.isModuleHeader)
1494 DataLen += 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001495 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001496 return std::make_pair(KeyLen, DataLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001497 }
1498
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001499 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1500 clang::io::Emit64(Out, key.FE->getSize());
1501 KeyLen -= 8;
1502 clang::io::Emit64(Out, key.FE->getModificationTime());
1503 KeyLen -= 8;
1504 Out.write(key.Filename, KeyLen);
Douglas Gregor09b69892011-02-10 17:09:37 +00001505 }
1506
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001507 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregor09b69892011-02-10 17:09:37 +00001508 data_type_ref Data, unsigned DataLen) {
1509 using namespace clang::io;
1510 uint64_t Start = Out.tell(); (void)Start;
1511
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001512 unsigned char Flags = (Data.HeaderRole << 6)
1513 | (Data.isImport << 5)
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001514 | (Data.isPragmaOnce << 4)
1515 | (Data.DirInfo << 2)
1516 | (Data.Resolved << 1)
1517 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001518 Emit8(Out, (uint8_t)Flags);
1519 Emit16(Out, (uint16_t) Data.NumIncludes);
1520
1521 if (!Data.ControllingMacro)
1522 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1523 else
1524 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001525
1526 unsigned Offset = 0;
1527 if (!Data.Framework.empty()) {
1528 // If this header refers into a framework, save the framework name.
1529 llvm::StringMap<unsigned>::iterator Pos
1530 = FrameworkNameOffset.find(Data.Framework);
1531 if (Pos == FrameworkNameOffset.end()) {
1532 Offset = FrameworkStringData.size() + 1;
1533 FrameworkStringData.append(Data.Framework.begin(),
1534 Data.Framework.end());
1535 FrameworkStringData.push_back(0);
1536
1537 FrameworkNameOffset[Data.Framework] = Offset;
1538 } else
1539 Offset = Pos->second;
1540 }
1541 Emit32(Out, Offset);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001542
1543 if (Data.isModuleHeader) {
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001544 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001545 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1546 }
1547
Douglas Gregor09b69892011-02-10 17:09:37 +00001548 assert(Out.tell() - Start == DataLen && "Wrong data length");
1549 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001550
1551 const char *strings_begin() const { return FrameworkStringData.begin(); }
1552 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001553 };
1554} // end anonymous namespace
1555
1556/// \brief Write the header search block for the list of files that
1557///
1558/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001559void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001560 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001561 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1562
1563 if (FilesByUID.size() > HS.header_file_size())
1564 FilesByUID.resize(HS.header_file_size());
1565
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001566 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregor09b69892011-02-10 17:09:37 +00001567 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001568 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001569 unsigned NumHeaderSearchEntries = 0;
1570 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1571 const FileEntry *File = FilesByUID[UID];
1572 if (!File)
1573 continue;
1574
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001575 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1576 // from the external source if it was not provided already.
1577 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001578 if (HFI.External && Chain)
1579 continue;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001580 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1581 continue;
Douglas Gregor09b69892011-02-10 17:09:37 +00001582
1583 // Turn the file name into an absolute path, if it isn't already.
1584 const char *Filename = File->getName();
1585 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1586
1587 // If we performed any translation on the file name at all, we need to
1588 // save this string, since the generator will refer to it later.
1589 if (Filename != File->getName()) {
1590 Filename = strdup(Filename);
1591 SavedStrings.push_back(Filename);
1592 }
1593
Argyrios Kyrtzidis5c2a3452013-03-06 18:12:47 +00001594 HeaderFileInfoTrait::key_type key = { File, Filename };
1595 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregor09b69892011-02-10 17:09:37 +00001596 ++NumHeaderSearchEntries;
1597 }
1598
1599 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001600 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001601 uint32_t BucketOffset;
1602 {
1603 llvm::raw_svector_ostream Out(TableData);
1604 // Make sure that no bucket is at offset 0
1605 clang::io::Emit32(Out, 0);
1606 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1607 }
1608
1609 // Create a blob abbreviation
1610 using namespace llvm;
1611 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1612 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1613 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1614 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001615 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1617 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1618
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001619 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001620 RecordData Record;
1621 Record.push_back(HEADER_SEARCH_TABLE);
1622 Record.push_back(BucketOffset);
1623 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001624 Record.push_back(TableData.size());
1625 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001626 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1627
1628 // Free all of the strings we had to duplicate.
1629 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greenebae0e352013-01-15 22:09:43 +00001630 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregor09b69892011-02-10 17:09:37 +00001631}
1632
Douglas Gregora7f71a92009-04-10 03:52:48 +00001633/// \brief Writes the block containing the serialized form of the
1634/// source manager.
1635///
1636/// TODO: We should probably use an on-disk hash table (stored in a
1637/// blob), indexed based on the file name, so that we only create
1638/// entries for files that we actually need. In the common case (no
1639/// errors), we probably won't have to create file entries for any of
1640/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001641void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001642 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001643 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001644 RecordData Record;
1645
Chris Lattner0910e3b2009-04-10 17:16:57 +00001646 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001647 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001648
1649 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001650 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1651 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1652 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001653 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001654
Douglas Gregor258ae542009-04-27 06:38:32 +00001655 // Write out the source location entry table. We skip the first
1656 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001657 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001658 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001659 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1660 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001661 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001662 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001663 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001664 FileID FID = FileID::get(I);
1665 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001666
Douglas Gregor258ae542009-04-27 06:38:32 +00001667 // Record the offset of this source-location entry.
1668 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1669
1670 // Figure out which record code to use.
1671 unsigned Code;
1672 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001673 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1674 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001675 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001676 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001677 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001678 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001679 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001680 Record.clear();
1681 Record.push_back(Code);
1682
Douglas Gregor925296b2011-07-19 16:10:42 +00001683 // Starting offset of this entry within this module, so skip the dummy.
1684 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001685 if (SLoc->isFile()) {
1686 const SrcMgr::FileInfo &File = SLoc->getFile();
1687 Record.push_back(File.getIncludeLoc().getRawEncoding());
1688 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1689 Record.push_back(File.hasLineDirectives());
1690
1691 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001692 if (Content->OrigEntry) {
1693 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001694 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001695
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001696 // The source location entry is a file. Emit input file ID.
1697 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1698 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump11289f42009-09-09 15:08:12 +00001699
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001700 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001701
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001702 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001703 if (FDI != FileDeclIDs.end()) {
1704 Record.push_back(FDI->second->FirstDeclIndex);
1705 Record.push_back(FDI->second->DeclIDs.size());
1706 } else {
1707 Record.push_back(0);
1708 Record.push_back(0);
1709 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001710
Douglas Gregor3120d2c2012-10-22 18:42:04 +00001711 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001712
1713 if (Content->BufferOverridden) {
1714 Record.clear();
1715 Record.push_back(SM_SLOC_BUFFER_BLOB);
1716 const llvm::MemoryBuffer *Buffer
1717 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1718 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1719 StringRef(Buffer->getBufferStart(),
1720 Buffer->getBufferSize() + 1));
1721 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001722 } else {
1723 // The source location entry is a buffer. The blob associated
1724 // with this entry contains the contents of the buffer.
1725
1726 // We add one to the size so that we capture the trailing NULL
1727 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1728 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001729 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001730 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001731 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001732 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001733 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001734 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001735 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001736 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001737 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001738 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001739
Douglas Gregor925296b2011-07-19 16:10:42 +00001740 if (strcmp(Name, "<built-in>") == 0) {
1741 PreloadSLocs.push_back(SLocEntryOffsets.size());
1742 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001743 }
1744 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001745 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001746 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001747 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1748 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001749 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1750 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001751
1752 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001753 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001754 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001755 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001756 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001757 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001758 }
1759 }
1760
Douglas Gregor8f45df52009-04-16 22:23:12 +00001761 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001762
1763 if (SLocEntryOffsets.empty())
1764 return;
1765
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001766 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001767 // table is used for lazily loading source-location information.
1768 using namespace llvm;
1769 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001770 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1774 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001775
Douglas Gregor258ae542009-04-27 06:38:32 +00001776 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001777 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001778 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001779 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001780 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001781
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001782 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001783 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001784 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001785
1786 // Write the line table. It depends on remapping working, so it must come
1787 // after the source location offsets.
1788 if (SourceMgr.hasLineTable()) {
1789 LineTableInfo &LineTable = SourceMgr.getLineTable();
1790
1791 Record.clear();
1792 // Emit the file names
1793 Record.push_back(LineTable.getNumFilenames());
1794 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1795 // Emit the file name
1796 const char *Filename = LineTable.getFilename(I);
1797 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1798 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1799 Record.push_back(FilenameLen);
1800 if (FilenameLen)
1801 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1802 }
1803
1804 // Emit the line entries
1805 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1806 L != LEnd; ++L) {
1807 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001808 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001809 continue;
1810
1811 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001812 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001813
1814 // Emit the line entries
1815 Record.push_back(L->second.size());
1816 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1817 LEEnd = L->second.end();
1818 LE != LEEnd; ++LE) {
1819 Record.push_back(LE->FileOffset);
1820 Record.push_back(LE->LineNo);
1821 Record.push_back(LE->FilenameID);
1822 Record.push_back((unsigned)LE->FileKind);
1823 Record.push_back(LE->IncludeOffset);
1824 }
1825 }
1826 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1827 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001828}
1829
Douglas Gregorc5046832009-04-27 18:38:38 +00001830//===----------------------------------------------------------------------===//
1831// Preprocessor Serialization
1832//===----------------------------------------------------------------------===//
1833
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001834namespace {
1835class ASTMacroTableTrait {
1836public:
1837 typedef IdentID key_type;
1838 typedef key_type key_type_ref;
1839
1840 struct Data {
1841 uint32_t MacroDirectivesOffset;
1842 };
1843
1844 typedef Data data_type;
1845 typedef const data_type &data_type_ref;
1846
1847 static unsigned ComputeHash(IdentID IdID) {
1848 return llvm::hash_value(IdID);
1849 }
1850
1851 std::pair<unsigned,unsigned>
1852 static EmitKeyDataLength(raw_ostream& Out,
1853 key_type_ref Key, data_type_ref Data) {
1854 unsigned KeyLen = 4; // IdentID.
1855 unsigned DataLen = 4; // MacroDirectivesOffset.
1856 return std::make_pair(KeyLen, DataLen);
1857 }
1858
1859 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1860 clang::io::Emit32(Out, Key);
1861 }
1862
1863 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1864 unsigned) {
1865 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1866 }
1867};
1868} // end anonymous namespace
1869
Benjamin Kramer04bf1872013-09-22 14:10:29 +00001870static int compareMacroDirectives(
1871 const std::pair<const IdentifierInfo *, MacroDirective *> *X,
1872 const std::pair<const IdentifierInfo *, MacroDirective *> *Y) {
1873 return X->first->getName().compare(Y->first->getName());
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001874}
1875
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001876static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1877 const Preprocessor &PP) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001878 if (MacroInfo *MI = MD->getMacroInfo())
1879 if (MI->isBuiltinMacro())
1880 return true;
Argyrios Kyrtzidis0aef0f02013-03-15 22:43:10 +00001881
1882 if (IsModule) {
1883 SourceLocation Loc = MD->getLocation();
1884 if (Loc.isInvalid())
1885 return true;
1886 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1887 return true;
1888 }
1889
1890 return false;
1891}
1892
Chris Lattnereeffaef2009-04-10 17:15:23 +00001893/// \brief Writes the block containing the serialized form of the
1894/// preprocessor.
1895///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001896void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001897 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1898 if (PPRec)
1899 WritePreprocessorDetail(*PPRec);
1900
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001901 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001902
Chris Lattner0af3ba12009-04-13 01:29:17 +00001903 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1904 if (PP.getCounterValue() != 0) {
1905 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001906 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001907 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001908 }
1909
1910 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001911 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001912
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001913 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001914 // FIXME: use diagnostics subsystem for localization etc.
1915 if (PP.SawDateOrTime())
1916 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001917
Douglas Gregor796d76a2010-10-20 22:00:55 +00001918
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001919 // Loop over all the macro directives that are live at the end of the file,
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001920 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001921
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001922 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00001923 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001924 MacroDirectives;
1925 for (Preprocessor::macro_iterator
1926 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1927 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001928 I != E; ++I) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001929 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001930 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001931
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001932 // Sort the set of macro definitions that need to be serialized by the
1933 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001934 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1935 &compareMacroDirectives);
1936
1937 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1938
1939 // Emit the macro directives as a list and associate the offset with the
1940 // identifier they belong to.
1941 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1942 const IdentifierInfo *Name = MacroDirectives[I].first;
1943 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1944 MacroDirective *MD = MacroDirectives[I].second;
1945
1946 // If the macro or identifier need no updates, don't write the macro history
1947 // for this one.
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001948 // FIXME: Chain the macro history instead of re-writing it.
1949 if (MD->isFromPCH() &&
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001950 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1951 continue;
1952
1953 // Emit the macro directives in reverse source order.
1954 for (; MD; MD = MD->getPrevious()) {
1955 if (shouldIgnoreMacro(MD, IsModule, PP))
1956 continue;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001957
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001958 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00001959 Record.push_back(MD->getKind());
1960 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1961 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1962 Record.push_back(InfoID);
1963 Record.push_back(DefMD->isImported());
1964 Record.push_back(DefMD->isAmbiguous());
1965
1966 } else if (VisibilityMacroDirective *
1967 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1968 Record.push_back(VisMD->isPublic());
1969 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001970 }
1971 if (Record.empty())
1972 continue;
1973
1974 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1975 Record.clear();
1976
1977 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1978
1979 IdentID NameID = getIdentifierRef(Name);
1980 ASTMacroTableTrait::Data data;
1981 data.MacroDirectivesOffset = MacroDirectiveOffset;
1982 Generator.insert(NameID, data);
1983 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001984
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001985 /// \brief Offsets of each of the macros into the bitstream, indexed by
1986 /// the local macro ID
1987 ///
1988 /// For each identifier that is associated with a macro, this map
1989 /// provides the offset into the bitstream where that macro is
1990 /// defined.
1991 std::vector<uint32_t> MacroOffsets;
1992
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001993 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1994 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1995 MacroInfo *MI = MacroInfosToEmit[I].MI;
1996 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001997
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00001998 if (ID < FirstMacroID) {
1999 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2000 continue;
Chris Lattner2199f5b2009-04-10 18:08:30 +00002001 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002002
2003 // Record the local offset of this macro.
2004 unsigned Index = ID - FirstMacroID;
2005 if (Index == MacroOffsets.size())
2006 MacroOffsets.push_back(Stream.GetCurrentBitNo());
2007 else {
2008 if (Index > MacroOffsets.size())
2009 MacroOffsets.resize(Index + 1);
2010
2011 MacroOffsets[Index] = Stream.GetCurrentBitNo();
2012 }
2013
2014 AddIdentifierRef(Name, Record);
2015 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2016 AddSourceLocation(MI->getDefinitionLoc(), Record);
2017 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2018 Record.push_back(MI->isUsed());
2019 unsigned Code;
2020 if (MI->isObjectLike()) {
2021 Code = PP_MACRO_OBJECT_LIKE;
2022 } else {
2023 Code = PP_MACRO_FUNCTION_LIKE;
2024
2025 Record.push_back(MI->isC99Varargs());
2026 Record.push_back(MI->isGNUVarargs());
2027 Record.push_back(MI->hasCommaPasting());
2028 Record.push_back(MI->getNumArgs());
2029 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2030 I != E; ++I)
2031 AddIdentifierRef(*I, Record);
2032 }
2033
2034 // If we have a detailed preprocessing record, record the macro definition
2035 // ID that corresponds to this macro.
2036 if (PPRec)
2037 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2038
2039 Stream.EmitRecord(Code, Record);
2040 Record.clear();
2041
2042 // Emit the tokens array.
2043 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2044 // Note that we know that the preprocessor does not have any annotation
2045 // tokens in it because they are created by the parser, and thus can't
2046 // be in a macro definition.
2047 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallf413f5e2013-05-03 00:10:13 +00002048 AddToken(Tok, Record);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002049 Stream.EmitRecord(PP_TOKEN, Record);
2050 Record.clear();
2051 }
2052 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00002053 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002054
Douglas Gregor92a96f52011-02-08 21:58:10 +00002055 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002056
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002057 // Create the on-disk hash table in a buffer.
2058 SmallString<4096> MacroTable;
2059 uint32_t BucketOffset;
2060 {
2061 llvm::raw_svector_ostream Out(MacroTable);
2062 // Make sure that no bucket is at offset 0
2063 clang::io::Emit32(Out, 0);
2064 BucketOffset = Generator.Emit(Out);
2065 }
2066
2067 // Write the macro table
2068 using namespace llvm;
2069 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2070 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2073 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2074
2075 Record.push_back(MACRO_TABLE);
2076 Record.push_back(BucketOffset);
2077 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2078 Record.clear();
2079
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002080 // Write the offsets table for macro IDs.
2081 using namespace llvm;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002082 Abbrev = new BitCodeAbbrev();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00002083 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2087
2088 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2089 Record.clear();
2090 Record.push_back(MACRO_OFFSET);
2091 Record.push_back(MacroOffsets.size());
2092 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2093 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2094 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002095}
2096
2097void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002098 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00002099 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002100
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002101 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002102
Douglas Gregor92a96f52011-02-08 21:58:10 +00002103 // Enter the preprocessor block.
2104 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002105
Douglas Gregoraae92242010-03-19 21:51:54 +00002106 // If the preprocessor has a preprocessing record, emit it.
2107 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002108 using namespace llvm;
2109
2110 // Set up the abbreviation for
2111 unsigned InclusionAbbrev = 0;
2112 {
2113 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2114 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00002115 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00002119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2120 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2121 }
2122
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002123 unsigned FirstPreprocessorEntityID
2124 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2125 + NUM_PREDEF_PP_ENTITY_IDS;
2126 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002127 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00002128 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2129 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00002130 E != EEnd;
2131 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00002132 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002133
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00002134 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2135 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002136
Douglas Gregor92a96f52011-02-08 21:58:10 +00002137 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002138 // Record this macro definition's ID.
2139 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002140
Douglas Gregor92a96f52011-02-08 21:58:10 +00002141 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002142 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2143 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00002144 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002145
Chandler Carrutha88a22182011-07-14 08:20:46 +00002146 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00002147 Record.push_back(ME->isBuiltinMacro());
2148 if (ME->isBuiltinMacro())
2149 AddIdentifierRef(ME->getName(), Record);
2150 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002151 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00002152 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002153 continue;
2154 }
2155
2156 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2157 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00002158 Record.push_back(ID->getFileName().size());
2159 Record.push_back(ID->wasInQuotes());
2160 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00002161 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002162 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00002163 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00002164 // Check that the FileEntry is not null because it was not resolved and
2165 // we create a PCH even with compiler errors.
2166 if (ID->getFile())
2167 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00002168 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2169 continue;
2170 }
2171
2172 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2173 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00002174 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002175
Douglas Gregoraae92242010-03-19 21:51:54 +00002176 // Write the offsets table for the preprocessing record.
2177 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002178 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2179
Douglas Gregoraae92242010-03-19 21:51:54 +00002180 // Write the offsets table for identifier IDs.
2181 using namespace llvm;
2182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002183 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00002185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002186 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00002187
Douglas Gregoraae92242010-03-19 21:51:54 +00002188 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002189 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00002190 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00002191 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2192 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00002193 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00002194}
2195
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002196unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2197 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2198 if (Known != SubmoduleIDs.end())
2199 return Known->second;
2200
2201 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2202}
2203
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00002204unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2205 if (!Mod)
2206 return 0;
2207
2208 llvm::DenseMap<Module *, unsigned>::const_iterator
2209 Known = SubmoduleIDs.find(Mod);
2210 if (Known != SubmoduleIDs.end())
2211 return Known->second;
2212
2213 return 0;
2214}
2215
Douglas Gregor253eefe2011-12-01 00:59:36 +00002216/// \brief Compute the number of modules within the given tree (including the
2217/// given module).
2218static unsigned getNumberOfModules(Module *Mod) {
2219 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00002220 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2221 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00002222 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002223 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00002224
2225 return ChildModules + 1;
2226}
2227
Douglas Gregorde3ef502011-11-30 23:21:26 +00002228void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00002229 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002230 // FIXME: This feels like it belongs somewhere else, but there are no
2231 // other consumers of this information.
2232 SourceManager &SrcMgr = PP->getSourceManager();
2233 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2234 for (ASTContext::import_iterator I = Context->local_import_begin(),
2235 IEnd = Context->local_import_end();
2236 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002237 if (Module *ImportedFrom
2238 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2239 SrcMgr))) {
2240 ImportedFrom->Imports.push_back(I->getImportedModule());
2241 }
2242 }
2243
Douglas Gregor69021972011-11-30 17:33:56 +00002244 // Enter the submodule description block.
2245 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2246
2247 // Write the abbreviations needed for the submodules block.
2248 using namespace llvm;
2249 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2250 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor69021972011-11-30 17:33:56 +00002260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2261 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2262
2263 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002264 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2266 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2267
2268 Abbrev = new BitCodeAbbrev();
2269 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2271 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002272
2273 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002274 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2276 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2277
2278 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002279 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2281 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2282
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002283 Abbrev = new BitCodeAbbrev();
2284 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
Richard Smitha3feee22013-10-28 22:18:19 +00002285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002287 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2288
Douglas Gregor59527662012-10-15 06:28:11 +00002289 Abbrev = new BitCodeAbbrev();
2290 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2292 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2293
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002294 Abbrev = new BitCodeAbbrev();
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002295 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2297 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2298
2299 Abbrev = new BitCodeAbbrev();
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002300 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2303 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2304
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002305 Abbrev = new BitCodeAbbrev();
2306 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2307 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2308 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2309
Douglas Gregorfb912652013-03-20 21:10:35 +00002310 Abbrev = new BitCodeAbbrev();
2311 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2314 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2315
Douglas Gregor253eefe2011-12-01 00:59:36 +00002316 // Write the submodule metadata block.
2317 RecordData Record;
2318 Record.push_back(getNumberOfModules(WritingModule));
2319 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2320 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2321
Douglas Gregor69021972011-11-30 17:33:56 +00002322 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002323 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002324 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002325 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002326 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002327 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002328 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002329
2330 // Emit the definition of the block.
2331 Record.clear();
2332 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002333 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002334 if (Mod->Parent) {
2335 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2336 Record.push_back(SubmoduleIDs[Mod->Parent]);
2337 } else {
2338 Record.push_back(0);
2339 }
2340 Record.push_back(Mod->IsFramework);
2341 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002342 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002343 Record.push_back(Mod->InferSubmodules);
2344 Record.push_back(Mod->InferExplicitSubmodules);
2345 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002346 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor69021972011-11-30 17:33:56 +00002347 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2348
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002349 // Emit the requirements.
Richard Smitha3feee22013-10-28 22:18:19 +00002350 for (unsigned I = 0, N = Mod->Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002351 Record.clear();
2352 Record.push_back(SUBMODULE_REQUIRES);
Richard Smitha3feee22013-10-28 22:18:19 +00002353 Record.push_back(Mod->Requirements[I].second);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002354 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
Richard Smitha3feee22013-10-28 22:18:19 +00002355 Mod->Requirements[I].first);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002356 }
2357
Douglas Gregor69021972011-11-30 17:33:56 +00002358 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002359 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002360 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002361 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002362 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002363 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002364 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2365 Record.clear();
2366 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2367 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2368 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002369 }
2370
2371 // Emit the headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002372 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor69021972011-11-30 17:33:56 +00002373 Record.clear();
2374 Record.push_back(SUBMODULE_HEADER);
2375 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002376 Mod->NormalHeaders[I]->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002377 }
Douglas Gregor59527662012-10-15 06:28:11 +00002378 // Emit the excluded headers.
2379 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2380 Record.clear();
2381 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2382 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2383 Mod->ExcludedHeaders[I]->getName());
2384 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00002385 // Emit the private headers.
2386 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2387 Record.clear();
2388 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2389 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2390 Mod->PrivateHeaders[I]->getName());
2391 }
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002392 ArrayRef<const FileEntry *>
2393 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2394 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002395 Record.clear();
2396 Record.push_back(SUBMODULE_TOPHEADER);
2397 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002398 TopHeaders[I]->getName());
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002399 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002400
2401 // Emit the imports.
2402 if (!Mod->Imports.empty()) {
2403 Record.clear();
2404 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002405 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002406 assert(ImportedID && "Unknown submodule!");
2407 Record.push_back(ImportedID);
2408 }
2409 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2410 }
2411
Douglas Gregor24bb9232011-12-02 18:58:38 +00002412 // Emit the exports.
2413 if (!Mod->Exports.empty()) {
2414 Record.clear();
2415 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002416 if (Module *Exported = Mod->Exports[I].getPointer()) {
2417 unsigned ExportedID = SubmoduleIDs[Exported];
2418 assert(ExportedID > 0 && "Unknown submodule ID?");
2419 Record.push_back(ExportedID);
2420 } else {
2421 Record.push_back(0);
2422 }
2423
Douglas Gregor24bb9232011-12-02 18:58:38 +00002424 Record.push_back(Mod->Exports[I].getInt());
2425 }
2426 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2427 }
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002428
Daniel Jasperba7f2f72013-09-24 09:14:14 +00002429 //FIXME: How do we emit the 'use'd modules? They may not be submodules.
2430 // Might be unnecessary as use declarations are only used to build the
2431 // module itself.
2432
Douglas Gregor6ddfca92013-01-14 17:21:00 +00002433 // Emit the link libraries.
2434 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2435 Record.clear();
2436 Record.push_back(SUBMODULE_LINK_LIBRARY);
2437 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2438 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2439 Mod->LinkLibraries[I].Library);
2440 }
2441
Douglas Gregorfb912652013-03-20 21:10:35 +00002442 // Emit the conflicts.
2443 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2444 Record.clear();
2445 Record.push_back(SUBMODULE_CONFLICT);
2446 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2447 assert(OtherID && "Unknown submodule!");
2448 Record.push_back(OtherID);
2449 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2450 Mod->Conflicts[I].Message);
2451 }
2452
Douglas Gregor35b13ec2013-03-20 00:22:05 +00002453 // Emit the configuration macros.
2454 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2455 Record.clear();
2456 Record.push_back(SUBMODULE_CONFIG_MACRO);
2457 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2458 Mod->ConfigMacros[I]);
2459 }
2460
Douglas Gregor69021972011-11-30 17:33:56 +00002461 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002462 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2463 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002464 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002465 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002466 }
2467
2468 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002469
2470 assert((NextSubmoduleID - FirstSubmoduleID
2471 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002472}
2473
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002474serialization::SubmoduleID
2475ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002476 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002477 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002478
2479 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002480 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002481 Module *OwningMod
2482 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002483 if (!OwningMod)
2484 return 0;
2485
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002486 // Check whether this submodule is part of our own module.
2487 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002488 return 0;
2489
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002490 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002491}
2492
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00002493void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2494 bool isModule) {
2495 // Make sure set diagnostic pragmas don't affect the translation unit that
2496 // imports the module.
2497 // FIXME: Make diagnostic pragma sections work properly with modules.
2498 if (isModule)
2499 return;
2500
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002501 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2502 DiagStateIDMap;
2503 unsigned CurrID = 0;
2504 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002505 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002506 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002507 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2508 I != E; ++I) {
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002509 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002510 if (point.Loc.isInvalid())
2511 continue;
2512
2513 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002514 unsigned &DiagStateID = DiagStateIDMap[point.State];
2515 Record.push_back(DiagStateID);
2516
2517 if (DiagStateID == 0) {
2518 DiagStateID = ++CurrID;
2519 for (DiagnosticsEngine::DiagState::const_iterator
2520 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2521 if (I->second.isPragma()) {
2522 Record.push_back(I->first);
2523 Record.push_back(I->second.getMapping());
2524 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002525 }
Argyrios Kyrtzidisefaa54a2012-10-30 00:27:21 +00002526 Record.push_back(-1); // mark the end of the diag/map pairs for this
2527 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002528 }
2529 }
2530
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002531 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002532 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002533}
2534
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002535void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2536 if (CXXBaseSpecifiersOffsets.empty())
2537 return;
2538
2539 RecordData Record;
2540
2541 // Create a blob abbreviation for the C++ base specifiers offsets.
2542 using namespace llvm;
2543
2544 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2545 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2547 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2548 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2549
Douglas Gregorc27b2872011-08-04 00:01:48 +00002550 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002551 Record.clear();
2552 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2553 Record.push_back(CXXBaseSpecifiersOffsets.size());
2554 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002555 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002556}
2557
Douglas Gregorc5046832009-04-27 18:38:38 +00002558//===----------------------------------------------------------------------===//
2559// Type Serialization
2560//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002561
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002562/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002563void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002564 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002565 if (Idx.getIndex() == 0) // we haven't seen this type before.
2566 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002567
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002568 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002569
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002570 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002571 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002572 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002573 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002574 else if (TypeOffsets.size() < Index) {
2575 TypeOffsets.resize(Index + 1);
2576 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002577 }
2578
2579 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002580
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002581 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002582 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002583
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002584 if (T.hasLocalNonFastQualifiers()) {
2585 Qualifiers Qs = T.getLocalQualifiers();
2586 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002587 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002588 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002589 } else {
2590 switch (T->getTypeClass()) {
2591 // For all of the concrete, non-dependent types, call the
2592 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002593#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002594 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002595#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002596#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002597 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002598 }
2599
2600 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002601 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002602
2603 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002604 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002605}
2606
Douglas Gregorc5046832009-04-27 18:38:38 +00002607//===----------------------------------------------------------------------===//
2608// Declaration Serialization
2609//===----------------------------------------------------------------------===//
2610
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002611/// \brief Write the block containing all of the declaration IDs
2612/// lexically declared within the given DeclContext.
2613///
2614/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2615/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002616uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002617 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002618 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002619 return 0;
2620
Douglas Gregor8f45df52009-04-16 22:23:12 +00002621 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002622 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002623 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002624 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002625 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2626 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002627 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002628
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002629 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002630 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002631 return Offset;
2632}
2633
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002634void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002635 using namespace llvm;
2636 RecordData Record;
2637
2638 // Write the type offsets array
2639 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002640 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002642 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002643 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2644 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2645 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002646 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002647 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002648 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002649 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002650
2651 // Write the declaration offsets array
2652 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002653 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002654 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002655 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002656 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2657 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2658 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002659 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002660 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002661 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002662 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002663}
2664
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002665void ASTWriter::WriteFileDeclIDsMap() {
2666 using namespace llvm;
2667 RecordData Record;
2668
2669 // Join the vectors of DeclIDs from all files.
2670 SmallVector<DeclID, 256> FileSortedIDs;
2671 for (FileDeclIDsTy::iterator
2672 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2673 DeclIDInFileInfo &Info = *FI->second;
2674 Info.FirstDeclIndex = FileSortedIDs.size();
2675 for (LocDeclIDsTy::iterator
2676 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2677 FileSortedIDs.push_back(DI->second);
2678 }
2679
2680 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2681 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2684 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2685 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002686 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002687 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2688}
2689
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002690void ASTWriter::WriteComments() {
2691 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002692 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002693 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002694 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2695 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002696 I != E; ++I) {
2697 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002698 AddSourceRange((*I)->getSourceRange(), Record);
2699 Record.push_back((*I)->getKind());
2700 Record.push_back((*I)->isTrailingComment());
2701 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002702 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2703 }
2704 Stream.ExitBlock();
2705}
2706
Douglas Gregorc5046832009-04-27 18:38:38 +00002707//===----------------------------------------------------------------------===//
2708// Global Method Pool and Selector Serialization
2709//===----------------------------------------------------------------------===//
2710
Douglas Gregore84a9da2009-04-20 20:36:09 +00002711namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002712// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002713class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002714 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002715
2716public:
2717 typedef Selector key_type;
2718 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002719
Sebastian Redl834bb972010-08-04 17:20:04 +00002720 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002721 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002722 ObjCMethodList Instance, Factory;
2723 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002724 typedef const data_type& data_type_ref;
2725
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002726 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002727
Douglas Gregorc78d3462009-04-24 21:10:55 +00002728 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002729 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002730 }
Mike Stump11289f42009-09-09 15:08:12 +00002731
2732 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002733 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002734 data_type_ref Methods) {
2735 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2736 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002737 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2738 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002739 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002740 if (Method->Method)
2741 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002742 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002743 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002744 if (Method->Method)
2745 DataLen += 4;
2746 clang::io::Emit16(Out, DataLen);
2747 return std::make_pair(KeyLen, DataLen);
2748 }
Mike Stump11289f42009-09-09 15:08:12 +00002749
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002750 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002751 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002752 assert((Start >> 32) == 0 && "Selector key offset too large");
2753 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002754 unsigned N = Sel.getNumArgs();
2755 clang::io::Emit16(Out, N);
2756 if (N == 0)
2757 N = 1;
2758 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002759 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002760 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2761 }
Mike Stump11289f42009-09-09 15:08:12 +00002762
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002763 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002764 data_type_ref Methods, unsigned DataLen) {
2765 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002766 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002767 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002768 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002769 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002770 if (Method->Method)
2771 ++NumInstanceMethods;
2772
2773 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002774 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002775 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002776 if (Method->Method)
2777 ++NumFactoryMethods;
2778
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002779 unsigned InstanceBits = Methods.Instance.getBits();
2780 assert(InstanceBits < 4);
2781 unsigned NumInstanceMethodsAndBits =
2782 (NumInstanceMethods << 2) | InstanceBits;
2783 unsigned FactoryBits = Methods.Factory.getBits();
2784 assert(FactoryBits < 4);
2785 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2786 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2787 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl834bb972010-08-04 17:20:04 +00002788 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002789 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002790 if (Method->Method)
2791 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002792 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002793 Method = Method->getNext())
Douglas Gregorc78d3462009-04-24 21:10:55 +00002794 if (Method->Method)
2795 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002796
2797 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002798 }
2799};
2800} // end anonymous namespace
2801
Sebastian Redla19a67f2010-08-03 21:58:15 +00002802/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002803///
2804/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002805/// in an on-disk hash table indexed by the selector. The hash table also
2806/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002807void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002808 using namespace llvm;
2809
Sebastian Redla19a67f2010-08-03 21:58:15 +00002810 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002811 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002812 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002813 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002814 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002815 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002816 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002817 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002818
Sebastian Redla19a67f2010-08-03 21:58:15 +00002819 // Create the on-disk hash table representation. We walk through every
2820 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002821 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002822 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002823 I = SelectorIDs.begin(), E = SelectorIDs.end();
2824 I != E; ++I) {
2825 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002826 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002827 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002828 I->second,
2829 ObjCMethodList(),
2830 ObjCMethodList()
2831 };
2832 if (F != SemaRef.MethodPool.end()) {
2833 Data.Instance = F->second.first;
2834 Data.Factory = F->second.second;
2835 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002836 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002837 // changed.
2838 if (Chain && I->second < FirstSelectorID) {
2839 // Selector already exists. Did it change?
2840 bool changed = false;
2841 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002842 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002843 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002844 changed = true;
2845 }
2846 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002847 M = M->getNext()) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002848 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002849 changed = true;
2850 }
2851 if (!changed)
2852 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002853 } else if (Data.Instance.Method || Data.Factory.Method) {
2854 // A new method pool entry.
2855 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002856 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002857 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002858 }
2859
Douglas Gregorc78d3462009-04-24 21:10:55 +00002860 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002861 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002862 uint32_t BucketOffset;
2863 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002864 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002865 llvm::raw_svector_ostream Out(MethodPool);
2866 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002867 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002868 BucketOffset = Generator.Emit(Out, Trait);
2869 }
2870
2871 // Create a blob abbreviation
2872 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002873 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002874 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2877 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2878
Douglas Gregor95c13f52009-04-25 17:48:32 +00002879 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002880 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002881 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002882 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002883 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002884 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002885
2886 // Create a blob abbreviation for the selector table offsets.
2887 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002888 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002889 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002890 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002891 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2892 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2893
2894 // Write the selector offsets table.
2895 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002896 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002897 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002898 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002899 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002900 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002901 }
2902}
2903
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002904/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002905void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002906 using namespace llvm;
2907 if (SemaRef.ReferencedSelectors.empty())
2908 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002909
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002910 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002911
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002912 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002913 // very tricky to fix, and given that @selector shouldn't really appear in
2914 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002915 for (DenseMap<Selector, SourceLocation>::iterator S =
2916 SemaRef.ReferencedSelectors.begin(),
2917 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2918 Selector Sel = (*S).first;
2919 SourceLocation Loc = (*S).second;
2920 AddSelectorRef(Sel, Record);
2921 AddSourceLocation(Loc, Record);
2922 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002923 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002924}
2925
Douglas Gregorc5046832009-04-27 18:38:38 +00002926//===----------------------------------------------------------------------===//
2927// Identifier Table Serialization
2928//===----------------------------------------------------------------------===//
2929
Douglas Gregorc78d3462009-04-24 21:10:55 +00002930namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002931class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002932 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002933 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002934 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002935 bool IsModule;
2936
Douglas Gregor1d583f22009-04-28 21:18:29 +00002937 /// \brief Determines whether this is an "interesting" identifier
2938 /// that needs a full IdentifierInfo structure written into the hash
2939 /// table.
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002940 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002941 if (II->isPoisoned() ||
2942 II->isExtensionToken() ||
2943 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002944 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002945 II->getFETokenInfo<void>())
2946 return true;
2947
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002948 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002949 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002950
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00002951 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002952 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002953 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002954
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002955 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2956 if (!IsModule)
2957 return !shouldIgnoreMacro(Macro, IsModule, PP);
2958 SubmoduleID ModID;
2959 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2960 return true;
2961 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002962
2963 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002964 }
2965
Richard Smith49f906a2014-03-01 00:08:04 +00002966 typedef llvm::SmallVectorImpl<SubmoduleID> OverriddenList;
2967
2968 MacroDirective *
2969 getFirstPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002970 ModID = 0;
Richard Smith49f906a2014-03-01 00:08:04 +00002971 llvm::SmallVector<SubmoduleID, 1> Overridden;
2972 if (MacroDirective *NextMD = getPublicSubmoduleMacro(MD, ModID, Overridden))
2973 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2974 return NextMD;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00002975 return 0;
2976 }
2977
Richard Smith49f906a2014-03-01 00:08:04 +00002978 MacroDirective *
2979 getNextPublicSubmoduleMacro(MacroDirective *MD, SubmoduleID &ModID,
2980 OverriddenList &Overridden) {
2981 if (MacroDirective *NextMD =
2982 getPublicSubmoduleMacro(MD->getPrevious(), ModID, Overridden))
2983 if (!shouldIgnoreMacro(NextMD, IsModule, PP))
2984 return NextMD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002985 return 0;
2986 }
2987
2988 /// \brief Traverses the macro directives history and returns the latest
Richard Smith49f906a2014-03-01 00:08:04 +00002989 /// public macro definition or undefinition that is not in ModID.
2990 /// A macro that is defined in submodule A and undefined in submodule B
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00002991 /// will still be considered as defined/exported from submodule A.
Richard Smith49f906a2014-03-01 00:08:04 +00002992 /// ModID is updated to the module containing the returned directive.
2993 ///
2994 /// FIXME: This process breaks down if a module defines a macro, imports
2995 /// another submodule that changes the macro, then changes the
2996 /// macro again itself.
2997 MacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2998 SubmoduleID &ModID,
2999 OverriddenList &Overridden) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003000 if (!MD)
3001 return 0;
3002
Richard Smith49f906a2014-03-01 00:08:04 +00003003 Overridden.clear();
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003004 SubmoduleID OrigModID = ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003005 Optional<bool> IsPublic;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003006 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003007 SubmoduleID ThisModID = getSubmoduleID(MD);
3008 if (ThisModID == 0) {
Richard Smith49f906a2014-03-01 00:08:04 +00003009 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003010 continue;
3011 }
Richard Smith49f906a2014-03-01 00:08:04 +00003012 if (ThisModID != ModID) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003013 ModID = ThisModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003014 IsPublic = Optional<bool>();
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003015 }
Richard Smith49f906a2014-03-01 00:08:04 +00003016
3017 // If this is a definition from a submodule import, that submodule's
3018 // definition is overridden by the definition or undefinition that we
3019 // started with.
3020 // FIXME: This should only apply to macros defined in OrigModID.
3021 // We can't do that currently, because a #include of a different submodule
3022 // of the same module just leaks through macros instead of providing new
3023 // DefMacroDirectives for them.
Richard Smith9d100862014-03-06 03:16:27 +00003024 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3025 // Figure out which submodule the macro was originally defined within.
3026 SubmoduleID SourceID = DefMD->getInfo()->getOwningModuleID();
3027 if (!SourceID) {
3028 SourceLocation DefLoc = DefMD->getInfo()->getDefinitionLoc();
3029 if (DefLoc == MD->getLocation())
3030 SourceID = ThisModID;
3031 else
3032 SourceID = Writer.inferSubmoduleIDFromLocation(DefLoc);
3033 }
3034 if (SourceID != OrigModID)
Richard Smith49f906a2014-03-01 00:08:04 +00003035 Overridden.push_back(SourceID);
Richard Smith9d100862014-03-06 03:16:27 +00003036 }
Richard Smith49f906a2014-03-01 00:08:04 +00003037
Argyrios Kyrtzidis3e612b42013-04-03 05:11:33 +00003038 // We are looking for a definition in a different submodule than the one
3039 // that we started with. If a submodule has re-definitions of the same
3040 // macro, only the last definition will be used as the "exported" one.
3041 if (ModID == OrigModID)
3042 continue;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003043
Richard Smith49f906a2014-03-01 00:08:04 +00003044 // The latest visibility directive for a name in a submodule affects all
3045 // the directives that come before it.
3046 if (VisibilityMacroDirective *VisMD =
3047 dyn_cast<VisibilityMacroDirective>(MD)) {
3048 if (!IsPublic.hasValue())
3049 IsPublic = VisMD->isPublic();
3050 } else if (!IsPublic.hasValue() || IsPublic.getValue()) {
3051 // FIXME: If we find an imported macro, we should include its list of
3052 // overrides in our export.
3053 return MD;
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003054 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003055 }
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003056
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003057 return 0;
3058 }
3059
3060 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +00003061 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003062 }
3063
Douglas Gregore84a9da2009-04-20 20:36:09 +00003064public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003065 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003066 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003067
Sebastian Redl539c5062010-08-18 23:57:32 +00003068 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003069 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00003070
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003071 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3072 IdentifierResolver &IdResolver, bool IsModule)
3073 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00003074
3075 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00003076 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00003077 }
Mike Stump11289f42009-09-09 15:08:12 +00003078
3079 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003080 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003081 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00003082 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003083 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003084 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003085 DataLen += 2; // 2 bytes for builtin ID
3086 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003087 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003088 DataLen += 4; // MacroDirectives offset.
3089 if (IsModule) {
3090 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003091 llvm::SmallVector<SubmoduleID, 4> Overridden;
3092 for (MacroDirective *
3093 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3094 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3095 // Previous macro's overrides.
3096 if (!Overridden.empty())
3097 DataLen += 4 * (1 + Overridden.size());
3098 DataLen += 4; // MacroInfo ID or ModuleID.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003099 }
Richard Smith49f906a2014-03-01 00:08:04 +00003100 // Previous macro's overrides.
3101 if (!Overridden.empty())
3102 DataLen += 4 * (1 + Overridden.size());
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003103 DataLen += 4;
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003104 }
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003105 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003106
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003107 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3108 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00003109 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00003110 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00003111 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00003112 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00003113 // We emit the key length after the data length so that every
3114 // string is preceded by a 16-bit length. This matches the PTH
3115 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00003116 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003117 return std::make_pair(KeyLen, DataLen);
3118 }
Mike Stump11289f42009-09-09 15:08:12 +00003119
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003120 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00003121 unsigned KeyLen) {
3122 // Record the location of the key data. This is used when generating
3123 // the mapping from persistent IDs to strings.
3124 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00003125 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003126 }
Mike Stump11289f42009-09-09 15:08:12 +00003127
Richard Smith49f906a2014-03-01 00:08:04 +00003128 static void emitMacroOverrides(raw_ostream &Out,
3129 llvm::ArrayRef<SubmoduleID> Overridden) {
3130 if (!Overridden.empty()) {
3131 clang::io::Emit32(Out, Overridden.size() | 0x80000000U);
3132 for (unsigned I = 0, N = Overridden.size(); I != N; ++I)
3133 clang::io::Emit32(Out, Overridden[I]);
3134 }
3135 }
3136
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003137 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00003138 IdentID ID, unsigned) {
Argyrios Kyrtzidis09c9e812013-02-20 00:54:57 +00003139 MacroDirective *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003140 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00003141 clang::io::Emit32(Out, ID << 1);
3142 return;
3143 }
Douglas Gregorb9256522009-04-28 21:32:13 +00003144
Douglas Gregor1d583f22009-04-28 21:18:29 +00003145 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003146 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3147 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3148 clang::io::Emit16(Out, Bits);
3149 Bits = 0;
3150 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003151 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003152 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003153 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3154 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00003155 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00003156 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00003157 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003158
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003159 if (HadMacroDefinition) {
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003160 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3161 if (IsModule) {
3162 // Write the IDs of macros coming from different submodules.
3163 SubmoduleID ModID;
Richard Smith49f906a2014-03-01 00:08:04 +00003164 llvm::SmallVector<SubmoduleID, 4> Overridden;
3165 for (MacroDirective *
3166 MD = getFirstPublicSubmoduleMacro(Macro, ModID);
3167 MD; MD = getNextPublicSubmoduleMacro(MD, ModID, Overridden)) {
3168 MacroID InfoID = 0;
3169 emitMacroOverrides(Out, Overridden);
3170 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3171 InfoID = Writer.getMacroID(DefMD->getInfo());
3172 assert(InfoID);
3173 clang::io::Emit32(Out, InfoID << 1);
3174 } else {
3175 assert(isa<UndefMacroDirective>(MD));
3176 clang::io::Emit32(Out, (ModID << 1) | 1);
3177 }
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003178 }
Richard Smith49f906a2014-03-01 00:08:04 +00003179 emitMacroOverrides(Out, Overridden);
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00003180 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidis719736c2013-01-19 03:14:56 +00003181 }
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00003182 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00003183
Douglas Gregora868bbd2009-04-21 22:25:48 +00003184 // Emit the declaration IDs in reverse order, because the
3185 // IdentifierResolver provides the declarations as they would be
3186 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003187 // "stat"), but the ASTReader adds declarations to the end of the list
3188 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00003189 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003190 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3191 IdResolver.end());
Craig Topper2341c0d2013-07-04 03:08:24 +00003192 for (SmallVectorImpl<Decl *>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003193 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00003194 D != DEnd; ++D)
Argyrios Kyrtzidise5edbf92013-04-26 21:33:35 +00003195 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3196 }
3197
3198 /// \brief Returns the most recent local decl or the given decl if there are
3199 /// no local ones. The given decl is assumed to be the most recent one.
3200 Decl *getMostRecentLocalDecl(Decl *Orig) {
3201 // The only way a "from AST file" decl would be more recent from a local one
3202 // is if it came from a module.
3203 if (!PP.getLangOpts().Modules)
3204 return Orig;
3205
3206 // Look for a local in the decl chain.
3207 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3208 if (!D->isFromASTFile())
3209 return D;
3210 // If we come up a decl from a (chained-)PCH stop since we won't find a
3211 // local one.
3212 if (D->getOwningModuleID() == 0)
3213 break;
3214 }
3215
3216 return Orig;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003217 }
3218};
3219} // end anonymous namespace
3220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003221/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003222///
3223/// The identifier table consists of a blob containing string data
3224/// (the actual identifiers themselves) and a separate "offsets" index
3225/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003226void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3227 IdentifierResolver &IdResolver,
3228 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003229 using namespace llvm;
3230
3231 // Create and write out the blob that contains the identifier
3232 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003233 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003234 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003235 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00003236
Douglas Gregore6648fb2009-04-28 20:33:11 +00003237 // Look for any identifiers that were named while processing the
3238 // headers, but are otherwise not needed. We add these to the hash
3239 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003240 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00003241 // file.
3242 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3243 IDEnd = PP.getIdentifierTable().end();
3244 ID != IDEnd; ++ID)
3245 getIdentifierRef(ID->second);
3246
Sebastian Redlff4a2952010-07-23 23:49:55 +00003247 // Create the on-disk hash table representation. We only store offsets
3248 // for identifiers that appear here for the first time.
3249 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00003250 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003251 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3252 ID != IDEnd; ++ID) {
3253 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003254 if (!Chain || !ID->first->isFromAST() ||
3255 ID->first->hasChangedSinceDeserialization())
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003256 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00003257 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003258 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003259
Douglas Gregore84a9da2009-04-20 20:36:09 +00003260 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003261 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00003262 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003263 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003264 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00003265 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00003266 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00003267 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003268 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003269 }
3270
3271 // Create a blob abbreviation
3272 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003273 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00003274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00003275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00003276 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003277
3278 // Write the identifier table
3279 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003280 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00003281 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00003282 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003283 }
3284
3285 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00003286 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003287 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00003288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00003290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3291 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3292
Douglas Gregor8d7edce2013-02-08 21:30:59 +00003293#ifndef NDEBUG
3294 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3295 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3296#endif
3297
Douglas Gregor0e149972009-04-25 19:10:14 +00003298 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003299 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00003300 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003301 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00003302 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003303 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003304}
3305
Douglas Gregorc5046832009-04-27 18:38:38 +00003306//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003307// DeclContext's Name Lookup Table Serialization
3308//===----------------------------------------------------------------------===//
3309
3310namespace {
3311// Trait used for the on-disk hash table used in the method pool.
3312class ASTDeclContextNameLookupTrait {
3313 ASTWriter &Writer;
3314
3315public:
3316 typedef DeclarationName key_type;
3317 typedef key_type key_type_ref;
3318
3319 typedef DeclContext::lookup_result data_type;
3320 typedef const data_type& data_type_ref;
3321
3322 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3323
3324 unsigned ComputeHash(DeclarationName Name) {
3325 llvm::FoldingSetNodeID ID;
3326 ID.AddInteger(Name.getNameKind());
3327
3328 switch (Name.getNameKind()) {
3329 case DeclarationName::Identifier:
3330 ID.AddString(Name.getAsIdentifierInfo()->getName());
3331 break;
3332 case DeclarationName::ObjCZeroArgSelector:
3333 case DeclarationName::ObjCOneArgSelector:
3334 case DeclarationName::ObjCMultiArgSelector:
3335 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3336 break;
3337 case DeclarationName::CXXConstructorName:
3338 case DeclarationName::CXXDestructorName:
3339 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003340 break;
3341 case DeclarationName::CXXOperatorName:
3342 ID.AddInteger(Name.getCXXOverloadedOperator());
3343 break;
3344 case DeclarationName::CXXLiteralOperatorName:
3345 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3346 case DeclarationName::CXXUsingDirective:
3347 break;
3348 }
3349
3350 return ID.ComputeHash();
3351 }
3352
3353 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003354 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003355 data_type_ref Lookup) {
3356 unsigned KeyLen = 1;
3357 switch (Name.getNameKind()) {
3358 case DeclarationName::Identifier:
3359 case DeclarationName::ObjCZeroArgSelector:
3360 case DeclarationName::ObjCOneArgSelector:
3361 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362 case DeclarationName::CXXLiteralOperatorName:
3363 KeyLen += 4;
3364 break;
3365 case DeclarationName::CXXOperatorName:
3366 KeyLen += 1;
3367 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003368 case DeclarationName::CXXConstructorName:
3369 case DeclarationName::CXXDestructorName:
3370 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003371 case DeclarationName::CXXUsingDirective:
3372 break;
3373 }
3374 clang::io::Emit16(Out, KeyLen);
3375
3376 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikieff7d47a2012-12-19 00:45:41 +00003377 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003378 clang::io::Emit16(Out, DataLen);
3379
3380 return std::make_pair(KeyLen, DataLen);
3381 }
3382
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003383 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003384 using namespace clang::io;
3385
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003386 Emit8(Out, Name.getNameKind());
3387 switch (Name.getNameKind()) {
3388 case DeclarationName::Identifier:
3389 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003390 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003391 case DeclarationName::ObjCZeroArgSelector:
3392 case DeclarationName::ObjCOneArgSelector:
3393 case DeclarationName::ObjCMultiArgSelector:
3394 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003395 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003396 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003397 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3398 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003399 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00003400 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003401 case DeclarationName::CXXLiteralOperatorName:
3402 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00003403 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003404 case DeclarationName::CXXConstructorName:
3405 case DeclarationName::CXXDestructorName:
3406 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003407 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00003408 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003409 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00003410
3411 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003412 }
3413
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003414 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003415 data_type Lookup, unsigned DataLen) {
3416 uint64_t Start = Out.tell(); (void)Start;
David Blaikieff7d47a2012-12-19 00:45:41 +00003417 clang::io::Emit16(Out, Lookup.size());
3418 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3419 I != E; ++I)
3420 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003421
3422 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3423 }
3424};
3425} // end anonymous namespace
3426
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003427/// \brief Write the block containing all of the declaration IDs
3428/// visible from the given DeclContext.
3429///
3430/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00003431/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003432uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3433 DeclContext *DC) {
3434 if (DC->getPrimaryContext() != DC)
3435 return 0;
3436
3437 // Since there is no name lookup into functions or methods, don't bother to
3438 // build a visible-declarations table for these entities.
3439 if (DC->isFunctionOrMethod())
3440 return 0;
3441
3442 // If not in C++, we perform name lookup for the translation unit via the
3443 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003444 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003445 return 0;
3446
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003447 // Serialize the contents of the mapping used for lookup. Note that,
3448 // although we have two very different code paths, the serialized
3449 // representation is the same for both cases: a declaration name,
3450 // followed by a size, followed by references to the visible
3451 // declarations that have that name.
3452 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00003453 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003454 if (!Map || Map->empty())
3455 return 0;
3456
3457 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3458 ASTDeclContextNameLookupTrait Trait(*this);
3459
3460 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00003461 DeclarationName ConversionName;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003462 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003463 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3464 D != DEnd; ++D) {
3465 DeclarationName Name = D->first;
3466 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikieff7d47a2012-12-19 00:45:41 +00003467 if (!Result.empty()) {
Douglas Gregor05ef9312011-08-30 20:49:19 +00003468 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3469 // Hash all conversion function names to the same name. The actual
3470 // type information in conversion function name is not used in the
3471 // key (since such type information is not stable across different
3472 // modules), so the intended effect is to coalesce all of the conversion
3473 // functions under a single key.
3474 if (!ConversionName)
3475 ConversionName = Name;
David Blaikieff7d47a2012-12-19 00:45:41 +00003476 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregor05ef9312011-08-30 20:49:19 +00003477 continue;
3478 }
3479
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003480 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00003481 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003482 }
3483
Douglas Gregor05ef9312011-08-30 20:49:19 +00003484 // Add the conversion functions
3485 if (!ConversionDecls.empty()) {
3486 Generator.insert(ConversionName,
3487 DeclContext::lookup_result(ConversionDecls.begin(),
3488 ConversionDecls.end()),
3489 Trait);
3490 }
3491
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003492 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003493 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00003494 uint32_t BucketOffset;
3495 {
3496 llvm::raw_svector_ostream Out(LookupTable);
3497 // Make sure that no bucket is at offset 0
3498 clang::io::Emit32(Out, 0);
3499 BucketOffset = Generator.Emit(Out, Trait);
3500 }
3501
3502 // Write the lookup table
3503 RecordData Record;
3504 Record.push_back(DECL_CONTEXT_VISIBLE);
3505 Record.push_back(BucketOffset);
3506 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3507 LookupTable.str());
3508
3509 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3510 ++NumVisibleDeclContexts;
3511 return Offset;
3512}
3513
Sebastian Redla4071b42010-08-24 00:50:09 +00003514/// \brief Write an UPDATE_VISIBLE block for the given context.
3515///
3516/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3517/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003518/// (in C++), for namespaces, and for classes with forward-declared unscoped
3519/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003520void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003521 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3522 if (!Map || Map->empty())
3523 return;
3524
3525 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3526 ASTDeclContextNameLookupTrait Trait(*this);
3527
3528 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003529 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3530 D != DEnd; ++D) {
3531 DeclarationName Name = D->first;
3532 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003533 // For any name that appears in this table, the results are complete, i.e.
3534 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikieff7d47a2012-12-19 00:45:41 +00003535 if (!Result.empty())
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003536 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003537 }
3538
3539 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003540 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003541 uint32_t BucketOffset;
3542 {
3543 llvm::raw_svector_ostream Out(LookupTable);
3544 // Make sure that no bucket is at offset 0
3545 clang::io::Emit32(Out, 0);
3546 BucketOffset = Generator.Emit(Out, Trait);
3547 }
3548
3549 // Write the lookup table
3550 RecordData Record;
3551 Record.push_back(UPDATE_VISIBLE);
3552 Record.push_back(getDeclID(cast<Decl>(DC)));
3553 Record.push_back(BucketOffset);
3554 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3555}
3556
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003557/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3558void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3559 RecordData Record;
3560 Record.push_back(Opts.fp_contract);
3561 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3562}
3563
3564/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3565void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003566 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003567 return;
3568
3569 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3570 RecordData Record;
3571#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3572#include "clang/Basic/OpenCLExtensions.def"
3573 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3574}
3575
Douglas Gregor358cd442012-01-15 16:58:34 +00003576void ASTWriter::WriteRedeclarations() {
3577 RecordData LocalRedeclChains;
3578 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3579
3580 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3581 Decl *First = Redeclarations[I];
Rafael Espindola3f9e4442013-10-19 02:13:21 +00003582 assert(First->isFirstDecl() && "Not the first declaration?");
Douglas Gregor358cd442012-01-15 16:58:34 +00003583
3584 Decl *MostRecent = First->getMostRecentDecl();
3585
3586 // If we only have a single declaration, there is no point in storing
3587 // a redeclaration chain.
3588 if (First == MostRecent)
3589 continue;
3590
3591 unsigned Offset = LocalRedeclChains.size();
3592 unsigned Size = 0;
3593 LocalRedeclChains.push_back(0); // Placeholder for the size.
3594
3595 // Collect the set of local redeclarations of this declaration.
Douglas Gregor6168bd22013-02-18 15:53:43 +00003596 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor358cd442012-01-15 16:58:34 +00003597 Prev = Prev->getPreviousDecl()) {
3598 if (!Prev->isFromASTFile()) {
3599 AddDeclRef(Prev, LocalRedeclChains);
3600 ++Size;
3601 }
3602 }
Douglas Gregor6168bd22013-02-18 15:53:43 +00003603
3604 if (!First->isFromASTFile() && Chain) {
3605 Decl *FirstFromAST = MostRecent;
3606 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3607 if (Prev->isFromASTFile())
3608 FirstFromAST = Prev;
3609 }
3610
3611 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3612 }
3613
Douglas Gregor358cd442012-01-15 16:58:34 +00003614 LocalRedeclChains[Offset] = Size;
3615
3616 // Reverse the set of local redeclarations, so that we store them in
3617 // order (since we found them in reverse order).
3618 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3619
Douglas Gregor6168bd22013-02-18 15:53:43 +00003620 // Add the mapping from the first ID from the AST to the set of local
3621 // declarations.
Douglas Gregor358cd442012-01-15 16:58:34 +00003622 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3623 LocalRedeclsMap.push_back(Info);
3624
3625 assert(N == Redeclarations.size() &&
3626 "Deserialized a declaration we shouldn't have");
3627 }
3628
3629 if (LocalRedeclChains.empty())
3630 return;
3631
3632 // Sort the local redeclarations map by the first declaration ID,
3633 // since the reader will be performing binary searches on this information.
3634 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3635
3636 // Emit the local redeclarations map.
3637 using namespace llvm;
3638 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3639 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3640 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3641 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3642 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3643
3644 RecordData Record;
3645 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3646 Record.push_back(LocalRedeclsMap.size());
3647 Stream.EmitRecordWithBlob(AbbrevID, Record,
3648 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3649 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3650
3651 // Emit the redeclaration chains.
3652 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3653}
3654
Douglas Gregor404cdde2012-01-27 01:47:08 +00003655void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003656 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregor404cdde2012-01-27 01:47:08 +00003657 RecordData Categories;
3658
3659 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3660 unsigned Size = 0;
3661 unsigned StartIndex = Categories.size();
3662
3663 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3664
3665 // Allocate space for the size.
3666 Categories.push_back(0);
3667
3668 // Add the categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003669 for (ObjCInterfaceDecl::known_categories_iterator
3670 Cat = Class->known_categories_begin(),
3671 CatEnd = Class->known_categories_end();
3672 Cat != CatEnd; ++Cat, ++Size) {
3673 assert(getDeclID(*Cat) != 0 && "Bogus category");
3674 AddDeclRef(*Cat, Categories);
Douglas Gregor404cdde2012-01-27 01:47:08 +00003675 }
3676
3677 // Update the size.
3678 Categories[StartIndex] = Size;
3679
3680 // Record this interface -> category map.
3681 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3682 CategoriesMap.push_back(CatInfo);
3683 }
3684
3685 // Sort the categories map by the definition ID, since the reader will be
3686 // performing binary searches on this information.
3687 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3688
3689 // Emit the categories map.
3690 using namespace llvm;
3691 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3692 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3693 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3694 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3695 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3696
3697 RecordData Record;
3698 Record.push_back(OBJC_CATEGORIES_MAP);
3699 Record.push_back(CategoriesMap.size());
3700 Stream.EmitRecordWithBlob(AbbrevID, Record,
3701 reinterpret_cast<char*>(CategoriesMap.data()),
3702 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3703
3704 // Emit the category lists.
3705 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3706}
3707
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003708void ASTWriter::WriteMergedDecls() {
3709 if (!Chain || Chain->MergedDecls.empty())
3710 return;
3711
3712 RecordData Record;
3713 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3714 IEnd = Chain->MergedDecls.end();
3715 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003716 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003717 : getDeclID(I->first);
3718 assert(CanonID && "Merged declaration not known?");
3719
3720 Record.push_back(CanonID);
3721 Record.push_back(I->second.size());
3722 Record.append(I->second.begin(), I->second.end());
3723 }
3724 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3725}
3726
Richard Smithe40f2ba2013-08-07 21:41:30 +00003727void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3728 Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3729
3730 if (LPTMap.empty())
3731 return;
3732
3733 RecordData Record;
3734 for (Sema::LateParsedTemplateMapT::iterator It = LPTMap.begin(),
3735 ItEnd = LPTMap.end();
3736 It != ItEnd; ++It) {
3737 LateParsedTemplate *LPT = It->second;
3738 AddDeclRef(It->first, Record);
3739 AddDeclRef(LPT->D, Record);
3740 Record.push_back(LPT->Toks.size());
3741
3742 for (CachedTokens::iterator TokIt = LPT->Toks.begin(),
3743 TokEnd = LPT->Toks.end();
3744 TokIt != TokEnd; ++TokIt) {
3745 AddToken(*TokIt, Record);
3746 }
3747 }
3748 Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3749}
3750
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003751//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003752// General Serialization Routines
3753//===----------------------------------------------------------------------===//
3754
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003755/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003756void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3757 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003758 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003759 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3760 e = Attrs.end(); i != e; ++i){
3761 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003762 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003763 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003764
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003765#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003766
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003767 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003768}
3769
John McCallf413f5e2013-05-03 00:10:13 +00003770void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3771 AddSourceLocation(Tok.getLocation(), Record);
3772 Record.push_back(Tok.getLength());
3773
3774 // FIXME: When reading literal tokens, reconstruct the literal pointer
3775 // if it is needed.
3776 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3777 // FIXME: Should translate token kind to a stable encoding.
3778 Record.push_back(Tok.getKind());
3779 // FIXME: Should translate token flags to a stable encoding.
3780 Record.push_back(Tok.getFlags());
3781}
3782
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003783void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003784 Record.push_back(Str.size());
3785 Record.insert(Record.end(), Str.begin(), Str.end());
3786}
3787
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003788void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3789 RecordDataImpl &Record) {
3790 Record.push_back(Version.getMajor());
David Blaikie05785d12013-02-20 22:23:23 +00003791 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003792 Record.push_back(*Minor + 1);
3793 else
3794 Record.push_back(0);
David Blaikie05785d12013-02-20 22:23:23 +00003795 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003796 Record.push_back(*Subminor + 1);
3797 else
3798 Record.push_back(0);
3799}
3800
Douglas Gregore84a9da2009-04-20 20:36:09 +00003801/// \brief Note that the identifier II occurs at the given offset
3802/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003803void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003804 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003805 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003806 // up earlier in the chain and thus don't need an offset.
3807 if (ID >= FirstIdentID)
3808 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003809}
3810
Douglas Gregor95c13f52009-04-25 17:48:32 +00003811/// \brief Note that the selector Sel occurs at the given offset
3812/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003813void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003814 unsigned ID = SelectorIDs[Sel];
3815 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003816 // Don't record offsets for selectors that are also available in a different
3817 // file.
3818 if (ID < FirstSelectorID)
3819 return;
3820 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003821}
3822
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003823ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003824 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003825 WritingAST(false), DoneWritingDeclsAndTypes(false),
3826 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003827 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003828 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003829 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3830 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003831 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3832 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003833 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003834 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003835 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003836 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003837 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003838 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003839 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3840 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3841 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003842 DeclTypedefAbbrev(0),
3843 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3844 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003845{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003846}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003847
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003848ASTWriter::~ASTWriter() {
Reid Kleckner588c9372014-02-19 23:44:52 +00003849 llvm::DeleteContainerSeconds(FileDeclIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003850}
3851
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003852void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003853 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003854 Module *WritingModule, StringRef isysroot,
3855 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003856 WritingAST = true;
3857
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003858 ASTHasCompilerErrors = hasErrors;
3859
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003860 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003861 Stream.Emit((unsigned)'C', 8);
3862 Stream.Emit((unsigned)'P', 8);
3863 Stream.Emit((unsigned)'C', 8);
3864 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003865
Chris Lattner28fa4e62009-04-26 22:26:21 +00003866 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003867
Douglas Gregoreda8e122011-08-09 15:13:55 +00003868 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003869 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003870 this->WritingModule = WritingModule;
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003871 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003872 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003873 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003874 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003875
3876 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003877}
3878
Douglas Gregora94a1542011-07-27 21:45:57 +00003879template<typename Vector>
3880static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3881 ASTWriter::RecordData &Record) {
3882 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3883 I != E; ++I) {
3884 Writer.AddDeclRef(*I, Record);
3885 }
3886}
3887
Argyrios Kyrtzidisd7c16b22012-10-31 20:59:50 +00003888void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003889 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003890 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003891 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003892 using namespace llvm;
3893
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003894 bool isModule = WritingModule != 0;
3895
Douglas Gregorcf68c582011-12-01 22:20:10 +00003896 // Make sure that the AST reader knows to finalize itself.
3897 if (Chain)
3898 Chain->finalizeForWriting();
3899
Sebastian Redl143413f2010-07-12 22:02:52 +00003900 ASTContext &Context = SemaRef.Context;
3901 Preprocessor &PP = SemaRef.PP;
3902
Douglas Gregordab42432011-08-12 00:15:20 +00003903 // Set up predefined declaration IDs.
3904 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003905 if (Context.ObjCIdDecl)
3906 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003907 if (Context.ObjCSelDecl)
3908 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003909 if (Context.ObjCClassDecl)
3910 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003911 if (Context.ObjCProtocolClassDecl)
3912 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003913 if (Context.Int128Decl)
3914 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3915 if (Context.UInt128Decl)
3916 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003917 if (Context.ObjCInstanceTypeDecl)
3918 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003919 if (Context.BuiltinVaListDecl)
3920 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3921
Douglas Gregor851443c2011-08-12 01:39:19 +00003922 if (!Chain) {
3923 // Make sure that we emit IdentifierInfos (and any attached
3924 // declarations) for builtins. We don't need to do this when we're
3925 // emitting chained PCH files, because all of the builtins will be
3926 // in the original PCH file.
3927 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003928 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003929 SmallVector<const char *, 32> BuiltinNames;
Eli Benderskye3cef2a2013-07-11 16:53:04 +00003930 if (!Context.getLangOpts().NoBuiltin) {
3931 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames);
3932 }
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003933 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3934 getIdentifierRef(&Table.get(BuiltinNames[I]));
3935 }
3936
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003937 // If there are any out-of-date identifiers, bring them up to date.
3938 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregore68cf272013-01-07 16:56:53 +00003939 // Find out-of-date identifiers.
3940 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003941 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3942 IDEnd = PP.getIdentifierTable().end();
Douglas Gregore68cf272013-01-07 16:56:53 +00003943 ID != IDEnd; ++ID) {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003944 if (ID->second->isOutOfDate())
Douglas Gregore68cf272013-01-07 16:56:53 +00003945 OutOfDate.push_back(ID->second);
3946 }
3947
3948 // Update the out-of-date identifiers.
3949 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3950 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3951 }
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003952 }
3953
Chris Lattner0c797362009-09-08 18:19:27 +00003954 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003955 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003956 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003957 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003958 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003959
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003960 // Build a record containing all of the file scoped decls in this file.
3961 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidis59852362013-03-14 04:45:00 +00003962 if (!isModule)
3963 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3964 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003965
Douglas Gregor851443c2011-08-12 01:39:19 +00003966 // Build a record containing all of the delegating constructors we still need
3967 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003968 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00003969 if (!isModule)
3970 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003971
Douglas Gregor851443c2011-08-12 01:39:19 +00003972 // Write the set of weak, undeclared identifiers. We always write the
3973 // entire table, since later PCH files in a PCH chain are only interested in
3974 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003975 RecordData WeakUndeclaredIdentifiers;
3976 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003977 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003978 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3979 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3980 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3981 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3982 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3983 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3984 }
3985 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003986
Richard Smith78165b52013-01-10 23:43:47 +00003987 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003988 // declarations in this header file. Generally, this record will be
3989 // empty.
Richard Smith78165b52013-01-10 23:43:47 +00003990 RecordData LocallyScopedExternCDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003991 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003992 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003993 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith78165b52013-01-10 23:43:47 +00003994 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3995 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003996 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003997 if (!TD->second->isFromASTFile())
Richard Smith78165b52013-01-10 23:43:47 +00003998 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregordc5c9582011-07-28 14:20:37 +00003999 }
4000
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004001 // Build a record containing all of the ext_vector declarations.
4002 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00004003 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004004
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004005 // Build a record containing all of the VTable uses information.
4006 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004007 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00004008 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4009 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4010 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4011 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4012 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004013 }
4014
4015 // Build a record containing all of dynamic classes declarations.
4016 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00004017 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004018
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004019 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004020 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004021 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00004022 I = SemaRef.PendingInstantiations.begin(),
4023 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
4024 AddDeclRef(I->first, PendingInstantiations);
4025 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004026 }
4027 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4028 "There are local ones at end of translation unit!");
4029
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004030 // Build a record containing some declaration references.
4031 RecordData SemaDeclRefs;
4032 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4033 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4034 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4035 }
4036
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004037 RecordData CUDASpecialDeclRefs;
4038 if (Context.getcudaConfigureCallDecl()) {
4039 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4040 }
4041
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004042 // Build a record containing all of the known namespaces.
4043 RecordData KnownNamespaces;
Nick Lewycky8334af82013-01-26 00:35:08 +00004044 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004045 I = SemaRef.KnownNamespaces.begin(),
4046 IEnd = SemaRef.KnownNamespaces.end();
4047 I != IEnd; ++I) {
4048 if (!I->second)
4049 AddDeclRef(I->first, KnownNamespaces);
4050 }
Douglas Gregor112b9072012-10-18 05:31:06 +00004051
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004052 // Build a record of all used, undefined objects that require definitions.
4053 RecordData UndefinedButUsed;
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004054
4055 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004056 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewyckyf0f56162013-01-31 03:23:57 +00004057 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
4058 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004059 AddDeclRef(I->first, UndefinedButUsed);
4060 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky8334af82013-01-26 00:35:08 +00004061 }
4062
Douglas Gregor112b9072012-10-18 05:31:06 +00004063 // Write the control block
Douglas Gregor2d302362012-10-24 16:50:34 +00004064 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor112b9072012-10-18 05:31:06 +00004065
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00004066 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00004067 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00004068 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor851443c2011-08-12 01:39:19 +00004069
Argyrios Kyrtzidis39605402012-12-13 21:38:23 +00004070 // This is so that older clang versions, before the introduction
4071 // of the control block, can read and reject the newer PCH format.
4072 Record.clear();
4073 Record.push_back(VERSION_MAJOR);
4074 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4075
Douglas Gregor851443c2011-08-12 01:39:19 +00004076 // Create a lexical update block containing all of the declarations in the
4077 // translation unit that do not come from other AST files.
4078 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4079 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
4080 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
4081 E = TU->noload_decls_end();
4082 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00004083 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00004084 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00004085 }
4086
4087 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
4088 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4089 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4090 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4091 Record.clear();
4092 Record.push_back(TU_UPDATE_LEXICAL);
4093 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4094 data(NewGlobalDecls));
4095
4096 // And a visible updates block for the translation unit.
4097 Abv = new llvm::BitCodeAbbrev();
4098 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4099 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4100 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4101 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4102 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4103 WriteDeclContextVisibleUpdate(TU);
4104
4105 // If the translation unit has an anonymous namespace, and we don't already
4106 // have an update block for it, write it as an update block.
4107 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4108 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4109 if (Record.empty()) {
4110 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004111 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00004112 }
4113 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004114
4115 // Make sure visible decls, added to DeclContexts previously loaded from
4116 // an AST file, are registered for serialization.
Craig Topper2341c0d2013-07-04 03:08:24 +00004117 for (SmallVectorImpl<const Decl *>::iterator
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004118 I = UpdatingVisibleDecls.begin(),
4119 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4120 GetDeclRef(*I);
4121 }
4122
Argyrios Kyrtzidisacfbbd72013-08-07 21:17:33 +00004123 // Make sure all decls associated with an identifier are registered for
4124 // serialization.
4125 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
4126 IDEnd = PP.getIdentifierTable().end();
4127 ID != IDEnd; ++ID) {
4128 const IdentifierInfo *II = ID->second;
4129 if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
4130 for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4131 DEnd = SemaRef.IdResolver.end();
4132 D != DEnd; ++D) {
4133 GetDeclRef(*D);
4134 }
4135 }
4136 }
4137
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00004138 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004139 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004140
Douglas Gregor5204bde2011-08-02 16:26:37 +00004141 // Form the record of special types.
4142 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00004143 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004144 AddTypeRef(Context.getFILEType(), SpecialTypes);
4145 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4146 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4147 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4148 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00004149 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00004150 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00004151
Douglas Gregor1970d882009-04-26 03:49:13 +00004152 // Keep writing types and declarations until all types and
4153 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00004154 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004155 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00004156 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4157 E = DeclsToRewrite.end();
4158 I != E; ++I)
4159 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00004160 while (!DeclTypesToEmit.empty()) {
4161 DeclOrType DOT = DeclTypesToEmit.front();
4162 DeclTypesToEmit.pop();
4163 if (DOT.isType())
4164 WriteType(DOT.getType());
4165 else
4166 WriteDecl(Context, DOT.getDecl());
4167 }
4168 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004169
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004170 DoneWritingDeclsAndTypes = true;
4171
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004172 WriteFileDeclIDsMap();
4173 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00004174 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004175
4176 if (Chain) {
4177 // Write the mapping information describing our module dependencies and how
4178 // each of those modules were mapped into our own offset/ID space, so that
4179 // the reader can build the appropriate mapping to its own offset/ID space.
4180 // The map consists solely of a blob with the following format:
4181 // *(module-name-len:i16 module-name:len*i8
4182 // source-location-offset:i32
4183 // identifier-id:i32
4184 // preprocessed-entity-id:i32
4185 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00004186 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004187 // selector-id:i32
4188 // declaration-id:i32
4189 // c++-base-specifiers-id:i32
4190 // type-id:i32)
4191 //
4192 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4193 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4195 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004196 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004197 {
4198 llvm::raw_svector_ostream Out(Buffer);
4199 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00004200 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004201 M != MEnd; ++M) {
4202 StringRef FileName = (*M)->FileName;
4203 io::Emit16(Out, FileName.size());
4204 Out.write(FileName.data(), FileName.size());
4205 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4206 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004207 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004208 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00004209 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004210 io::Emit32(Out, (*M)->BaseSelectorID);
4211 io::Emit32(Out, (*M)->BaseDeclID);
4212 io::Emit32(Out, (*M)->BaseTypeIndex);
4213 }
4214 }
4215 Record.clear();
4216 Record.push_back(MODULE_OFFSET_MAP);
4217 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4218 Buffer.data(), Buffer.size());
4219 }
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004220 WritePreprocessor(PP, isModule);
Douglas Gregor09b69892011-02-10 17:09:37 +00004221 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00004222 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00004223 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidisffb35582013-03-14 04:44:56 +00004224 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00004225 WriteFPPragmaOptions(SemaRef.getFPOptions());
4226 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00004227
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004228 WriteTypeDeclOffsets();
Argyrios Kyrtzidis0f06b982013-03-27 17:17:23 +00004229 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregor652d82a2009-04-18 05:55:16 +00004230
Anders Carlsson9bb83e82011-03-06 18:41:18 +00004231 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004232
Douglas Gregora89c5ac2011-12-06 01:10:29 +00004233 // If we're emitting a module, write out the submodule information.
4234 if (WritingModule)
4235 WriteSubmodules(WritingModule);
4236
Douglas Gregor5204bde2011-08-02 16:26:37 +00004237 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4238
Douglas Gregord4df8652009-04-22 22:02:47 +00004239 // Write the record containing external, unnamed definitions.
Ben Langmuir332aafe2014-01-31 01:06:56 +00004240 if (!EagerlyDeserializedDecls.empty())
4241 Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
Douglas Gregord4df8652009-04-22 22:02:47 +00004242
4243 // Write the record containing tentative definitions.
4244 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004245 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00004246
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00004247 // Write the record containing unused file scoped decls.
4248 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004249 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004250
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004251 // Write the record containing weak undeclared identifiers.
4252 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004253 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00004254 WeakUndeclaredIdentifiers);
4255
Richard Smith78165b52013-01-10 23:43:47 +00004256 // Write the record containing locally-scoped extern "C" definitions.
4257 if (!LocallyScopedExternCDecls.empty())
4258 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4259 LocallyScopedExternCDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00004260
4261 // Write the record containing ext_vector type names.
4262 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004263 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00004264
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004265 // Write the record containing VTable uses information.
4266 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004267 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004268
4269 // Write the record containing dynamic classes declarations.
4270 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004271 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00004272
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004273 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00004274 if (!PendingInstantiations.empty())
4275 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00004276
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004277 // Write the record containing declaration references of Sema.
4278 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00004279 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00004280
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004281 // Write the record containing CUDA-specific declaration references.
4282 if (!CUDASpecialDeclRefs.empty())
4283 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00004284
4285 // Write the delegating constructors.
4286 if (!DelegatingCtorDecls.empty())
4287 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00004288
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004289 // Write the known namespaces.
4290 if (!KnownNamespaces.empty())
4291 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky8334af82013-01-26 00:35:08 +00004292
Nick Lewycky9c7eb1d2013-02-01 08:13:20 +00004293 // Write the undefined internal functions and variables, and inline functions.
4294 if (!UndefinedButUsed.empty())
4295 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregorc2fa1692011-06-28 16:20:02 +00004296
Douglas Gregor851443c2011-08-12 01:39:19 +00004297 // Write the visible updates to DeclContexts.
4298 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4299 I = UpdatedDeclContexts.begin(),
4300 E = UpdatedDeclContexts.end();
4301 I != E; ++I)
4302 WriteDeclContextVisibleUpdate(*I);
4303
Douglas Gregor959bb062011-12-03 01:15:29 +00004304 if (!WritingModule) {
4305 // Write the submodules that were imported, if any.
4306 RecordData ImportedModules;
4307 for (ASTContext::import_iterator I = Context.local_import_begin(),
4308 IEnd = Context.local_import_end();
4309 I != IEnd; ++I) {
4310 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4311 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4312 }
4313 if (!ImportedModules.empty()) {
4314 // Sort module IDs.
4315 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4316
4317 // Unique module IDs.
4318 ImportedModules.erase(std::unique(ImportedModules.begin(),
4319 ImportedModules.end()),
4320 ImportedModules.end());
4321
4322 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4323 }
Douglas Gregor0a839132011-12-03 00:59:55 +00004324 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004325
Douglas Gregordab42432011-08-12 00:15:20 +00004326 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00004327 WriteDeclReplacementsBlock();
Douglas Gregor358cd442012-01-15 16:58:34 +00004328 WriteRedeclarations();
Douglas Gregor6168bd22013-02-18 15:53:43 +00004329 WriteMergedDecls();
Douglas Gregor404cdde2012-01-27 01:47:08 +00004330 WriteObjCCategories();
Richard Smithe40f2ba2013-08-07 21:41:30 +00004331 WriteLateParsedTemplates(SemaRef);
4332
Douglas Gregor08f01292009-04-17 22:13:46 +00004333 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00004334 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00004335 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00004336 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00004337 Record.push_back(NumLexicalDeclContexts);
4338 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00004339 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00004340 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004341}
4342
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004343/// \brief Go through the declaration update blocks and resolve declaration
4344/// pointers into declaration IDs.
4345void ASTWriter::ResolveDeclUpdatesBlocks() {
4346 for (DeclUpdateMap::iterator
4347 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4348 const Decl *D = I->first;
4349 UpdateRecord &URec = I->second;
4350
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004351 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004352 continue; // The decl will be written completely
4353
4354 unsigned Idx = 0, N = URec.size();
4355 while (Idx < N) {
4356 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004357 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4358 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4359 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4360 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4361 ++Idx;
4362 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004363
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004364 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
Eli Friedman276dd182013-09-05 00:02:25 +00004365 case UPD_DECL_MARKED_USED:
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004366 ++Idx;
4367 break;
Richard Smith1fa5d642013-05-11 05:45:24 +00004368
4369 case UPD_CXX_DEDUCED_RETURN_TYPE:
4370 URec[Idx] = GetOrCreateTypeID(
4371 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4372 ++Idx;
4373 break;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004374 }
4375 }
4376 }
4377}
4378
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004379void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004380 if (DeclUpdates.empty())
4381 return;
4382
4383 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00004384 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004385 for (DeclUpdateMap::iterator
4386 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4387 const Decl *D = I->first;
4388 UpdateRecord &URec = I->second;
4389
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004390 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00004391 continue; // The decl will be written completely,no need to store updates.
4392
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004393 uint64_t Offset = Stream.GetCurrentBitNo();
4394 Stream.EmitRecord(DECL_UPDATES, URec);
4395
4396 OffsetsRecord.push_back(GetDeclRef(D));
4397 OffsetsRecord.push_back(Offset);
4398 }
4399 Stream.ExitBlock();
4400 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4401}
4402
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00004403void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004404 if (ReplacedDecls.empty())
4405 return;
4406
4407 RecordData Record;
Craig Topper2341c0d2013-07-04 03:08:24 +00004408 for (SmallVectorImpl<ReplacedDeclInfo>::iterator
4409 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00004410 Record.push_back(I->ID);
4411 Record.push_back(I->Offset);
4412 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004413 }
Sebastian Redl539c5062010-08-18 23:57:32 +00004414 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00004415}
4416
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004417void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004418 Record.push_back(Loc.getRawEncoding());
4419}
4420
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004421void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004422 AddSourceLocation(Range.getBegin(), Record);
4423 AddSourceLocation(Range.getEnd(), Record);
4424}
4425
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004426void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004427 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00004428 const uint64_t *Words = Value.getRawData();
4429 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004430}
4431
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004432void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00004433 Record.push_back(Value.isUnsigned());
4434 AddAPInt(Value, Record);
4435}
4436
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004437void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00004438 AddAPInt(Value.bitcastToAPInt(), Record);
4439}
4440
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004441void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004442 Record.push_back(getIdentifierRef(II));
4443}
4444
Sebastian Redl539c5062010-08-18 23:57:32 +00004445IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004446 if (II == 0)
4447 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004448
Sebastian Redl539c5062010-08-18 23:57:32 +00004449 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00004450 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00004451 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00004452 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004453}
4454
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004455MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004456 // Don't emit builtin macros like __LINE__ to the AST file unless they
4457 // have been redefined by the header (in which case they are not
4458 // isBuiltinMacro).
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004459 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004460 return 0;
4461
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004462 MacroID &ID = MacroIDs[MI];
4463 if (ID == 0) {
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004464 ID = NextMacroID++;
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004465 MacroInfoToEmitData Info = { Name, MI, ID };
4466 MacroInfosToEmit.push_back(Info);
4467 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004468 return ID;
4469}
4470
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00004471MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4472 if (MI == 0 || MI->isBuiltinMacro())
4473 return 0;
4474
4475 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4476 return MacroIDs[MI];
4477}
4478
4479uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4480 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4481 return IdentMacroDirectivesOffsetMap[Name];
4482}
4483
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004484void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004485 Record.push_back(getSelectorRef(SelRef));
4486}
4487
Sebastian Redl539c5062010-08-18 23:57:32 +00004488SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004489 if (Sel.getAsOpaquePtr() == 0) {
4490 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00004491 }
4492
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004493 SelectorID SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004494 if (SID == 0 && Chain) {
4495 // This might trigger a ReadSelector callback, which will set the ID for
4496 // this selector.
4497 Chain->LoadSelector(Sel);
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004498 SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00004499 }
Steve Naroff2ddea052009-04-23 10:39:46 +00004500 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00004501 SID = NextSelectorID++;
Douglas Gregor8d7edce2013-02-08 21:30:59 +00004502 SelectorIDs[Sel] = SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004503 }
Sebastian Redl834bb972010-08-04 17:20:04 +00004504 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00004505}
4506
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004507void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00004508 AddDeclRef(Temp->getDestructor(), Record);
4509}
4510
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004511void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4512 CXXBaseSpecifier const *BasesEnd,
4513 RecordDataImpl &Record) {
4514 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4515 CXXBaseSpecifiersToWrite.push_back(
4516 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4517 Bases, BasesEnd));
4518 Record.push_back(NextCXXBaseSpecifiersID++);
4519}
4520
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004521void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004522 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004523 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004524 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00004525 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004526 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00004527 break;
4528 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004529 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00004530 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004531 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00004532 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004533 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004534 break;
4535 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00004536 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004537 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00004538 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004539 break;
John McCall0ad16662009-10-29 08:12:44 +00004540 case TemplateArgument::Null:
4541 case TemplateArgument::Integral:
4542 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00004543 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00004544 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00004545 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00004546 break;
4547 }
4548}
4549
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004550void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004551 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004552 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00004553
4554 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4555 bool InfoHasSameExpr
4556 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4557 Record.push_back(InfoHasSameExpr);
4558 if (InfoHasSameExpr)
4559 return; // Avoid storing the same expr twice.
4560 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00004561 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4562 Record);
4563}
4564
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004565void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4566 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00004567 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00004568 AddTypeRef(QualType(), Record);
4569 return;
4570 }
4571
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004572 AddTypeLoc(TInfo->getTypeLoc(), Record);
4573}
4574
4575void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4576 AddTypeRef(TL.getType(), Record);
4577
John McCall8f115c62009-10-16 21:56:05 +00004578 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004579 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00004580 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00004581}
4582
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004583void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00004584 Record.push_back(GetOrCreateTypeID(T));
4585}
4586
Douglas Gregoreda8e122011-08-09 15:13:55 +00004587TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith1fa5d642013-05-11 05:45:24 +00004588 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004589 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004590 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4591}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004592
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004593TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith1fa5d642013-05-11 05:45:24 +00004594 assert(Context);
Douglas Gregoreda8e122011-08-09 15:13:55 +00004595 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00004596 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004597}
4598
4599TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4600 if (T.isNull())
4601 return TypeIdx();
4602 assert(!T.getLocalFastQualifiers());
4603
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00004604 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004605 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004606 if (DoneWritingDeclsAndTypes) {
4607 assert(0 && "New type seen after serializing all the types to emit!");
4608 return TypeIdx();
4609 }
4610
Douglas Gregor1970d882009-04-26 03:49:13 +00004611 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004612 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004613 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004614 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004615 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004616 return Idx;
4617}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004618
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004619TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004620 if (T.isNull())
4621 return TypeIdx();
4622 assert(!T.getLocalFastQualifiers());
4623
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004624 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4625 assert(I != TypeIdxs.end() && "Type not emitted!");
4626 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004627}
4628
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004629void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004630 Record.push_back(GetDeclRef(D));
4631}
4632
Sebastian Redl539c5062010-08-18 23:57:32 +00004633DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004634 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4635
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004636 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004637 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004638 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004639
4640 // If D comes from an AST file, its declaration ID is already known and
4641 // fixed.
4642 if (D->isFromASTFile())
4643 return D->getGlobalID();
4644
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004645 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004646 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004647 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004648 if (DoneWritingDeclsAndTypes) {
4649 assert(0 && "New decl seen after serializing all the decls to emit!");
4650 return 0;
4651 }
4652
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004653 // We haven't seen this declaration before. Give it a new ID and
4654 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004655 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004656 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004657 }
4658
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004659 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004660}
4661
Sebastian Redl539c5062010-08-18 23:57:32 +00004662DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004663 if (D == 0)
4664 return 0;
4665
Douglas Gregorb3163e52012-01-05 22:33:30 +00004666 // If D comes from an AST file, its declaration ID is already known and
4667 // fixed.
4668 if (D->isFromASTFile())
4669 return D->getGlobalID();
4670
Douglas Gregore84a9da2009-04-20 20:36:09 +00004671 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4672 return DeclIDs[D];
4673}
4674
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004675void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004676 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004677 assert(D);
4678
4679 SourceLocation Loc = D->getLocation();
4680 if (Loc.isInvalid())
4681 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004682
4683 // We only keep track of the file-level declarations of each file.
4684 if (!D->getLexicalDeclContext()->isFileContext())
4685 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004686 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4687 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004688 if (isa<ParmVarDecl>(D))
4689 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004690
4691 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004692 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004693 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004694 FileID FID;
4695 unsigned Offset;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00004696 std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004697 if (FID.isInvalid())
4698 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004699 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004700
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004701 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004702 if (!Info)
4703 Info = new DeclIDInFileInfo();
4704
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004705 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004706 LocDeclIDsTy &Decls = Info->DeclIDs;
4707
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004708 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004709 Decls.push_back(LocDecl);
4710 return;
4711 }
4712
Benjamin Kramer45025c02013-08-24 13:22:59 +00004713 LocDeclIDsTy::iterator I =
4714 std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004715
4716 Decls.insert(I, LocDecl);
4717}
4718
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004719void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004720 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004721 Record.push_back(Name.getNameKind());
4722 switch (Name.getNameKind()) {
4723 case DeclarationName::Identifier:
4724 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4725 break;
4726
4727 case DeclarationName::ObjCZeroArgSelector:
4728 case DeclarationName::ObjCOneArgSelector:
4729 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004730 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004731 break;
4732
4733 case DeclarationName::CXXConstructorName:
4734 case DeclarationName::CXXDestructorName:
4735 case DeclarationName::CXXConversionFunctionName:
4736 AddTypeRef(Name.getCXXNameType(), Record);
4737 break;
4738
4739 case DeclarationName::CXXOperatorName:
4740 Record.push_back(Name.getCXXOverloadedOperator());
4741 break;
4742
Alexis Hunt3d221f22009-11-29 07:34:05 +00004743 case DeclarationName::CXXLiteralOperatorName:
4744 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4745 break;
4746
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004747 case DeclarationName::CXXUsingDirective:
4748 // No extra data to emit
4749 break;
4750 }
4751}
Chris Lattnerca025db2010-05-07 21:43:38 +00004752
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004753void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004754 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004755 switch (Name.getNameKind()) {
4756 case DeclarationName::CXXConstructorName:
4757 case DeclarationName::CXXDestructorName:
4758 case DeclarationName::CXXConversionFunctionName:
4759 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4760 break;
4761
4762 case DeclarationName::CXXOperatorName:
4763 AddSourceLocation(
4764 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4765 Record);
4766 AddSourceLocation(
4767 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4768 Record);
4769 break;
4770
4771 case DeclarationName::CXXLiteralOperatorName:
4772 AddSourceLocation(
4773 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4774 Record);
4775 break;
4776
4777 case DeclarationName::Identifier:
4778 case DeclarationName::ObjCZeroArgSelector:
4779 case DeclarationName::ObjCOneArgSelector:
4780 case DeclarationName::ObjCMultiArgSelector:
4781 case DeclarationName::CXXUsingDirective:
4782 break;
4783 }
4784}
4785
4786void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004787 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004788 AddDeclarationName(NameInfo.getName(), Record);
4789 AddSourceLocation(NameInfo.getLoc(), Record);
4790 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4791}
4792
4793void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004794 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004795 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004796 Record.push_back(Info.NumTemplParamLists);
4797 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4798 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4799}
4800
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004801void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004802 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004803 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004804 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004805 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004806
4807 // Push each of the NNS's onto a stack for serialization in reverse order.
4808 while (NNS) {
4809 NestedNames.push_back(NNS);
4810 NNS = NNS->getPrefix();
4811 }
4812
4813 Record.push_back(NestedNames.size());
4814 while(!NestedNames.empty()) {
4815 NNS = NestedNames.pop_back_val();
4816 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4817 Record.push_back(Kind);
4818 switch (Kind) {
4819 case NestedNameSpecifier::Identifier:
4820 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4821 break;
4822
4823 case NestedNameSpecifier::Namespace:
4824 AddDeclRef(NNS->getAsNamespace(), Record);
4825 break;
4826
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004827 case NestedNameSpecifier::NamespaceAlias:
4828 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4829 break;
4830
Chris Lattnerca025db2010-05-07 21:43:38 +00004831 case NestedNameSpecifier::TypeSpec:
4832 case NestedNameSpecifier::TypeSpecWithTemplate:
4833 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4834 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4835 break;
4836
4837 case NestedNameSpecifier::Global:
4838 // Don't need to write an associated value.
4839 break;
4840 }
4841 }
4842}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004843
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004844void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4845 RecordDataImpl &Record) {
4846 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004847 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004848 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004849
4850 // Push each of the nested-name-specifiers's onto a stack for
4851 // serialization in reverse order.
4852 while (NNS) {
4853 NestedNames.push_back(NNS);
4854 NNS = NNS.getPrefix();
4855 }
4856
4857 Record.push_back(NestedNames.size());
4858 while(!NestedNames.empty()) {
4859 NNS = NestedNames.pop_back_val();
4860 NestedNameSpecifier::SpecifierKind Kind
4861 = NNS.getNestedNameSpecifier()->getKind();
4862 Record.push_back(Kind);
4863 switch (Kind) {
4864 case NestedNameSpecifier::Identifier:
4865 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4866 AddSourceRange(NNS.getLocalSourceRange(), Record);
4867 break;
4868
4869 case NestedNameSpecifier::Namespace:
4870 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4871 AddSourceRange(NNS.getLocalSourceRange(), Record);
4872 break;
4873
4874 case NestedNameSpecifier::NamespaceAlias:
4875 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4876 AddSourceRange(NNS.getLocalSourceRange(), Record);
4877 break;
4878
4879 case NestedNameSpecifier::TypeSpec:
4880 case NestedNameSpecifier::TypeSpecWithTemplate:
4881 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4882 AddTypeLoc(NNS.getTypeLoc(), Record);
4883 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4884 break;
4885
4886 case NestedNameSpecifier::Global:
4887 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4888 break;
4889 }
4890 }
4891}
4892
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004893void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004894 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004895 Record.push_back(Kind);
4896 switch (Kind) {
4897 case TemplateName::Template:
4898 AddDeclRef(Name.getAsTemplateDecl(), Record);
4899 break;
4900
4901 case TemplateName::OverloadedTemplate: {
4902 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4903 Record.push_back(OvT->size());
4904 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4905 I != E; ++I)
4906 AddDeclRef(*I, Record);
4907 break;
4908 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004909
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004910 case TemplateName::QualifiedTemplate: {
4911 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4912 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4913 Record.push_back(QualT->hasTemplateKeyword());
4914 AddDeclRef(QualT->getTemplateDecl(), Record);
4915 break;
4916 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004917
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004918 case TemplateName::DependentTemplate: {
4919 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4920 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4921 Record.push_back(DepT->isIdentifier());
4922 if (DepT->isIdentifier())
4923 AddIdentifierRef(DepT->getIdentifier(), Record);
4924 else
4925 Record.push_back(DepT->getOperator());
4926 break;
4927 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004928
4929 case TemplateName::SubstTemplateTemplateParm: {
4930 SubstTemplateTemplateParmStorage *subst
4931 = Name.getAsSubstTemplateTemplateParm();
4932 AddDeclRef(subst->getParameter(), Record);
4933 AddTemplateName(subst->getReplacement(), Record);
4934 break;
4935 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004936
4937 case TemplateName::SubstTemplateTemplateParmPack: {
4938 SubstTemplateTemplateParmPackStorage *SubstPack
4939 = Name.getAsSubstTemplateTemplateParmPack();
4940 AddDeclRef(SubstPack->getParameterPack(), Record);
4941 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4942 break;
4943 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004944 }
4945}
4946
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004947void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004948 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004949 Record.push_back(Arg.getKind());
4950 switch (Arg.getKind()) {
4951 case TemplateArgument::Null:
4952 break;
4953 case TemplateArgument::Type:
4954 AddTypeRef(Arg.getAsType(), Record);
4955 break;
4956 case TemplateArgument::Declaration:
4957 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004958 Record.push_back(Arg.isDeclForReferenceParam());
4959 break;
4960 case TemplateArgument::NullPtr:
4961 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004962 break;
4963 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004964 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004965 AddTypeRef(Arg.getIntegralType(), Record);
4966 break;
4967 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004968 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4969 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004970 case TemplateArgument::TemplateExpansion:
4971 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikie05785d12013-02-20 22:23:23 +00004972 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregore1d60df2011-01-14 23:41:42 +00004973 Record.push_back(*NumExpansions + 1);
4974 else
4975 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004976 break;
4977 case TemplateArgument::Expression:
4978 AddStmt(Arg.getAsExpr());
4979 break;
4980 case TemplateArgument::Pack:
4981 Record.push_back(Arg.pack_size());
4982 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4983 I != E; ++I)
4984 AddTemplateArgument(*I, Record);
4985 break;
4986 }
4987}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004988
4989void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004990ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004991 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004992 assert(TemplateParams && "No TemplateParams!");
4993 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4994 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4995 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4996 Record.push_back(TemplateParams->size());
4997 for (TemplateParameterList::const_iterator
4998 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4999 P != PEnd; ++P)
5000 AddDeclRef(*P, Record);
5001}
5002
5003/// \brief Emit a template argument list.
5004void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005005ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005006 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005007 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00005008 Record.push_back(TemplateArgs->size());
5009 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00005010 AddTemplateArgument(TemplateArgs->get(i), Record);
5011}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005012
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00005013void
5014ASTWriter::AddASTTemplateArgumentListInfo
5015(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5016 assert(ASTTemplArgList && "No ASTTemplArgList!");
5017 AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5018 AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5019 Record.push_back(ASTTemplArgList->NumTemplateArgs);
5020 const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5021 for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5022 AddTemplateArgumentLoc(TemplArgs[i], Record);
5023}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005024
5025void
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005026ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005027 Record.push_back(Set.size());
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00005028 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00005029 I = Set.begin(), E = Set.end(); I != E; ++I) {
5030 AddDeclRef(I.getDecl(), Record);
5031 Record.push_back(I.getAccess());
5032 }
5033}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005034
Sebastian Redl55c0ad52010-08-18 23:56:21 +00005035void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005036 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005037 Record.push_back(Base.isVirtual());
5038 Record.push_back(Base.isBaseOfClass());
5039 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00005040 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00005041 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005042 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00005043 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5044 : SourceLocation(),
5045 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00005046}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005047
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005048void ASTWriter::FlushCXXBaseSpecifiers() {
5049 RecordData Record;
5050 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
5051 Record.clear();
5052
5053 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00005054 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005055 if (Index == CXXBaseSpecifiersOffsets.size())
5056 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5057 else {
5058 if (Index > CXXBaseSpecifiersOffsets.size())
5059 CXXBaseSpecifiersOffsets.resize(Index + 1);
5060 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5061 }
5062
5063 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5064 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5065 Record.push_back(BEnd - B);
5066 for (; B != BEnd; ++B)
5067 AddCXXBaseSpecifier(*B, Record);
5068 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00005069
5070 // Flush any expressions that were written as part of the base specifiers.
5071 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005072 }
5073
5074 CXXBaseSpecifiersToWrite.clear();
5075}
5076
Alexis Hunt1d792652011-01-08 20:30:50 +00005077void ASTWriter::AddCXXCtorInitializers(
5078 const CXXCtorInitializer * const *CtorInitializers,
5079 unsigned NumCtorInitializers,
5080 RecordDataImpl &Record) {
5081 Record.push_back(NumCtorInitializers);
5082 for (unsigned i=0; i != NumCtorInitializers; ++i) {
5083 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005084
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005085 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005086 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005087 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005088 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00005089 } else if (Init->isDelegatingInitializer()) {
5090 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00005091 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00005092 } else if (Init->isMemberInitializer()){
5093 Record.push_back(CTOR_INITIALIZER_MEMBER);
5094 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005095 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00005096 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5097 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005098 }
Francois Pichetd583da02010-12-04 09:14:42 +00005099
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005100 AddSourceLocation(Init->getMemberLocation(), Record);
5101 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00005102 AddSourceLocation(Init->getLParenLoc(), Record);
5103 AddSourceLocation(Init->getRParenLoc(), Record);
5104 Record.push_back(Init->isWritten());
5105 if (Init->isWritten()) {
5106 Record.push_back(Init->getSourceOrder());
5107 } else {
5108 Record.push_back(Init->getNumArrayIndices());
5109 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5110 AddDeclRef(Init->getArrayIndex(i), Record);
5111 }
5112 }
5113}
5114
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005115void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5116 assert(D->DefinitionData);
5117 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00005118 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005119 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005120 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005121 Record.push_back(Data.Aggregate);
5122 Record.push_back(Data.PlainOldData);
5123 Record.push_back(Data.Empty);
5124 Record.push_back(Data.Polymorphic);
5125 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00005126 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00005127 Record.push_back(Data.HasNoNonEmptyBases);
5128 Record.push_back(Data.HasPrivateFields);
5129 Record.push_back(Data.HasProtectedFields);
5130 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00005131 Record.push_back(Data.HasMutableFields);
Richard Smithab44d5b2013-12-10 08:25:00 +00005132 Record.push_back(Data.HasVariantMembers);
Richard Smith561fb152012-02-25 07:33:38 +00005133 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00005134 Record.push_back(Data.HasInClassInitializer);
Richard Smith593f9932012-12-08 02:01:17 +00005135 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smith6b02d462012-12-08 08:32:28 +00005136 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5137 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5138 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5139 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5140 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5141 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith328aae52012-11-30 05:11:39 +00005142 Record.push_back(Data.HasTrivialSpecialMembers);
5143 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith111af8d2011-08-10 18:11:37 +00005144 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00005145 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00005146 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00005147 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005148 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00005149 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith328aae52012-11-30 05:11:39 +00005150 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smith1c33fe82012-11-28 06:23:12 +00005151 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5152 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5153 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5154 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Richard Smith561fb152012-02-25 07:33:38 +00005155 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005156
5157 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005158 if (Data.NumBases > 0)
5159 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5160 Record);
5161
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005162 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5163 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00005164 if (Data.NumVBases > 0)
5165 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5166 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005167
Richard Smitha4ba74c2013-08-30 04:46:40 +00005168 AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5169 AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005170 // Data.Definition is the owning decl, no need to write it.
Richard Smith68ad0e72013-06-26 02:41:25 +00005171 AddDeclRef(D->getFirstFriend(), Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005172
5173 // Add lambda-specific data.
5174 if (Data.IsLambda) {
5175 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00005176 Record.push_back(Lambda.Dependent);
Faisal Valic1a6dc42013-10-23 16:10:50 +00005177 Record.push_back(Lambda.IsGenericLambda);
5178 Record.push_back(Lambda.CaptureDefault);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005179 Record.push_back(Lambda.NumCaptures);
5180 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00005181 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005182 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00005183 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00005184 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5185 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5186 AddSourceLocation(Capture.getLocation(), Record);
5187 Record.push_back(Capture.isImplicit());
Richard Smithba71c082013-05-16 06:20:58 +00005188 Record.push_back(Capture.getCaptureKind());
5189 switch (Capture.getCaptureKind()) {
5190 case LCK_This:
5191 break;
5192 case LCK_ByCopy:
Richard Smithbb13c9a2013-09-28 04:02:39 +00005193 case LCK_ByRef:
Richard Smithba71c082013-05-16 06:20:58 +00005194 VarDecl *Var =
5195 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5196 AddDeclRef(Var, Record);
5197 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5198 : SourceLocation(),
5199 Record);
5200 break;
5201 }
Douglas Gregor99ae8062012-02-14 17:54:36 +00005202 }
5203 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00005204}
5205
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005206void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00005207 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00005208 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00005209 assert(FirstDeclID == NextDeclID &&
5210 FirstTypeID == NextTypeID &&
5211 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005212 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00005213 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00005214 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00005215 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00005216
Sebastian Redl07a89a82010-07-30 00:29:29 +00005217 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005218
Douglas Gregordf0c1512011-08-18 04:12:04 +00005219 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5220 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5221 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005222 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00005223 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00005224 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005225 NextDeclID = FirstDeclID;
5226 NextTypeID = FirstTypeID;
5227 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005228 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00005229 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00005230 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00005231}
5232
Sebastian Redl539c5062010-08-18 23:57:32 +00005233void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005234 // Always keep the highest ID. See \p TypeRead() for more information.
5235 IdentID &StoredID = IdentifierIDs[II];
5236 if (ID > StoredID)
5237 StoredID = ID;
Sebastian Redlff4a2952010-07-23 23:49:55 +00005238}
5239
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005240void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005241 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidiseb663da2013-03-22 21:12:57 +00005242 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005243 if (ID > StoredID)
5244 StoredID = ID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005245}
5246
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00005247void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005248 // Always take the highest-numbered type index. This copes with an interesting
5249 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00005250 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00005251 // keep the higher-numbered entry so that we can properly write it out to
5252 // the AST file.
5253 TypeIdx &StoredIdx = TypeIdxs[T];
5254 if (Idx.getIndex() >= StoredIdx.getIndex())
5255 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00005256}
5257
Sebastian Redl539c5062010-08-18 23:57:32 +00005258void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor8d7edce2013-02-08 21:30:59 +00005259 // Always keep the highest ID. See \p TypeRead() for more information.
5260 SelectorID &StoredID = SelectorIDs[S];
5261 if (ID > StoredID)
5262 StoredID = ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00005263}
Douglas Gregor91096292010-10-02 19:29:26 +00005264
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005265void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00005266 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00005267 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00005268 MacroDefinitions[MD] = ID;
5269}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005270
Douglas Gregore37a85a2011-12-02 17:30:13 +00005271void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5272 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5273 SubmoduleIDs[Mod] = ID;
5274}
5275
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005276void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00005277 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005278 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005279 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5280 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00005281 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005282 // A forward reference was mutated into a definition. Rewrite it.
5283 // FIXME: This happens during template instantiation, should we
5284 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00005285 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005286 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00005287 }
5288}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00005289
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005290void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005291 assert(!WritingAST && "Already writing the AST!");
5292
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005293 // TU and namespaces are handled elsewhere.
5294 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5295 return;
5296
Douglas Gregorb3722e22011-09-09 23:01:35 +00005297 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005298 return; // Not a source decl added to a DeclContext from PCH.
5299
Douglas Gregor9f782892013-01-21 15:25:38 +00005300 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005301 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00005302 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00005303}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005304
5305void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005306 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005307 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00005308 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005309 return; // Not a source member added to a class from PCH.
5310 if (!isa<CXXMethodDecl>(D))
5311 return; // We are interested in lazily declared implicit methods.
5312
5313 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00005314 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005315 UpdateRecord &Record = DeclUpdates[RD];
5316 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005317 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00005318}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005319
5320void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5321 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005322 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005323 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00005324 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005325 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005326 return; // Not a source specialization added to a template from PCH.
5327
5328 UpdateRecord &Record = DeclUpdates[TD];
5329 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005330 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00005331}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00005332
Larisse Voufo39a1e502013-08-06 01:03:05 +00005333void ASTWriter::AddedCXXTemplateSpecialization(
5334 const VarTemplateDecl *TD, const VarTemplateSpecializationDecl *D) {
5335 // The specializations set is kept in the canonical template.
5336 assert(!WritingAST && "Already writing the AST!");
5337 TD = TD->getCanonicalDecl();
5338 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
5339 return; // Not a source specialization added to a template from PCH.
5340
5341 UpdateRecord &Record = DeclUpdates[TD];
5342 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
5343 Record.push_back(reinterpret_cast<uint64_t>(D));
5344}
5345
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005346void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5347 const FunctionDecl *D) {
5348 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005349 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005350 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00005351 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005352 return; // Not a source specialization added to a template from PCH.
5353
5354 UpdateRecord &Record = DeclUpdates[TD];
5355 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005356 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00005357}
5358
Richard Smith1fa5d642013-05-11 05:45:24 +00005359void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5360 assert(!WritingAST && "Already writing the AST!");
5361 FD = FD->getCanonicalDecl();
5362 if (!FD->isFromASTFile())
5363 return; // Not a function declared in PCH and defined outside.
5364
5365 UpdateRecord &Record = DeclUpdates[FD];
5366 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5367 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5368}
5369
Sebastian Redlab238a72011-04-24 16:28:06 +00005370void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005371 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005372 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00005373 return; // Declaration not imported from PCH.
5374
5375 // Implicit decl from a PCH was defined.
5376 // FIXME: Should implicit definition be a separate FunctionDecl?
5377 RewriteDecl(D);
5378}
5379
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005380void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005381 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005382 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00005383 return;
5384
5385 // Since the actual instantiation is delayed, this really means that we need
5386 // to update the instantiation location.
5387 UpdateRecord &Record = DeclUpdates[D];
5388 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5389 AddSourceLocation(
5390 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5391}
5392
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005393void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5394 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00005395 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00005396 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005397 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00005398
5399 assert(IFD->getDefinition() && "Category on a class without a definition?");
5400 ObjCClassesWithCategories.insert(
5401 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00005402}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00005403
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00005404
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00005405void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5406 const ObjCPropertyDecl *OrigProp,
5407 const ObjCCategoryDecl *ClassExt) {
5408 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5409 if (!D)
5410 return;
5411
5412 assert(!WritingAST && "Already writing the AST!");
5413 if (!D->isFromASTFile())
5414 return; // Declaration not imported from PCH.
5415
5416 RewriteDecl(D);
5417}
Eli Friedman276dd182013-09-05 00:02:25 +00005418
5419void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5420 assert(!WritingAST && "Already writing the AST!");
5421 if (!D->isFromASTFile())
5422 return;
5423
5424 UpdateRecord &Record = DeclUpdates[D];
5425 Record.push_back(UPD_DECL_MARKED_USED);
5426}