blob: ece297f3e53aeb1b4c3645d408252deaeb1835d8 [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
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001479 unsigned char Flags = (Data.isImport << 5)
1480 | (Data.isPragmaOnce << 4)
1481 | (Data.DirInfo << 2)
1482 | (Data.Resolved << 1)
1483 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001484 Emit8(Out, (uint8_t)Flags);
1485 Emit16(Out, (uint16_t) Data.NumIncludes);
1486
1487 if (!Data.ControllingMacro)
1488 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1489 else
1490 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001491
1492 unsigned Offset = 0;
1493 if (!Data.Framework.empty()) {
1494 // If this header refers into a framework, save the framework name.
1495 llvm::StringMap<unsigned>::iterator Pos
1496 = FrameworkNameOffset.find(Data.Framework);
1497 if (Pos == FrameworkNameOffset.end()) {
1498 Offset = FrameworkStringData.size() + 1;
1499 FrameworkStringData.append(Data.Framework.begin(),
1500 Data.Framework.end());
1501 FrameworkStringData.push_back(0);
1502
1503 FrameworkNameOffset[Data.Framework] = Offset;
1504 } else
1505 Offset = Pos->second;
1506 }
1507 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001508
1509 if (Data.isModuleHeader) {
1510 Module *Mod = HS.findModuleForHeader(key.FE);
1511 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1512 }
1513
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001514 assert(Out.tell() - Start == DataLen && "Wrong data length");
1515 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001516
1517 const char *strings_begin() const { return FrameworkStringData.begin(); }
1518 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001519 };
1520} // end anonymous namespace
1521
1522/// \brief Write the header search block for the list of files that
1523///
1524/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001525void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001526 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001527 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1528
1529 if (FilesByUID.size() > HS.header_file_size())
1530 FilesByUID.resize(HS.header_file_size());
1531
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001532 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001533 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001534 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001535 unsigned NumHeaderSearchEntries = 0;
1536 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1537 const FileEntry *File = FilesByUID[UID];
1538 if (!File)
1539 continue;
1540
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001541 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1542 // from the external source if it was not provided already.
1543 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001544 if (HFI.External && Chain)
1545 continue;
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001546 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1547 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001548
1549 // Turn the file name into an absolute path, if it isn't already.
1550 const char *Filename = File->getName();
1551 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1552
1553 // If we performed any translation on the file name at all, we need to
1554 // save this string, since the generator will refer to it later.
1555 if (Filename != File->getName()) {
1556 Filename = strdup(Filename);
1557 SavedStrings.push_back(Filename);
1558 }
1559
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001560 HeaderFileInfoTrait::key_type key = { File, Filename };
1561 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001562 ++NumHeaderSearchEntries;
1563 }
1564
1565 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001566 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001567 uint32_t BucketOffset;
1568 {
1569 llvm::raw_svector_ostream Out(TableData);
1570 // Make sure that no bucket is at offset 0
1571 clang::io::Emit32(Out, 0);
1572 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1573 }
1574
1575 // Create a blob abbreviation
1576 using namespace llvm;
1577 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1578 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1583 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1584
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001585 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001586 RecordData Record;
1587 Record.push_back(HEADER_SEARCH_TABLE);
1588 Record.push_back(BucketOffset);
1589 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001590 Record.push_back(TableData.size());
1591 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001592 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1593
1594 // Free all of the strings we had to duplicate.
1595 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001596 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001597}
1598
Douglas Gregor14f79002009-04-10 03:52:48 +00001599/// \brief Writes the block containing the serialized form of the
1600/// source manager.
1601///
1602/// TODO: We should probably use an on-disk hash table (stored in a
1603/// blob), indexed based on the file name, so that we only create
1604/// entries for files that we actually need. In the common case (no
1605/// errors), we probably won't have to create file entries for any of
1606/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001607void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001608 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001609 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001610 RecordData Record;
1611
Chris Lattnerf04ad692009-04-10 17:16:57 +00001612 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001613 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001614
1615 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001616 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1617 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1618 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001619 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001620
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001621 // Write out the source location entry table. We skip the first
1622 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001623 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001624 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001625 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1626 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001627 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001628 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001629 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001630 FileID FID = FileID::get(I);
1631 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001632
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001633 // Record the offset of this source-location entry.
1634 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1635
1636 // Figure out which record code to use.
1637 unsigned Code;
1638 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001639 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1640 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001641 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001642 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001643 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001644 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001645 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001646 Record.clear();
1647 Record.push_back(Code);
1648
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001649 // Starting offset of this entry within this module, so skip the dummy.
1650 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001651 if (SLoc->isFile()) {
1652 const SrcMgr::FileInfo &File = SLoc->getFile();
1653 Record.push_back(File.getIncludeLoc().getRawEncoding());
1654 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1655 Record.push_back(File.hasLineDirectives());
1656
1657 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001658 if (Content->OrigEntry) {
1659 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001660 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001661
Douglas Gregora930dc92012-10-22 18:42:04 +00001662 // The source location entry is a file. Emit input file ID.
1663 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1664 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001666 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001667
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001668 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001669 if (FDI != FileDeclIDs.end()) {
1670 Record.push_back(FDI->second->FirstDeclIndex);
1671 Record.push_back(FDI->second->DeclIDs.size());
1672 } else {
1673 Record.push_back(0);
1674 Record.push_back(0);
1675 }
Douglas Gregora081da52011-11-16 20:05:18 +00001676
Douglas Gregora930dc92012-10-22 18:42:04 +00001677 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001678
1679 if (Content->BufferOverridden) {
1680 Record.clear();
1681 Record.push_back(SM_SLOC_BUFFER_BLOB);
1682 const llvm::MemoryBuffer *Buffer
1683 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1684 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1685 StringRef(Buffer->getBufferStart(),
1686 Buffer->getBufferSize() + 1));
1687 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688 } else {
1689 // The source location entry is a buffer. The blob associated
1690 // with this entry contains the contents of the buffer.
1691
1692 // We add one to the size so that we capture the trailing NULL
1693 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1694 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001695 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001696 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001698 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001699 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001700 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001701 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001702 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001703 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001704 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001705
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001706 if (strcmp(Name, "<built-in>") == 0) {
1707 PreloadSLocs.push_back(SLocEntryOffsets.size());
1708 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001709 }
1710 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001711 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001712 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001713 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1714 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001715 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1716 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001717
1718 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001719 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001720 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001721 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001722 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001723 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001724 }
1725 }
1726
Douglas Gregorc9490c02009-04-16 22:23:12 +00001727 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001728
1729 if (SLocEntryOffsets.empty())
1730 return;
1731
Sebastian Redl3397c552010-08-18 23:56:27 +00001732 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001733 // table is used for lazily loading source-location information.
1734 using namespace llvm;
1735 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001736 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1740 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001742 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001743 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001744 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001745 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001746 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001747
Sebastian Redl3397c552010-08-18 23:56:27 +00001748 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001749 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001750 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001751
1752 // Write the line table. It depends on remapping working, so it must come
1753 // after the source location offsets.
1754 if (SourceMgr.hasLineTable()) {
1755 LineTableInfo &LineTable = SourceMgr.getLineTable();
1756
1757 Record.clear();
1758 // Emit the file names
1759 Record.push_back(LineTable.getNumFilenames());
1760 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1761 // Emit the file name
1762 const char *Filename = LineTable.getFilename(I);
1763 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1764 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1765 Record.push_back(FilenameLen);
1766 if (FilenameLen)
1767 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1768 }
1769
1770 // Emit the line entries
1771 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1772 L != LEnd; ++L) {
1773 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001774 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001775 continue;
1776
1777 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001778 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001779
1780 // Emit the line entries
1781 Record.push_back(L->second.size());
1782 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1783 LEEnd = L->second.end();
1784 LE != LEEnd; ++LE) {
1785 Record.push_back(LE->FileOffset);
1786 Record.push_back(LE->LineNo);
1787 Record.push_back(LE->FilenameID);
1788 Record.push_back((unsigned)LE->FileKind);
1789 Record.push_back(LE->IncludeOffset);
1790 }
1791 }
1792 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1793 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001794}
1795
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001796//===----------------------------------------------------------------------===//
1797// Preprocessor Serialization
1798//===----------------------------------------------------------------------===//
1799
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001800namespace {
1801class ASTMacroTableTrait {
1802public:
1803 typedef IdentID key_type;
1804 typedef key_type key_type_ref;
1805
1806 struct Data {
1807 uint32_t MacroDirectivesOffset;
1808 };
1809
1810 typedef Data data_type;
1811 typedef const data_type &data_type_ref;
1812
1813 static unsigned ComputeHash(IdentID IdID) {
1814 return llvm::hash_value(IdID);
1815 }
1816
1817 std::pair<unsigned,unsigned>
1818 static EmitKeyDataLength(raw_ostream& Out,
1819 key_type_ref Key, data_type_ref Data) {
1820 unsigned KeyLen = 4; // IdentID.
1821 unsigned DataLen = 4; // MacroDirectivesOffset.
1822 return std::make_pair(KeyLen, DataLen);
1823 }
1824
1825 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1826 clang::io::Emit32(Out, Key);
1827 }
1828
1829 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1830 unsigned) {
1831 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1832 }
1833};
1834} // end anonymous namespace
1835
1836static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1837 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1838 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1839 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1840 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001841 return X.first->getName().compare(Y.first->getName());
1842}
1843
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001844static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1845 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001846 if (MacroInfo *MI = MD->getMacroInfo())
1847 if (MI->isBuiltinMacro())
1848 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001849
1850 if (IsModule) {
1851 SourceLocation Loc = MD->getLocation();
1852 if (Loc.isInvalid())
1853 return true;
1854 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1855 return true;
1856 }
1857
1858 return false;
1859}
1860
Chris Lattner0b1fb982009-04-10 17:15:23 +00001861/// \brief Writes the block containing the serialized form of the
1862/// preprocessor.
1863///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001864void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001865 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1866 if (PPRec)
1867 WritePreprocessorDetail(*PPRec);
1868
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001869 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001870
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001871 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1872 if (PP.getCounterValue() != 0) {
1873 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001874 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001875 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001876 }
1877
1878 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001879 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Sebastian Redl3397c552010-08-18 23:56:27 +00001881 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001882 // FIXME: use diagnostics subsystem for localization etc.
1883 if (PP.SawDateOrTime())
1884 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregorecdcb882010-10-20 22:00:55 +00001886
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001887 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001888 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001889
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001890 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001891 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001892 MacroDirectives;
1893 for (Preprocessor::macro_iterator
1894 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1895 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001896 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001897 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001898 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001899
Douglas Gregor9c736102011-02-10 18:20:09 +00001900 // Sort the set of macro definitions that need to be serialized by the
1901 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001902 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1903 &compareMacroDirectives);
1904
1905 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1906
1907 // Emit the macro directives as a list and associate the offset with the
1908 // identifier they belong to.
1909 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1910 const IdentifierInfo *Name = MacroDirectives[I].first;
1911 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1912 MacroDirective *MD = MacroDirectives[I].second;
1913
1914 // If the macro or identifier need no updates, don't write the macro history
1915 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001916 // FIXME: Chain the macro history instead of re-writing it.
1917 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001918 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1919 continue;
1920
1921 // Emit the macro directives in reverse source order.
1922 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001923 if (MD->isHidden())
1924 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001925 if (shouldIgnoreMacro(MD, IsModule, PP))
1926 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001927
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001928 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001929 Record.push_back(MD->getKind());
1930 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1931 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1932 Record.push_back(InfoID);
1933 Record.push_back(DefMD->isImported());
1934 Record.push_back(DefMD->isAmbiguous());
1935
1936 } else if (VisibilityMacroDirective *
1937 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1938 Record.push_back(VisMD->isPublic());
1939 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001940 }
1941 if (Record.empty())
1942 continue;
1943
1944 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1945 Record.clear();
1946
1947 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1948
1949 IdentID NameID = getIdentifierRef(Name);
1950 ASTMacroTableTrait::Data data;
1951 data.MacroDirectivesOffset = MacroDirectiveOffset;
1952 Generator.insert(NameID, data);
1953 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001954
Douglas Gregora8235d62012-10-09 23:05:51 +00001955 /// \brief Offsets of each of the macros into the bitstream, indexed by
1956 /// the local macro ID
1957 ///
1958 /// For each identifier that is associated with a macro, this map
1959 /// provides the offset into the bitstream where that macro is
1960 /// defined.
1961 std::vector<uint32_t> MacroOffsets;
1962
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001963 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1964 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1965 MacroInfo *MI = MacroInfosToEmit[I].MI;
1966 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001967
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001968 if (ID < FirstMacroID) {
1969 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1970 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001971 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001972
1973 // Record the local offset of this macro.
1974 unsigned Index = ID - FirstMacroID;
1975 if (Index == MacroOffsets.size())
1976 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1977 else {
1978 if (Index > MacroOffsets.size())
1979 MacroOffsets.resize(Index + 1);
1980
1981 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1982 }
1983
1984 AddIdentifierRef(Name, Record);
1985 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1986 AddSourceLocation(MI->getDefinitionLoc(), Record);
1987 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1988 Record.push_back(MI->isUsed());
1989 unsigned Code;
1990 if (MI->isObjectLike()) {
1991 Code = PP_MACRO_OBJECT_LIKE;
1992 } else {
1993 Code = PP_MACRO_FUNCTION_LIKE;
1994
1995 Record.push_back(MI->isC99Varargs());
1996 Record.push_back(MI->isGNUVarargs());
1997 Record.push_back(MI->hasCommaPasting());
1998 Record.push_back(MI->getNumArgs());
1999 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
2000 I != E; ++I)
2001 AddIdentifierRef(*I, Record);
2002 }
2003
2004 // If we have a detailed preprocessing record, record the macro definition
2005 // ID that corresponds to this macro.
2006 if (PPRec)
2007 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2008
2009 Stream.EmitRecord(Code, Record);
2010 Record.clear();
2011
2012 // Emit the tokens array.
2013 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2014 // Note that we know that the preprocessor does not have any annotation
2015 // tokens in it because they are created by the parser, and thus can't
2016 // be in a macro definition.
2017 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002018 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002019 Stream.EmitRecord(PP_TOKEN, Record);
2020 Record.clear();
2021 }
2022 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002023 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002024
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002025 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002026
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002027 // Create the on-disk hash table in a buffer.
2028 SmallString<4096> MacroTable;
2029 uint32_t BucketOffset;
2030 {
2031 llvm::raw_svector_ostream Out(MacroTable);
2032 // Make sure that no bucket is at offset 0
2033 clang::io::Emit32(Out, 0);
2034 BucketOffset = Generator.Emit(Out);
2035 }
2036
2037 // Write the macro table
2038 using namespace llvm;
2039 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2040 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2043 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2044
2045 Record.push_back(MACRO_TABLE);
2046 Record.push_back(BucketOffset);
2047 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2048 Record.clear();
2049
Douglas Gregora8235d62012-10-09 23:05:51 +00002050 // Write the offsets table for macro IDs.
2051 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002052 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002053 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2056 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2057
2058 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2059 Record.clear();
2060 Record.push_back(MACRO_OFFSET);
2061 Record.push_back(MacroOffsets.size());
2062 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2063 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2064 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002065}
2066
2067void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002068 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002069 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002070
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002071 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002072
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002073 // Enter the preprocessor block.
2074 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002075
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002076 // If the preprocessor has a preprocessing record, emit it.
2077 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002078 using namespace llvm;
2079
2080 // Set up the abbreviation for
2081 unsigned InclusionAbbrev = 0;
2082 {
2083 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2084 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002089 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2090 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2091 }
2092
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002093 unsigned FirstPreprocessorEntityID
2094 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2095 + NUM_PREDEF_PP_ENTITY_IDS;
2096 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002097 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002098 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2099 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002100 E != EEnd;
2101 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002102 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002103
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002104 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2105 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002106
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002107 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002108 // Record this macro definition's ID.
2109 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002110
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002111 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002112 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2113 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002114 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002115
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002116 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002117 Record.push_back(ME->isBuiltinMacro());
2118 if (ME->isBuiltinMacro())
2119 AddIdentifierRef(ME->getName(), Record);
2120 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002121 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002122 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002123 continue;
2124 }
2125
2126 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2127 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002128 Record.push_back(ID->getFileName().size());
2129 Record.push_back(ID->wasInQuotes());
2130 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002131 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002132 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002133 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002134 // Check that the FileEntry is not null because it was not resolved and
2135 // we create a PCH even with compiler errors.
2136 if (ID->getFile())
2137 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002138 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2139 continue;
2140 }
2141
2142 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2143 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002144 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002145
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002146 // Write the offsets table for the preprocessing record.
2147 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002148 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2149
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002150 // Write the offsets table for identifier IDs.
2151 using namespace llvm;
2152 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002153 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002156 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002157
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002158 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002159 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002160 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002161 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2162 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002163 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002164}
2165
Douglas Gregore209e502011-12-06 01:10:29 +00002166unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2167 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2168 if (Known != SubmoduleIDs.end())
2169 return Known->second;
2170
2171 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2172}
2173
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002174unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2175 if (!Mod)
2176 return 0;
2177
2178 llvm::DenseMap<Module *, unsigned>::const_iterator
2179 Known = SubmoduleIDs.find(Mod);
2180 if (Known != SubmoduleIDs.end())
2181 return Known->second;
2182
2183 return 0;
2184}
2185
Douglas Gregor26ced122011-12-01 00:59:36 +00002186/// \brief Compute the number of modules within the given tree (including the
2187/// given module).
2188static unsigned getNumberOfModules(Module *Mod) {
2189 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002190 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2191 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002192 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002193 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002194
2195 return ChildModules + 1;
2196}
2197
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002198void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002199 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002200 // FIXME: This feels like it belongs somewhere else, but there are no
2201 // other consumers of this information.
2202 SourceManager &SrcMgr = PP->getSourceManager();
2203 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2204 for (ASTContext::import_iterator I = Context->local_import_begin(),
2205 IEnd = Context->local_import_end();
2206 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002207 if (Module *ImportedFrom
2208 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2209 SrcMgr))) {
2210 ImportedFrom->Imports.push_back(I->getImportedModule());
2211 }
2212 }
2213
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002214 // Enter the submodule description block.
2215 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2216
2217 // Write the abbreviations needed for the submodules block.
2218 using namespace llvm;
2219 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2220 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2231 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2232
2233 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002234 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2236 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2237
2238 Abbrev = new BitCodeAbbrev();
2239 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2241 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002242
2243 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002244 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2246 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2247
2248 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002249 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2250 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2251 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2252
Douglas Gregor51f564f2011-12-31 04:05:44 +00002253 Abbrev = new BitCodeAbbrev();
2254 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2256 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2257
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002258 Abbrev = new BitCodeAbbrev();
2259 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2261 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2262
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002263 Abbrev = new BitCodeAbbrev();
2264 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2267 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2268
Douglas Gregor63a72682013-03-20 00:22:05 +00002269 Abbrev = new BitCodeAbbrev();
2270 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2272 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2273
Douglas Gregor906d66a2013-03-20 21:10:35 +00002274 Abbrev = new BitCodeAbbrev();
2275 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2278 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2279
Douglas Gregor26ced122011-12-01 00:59:36 +00002280 // Write the submodule metadata block.
2281 RecordData Record;
2282 Record.push_back(getNumberOfModules(WritingModule));
2283 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2284 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2285
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002286 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002287 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002288 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002289 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002290 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002291 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002292 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002293
2294 // Emit the definition of the block.
2295 Record.clear();
2296 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002297 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002298 if (Mod->Parent) {
2299 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2300 Record.push_back(SubmoduleIDs[Mod->Parent]);
2301 } else {
2302 Record.push_back(0);
2303 }
2304 Record.push_back(Mod->IsFramework);
2305 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002306 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002307 Record.push_back(Mod->InferSubmodules);
2308 Record.push_back(Mod->InferExplicitSubmodules);
2309 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002310 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002311 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2312
Douglas Gregor51f564f2011-12-31 04:05:44 +00002313 // Emit the requirements.
2314 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2315 Record.clear();
2316 Record.push_back(SUBMODULE_REQUIRES);
2317 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2318 Mod->Requires[I].data(),
2319 Mod->Requires[I].size());
2320 }
2321
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002322 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002323 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002324 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002325 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002326 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002327 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002328 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2329 Record.clear();
2330 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2331 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2332 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002333 }
2334
2335 // Emit the headers.
2336 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2337 Record.clear();
2338 Record.push_back(SUBMODULE_HEADER);
2339 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2340 Mod->Headers[I]->getName());
2341 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002342 // Emit the excluded headers.
2343 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2344 Record.clear();
2345 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2346 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2347 Mod->ExcludedHeaders[I]->getName());
2348 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002349 ArrayRef<const FileEntry *>
2350 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2351 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002352 Record.clear();
2353 Record.push_back(SUBMODULE_TOPHEADER);
2354 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002355 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002356 }
Douglas Gregor55988682011-12-05 16:33:54 +00002357
2358 // Emit the imports.
2359 if (!Mod->Imports.empty()) {
2360 Record.clear();
2361 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002362 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002363 assert(ImportedID && "Unknown submodule!");
2364 Record.push_back(ImportedID);
2365 }
2366 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2367 }
2368
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002369 // Emit the exports.
2370 if (!Mod->Exports.empty()) {
2371 Record.clear();
2372 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002373 if (Module *Exported = Mod->Exports[I].getPointer()) {
2374 unsigned ExportedID = SubmoduleIDs[Exported];
2375 assert(ExportedID > 0 && "Unknown submodule ID?");
2376 Record.push_back(ExportedID);
2377 } else {
2378 Record.push_back(0);
2379 }
2380
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002381 Record.push_back(Mod->Exports[I].getInt());
2382 }
2383 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2384 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002385
2386 // Emit the link libraries.
2387 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2388 Record.clear();
2389 Record.push_back(SUBMODULE_LINK_LIBRARY);
2390 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2391 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2392 Mod->LinkLibraries[I].Library);
2393 }
2394
Douglas Gregor906d66a2013-03-20 21:10:35 +00002395 // Emit the conflicts.
2396 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2397 Record.clear();
2398 Record.push_back(SUBMODULE_CONFLICT);
2399 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2400 assert(OtherID && "Unknown submodule!");
2401 Record.push_back(OtherID);
2402 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2403 Mod->Conflicts[I].Message);
2404 }
2405
Douglas Gregor63a72682013-03-20 00:22:05 +00002406 // Emit the configuration macros.
2407 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2408 Record.clear();
2409 Record.push_back(SUBMODULE_CONFIG_MACRO);
2410 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2411 Mod->ConfigMacros[I]);
2412 }
2413
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002414 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002415 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2416 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002417 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002418 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002419 }
2420
2421 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002422
2423 assert((NextSubmoduleID - FirstSubmoduleID
2424 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002425}
2426
Douglas Gregor185dbd72011-12-01 02:07:58 +00002427serialization::SubmoduleID
2428ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002429 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002430 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002431
2432 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002433 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002434 Module *OwningMod
2435 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002436 if (!OwningMod)
2437 return 0;
2438
Douglas Gregore209e502011-12-06 01:10:29 +00002439 // Check whether this submodule is part of our own module.
2440 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002441 return 0;
2442
Douglas Gregore209e502011-12-06 01:10:29 +00002443 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002444}
2445
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002446void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2447 bool isModule) {
2448 // Make sure set diagnostic pragmas don't affect the translation unit that
2449 // imports the module.
2450 // FIXME: Make diagnostic pragma sections work properly with modules.
2451 if (isModule)
2452 return;
2453
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002454 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2455 DiagStateIDMap;
2456 unsigned CurrID = 0;
2457 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002458 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002459 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002460 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2461 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002462 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002463 if (point.Loc.isInvalid())
2464 continue;
2465
2466 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002467 unsigned &DiagStateID = DiagStateIDMap[point.State];
2468 Record.push_back(DiagStateID);
2469
2470 if (DiagStateID == 0) {
2471 DiagStateID = ++CurrID;
2472 for (DiagnosticsEngine::DiagState::const_iterator
2473 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2474 if (I->second.isPragma()) {
2475 Record.push_back(I->first);
2476 Record.push_back(I->second.getMapping());
2477 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002478 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002479 Record.push_back(-1); // mark the end of the diag/map pairs for this
2480 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002481 }
2482 }
2483
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002484 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002485 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002486}
2487
Anders Carlssonc8505782011-03-06 18:41:18 +00002488void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2489 if (CXXBaseSpecifiersOffsets.empty())
2490 return;
2491
2492 RecordData Record;
2493
2494 // Create a blob abbreviation for the C++ base specifiers offsets.
2495 using namespace llvm;
2496
2497 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2498 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2501 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2502
Douglas Gregore92b8a12011-08-04 00:01:48 +00002503 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002504 Record.clear();
2505 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2506 Record.push_back(CXXBaseSpecifiersOffsets.size());
2507 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002508 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002509}
2510
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002511//===----------------------------------------------------------------------===//
2512// Type Serialization
2513//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002514
Sebastian Redl3397c552010-08-18 23:56:27 +00002515/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002516void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002517 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002518 if (Idx.getIndex() == 0) // we haven't seen this type before.
2519 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002520
Douglas Gregor97475832010-10-05 18:37:06 +00002521 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002522
Douglas Gregor2cf26342009-04-09 22:27:44 +00002523 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002524 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002525 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002526 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002527 else if (TypeOffsets.size() < Index) {
2528 TypeOffsets.resize(Index + 1);
2529 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002530 }
2531
2532 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002533
Douglas Gregor2cf26342009-04-09 22:27:44 +00002534 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002535 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002536
Douglas Gregora4923eb2009-11-16 21:35:15 +00002537 if (T.hasLocalNonFastQualifiers()) {
2538 Qualifiers Qs = T.getLocalQualifiers();
2539 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002540 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002541 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002542 } else {
2543 switch (T->getTypeClass()) {
2544 // For all of the concrete, non-dependent types, call the
2545 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002546#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002547 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002548#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002549#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002550 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002551 }
2552
2553 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002554 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002555
2556 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002557 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002558}
2559
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002560//===----------------------------------------------------------------------===//
2561// Declaration Serialization
2562//===----------------------------------------------------------------------===//
2563
Douglas Gregor2cf26342009-04-09 22:27:44 +00002564/// \brief Write the block containing all of the declaration IDs
2565/// lexically declared within the given DeclContext.
2566///
2567/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2568/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002569uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002570 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002571 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002572 return 0;
2573
Douglas Gregorc9490c02009-04-16 22:23:12 +00002574 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002575 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002576 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002577 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002578 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2579 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002580 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002581
Douglas Gregor25123082009-04-22 22:34:57 +00002582 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002583 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002584 return Offset;
2585}
2586
Sebastian Redla4232eb2010-08-18 23:56:21 +00002587void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002588 using namespace llvm;
2589 RecordData Record;
2590
2591 // Write the type offsets array
2592 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002593 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002596 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2597 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2598 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002599 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002600 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002601 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002602 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002603
2604 // Write the declaration offsets array
2605 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002606 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002609 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2610 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2611 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002613 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002614 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002615 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002616}
2617
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002618void ASTWriter::WriteFileDeclIDsMap() {
2619 using namespace llvm;
2620 RecordData Record;
2621
2622 // Join the vectors of DeclIDs from all files.
2623 SmallVector<DeclID, 256> FileSortedIDs;
2624 for (FileDeclIDsTy::iterator
2625 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2626 DeclIDInFileInfo &Info = *FI->second;
2627 Info.FirstDeclIndex = FileSortedIDs.size();
2628 for (LocDeclIDsTy::iterator
2629 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2630 FileSortedIDs.push_back(DI->second);
2631 }
2632
2633 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2634 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002636 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2637 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2638 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002639 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002640 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2641}
2642
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002643void ASTWriter::WriteComments() {
2644 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002645 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002646 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002647 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2648 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002649 I != E; ++I) {
2650 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002651 AddSourceRange((*I)->getSourceRange(), Record);
2652 Record.push_back((*I)->getKind());
2653 Record.push_back((*I)->isTrailingComment());
2654 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002655 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2656 }
2657 Stream.ExitBlock();
2658}
2659
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002660//===----------------------------------------------------------------------===//
2661// Global Method Pool and Selector Serialization
2662//===----------------------------------------------------------------------===//
2663
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002664namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002665// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002666class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002667 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002668
2669public:
2670 typedef Selector key_type;
2671 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002672
Sebastian Redl5d050072010-08-04 17:20:04 +00002673 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002674 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002675 ObjCMethodList Instance, Factory;
2676 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002677 typedef const data_type& data_type_ref;
2678
Sebastian Redl3397c552010-08-18 23:56:27 +00002679 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002681 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002682 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002683 }
Mike Stump1eb44332009-09-09 15:08:12 +00002684
2685 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002686 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002687 data_type_ref Methods) {
2688 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2689 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002690 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2691 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002692 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002693 if (Method->Method)
2694 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002695 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002696 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002697 if (Method->Method)
2698 DataLen += 4;
2699 clang::io::Emit16(Out, DataLen);
2700 return std::make_pair(KeyLen, DataLen);
2701 }
Mike Stump1eb44332009-09-09 15:08:12 +00002702
Chris Lattner5f9e2722011-07-23 10:55:15 +00002703 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002704 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002705 assert((Start >> 32) == 0 && "Selector key offset too large");
2706 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002707 unsigned N = Sel.getNumArgs();
2708 clang::io::Emit16(Out, N);
2709 if (N == 0)
2710 N = 1;
2711 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002712 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002713 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2714 }
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002717 data_type_ref Methods, unsigned DataLen) {
2718 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002719 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002720 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002721 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002722 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002723 if (Method->Method)
2724 ++NumInstanceMethods;
2725
2726 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002727 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002728 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002729 if (Method->Method)
2730 ++NumFactoryMethods;
2731
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002732 unsigned InstanceBits = Methods.Instance.getBits();
2733 assert(InstanceBits < 4);
2734 unsigned NumInstanceMethodsAndBits =
2735 (NumInstanceMethods << 2) | InstanceBits;
2736 unsigned FactoryBits = Methods.Factory.getBits();
2737 assert(FactoryBits < 4);
2738 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2739 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2740 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002741 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002742 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002743 if (Method->Method)
2744 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002745 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002746 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002747 if (Method->Method)
2748 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002749
2750 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002751 }
2752};
2753} // end anonymous namespace
2754
Sebastian Redl059612d2010-08-03 21:58:15 +00002755/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002756///
2757/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002758/// in an on-disk hash table indexed by the selector. The hash table also
2759/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002760void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002761 using namespace llvm;
2762
Sebastian Redl059612d2010-08-03 21:58:15 +00002763 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002764 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002765 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002766 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002767 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002768 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002769 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002770 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Sebastian Redl059612d2010-08-03 21:58:15 +00002772 // Create the on-disk hash table representation. We walk through every
2773 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002774 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002775 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002776 I = SelectorIDs.begin(), E = SelectorIDs.end();
2777 I != E; ++I) {
2778 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002779 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002780 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002781 I->second,
2782 ObjCMethodList(),
2783 ObjCMethodList()
2784 };
2785 if (F != SemaRef.MethodPool.end()) {
2786 Data.Instance = F->second.first;
2787 Data.Factory = F->second.second;
2788 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002789 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002790 // changed.
2791 if (Chain && I->second < FirstSelectorID) {
2792 // Selector already exists. Did it change?
2793 bool changed = false;
2794 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002795 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002796 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002797 changed = true;
2798 }
2799 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002800 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002801 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002802 changed = true;
2803 }
2804 if (!changed)
2805 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002806 } else if (Data.Instance.Method || Data.Factory.Method) {
2807 // A new method pool entry.
2808 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002809 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002810 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002811 }
2812
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002813 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002814 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002815 uint32_t BucketOffset;
2816 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002817 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002818 llvm::raw_svector_ostream Out(MethodPool);
2819 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002820 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002821 BucketOffset = Generator.Emit(Out, Trait);
2822 }
2823
2824 // Create a blob abbreviation
2825 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002826 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2830 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2831
Douglas Gregor83941df2009-04-25 17:48:32 +00002832 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002833 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002834 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002835 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002836 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002837 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002838
2839 // Create a blob abbreviation for the selector table offsets.
2840 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002841 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2845 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2846
2847 // Write the selector offsets table.
2848 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002849 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002850 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002851 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002852 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002853 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002854 }
2855}
2856
Sebastian Redl3397c552010-08-18 23:56:27 +00002857/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002858void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002859 using namespace llvm;
2860 if (SemaRef.ReferencedSelectors.empty())
2861 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002862
Fariborz Jahanian32019832010-07-23 19:11:11 +00002863 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002864
Sebastian Redl3397c552010-08-18 23:56:27 +00002865 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002866 // very tricky to fix, and given that @selector shouldn't really appear in
2867 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002868 for (DenseMap<Selector, SourceLocation>::iterator S =
2869 SemaRef.ReferencedSelectors.begin(),
2870 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2871 Selector Sel = (*S).first;
2872 SourceLocation Loc = (*S).second;
2873 AddSelectorRef(Sel, Record);
2874 AddSourceLocation(Loc, Record);
2875 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002876 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002877}
2878
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002879//===----------------------------------------------------------------------===//
2880// Identifier Table Serialization
2881//===----------------------------------------------------------------------===//
2882
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002883namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002884class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002885 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002886 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002887 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002888 bool IsModule;
2889
Douglas Gregora92193e2009-04-28 21:18:29 +00002890 /// \brief Determines whether this is an "interesting" identifier
2891 /// that needs a full IdentifierInfo structure written into the hash
2892 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002893 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002894 if (II->isPoisoned() ||
2895 II->isExtensionToken() ||
2896 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002897 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002898 II->getFETokenInfo<void>())
2899 return true;
2900
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002901 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002902 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002903
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002904 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002905 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002906 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002907
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002908 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2909 if (!IsModule)
2910 return !shouldIgnoreMacro(Macro, IsModule, PP);
2911 SubmoduleID ModID;
2912 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2913 return true;
2914 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002915
2916 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002917 }
2918
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002919 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2920 SubmoduleID &ModID) {
2921 ModID = 0;
2922 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2923 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2924 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002925 return 0;
2926 }
2927
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002928 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2929 SubmoduleID &ModID) {
2930 if (DefMacroDirective *
2931 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2932 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2933 return DefMD;
2934 return 0;
2935 }
2936
2937 /// \brief Traverses the macro directives history and returns the latest
2938 /// macro that is public and not undefined in the same submodule.
2939 /// A macro that is defined in submodule A and undefined in submodule B,
2940 /// will still be considered as defined/exported from submodule A.
2941 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2942 SubmoduleID &ModID) {
2943 if (!MD)
2944 return 0;
2945
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002946 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002947 bool isUndefined = false;
2948 Optional<bool> isPublic;
2949 for (; MD; MD = MD->getPrevious()) {
2950 if (MD->isHidden())
2951 continue;
2952
2953 SubmoduleID ThisModID = getSubmoduleID(MD);
2954 if (ThisModID == 0) {
2955 isUndefined = false;
2956 isPublic = Optional<bool>();
2957 continue;
2958 }
2959 if (ThisModID != ModID){
2960 ModID = ThisModID;
2961 isUndefined = false;
2962 isPublic = Optional<bool>();
2963 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002964 // We are looking for a definition in a different submodule than the one
2965 // that we started with. If a submodule has re-definitions of the same
2966 // macro, only the last definition will be used as the "exported" one.
2967 if (ModID == OrigModID)
2968 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002969
2970 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2971 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2972 return DefMD;
2973 continue;
2974 }
2975
2976 if (isa<UndefMacroDirective>(MD)) {
2977 isUndefined = true;
2978 continue;
2979 }
2980
2981 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2982 if (!isPublic.hasValue())
2983 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002984 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002985
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002986 return 0;
2987 }
2988
2989 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002990 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2991 MacroInfo *MI = DefMD->getInfo();
2992 if (unsigned ID = MI->getOwningModuleID())
2993 return ID;
2994 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
2995 }
2996 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002997 }
2998
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002999public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00003000 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003001 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003003 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003004 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Douglas Gregoreee242f2011-10-27 09:33:13 +00003006 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3007 IdentifierResolver &IdResolver, bool IsModule)
3008 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003009
3010 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003011 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003012 }
Mike Stump1eb44332009-09-09 15:08:12 +00003013
3014 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003015 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003016 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003017 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003018 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003019 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003020 DataLen += 2; // 2 bytes for builtin ID
3021 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003022 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003023 DataLen += 4; // MacroDirectives offset.
3024 if (IsModule) {
3025 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003026 for (DefMacroDirective *
3027 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3028 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003029 DataLen += 4; // MacroInfo ID.
3030 }
3031 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003032 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003033 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003034
Douglas Gregoreee242f2011-10-27 09:33:13 +00003035 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3036 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003037 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003038 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003039 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003040 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003041 // We emit the key length after the data length so that every
3042 // string is preceded by a 16-bit length. This matches the PTH
3043 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003044 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003045 return std::make_pair(KeyLen, DataLen);
3046 }
Mike Stump1eb44332009-09-09 15:08:12 +00003047
Chris Lattner5f9e2722011-07-23 10:55:15 +00003048 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003049 unsigned KeyLen) {
3050 // Record the location of the key data. This is used when generating
3051 // the mapping from persistent IDs to strings.
3052 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003053 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003054 }
Mike Stump1eb44332009-09-09 15:08:12 +00003055
Douglas Gregor7143aab2011-09-01 17:04:32 +00003056 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003057 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003058 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003059 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003060 clang::io::Emit32(Out, ID << 1);
3061 return;
3062 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003063
Douglas Gregora92193e2009-04-28 21:18:29 +00003064 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003065 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3066 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3067 clang::io::Emit16(Out, Bits);
3068 Bits = 0;
3069 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003070 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003071 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003072 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3073 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003074 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003075 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003076 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003077
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003078 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003079 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3080 if (IsModule) {
3081 // Write the IDs of macros coming from different submodules.
3082 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003083 for (DefMacroDirective *
3084 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3085 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3086 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003087 assert(InfoID);
3088 clang::io::Emit32(Out, InfoID);
3089 }
3090 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003091 }
Douglas Gregor13292642011-12-02 15:45:10 +00003092 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003093
Douglas Gregor668c1a42009-04-21 22:25:48 +00003094 // Emit the declaration IDs in reverse order, because the
3095 // IdentifierResolver provides the declarations as they would be
3096 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003097 // "stat"), but the ASTReader adds declarations to the end of the list
3098 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003099 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003100 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3101 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003102 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003103 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003104 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003105 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3106 }
3107
3108 /// \brief Returns the most recent local decl or the given decl if there are
3109 /// no local ones. The given decl is assumed to be the most recent one.
3110 Decl *getMostRecentLocalDecl(Decl *Orig) {
3111 // The only way a "from AST file" decl would be more recent from a local one
3112 // is if it came from a module.
3113 if (!PP.getLangOpts().Modules)
3114 return Orig;
3115
3116 // Look for a local in the decl chain.
3117 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3118 if (!D->isFromASTFile())
3119 return D;
3120 // If we come up a decl from a (chained-)PCH stop since we won't find a
3121 // local one.
3122 if (D->getOwningModuleID() == 0)
3123 break;
3124 }
3125
3126 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003127 }
3128};
3129} // end anonymous namespace
3130
Sebastian Redl3397c552010-08-18 23:56:27 +00003131/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003132///
3133/// The identifier table consists of a blob containing string data
3134/// (the actual identifiers themselves) and a separate "offsets" index
3135/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003136void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3137 IdentifierResolver &IdResolver,
3138 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003139 using namespace llvm;
3140
3141 // Create and write out the blob that contains the identifier
3142 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003143 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003144 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003145 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003146
Douglas Gregor92b059e2009-04-28 20:33:11 +00003147 // Look for any identifiers that were named while processing the
3148 // headers, but are otherwise not needed. We add these to the hash
3149 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003150 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003151 // file.
3152 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3153 IDEnd = PP.getIdentifierTable().end();
3154 ID != IDEnd; ++ID)
3155 getIdentifierRef(ID->second);
3156
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003157 // Create the on-disk hash table representation. We only store offsets
3158 // for identifiers that appear here for the first time.
3159 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003160 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003161 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3162 ID != IDEnd; ++ID) {
3163 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003164 if (!Chain || !ID->first->isFromAST() ||
3165 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003166 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003167 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003168 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003169
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003170 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003171 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003172 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003173 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003174 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003175 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003176 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003177 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003178 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003179 }
3180
3181 // Create a blob abbreviation
3182 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003183 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003186 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003187
3188 // Write the identifier table
3189 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003190 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003191 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003192 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003193 }
3194
3195 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003196 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003197 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3201 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3202
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003203#ifndef NDEBUG
3204 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3205 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3206#endif
3207
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003208 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003209 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003210 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003211 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003212 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003213 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003214}
3215
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003216//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003217// DeclContext's Name Lookup Table Serialization
3218//===----------------------------------------------------------------------===//
3219
3220namespace {
3221// Trait used for the on-disk hash table used in the method pool.
3222class ASTDeclContextNameLookupTrait {
3223 ASTWriter &Writer;
3224
3225public:
3226 typedef DeclarationName key_type;
3227 typedef key_type key_type_ref;
3228
3229 typedef DeclContext::lookup_result data_type;
3230 typedef const data_type& data_type_ref;
3231
3232 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3233
3234 unsigned ComputeHash(DeclarationName Name) {
3235 llvm::FoldingSetNodeID ID;
3236 ID.AddInteger(Name.getNameKind());
3237
3238 switch (Name.getNameKind()) {
3239 case DeclarationName::Identifier:
3240 ID.AddString(Name.getAsIdentifierInfo()->getName());
3241 break;
3242 case DeclarationName::ObjCZeroArgSelector:
3243 case DeclarationName::ObjCOneArgSelector:
3244 case DeclarationName::ObjCMultiArgSelector:
3245 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3246 break;
3247 case DeclarationName::CXXConstructorName:
3248 case DeclarationName::CXXDestructorName:
3249 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003250 break;
3251 case DeclarationName::CXXOperatorName:
3252 ID.AddInteger(Name.getCXXOverloadedOperator());
3253 break;
3254 case DeclarationName::CXXLiteralOperatorName:
3255 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3256 case DeclarationName::CXXUsingDirective:
3257 break;
3258 }
3259
3260 return ID.ComputeHash();
3261 }
3262
3263 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003264 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003265 data_type_ref Lookup) {
3266 unsigned KeyLen = 1;
3267 switch (Name.getNameKind()) {
3268 case DeclarationName::Identifier:
3269 case DeclarationName::ObjCZeroArgSelector:
3270 case DeclarationName::ObjCOneArgSelector:
3271 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003272 case DeclarationName::CXXLiteralOperatorName:
3273 KeyLen += 4;
3274 break;
3275 case DeclarationName::CXXOperatorName:
3276 KeyLen += 1;
3277 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003278 case DeclarationName::CXXConstructorName:
3279 case DeclarationName::CXXDestructorName:
3280 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003281 case DeclarationName::CXXUsingDirective:
3282 break;
3283 }
3284 clang::io::Emit16(Out, KeyLen);
3285
3286 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003287 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003288 clang::io::Emit16(Out, DataLen);
3289
3290 return std::make_pair(KeyLen, DataLen);
3291 }
3292
Chris Lattner5f9e2722011-07-23 10:55:15 +00003293 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003294 using namespace clang::io;
3295
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003296 Emit8(Out, Name.getNameKind());
3297 switch (Name.getNameKind()) {
3298 case DeclarationName::Identifier:
3299 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003300 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003301 case DeclarationName::ObjCZeroArgSelector:
3302 case DeclarationName::ObjCOneArgSelector:
3303 case DeclarationName::ObjCMultiArgSelector:
3304 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003305 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003306 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003307 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3308 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003309 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003310 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003311 case DeclarationName::CXXLiteralOperatorName:
3312 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003313 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003314 case DeclarationName::CXXConstructorName:
3315 case DeclarationName::CXXDestructorName:
3316 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003317 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003318 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003319 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003320
3321 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003322 }
3323
Chris Lattner5f9e2722011-07-23 10:55:15 +00003324 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003325 data_type Lookup, unsigned DataLen) {
3326 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003327 clang::io::Emit16(Out, Lookup.size());
3328 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3329 I != E; ++I)
3330 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003331
3332 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3333 }
3334};
3335} // end anonymous namespace
3336
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003337/// \brief Write the block containing all of the declaration IDs
3338/// visible from the given DeclContext.
3339///
3340/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003341/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003342uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3343 DeclContext *DC) {
3344 if (DC->getPrimaryContext() != DC)
3345 return 0;
3346
3347 // Since there is no name lookup into functions or methods, don't bother to
3348 // build a visible-declarations table for these entities.
3349 if (DC->isFunctionOrMethod())
3350 return 0;
3351
3352 // If not in C++, we perform name lookup for the translation unit via the
3353 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003354 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003355 return 0;
3356
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003357 // Serialize the contents of the mapping used for lookup. Note that,
3358 // although we have two very different code paths, the serialized
3359 // representation is the same for both cases: a declaration name,
3360 // followed by a size, followed by references to the visible
3361 // declarations that have that name.
3362 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003363 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003364 if (!Map || Map->empty())
3365 return 0;
3366
3367 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3368 ASTDeclContextNameLookupTrait Trait(*this);
3369
3370 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003371 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003372 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003373 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3374 D != DEnd; ++D) {
3375 DeclarationName Name = D->first;
3376 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003377 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003378 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3379 // Hash all conversion function names to the same name. The actual
3380 // type information in conversion function name is not used in the
3381 // key (since such type information is not stable across different
3382 // modules), so the intended effect is to coalesce all of the conversion
3383 // functions under a single key.
3384 if (!ConversionName)
3385 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003386 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003387 continue;
3388 }
3389
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003390 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003391 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003392 }
3393
Douglas Gregore5a54b62011-08-30 20:49:19 +00003394 // Add the conversion functions
3395 if (!ConversionDecls.empty()) {
3396 Generator.insert(ConversionName,
3397 DeclContext::lookup_result(ConversionDecls.begin(),
3398 ConversionDecls.end()),
3399 Trait);
3400 }
3401
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003402 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003403 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003404 uint32_t BucketOffset;
3405 {
3406 llvm::raw_svector_ostream Out(LookupTable);
3407 // Make sure that no bucket is at offset 0
3408 clang::io::Emit32(Out, 0);
3409 BucketOffset = Generator.Emit(Out, Trait);
3410 }
3411
3412 // Write the lookup table
3413 RecordData Record;
3414 Record.push_back(DECL_CONTEXT_VISIBLE);
3415 Record.push_back(BucketOffset);
3416 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3417 LookupTable.str());
3418
3419 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3420 ++NumVisibleDeclContexts;
3421 return Offset;
3422}
3423
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003424/// \brief Write an UPDATE_VISIBLE block for the given context.
3425///
3426/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3427/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003428/// (in C++), for namespaces, and for classes with forward-declared unscoped
3429/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003430void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003431 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3432 if (!Map || Map->empty())
3433 return;
3434
3435 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3436 ASTDeclContextNameLookupTrait Trait(*this);
3437
3438 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003439 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3440 D != DEnd; ++D) {
3441 DeclarationName Name = D->first;
3442 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003443 // For any name that appears in this table, the results are complete, i.e.
3444 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003445 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003446 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003447 }
3448
3449 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003450 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003451 uint32_t BucketOffset;
3452 {
3453 llvm::raw_svector_ostream Out(LookupTable);
3454 // Make sure that no bucket is at offset 0
3455 clang::io::Emit32(Out, 0);
3456 BucketOffset = Generator.Emit(Out, Trait);
3457 }
3458
3459 // Write the lookup table
3460 RecordData Record;
3461 Record.push_back(UPDATE_VISIBLE);
3462 Record.push_back(getDeclID(cast<Decl>(DC)));
3463 Record.push_back(BucketOffset);
3464 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3465}
3466
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003467/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3468void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3469 RecordData Record;
3470 Record.push_back(Opts.fp_contract);
3471 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3472}
3473
3474/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3475void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003476 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003477 return;
3478
3479 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3480 RecordData Record;
3481#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3482#include "clang/Basic/OpenCLExtensions.def"
3483 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3484}
3485
Douglas Gregor2171bf12012-01-15 16:58:34 +00003486void ASTWriter::WriteRedeclarations() {
3487 RecordData LocalRedeclChains;
3488 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3489
3490 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3491 Decl *First = Redeclarations[I];
3492 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3493
3494 Decl *MostRecent = First->getMostRecentDecl();
3495
3496 // If we only have a single declaration, there is no point in storing
3497 // a redeclaration chain.
3498 if (First == MostRecent)
3499 continue;
3500
3501 unsigned Offset = LocalRedeclChains.size();
3502 unsigned Size = 0;
3503 LocalRedeclChains.push_back(0); // Placeholder for the size.
3504
3505 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003506 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003507 Prev = Prev->getPreviousDecl()) {
3508 if (!Prev->isFromASTFile()) {
3509 AddDeclRef(Prev, LocalRedeclChains);
3510 ++Size;
3511 }
3512 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003513
3514 if (!First->isFromASTFile() && Chain) {
3515 Decl *FirstFromAST = MostRecent;
3516 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3517 if (Prev->isFromASTFile())
3518 FirstFromAST = Prev;
3519 }
3520
3521 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3522 }
3523
Douglas Gregor2171bf12012-01-15 16:58:34 +00003524 LocalRedeclChains[Offset] = Size;
3525
3526 // Reverse the set of local redeclarations, so that we store them in
3527 // order (since we found them in reverse order).
3528 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3529
Douglas Gregoraa945902013-02-18 15:53:43 +00003530 // Add the mapping from the first ID from the AST to the set of local
3531 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003532 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3533 LocalRedeclsMap.push_back(Info);
3534
3535 assert(N == Redeclarations.size() &&
3536 "Deserialized a declaration we shouldn't have");
3537 }
3538
3539 if (LocalRedeclChains.empty())
3540 return;
3541
3542 // Sort the local redeclarations map by the first declaration ID,
3543 // since the reader will be performing binary searches on this information.
3544 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3545
3546 // Emit the local redeclarations map.
3547 using namespace llvm;
3548 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3549 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3552 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3553
3554 RecordData Record;
3555 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3556 Record.push_back(LocalRedeclsMap.size());
3557 Stream.EmitRecordWithBlob(AbbrevID, Record,
3558 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3559 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3560
3561 // Emit the redeclaration chains.
3562 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3563}
3564
Douglas Gregorcff9f262012-01-27 01:47:08 +00003565void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003566 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003567 RecordData Categories;
3568
3569 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3570 unsigned Size = 0;
3571 unsigned StartIndex = Categories.size();
3572
3573 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3574
3575 // Allocate space for the size.
3576 Categories.push_back(0);
3577
3578 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003579 for (ObjCInterfaceDecl::known_categories_iterator
3580 Cat = Class->known_categories_begin(),
3581 CatEnd = Class->known_categories_end();
3582 Cat != CatEnd; ++Cat, ++Size) {
3583 assert(getDeclID(*Cat) != 0 && "Bogus category");
3584 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003585 }
3586
3587 // Update the size.
3588 Categories[StartIndex] = Size;
3589
3590 // Record this interface -> category map.
3591 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3592 CategoriesMap.push_back(CatInfo);
3593 }
3594
3595 // Sort the categories map by the definition ID, since the reader will be
3596 // performing binary searches on this information.
3597 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3598
3599 // Emit the categories map.
3600 using namespace llvm;
3601 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3602 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3605 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3606
3607 RecordData Record;
3608 Record.push_back(OBJC_CATEGORIES_MAP);
3609 Record.push_back(CategoriesMap.size());
3610 Stream.EmitRecordWithBlob(AbbrevID, Record,
3611 reinterpret_cast<char*>(CategoriesMap.data()),
3612 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3613
3614 // Emit the category lists.
3615 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3616}
3617
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003618void ASTWriter::WriteMergedDecls() {
3619 if (!Chain || Chain->MergedDecls.empty())
3620 return;
3621
3622 RecordData Record;
3623 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3624 IEnd = Chain->MergedDecls.end();
3625 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003626 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003627 : getDeclID(I->first);
3628 assert(CanonID && "Merged declaration not known?");
3629
3630 Record.push_back(CanonID);
3631 Record.push_back(I->second.size());
3632 Record.append(I->second.begin(), I->second.end());
3633 }
3634 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3635}
3636
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003637//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003638// General Serialization Routines
3639//===----------------------------------------------------------------------===//
3640
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003641/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003642void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3643 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003644 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003645 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3646 e = Attrs.end(); i != e; ++i){
3647 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003648 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003649 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003650
Sean Huntcf807c42010-08-18 23:23:40 +00003651#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003652
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003653 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003654}
3655
John McCallaeeacf72013-05-03 00:10:13 +00003656void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3657 AddSourceLocation(Tok.getLocation(), Record);
3658 Record.push_back(Tok.getLength());
3659
3660 // FIXME: When reading literal tokens, reconstruct the literal pointer
3661 // if it is needed.
3662 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3663 // FIXME: Should translate token kind to a stable encoding.
3664 Record.push_back(Tok.getKind());
3665 // FIXME: Should translate token flags to a stable encoding.
3666 Record.push_back(Tok.getFlags());
3667}
3668
Chris Lattner5f9e2722011-07-23 10:55:15 +00003669void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003670 Record.push_back(Str.size());
3671 Record.insert(Record.end(), Str.begin(), Str.end());
3672}
3673
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003674void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3675 RecordDataImpl &Record) {
3676 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003677 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003678 Record.push_back(*Minor + 1);
3679 else
3680 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003681 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003682 Record.push_back(*Subminor + 1);
3683 else
3684 Record.push_back(0);
3685}
3686
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003687/// \brief Note that the identifier II occurs at the given offset
3688/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003689void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003690 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003691 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003692 // up earlier in the chain and thus don't need an offset.
3693 if (ID >= FirstIdentID)
3694 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003695}
3696
Douglas Gregor83941df2009-04-25 17:48:32 +00003697/// \brief Note that the selector Sel occurs at the given offset
3698/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003699void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003700 unsigned ID = SelectorIDs[Sel];
3701 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003702 // Don't record offsets for selectors that are also available in a different
3703 // file.
3704 if (ID < FirstSelectorID)
3705 return;
3706 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003707}
3708
Sebastian Redla4232eb2010-08-18 23:56:21 +00003709ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003710 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003711 WritingAST(false), DoneWritingDeclsAndTypes(false),
3712 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003713 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003714 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003715 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3716 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003717 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3718 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003719 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003720 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003721 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003722 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003723 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003724 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003725 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3726 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3727 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003728 DeclTypedefAbbrev(0),
3729 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3730 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003731{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003732}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003733
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003734ASTWriter::~ASTWriter() {
3735 for (FileDeclIDsTy::iterator
3736 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3737 delete I->second;
3738}
3739
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003740void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003741 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003742 Module *WritingModule, StringRef isysroot,
3743 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003744 WritingAST = true;
3745
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003746 ASTHasCompilerErrors = hasErrors;
3747
Douglas Gregor2cf26342009-04-09 22:27:44 +00003748 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003749 Stream.Emit((unsigned)'C', 8);
3750 Stream.Emit((unsigned)'P', 8);
3751 Stream.Emit((unsigned)'C', 8);
3752 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003753
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003754 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003755
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003756 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003757 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003758 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003759 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003760 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003761 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003762 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003763
3764 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003765}
3766
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003767template<typename Vector>
3768static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3769 ASTWriter::RecordData &Record) {
3770 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3771 I != E; ++I) {
3772 Writer.AddDeclRef(*I, Record);
3773 }
3774}
3775
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003776void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003777 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003778 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003779 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003780 using namespace llvm;
3781
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003782 bool isModule = WritingModule != 0;
3783
Douglas Gregorecc2c092011-12-01 22:20:10 +00003784 // Make sure that the AST reader knows to finalize itself.
3785 if (Chain)
3786 Chain->finalizeForWriting();
3787
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003788 ASTContext &Context = SemaRef.Context;
3789 Preprocessor &PP = SemaRef.PP;
3790
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003791 // Set up predefined declaration IDs.
3792 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003793 if (Context.ObjCIdDecl)
3794 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003795 if (Context.ObjCSelDecl)
3796 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003797 if (Context.ObjCClassDecl)
3798 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003799 if (Context.ObjCProtocolClassDecl)
3800 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003801 if (Context.Int128Decl)
3802 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3803 if (Context.UInt128Decl)
3804 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003805 if (Context.ObjCInstanceTypeDecl)
3806 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003807 if (Context.BuiltinVaListDecl)
3808 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3809
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003810 if (!Chain) {
3811 // Make sure that we emit IdentifierInfos (and any attached
3812 // declarations) for builtins. We don't need to do this when we're
3813 // emitting chained PCH files, because all of the builtins will be
3814 // in the original PCH file.
3815 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003816 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003817 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003818 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003819 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003820 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3821 getIdentifierRef(&Table.get(BuiltinNames[I]));
3822 }
3823
Douglas Gregoreee242f2011-10-27 09:33:13 +00003824 // If there are any out-of-date identifiers, bring them up to date.
3825 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003826 // Find out-of-date identifiers.
3827 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003828 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3829 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003830 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003831 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003832 OutOfDate.push_back(ID->second);
3833 }
3834
3835 // Update the out-of-date identifiers.
3836 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3837 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3838 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003839 }
3840
Chris Lattner63d65f82009-09-08 18:19:27 +00003841 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003842 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003843 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003844 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003845 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003846
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003847 // Build a record containing all of the file scoped decls in this file.
3848 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003849 if (!isModule)
3850 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3851 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003852
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003853 // Build a record containing all of the delegating constructors we still need
3854 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003855 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003856 if (!isModule)
3857 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003858
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003859 // Write the set of weak, undeclared identifiers. We always write the
3860 // entire table, since later PCH files in a PCH chain are only interested in
3861 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003862 RecordData WeakUndeclaredIdentifiers;
3863 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003864 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003865 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3866 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3867 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3868 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3869 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3870 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3871 }
3872 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003873
Richard Smith5ea6ef42013-01-10 23:43:47 +00003874 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003875 // declarations in this header file. Generally, this record will be
3876 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003877 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003878 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003879 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003880 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003881 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3882 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003883 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003884 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003885 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003886 }
3887
Douglas Gregorb81c1702009-04-27 20:06:05 +00003888 // Build a record containing all of the ext_vector declarations.
3889 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003890 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003891
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003892 // Build a record containing all of the VTable uses information.
3893 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003894 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003895 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3896 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3897 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3898 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3899 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003900 }
3901
3902 // Build a record containing all of dynamic classes declarations.
3903 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003904 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003905
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003906 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003907 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003908 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003909 I = SemaRef.PendingInstantiations.begin(),
3910 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3911 AddDeclRef(I->first, PendingInstantiations);
3912 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003913 }
3914 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3915 "There are local ones at end of translation unit!");
3916
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003917 // Build a record containing some declaration references.
3918 RecordData SemaDeclRefs;
3919 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3920 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3921 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3922 }
3923
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003924 RecordData CUDASpecialDeclRefs;
3925 if (Context.getcudaConfigureCallDecl()) {
3926 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3927 }
3928
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003929 // Build a record containing all of the known namespaces.
3930 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003931 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003932 I = SemaRef.KnownNamespaces.begin(),
3933 IEnd = SemaRef.KnownNamespaces.end();
3934 I != IEnd; ++I) {
3935 if (!I->second)
3936 AddDeclRef(I->first, KnownNamespaces);
3937 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003938
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003939 // Build a record of all used, undefined objects that require definitions.
3940 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003941
3942 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003943 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003944 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3945 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003946 AddDeclRef(I->first, UndefinedButUsed);
3947 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003948 }
3949
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003950 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003951 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003952
Sebastian Redl3397c552010-08-18 23:56:27 +00003953 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003954 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003955 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003956
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003957 // This is so that older clang versions, before the introduction
3958 // of the control block, can read and reject the newer PCH format.
3959 Record.clear();
3960 Record.push_back(VERSION_MAJOR);
3961 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3962
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003963 // Create a lexical update block containing all of the declarations in the
3964 // translation unit that do not come from other AST files.
3965 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3966 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3967 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3968 E = TU->noload_decls_end();
3969 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003970 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003971 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003972 }
3973
3974 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3975 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3976 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3977 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3978 Record.clear();
3979 Record.push_back(TU_UPDATE_LEXICAL);
3980 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3981 data(NewGlobalDecls));
3982
3983 // And a visible updates block for the translation unit.
3984 Abv = new llvm::BitCodeAbbrev();
3985 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3986 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3987 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3988 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3989 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3990 WriteDeclContextVisibleUpdate(TU);
3991
3992 // If the translation unit has an anonymous namespace, and we don't already
3993 // have an update block for it, write it as an update block.
3994 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3995 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3996 if (Record.empty()) {
3997 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003998 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003999 }
4000 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004001
4002 // Make sure visible decls, added to DeclContexts previously loaded from
4003 // an AST file, are registered for serialization.
4004 for (SmallVector<const Decl *, 16>::iterator
4005 I = UpdatingVisibleDecls.begin(),
4006 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4007 GetDeclRef(*I);
4008 }
4009
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004010 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004011 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004012
Douglas Gregora119da02011-08-02 16:26:37 +00004013 // Form the record of special types.
4014 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004015 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004016 AddTypeRef(Context.getFILEType(), SpecialTypes);
4017 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4018 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4019 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4020 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004021 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004022 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004023
Douglas Gregor366809a2009-04-26 03:49:13 +00004024 // Keep writing types and declarations until all types and
4025 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004026 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004027 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004028 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4029 E = DeclsToRewrite.end();
4030 I != E; ++I)
4031 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004032 while (!DeclTypesToEmit.empty()) {
4033 DeclOrType DOT = DeclTypesToEmit.front();
4034 DeclTypesToEmit.pop();
4035 if (DOT.isType())
4036 WriteType(DOT.getType());
4037 else
4038 WriteDecl(Context, DOT.getDecl());
4039 }
4040 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004041
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004042 DoneWritingDeclsAndTypes = true;
4043
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004044 WriteFileDeclIDsMap();
4045 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004046 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004047
4048 if (Chain) {
4049 // Write the mapping information describing our module dependencies and how
4050 // each of those modules were mapped into our own offset/ID space, so that
4051 // the reader can build the appropriate mapping to its own offset/ID space.
4052 // The map consists solely of a blob with the following format:
4053 // *(module-name-len:i16 module-name:len*i8
4054 // source-location-offset:i32
4055 // identifier-id:i32
4056 // preprocessed-entity-id:i32
4057 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004058 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004059 // selector-id:i32
4060 // declaration-id:i32
4061 // c++-base-specifiers-id:i32
4062 // type-id:i32)
4063 //
4064 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4065 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4067 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004068 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004069 {
4070 llvm::raw_svector_ostream Out(Buffer);
4071 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004072 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004073 M != MEnd; ++M) {
4074 StringRef FileName = (*M)->FileName;
4075 io::Emit16(Out, FileName.size());
4076 Out.write(FileName.data(), FileName.size());
4077 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4078 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004079 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004080 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004081 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004082 io::Emit32(Out, (*M)->BaseSelectorID);
4083 io::Emit32(Out, (*M)->BaseDeclID);
4084 io::Emit32(Out, (*M)->BaseTypeIndex);
4085 }
4086 }
4087 Record.clear();
4088 Record.push_back(MODULE_OFFSET_MAP);
4089 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4090 Buffer.data(), Buffer.size());
4091 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004092 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004093 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004094 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004095 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004096 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004097 WriteFPPragmaOptions(SemaRef.getFPOptions());
4098 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004099
Sebastian Redl1476ed42010-07-16 16:36:56 +00004100 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004101 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004102
Anders Carlssonc8505782011-03-06 18:41:18 +00004103 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004104
Douglas Gregore209e502011-12-06 01:10:29 +00004105 // If we're emitting a module, write out the submodule information.
4106 if (WritingModule)
4107 WriteSubmodules(WritingModule);
4108
Douglas Gregora119da02011-08-02 16:26:37 +00004109 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4110
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004111 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004112 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004113 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004114
4115 // Write the record containing tentative definitions.
4116 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004117 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004118
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004119 // Write the record containing unused file scoped decls.
4120 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004121 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004122
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004123 // Write the record containing weak undeclared identifiers.
4124 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004125 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004126 WeakUndeclaredIdentifiers);
4127
Richard Smith5ea6ef42013-01-10 23:43:47 +00004128 // Write the record containing locally-scoped extern "C" definitions.
4129 if (!LocallyScopedExternCDecls.empty())
4130 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4131 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004132
4133 // Write the record containing ext_vector type names.
4134 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004135 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004136
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004137 // Write the record containing VTable uses information.
4138 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004139 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004140
4141 // Write the record containing dynamic classes declarations.
4142 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004143 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004144
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004145 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004146 if (!PendingInstantiations.empty())
4147 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004148
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004149 // Write the record containing declaration references of Sema.
4150 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004151 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004152
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004153 // Write the record containing CUDA-specific declaration references.
4154 if (!CUDASpecialDeclRefs.empty())
4155 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004156
4157 // Write the delegating constructors.
4158 if (!DelegatingCtorDecls.empty())
4159 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004160
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004161 // Write the known namespaces.
4162 if (!KnownNamespaces.empty())
4163 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004164
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004165 // Write the undefined internal functions and variables, and inline functions.
4166 if (!UndefinedButUsed.empty())
4167 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004168
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004169 // Write the visible updates to DeclContexts.
4170 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4171 I = UpdatedDeclContexts.begin(),
4172 E = UpdatedDeclContexts.end();
4173 I != E; ++I)
4174 WriteDeclContextVisibleUpdate(*I);
4175
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004176 if (!WritingModule) {
4177 // Write the submodules that were imported, if any.
4178 RecordData ImportedModules;
4179 for (ASTContext::import_iterator I = Context.local_import_begin(),
4180 IEnd = Context.local_import_end();
4181 I != IEnd; ++I) {
4182 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4183 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4184 }
4185 if (!ImportedModules.empty()) {
4186 // Sort module IDs.
4187 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4188
4189 // Unique module IDs.
4190 ImportedModules.erase(std::unique(ImportedModules.begin(),
4191 ImportedModules.end()),
4192 ImportedModules.end());
4193
4194 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4195 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004196 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004197
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004198 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004199 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004200 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004201 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004202 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004203
Douglas Gregor3e1af842009-04-17 22:13:46 +00004204 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004205 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004206 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004207 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004208 Record.push_back(NumLexicalDeclContexts);
4209 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004210 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004211 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004212}
4213
Douglas Gregor61c5e342011-09-17 00:05:03 +00004214/// \brief Go through the declaration update blocks and resolve declaration
4215/// pointers into declaration IDs.
4216void ASTWriter::ResolveDeclUpdatesBlocks() {
4217 for (DeclUpdateMap::iterator
4218 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4219 const Decl *D = I->first;
4220 UpdateRecord &URec = I->second;
4221
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004222 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004223 continue; // The decl will be written completely
4224
4225 unsigned Idx = 0, N = URec.size();
4226 while (Idx < N) {
4227 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004228 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4229 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4230 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4231 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4232 ++Idx;
4233 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004234
Douglas Gregor61c5e342011-09-17 00:05:03 +00004235 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4236 ++Idx;
4237 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004238
4239 case UPD_CXX_DEDUCED_RETURN_TYPE:
4240 URec[Idx] = GetOrCreateTypeID(
4241 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4242 ++Idx;
4243 break;
Douglas Gregor61c5e342011-09-17 00:05:03 +00004244 }
4245 }
4246 }
4247}
4248
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004249void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004250 if (DeclUpdates.empty())
4251 return;
4252
4253 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004254 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004255 for (DeclUpdateMap::iterator
4256 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4257 const Decl *D = I->first;
4258 UpdateRecord &URec = I->second;
4259
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004260 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004261 continue; // The decl will be written completely,no need to store updates.
4262
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004263 uint64_t Offset = Stream.GetCurrentBitNo();
4264 Stream.EmitRecord(DECL_UPDATES, URec);
4265
4266 OffsetsRecord.push_back(GetDeclRef(D));
4267 OffsetsRecord.push_back(Offset);
4268 }
4269 Stream.ExitBlock();
4270 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4271}
4272
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004273void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004274 if (ReplacedDecls.empty())
4275 return;
4276
4277 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004278 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004279 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004280 Record.push_back(I->ID);
4281 Record.push_back(I->Offset);
4282 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004283 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004284 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004285}
4286
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004287void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004288 Record.push_back(Loc.getRawEncoding());
4289}
4290
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004291void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004292 AddSourceLocation(Range.getBegin(), Record);
4293 AddSourceLocation(Range.getEnd(), Record);
4294}
4295
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004296void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004297 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004298 const uint64_t *Words = Value.getRawData();
4299 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004300}
4301
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004302void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004303 Record.push_back(Value.isUnsigned());
4304 AddAPInt(Value, Record);
4305}
4306
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004307void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004308 AddAPInt(Value.bitcastToAPInt(), Record);
4309}
4310
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004311void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004312 Record.push_back(getIdentifierRef(II));
4313}
4314
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004315IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004316 if (II == 0)
4317 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004318
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004319 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004320 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004321 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004322 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004323}
4324
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004325MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004326 // Don't emit builtin macros like __LINE__ to the AST file unless they
4327 // have been redefined by the header (in which case they are not
4328 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004329 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004330 return 0;
4331
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004332 MacroID &ID = MacroIDs[MI];
4333 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004334 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004335 MacroInfoToEmitData Info = { Name, MI, ID };
4336 MacroInfosToEmit.push_back(Info);
4337 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004338 return ID;
4339}
4340
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004341MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4342 if (MI == 0 || MI->isBuiltinMacro())
4343 return 0;
4344
4345 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4346 return MacroIDs[MI];
4347}
4348
4349uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4350 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4351 return IdentMacroDirectivesOffsetMap[Name];
4352}
4353
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004354void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004355 Record.push_back(getSelectorRef(SelRef));
4356}
4357
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004358SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004359 if (Sel.getAsOpaquePtr() == 0) {
4360 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004361 }
4362
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004363 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004364 if (SID == 0 && Chain) {
4365 // This might trigger a ReadSelector callback, which will set the ID for
4366 // this selector.
4367 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004368 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004369 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004370 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004371 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004372 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004373 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004374 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004375}
4376
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004377void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004378 AddDeclRef(Temp->getDestructor(), Record);
4379}
4380
Douglas Gregor7c789c12010-10-29 22:39:52 +00004381void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4382 CXXBaseSpecifier const *BasesEnd,
4383 RecordDataImpl &Record) {
4384 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4385 CXXBaseSpecifiersToWrite.push_back(
4386 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4387 Bases, BasesEnd));
4388 Record.push_back(NextCXXBaseSpecifiersID++);
4389}
4390
Sebastian Redla4232eb2010-08-18 23:56:21 +00004391void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004392 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004393 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004394 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004395 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004396 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004397 break;
4398 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004399 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004400 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004401 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004402 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004403 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004404 break;
4405 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004406 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004407 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004408 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004409 break;
John McCall833ca992009-10-29 08:12:44 +00004410 case TemplateArgument::Null:
4411 case TemplateArgument::Integral:
4412 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004413 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004414 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004415 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004416 break;
4417 }
4418}
4419
Sebastian Redla4232eb2010-08-18 23:56:21 +00004420void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004421 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004422 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004423
4424 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4425 bool InfoHasSameExpr
4426 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4427 Record.push_back(InfoHasSameExpr);
4428 if (InfoHasSameExpr)
4429 return; // Avoid storing the same expr twice.
4430 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004431 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4432 Record);
4433}
4434
Douglas Gregordc355712011-02-25 00:36:19 +00004435void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4436 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004437 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004438 AddTypeRef(QualType(), Record);
4439 return;
4440 }
4441
Douglas Gregordc355712011-02-25 00:36:19 +00004442 AddTypeLoc(TInfo->getTypeLoc(), Record);
4443}
4444
4445void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4446 AddTypeRef(TL.getType(), Record);
4447
John McCalla1ee0c52009-10-16 21:56:05 +00004448 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004449 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004450 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004451}
4452
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004453void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004454 Record.push_back(GetOrCreateTypeID(T));
4455}
4456
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004457TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004458 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004459 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004460 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4461}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004462
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004463TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004464 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004465 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004466 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004467}
4468
4469TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4470 if (T.isNull())
4471 return TypeIdx();
4472 assert(!T.getLocalFastQualifiers());
4473
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004474 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004475 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004476 if (DoneWritingDeclsAndTypes) {
4477 assert(0 && "New type seen after serializing all the types to emit!");
4478 return TypeIdx();
4479 }
4480
Douglas Gregor366809a2009-04-26 03:49:13 +00004481 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004482 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004483 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004484 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004485 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004486 return Idx;
4487}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004488
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004489TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004490 if (T.isNull())
4491 return TypeIdx();
4492 assert(!T.getLocalFastQualifiers());
4493
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004494 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4495 assert(I != TypeIdxs.end() && "Type not emitted!");
4496 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004497}
4498
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004499void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004500 Record.push_back(GetDeclRef(D));
4501}
4502
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004503DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004504 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4505
Douglas Gregor2cf26342009-04-09 22:27:44 +00004506 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004507 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004508 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004509
4510 // If D comes from an AST file, its declaration ID is already known and
4511 // fixed.
4512 if (D->isFromASTFile())
4513 return D->getGlobalID();
4514
Douglas Gregor97475832010-10-05 18:37:06 +00004515 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004516 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004517 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004518 if (DoneWritingDeclsAndTypes) {
4519 assert(0 && "New decl seen after serializing all the decls to emit!");
4520 return 0;
4521 }
4522
Douglas Gregor2cf26342009-04-09 22:27:44 +00004523 // We haven't seen this declaration before. Give it a new ID and
4524 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004525 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004526 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004527 }
4528
Sebastian Redl681d7232010-07-27 00:17:23 +00004529 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004530}
4531
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004532DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004533 if (D == 0)
4534 return 0;
4535
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004536 // If D comes from an AST file, its declaration ID is already known and
4537 // fixed.
4538 if (D->isFromASTFile())
4539 return D->getGlobalID();
4540
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004541 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4542 return DeclIDs[D];
4543}
4544
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004545static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4546 std::pair<unsigned, serialization::DeclID> R) {
4547 return L.first < R.first;
4548}
4549
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004550void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004551 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004552 assert(D);
4553
4554 SourceLocation Loc = D->getLocation();
4555 if (Loc.isInvalid())
4556 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004557
4558 // We only keep track of the file-level declarations of each file.
4559 if (!D->getLexicalDeclContext()->isFileContext())
4560 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004561 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4562 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004563 if (isa<ParmVarDecl>(D))
4564 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004565
4566 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004567 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004568 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004569 FileID FID;
4570 unsigned Offset;
4571 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004572 if (FID.isInvalid())
4573 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004574 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004575
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004576 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004577 if (!Info)
4578 Info = new DeclIDInFileInfo();
4579
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004580 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004581 LocDeclIDsTy &Decls = Info->DeclIDs;
4582
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004583 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004584 Decls.push_back(LocDecl);
4585 return;
4586 }
4587
4588 LocDeclIDsTy::iterator
4589 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4590
4591 Decls.insert(I, LocDecl);
4592}
4593
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004594void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004595 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004596 Record.push_back(Name.getNameKind());
4597 switch (Name.getNameKind()) {
4598 case DeclarationName::Identifier:
4599 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4600 break;
4601
4602 case DeclarationName::ObjCZeroArgSelector:
4603 case DeclarationName::ObjCOneArgSelector:
4604 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004605 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004606 break;
4607
4608 case DeclarationName::CXXConstructorName:
4609 case DeclarationName::CXXDestructorName:
4610 case DeclarationName::CXXConversionFunctionName:
4611 AddTypeRef(Name.getCXXNameType(), Record);
4612 break;
4613
4614 case DeclarationName::CXXOperatorName:
4615 Record.push_back(Name.getCXXOverloadedOperator());
4616 break;
4617
Sean Hunt3e518bd2009-11-29 07:34:05 +00004618 case DeclarationName::CXXLiteralOperatorName:
4619 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4620 break;
4621
Douglas Gregor2cf26342009-04-09 22:27:44 +00004622 case DeclarationName::CXXUsingDirective:
4623 // No extra data to emit
4624 break;
4625 }
4626}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004627
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004628void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004629 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004630 switch (Name.getNameKind()) {
4631 case DeclarationName::CXXConstructorName:
4632 case DeclarationName::CXXDestructorName:
4633 case DeclarationName::CXXConversionFunctionName:
4634 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4635 break;
4636
4637 case DeclarationName::CXXOperatorName:
4638 AddSourceLocation(
4639 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4640 Record);
4641 AddSourceLocation(
4642 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4643 Record);
4644 break;
4645
4646 case DeclarationName::CXXLiteralOperatorName:
4647 AddSourceLocation(
4648 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4649 Record);
4650 break;
4651
4652 case DeclarationName::Identifier:
4653 case DeclarationName::ObjCZeroArgSelector:
4654 case DeclarationName::ObjCOneArgSelector:
4655 case DeclarationName::ObjCMultiArgSelector:
4656 case DeclarationName::CXXUsingDirective:
4657 break;
4658 }
4659}
4660
4661void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004662 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004663 AddDeclarationName(NameInfo.getName(), Record);
4664 AddSourceLocation(NameInfo.getLoc(), Record);
4665 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4666}
4667
4668void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004669 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004670 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004671 Record.push_back(Info.NumTemplParamLists);
4672 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4673 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4674}
4675
Sebastian Redla4232eb2010-08-18 23:56:21 +00004676void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004677 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004678 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004679 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004680 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004681
4682 // Push each of the NNS's onto a stack for serialization in reverse order.
4683 while (NNS) {
4684 NestedNames.push_back(NNS);
4685 NNS = NNS->getPrefix();
4686 }
4687
4688 Record.push_back(NestedNames.size());
4689 while(!NestedNames.empty()) {
4690 NNS = NestedNames.pop_back_val();
4691 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4692 Record.push_back(Kind);
4693 switch (Kind) {
4694 case NestedNameSpecifier::Identifier:
4695 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4696 break;
4697
4698 case NestedNameSpecifier::Namespace:
4699 AddDeclRef(NNS->getAsNamespace(), Record);
4700 break;
4701
Douglas Gregor14aba762011-02-24 02:36:08 +00004702 case NestedNameSpecifier::NamespaceAlias:
4703 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4704 break;
4705
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004706 case NestedNameSpecifier::TypeSpec:
4707 case NestedNameSpecifier::TypeSpecWithTemplate:
4708 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4709 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4710 break;
4711
4712 case NestedNameSpecifier::Global:
4713 // Don't need to write an associated value.
4714 break;
4715 }
4716 }
4717}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004718
Douglas Gregordc355712011-02-25 00:36:19 +00004719void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4720 RecordDataImpl &Record) {
4721 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004722 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004723 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004724
4725 // Push each of the nested-name-specifiers's onto a stack for
4726 // serialization in reverse order.
4727 while (NNS) {
4728 NestedNames.push_back(NNS);
4729 NNS = NNS.getPrefix();
4730 }
4731
4732 Record.push_back(NestedNames.size());
4733 while(!NestedNames.empty()) {
4734 NNS = NestedNames.pop_back_val();
4735 NestedNameSpecifier::SpecifierKind Kind
4736 = NNS.getNestedNameSpecifier()->getKind();
4737 Record.push_back(Kind);
4738 switch (Kind) {
4739 case NestedNameSpecifier::Identifier:
4740 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4741 AddSourceRange(NNS.getLocalSourceRange(), Record);
4742 break;
4743
4744 case NestedNameSpecifier::Namespace:
4745 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4746 AddSourceRange(NNS.getLocalSourceRange(), Record);
4747 break;
4748
4749 case NestedNameSpecifier::NamespaceAlias:
4750 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4751 AddSourceRange(NNS.getLocalSourceRange(), Record);
4752 break;
4753
4754 case NestedNameSpecifier::TypeSpec:
4755 case NestedNameSpecifier::TypeSpecWithTemplate:
4756 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4757 AddTypeLoc(NNS.getTypeLoc(), Record);
4758 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4759 break;
4760
4761 case NestedNameSpecifier::Global:
4762 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4763 break;
4764 }
4765 }
4766}
4767
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004768void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004769 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004770 Record.push_back(Kind);
4771 switch (Kind) {
4772 case TemplateName::Template:
4773 AddDeclRef(Name.getAsTemplateDecl(), Record);
4774 break;
4775
4776 case TemplateName::OverloadedTemplate: {
4777 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4778 Record.push_back(OvT->size());
4779 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4780 I != E; ++I)
4781 AddDeclRef(*I, Record);
4782 break;
4783 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004784
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004785 case TemplateName::QualifiedTemplate: {
4786 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4787 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4788 Record.push_back(QualT->hasTemplateKeyword());
4789 AddDeclRef(QualT->getTemplateDecl(), Record);
4790 break;
4791 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004792
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004793 case TemplateName::DependentTemplate: {
4794 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4795 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4796 Record.push_back(DepT->isIdentifier());
4797 if (DepT->isIdentifier())
4798 AddIdentifierRef(DepT->getIdentifier(), Record);
4799 else
4800 Record.push_back(DepT->getOperator());
4801 break;
4802 }
John McCall14606042011-06-30 08:33:18 +00004803
4804 case TemplateName::SubstTemplateTemplateParm: {
4805 SubstTemplateTemplateParmStorage *subst
4806 = Name.getAsSubstTemplateTemplateParm();
4807 AddDeclRef(subst->getParameter(), Record);
4808 AddTemplateName(subst->getReplacement(), Record);
4809 break;
4810 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004811
4812 case TemplateName::SubstTemplateTemplateParmPack: {
4813 SubstTemplateTemplateParmPackStorage *SubstPack
4814 = Name.getAsSubstTemplateTemplateParmPack();
4815 AddDeclRef(SubstPack->getParameterPack(), Record);
4816 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4817 break;
4818 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004819 }
4820}
4821
Michael J. Spencer20249a12010-10-21 03:16:25 +00004822void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004823 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004824 Record.push_back(Arg.getKind());
4825 switch (Arg.getKind()) {
4826 case TemplateArgument::Null:
4827 break;
4828 case TemplateArgument::Type:
4829 AddTypeRef(Arg.getAsType(), Record);
4830 break;
4831 case TemplateArgument::Declaration:
4832 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004833 Record.push_back(Arg.isDeclForReferenceParam());
4834 break;
4835 case TemplateArgument::NullPtr:
4836 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004837 break;
4838 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004839 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004840 AddTypeRef(Arg.getIntegralType(), Record);
4841 break;
4842 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004843 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4844 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004845 case TemplateArgument::TemplateExpansion:
4846 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004847 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004848 Record.push_back(*NumExpansions + 1);
4849 else
4850 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004851 break;
4852 case TemplateArgument::Expression:
4853 AddStmt(Arg.getAsExpr());
4854 break;
4855 case TemplateArgument::Pack:
4856 Record.push_back(Arg.pack_size());
4857 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4858 I != E; ++I)
4859 AddTemplateArgument(*I, Record);
4860 break;
4861 }
4862}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004863
4864void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004865ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004866 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004867 assert(TemplateParams && "No TemplateParams!");
4868 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4869 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4870 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4871 Record.push_back(TemplateParams->size());
4872 for (TemplateParameterList::const_iterator
4873 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4874 P != PEnd; ++P)
4875 AddDeclRef(*P, Record);
4876}
4877
4878/// \brief Emit a template argument list.
4879void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004880ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004881 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004882 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004883 Record.push_back(TemplateArgs->size());
4884 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004885 AddTemplateArgument(TemplateArgs->get(i), Record);
4886}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004887
4888
4889void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004890ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004891 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004892 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004893 I = Set.begin(), E = Set.end(); I != E; ++I) {
4894 AddDeclRef(I.getDecl(), Record);
4895 Record.push_back(I.getAccess());
4896 }
4897}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004898
Sebastian Redla4232eb2010-08-18 23:56:21 +00004899void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004900 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004901 Record.push_back(Base.isVirtual());
4902 Record.push_back(Base.isBaseOfClass());
4903 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004904 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004905 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004906 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004907 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4908 : SourceLocation(),
4909 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004910}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004911
Douglas Gregor7c789c12010-10-29 22:39:52 +00004912void ASTWriter::FlushCXXBaseSpecifiers() {
4913 RecordData Record;
4914 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4915 Record.clear();
4916
4917 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004918 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004919 if (Index == CXXBaseSpecifiersOffsets.size())
4920 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4921 else {
4922 if (Index > CXXBaseSpecifiersOffsets.size())
4923 CXXBaseSpecifiersOffsets.resize(Index + 1);
4924 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4925 }
4926
4927 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4928 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4929 Record.push_back(BEnd - B);
4930 for (; B != BEnd; ++B)
4931 AddCXXBaseSpecifier(*B, Record);
4932 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004933
4934 // Flush any expressions that were written as part of the base specifiers.
4935 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004936 }
4937
4938 CXXBaseSpecifiersToWrite.clear();
4939}
4940
Sean Huntcbb67482011-01-08 20:30:50 +00004941void ASTWriter::AddCXXCtorInitializers(
4942 const CXXCtorInitializer * const *CtorInitializers,
4943 unsigned NumCtorInitializers,
4944 RecordDataImpl &Record) {
4945 Record.push_back(NumCtorInitializers);
4946 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4947 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004948
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004949 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004950 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004951 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004952 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004953 } else if (Init->isDelegatingInitializer()) {
4954 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004955 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004956 } else if (Init->isMemberInitializer()){
4957 Record.push_back(CTOR_INITIALIZER_MEMBER);
4958 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004959 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004960 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4961 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004962 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004963
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004964 AddSourceLocation(Init->getMemberLocation(), Record);
4965 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004966 AddSourceLocation(Init->getLParenLoc(), Record);
4967 AddSourceLocation(Init->getRParenLoc(), Record);
4968 Record.push_back(Init->isWritten());
4969 if (Init->isWritten()) {
4970 Record.push_back(Init->getSourceOrder());
4971 } else {
4972 Record.push_back(Init->getNumArrayIndices());
4973 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4974 AddDeclRef(Init->getArrayIndex(i), Record);
4975 }
4976 }
4977}
4978
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004979void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4980 assert(D->DefinitionData);
4981 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004982 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004983 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004984 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004985 Record.push_back(Data.Aggregate);
4986 Record.push_back(Data.PlainOldData);
4987 Record.push_back(Data.Empty);
4988 Record.push_back(Data.Polymorphic);
4989 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004990 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004991 Record.push_back(Data.HasNoNonEmptyBases);
4992 Record.push_back(Data.HasPrivateFields);
4993 Record.push_back(Data.HasProtectedFields);
4994 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004995 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004996 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004997 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004998 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004999 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5000 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5001 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5002 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5003 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5004 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005005 Record.push_back(Data.HasTrivialSpecialMembers);
5006 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005007 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005008 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005009 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005010 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005011 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005012 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005013 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005014 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5015 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5016 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5017 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005018 Record.push_back(Data.FailedImplicitMoveConstructor);
5019 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005020 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005021
5022 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005023 if (Data.NumBases > 0)
5024 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5025 Record);
5026
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005027 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5028 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005029 if (Data.NumVBases > 0)
5030 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5031 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005032
5033 AddUnresolvedSet(Data.Conversions, Record);
5034 AddUnresolvedSet(Data.VisibleConversions, Record);
5035 // Data.Definition is the owning decl, no need to write it.
5036 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005037
5038 // Add lambda-specific data.
5039 if (Data.IsLambda) {
5040 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005041 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005042 Record.push_back(Lambda.NumCaptures);
5043 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005044 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005045 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005046 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005047 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5048 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5049 AddSourceLocation(Capture.getLocation(), Record);
5050 Record.push_back(Capture.isImplicit());
Richard Smith0d8e9642013-05-16 06:20:58 +00005051 Record.push_back(Capture.getCaptureKind());
5052 switch (Capture.getCaptureKind()) {
5053 case LCK_This:
5054 break;
5055 case LCK_ByCopy:
5056 case LCK_ByRef: {
5057 VarDecl *Var =
5058 Capture.capturesVariable() ? Capture.getCapturedVar() : 0;
5059 AddDeclRef(Var, Record);
5060 AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5061 : SourceLocation(),
5062 Record);
5063 break;
5064 }
5065 case LCK_Init:
5066 FieldDecl *Field = Capture.getInitCaptureField();
5067 AddDeclRef(Field, Record);
5068 break;
5069 }
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005070 }
5071 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005072}
5073
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005074void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005075 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005076 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005077 assert(FirstDeclID == NextDeclID &&
5078 FirstTypeID == NextTypeID &&
5079 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005080 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005081 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005082 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005083 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005084
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005085 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005086
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005087 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5088 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5089 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005090 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005091 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005092 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005093 NextDeclID = FirstDeclID;
5094 NextTypeID = FirstTypeID;
5095 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005096 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005097 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005098 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005099}
5100
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005101void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005102 // Always keep the highest ID. See \p TypeRead() for more information.
5103 IdentID &StoredID = IdentifierIDs[II];
5104 if (ID > StoredID)
5105 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005106}
5107
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005108void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005109 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005110 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005111 if (ID > StoredID)
5112 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005113}
5114
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005115void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005116 // Always take the highest-numbered type index. This copes with an interesting
5117 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005118 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005119 // keep the higher-numbered entry so that we can properly write it out to
5120 // the AST file.
5121 TypeIdx &StoredIdx = TypeIdxs[T];
5122 if (Idx.getIndex() >= StoredIdx.getIndex())
5123 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005124}
5125
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005126void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005127 // Always keep the highest ID. See \p TypeRead() for more information.
5128 SelectorID &StoredID = SelectorIDs[S];
5129 if (ID > StoredID)
5130 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005131}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005132
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005133void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005134 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005135 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005136 MacroDefinitions[MD] = ID;
5137}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005138
Douglas Gregora015cab2011-12-02 17:30:13 +00005139void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5140 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5141 SubmoduleIDs[Mod] = ID;
5142}
5143
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005144void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005145 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005146 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005147 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5148 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005149 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005150 // A forward reference was mutated into a definition. Rewrite it.
5151 // FIXME: This happens during template instantiation, should we
5152 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005153 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005154 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005155 }
5156}
Douglas Gregora8235d62012-10-09 23:05:51 +00005157
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005158void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005159 assert(!WritingAST && "Already writing the AST!");
5160
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005161 // TU and namespaces are handled elsewhere.
5162 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5163 return;
5164
Douglas Gregor919814d2011-09-09 23:01:35 +00005165 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005166 return; // Not a source decl added to a DeclContext from PCH.
5167
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005168 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005169 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005170 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005171}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005172
5173void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005174 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005175 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005176 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005177 return; // Not a source member added to a class from PCH.
5178 if (!isa<CXXMethodDecl>(D))
5179 return; // We are interested in lazily declared implicit methods.
5180
5181 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005182 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005183 UpdateRecord &Record = DeclUpdates[RD];
5184 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005185 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005186}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005187
5188void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5189 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005190 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005191 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005192 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005193 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005194 return; // Not a source specialization added to a template from PCH.
5195
5196 UpdateRecord &Record = DeclUpdates[TD];
5197 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005198 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005199}
Douglas Gregor89d99802010-11-30 06:16:57 +00005200
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005201void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5202 const FunctionDecl *D) {
5203 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005204 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005205 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005206 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +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));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005212}
5213
Richard Smith9dadfab2013-05-11 05:45:24 +00005214void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5215 assert(!WritingAST && "Already writing the AST!");
5216 FD = FD->getCanonicalDecl();
5217 if (!FD->isFromASTFile())
5218 return; // Not a function declared in PCH and defined outside.
5219
5220 UpdateRecord &Record = DeclUpdates[FD];
5221 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5222 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5223}
5224
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005225void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005226 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005227 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005228 return; // Declaration not imported from PCH.
5229
5230 // Implicit decl from a PCH was defined.
5231 // FIXME: Should implicit definition be a separate FunctionDecl?
5232 RewriteDecl(D);
5233}
5234
Sebastian Redlf79a7192011-04-29 08:19:30 +00005235void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005236 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005237 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005238 return;
5239
5240 // Since the actual instantiation is delayed, this really means that we need
5241 // to update the instantiation location.
5242 UpdateRecord &Record = DeclUpdates[D];
5243 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5244 AddSourceLocation(
5245 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5246}
5247
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005248void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5249 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005250 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005251 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005252 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005253
5254 assert(IFD->getDefinition() && "Category on a class without a definition?");
5255 ObjCClassesWithCategories.insert(
5256 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005257}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005258
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005259
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005260void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5261 const ObjCPropertyDecl *OrigProp,
5262 const ObjCCategoryDecl *ClassExt) {
5263 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5264 if (!D)
5265 return;
5266
5267 assert(!WritingAST && "Already writing the AST!");
5268 if (!D->isFromASTFile())
5269 return; // Declaration not imported from PCH.
5270
5271 RewriteDecl(D);
5272}