blob: 3654ec27f40a697598a502bacb9b0e86178e7a6c [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-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 Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000019#include "clang/AST/DeclFriend.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000025#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000026#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000031#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000032#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000033#include "clang/Basic/VersionTuple.h"
Chandler Carruth55fc8732012-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 Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +000045#include "llvm/ADT/Hashing.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000046#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000047#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000048#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000049#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000050#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000052#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000053#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000054#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000055using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000056using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000057
Sebastian Redlade50002010-07-30 17:03:48 +000058template <typename T, typename Allocator>
Chris Lattner5f9e2722011-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 Kramer6e089c62011-04-24 17:44:50 +000062 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000063}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064
65template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000066static StringRef data(const SmallVectorImpl<T> &v) {
67 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000068 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000069}
70
Douglas Gregor2cf26342009-04-09 22:27:44 +000071//===----------------------------------------------------------------------===//
72// Type serialization
73//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000074
Douglas Gregor2cf26342009-04-09 22:27:44 +000075namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000076 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000077 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000078 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
80 public:
81 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000084 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000085 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-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 Gregor2cf26342009-04-09 22:27:44 +000093#include "clang/AST/TypeNodes.def"
94 };
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000098 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000117 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
118 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000123 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000124 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000128 Writer.AddTypeRef(T->getPointeeType(), Record);
129 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 Writer.AddTypeRef(T->getElementType(), Record);
135 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000136 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 VisitArrayType(T);
141 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000147 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000152 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
153 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000154 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getElementType(), Record);
160 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000161 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000167 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 FunctionType::ExtInfo C = T->getExtInfo();
173 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000174 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000175 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000176 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000177 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000178 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179}
180
Sebastian Redl3397c552010-08-18 23:56:27 +0000181void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000182 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187 VisitFunctionType(T);
188 Record.push_back(T->getNumArgs());
189 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
190 Writer.AddTypeRef(T->getArgType(I), Record);
191 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000192 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000194 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000195 Record.push_back(T->getExceptionSpecType());
196 if (T->getExceptionSpecType() == EST_Dynamic) {
197 Record.push_back(T->getNumExceptions());
198 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
199 Writer.AddTypeRef(T->getExceptionType(I), Record);
200 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
201 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000202 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
204 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000205 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
206 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000207 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000214}
John McCalled976492009-12-04 22:46:56 +0000215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000218 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
219 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000220 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000224 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000234 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000235 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000237}
238
Sean Huntca63c202011-05-24 22:41:36 +0000239void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
240 Writer.AddTypeRef(T->getBaseType(), Record);
241 Writer.AddTypeRef(T->getUnderlyingType(), Record);
242 Record.push_back(T->getUTTKind());
243 Code = TYPE_UNARY_TRANSFORM;
244}
245
Richard Smith34b41d92011-02-20 03:19:35 +0000246void ASTTypeWriter::VisitAutoType(const AutoType *T) {
247 Writer.AddTypeRef(T->getDeducedType(), Record);
Richard Smitha2c36462013-04-26 16:15:35 +0000248 Record.push_back(T->isDecltypeAuto());
Richard Smithdc7a4f52013-04-30 13:56:41 +0000249 if (T->getDeducedType().isNull())
250 Record.push_back(T->isDependentType());
Richard Smith34b41d92011-02-20 03:19:35 +0000251 Code = TYPE_AUTO;
252}
253
Sebastian Redl3397c552010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000255 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000256 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000257 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 "Cannot serialize in the middle of a type definition");
259}
260
Sebastian Redl3397c552010-08-18 23:56:27 +0000261void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000263 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000264}
265
Sebastian Redl3397c552010-08-18 23:56:27 +0000266void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000267 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000268 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000269}
270
John McCall9d156a72011-01-06 01:58:22 +0000271void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
272 Writer.AddTypeRef(T->getModifiedType(), Record);
273 Writer.AddTypeRef(T->getEquivalentType(), Record);
274 Record.push_back(T->getAttrKind());
275 Code = TYPE_ATTRIBUTED;
276}
277
Mike Stump1eb44332009-09-09 15:08:12 +0000278void
Sebastian Redl3397c552010-08-18 23:56:27 +0000279ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000280 const SubstTemplateTypeParmType *T) {
281 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
282 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000283 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000284}
285
286void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000287ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
288 const SubstTemplateTypeParmPackType *T) {
289 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
290 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
291 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
292}
293
294void
Sebastian Redl3397c552010-08-18 23:56:27 +0000295ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000296 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000297 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000298 Writer.AddTemplateName(T->getTemplateName(), Record);
299 Record.push_back(T->getNumArgs());
300 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
301 ArgI != ArgE; ++ArgI)
302 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000303 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
304 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000305 : T->getCanonicalTypeInternal(),
306 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000307 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000308}
309
310void
Sebastian Redl3397c552010-08-18 23:56:27 +0000311ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000312 VisitArrayType(T);
313 Writer.AddStmt(T->getSizeExpr());
314 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000315 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl3397c552010-08-18 23:56:27 +0000319ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320 const DependentSizedExtVectorType *T) {
321 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000322 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000323}
324
325void
Sebastian Redl3397c552010-08-18 23:56:27 +0000326ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000327 Record.push_back(T->getDepth());
328 Record.push_back(T->getIndex());
329 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000330 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000331 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000332}
333
334void
Sebastian Redl3397c552010-08-18 23:56:27 +0000335ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000336 Record.push_back(T->getKeyword());
337 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
338 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000339 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
340 : T->getCanonicalTypeInternal(),
341 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000343}
344
345void
Sebastian Redl3397c552010-08-18 23:56:27 +0000346ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000347 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000348 Record.push_back(T->getKeyword());
349 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
350 Writer.AddIdentifierRef(T->getIdentifier(), Record);
351 Record.push_back(T->getNumArgs());
352 for (DependentTemplateSpecializationType::iterator
353 I = T->begin(), E = T->end(); I != E; ++I)
354 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000355 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000356}
357
Douglas Gregor7536dd52010-12-20 02:24:11 +0000358void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
359 Writer.AddTypeRef(T->getPattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +0000360 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000361 Record.push_back(*NumExpansions + 1);
362 else
363 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000364 Code = TYPE_PACK_EXPANSION;
365}
366
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000367void ASTTypeWriter::VisitParenType(const ParenType *T) {
368 Writer.AddTypeRef(T->getInnerType(), Record);
369 Code = TYPE_PAREN;
370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000373 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000374 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
375 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000376 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000377}
378
Sebastian Redl3397c552010-08-18 23:56:27 +0000379void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000380 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000381 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000382 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000383}
384
Sebastian Redl3397c552010-08-18 23:56:27 +0000385void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000386 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000387 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000388}
389
Sebastian Redl3397c552010-08-18 23:56:27 +0000390void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000391 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000392 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000393 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000394 E = T->qual_end(); I != E; ++I)
395 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000396 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000397}
398
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000399void
Sebastian Redl3397c552010-08-18 23:56:27 +0000400ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000401 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000402 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000403}
404
Eli Friedmanb001de72011-10-06 23:00:33 +0000405void
406ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
407 Writer.AddTypeRef(T->getValueType(), Record);
408 Code = TYPE_ATOMIC;
409}
410
John McCalla1ee0c52009-10-16 21:56:05 +0000411namespace {
412
413class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000414 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000415 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000416
417public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000418 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000419 : Writer(Writer), Record(Record) { }
420
John McCall51bd8032009-10-18 01:05:36 +0000421#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000422#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000423 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000424#include "clang/AST/TypeLocNodes.def"
425
John McCall51bd8032009-10-18 01:05:36 +0000426 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
427 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000428};
429
430}
431
John McCall51bd8032009-10-18 01:05:36 +0000432void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
433 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000434}
John McCall51bd8032009-10-18 01:05:36 +0000435void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000436 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
437 if (TL.needsExtraLocalData()) {
438 Record.push_back(TL.getWrittenTypeSpec());
439 Record.push_back(TL.getWrittenSignSpec());
440 Record.push_back(TL.getWrittenWidthSpec());
441 Record.push_back(TL.hasModeAttr());
442 }
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000446}
John McCall51bd8032009-10-18 01:05:36 +0000447void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000458}
John McCall51bd8032009-10-18 01:05:36 +0000459void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000461 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000462}
John McCall51bd8032009-10-18 01:05:36 +0000463void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
465 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
466 Record.push_back(TL.getSizeExpr() ? 1 : 0);
467 if (TL.getSizeExpr())
468 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000469}
John McCall51bd8032009-10-18 01:05:36 +0000470void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
477 VisitArrayTypeLoc(TL);
478}
479void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
480 DependentSizedArrayTypeLoc TL) {
481 VisitArrayTypeLoc(TL);
482}
483void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
484 DependentSizedExtVectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000494 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000495 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
496 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000497 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000498 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
499 Writer.AddDeclRef(TL.getArg(i), Record);
500}
501void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
502 VisitFunctionTypeLoc(TL);
503}
504void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
505 VisitFunctionTypeLoc(TL);
506}
John McCalled976492009-12-04 22:46:56 +0000507void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
John McCall51bd8032009-10-18 01:05:36 +0000510void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
513void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000514 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
515 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
516 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000519 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
520 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
521 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
522 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000523}
524void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
Sean Huntca63c202011-05-24 22:41:36 +0000527void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getKWLoc(), Record);
529 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
530 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
531 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
532}
Richard Smith34b41d92011-02-20 03:19:35 +0000533void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
John McCall51bd8032009-10-18 01:05:36 +0000536void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getNameLoc(), Record);
538}
539void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
540 Writer.AddSourceLocation(TL.getNameLoc(), Record);
541}
John McCall9d156a72011-01-06 01:58:22 +0000542void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
544 if (TL.hasAttrOperand()) {
545 SourceRange range = TL.getAttrOperandParensRange();
546 Writer.AddSourceLocation(range.getBegin(), Record);
547 Writer.AddSourceLocation(range.getEnd(), Record);
548 }
549 if (TL.hasAttrExprOperand()) {
550 Expr *operand = TL.getAttrExprOperand();
551 Record.push_back(operand ? 1 : 0);
552 if (operand) Writer.AddStmt(operand);
553 } else if (TL.hasAttrEnumOperand()) {
554 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
555 }
556}
John McCall51bd8032009-10-18 01:05:36 +0000557void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
John McCall49a832b2009-10-18 09:09:24 +0000560void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
561 SubstTemplateTypeParmTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000564void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
565 SubstTemplateTypeParmPackTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
John McCall51bd8032009-10-18 01:05:36 +0000568void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
569 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000570 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000571 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
572 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
573 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
574 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000575 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
576 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000577}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000578void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
579 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
580 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
581}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000582void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000585}
John McCall3cb0ebd2010-03-10 03:28:59 +0000586void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
587 Writer.AddSourceLocation(TL.getNameLoc(), Record);
588}
Douglas Gregor4714c122010-03-31 17:34:00 +0000589void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000590 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000591 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000592 Writer.AddSourceLocation(TL.getNameLoc(), Record);
593}
John McCall33500952010-06-11 00:33:02 +0000594void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
595 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000596 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000597 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000598 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000599 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000600 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
601 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
602 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000603 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
604 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000605}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000606void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
608}
John McCall51bd8032009-10-18 01:05:36 +0000609void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000611}
612void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
613 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000614 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
615 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
616 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
617 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000618}
John McCall54e14c42009-10-22 22:37:11 +0000619void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
620 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000621}
Eli Friedmanb001de72011-10-06 23:00:33 +0000622void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
623 Writer.AddSourceLocation(TL.getKWLoc(), Record);
624 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
625 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
626}
John McCalla1ee0c52009-10-16 21:56:05 +0000627
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000628//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000629// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000630//===----------------------------------------------------------------------===//
631
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000632static void EmitBlockID(unsigned ID, const char *Name,
633 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000634 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000635 Record.clear();
636 Record.push_back(ID);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
638
639 // Emit the block name if present.
640 if (Name == 0 || Name[0] == 0) return;
641 Record.clear();
642 while (*Name)
643 Record.push_back(*Name++);
644 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
645}
646
647static void EmitRecordID(unsigned ID, const char *Name,
648 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000649 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000650 Record.clear();
651 Record.push_back(ID);
652 while (*Name)
653 Record.push_back(*Name++);
654 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000655}
656
657static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000658 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000659#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000660 RECORD(STMT_STOP);
661 RECORD(STMT_NULL_PTR);
662 RECORD(STMT_NULL);
663 RECORD(STMT_COMPOUND);
664 RECORD(STMT_CASE);
665 RECORD(STMT_DEFAULT);
666 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000667 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000668 RECORD(STMT_IF);
669 RECORD(STMT_SWITCH);
670 RECORD(STMT_WHILE);
671 RECORD(STMT_DO);
672 RECORD(STMT_FOR);
673 RECORD(STMT_GOTO);
674 RECORD(STMT_INDIRECT_GOTO);
675 RECORD(STMT_CONTINUE);
676 RECORD(STMT_BREAK);
677 RECORD(STMT_RETURN);
678 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000679 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000680 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000681 RECORD(EXPR_PREDEFINED);
682 RECORD(EXPR_DECL_REF);
683 RECORD(EXPR_INTEGER_LITERAL);
684 RECORD(EXPR_FLOATING_LITERAL);
685 RECORD(EXPR_IMAGINARY_LITERAL);
686 RECORD(EXPR_STRING_LITERAL);
687 RECORD(EXPR_CHARACTER_LITERAL);
688 RECORD(EXPR_PAREN);
689 RECORD(EXPR_UNARY_OPERATOR);
690 RECORD(EXPR_SIZEOF_ALIGN_OF);
691 RECORD(EXPR_ARRAY_SUBSCRIPT);
692 RECORD(EXPR_CALL);
693 RECORD(EXPR_MEMBER);
694 RECORD(EXPR_BINARY_OPERATOR);
695 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
696 RECORD(EXPR_CONDITIONAL_OPERATOR);
697 RECORD(EXPR_IMPLICIT_CAST);
698 RECORD(EXPR_CSTYLE_CAST);
699 RECORD(EXPR_COMPOUND_LITERAL);
700 RECORD(EXPR_EXT_VECTOR_ELEMENT);
701 RECORD(EXPR_INIT_LIST);
702 RECORD(EXPR_DESIGNATED_INIT);
703 RECORD(EXPR_IMPLICIT_VALUE_INIT);
704 RECORD(EXPR_VA_ARG);
705 RECORD(EXPR_ADDR_LABEL);
706 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000707 RECORD(EXPR_CHOOSE);
708 RECORD(EXPR_GNU_NULL);
709 RECORD(EXPR_SHUFFLE_VECTOR);
710 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000711 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000712 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000713 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000714 RECORD(EXPR_OBJC_ARRAY_LITERAL);
715 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000716 RECORD(EXPR_OBJC_ENCODE);
717 RECORD(EXPR_OBJC_SELECTOR_EXPR);
718 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
719 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
720 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
721 RECORD(EXPR_OBJC_KVC_REF_EXPR);
722 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000723 RECORD(STMT_OBJC_FOR_COLLECTION);
724 RECORD(STMT_OBJC_CATCH);
725 RECORD(STMT_OBJC_FINALLY);
726 RECORD(STMT_OBJC_AT_TRY);
727 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
728 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000729 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000730 RECORD(EXPR_CXX_OPERATOR_CALL);
731 RECORD(EXPR_CXX_CONSTRUCT);
732 RECORD(EXPR_CXX_STATIC_CAST);
733 RECORD(EXPR_CXX_DYNAMIC_CAST);
734 RECORD(EXPR_CXX_REINTERPRET_CAST);
735 RECORD(EXPR_CXX_CONST_CAST);
736 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000737 RECORD(EXPR_USER_DEFINED_LITERAL);
Richard Smith7c3e6152013-06-12 22:31:48 +0000738 RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000739 RECORD(EXPR_CXX_BOOL_LITERAL);
740 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000741 RECORD(EXPR_CXX_TYPEID_EXPR);
742 RECORD(EXPR_CXX_TYPEID_TYPE);
743 RECORD(EXPR_CXX_UUIDOF_EXPR);
744 RECORD(EXPR_CXX_UUIDOF_TYPE);
745 RECORD(EXPR_CXX_THIS);
746 RECORD(EXPR_CXX_THROW);
747 RECORD(EXPR_CXX_DEFAULT_ARG);
748 RECORD(EXPR_CXX_BIND_TEMPORARY);
749 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
750 RECORD(EXPR_CXX_NEW);
751 RECORD(EXPR_CXX_DELETE);
752 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
753 RECORD(EXPR_EXPR_WITH_CLEANUPS);
754 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
755 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
756 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
757 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
758 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
759 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
760 RECORD(EXPR_CXX_NOEXCEPT);
761 RECORD(EXPR_OPAQUE_VALUE);
762 RECORD(EXPR_BINARY_TYPE_TRAIT);
763 RECORD(EXPR_PACK_EXPANSION);
764 RECORD(EXPR_SIZEOF_PACK);
765 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000766 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000767#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768}
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Sebastian Redla4232eb2010-08-18 23:56:21 +0000770void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000771 RecordData Record;
772 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000774#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
775#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000777 // Control Block.
778 BLOCK(CONTROL_BLOCK);
779 RECORD(METADATA);
780 RECORD(IMPORTS);
781 RECORD(LANGUAGE_OPTIONS);
782 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000783 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000784 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000785 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000786 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000787 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000788 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000789 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000790 RECORD(PREPROCESSOR_OPTIONS);
791
Douglas Gregorc337fef2012-10-19 00:45:00 +0000792 BLOCK(INPUT_FILES_BLOCK);
793 RECORD(INPUT_FILE);
794
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000795 // AST Top-Level Block.
796 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000797 RECORD(TYPE_OFFSET);
798 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000799 RECORD(IDENTIFIER_OFFSET);
800 RECORD(IDENTIFIER_TABLE);
801 RECORD(EXTERNAL_DEFINITIONS);
802 RECORD(SPECIAL_TYPES);
803 RECORD(STATISTICS);
804 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000805 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000806 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 RECORD(SELECTOR_OFFSETS);
808 RECORD(METHOD_POOL);
809 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000810 RECORD(SOURCE_LOCATION_OFFSETS);
811 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000812 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000813 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000814 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000815 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000816 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000817 RECORD(SEMA_DECL_REFS);
818 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
819 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
820 RECORD(DECL_REPLACEMENTS);
821 RECORD(UPDATE_VISIBLE);
822 RECORD(DECL_UPDATE_OFFSETS);
823 RECORD(DECL_UPDATES);
824 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
825 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000826 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000827 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000828 RECORD(FP_PRAGMA_OPTIONS);
829 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000830 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000831 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000832 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000833 RECORD(MODULE_OFFSET_MAP);
834 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000835 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000836 RECORD(FILE_SORTED_DECLS);
837 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000838 RECORD(MERGED_DECLARATIONS);
839 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000840 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000841 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000842 RECORD(MACRO_TABLE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000843
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000844 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000845 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000846 RECORD(SM_SLOC_FILE_ENTRY);
847 RECORD(SM_SLOC_BUFFER_ENTRY);
848 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000849 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000851 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000852 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000853 RECORD(PP_MACRO_OBJECT_LIKE);
854 RECORD(PP_MACRO_FUNCTION_LIKE);
855 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000856
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000857 // Decls and Types block.
858 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000859 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000860 RECORD(TYPE_COMPLEX);
861 RECORD(TYPE_POINTER);
862 RECORD(TYPE_BLOCK_POINTER);
863 RECORD(TYPE_LVALUE_REFERENCE);
864 RECORD(TYPE_RVALUE_REFERENCE);
865 RECORD(TYPE_MEMBER_POINTER);
866 RECORD(TYPE_CONSTANT_ARRAY);
867 RECORD(TYPE_INCOMPLETE_ARRAY);
868 RECORD(TYPE_VARIABLE_ARRAY);
869 RECORD(TYPE_VECTOR);
870 RECORD(TYPE_EXT_VECTOR);
871 RECORD(TYPE_FUNCTION_PROTO);
872 RECORD(TYPE_FUNCTION_NO_PROTO);
873 RECORD(TYPE_TYPEDEF);
874 RECORD(TYPE_TYPEOF_EXPR);
875 RECORD(TYPE_TYPEOF);
876 RECORD(TYPE_RECORD);
877 RECORD(TYPE_ENUM);
878 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000879 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000880 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000881 RECORD(TYPE_DECLTYPE);
882 RECORD(TYPE_ELABORATED);
883 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
884 RECORD(TYPE_UNRESOLVED_USING);
885 RECORD(TYPE_INJECTED_CLASS_NAME);
886 RECORD(TYPE_OBJC_OBJECT);
887 RECORD(TYPE_TEMPLATE_TYPE_PARM);
888 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
889 RECORD(TYPE_DEPENDENT_NAME);
890 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
891 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
892 RECORD(TYPE_PAREN);
893 RECORD(TYPE_PACK_EXPANSION);
894 RECORD(TYPE_ATTRIBUTED);
895 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000896 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000897 RECORD(DECL_TYPEDEF);
898 RECORD(DECL_ENUM);
899 RECORD(DECL_RECORD);
900 RECORD(DECL_ENUM_CONSTANT);
901 RECORD(DECL_FUNCTION);
902 RECORD(DECL_OBJC_METHOD);
903 RECORD(DECL_OBJC_INTERFACE);
904 RECORD(DECL_OBJC_PROTOCOL);
905 RECORD(DECL_OBJC_IVAR);
906 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000907 RECORD(DECL_OBJC_CATEGORY);
908 RECORD(DECL_OBJC_CATEGORY_IMPL);
909 RECORD(DECL_OBJC_IMPLEMENTATION);
910 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
911 RECORD(DECL_OBJC_PROPERTY);
912 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000913 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000914 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000915 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000916 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000917 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000918 RECORD(DECL_FILE_SCOPE_ASM);
919 RECORD(DECL_BLOCK);
920 RECORD(DECL_CONTEXT_LEXICAL);
921 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000922 RECORD(DECL_NAMESPACE);
923 RECORD(DECL_NAMESPACE_ALIAS);
924 RECORD(DECL_USING);
925 RECORD(DECL_USING_SHADOW);
926 RECORD(DECL_USING_DIRECTIVE);
927 RECORD(DECL_UNRESOLVED_USING_VALUE);
928 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
929 RECORD(DECL_LINKAGE_SPEC);
930 RECORD(DECL_CXX_RECORD);
931 RECORD(DECL_CXX_METHOD);
932 RECORD(DECL_CXX_CONSTRUCTOR);
933 RECORD(DECL_CXX_DESTRUCTOR);
934 RECORD(DECL_CXX_CONVERSION);
935 RECORD(DECL_ACCESS_SPEC);
936 RECORD(DECL_FRIEND);
937 RECORD(DECL_FRIEND_TEMPLATE);
938 RECORD(DECL_CLASS_TEMPLATE);
939 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
940 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
941 RECORD(DECL_FUNCTION_TEMPLATE);
942 RECORD(DECL_TEMPLATE_TYPE_PARM);
943 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
944 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
945 RECORD(DECL_STATIC_ASSERT);
946 RECORD(DECL_CXX_BASE_SPECIFIERS);
947 RECORD(DECL_INDIRECTFIELD);
948 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
949
Douglas Gregora72d8c42011-06-03 02:27:19 +0000950 // Statements and Exprs can occur in the Decls and Types block.
951 AddStmtsExprs(Stream, Record);
952
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000953 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000954 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000955 RECORD(PPD_MACRO_DEFINITION);
956 RECORD(PPD_INCLUSION_DIRECTIVE);
957
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000958#undef RECORD
959#undef BLOCK
960 Stream.ExitBlock();
961}
962
Douglas Gregore650c8c2009-07-07 00:12:59 +0000963/// \brief Adjusts the given filename to only write out the portion of the
964/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000965///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000966/// \param Filename the file name to adjust.
967///
968/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
969/// the returned filename will be adjusted by this system root.
970///
971/// \returns either the original filename (if it needs no adjustment) or the
972/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000973static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000974adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregor832d6202011-07-22 16:35:34 +0000977 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 // Verify that the filename and the system root have the same prefix.
981 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000982 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 if (Filename[Pos] != isysroot[Pos])
984 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Douglas Gregore650c8c2009-07-07 00:12:59 +0000986 // We hit the end of the filename before we hit the end of the system root.
987 if (!Filename[Pos])
988 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregore650c8c2009-07-07 00:12:59 +0000990 // If the file name has a '/' at the current position, skip over the '/'.
991 // We distinguish sysroot-based includes from absolute includes by the
992 // absence of '/' at the beginning of sysroot-based includes.
993 if (Filename[Pos] == '/')
994 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Douglas Gregore650c8c2009-07-07 00:12:59 +0000996 return Filename + Pos;
997}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000998
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000999/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +00001000void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1001 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001002 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001003 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001004 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1005 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001006
Douglas Gregore650c8c2009-07-07 00:12:59 +00001007 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001008 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1012 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1013 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1014 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1015 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1016 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1017 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1018 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001019 Record.push_back(VERSION_MAJOR);
1020 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001021 Record.push_back(CLANG_VERSION_MAJOR);
1022 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001023 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001024 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001025 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1026 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001027
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001028 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001029 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001030 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001031 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001032 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001033
1034 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1035 M != MEnd; ++M) {
1036 // Skip modules that weren't directly imported.
1037 if (!(*M)->isDirectlyImported())
1038 continue;
1039
1040 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001041 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001042 Record.push_back((*M)->File->getSize());
1043 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001044 // FIXME: This writes the absolute path for AST files we depend on.
1045 const std::string &FileName = (*M)->FileName;
1046 Record.push_back(FileName.size());
1047 Record.append(FileName.begin(), FileName.end());
1048 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001049 Stream.EmitRecord(IMPORTS, Record);
1050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001052 // Language options.
1053 Record.clear();
1054 const LangOptions &LangOpts = Context.getLangOpts();
1055#define LANGOPT(Name, Bits, Default, Description) \
1056 Record.push_back(LangOpts.Name);
1057#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1058 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1059#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001060#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1061#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001062
1063 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1064 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1065
1066 Record.push_back(LangOpts.CurrentModule.size());
1067 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001068
1069 // Comment options.
1070 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1071 for (CommentOptions::BlockCommandNamesTy::const_iterator
1072 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1073 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1074 I != IEnd; ++I) {
1075 AddString(*I, Record);
1076 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001077 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001078
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001079 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1080
Douglas Gregoree097c12012-10-18 17:58:09 +00001081 // Target options.
1082 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001083 const TargetInfo &Target = Context.getTargetInfo();
1084 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001085 AddString(TargetOpts.Triple, Record);
1086 AddString(TargetOpts.CPU, Record);
1087 AddString(TargetOpts.ABI, Record);
1088 AddString(TargetOpts.CXXABI, Record);
1089 AddString(TargetOpts.LinkerVersion, Record);
1090 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1091 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1092 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1093 }
1094 Record.push_back(TargetOpts.Features.size());
1095 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1096 AddString(TargetOpts.Features[I], Record);
1097 }
1098 Stream.EmitRecord(TARGET_OPTIONS, Record);
1099
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001100 // Diagnostic options.
1101 Record.clear();
1102 const DiagnosticOptions &DiagOpts
1103 = Context.getDiagnostics().getDiagnosticOptions();
1104#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1105#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1106 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1107#include "clang/Basic/DiagnosticOptions.def"
1108 Record.push_back(DiagOpts.Warnings.size());
1109 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1110 AddString(DiagOpts.Warnings[I], Record);
1111 // Note: we don't serialize the log or serialization file names, because they
1112 // are generally transient files and will almost always be overridden.
1113 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1114
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001115 // File system options.
1116 Record.clear();
1117 const FileSystemOptions &FSOpts
1118 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1119 AddString(FSOpts.WorkingDir, Record);
1120 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1121
Douglas Gregorbbf38312012-10-24 16:50:34 +00001122 // Header search options.
1123 Record.clear();
1124 const HeaderSearchOptions &HSOpts
1125 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1126 AddString(HSOpts.Sysroot, Record);
1127
1128 // Include entries.
1129 Record.push_back(HSOpts.UserEntries.size());
1130 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1131 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1132 AddString(Entry.Path, Record);
1133 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001134 Record.push_back(Entry.IsFramework);
1135 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001136 }
1137
1138 // System header prefixes.
1139 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1140 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1141 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1142 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1143 }
1144
1145 AddString(HSOpts.ResourceDir, Record);
1146 AddString(HSOpts.ModuleCachePath, Record);
1147 Record.push_back(HSOpts.DisableModuleHash);
1148 Record.push_back(HSOpts.UseBuiltinIncludes);
1149 Record.push_back(HSOpts.UseStandardSystemIncludes);
1150 Record.push_back(HSOpts.UseStandardCXXIncludes);
1151 Record.push_back(HSOpts.UseLibcxx);
1152 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1153
Douglas Gregora71a7d82012-10-24 20:05:57 +00001154 // Preprocessor options.
1155 Record.clear();
1156 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1157
1158 // Macro definitions.
1159 Record.push_back(PPOpts.Macros.size());
1160 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1161 AddString(PPOpts.Macros[I].first, Record);
1162 Record.push_back(PPOpts.Macros[I].second);
1163 }
1164
1165 // Includes
1166 Record.push_back(PPOpts.Includes.size());
1167 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1168 AddString(PPOpts.Includes[I], Record);
1169
1170 // Macro includes
1171 Record.push_back(PPOpts.MacroIncludes.size());
1172 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1173 AddString(PPOpts.MacroIncludes[I], Record);
1174
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001175 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001176 // Detailed record is important since it is used for the module cache hash.
1177 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001178 AddString(PPOpts.ImplicitPCHInclude, Record);
1179 AddString(PPOpts.ImplicitPTHInclude, Record);
1180 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1181 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1182
Douglas Gregor31d375f2011-05-06 21:43:30 +00001183 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001184 SourceManager &SM = Context.getSourceManager();
1185 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1186 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001187 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1188 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001189 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1190 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1191
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001192 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001194 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001195
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001196 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001197 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001198 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001199 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001200 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001201 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001202 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001203 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001204
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001205 Record.clear();
1206 Record.push_back(SM.getMainFileID().getOpaqueValue());
1207 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1208
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001209 // Original PCH directory
1210 if (!OutputFile.empty() && OutputFile != "-") {
1211 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1212 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1214 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1215
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001216 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001217
1218 llvm::sys::fs::make_absolute(OutputPath);
1219 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1220
1221 RecordData Record;
1222 Record.push_back(ORIGINAL_PCH_DIR);
1223 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1224 }
1225
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001226 WriteInputFiles(Context.SourceMgr,
1227 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1228 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001229 Stream.ExitBlock();
1230}
1231
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001232namespace {
1233 /// \brief An input file.
1234 struct InputFileEntry {
1235 const FileEntry *File;
1236 bool IsSystemFile;
1237 bool BufferOverridden;
1238 };
1239}
1240
1241void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1242 HeaderSearchOptions &HSOpts,
1243 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001244 using namespace llvm;
1245 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1246 RecordData Record;
1247
1248 // Create input-file abbreviation.
1249 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1250 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001251 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001252 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1253 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001254 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001255 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1256 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1257
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001258 // Get all ContentCache objects for files, sorted by whether the file is a
1259 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001260 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001261 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1262 // Get this source location entry.
1263 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001264 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001265
1266 // We only care about file entries that were not overridden.
1267 if (!SLoc->isFile())
1268 continue;
1269 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001270 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001271 continue;
1272
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001273 InputFileEntry Entry;
1274 Entry.File = Cache->OrigEntry;
1275 Entry.IsSystemFile = Cache->IsSystemFile;
1276 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001277 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001278 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001279 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001280 SortedFiles.push_front(Entry);
1281 }
1282
1283 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1284 // the set of (non-system) input files. This is simple heuristic for
1285 // detecting whether the system headers may have changed, because it is too
1286 // expensive to stat() all of the system headers.
Richard Smithcc8e22b2013-05-20 23:40:27 +00001287 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001288 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001289 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1290 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1291 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1292 InputFileEntry Entry = { SDKSettingsFile, false, false };
1293 SortedFiles.push_front(Entry);
1294 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001295 }
1296
1297 unsigned UserFilesNum = 0;
1298 // Write out all of the input files.
1299 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001300 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001301 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001302 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001303
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001304 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001305 if (InputFileID != 0)
1306 continue; // already recorded this file.
1307
Douglas Gregora930dc92012-10-22 18:42:04 +00001308 // Record this entry's offset.
1309 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001310
1311 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001312
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001313 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001314 ++UserFilesNum;
1315
Douglas Gregor745e6f12012-10-19 00:38:02 +00001316 Record.clear();
1317 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001318 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001319
1320 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001321 Record.push_back(Entry.File->getSize());
1322 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001323
Douglas Gregora930dc92012-10-22 18:42:04 +00001324 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001325 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001326
Douglas Gregor745e6f12012-10-19 00:38:02 +00001327 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001328 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001329 SmallString<128> FilePath(Filename);
1330
1331 // Ask the file manager to fixup the relative path for us. This will
1332 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001333 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001334
1335 // FIXME: This call to make_absolute shouldn't be necessary, the
1336 // call to FixupRelativePath should always return an absolute path.
1337 llvm::sys::fs::make_absolute(FilePath);
1338 Filename = FilePath.c_str();
1339
1340 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1341
1342 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1343 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001344
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001345 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001346
1347 // Create input file offsets abbreviation.
1348 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1349 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1350 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001351 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1352 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001353 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1354 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1355
1356 // Write input file offsets.
1357 Record.clear();
1358 Record.push_back(INPUT_FILE_OFFSETS);
1359 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001360 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001361 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001362}
1363
Douglas Gregor14f79002009-04-10 03:52:48 +00001364//===----------------------------------------------------------------------===//
1365// Source Manager Serialization
1366//===----------------------------------------------------------------------===//
1367
1368/// \brief Create an abbreviation for the SLocEntry that refers to a
1369/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001370static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001371 using namespace llvm;
1372 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001373 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001378 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001383 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001384}
1385
1386/// \brief Create an abbreviation for the SLocEntry that refers to a
1387/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001388static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001389 using namespace llvm;
1390 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001391 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001397 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001398}
1399
1400/// \brief Create an abbreviation for the SLocEntry that refers to a
1401/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001402static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001403 using namespace llvm;
1404 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001405 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001407 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001408}
1409
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001410/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1411/// expansion.
1412static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001413 using namespace llvm;
1414 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001415 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001421 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001422}
1423
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001424namespace {
1425 // Trait used for the on-disk hash table of header search information.
1426 class HeaderFileInfoTrait {
1427 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001428 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001429
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001430 // Keep track of the framework names we've used during serialization.
1431 SmallVector<char, 128> FrameworkStringData;
1432 llvm::StringMap<unsigned> FrameworkNameOffset;
1433
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001434 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001435 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1436 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001437
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001438 struct key_type {
1439 const FileEntry *FE;
1440 const char *Filename;
1441 };
1442 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001443
1444 typedef HeaderFileInfo data_type;
1445 typedef const data_type &data_type_ref;
1446
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001447 static unsigned ComputeHash(key_type_ref key) {
1448 // The hash is based only on size/time of the file, so that the reader can
1449 // match even when symlinking or excess path elements ("foo/../", "../")
1450 // change the form of the name. However, complete path is still the key.
1451 return llvm::hash_combine(key.FE->getSize(),
1452 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001453 }
1454
1455 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001456 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1457 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1458 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001459 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001460 if (Data.isModuleHeader)
1461 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001462 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001463 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001464 }
1465
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001466 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1467 clang::io::Emit64(Out, key.FE->getSize());
1468 KeyLen -= 8;
1469 clang::io::Emit64(Out, key.FE->getModificationTime());
1470 KeyLen -= 8;
1471 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001472 }
1473
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001474 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001475 data_type_ref Data, unsigned DataLen) {
1476 using namespace clang::io;
1477 uint64_t Start = Out.tell(); (void)Start;
1478
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001479 unsigned char Flags = (Data.HeaderRole << 6)
1480 | (Data.isImport << 5)
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001481 | (Data.isPragmaOnce << 4)
1482 | (Data.DirInfo << 2)
1483 | (Data.Resolved << 1)
1484 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001485 Emit8(Out, (uint8_t)Flags);
1486 Emit16(Out, (uint16_t) Data.NumIncludes);
1487
1488 if (!Data.ControllingMacro)
1489 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1490 else
1491 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001492
1493 unsigned Offset = 0;
1494 if (!Data.Framework.empty()) {
1495 // If this header refers into a framework, save the framework name.
1496 llvm::StringMap<unsigned>::iterator Pos
1497 = FrameworkNameOffset.find(Data.Framework);
1498 if (Pos == FrameworkNameOffset.end()) {
1499 Offset = FrameworkStringData.size() + 1;
1500 FrameworkStringData.append(Data.Framework.begin(),
1501 Data.Framework.end());
1502 FrameworkStringData.push_back(0);
1503
1504 FrameworkNameOffset[Data.Framework] = Offset;
1505 } else
1506 Offset = Pos->second;
1507 }
1508 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001509
1510 if (Data.isModuleHeader) {
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00001511 Module *Mod = HS.findModuleForHeader(key.FE).getModule();
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001512 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1513 }
1514
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001515 assert(Out.tell() - Start == DataLen && "Wrong data length");
1516 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001517
1518 const char *strings_begin() const { return FrameworkStringData.begin(); }
1519 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001520 };
1521} // end anonymous namespace
1522
1523/// \brief Write the header search block for the list of files that
1524///
1525/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001526void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001527 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001528 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1529
1530 if (FilesByUID.size() > HS.header_file_size())
1531 FilesByUID.resize(HS.header_file_size());
1532
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001533 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001534 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001535 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001536 unsigned NumHeaderSearchEntries = 0;
1537 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1538 const FileEntry *File = FilesByUID[UID];
1539 if (!File)
1540 continue;
1541
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001542 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1543 // from the external source if it was not provided already.
1544 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001545 if (HFI.External && Chain)
1546 continue;
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001547 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1548 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001549
1550 // Turn the file name into an absolute path, if it isn't already.
1551 const char *Filename = File->getName();
1552 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1553
1554 // If we performed any translation on the file name at all, we need to
1555 // save this string, since the generator will refer to it later.
1556 if (Filename != File->getName()) {
1557 Filename = strdup(Filename);
1558 SavedStrings.push_back(Filename);
1559 }
1560
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001561 HeaderFileInfoTrait::key_type key = { File, Filename };
1562 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001563 ++NumHeaderSearchEntries;
1564 }
1565
1566 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001567 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001568 uint32_t BucketOffset;
1569 {
1570 llvm::raw_svector_ostream Out(TableData);
1571 // Make sure that no bucket is at offset 0
1572 clang::io::Emit32(Out, 0);
1573 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1574 }
1575
1576 // Create a blob abbreviation
1577 using namespace llvm;
1578 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1579 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1584 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1585
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001586 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001587 RecordData Record;
1588 Record.push_back(HEADER_SEARCH_TABLE);
1589 Record.push_back(BucketOffset);
1590 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001591 Record.push_back(TableData.size());
1592 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001593 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1594
1595 // Free all of the strings we had to duplicate.
1596 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001597 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001598}
1599
Douglas Gregor14f79002009-04-10 03:52:48 +00001600/// \brief Writes the block containing the serialized form of the
1601/// source manager.
1602///
1603/// TODO: We should probably use an on-disk hash table (stored in a
1604/// blob), indexed based on the file name, so that we only create
1605/// entries for files that we actually need. In the common case (no
1606/// errors), we probably won't have to create file entries for any of
1607/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001608void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001609 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001610 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001611 RecordData Record;
1612
Chris Lattnerf04ad692009-04-10 17:16:57 +00001613 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001614 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001615
1616 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001617 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1618 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1619 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001620 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001621
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001622 // Write out the source location entry table. We skip the first
1623 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001624 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001625 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001626 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1627 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001628 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001629 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001630 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001631 FileID FID = FileID::get(I);
1632 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001633
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001634 // Record the offset of this source-location entry.
1635 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1636
1637 // Figure out which record code to use.
1638 unsigned Code;
1639 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001640 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1641 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001642 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001643 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001644 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001645 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001646 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001647 Record.clear();
1648 Record.push_back(Code);
1649
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001650 // Starting offset of this entry within this module, so skip the dummy.
1651 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001652 if (SLoc->isFile()) {
1653 const SrcMgr::FileInfo &File = SLoc->getFile();
1654 Record.push_back(File.getIncludeLoc().getRawEncoding());
1655 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1656 Record.push_back(File.hasLineDirectives());
1657
1658 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001659 if (Content->OrigEntry) {
1660 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001661 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001662
Douglas Gregora930dc92012-10-22 18:42:04 +00001663 // The source location entry is a file. Emit input file ID.
1664 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1665 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001667 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001668
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001669 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001670 if (FDI != FileDeclIDs.end()) {
1671 Record.push_back(FDI->second->FirstDeclIndex);
1672 Record.push_back(FDI->second->DeclIDs.size());
1673 } else {
1674 Record.push_back(0);
1675 Record.push_back(0);
1676 }
Douglas Gregora081da52011-11-16 20:05:18 +00001677
Douglas Gregora930dc92012-10-22 18:42:04 +00001678 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001679
1680 if (Content->BufferOverridden) {
1681 Record.clear();
1682 Record.push_back(SM_SLOC_BUFFER_BLOB);
1683 const llvm::MemoryBuffer *Buffer
1684 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1685 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1686 StringRef(Buffer->getBufferStart(),
1687 Buffer->getBufferSize() + 1));
1688 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001689 } else {
1690 // The source location entry is a buffer. The blob associated
1691 // with this entry contains the contents of the buffer.
1692
1693 // We add one to the size so that we capture the trailing NULL
1694 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1695 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001696 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001697 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001698 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001699 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001700 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001701 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001702 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001703 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001704 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001705 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001706
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001707 if (strcmp(Name, "<built-in>") == 0) {
1708 PreloadSLocs.push_back(SLocEntryOffsets.size());
1709 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001710 }
1711 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001712 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001713 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001714 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1715 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001716 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1717 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001718
1719 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001720 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001721 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001722 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001723 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001724 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001725 }
1726 }
1727
Douglas Gregorc9490c02009-04-16 22:23:12 +00001728 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001729
1730 if (SLocEntryOffsets.empty())
1731 return;
1732
Sebastian Redl3397c552010-08-18 23:56:27 +00001733 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001734 // table is used for lazily loading source-location information.
1735 using namespace llvm;
1736 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001737 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001740 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1741 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001743 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001744 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001745 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001746 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001747 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001748
Sebastian Redl3397c552010-08-18 23:56:27 +00001749 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001750 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001751 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001752
1753 // Write the line table. It depends on remapping working, so it must come
1754 // after the source location offsets.
1755 if (SourceMgr.hasLineTable()) {
1756 LineTableInfo &LineTable = SourceMgr.getLineTable();
1757
1758 Record.clear();
1759 // Emit the file names
1760 Record.push_back(LineTable.getNumFilenames());
1761 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1762 // Emit the file name
1763 const char *Filename = LineTable.getFilename(I);
1764 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1765 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1766 Record.push_back(FilenameLen);
1767 if (FilenameLen)
1768 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1769 }
1770
1771 // Emit the line entries
1772 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1773 L != LEnd; ++L) {
1774 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001775 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001776 continue;
1777
1778 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001779 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001780
1781 // Emit the line entries
1782 Record.push_back(L->second.size());
1783 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1784 LEEnd = L->second.end();
1785 LE != LEEnd; ++LE) {
1786 Record.push_back(LE->FileOffset);
1787 Record.push_back(LE->LineNo);
1788 Record.push_back(LE->FilenameID);
1789 Record.push_back((unsigned)LE->FileKind);
1790 Record.push_back(LE->IncludeOffset);
1791 }
1792 }
1793 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1794 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001795}
1796
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001797//===----------------------------------------------------------------------===//
1798// Preprocessor Serialization
1799//===----------------------------------------------------------------------===//
1800
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001801namespace {
1802class ASTMacroTableTrait {
1803public:
1804 typedef IdentID key_type;
1805 typedef key_type key_type_ref;
1806
1807 struct Data {
1808 uint32_t MacroDirectivesOffset;
1809 };
1810
1811 typedef Data data_type;
1812 typedef const data_type &data_type_ref;
1813
1814 static unsigned ComputeHash(IdentID IdID) {
1815 return llvm::hash_value(IdID);
1816 }
1817
1818 std::pair<unsigned,unsigned>
1819 static EmitKeyDataLength(raw_ostream& Out,
1820 key_type_ref Key, data_type_ref Data) {
1821 unsigned KeyLen = 4; // IdentID.
1822 unsigned DataLen = 4; // MacroDirectivesOffset.
1823 return std::make_pair(KeyLen, DataLen);
1824 }
1825
1826 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1827 clang::io::Emit32(Out, Key);
1828 }
1829
1830 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1831 unsigned) {
1832 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1833 }
1834};
1835} // end anonymous namespace
1836
1837static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1838 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1839 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1840 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1841 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001842 return X.first->getName().compare(Y.first->getName());
1843}
1844
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001845static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1846 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001847 if (MacroInfo *MI = MD->getMacroInfo())
1848 if (MI->isBuiltinMacro())
1849 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001850
1851 if (IsModule) {
1852 SourceLocation Loc = MD->getLocation();
1853 if (Loc.isInvalid())
1854 return true;
1855 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1856 return true;
1857 }
1858
1859 return false;
1860}
1861
Chris Lattner0b1fb982009-04-10 17:15:23 +00001862/// \brief Writes the block containing the serialized form of the
1863/// preprocessor.
1864///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001865void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001866 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1867 if (PPRec)
1868 WritePreprocessorDetail(*PPRec);
1869
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001870 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001871
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001872 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1873 if (PP.getCounterValue() != 0) {
1874 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001875 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001876 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001877 }
1878
1879 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001880 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Sebastian Redl3397c552010-08-18 23:56:27 +00001882 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001883 // FIXME: use diagnostics subsystem for localization etc.
1884 if (PP.SawDateOrTime())
1885 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Douglas Gregorecdcb882010-10-20 22:00:55 +00001887
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001888 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001889 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001890
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001891 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001892 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001893 MacroDirectives;
1894 for (Preprocessor::macro_iterator
1895 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1896 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001897 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001898 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001899 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001900
Douglas Gregor9c736102011-02-10 18:20:09 +00001901 // Sort the set of macro definitions that need to be serialized by the
1902 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001903 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1904 &compareMacroDirectives);
1905
1906 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1907
1908 // Emit the macro directives as a list and associate the offset with the
1909 // identifier they belong to.
1910 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1911 const IdentifierInfo *Name = MacroDirectives[I].first;
1912 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1913 MacroDirective *MD = MacroDirectives[I].second;
1914
1915 // If the macro or identifier need no updates, don't write the macro history
1916 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001917 // FIXME: Chain the macro history instead of re-writing it.
1918 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001919 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1920 continue;
1921
1922 // Emit the macro directives in reverse source order.
1923 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001924 if (MD->isHidden())
1925 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001926 if (shouldIgnoreMacro(MD, IsModule, PP))
1927 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001928
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001929 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001930 Record.push_back(MD->getKind());
1931 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1932 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1933 Record.push_back(InfoID);
1934 Record.push_back(DefMD->isImported());
1935 Record.push_back(DefMD->isAmbiguous());
1936
1937 } else if (VisibilityMacroDirective *
1938 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1939 Record.push_back(VisMD->isPublic());
1940 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001941 }
1942 if (Record.empty())
1943 continue;
1944
1945 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1946 Record.clear();
1947
1948 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1949
1950 IdentID NameID = getIdentifierRef(Name);
1951 ASTMacroTableTrait::Data data;
1952 data.MacroDirectivesOffset = MacroDirectiveOffset;
1953 Generator.insert(NameID, data);
1954 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001955
Douglas Gregora8235d62012-10-09 23:05:51 +00001956 /// \brief Offsets of each of the macros into the bitstream, indexed by
1957 /// the local macro ID
1958 ///
1959 /// For each identifier that is associated with a macro, this map
1960 /// provides the offset into the bitstream where that macro is
1961 /// defined.
1962 std::vector<uint32_t> MacroOffsets;
1963
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001964 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1965 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1966 MacroInfo *MI = MacroInfosToEmit[I].MI;
1967 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001968
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001969 if (ID < FirstMacroID) {
1970 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1971 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001972 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001973
1974 // Record the local offset of this macro.
1975 unsigned Index = ID - FirstMacroID;
1976 if (Index == MacroOffsets.size())
1977 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1978 else {
1979 if (Index > MacroOffsets.size())
1980 MacroOffsets.resize(Index + 1);
1981
1982 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1983 }
1984
1985 AddIdentifierRef(Name, Record);
1986 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1987 AddSourceLocation(MI->getDefinitionLoc(), Record);
1988 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1989 Record.push_back(MI->isUsed());
1990 unsigned Code;
1991 if (MI->isObjectLike()) {
1992 Code = PP_MACRO_OBJECT_LIKE;
1993 } else {
1994 Code = PP_MACRO_FUNCTION_LIKE;
1995
1996 Record.push_back(MI->isC99Varargs());
1997 Record.push_back(MI->isGNUVarargs());
1998 Record.push_back(MI->hasCommaPasting());
1999 Record.push_back(MI->getNumArgs());
2000 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2001 I != E; ++I)
2002 AddIdentifierRef(*I, Record);
2003 }
2004
2005 // If we have a detailed preprocessing record, record the macro definition
2006 // ID that corresponds to this macro.
2007 if (PPRec)
2008 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2009
2010 Stream.EmitRecord(Code, Record);
2011 Record.clear();
2012
2013 // Emit the tokens array.
2014 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2015 // Note that we know that the preprocessor does not have any annotation
2016 // tokens in it because they are created by the parser, and thus can't
2017 // be in a macro definition.
2018 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002019 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002020 Stream.EmitRecord(PP_TOKEN, Record);
2021 Record.clear();
2022 }
2023 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002024 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002025
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002026 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002027
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002028 // Create the on-disk hash table in a buffer.
2029 SmallString<4096> MacroTable;
2030 uint32_t BucketOffset;
2031 {
2032 llvm::raw_svector_ostream Out(MacroTable);
2033 // Make sure that no bucket is at offset 0
2034 clang::io::Emit32(Out, 0);
2035 BucketOffset = Generator.Emit(Out);
2036 }
2037
2038 // Write the macro table
2039 using namespace llvm;
2040 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2041 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2044 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2045
2046 Record.push_back(MACRO_TABLE);
2047 Record.push_back(BucketOffset);
2048 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2049 Record.clear();
2050
Douglas Gregora8235d62012-10-09 23:05:51 +00002051 // Write the offsets table for macro IDs.
2052 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002053 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002054 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2058
2059 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2060 Record.clear();
2061 Record.push_back(MACRO_OFFSET);
2062 Record.push_back(MacroOffsets.size());
2063 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2064 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2065 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002066}
2067
2068void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002069 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002070 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002071
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002072 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002073
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002074 // Enter the preprocessor block.
2075 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002076
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002077 // If the preprocessor has a preprocessing record, emit it.
2078 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002079 using namespace llvm;
2080
2081 // Set up the abbreviation for
2082 unsigned InclusionAbbrev = 0;
2083 {
2084 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2085 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2091 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2092 }
2093
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002094 unsigned FirstPreprocessorEntityID
2095 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2096 + NUM_PREDEF_PP_ENTITY_IDS;
2097 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002098 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002099 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2100 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002101 E != EEnd;
2102 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002103 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002104
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002105 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2106 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002107
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002108 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002109 // Record this macro definition's ID.
2110 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002111
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002112 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002113 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2114 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002115 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002116
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002117 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002118 Record.push_back(ME->isBuiltinMacro());
2119 if (ME->isBuiltinMacro())
2120 AddIdentifierRef(ME->getName(), Record);
2121 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002122 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002123 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002124 continue;
2125 }
2126
2127 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2128 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002129 Record.push_back(ID->getFileName().size());
2130 Record.push_back(ID->wasInQuotes());
2131 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002132 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002133 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002134 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002135 // Check that the FileEntry is not null because it was not resolved and
2136 // we create a PCH even with compiler errors.
2137 if (ID->getFile())
2138 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002139 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2140 continue;
2141 }
2142
2143 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2144 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002145 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002146
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002147 // Write the offsets table for the preprocessing record.
2148 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002149 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2150
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002151 // Write the offsets table for identifier IDs.
2152 using namespace llvm;
2153 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002154 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002157 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002158
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002159 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002160 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002161 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002162 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2163 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002164 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002165}
2166
Douglas Gregore209e502011-12-06 01:10:29 +00002167unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2168 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2169 if (Known != SubmoduleIDs.end())
2170 return Known->second;
2171
2172 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2173}
2174
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002175unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2176 if (!Mod)
2177 return 0;
2178
2179 llvm::DenseMap<Module *, unsigned>::const_iterator
2180 Known = SubmoduleIDs.find(Mod);
2181 if (Known != SubmoduleIDs.end())
2182 return Known->second;
2183
2184 return 0;
2185}
2186
Douglas Gregor26ced122011-12-01 00:59:36 +00002187/// \brief Compute the number of modules within the given tree (including the
2188/// given module).
2189static unsigned getNumberOfModules(Module *Mod) {
2190 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002191 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2192 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002193 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002194 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002195
2196 return ChildModules + 1;
2197}
2198
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002199void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002200 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002201 // FIXME: This feels like it belongs somewhere else, but there are no
2202 // other consumers of this information.
2203 SourceManager &SrcMgr = PP->getSourceManager();
2204 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2205 for (ASTContext::import_iterator I = Context->local_import_begin(),
2206 IEnd = Context->local_import_end();
2207 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002208 if (Module *ImportedFrom
2209 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2210 SrcMgr))) {
2211 ImportedFrom->Imports.push_back(I->getImportedModule());
2212 }
2213 }
2214
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002215 // Enter the submodule description block.
2216 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2217
2218 // Write the abbreviations needed for the submodules block.
2219 using namespace llvm;
2220 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2221 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2232 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2233
2234 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002235 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2237 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2238
2239 Abbrev = new BitCodeAbbrev();
2240 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2242 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002243
2244 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002245 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2247 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2248
2249 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002250 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2252 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2253
Douglas Gregor51f564f2011-12-31 04:05:44 +00002254 Abbrev = new BitCodeAbbrev();
2255 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2257 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2258
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002259 Abbrev = new BitCodeAbbrev();
2260 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2262 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2263
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002264 Abbrev = new BitCodeAbbrev();
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002265 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2267 unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2268
2269 Abbrev = new BitCodeAbbrev();
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002270 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2273 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2274
Douglas Gregor63a72682013-03-20 00:22:05 +00002275 Abbrev = new BitCodeAbbrev();
2276 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2278 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2279
Douglas Gregor906d66a2013-03-20 21:10:35 +00002280 Abbrev = new BitCodeAbbrev();
2281 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2284 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2285
Douglas Gregor26ced122011-12-01 00:59:36 +00002286 // Write the submodule metadata block.
2287 RecordData Record;
2288 Record.push_back(getNumberOfModules(WritingModule));
2289 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2290 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2291
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002292 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002293 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002294 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002295 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002296 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002297 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002298 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002299
2300 // Emit the definition of the block.
2301 Record.clear();
2302 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002303 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002304 if (Mod->Parent) {
2305 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2306 Record.push_back(SubmoduleIDs[Mod->Parent]);
2307 } else {
2308 Record.push_back(0);
2309 }
2310 Record.push_back(Mod->IsFramework);
2311 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002312 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002313 Record.push_back(Mod->InferSubmodules);
2314 Record.push_back(Mod->InferExplicitSubmodules);
2315 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002316 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002317 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2318
Douglas Gregor51f564f2011-12-31 04:05:44 +00002319 // Emit the requirements.
2320 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2321 Record.clear();
2322 Record.push_back(SUBMODULE_REQUIRES);
2323 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2324 Mod->Requires[I].data(),
2325 Mod->Requires[I].size());
2326 }
2327
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002328 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002329 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002330 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002331 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002332 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002333 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002334 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2335 Record.clear();
2336 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2337 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2338 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002339 }
2340
2341 // Emit the headers.
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002342 for (unsigned I = 0, N = Mod->NormalHeaders.size(); I != N; ++I) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002343 Record.clear();
2344 Record.push_back(SUBMODULE_HEADER);
2345 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002346 Mod->NormalHeaders[I]->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002347 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002348 // Emit the excluded headers.
2349 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2350 Record.clear();
2351 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2352 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2353 Mod->ExcludedHeaders[I]->getName());
2354 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +00002355 // Emit the private headers.
2356 for (unsigned I = 0, N = Mod->PrivateHeaders.size(); I != N; ++I) {
2357 Record.clear();
2358 Record.push_back(SUBMODULE_PRIVATE_HEADER);
2359 Stream.EmitRecordWithBlob(PrivateHeaderAbbrev, Record,
2360 Mod->PrivateHeaders[I]->getName());
2361 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002362 ArrayRef<const FileEntry *>
2363 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2364 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002365 Record.clear();
2366 Record.push_back(SUBMODULE_TOPHEADER);
2367 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002368 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002369 }
Douglas Gregor55988682011-12-05 16:33:54 +00002370
2371 // Emit the imports.
2372 if (!Mod->Imports.empty()) {
2373 Record.clear();
2374 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002375 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002376 assert(ImportedID && "Unknown submodule!");
2377 Record.push_back(ImportedID);
2378 }
2379 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2380 }
2381
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002382 // Emit the exports.
2383 if (!Mod->Exports.empty()) {
2384 Record.clear();
2385 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002386 if (Module *Exported = Mod->Exports[I].getPointer()) {
2387 unsigned ExportedID = SubmoduleIDs[Exported];
2388 assert(ExportedID > 0 && "Unknown submodule ID?");
2389 Record.push_back(ExportedID);
2390 } else {
2391 Record.push_back(0);
2392 }
2393
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002394 Record.push_back(Mod->Exports[I].getInt());
2395 }
2396 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2397 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002398
2399 // Emit the link libraries.
2400 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2401 Record.clear();
2402 Record.push_back(SUBMODULE_LINK_LIBRARY);
2403 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2404 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2405 Mod->LinkLibraries[I].Library);
2406 }
2407
Douglas Gregor906d66a2013-03-20 21:10:35 +00002408 // Emit the conflicts.
2409 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2410 Record.clear();
2411 Record.push_back(SUBMODULE_CONFLICT);
2412 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2413 assert(OtherID && "Unknown submodule!");
2414 Record.push_back(OtherID);
2415 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2416 Mod->Conflicts[I].Message);
2417 }
2418
Douglas Gregor63a72682013-03-20 00:22:05 +00002419 // Emit the configuration macros.
2420 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2421 Record.clear();
2422 Record.push_back(SUBMODULE_CONFIG_MACRO);
2423 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2424 Mod->ConfigMacros[I]);
2425 }
2426
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002427 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002428 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2429 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002430 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002431 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002432 }
2433
2434 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002435
2436 assert((NextSubmoduleID - FirstSubmoduleID
2437 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002438}
2439
Douglas Gregor185dbd72011-12-01 02:07:58 +00002440serialization::SubmoduleID
2441ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002442 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002443 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002444
2445 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002446 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002447 Module *OwningMod
2448 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002449 if (!OwningMod)
2450 return 0;
2451
Douglas Gregore209e502011-12-06 01:10:29 +00002452 // Check whether this submodule is part of our own module.
2453 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002454 return 0;
2455
Douglas Gregore209e502011-12-06 01:10:29 +00002456 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002457}
2458
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002459void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2460 bool isModule) {
2461 // Make sure set diagnostic pragmas don't affect the translation unit that
2462 // imports the module.
2463 // FIXME: Make diagnostic pragma sections work properly with modules.
2464 if (isModule)
2465 return;
2466
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002467 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2468 DiagStateIDMap;
2469 unsigned CurrID = 0;
2470 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002471 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002472 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002473 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2474 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002475 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002476 if (point.Loc.isInvalid())
2477 continue;
2478
2479 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002480 unsigned &DiagStateID = DiagStateIDMap[point.State];
2481 Record.push_back(DiagStateID);
2482
2483 if (DiagStateID == 0) {
2484 DiagStateID = ++CurrID;
2485 for (DiagnosticsEngine::DiagState::const_iterator
2486 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2487 if (I->second.isPragma()) {
2488 Record.push_back(I->first);
2489 Record.push_back(I->second.getMapping());
2490 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002491 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002492 Record.push_back(-1); // mark the end of the diag/map pairs for this
2493 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002494 }
2495 }
2496
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002497 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002498 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002499}
2500
Anders Carlssonc8505782011-03-06 18:41:18 +00002501void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2502 if (CXXBaseSpecifiersOffsets.empty())
2503 return;
2504
2505 RecordData Record;
2506
2507 // Create a blob abbreviation for the C++ base specifiers offsets.
2508 using namespace llvm;
2509
2510 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2511 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2514 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2515
Douglas Gregore92b8a12011-08-04 00:01:48 +00002516 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002517 Record.clear();
2518 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2519 Record.push_back(CXXBaseSpecifiersOffsets.size());
2520 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002521 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002522}
2523
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002524//===----------------------------------------------------------------------===//
2525// Type Serialization
2526//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002527
Sebastian Redl3397c552010-08-18 23:56:27 +00002528/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002529void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002530 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002531 if (Idx.getIndex() == 0) // we haven't seen this type before.
2532 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002533
Douglas Gregor97475832010-10-05 18:37:06 +00002534 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002535
Douglas Gregor2cf26342009-04-09 22:27:44 +00002536 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002537 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002538 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002539 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002540 else if (TypeOffsets.size() < Index) {
2541 TypeOffsets.resize(Index + 1);
2542 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002543 }
2544
2545 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Douglas Gregor2cf26342009-04-09 22:27:44 +00002547 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002548 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002549
Douglas Gregora4923eb2009-11-16 21:35:15 +00002550 if (T.hasLocalNonFastQualifiers()) {
2551 Qualifiers Qs = T.getLocalQualifiers();
2552 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002553 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002554 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002555 } else {
2556 switch (T->getTypeClass()) {
2557 // For all of the concrete, non-dependent types, call the
2558 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002559#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002560 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002561#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002562#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002563 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002564 }
2565
2566 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002567 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002568
2569 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002570 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002571}
2572
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002573//===----------------------------------------------------------------------===//
2574// Declaration Serialization
2575//===----------------------------------------------------------------------===//
2576
Douglas Gregor2cf26342009-04-09 22:27:44 +00002577/// \brief Write the block containing all of the declaration IDs
2578/// lexically declared within the given DeclContext.
2579///
2580/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2581/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002582uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002583 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002584 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002585 return 0;
2586
Douglas Gregorc9490c02009-04-16 22:23:12 +00002587 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002588 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002590 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002591 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2592 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002593 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002594
Douglas Gregor25123082009-04-22 22:34:57 +00002595 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002596 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002597 return Offset;
2598}
2599
Sebastian Redla4232eb2010-08-18 23:56:21 +00002600void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002601 using namespace llvm;
2602 RecordData Record;
2603
2604 // Write the type offsets array
2605 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002606 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2610 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2611 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002613 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002614 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002615 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002616
2617 // Write the declaration offsets array
2618 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002619 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002620 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002621 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002622 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2623 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2624 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002626 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002627 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002628 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002629}
2630
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002631void ASTWriter::WriteFileDeclIDsMap() {
2632 using namespace llvm;
2633 RecordData Record;
2634
2635 // Join the vectors of DeclIDs from all files.
2636 SmallVector<DeclID, 256> FileSortedIDs;
2637 for (FileDeclIDsTy::iterator
2638 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2639 DeclIDInFileInfo &Info = *FI->second;
2640 Info.FirstDeclIndex = FileSortedIDs.size();
2641 for (LocDeclIDsTy::iterator
2642 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2643 FileSortedIDs.push_back(DI->second);
2644 }
2645
2646 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2647 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002648 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002649 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2650 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2651 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002652 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002653 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2654}
2655
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002656void ASTWriter::WriteComments() {
2657 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002658 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002659 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002660 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2661 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002662 I != E; ++I) {
2663 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002664 AddSourceRange((*I)->getSourceRange(), Record);
2665 Record.push_back((*I)->getKind());
2666 Record.push_back((*I)->isTrailingComment());
2667 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002668 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2669 }
2670 Stream.ExitBlock();
2671}
2672
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002673//===----------------------------------------------------------------------===//
2674// Global Method Pool and Selector Serialization
2675//===----------------------------------------------------------------------===//
2676
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002677namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002678// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002679class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002680 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002681
2682public:
2683 typedef Selector key_type;
2684 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Sebastian Redl5d050072010-08-04 17:20:04 +00002686 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002687 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002688 ObjCMethodList Instance, Factory;
2689 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002690 typedef const data_type& data_type_ref;
2691
Sebastian Redl3397c552010-08-18 23:56:27 +00002692 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002694 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002695 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002696 }
Mike Stump1eb44332009-09-09 15:08:12 +00002697
2698 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002699 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002700 data_type_ref Methods) {
2701 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2702 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002703 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2704 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002705 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002706 if (Method->Method)
2707 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002708 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002709 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002710 if (Method->Method)
2711 DataLen += 4;
2712 clang::io::Emit16(Out, DataLen);
2713 return std::make_pair(KeyLen, DataLen);
2714 }
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002717 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002718 assert((Start >> 32) == 0 && "Selector key offset too large");
2719 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002720 unsigned N = Sel.getNumArgs();
2721 clang::io::Emit16(Out, N);
2722 if (N == 0)
2723 N = 1;
2724 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002725 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002726 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2727 }
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Chris Lattner5f9e2722011-07-23 10:55:15 +00002729 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002730 data_type_ref Methods, unsigned DataLen) {
2731 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002732 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002733 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002734 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002735 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002736 if (Method->Method)
2737 ++NumInstanceMethods;
2738
2739 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002740 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002741 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002742 if (Method->Method)
2743 ++NumFactoryMethods;
2744
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002745 unsigned InstanceBits = Methods.Instance.getBits();
2746 assert(InstanceBits < 4);
2747 unsigned NumInstanceMethodsAndBits =
2748 (NumInstanceMethods << 2) | InstanceBits;
2749 unsigned FactoryBits = Methods.Factory.getBits();
2750 assert(FactoryBits < 4);
2751 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2752 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2753 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002754 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002755 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002756 if (Method->Method)
2757 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002758 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002759 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002760 if (Method->Method)
2761 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002762
2763 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002764 }
2765};
2766} // end anonymous namespace
2767
Sebastian Redl059612d2010-08-03 21:58:15 +00002768/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002769///
2770/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002771/// in an on-disk hash table indexed by the selector. The hash table also
2772/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002773void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002774 using namespace llvm;
2775
Sebastian Redl059612d2010-08-03 21:58:15 +00002776 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002777 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002778 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002779 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002780 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002781 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002782 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002783 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002784
Sebastian Redl059612d2010-08-03 21:58:15 +00002785 // Create the on-disk hash table representation. We walk through every
2786 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002787 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002788 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002789 I = SelectorIDs.begin(), E = SelectorIDs.end();
2790 I != E; ++I) {
2791 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002792 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002793 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002794 I->second,
2795 ObjCMethodList(),
2796 ObjCMethodList()
2797 };
2798 if (F != SemaRef.MethodPool.end()) {
2799 Data.Instance = F->second.first;
2800 Data.Factory = F->second.second;
2801 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002802 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002803 // changed.
2804 if (Chain && I->second < FirstSelectorID) {
2805 // Selector already exists. Did it change?
2806 bool changed = false;
2807 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002808 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002809 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002810 changed = true;
2811 }
2812 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002813 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002814 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002815 changed = true;
2816 }
2817 if (!changed)
2818 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002819 } else if (Data.Instance.Method || Data.Factory.Method) {
2820 // A new method pool entry.
2821 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002822 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002823 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002824 }
2825
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002826 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002827 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002828 uint32_t BucketOffset;
2829 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002830 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002831 llvm::raw_svector_ostream Out(MethodPool);
2832 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002833 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002834 BucketOffset = Generator.Emit(Out, Trait);
2835 }
2836
2837 // Create a blob abbreviation
2838 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002839 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2843 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2844
Douglas Gregor83941df2009-04-25 17:48:32 +00002845 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002846 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002847 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002848 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002849 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002850 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002851
2852 // Create a blob abbreviation for the selector table offsets.
2853 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002854 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2858 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2859
2860 // Write the selector offsets table.
2861 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002862 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002863 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002864 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002865 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002866 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002867 }
2868}
2869
Sebastian Redl3397c552010-08-18 23:56:27 +00002870/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002871void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002872 using namespace llvm;
2873 if (SemaRef.ReferencedSelectors.empty())
2874 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002875
Fariborz Jahanian32019832010-07-23 19:11:11 +00002876 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002877
Sebastian Redl3397c552010-08-18 23:56:27 +00002878 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002879 // very tricky to fix, and given that @selector shouldn't really appear in
2880 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002881 for (DenseMap<Selector, SourceLocation>::iterator S =
2882 SemaRef.ReferencedSelectors.begin(),
2883 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2884 Selector Sel = (*S).first;
2885 SourceLocation Loc = (*S).second;
2886 AddSelectorRef(Sel, Record);
2887 AddSourceLocation(Loc, Record);
2888 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002889 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002890}
2891
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002892//===----------------------------------------------------------------------===//
2893// Identifier Table Serialization
2894//===----------------------------------------------------------------------===//
2895
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002896namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002897class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002898 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002899 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002900 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002901 bool IsModule;
2902
Douglas Gregora92193e2009-04-28 21:18:29 +00002903 /// \brief Determines whether this is an "interesting" identifier
2904 /// that needs a full IdentifierInfo structure written into the hash
2905 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002906 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002907 if (II->isPoisoned() ||
2908 II->isExtensionToken() ||
2909 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002910 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002911 II->getFETokenInfo<void>())
2912 return true;
2913
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002914 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002915 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002916
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002917 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002918 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002919 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002920
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002921 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2922 if (!IsModule)
2923 return !shouldIgnoreMacro(Macro, IsModule, PP);
2924 SubmoduleID ModID;
2925 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2926 return true;
2927 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002928
2929 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002930 }
2931
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002932 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2933 SubmoduleID &ModID) {
2934 ModID = 0;
2935 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2936 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2937 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002938 return 0;
2939 }
2940
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002941 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2942 SubmoduleID &ModID) {
2943 if (DefMacroDirective *
2944 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2945 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2946 return DefMD;
2947 return 0;
2948 }
2949
2950 /// \brief Traverses the macro directives history and returns the latest
2951 /// macro that is public and not undefined in the same submodule.
2952 /// A macro that is defined in submodule A and undefined in submodule B,
2953 /// will still be considered as defined/exported from submodule A.
2954 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2955 SubmoduleID &ModID) {
2956 if (!MD)
2957 return 0;
2958
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002959 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002960 bool isUndefined = false;
2961 Optional<bool> isPublic;
2962 for (; MD; MD = MD->getPrevious()) {
2963 if (MD->isHidden())
2964 continue;
2965
2966 SubmoduleID ThisModID = getSubmoduleID(MD);
2967 if (ThisModID == 0) {
2968 isUndefined = false;
2969 isPublic = Optional<bool>();
2970 continue;
2971 }
2972 if (ThisModID != ModID){
2973 ModID = ThisModID;
2974 isUndefined = false;
2975 isPublic = Optional<bool>();
2976 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002977 // We are looking for a definition in a different submodule than the one
2978 // that we started with. If a submodule has re-definitions of the same
2979 // macro, only the last definition will be used as the "exported" one.
2980 if (ModID == OrigModID)
2981 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002982
2983 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2984 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2985 return DefMD;
2986 continue;
2987 }
2988
2989 if (isa<UndefMacroDirective>(MD)) {
2990 isUndefined = true;
2991 continue;
2992 }
2993
2994 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2995 if (!isPublic.hasValue())
2996 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002997 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002998
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002999 return 0;
3000 }
3001
3002 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003003 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
3004 MacroInfo *MI = DefMD->getInfo();
3005 if (unsigned ID = MI->getOwningModuleID())
3006 return ID;
3007 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
3008 }
3009 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003010 }
3011
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003012public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003013 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003014 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003016 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003017 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Douglas Gregoreee242f2011-10-27 09:33:13 +00003019 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3020 IdentifierResolver &IdResolver, bool IsModule)
3021 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003022
3023 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003024 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003025 }
Mike Stump1eb44332009-09-09 15:08:12 +00003026
3027 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003028 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003029 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003030 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003031 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003032 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003033 DataLen += 2; // 2 bytes for builtin ID
3034 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003035 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003036 DataLen += 4; // MacroDirectives offset.
3037 if (IsModule) {
3038 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003039 for (DefMacroDirective *
3040 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3041 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003042 DataLen += 4; // MacroInfo ID.
3043 }
3044 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003045 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003046 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003047
Douglas Gregoreee242f2011-10-27 09:33:13 +00003048 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3049 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003050 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003051 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003052 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003053 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003054 // We emit the key length after the data length so that every
3055 // string is preceded by a 16-bit length. This matches the PTH
3056 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003057 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003058 return std::make_pair(KeyLen, DataLen);
3059 }
Mike Stump1eb44332009-09-09 15:08:12 +00003060
Chris Lattner5f9e2722011-07-23 10:55:15 +00003061 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003062 unsigned KeyLen) {
3063 // Record the location of the key data. This is used when generating
3064 // the mapping from persistent IDs to strings.
3065 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003066 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003067 }
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Douglas Gregor7143aab2011-09-01 17:04:32 +00003069 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003070 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003071 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003072 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003073 clang::io::Emit32(Out, ID << 1);
3074 return;
3075 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003076
Douglas Gregora92193e2009-04-28 21:18:29 +00003077 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003078 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3079 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3080 clang::io::Emit16(Out, Bits);
3081 Bits = 0;
3082 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003083 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003084 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003085 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3086 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003087 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003088 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003089 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003090
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003091 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003092 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3093 if (IsModule) {
3094 // Write the IDs of macros coming from different submodules.
3095 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003096 for (DefMacroDirective *
3097 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3098 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3099 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003100 assert(InfoID);
3101 clang::io::Emit32(Out, InfoID);
3102 }
3103 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003104 }
Douglas Gregor13292642011-12-02 15:45:10 +00003105 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003106
Douglas Gregor668c1a42009-04-21 22:25:48 +00003107 // Emit the declaration IDs in reverse order, because the
3108 // IdentifierResolver provides the declarations as they would be
3109 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003110 // "stat"), but the ASTReader adds declarations to the end of the list
3111 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003112 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003113 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3114 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003115 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003116 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003117 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003118 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3119 }
3120
3121 /// \brief Returns the most recent local decl or the given decl if there are
3122 /// no local ones. The given decl is assumed to be the most recent one.
3123 Decl *getMostRecentLocalDecl(Decl *Orig) {
3124 // The only way a "from AST file" decl would be more recent from a local one
3125 // is if it came from a module.
3126 if (!PP.getLangOpts().Modules)
3127 return Orig;
3128
3129 // Look for a local in the decl chain.
3130 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3131 if (!D->isFromASTFile())
3132 return D;
3133 // If we come up a decl from a (chained-)PCH stop since we won't find a
3134 // local one.
3135 if (D->getOwningModuleID() == 0)
3136 break;
3137 }
3138
3139 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003140 }
3141};
3142} // end anonymous namespace
3143
Sebastian Redl3397c552010-08-18 23:56:27 +00003144/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003145///
3146/// The identifier table consists of a blob containing string data
3147/// (the actual identifiers themselves) and a separate "offsets" index
3148/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003149void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3150 IdentifierResolver &IdResolver,
3151 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003152 using namespace llvm;
3153
3154 // Create and write out the blob that contains the identifier
3155 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003156 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003157 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003158 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Douglas Gregor92b059e2009-04-28 20:33:11 +00003160 // Look for any identifiers that were named while processing the
3161 // headers, but are otherwise not needed. We add these to the hash
3162 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003163 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003164 // file.
3165 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3166 IDEnd = PP.getIdentifierTable().end();
3167 ID != IDEnd; ++ID)
3168 getIdentifierRef(ID->second);
3169
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003170 // Create the on-disk hash table representation. We only store offsets
3171 // for identifiers that appear here for the first time.
3172 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003173 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003174 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3175 ID != IDEnd; ++ID) {
3176 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003177 if (!Chain || !ID->first->isFromAST() ||
3178 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003179 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003180 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003181 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003182
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003183 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003184 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003185 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003186 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003187 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003188 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003189 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003190 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003191 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003192 }
3193
3194 // Create a blob abbreviation
3195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003196 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003199 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003200
3201 // Write the identifier table
3202 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003203 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003204 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003205 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003206 }
3207
3208 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003210 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3214 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3215
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003216#ifndef NDEBUG
3217 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3218 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3219#endif
3220
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003221 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003222 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003223 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003224 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003225 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003226 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003227}
3228
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003229//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003230// DeclContext's Name Lookup Table Serialization
3231//===----------------------------------------------------------------------===//
3232
3233namespace {
3234// Trait used for the on-disk hash table used in the method pool.
3235class ASTDeclContextNameLookupTrait {
3236 ASTWriter &Writer;
3237
3238public:
3239 typedef DeclarationName key_type;
3240 typedef key_type key_type_ref;
3241
3242 typedef DeclContext::lookup_result data_type;
3243 typedef const data_type& data_type_ref;
3244
3245 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3246
3247 unsigned ComputeHash(DeclarationName Name) {
3248 llvm::FoldingSetNodeID ID;
3249 ID.AddInteger(Name.getNameKind());
3250
3251 switch (Name.getNameKind()) {
3252 case DeclarationName::Identifier:
3253 ID.AddString(Name.getAsIdentifierInfo()->getName());
3254 break;
3255 case DeclarationName::ObjCZeroArgSelector:
3256 case DeclarationName::ObjCOneArgSelector:
3257 case DeclarationName::ObjCMultiArgSelector:
3258 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3259 break;
3260 case DeclarationName::CXXConstructorName:
3261 case DeclarationName::CXXDestructorName:
3262 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003263 break;
3264 case DeclarationName::CXXOperatorName:
3265 ID.AddInteger(Name.getCXXOverloadedOperator());
3266 break;
3267 case DeclarationName::CXXLiteralOperatorName:
3268 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3269 case DeclarationName::CXXUsingDirective:
3270 break;
3271 }
3272
3273 return ID.ComputeHash();
3274 }
3275
3276 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003277 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003278 data_type_ref Lookup) {
3279 unsigned KeyLen = 1;
3280 switch (Name.getNameKind()) {
3281 case DeclarationName::Identifier:
3282 case DeclarationName::ObjCZeroArgSelector:
3283 case DeclarationName::ObjCOneArgSelector:
3284 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003285 case DeclarationName::CXXLiteralOperatorName:
3286 KeyLen += 4;
3287 break;
3288 case DeclarationName::CXXOperatorName:
3289 KeyLen += 1;
3290 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003291 case DeclarationName::CXXConstructorName:
3292 case DeclarationName::CXXDestructorName:
3293 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003294 case DeclarationName::CXXUsingDirective:
3295 break;
3296 }
3297 clang::io::Emit16(Out, KeyLen);
3298
3299 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003300 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003301 clang::io::Emit16(Out, DataLen);
3302
3303 return std::make_pair(KeyLen, DataLen);
3304 }
3305
Chris Lattner5f9e2722011-07-23 10:55:15 +00003306 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003307 using namespace clang::io;
3308
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003309 Emit8(Out, Name.getNameKind());
3310 switch (Name.getNameKind()) {
3311 case DeclarationName::Identifier:
3312 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003313 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003314 case DeclarationName::ObjCZeroArgSelector:
3315 case DeclarationName::ObjCOneArgSelector:
3316 case DeclarationName::ObjCMultiArgSelector:
3317 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003318 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003319 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003320 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3321 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003322 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003323 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003324 case DeclarationName::CXXLiteralOperatorName:
3325 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003326 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003327 case DeclarationName::CXXConstructorName:
3328 case DeclarationName::CXXDestructorName:
3329 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003330 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003331 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003332 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003333
3334 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003335 }
3336
Chris Lattner5f9e2722011-07-23 10:55:15 +00003337 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003338 data_type Lookup, unsigned DataLen) {
3339 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003340 clang::io::Emit16(Out, Lookup.size());
3341 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3342 I != E; ++I)
3343 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003344
3345 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3346 }
3347};
3348} // end anonymous namespace
3349
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003350/// \brief Write the block containing all of the declaration IDs
3351/// visible from the given DeclContext.
3352///
3353/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003354/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003355uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3356 DeclContext *DC) {
3357 if (DC->getPrimaryContext() != DC)
3358 return 0;
3359
3360 // Since there is no name lookup into functions or methods, don't bother to
3361 // build a visible-declarations table for these entities.
3362 if (DC->isFunctionOrMethod())
3363 return 0;
3364
3365 // If not in C++, we perform name lookup for the translation unit via the
3366 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003367 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003368 return 0;
3369
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003370 // Serialize the contents of the mapping used for lookup. Note that,
3371 // although we have two very different code paths, the serialized
3372 // representation is the same for both cases: a declaration name,
3373 // followed by a size, followed by references to the visible
3374 // declarations that have that name.
3375 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003376 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003377 if (!Map || Map->empty())
3378 return 0;
3379
3380 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3381 ASTDeclContextNameLookupTrait Trait(*this);
3382
3383 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003384 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003385 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003386 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3387 D != DEnd; ++D) {
3388 DeclarationName Name = D->first;
3389 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003390 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003391 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3392 // Hash all conversion function names to the same name. The actual
3393 // type information in conversion function name is not used in the
3394 // key (since such type information is not stable across different
3395 // modules), so the intended effect is to coalesce all of the conversion
3396 // functions under a single key.
3397 if (!ConversionName)
3398 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003399 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003400 continue;
3401 }
3402
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003403 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003404 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003405 }
3406
Douglas Gregore5a54b62011-08-30 20:49:19 +00003407 // Add the conversion functions
3408 if (!ConversionDecls.empty()) {
3409 Generator.insert(ConversionName,
3410 DeclContext::lookup_result(ConversionDecls.begin(),
3411 ConversionDecls.end()),
3412 Trait);
3413 }
3414
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003415 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003416 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003417 uint32_t BucketOffset;
3418 {
3419 llvm::raw_svector_ostream Out(LookupTable);
3420 // Make sure that no bucket is at offset 0
3421 clang::io::Emit32(Out, 0);
3422 BucketOffset = Generator.Emit(Out, Trait);
3423 }
3424
3425 // Write the lookup table
3426 RecordData Record;
3427 Record.push_back(DECL_CONTEXT_VISIBLE);
3428 Record.push_back(BucketOffset);
3429 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3430 LookupTable.str());
3431
3432 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3433 ++NumVisibleDeclContexts;
3434 return Offset;
3435}
3436
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003437/// \brief Write an UPDATE_VISIBLE block for the given context.
3438///
3439/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3440/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003441/// (in C++), for namespaces, and for classes with forward-declared unscoped
3442/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003443void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003444 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3445 if (!Map || Map->empty())
3446 return;
3447
3448 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3449 ASTDeclContextNameLookupTrait Trait(*this);
3450
3451 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003452 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3453 D != DEnd; ++D) {
3454 DeclarationName Name = D->first;
3455 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003456 // For any name that appears in this table, the results are complete, i.e.
3457 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003458 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003459 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003460 }
3461
3462 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003463 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003464 uint32_t BucketOffset;
3465 {
3466 llvm::raw_svector_ostream Out(LookupTable);
3467 // Make sure that no bucket is at offset 0
3468 clang::io::Emit32(Out, 0);
3469 BucketOffset = Generator.Emit(Out, Trait);
3470 }
3471
3472 // Write the lookup table
3473 RecordData Record;
3474 Record.push_back(UPDATE_VISIBLE);
3475 Record.push_back(getDeclID(cast<Decl>(DC)));
3476 Record.push_back(BucketOffset);
3477 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3478}
3479
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003480/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3481void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3482 RecordData Record;
3483 Record.push_back(Opts.fp_contract);
3484 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3485}
3486
3487/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3488void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003489 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003490 return;
3491
3492 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3493 RecordData Record;
3494#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3495#include "clang/Basic/OpenCLExtensions.def"
3496 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3497}
3498
Douglas Gregor2171bf12012-01-15 16:58:34 +00003499void ASTWriter::WriteRedeclarations() {
3500 RecordData LocalRedeclChains;
3501 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3502
3503 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3504 Decl *First = Redeclarations[I];
3505 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3506
3507 Decl *MostRecent = First->getMostRecentDecl();
3508
3509 // If we only have a single declaration, there is no point in storing
3510 // a redeclaration chain.
3511 if (First == MostRecent)
3512 continue;
3513
3514 unsigned Offset = LocalRedeclChains.size();
3515 unsigned Size = 0;
3516 LocalRedeclChains.push_back(0); // Placeholder for the size.
3517
3518 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003519 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003520 Prev = Prev->getPreviousDecl()) {
3521 if (!Prev->isFromASTFile()) {
3522 AddDeclRef(Prev, LocalRedeclChains);
3523 ++Size;
3524 }
3525 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003526
3527 if (!First->isFromASTFile() && Chain) {
3528 Decl *FirstFromAST = MostRecent;
3529 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3530 if (Prev->isFromASTFile())
3531 FirstFromAST = Prev;
3532 }
3533
3534 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3535 }
3536
Douglas Gregor2171bf12012-01-15 16:58:34 +00003537 LocalRedeclChains[Offset] = Size;
3538
3539 // Reverse the set of local redeclarations, so that we store them in
3540 // order (since we found them in reverse order).
3541 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3542
Douglas Gregoraa945902013-02-18 15:53:43 +00003543 // Add the mapping from the first ID from the AST to the set of local
3544 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003545 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3546 LocalRedeclsMap.push_back(Info);
3547
3548 assert(N == Redeclarations.size() &&
3549 "Deserialized a declaration we shouldn't have");
3550 }
3551
3552 if (LocalRedeclChains.empty())
3553 return;
3554
3555 // Sort the local redeclarations map by the first declaration ID,
3556 // since the reader will be performing binary searches on this information.
3557 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3558
3559 // Emit the local redeclarations map.
3560 using namespace llvm;
3561 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3562 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3565 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3566
3567 RecordData Record;
3568 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3569 Record.push_back(LocalRedeclsMap.size());
3570 Stream.EmitRecordWithBlob(AbbrevID, Record,
3571 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3572 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3573
3574 // Emit the redeclaration chains.
3575 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3576}
3577
Douglas Gregorcff9f262012-01-27 01:47:08 +00003578void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003579 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003580 RecordData Categories;
3581
3582 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3583 unsigned Size = 0;
3584 unsigned StartIndex = Categories.size();
3585
3586 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3587
3588 // Allocate space for the size.
3589 Categories.push_back(0);
3590
3591 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003592 for (ObjCInterfaceDecl::known_categories_iterator
3593 Cat = Class->known_categories_begin(),
3594 CatEnd = Class->known_categories_end();
3595 Cat != CatEnd; ++Cat, ++Size) {
3596 assert(getDeclID(*Cat) != 0 && "Bogus category");
3597 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003598 }
3599
3600 // Update the size.
3601 Categories[StartIndex] = Size;
3602
3603 // Record this interface -> category map.
3604 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3605 CategoriesMap.push_back(CatInfo);
3606 }
3607
3608 // Sort the categories map by the definition ID, since the reader will be
3609 // performing binary searches on this information.
3610 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3611
3612 // Emit the categories map.
3613 using namespace llvm;
3614 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3615 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3618 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3619
3620 RecordData Record;
3621 Record.push_back(OBJC_CATEGORIES_MAP);
3622 Record.push_back(CategoriesMap.size());
3623 Stream.EmitRecordWithBlob(AbbrevID, Record,
3624 reinterpret_cast<char*>(CategoriesMap.data()),
3625 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3626
3627 // Emit the category lists.
3628 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3629}
3630
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003631void ASTWriter::WriteMergedDecls() {
3632 if (!Chain || Chain->MergedDecls.empty())
3633 return;
3634
3635 RecordData Record;
3636 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3637 IEnd = Chain->MergedDecls.end();
3638 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003639 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003640 : getDeclID(I->first);
3641 assert(CanonID && "Merged declaration not known?");
3642
3643 Record.push_back(CanonID);
3644 Record.push_back(I->second.size());
3645 Record.append(I->second.begin(), I->second.end());
3646 }
3647 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3648}
3649
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003650//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003651// General Serialization Routines
3652//===----------------------------------------------------------------------===//
3653
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003654/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003655void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3656 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003657 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003658 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3659 e = Attrs.end(); i != e; ++i){
3660 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003661 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003662 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003663
Sean Huntcf807c42010-08-18 23:23:40 +00003664#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003665
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003666 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003667}
3668
John McCallaeeacf72013-05-03 00:10:13 +00003669void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3670 AddSourceLocation(Tok.getLocation(), Record);
3671 Record.push_back(Tok.getLength());
3672
3673 // FIXME: When reading literal tokens, reconstruct the literal pointer
3674 // if it is needed.
3675 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3676 // FIXME: Should translate token kind to a stable encoding.
3677 Record.push_back(Tok.getKind());
3678 // FIXME: Should translate token flags to a stable encoding.
3679 Record.push_back(Tok.getFlags());
3680}
3681
Chris Lattner5f9e2722011-07-23 10:55:15 +00003682void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003683 Record.push_back(Str.size());
3684 Record.insert(Record.end(), Str.begin(), Str.end());
3685}
3686
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003687void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3688 RecordDataImpl &Record) {
3689 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003690 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003691 Record.push_back(*Minor + 1);
3692 else
3693 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003694 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003695 Record.push_back(*Subminor + 1);
3696 else
3697 Record.push_back(0);
3698}
3699
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003700/// \brief Note that the identifier II occurs at the given offset
3701/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003702void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003703 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003704 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003705 // up earlier in the chain and thus don't need an offset.
3706 if (ID >= FirstIdentID)
3707 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003708}
3709
Douglas Gregor83941df2009-04-25 17:48:32 +00003710/// \brief Note that the selector Sel occurs at the given offset
3711/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003712void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003713 unsigned ID = SelectorIDs[Sel];
3714 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003715 // Don't record offsets for selectors that are also available in a different
3716 // file.
3717 if (ID < FirstSelectorID)
3718 return;
3719 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003720}
3721
Sebastian Redla4232eb2010-08-18 23:56:21 +00003722ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003723 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003724 WritingAST(false), DoneWritingDeclsAndTypes(false),
3725 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003726 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003727 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003728 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3729 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003730 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3731 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003732 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003733 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003734 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003735 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003736 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003737 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003738 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3739 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3740 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003741 DeclTypedefAbbrev(0),
3742 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3743 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003744{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003745}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003746
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003747ASTWriter::~ASTWriter() {
3748 for (FileDeclIDsTy::iterator
3749 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3750 delete I->second;
3751}
3752
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003753void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003754 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003755 Module *WritingModule, StringRef isysroot,
3756 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003757 WritingAST = true;
3758
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003759 ASTHasCompilerErrors = hasErrors;
3760
Douglas Gregor2cf26342009-04-09 22:27:44 +00003761 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003762 Stream.Emit((unsigned)'C', 8);
3763 Stream.Emit((unsigned)'P', 8);
3764 Stream.Emit((unsigned)'C', 8);
3765 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003766
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003767 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003768
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003769 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003770 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003771 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003772 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003773 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003774 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003775 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003776
3777 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003778}
3779
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003780template<typename Vector>
3781static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3782 ASTWriter::RecordData &Record) {
3783 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3784 I != E; ++I) {
3785 Writer.AddDeclRef(*I, Record);
3786 }
3787}
3788
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003789void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003790 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003791 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003792 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003793 using namespace llvm;
3794
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003795 bool isModule = WritingModule != 0;
3796
Douglas Gregorecc2c092011-12-01 22:20:10 +00003797 // Make sure that the AST reader knows to finalize itself.
3798 if (Chain)
3799 Chain->finalizeForWriting();
3800
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003801 ASTContext &Context = SemaRef.Context;
3802 Preprocessor &PP = SemaRef.PP;
3803
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003804 // Set up predefined declaration IDs.
3805 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003806 if (Context.ObjCIdDecl)
3807 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003808 if (Context.ObjCSelDecl)
3809 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003810 if (Context.ObjCClassDecl)
3811 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003812 if (Context.ObjCProtocolClassDecl)
3813 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003814 if (Context.Int128Decl)
3815 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3816 if (Context.UInt128Decl)
3817 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003818 if (Context.ObjCInstanceTypeDecl)
3819 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003820 if (Context.BuiltinVaListDecl)
3821 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3822
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003823 if (!Chain) {
3824 // Make sure that we emit IdentifierInfos (and any attached
3825 // declarations) for builtins. We don't need to do this when we're
3826 // emitting chained PCH files, because all of the builtins will be
3827 // in the original PCH file.
3828 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003829 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003830 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003831 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003832 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003833 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3834 getIdentifierRef(&Table.get(BuiltinNames[I]));
3835 }
3836
Douglas Gregoreee242f2011-10-27 09:33:13 +00003837 // If there are any out-of-date identifiers, bring them up to date.
3838 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003839 // Find out-of-date identifiers.
3840 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003841 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3842 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003843 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003844 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003845 OutOfDate.push_back(ID->second);
3846 }
3847
3848 // Update the out-of-date identifiers.
3849 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3850 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3851 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003852 }
3853
Chris Lattner63d65f82009-09-08 18:19:27 +00003854 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003855 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003856 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003857 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003858 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003859
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003860 // Build a record containing all of the file scoped decls in this file.
3861 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003862 if (!isModule)
3863 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3864 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003865
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003866 // Build a record containing all of the delegating constructors we still need
3867 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003868 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003869 if (!isModule)
3870 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003871
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003872 // Write the set of weak, undeclared identifiers. We always write the
3873 // entire table, since later PCH files in a PCH chain are only interested in
3874 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003875 RecordData WeakUndeclaredIdentifiers;
3876 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003877 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003878 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3879 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3880 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3881 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3882 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3883 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3884 }
3885 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003886
Richard Smith5ea6ef42013-01-10 23:43:47 +00003887 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003888 // declarations in this header file. Generally, this record will be
3889 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003890 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003891 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003892 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003893 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003894 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3895 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003896 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003897 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003898 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003899 }
3900
Douglas Gregorb81c1702009-04-27 20:06:05 +00003901 // Build a record containing all of the ext_vector declarations.
3902 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003903 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003904
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003905 // Build a record containing all of the VTable uses information.
3906 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003907 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003908 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3909 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3910 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3911 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3912 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003913 }
3914
3915 // Build a record containing all of dynamic classes declarations.
3916 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003917 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003918
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003919 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003920 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003921 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003922 I = SemaRef.PendingInstantiations.begin(),
3923 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3924 AddDeclRef(I->first, PendingInstantiations);
3925 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003926 }
3927 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3928 "There are local ones at end of translation unit!");
3929
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003930 // Build a record containing some declaration references.
3931 RecordData SemaDeclRefs;
3932 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3933 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3934 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3935 }
3936
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003937 RecordData CUDASpecialDeclRefs;
3938 if (Context.getcudaConfigureCallDecl()) {
3939 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3940 }
3941
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003942 // Build a record containing all of the known namespaces.
3943 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003944 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003945 I = SemaRef.KnownNamespaces.begin(),
3946 IEnd = SemaRef.KnownNamespaces.end();
3947 I != IEnd; ++I) {
3948 if (!I->second)
3949 AddDeclRef(I->first, KnownNamespaces);
3950 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003951
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003952 // Build a record of all used, undefined objects that require definitions.
3953 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003954
3955 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003956 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003957 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3958 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003959 AddDeclRef(I->first, UndefinedButUsed);
3960 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003961 }
3962
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003963 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003964 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003965
Sebastian Redl3397c552010-08-18 23:56:27 +00003966 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003967 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003968 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003969
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003970 // This is so that older clang versions, before the introduction
3971 // of the control block, can read and reject the newer PCH format.
3972 Record.clear();
3973 Record.push_back(VERSION_MAJOR);
3974 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3975
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003976 // Create a lexical update block containing all of the declarations in the
3977 // translation unit that do not come from other AST files.
3978 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3979 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3980 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3981 E = TU->noload_decls_end();
3982 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003983 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003984 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003985 }
3986
3987 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3988 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3989 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3990 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3991 Record.clear();
3992 Record.push_back(TU_UPDATE_LEXICAL);
3993 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3994 data(NewGlobalDecls));
3995
3996 // And a visible updates block for the translation unit.
3997 Abv = new llvm::BitCodeAbbrev();
3998 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3999 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4000 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
4001 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4002 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4003 WriteDeclContextVisibleUpdate(TU);
4004
4005 // If the translation unit has an anonymous namespace, and we don't already
4006 // have an update block for it, write it as an update block.
4007 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4008 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4009 if (Record.empty()) {
4010 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004011 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004012 }
4013 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004014
4015 // Make sure visible decls, added to DeclContexts previously loaded from
4016 // an AST file, are registered for serialization.
4017 for (SmallVector<const Decl *, 16>::iterator
4018 I = UpdatingVisibleDecls.begin(),
4019 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4020 GetDeclRef(*I);
4021 }
4022
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004023 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004024 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004025
Douglas Gregora119da02011-08-02 16:26:37 +00004026 // Form the record of special types.
4027 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004028 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004029 AddTypeRef(Context.getFILEType(), SpecialTypes);
4030 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4031 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4032 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4033 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004034 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004035 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004036
Douglas Gregor366809a2009-04-26 03:49:13 +00004037 // Keep writing types and declarations until all types and
4038 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004039 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004040 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004041 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4042 E = DeclsToRewrite.end();
4043 I != E; ++I)
4044 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004045 while (!DeclTypesToEmit.empty()) {
4046 DeclOrType DOT = DeclTypesToEmit.front();
4047 DeclTypesToEmit.pop();
4048 if (DOT.isType())
4049 WriteType(DOT.getType());
4050 else
4051 WriteDecl(Context, DOT.getDecl());
4052 }
4053 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004054
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004055 DoneWritingDeclsAndTypes = true;
4056
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004057 WriteFileDeclIDsMap();
4058 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004059 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004060
4061 if (Chain) {
4062 // Write the mapping information describing our module dependencies and how
4063 // each of those modules were mapped into our own offset/ID space, so that
4064 // the reader can build the appropriate mapping to its own offset/ID space.
4065 // The map consists solely of a blob with the following format:
4066 // *(module-name-len:i16 module-name:len*i8
4067 // source-location-offset:i32
4068 // identifier-id:i32
4069 // preprocessed-entity-id:i32
4070 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004071 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004072 // selector-id:i32
4073 // declaration-id:i32
4074 // c++-base-specifiers-id:i32
4075 // type-id:i32)
4076 //
4077 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4078 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4079 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4080 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004081 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004082 {
4083 llvm::raw_svector_ostream Out(Buffer);
4084 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004085 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004086 M != MEnd; ++M) {
4087 StringRef FileName = (*M)->FileName;
4088 io::Emit16(Out, FileName.size());
4089 Out.write(FileName.data(), FileName.size());
4090 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4091 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004092 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004093 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004094 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004095 io::Emit32(Out, (*M)->BaseSelectorID);
4096 io::Emit32(Out, (*M)->BaseDeclID);
4097 io::Emit32(Out, (*M)->BaseTypeIndex);
4098 }
4099 }
4100 Record.clear();
4101 Record.push_back(MODULE_OFFSET_MAP);
4102 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4103 Buffer.data(), Buffer.size());
4104 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004105 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004106 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004107 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004108 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004109 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004110 WriteFPPragmaOptions(SemaRef.getFPOptions());
4111 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004112
Sebastian Redl1476ed42010-07-16 16:36:56 +00004113 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004114 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004115
Anders Carlssonc8505782011-03-06 18:41:18 +00004116 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004117
Douglas Gregore209e502011-12-06 01:10:29 +00004118 // If we're emitting a module, write out the submodule information.
4119 if (WritingModule)
4120 WriteSubmodules(WritingModule);
4121
Douglas Gregora119da02011-08-02 16:26:37 +00004122 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4123
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004124 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004125 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004126 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004127
4128 // Write the record containing tentative definitions.
4129 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004130 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004131
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004132 // Write the record containing unused file scoped decls.
4133 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004134 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004135
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004136 // Write the record containing weak undeclared identifiers.
4137 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004138 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004139 WeakUndeclaredIdentifiers);
4140
Richard Smith5ea6ef42013-01-10 23:43:47 +00004141 // Write the record containing locally-scoped extern "C" definitions.
4142 if (!LocallyScopedExternCDecls.empty())
4143 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4144 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004145
4146 // Write the record containing ext_vector type names.
4147 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004148 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004149
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004150 // Write the record containing VTable uses information.
4151 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004152 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004153
4154 // Write the record containing dynamic classes declarations.
4155 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004156 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004157
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004158 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004159 if (!PendingInstantiations.empty())
4160 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004161
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004162 // Write the record containing declaration references of Sema.
4163 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004164 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004165
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004166 // Write the record containing CUDA-specific declaration references.
4167 if (!CUDASpecialDeclRefs.empty())
4168 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004169
4170 // Write the delegating constructors.
4171 if (!DelegatingCtorDecls.empty())
4172 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004173
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004174 // Write the known namespaces.
4175 if (!KnownNamespaces.empty())
4176 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004177
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004178 // Write the undefined internal functions and variables, and inline functions.
4179 if (!UndefinedButUsed.empty())
4180 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004181
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004182 // Write the visible updates to DeclContexts.
4183 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4184 I = UpdatedDeclContexts.begin(),
4185 E = UpdatedDeclContexts.end();
4186 I != E; ++I)
4187 WriteDeclContextVisibleUpdate(*I);
4188
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004189 if (!WritingModule) {
4190 // Write the submodules that were imported, if any.
4191 RecordData ImportedModules;
4192 for (ASTContext::import_iterator I = Context.local_import_begin(),
4193 IEnd = Context.local_import_end();
4194 I != IEnd; ++I) {
4195 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4196 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4197 }
4198 if (!ImportedModules.empty()) {
4199 // Sort module IDs.
4200 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4201
4202 // Unique module IDs.
4203 ImportedModules.erase(std::unique(ImportedModules.begin(),
4204 ImportedModules.end()),
4205 ImportedModules.end());
4206
4207 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4208 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004209 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004210
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004211 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004212 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004213 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004214 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004215 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004216
Douglas Gregor3e1af842009-04-17 22:13:46 +00004217 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004218 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004219 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004220 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004221 Record.push_back(NumLexicalDeclContexts);
4222 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004223 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004224 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004225}
4226
Douglas Gregor61c5e342011-09-17 00:05:03 +00004227/// \brief Go through the declaration update blocks and resolve declaration
4228/// pointers into declaration IDs.
4229void ASTWriter::ResolveDeclUpdatesBlocks() {
4230 for (DeclUpdateMap::iterator
4231 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4232 const Decl *D = I->first;
4233 UpdateRecord &URec = I->second;
4234
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004235 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004236 continue; // The decl will be written completely
4237
4238 unsigned Idx = 0, N = URec.size();
4239 while (Idx < N) {
4240 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004241 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4242 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4243 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4244 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4245 ++Idx;
4246 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004247
Douglas Gregor61c5e342011-09-17 00:05:03 +00004248 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4249 ++Idx;
4250 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004251
4252 case UPD_CXX_DEDUCED_RETURN_TYPE:
4253 URec[Idx] = GetOrCreateTypeID(
4254 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4255 ++Idx;
4256 break;
Douglas Gregor61c5e342011-09-17 00:05:03 +00004257 }
4258 }
4259 }
4260}
4261
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004262void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004263 if (DeclUpdates.empty())
4264 return;
4265
4266 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004267 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004268 for (DeclUpdateMap::iterator
4269 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4270 const Decl *D = I->first;
4271 UpdateRecord &URec = I->second;
4272
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004273 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004274 continue; // The decl will be written completely,no need to store updates.
4275
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004276 uint64_t Offset = Stream.GetCurrentBitNo();
4277 Stream.EmitRecord(DECL_UPDATES, URec);
4278
4279 OffsetsRecord.push_back(GetDeclRef(D));
4280 OffsetsRecord.push_back(Offset);
4281 }
4282 Stream.ExitBlock();
4283 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4284}
4285
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004286void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004287 if (ReplacedDecls.empty())
4288 return;
4289
4290 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004291 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004292 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004293 Record.push_back(I->ID);
4294 Record.push_back(I->Offset);
4295 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004296 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004297 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004298}
4299
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004300void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004301 Record.push_back(Loc.getRawEncoding());
4302}
4303
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004304void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004305 AddSourceLocation(Range.getBegin(), Record);
4306 AddSourceLocation(Range.getEnd(), Record);
4307}
4308
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004309void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004310 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004311 const uint64_t *Words = Value.getRawData();
4312 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004313}
4314
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004315void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004316 Record.push_back(Value.isUnsigned());
4317 AddAPInt(Value, Record);
4318}
4319
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004320void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004321 AddAPInt(Value.bitcastToAPInt(), Record);
4322}
4323
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004324void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004325 Record.push_back(getIdentifierRef(II));
4326}
4327
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004328IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004329 if (II == 0)
4330 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004331
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004332 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004333 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004334 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004335 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004336}
4337
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004338MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004339 // Don't emit builtin macros like __LINE__ to the AST file unless they
4340 // have been redefined by the header (in which case they are not
4341 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004342 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004343 return 0;
4344
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004345 MacroID &ID = MacroIDs[MI];
4346 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004347 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004348 MacroInfoToEmitData Info = { Name, MI, ID };
4349 MacroInfosToEmit.push_back(Info);
4350 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004351 return ID;
4352}
4353
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004354MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4355 if (MI == 0 || MI->isBuiltinMacro())
4356 return 0;
4357
4358 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4359 return MacroIDs[MI];
4360}
4361
4362uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4363 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4364 return IdentMacroDirectivesOffsetMap[Name];
4365}
4366
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004367void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004368 Record.push_back(getSelectorRef(SelRef));
4369}
4370
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004371SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004372 if (Sel.getAsOpaquePtr() == 0) {
4373 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004374 }
4375
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004376 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004377 if (SID == 0 && Chain) {
4378 // This might trigger a ReadSelector callback, which will set the ID for
4379 // this selector.
4380 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004381 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004382 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004383 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004384 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004385 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004386 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004387 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004388}
4389
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004390void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004391 AddDeclRef(Temp->getDestructor(), Record);
4392}
4393
Douglas Gregor7c789c12010-10-29 22:39:52 +00004394void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4395 CXXBaseSpecifier const *BasesEnd,
4396 RecordDataImpl &Record) {
4397 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4398 CXXBaseSpecifiersToWrite.push_back(
4399 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4400 Bases, BasesEnd));
4401 Record.push_back(NextCXXBaseSpecifiersID++);
4402}
4403
Sebastian Redla4232eb2010-08-18 23:56:21 +00004404void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004405 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004406 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004407 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004408 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004409 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004410 break;
4411 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004412 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004413 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004414 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004415 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004416 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004417 break;
4418 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004419 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004420 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004421 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004422 break;
John McCall833ca992009-10-29 08:12:44 +00004423 case TemplateArgument::Null:
4424 case TemplateArgument::Integral:
4425 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004426 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004427 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004428 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004429 break;
4430 }
4431}
4432
Sebastian Redla4232eb2010-08-18 23:56:21 +00004433void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004434 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004435 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004436
4437 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4438 bool InfoHasSameExpr
4439 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4440 Record.push_back(InfoHasSameExpr);
4441 if (InfoHasSameExpr)
4442 return; // Avoid storing the same expr twice.
4443 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004444 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4445 Record);
4446}
4447
Douglas Gregordc355712011-02-25 00:36:19 +00004448void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4449 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004450 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004451 AddTypeRef(QualType(), Record);
4452 return;
4453 }
4454
Douglas Gregordc355712011-02-25 00:36:19 +00004455 AddTypeLoc(TInfo->getTypeLoc(), Record);
4456}
4457
4458void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4459 AddTypeRef(TL.getType(), Record);
4460
John McCalla1ee0c52009-10-16 21:56:05 +00004461 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004462 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004463 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004464}
4465
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004466void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004467 Record.push_back(GetOrCreateTypeID(T));
4468}
4469
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004470TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004471 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004472 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004473 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4474}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004475
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004476TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004477 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004478 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004479 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004480}
4481
4482TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4483 if (T.isNull())
4484 return TypeIdx();
4485 assert(!T.getLocalFastQualifiers());
4486
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004487 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004488 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004489 if (DoneWritingDeclsAndTypes) {
4490 assert(0 && "New type seen after serializing all the types to emit!");
4491 return TypeIdx();
4492 }
4493
Douglas Gregor366809a2009-04-26 03:49:13 +00004494 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004495 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004496 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004497 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004498 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004499 return Idx;
4500}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004501
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004502TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004503 if (T.isNull())
4504 return TypeIdx();
4505 assert(!T.getLocalFastQualifiers());
4506
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004507 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4508 assert(I != TypeIdxs.end() && "Type not emitted!");
4509 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004510}
4511
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004512void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004513 Record.push_back(GetDeclRef(D));
4514}
4515
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004516DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004517 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4518
Douglas Gregor2cf26342009-04-09 22:27:44 +00004519 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004520 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004521 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004522
4523 // If D comes from an AST file, its declaration ID is already known and
4524 // fixed.
4525 if (D->isFromASTFile())
4526 return D->getGlobalID();
4527
Douglas Gregor97475832010-10-05 18:37:06 +00004528 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004529 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004530 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004531 if (DoneWritingDeclsAndTypes) {
4532 assert(0 && "New decl seen after serializing all the decls to emit!");
4533 return 0;
4534 }
4535
Douglas Gregor2cf26342009-04-09 22:27:44 +00004536 // We haven't seen this declaration before. Give it a new ID and
4537 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004538 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004539 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004540 }
4541
Sebastian Redl681d7232010-07-27 00:17:23 +00004542 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004543}
4544
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004545DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004546 if (D == 0)
4547 return 0;
4548
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004549 // If D comes from an AST file, its declaration ID is already known and
4550 // fixed.
4551 if (D->isFromASTFile())
4552 return D->getGlobalID();
4553
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004554 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4555 return DeclIDs[D];
4556}
4557
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004558static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4559 std::pair<unsigned, serialization::DeclID> R) {
4560 return L.first < R.first;
4561}
4562
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004563void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004564 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004565 assert(D);
4566
4567 SourceLocation Loc = D->getLocation();
4568 if (Loc.isInvalid())
4569 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004570
4571 // We only keep track of the file-level declarations of each file.
4572 if (!D->getLexicalDeclContext()->isFileContext())
4573 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004574 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4575 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004576 if (isa<ParmVarDecl>(D))
4577 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004578
4579 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004580 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004581 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004582 FileID FID;
4583 unsigned Offset;
4584 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004585 if (FID.isInvalid())
4586 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004587 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004588
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004589 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004590 if (!Info)
4591 Info = new DeclIDInFileInfo();
4592
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004593 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004594 LocDeclIDsTy &Decls = Info->DeclIDs;
4595
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004596 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004597 Decls.push_back(LocDecl);
4598 return;
4599 }
4600
4601 LocDeclIDsTy::iterator
4602 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4603
4604 Decls.insert(I, LocDecl);
4605}
4606
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004607void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004608 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004609 Record.push_back(Name.getNameKind());
4610 switch (Name.getNameKind()) {
4611 case DeclarationName::Identifier:
4612 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4613 break;
4614
4615 case DeclarationName::ObjCZeroArgSelector:
4616 case DeclarationName::ObjCOneArgSelector:
4617 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004618 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004619 break;
4620
4621 case DeclarationName::CXXConstructorName:
4622 case DeclarationName::CXXDestructorName:
4623 case DeclarationName::CXXConversionFunctionName:
4624 AddTypeRef(Name.getCXXNameType(), Record);
4625 break;
4626
4627 case DeclarationName::CXXOperatorName:
4628 Record.push_back(Name.getCXXOverloadedOperator());
4629 break;
4630
Sean Hunt3e518bd2009-11-29 07:34:05 +00004631 case DeclarationName::CXXLiteralOperatorName:
4632 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4633 break;
4634
Douglas Gregor2cf26342009-04-09 22:27:44 +00004635 case DeclarationName::CXXUsingDirective:
4636 // No extra data to emit
4637 break;
4638 }
4639}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004640
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004641void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004642 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004643 switch (Name.getNameKind()) {
4644 case DeclarationName::CXXConstructorName:
4645 case DeclarationName::CXXDestructorName:
4646 case DeclarationName::CXXConversionFunctionName:
4647 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4648 break;
4649
4650 case DeclarationName::CXXOperatorName:
4651 AddSourceLocation(
4652 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4653 Record);
4654 AddSourceLocation(
4655 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4656 Record);
4657 break;
4658
4659 case DeclarationName::CXXLiteralOperatorName:
4660 AddSourceLocation(
4661 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4662 Record);
4663 break;
4664
4665 case DeclarationName::Identifier:
4666 case DeclarationName::ObjCZeroArgSelector:
4667 case DeclarationName::ObjCOneArgSelector:
4668 case DeclarationName::ObjCMultiArgSelector:
4669 case DeclarationName::CXXUsingDirective:
4670 break;
4671 }
4672}
4673
4674void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004675 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004676 AddDeclarationName(NameInfo.getName(), Record);
4677 AddSourceLocation(NameInfo.getLoc(), Record);
4678 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4679}
4680
4681void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004682 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004683 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004684 Record.push_back(Info.NumTemplParamLists);
4685 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4686 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4687}
4688
Sebastian Redla4232eb2010-08-18 23:56:21 +00004689void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004690 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004691 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004692 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004693 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004694
4695 // Push each of the NNS's onto a stack for serialization in reverse order.
4696 while (NNS) {
4697 NestedNames.push_back(NNS);
4698 NNS = NNS->getPrefix();
4699 }
4700
4701 Record.push_back(NestedNames.size());
4702 while(!NestedNames.empty()) {
4703 NNS = NestedNames.pop_back_val();
4704 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4705 Record.push_back(Kind);
4706 switch (Kind) {
4707 case NestedNameSpecifier::Identifier:
4708 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4709 break;
4710
4711 case NestedNameSpecifier::Namespace:
4712 AddDeclRef(NNS->getAsNamespace(), Record);
4713 break;
4714
Douglas Gregor14aba762011-02-24 02:36:08 +00004715 case NestedNameSpecifier::NamespaceAlias:
4716 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4717 break;
4718
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004719 case NestedNameSpecifier::TypeSpec:
4720 case NestedNameSpecifier::TypeSpecWithTemplate:
4721 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4722 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4723 break;
4724
4725 case NestedNameSpecifier::Global:
4726 // Don't need to write an associated value.
4727 break;
4728 }
4729 }
4730}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004731
Douglas Gregordc355712011-02-25 00:36:19 +00004732void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4733 RecordDataImpl &Record) {
4734 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004735 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004736 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004737
4738 // Push each of the nested-name-specifiers's onto a stack for
4739 // serialization in reverse order.
4740 while (NNS) {
4741 NestedNames.push_back(NNS);
4742 NNS = NNS.getPrefix();
4743 }
4744
4745 Record.push_back(NestedNames.size());
4746 while(!NestedNames.empty()) {
4747 NNS = NestedNames.pop_back_val();
4748 NestedNameSpecifier::SpecifierKind Kind
4749 = NNS.getNestedNameSpecifier()->getKind();
4750 Record.push_back(Kind);
4751 switch (Kind) {
4752 case NestedNameSpecifier::Identifier:
4753 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4754 AddSourceRange(NNS.getLocalSourceRange(), Record);
4755 break;
4756
4757 case NestedNameSpecifier::Namespace:
4758 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4759 AddSourceRange(NNS.getLocalSourceRange(), Record);
4760 break;
4761
4762 case NestedNameSpecifier::NamespaceAlias:
4763 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4764 AddSourceRange(NNS.getLocalSourceRange(), Record);
4765 break;
4766
4767 case NestedNameSpecifier::TypeSpec:
4768 case NestedNameSpecifier::TypeSpecWithTemplate:
4769 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4770 AddTypeLoc(NNS.getTypeLoc(), Record);
4771 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4772 break;
4773
4774 case NestedNameSpecifier::Global:
4775 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4776 break;
4777 }
4778 }
4779}
4780
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004781void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004782 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004783 Record.push_back(Kind);
4784 switch (Kind) {
4785 case TemplateName::Template:
4786 AddDeclRef(Name.getAsTemplateDecl(), Record);
4787 break;
4788
4789 case TemplateName::OverloadedTemplate: {
4790 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4791 Record.push_back(OvT->size());
4792 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4793 I != E; ++I)
4794 AddDeclRef(*I, Record);
4795 break;
4796 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004797
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004798 case TemplateName::QualifiedTemplate: {
4799 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4800 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4801 Record.push_back(QualT->hasTemplateKeyword());
4802 AddDeclRef(QualT->getTemplateDecl(), Record);
4803 break;
4804 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004805
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004806 case TemplateName::DependentTemplate: {
4807 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4808 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4809 Record.push_back(DepT->isIdentifier());
4810 if (DepT->isIdentifier())
4811 AddIdentifierRef(DepT->getIdentifier(), Record);
4812 else
4813 Record.push_back(DepT->getOperator());
4814 break;
4815 }
John McCall14606042011-06-30 08:33:18 +00004816
4817 case TemplateName::SubstTemplateTemplateParm: {
4818 SubstTemplateTemplateParmStorage *subst
4819 = Name.getAsSubstTemplateTemplateParm();
4820 AddDeclRef(subst->getParameter(), Record);
4821 AddTemplateName(subst->getReplacement(), Record);
4822 break;
4823 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004824
4825 case TemplateName::SubstTemplateTemplateParmPack: {
4826 SubstTemplateTemplateParmPackStorage *SubstPack
4827 = Name.getAsSubstTemplateTemplateParmPack();
4828 AddDeclRef(SubstPack->getParameterPack(), Record);
4829 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4830 break;
4831 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004832 }
4833}
4834
Michael J. Spencer20249a12010-10-21 03:16:25 +00004835void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004836 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004837 Record.push_back(Arg.getKind());
4838 switch (Arg.getKind()) {
4839 case TemplateArgument::Null:
4840 break;
4841 case TemplateArgument::Type:
4842 AddTypeRef(Arg.getAsType(), Record);
4843 break;
4844 case TemplateArgument::Declaration:
4845 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004846 Record.push_back(Arg.isDeclForReferenceParam());
4847 break;
4848 case TemplateArgument::NullPtr:
4849 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004850 break;
4851 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004852 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004853 AddTypeRef(Arg.getIntegralType(), Record);
4854 break;
4855 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004856 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4857 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004858 case TemplateArgument::TemplateExpansion:
4859 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004860 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004861 Record.push_back(*NumExpansions + 1);
4862 else
4863 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004864 break;
4865 case TemplateArgument::Expression:
4866 AddStmt(Arg.getAsExpr());
4867 break;
4868 case TemplateArgument::Pack:
4869 Record.push_back(Arg.pack_size());
4870 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4871 I != E; ++I)
4872 AddTemplateArgument(*I, Record);
4873 break;
4874 }
4875}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004876
4877void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004878ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004879 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004880 assert(TemplateParams && "No TemplateParams!");
4881 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4882 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4883 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4884 Record.push_back(TemplateParams->size());
4885 for (TemplateParameterList::const_iterator
4886 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4887 P != PEnd; ++P)
4888 AddDeclRef(*P, Record);
4889}
4890
4891/// \brief Emit a template argument list.
4892void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004893ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004894 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004895 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004896 Record.push_back(TemplateArgs->size());
4897 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004898 AddTemplateArgument(TemplateArgs->get(i), Record);
4899}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004900
4901
4902void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004903ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004904 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004905 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004906 I = Set.begin(), E = Set.end(); I != E; ++I) {
4907 AddDeclRef(I.getDecl(), Record);
4908 Record.push_back(I.getAccess());
4909 }
4910}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004911
Sebastian Redla4232eb2010-08-18 23:56:21 +00004912void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004913 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004914 Record.push_back(Base.isVirtual());
4915 Record.push_back(Base.isBaseOfClass());
4916 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004917 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004918 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004919 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004920 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4921 : SourceLocation(),
4922 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004923}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004924
Douglas Gregor7c789c12010-10-29 22:39:52 +00004925void ASTWriter::FlushCXXBaseSpecifiers() {
4926 RecordData Record;
4927 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4928 Record.clear();
4929
4930 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004931 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004932 if (Index == CXXBaseSpecifiersOffsets.size())
4933 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4934 else {
4935 if (Index > CXXBaseSpecifiersOffsets.size())
4936 CXXBaseSpecifiersOffsets.resize(Index + 1);
4937 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4938 }
4939
4940 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4941 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4942 Record.push_back(BEnd - B);
4943 for (; B != BEnd; ++B)
4944 AddCXXBaseSpecifier(*B, Record);
4945 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004946
4947 // Flush any expressions that were written as part of the base specifiers.
4948 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004949 }
4950
4951 CXXBaseSpecifiersToWrite.clear();
4952}
4953
Sean Huntcbb67482011-01-08 20:30:50 +00004954void ASTWriter::AddCXXCtorInitializers(
4955 const CXXCtorInitializer * const *CtorInitializers,
4956 unsigned NumCtorInitializers,
4957 RecordDataImpl &Record) {
4958 Record.push_back(NumCtorInitializers);
4959 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4960 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004961
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004962 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004963 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004964 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004965 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004966 } else if (Init->isDelegatingInitializer()) {
4967 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004968 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004969 } else if (Init->isMemberInitializer()){
4970 Record.push_back(CTOR_INITIALIZER_MEMBER);
4971 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004972 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004973 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4974 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004975 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004976
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004977 AddSourceLocation(Init->getMemberLocation(), Record);
4978 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004979 AddSourceLocation(Init->getLParenLoc(), Record);
4980 AddSourceLocation(Init->getRParenLoc(), Record);
4981 Record.push_back(Init->isWritten());
4982 if (Init->isWritten()) {
4983 Record.push_back(Init->getSourceOrder());
4984 } else {
4985 Record.push_back(Init->getNumArrayIndices());
4986 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4987 AddDeclRef(Init->getArrayIndex(i), Record);
4988 }
4989 }
4990}
4991
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004992void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4993 assert(D->DefinitionData);
4994 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004995 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004996 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004997 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004998 Record.push_back(Data.Aggregate);
4999 Record.push_back(Data.PlainOldData);
5000 Record.push_back(Data.Empty);
5001 Record.push_back(Data.Polymorphic);
5002 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00005003 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00005004 Record.push_back(Data.HasNoNonEmptyBases);
5005 Record.push_back(Data.HasPrivateFields);
5006 Record.push_back(Data.HasProtectedFields);
5007 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00005008 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00005009 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00005010 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00005011 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00005012 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5013 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5014 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5015 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5016 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5017 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005018 Record.push_back(Data.HasTrivialSpecialMembers);
5019 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005020 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005021 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005022 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005023 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005024 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005025 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005026 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005027 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5028 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5029 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5030 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005031 Record.push_back(Data.FailedImplicitMoveConstructor);
5032 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005033 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005034
5035 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005036 if (Data.NumBases > 0)
5037 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5038 Record);
5039
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005040 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5041 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005042 if (Data.NumVBases > 0)
5043 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5044 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005045
5046 AddUnresolvedSet(Data.Conversions, Record);
5047 AddUnresolvedSet(Data.VisibleConversions, Record);
5048 // Data.Definition is the owning decl, no need to write it.
5049 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005050
5051 // Add lambda-specific data.
5052 if (Data.IsLambda) {
5053 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005054 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005055 Record.push_back(Lambda.NumCaptures);
5056 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005057 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005058 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005059 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005060 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5061 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5062 AddSourceLocation(Capture.getLocation(), Record);
5063 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005064 Record.push_back(Capture.getCaptureKind());
5065 switch (Capture.getCaptureKind()) {
5066 case LCK_This:
5067 break;
5068 case LCK_ByCopy:
5069 case LCK_ByRef: {
5070 VarDecl *Var =
5071 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5072 AddDeclRef(Var, Record);
5073 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5074 : SourceLocation(),
5075 Record);
5076 break;
5077 }
5078 case LCK_Init:
5079 FieldDecl *Field = Capture.getInitCaptureField();
5080 AddDeclRef(Field, Record);
5081 break;
5082 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005083 }
5084 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005085}
5086
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005087void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005088 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005089 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005090 assert(FirstDeclID == NextDeclID &&
5091 FirstTypeID == NextTypeID &&
5092 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005093 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005094 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005095 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005096 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005097
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005098 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005099
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005100 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5101 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5102 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005103 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005104 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005105 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005106 NextDeclID = FirstDeclID;
5107 NextTypeID = FirstTypeID;
5108 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005109 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005110 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005111 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005112}
5113
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005114void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005115 // Always keep the highest ID. See \p TypeRead() for more information.
5116 IdentID &StoredID = IdentifierIDs[II];
5117 if (ID > StoredID)
5118 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005119}
5120
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005121void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005122 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005123 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005124 if (ID > StoredID)
5125 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005126}
5127
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005128void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005129 // Always take the highest-numbered type index. This copes with an interesting
5130 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005131 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005132 // keep the higher-numbered entry so that we can properly write it out to
5133 // the AST file.
5134 TypeIdx &StoredIdx = TypeIdxs[T];
5135 if (Idx.getIndex() >= StoredIdx.getIndex())
5136 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005137}
5138
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005139void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005140 // Always keep the highest ID. See \p TypeRead() for more information.
5141 SelectorID &StoredID = SelectorIDs[S];
5142 if (ID > StoredID)
5143 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005144}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005145
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005146void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005147 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005148 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005149 MacroDefinitions[MD] = ID;
5150}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005151
Douglas Gregora015cab2011-12-02 17:30:13 +00005152void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5153 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5154 SubmoduleIDs[Mod] = ID;
5155}
5156
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005157void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005158 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005159 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005160 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5161 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005162 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005163 // A forward reference was mutated into a definition. Rewrite it.
5164 // FIXME: This happens during template instantiation, should we
5165 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005166 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005167 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005168 }
5169}
Douglas Gregora8235d62012-10-09 23:05:51 +00005170
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005171void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005172 assert(!WritingAST && "Already writing the AST!");
5173
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005174 // TU and namespaces are handled elsewhere.
5175 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5176 return;
5177
Douglas Gregor919814d2011-09-09 23:01:35 +00005178 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005179 return; // Not a source decl added to a DeclContext from PCH.
5180
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005181 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005182 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005183 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005184}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005185
5186void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005187 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005188 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005189 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005190 return; // Not a source member added to a class from PCH.
5191 if (!isa<CXXMethodDecl>(D))
5192 return; // We are interested in lazily declared implicit methods.
5193
5194 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005195 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005196 UpdateRecord &Record = DeclUpdates[RD];
5197 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005198 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005199}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005200
5201void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5202 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005203 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005204 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005205 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005206 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005207 return; // Not a source specialization added to a template from PCH.
5208
5209 UpdateRecord &Record = DeclUpdates[TD];
5210 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005211 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005212}
Douglas Gregor89d99802010-11-30 06:16:57 +00005213
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005214void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5215 const FunctionDecl *D) {
5216 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005217 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005218 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005219 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005220 return; // Not a source specialization added to a template from PCH.
5221
5222 UpdateRecord &Record = DeclUpdates[TD];
5223 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005224 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005225}
5226
Richard Smith9dadfab2013-05-11 05:45:24 +00005227void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5228 assert(!WritingAST && "Already writing the AST!");
5229 FD = FD->getCanonicalDecl();
5230 if (!FD->isFromASTFile())
5231 return; // Not a function declared in PCH and defined outside.
5232
5233 UpdateRecord &Record = DeclUpdates[FD];
5234 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5235 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5236}
5237
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005238void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005239 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005240 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005241 return; // Declaration not imported from PCH.
5242
5243 // Implicit decl from a PCH was defined.
5244 // FIXME: Should implicit definition be a separate FunctionDecl?
5245 RewriteDecl(D);
5246}
5247
Sebastian Redlf79a7192011-04-29 08:19:30 +00005248void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005249 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005250 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005251 return;
5252
5253 // Since the actual instantiation is delayed, this really means that we need
5254 // to update the instantiation location.
5255 UpdateRecord &Record = DeclUpdates[D];
5256 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5257 AddSourceLocation(
5258 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5259}
5260
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005261void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5262 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005263 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005264 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005265 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005266
5267 assert(IFD->getDefinition() && "Category on a class without a definition?");
5268 ObjCClassesWithCategories.insert(
5269 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005270}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005271
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005272
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005273void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5274 const ObjCPropertyDecl *OrigProp,
5275 const ObjCCategoryDecl *ClassExt) {
5276 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5277 if (!D)
5278 return;
5279
5280 assert(!WritingAST && "Already writing the AST!");
5281 if (!D->isFromASTFile())
5282 return; // Declaration not imported from PCH.
5283
5284 RewriteDecl(D);
5285}