blob: dd5818f843df77aa06c5e4c95734160ec67b2c5c [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);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000738 RECORD(EXPR_CXX_BOOL_LITERAL);
739 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000740 RECORD(EXPR_CXX_TYPEID_EXPR);
741 RECORD(EXPR_CXX_TYPEID_TYPE);
742 RECORD(EXPR_CXX_UUIDOF_EXPR);
743 RECORD(EXPR_CXX_UUIDOF_TYPE);
744 RECORD(EXPR_CXX_THIS);
745 RECORD(EXPR_CXX_THROW);
746 RECORD(EXPR_CXX_DEFAULT_ARG);
747 RECORD(EXPR_CXX_BIND_TEMPORARY);
748 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
749 RECORD(EXPR_CXX_NEW);
750 RECORD(EXPR_CXX_DELETE);
751 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
752 RECORD(EXPR_EXPR_WITH_CLEANUPS);
753 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
754 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
755 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
756 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
757 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
758 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
759 RECORD(EXPR_CXX_NOEXCEPT);
760 RECORD(EXPR_OPAQUE_VALUE);
761 RECORD(EXPR_BINARY_TYPE_TRAIT);
762 RECORD(EXPR_PACK_EXPANSION);
763 RECORD(EXPR_SIZEOF_PACK);
764 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000765 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000766#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767}
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Sebastian Redla4232eb2010-08-18 23:56:21 +0000769void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000770 RecordData Record;
771 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000773#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
774#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000776 // Control Block.
777 BLOCK(CONTROL_BLOCK);
778 RECORD(METADATA);
779 RECORD(IMPORTS);
780 RECORD(LANGUAGE_OPTIONS);
781 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000782 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000783 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000784 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000785 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000786 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000787 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000788 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000789 RECORD(PREPROCESSOR_OPTIONS);
790
Douglas Gregorc337fef2012-10-19 00:45:00 +0000791 BLOCK(INPUT_FILES_BLOCK);
792 RECORD(INPUT_FILE);
793
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000794 // AST Top-Level Block.
795 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000796 RECORD(TYPE_OFFSET);
797 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000798 RECORD(IDENTIFIER_OFFSET);
799 RECORD(IDENTIFIER_TABLE);
800 RECORD(EXTERNAL_DEFINITIONS);
801 RECORD(SPECIAL_TYPES);
802 RECORD(STATISTICS);
803 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000804 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000805 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000806 RECORD(SELECTOR_OFFSETS);
807 RECORD(METHOD_POOL);
808 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000809 RECORD(SOURCE_LOCATION_OFFSETS);
810 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000811 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000812 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000813 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000814 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000815 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000816 RECORD(SEMA_DECL_REFS);
817 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
818 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
819 RECORD(DECL_REPLACEMENTS);
820 RECORD(UPDATE_VISIBLE);
821 RECORD(DECL_UPDATE_OFFSETS);
822 RECORD(DECL_UPDATES);
823 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
824 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000825 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000826 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000827 RECORD(FP_PRAGMA_OPTIONS);
828 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000829 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000830 RECORD(KNOWN_NAMESPACES);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000831 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000832 RECORD(MODULE_OFFSET_MAP);
833 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000834 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000835 RECORD(FILE_SORTED_DECLS);
836 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000837 RECORD(MERGED_DECLARATIONS);
838 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000839 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000840 RECORD(MACRO_OFFSET);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +0000841 RECORD(MACRO_TABLE);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000842
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000843 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000844 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000845 RECORD(SM_SLOC_FILE_ENTRY);
846 RECORD(SM_SLOC_BUFFER_ENTRY);
847 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000848 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000850 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000851 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000852 RECORD(PP_MACRO_OBJECT_LIKE);
853 RECORD(PP_MACRO_FUNCTION_LIKE);
854 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000855
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000856 // Decls and Types block.
857 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000858 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000859 RECORD(TYPE_COMPLEX);
860 RECORD(TYPE_POINTER);
861 RECORD(TYPE_BLOCK_POINTER);
862 RECORD(TYPE_LVALUE_REFERENCE);
863 RECORD(TYPE_RVALUE_REFERENCE);
864 RECORD(TYPE_MEMBER_POINTER);
865 RECORD(TYPE_CONSTANT_ARRAY);
866 RECORD(TYPE_INCOMPLETE_ARRAY);
867 RECORD(TYPE_VARIABLE_ARRAY);
868 RECORD(TYPE_VECTOR);
869 RECORD(TYPE_EXT_VECTOR);
870 RECORD(TYPE_FUNCTION_PROTO);
871 RECORD(TYPE_FUNCTION_NO_PROTO);
872 RECORD(TYPE_TYPEDEF);
873 RECORD(TYPE_TYPEOF_EXPR);
874 RECORD(TYPE_TYPEOF);
875 RECORD(TYPE_RECORD);
876 RECORD(TYPE_ENUM);
877 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000878 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000879 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000880 RECORD(TYPE_DECLTYPE);
881 RECORD(TYPE_ELABORATED);
882 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
883 RECORD(TYPE_UNRESOLVED_USING);
884 RECORD(TYPE_INJECTED_CLASS_NAME);
885 RECORD(TYPE_OBJC_OBJECT);
886 RECORD(TYPE_TEMPLATE_TYPE_PARM);
887 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
888 RECORD(TYPE_DEPENDENT_NAME);
889 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
890 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
891 RECORD(TYPE_PAREN);
892 RECORD(TYPE_PACK_EXPANSION);
893 RECORD(TYPE_ATTRIBUTED);
894 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000895 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000896 RECORD(DECL_TYPEDEF);
897 RECORD(DECL_ENUM);
898 RECORD(DECL_RECORD);
899 RECORD(DECL_ENUM_CONSTANT);
900 RECORD(DECL_FUNCTION);
901 RECORD(DECL_OBJC_METHOD);
902 RECORD(DECL_OBJC_INTERFACE);
903 RECORD(DECL_OBJC_PROTOCOL);
904 RECORD(DECL_OBJC_IVAR);
905 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000906 RECORD(DECL_OBJC_CATEGORY);
907 RECORD(DECL_OBJC_CATEGORY_IMPL);
908 RECORD(DECL_OBJC_IMPLEMENTATION);
909 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
910 RECORD(DECL_OBJC_PROPERTY);
911 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000912 RECORD(DECL_FIELD);
John McCall76da55d2013-04-16 07:28:30 +0000913 RECORD(DECL_MS_PROPERTY);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000914 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000915 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000916 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000917 RECORD(DECL_FILE_SCOPE_ASM);
918 RECORD(DECL_BLOCK);
919 RECORD(DECL_CONTEXT_LEXICAL);
920 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000921 RECORD(DECL_NAMESPACE);
922 RECORD(DECL_NAMESPACE_ALIAS);
923 RECORD(DECL_USING);
924 RECORD(DECL_USING_SHADOW);
925 RECORD(DECL_USING_DIRECTIVE);
926 RECORD(DECL_UNRESOLVED_USING_VALUE);
927 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
928 RECORD(DECL_LINKAGE_SPEC);
929 RECORD(DECL_CXX_RECORD);
930 RECORD(DECL_CXX_METHOD);
931 RECORD(DECL_CXX_CONSTRUCTOR);
932 RECORD(DECL_CXX_DESTRUCTOR);
933 RECORD(DECL_CXX_CONVERSION);
934 RECORD(DECL_ACCESS_SPEC);
935 RECORD(DECL_FRIEND);
936 RECORD(DECL_FRIEND_TEMPLATE);
937 RECORD(DECL_CLASS_TEMPLATE);
938 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
939 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
940 RECORD(DECL_FUNCTION_TEMPLATE);
941 RECORD(DECL_TEMPLATE_TYPE_PARM);
942 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
943 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
944 RECORD(DECL_STATIC_ASSERT);
945 RECORD(DECL_CXX_BASE_SPECIFIERS);
946 RECORD(DECL_INDIRECTFIELD);
947 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
948
Douglas Gregora72d8c42011-06-03 02:27:19 +0000949 // Statements and Exprs can occur in the Decls and Types block.
950 AddStmtsExprs(Stream, Record);
951
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000952 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000953 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000954 RECORD(PPD_MACRO_DEFINITION);
955 RECORD(PPD_INCLUSION_DIRECTIVE);
956
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000957#undef RECORD
958#undef BLOCK
959 Stream.ExitBlock();
960}
961
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962/// \brief Adjusts the given filename to only write out the portion of the
963/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000964///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965/// \param Filename the file name to adjust.
966///
967/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
968/// the returned filename will be adjusted by this system root.
969///
970/// \returns either the original filename (if it needs no adjustment) or the
971/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000972static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000973adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor832d6202011-07-22 16:35:34 +0000976 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 // Verify that the filename and the system root have the same prefix.
980 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000981 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000982 if (Filename[Pos] != isysroot[Pos])
983 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 // We hit the end of the filename before we hit the end of the system root.
986 if (!Filename[Pos])
987 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 // If the file name has a '/' at the current position, skip over the '/'.
990 // We distinguish sysroot-based includes from absolute includes by the
991 // absence of '/' at the beginning of sysroot-based includes.
992 if (Filename[Pos] == '/')
993 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Douglas Gregore650c8c2009-07-07 00:12:59 +0000995 return Filename + Pos;
996}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000997
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000998/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000999void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
1000 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001001 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +00001002 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001003 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1004 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001005
Douglas Gregore650c8c2009-07-07 00:12:59 +00001006 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001007 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1011 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1012 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1013 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1014 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1015 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1016 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1017 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001018 Record.push_back(VERSION_MAJOR);
1019 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001020 Record.push_back(CLANG_VERSION_MAJOR);
1021 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001022 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001023 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001024 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1025 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001026
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001027 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001028 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001029 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001030 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001031 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001032
1033 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1034 M != MEnd; ++M) {
1035 // Skip modules that weren't directly imported.
1036 if (!(*M)->isDirectlyImported())
1037 continue;
1038
1039 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001040 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor677e15f2013-03-19 00:28:20 +00001041 Record.push_back((*M)->File->getSize());
1042 Record.push_back((*M)->File->getModificationTime());
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001043 // FIXME: This writes the absolute path for AST files we depend on.
1044 const std::string &FileName = (*M)->FileName;
1045 Record.push_back(FileName.size());
1046 Record.append(FileName.begin(), FileName.end());
1047 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001048 Stream.EmitRecord(IMPORTS, Record);
1049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001051 // Language options.
1052 Record.clear();
1053 const LangOptions &LangOpts = Context.getLangOpts();
1054#define LANGOPT(Name, Bits, Default, Description) \
1055 Record.push_back(LangOpts.Name);
1056#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1057 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1058#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001059#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1060#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001061
1062 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1063 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1064
1065 Record.push_back(LangOpts.CurrentModule.size());
1066 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001067
1068 // Comment options.
1069 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1070 for (CommentOptions::BlockCommandNamesTy::const_iterator
1071 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1072 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1073 I != IEnd; ++I) {
1074 AddString(*I, Record);
1075 }
Dmitri Gribenko6fd7d302013-04-10 15:35:17 +00001076 Record.push_back(LangOpts.CommentOpts.ParseAllComments);
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001077
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001078 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1079
Douglas Gregoree097c12012-10-18 17:58:09 +00001080 // Target options.
1081 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001082 const TargetInfo &Target = Context.getTargetInfo();
1083 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001084 AddString(TargetOpts.Triple, Record);
1085 AddString(TargetOpts.CPU, Record);
1086 AddString(TargetOpts.ABI, Record);
1087 AddString(TargetOpts.CXXABI, Record);
1088 AddString(TargetOpts.LinkerVersion, Record);
1089 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1090 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1091 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1092 }
1093 Record.push_back(TargetOpts.Features.size());
1094 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1095 AddString(TargetOpts.Features[I], Record);
1096 }
1097 Stream.EmitRecord(TARGET_OPTIONS, Record);
1098
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001099 // Diagnostic options.
1100 Record.clear();
1101 const DiagnosticOptions &DiagOpts
1102 = Context.getDiagnostics().getDiagnosticOptions();
1103#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1104#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1105 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1106#include "clang/Basic/DiagnosticOptions.def"
1107 Record.push_back(DiagOpts.Warnings.size());
1108 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1109 AddString(DiagOpts.Warnings[I], Record);
1110 // Note: we don't serialize the log or serialization file names, because they
1111 // are generally transient files and will almost always be overridden.
1112 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1113
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001114 // File system options.
1115 Record.clear();
1116 const FileSystemOptions &FSOpts
1117 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1118 AddString(FSOpts.WorkingDir, Record);
1119 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1120
Douglas Gregorbbf38312012-10-24 16:50:34 +00001121 // Header search options.
1122 Record.clear();
1123 const HeaderSearchOptions &HSOpts
1124 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1125 AddString(HSOpts.Sysroot, Record);
1126
1127 // Include entries.
1128 Record.push_back(HSOpts.UserEntries.size());
1129 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1130 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1131 AddString(Entry.Path, Record);
1132 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001133 Record.push_back(Entry.IsFramework);
1134 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001135 }
1136
1137 // System header prefixes.
1138 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1139 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1140 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1141 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1142 }
1143
1144 AddString(HSOpts.ResourceDir, Record);
1145 AddString(HSOpts.ModuleCachePath, Record);
1146 Record.push_back(HSOpts.DisableModuleHash);
1147 Record.push_back(HSOpts.UseBuiltinIncludes);
1148 Record.push_back(HSOpts.UseStandardSystemIncludes);
1149 Record.push_back(HSOpts.UseStandardCXXIncludes);
1150 Record.push_back(HSOpts.UseLibcxx);
1151 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1152
Douglas Gregora71a7d82012-10-24 20:05:57 +00001153 // Preprocessor options.
1154 Record.clear();
1155 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1156
1157 // Macro definitions.
1158 Record.push_back(PPOpts.Macros.size());
1159 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1160 AddString(PPOpts.Macros[I].first, Record);
1161 Record.push_back(PPOpts.Macros[I].second);
1162 }
1163
1164 // Includes
1165 Record.push_back(PPOpts.Includes.size());
1166 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1167 AddString(PPOpts.Includes[I], Record);
1168
1169 // Macro includes
1170 Record.push_back(PPOpts.MacroIncludes.size());
1171 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1172 AddString(PPOpts.MacroIncludes[I], Record);
1173
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001174 Record.push_back(PPOpts.UsePredefines);
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00001175 // Detailed record is important since it is used for the module cache hash.
1176 Record.push_back(PPOpts.DetailedRecord);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001177 AddString(PPOpts.ImplicitPCHInclude, Record);
1178 AddString(PPOpts.ImplicitPTHInclude, Record);
1179 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1180 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1181
Douglas Gregor31d375f2011-05-06 21:43:30 +00001182 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001183 SourceManager &SM = Context.getSourceManager();
1184 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1185 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001186 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1187 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001188 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1189 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1190
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001191 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001193 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001194
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001195 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001196 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001197 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001198 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001199 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001200 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001201 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001202 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001203
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001204 Record.clear();
1205 Record.push_back(SM.getMainFileID().getOpaqueValue());
1206 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1207
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001208 // Original PCH directory
1209 if (!OutputFile.empty() && OutputFile != "-") {
1210 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1211 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1213 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1214
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001215 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001216
1217 llvm::sys::fs::make_absolute(OutputPath);
1218 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1219
1220 RecordData Record;
1221 Record.push_back(ORIGINAL_PCH_DIR);
1222 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1223 }
1224
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001225 WriteInputFiles(Context.SourceMgr,
1226 PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1227 isysroot);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001228 Stream.ExitBlock();
1229}
1230
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001231namespace {
1232 /// \brief An input file.
1233 struct InputFileEntry {
1234 const FileEntry *File;
1235 bool IsSystemFile;
1236 bool BufferOverridden;
1237 };
1238}
1239
1240void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1241 HeaderSearchOptions &HSOpts,
1242 StringRef isysroot) {
Douglas Gregor745e6f12012-10-19 00:38:02 +00001243 using namespace llvm;
1244 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1245 RecordData Record;
1246
1247 // Create input-file abbreviation.
1248 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1249 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001250 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001251 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1252 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001253 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001254 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1255 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1256
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001257 // Get all ContentCache objects for files, sorted by whether the file is a
1258 // system one or not. System files go at the back, users files at the front.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001259 std::deque<InputFileEntry> SortedFiles;
Douglas Gregor745e6f12012-10-19 00:38:02 +00001260 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1261 // Get this source location entry.
1262 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001263 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001264
1265 // We only care about file entries that were not overridden.
1266 if (!SLoc->isFile())
1267 continue;
1268 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001269 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001270 continue;
1271
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001272 InputFileEntry Entry;
1273 Entry.File = Cache->OrigEntry;
1274 Entry.IsSystemFile = Cache->IsSystemFile;
1275 Entry.BufferOverridden = Cache->BufferOverridden;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001276 if (Cache->IsSystemFile)
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001277 SortedFiles.push_back(Entry);
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001278 else
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001279 SortedFiles.push_front(Entry);
1280 }
1281
1282 // If we have an isysroot for a Darwin SDK, include its SDKSettings.plist in
1283 // the set of (non-system) input files. This is simple heuristic for
1284 // detecting whether the system headers may have changed, because it is too
1285 // expensive to stat() all of the system headers.
1286 FileManager &FileMgr = SourceMgr.getFileManager();
Douglas Gregor2bf383d2013-03-20 16:59:53 +00001287 if (!HSOpts.Sysroot.empty() && !Chain) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001288 llvm::SmallString<128> SDKSettingsFileName(HSOpts.Sysroot);
1289 llvm::sys::path::append(SDKSettingsFileName, "SDKSettings.plist");
1290 if (const FileEntry *SDKSettingsFile = FileMgr.getFile(SDKSettingsFileName)) {
1291 InputFileEntry Entry = { SDKSettingsFile, false, false };
1292 SortedFiles.push_front(Entry);
1293 }
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001294 }
1295
1296 unsigned UserFilesNum = 0;
1297 // Write out all of the input files.
1298 std::vector<uint32_t> InputFileOffsets;
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001299 for (std::deque<InputFileEntry>::iterator
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001300 I = SortedFiles.begin(), E = SortedFiles.end(); I != E; ++I) {
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001301 const InputFileEntry &Entry = *I;
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001302
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001303 uint32_t &InputFileID = InputFileIDs[Entry.File];
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001304 if (InputFileID != 0)
1305 continue; // already recorded this file.
1306
Douglas Gregora930dc92012-10-22 18:42:04 +00001307 // Record this entry's offset.
1308 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001309
1310 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001311
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001312 if (!Entry.IsSystemFile)
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001313 ++UserFilesNum;
1314
Douglas Gregor745e6f12012-10-19 00:38:02 +00001315 Record.clear();
1316 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001317 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001318
1319 // Emit size/modification time for this file.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001320 Record.push_back(Entry.File->getSize());
1321 Record.push_back(Entry.File->getModificationTime());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001322
Douglas Gregora930dc92012-10-22 18:42:04 +00001323 // Whether this file was overridden.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001324 Record.push_back(Entry.BufferOverridden);
Douglas Gregora930dc92012-10-22 18:42:04 +00001325
Douglas Gregor745e6f12012-10-19 00:38:02 +00001326 // Turn the file name into an absolute path, if it isn't already.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001327 const char *Filename = Entry.File->getName();
Douglas Gregor745e6f12012-10-19 00:38:02 +00001328 SmallString<128> FilePath(Filename);
1329
1330 // Ask the file manager to fixup the relative path for us. This will
1331 // honor the working directory.
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001332 FileMgr.FixupRelativePath(FilePath);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001333
1334 // FIXME: This call to make_absolute shouldn't be necessary, the
1335 // call to FixupRelativePath should always return an absolute path.
1336 llvm::sys::fs::make_absolute(FilePath);
1337 Filename = FilePath.c_str();
1338
1339 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1340
1341 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1342 }
Douglas Gregor4a18c3b2013-03-15 22:15:07 +00001343
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001344 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001345
1346 // Create input file offsets abbreviation.
1347 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1348 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1349 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001350 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1351 // input files
Douglas Gregora930dc92012-10-22 18:42:04 +00001352 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1353 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1354
1355 // Write input file offsets.
1356 Record.clear();
1357 Record.push_back(INPUT_FILE_OFFSETS);
1358 Record.push_back(InputFileOffsets.size());
Argyrios Kyrtzidis398253a2013-03-06 18:12:50 +00001359 Record.push_back(UserFilesNum);
Douglas Gregora930dc92012-10-22 18:42:04 +00001360 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001361}
1362
Douglas Gregor14f79002009-04-10 03:52:48 +00001363//===----------------------------------------------------------------------===//
1364// Source Manager Serialization
1365//===----------------------------------------------------------------------===//
1366
1367/// \brief Create an abbreviation for the SLocEntry that refers to a
1368/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001369static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001370 using namespace llvm;
1371 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001372 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001377 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001382 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001383}
1384
1385/// \brief Create an abbreviation for the SLocEntry that refers to a
1386/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001387static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001388 using namespace llvm;
1389 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001390 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1392 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001396 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001397}
1398
1399/// \brief Create an abbreviation for the SLocEntry that refers to a
1400/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001401static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001402 using namespace llvm;
1403 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001404 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001406 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001407}
1408
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001409/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1410/// expansion.
1411static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001412 using namespace llvm;
1413 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001414 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001415 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001420 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001421}
1422
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001423namespace {
1424 // Trait used for the on-disk hash table of header search information.
1425 class HeaderFileInfoTrait {
1426 ASTWriter &Writer;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001427 const HeaderSearch &HS;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001428
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001429 // Keep track of the framework names we've used during serialization.
1430 SmallVector<char, 128> FrameworkStringData;
1431 llvm::StringMap<unsigned> FrameworkNameOffset;
1432
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001433 public:
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001434 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1435 : Writer(Writer), HS(HS) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001436
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001437 struct key_type {
1438 const FileEntry *FE;
1439 const char *Filename;
1440 };
1441 typedef const key_type &key_type_ref;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001442
1443 typedef HeaderFileInfo data_type;
1444 typedef const data_type &data_type_ref;
1445
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001446 static unsigned ComputeHash(key_type_ref key) {
1447 // The hash is based only on size/time of the file, so that the reader can
1448 // match even when symlinking or excess path elements ("foo/../", "../")
1449 // change the form of the name. However, complete path is still the key.
1450 return llvm::hash_combine(key.FE->getSize(),
1451 key.FE->getModificationTime());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001452 }
1453
1454 std::pair<unsigned,unsigned>
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001455 EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1456 unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1457 clang::io::Emit16(Out, KeyLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001458 unsigned DataLen = 1 + 2 + 4 + 4;
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001459 if (Data.isModuleHeader)
1460 DataLen += 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001461 clang::io::Emit8(Out, DataLen);
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001462 return std::make_pair(KeyLen, DataLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001463 }
1464
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001465 void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1466 clang::io::Emit64(Out, key.FE->getSize());
1467 KeyLen -= 8;
1468 clang::io::Emit64(Out, key.FE->getModificationTime());
1469 KeyLen -= 8;
1470 Out.write(key.Filename, KeyLen);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001471 }
1472
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001473 void EmitData(raw_ostream &Out, key_type_ref key,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001474 data_type_ref Data, unsigned DataLen) {
1475 using namespace clang::io;
1476 uint64_t Start = Out.tell(); (void)Start;
1477
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001478 unsigned char Flags = (Data.isImport << 5)
1479 | (Data.isPragmaOnce << 4)
1480 | (Data.DirInfo << 2)
1481 | (Data.Resolved << 1)
1482 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001483 Emit8(Out, (uint8_t)Flags);
1484 Emit16(Out, (uint16_t) Data.NumIncludes);
1485
1486 if (!Data.ControllingMacro)
1487 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1488 else
1489 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001490
1491 unsigned Offset = 0;
1492 if (!Data.Framework.empty()) {
1493 // If this header refers into a framework, save the framework name.
1494 llvm::StringMap<unsigned>::iterator Pos
1495 = FrameworkNameOffset.find(Data.Framework);
1496 if (Pos == FrameworkNameOffset.end()) {
1497 Offset = FrameworkStringData.size() + 1;
1498 FrameworkStringData.append(Data.Framework.begin(),
1499 Data.Framework.end());
1500 FrameworkStringData.push_back(0);
1501
1502 FrameworkNameOffset[Data.Framework] = Offset;
1503 } else
1504 Offset = Pos->second;
1505 }
1506 Emit32(Out, Offset);
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001507
1508 if (Data.isModuleHeader) {
1509 Module *Mod = HS.findModuleForHeader(key.FE);
1510 Emit32(Out, Writer.getExistingSubmoduleID(Mod));
1511 }
1512
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001513 assert(Out.tell() - Start == DataLen && "Wrong data length");
1514 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001515
1516 const char *strings_begin() const { return FrameworkStringData.begin(); }
1517 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001518 };
1519} // end anonymous namespace
1520
1521/// \brief Write the header search block for the list of files that
1522///
1523/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001524void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001525 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001526 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1527
1528 if (FilesByUID.size() > HS.header_file_size())
1529 FilesByUID.resize(HS.header_file_size());
1530
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00001531 HeaderFileInfoTrait GeneratorTrait(*this, HS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001532 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001533 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001534 unsigned NumHeaderSearchEntries = 0;
1535 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1536 const FileEntry *File = FilesByUID[UID];
1537 if (!File)
1538 continue;
1539
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001540 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1541 // from the external source if it was not provided already.
1542 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001543 if (HFI.External && Chain)
1544 continue;
Argyrios Kyrtzidisd3220db2013-05-08 23:46:46 +00001545 if (HFI.isModuleHeader && !HFI.isCompilingModuleHeader)
1546 continue;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001547
1548 // Turn the file name into an absolute path, if it isn't already.
1549 const char *Filename = File->getName();
1550 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1551
1552 // If we performed any translation on the file name at all, we need to
1553 // save this string, since the generator will refer to it later.
1554 if (Filename != File->getName()) {
1555 Filename = strdup(Filename);
1556 SavedStrings.push_back(Filename);
1557 }
1558
Argyrios Kyrtzidised3802e2013-03-06 18:12:47 +00001559 HeaderFileInfoTrait::key_type key = { File, Filename };
1560 Generator.insert(key, HFI, GeneratorTrait);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001561 ++NumHeaderSearchEntries;
1562 }
1563
1564 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001565 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001566 uint32_t BucketOffset;
1567 {
1568 llvm::raw_svector_ostream Out(TableData);
1569 // Make sure that no bucket is at offset 0
1570 clang::io::Emit32(Out, 0);
1571 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1572 }
1573
1574 // Create a blob abbreviation
1575 using namespace llvm;
1576 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1577 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1582 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1583
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001584 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001585 RecordData Record;
1586 Record.push_back(HEADER_SEARCH_TABLE);
1587 Record.push_back(BucketOffset);
1588 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001589 Record.push_back(TableData.size());
1590 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001591 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1592
1593 // Free all of the strings we had to duplicate.
1594 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001595 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001596}
1597
Douglas Gregor14f79002009-04-10 03:52:48 +00001598/// \brief Writes the block containing the serialized form of the
1599/// source manager.
1600///
1601/// TODO: We should probably use an on-disk hash table (stored in a
1602/// blob), indexed based on the file name, so that we only create
1603/// entries for files that we actually need. In the common case (no
1604/// errors), we probably won't have to create file entries for any of
1605/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001606void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001607 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001608 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001609 RecordData Record;
1610
Chris Lattnerf04ad692009-04-10 17:16:57 +00001611 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001612 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001613
1614 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001615 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1616 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1617 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001618 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001619
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001620 // Write out the source location entry table. We skip the first
1621 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001622 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001623 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001624 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1625 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001626 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001627 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001628 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001629 FileID FID = FileID::get(I);
1630 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001631
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001632 // Record the offset of this source-location entry.
1633 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1634
1635 // Figure out which record code to use.
1636 unsigned Code;
1637 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001638 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1639 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001640 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001641 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001642 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001643 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001644 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001645 Record.clear();
1646 Record.push_back(Code);
1647
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001648 // Starting offset of this entry within this module, so skip the dummy.
1649 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001650 if (SLoc->isFile()) {
1651 const SrcMgr::FileInfo &File = SLoc->getFile();
1652 Record.push_back(File.getIncludeLoc().getRawEncoding());
1653 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1654 Record.push_back(File.hasLineDirectives());
1655
1656 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001657 if (Content->OrigEntry) {
1658 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001659 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001660
Douglas Gregora930dc92012-10-22 18:42:04 +00001661 // The source location entry is a file. Emit input file ID.
1662 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1663 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001665 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001666
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001667 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001668 if (FDI != FileDeclIDs.end()) {
1669 Record.push_back(FDI->second->FirstDeclIndex);
1670 Record.push_back(FDI->second->DeclIDs.size());
1671 } else {
1672 Record.push_back(0);
1673 Record.push_back(0);
1674 }
Douglas Gregora081da52011-11-16 20:05:18 +00001675
Douglas Gregora930dc92012-10-22 18:42:04 +00001676 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001677
1678 if (Content->BufferOverridden) {
1679 Record.clear();
1680 Record.push_back(SM_SLOC_BUFFER_BLOB);
1681 const llvm::MemoryBuffer *Buffer
1682 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1683 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1684 StringRef(Buffer->getBufferStart(),
1685 Buffer->getBufferSize() + 1));
1686 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001687 } else {
1688 // The source location entry is a buffer. The blob associated
1689 // with this entry contains the contents of the buffer.
1690
1691 // We add one to the size so that we capture the trailing NULL
1692 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1693 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001694 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001695 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001696 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001697 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001698 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001699 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001700 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001701 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001702 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001703 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001704
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001705 if (strcmp(Name, "<built-in>") == 0) {
1706 PreloadSLocs.push_back(SLocEntryOffsets.size());
1707 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001708 }
1709 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001710 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001711 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001712 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1713 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001714 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1715 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001716
1717 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001718 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001719 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001720 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001721 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001722 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001723 }
1724 }
1725
Douglas Gregorc9490c02009-04-16 22:23:12 +00001726 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001727
1728 if (SLocEntryOffsets.empty())
1729 return;
1730
Sebastian Redl3397c552010-08-18 23:56:27 +00001731 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001732 // table is used for lazily loading source-location information.
1733 using namespace llvm;
1734 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001735 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1739 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001741 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001742 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001743 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001744 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001745 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001746
Sebastian Redl3397c552010-08-18 23:56:27 +00001747 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001748 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001749 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001750
1751 // Write the line table. It depends on remapping working, so it must come
1752 // after the source location offsets.
1753 if (SourceMgr.hasLineTable()) {
1754 LineTableInfo &LineTable = SourceMgr.getLineTable();
1755
1756 Record.clear();
1757 // Emit the file names
1758 Record.push_back(LineTable.getNumFilenames());
1759 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1760 // Emit the file name
1761 const char *Filename = LineTable.getFilename(I);
1762 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1763 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1764 Record.push_back(FilenameLen);
1765 if (FilenameLen)
1766 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1767 }
1768
1769 // Emit the line entries
1770 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1771 L != LEnd; ++L) {
1772 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001773 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001774 continue;
1775
1776 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001777 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001778
1779 // Emit the line entries
1780 Record.push_back(L->second.size());
1781 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1782 LEEnd = L->second.end();
1783 LE != LEEnd; ++LE) {
1784 Record.push_back(LE->FileOffset);
1785 Record.push_back(LE->LineNo);
1786 Record.push_back(LE->FilenameID);
1787 Record.push_back((unsigned)LE->FileKind);
1788 Record.push_back(LE->IncludeOffset);
1789 }
1790 }
1791 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1792 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001793}
1794
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001795//===----------------------------------------------------------------------===//
1796// Preprocessor Serialization
1797//===----------------------------------------------------------------------===//
1798
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001799namespace {
1800class ASTMacroTableTrait {
1801public:
1802 typedef IdentID key_type;
1803 typedef key_type key_type_ref;
1804
1805 struct Data {
1806 uint32_t MacroDirectivesOffset;
1807 };
1808
1809 typedef Data data_type;
1810 typedef const data_type &data_type_ref;
1811
1812 static unsigned ComputeHash(IdentID IdID) {
1813 return llvm::hash_value(IdID);
1814 }
1815
1816 std::pair<unsigned,unsigned>
1817 static EmitKeyDataLength(raw_ostream& Out,
1818 key_type_ref Key, data_type_ref Data) {
1819 unsigned KeyLen = 4; // IdentID.
1820 unsigned DataLen = 4; // MacroDirectivesOffset.
1821 return std::make_pair(KeyLen, DataLen);
1822 }
1823
1824 static void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
1825 clang::io::Emit32(Out, Key);
1826 }
1827
1828 static void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
1829 unsigned) {
1830 clang::io::Emit32(Out, Data.MacroDirectivesOffset);
1831 }
1832};
1833} // end anonymous namespace
1834
1835static int compareMacroDirectives(const void *XPtr, const void *YPtr) {
1836 const std::pair<const IdentifierInfo *, MacroDirective *> &X =
1837 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)XPtr;
1838 const std::pair<const IdentifierInfo *, MacroDirective *> &Y =
1839 *(const std::pair<const IdentifierInfo *, MacroDirective *>*)YPtr;
Douglas Gregor9c736102011-02-10 18:20:09 +00001840 return X.first->getName().compare(Y.first->getName());
1841}
1842
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001843static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
1844 const Preprocessor &PP) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001845 if (MacroInfo *MI = MD->getMacroInfo())
1846 if (MI->isBuiltinMacro())
1847 return true;
Argyrios Kyrtzidis9cc3ed42013-03-15 22:43:10 +00001848
1849 if (IsModule) {
1850 SourceLocation Loc = MD->getLocation();
1851 if (Loc.isInvalid())
1852 return true;
1853 if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
1854 return true;
1855 }
1856
1857 return false;
1858}
1859
Chris Lattner0b1fb982009-04-10 17:15:23 +00001860/// \brief Writes the block containing the serialized form of the
1861/// preprocessor.
1862///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001863void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001864 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1865 if (PPRec)
1866 WritePreprocessorDetail(*PPRec);
1867
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001868 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001869
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001870 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1871 if (PP.getCounterValue() != 0) {
1872 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001873 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001874 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001875 }
1876
1877 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001878 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Sebastian Redl3397c552010-08-18 23:56:27 +00001880 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001881 // FIXME: use diagnostics subsystem for localization etc.
1882 if (PP.SawDateOrTime())
1883 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Douglas Gregorecdcb882010-10-20 22:00:55 +00001885
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001886 // Loop over all the macro directives that are live at the end of the file,
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001887 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001888
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001889 // Construct the list of macro directives that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001890 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001891 MacroDirectives;
1892 for (Preprocessor::macro_iterator
1893 I = PP.macro_begin(/*IncludeExternalMacros=*/false),
1894 E = PP.macro_end(/*IncludeExternalMacros=*/false);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001895 I != E; ++I) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001896 MacroDirectives.push_back(std::make_pair(I->first, I->second));
Douglas Gregor9c736102011-02-10 18:20:09 +00001897 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001898
Douglas Gregor9c736102011-02-10 18:20:09 +00001899 // Sort the set of macro definitions that need to be serialized by the
1900 // name of the macro, to provide a stable ordering.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001901 llvm::array_pod_sort(MacroDirectives.begin(), MacroDirectives.end(),
1902 &compareMacroDirectives);
1903
1904 OnDiskChainedHashTableGenerator<ASTMacroTableTrait> Generator;
1905
1906 // Emit the macro directives as a list and associate the offset with the
1907 // identifier they belong to.
1908 for (unsigned I = 0, N = MacroDirectives.size(); I != N; ++I) {
1909 const IdentifierInfo *Name = MacroDirectives[I].first;
1910 uint64_t MacroDirectiveOffset = Stream.GetCurrentBitNo();
1911 MacroDirective *MD = MacroDirectives[I].second;
1912
1913 // If the macro or identifier need no updates, don't write the macro history
1914 // for this one.
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001915 // FIXME: Chain the macro history instead of re-writing it.
1916 if (MD->isFromPCH() &&
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001917 Name->isFromAST() && !Name->hasChangedSinceDeserialization())
1918 continue;
1919
1920 // Emit the macro directives in reverse source order.
1921 for (; MD; MD = MD->getPrevious()) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001922 if (MD->isHidden())
1923 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001924 if (shouldIgnoreMacro(MD, IsModule, PP))
1925 continue;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001926
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001927 AddSourceLocation(MD->getLocation(), Record);
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00001928 Record.push_back(MD->getKind());
1929 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
1930 MacroID InfoID = getMacroRef(DefMD->getInfo(), Name);
1931 Record.push_back(InfoID);
1932 Record.push_back(DefMD->isImported());
1933 Record.push_back(DefMD->isAmbiguous());
1934
1935 } else if (VisibilityMacroDirective *
1936 VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
1937 Record.push_back(VisMD->isPublic());
1938 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001939 }
1940 if (Record.empty())
1941 continue;
1942
1943 Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
1944 Record.clear();
1945
1946 IdentMacroDirectivesOffsetMap[Name] = MacroDirectiveOffset;
1947
1948 IdentID NameID = getIdentifierRef(Name);
1949 ASTMacroTableTrait::Data data;
1950 data.MacroDirectivesOffset = MacroDirectiveOffset;
1951 Generator.insert(NameID, data);
1952 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001953
Douglas Gregora8235d62012-10-09 23:05:51 +00001954 /// \brief Offsets of each of the macros into the bitstream, indexed by
1955 /// the local macro ID
1956 ///
1957 /// For each identifier that is associated with a macro, this map
1958 /// provides the offset into the bitstream where that macro is
1959 /// defined.
1960 std::vector<uint32_t> MacroOffsets;
1961
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001962 for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
1963 const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
1964 MacroInfo *MI = MacroInfosToEmit[I].MI;
1965 MacroID ID = MacroInfosToEmit[I].ID;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001966
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001967 if (ID < FirstMacroID) {
1968 assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
1969 continue;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001970 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00001971
1972 // Record the local offset of this macro.
1973 unsigned Index = ID - FirstMacroID;
1974 if (Index == MacroOffsets.size())
1975 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1976 else {
1977 if (Index > MacroOffsets.size())
1978 MacroOffsets.resize(Index + 1);
1979
1980 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1981 }
1982
1983 AddIdentifierRef(Name, Record);
1984 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
1985 AddSourceLocation(MI->getDefinitionLoc(), Record);
1986 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
1987 Record.push_back(MI->isUsed());
1988 unsigned Code;
1989 if (MI->isObjectLike()) {
1990 Code = PP_MACRO_OBJECT_LIKE;
1991 } else {
1992 Code = PP_MACRO_FUNCTION_LIKE;
1993
1994 Record.push_back(MI->isC99Varargs());
1995 Record.push_back(MI->isGNUVarargs());
1996 Record.push_back(MI->hasCommaPasting());
1997 Record.push_back(MI->getNumArgs());
1998 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1999 I != E; ++I)
2000 AddIdentifierRef(*I, Record);
2001 }
2002
2003 // If we have a detailed preprocessing record, record the macro definition
2004 // ID that corresponds to this macro.
2005 if (PPRec)
2006 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2007
2008 Stream.EmitRecord(Code, Record);
2009 Record.clear();
2010
2011 // Emit the tokens array.
2012 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2013 // Note that we know that the preprocessor does not have any annotation
2014 // tokens in it because they are created by the parser, and thus can't
2015 // be in a macro definition.
2016 const Token &Tok = MI->getReplacementToken(TokNo);
John McCallaeeacf72013-05-03 00:10:13 +00002017 AddToken(Tok, Record);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002018 Stream.EmitRecord(PP_TOKEN, Record);
2019 Record.clear();
2020 }
2021 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00002022 }
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002023
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002024 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00002025
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002026 // Create the on-disk hash table in a buffer.
2027 SmallString<4096> MacroTable;
2028 uint32_t BucketOffset;
2029 {
2030 llvm::raw_svector_ostream Out(MacroTable);
2031 // Make sure that no bucket is at offset 0
2032 clang::io::Emit32(Out, 0);
2033 BucketOffset = Generator.Emit(Out);
2034 }
2035
2036 // Write the macro table
2037 using namespace llvm;
2038 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2039 Abbrev->Add(BitCodeAbbrevOp(MACRO_TABLE));
2040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2042 unsigned MacroTableAbbrev = Stream.EmitAbbrev(Abbrev);
2043
2044 Record.push_back(MACRO_TABLE);
2045 Record.push_back(BucketOffset);
2046 Stream.EmitRecordWithBlob(MacroTableAbbrev, Record, MacroTable.str());
2047 Record.clear();
2048
Douglas Gregora8235d62012-10-09 23:05:51 +00002049 // Write the offsets table for macro IDs.
2050 using namespace llvm;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002051 Abbrev = new BitCodeAbbrev();
Douglas Gregora8235d62012-10-09 23:05:51 +00002052 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2056
2057 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2058 Record.clear();
2059 Record.push_back(MACRO_OFFSET);
2060 Record.push_back(MacroOffsets.size());
2061 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
2062 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
2063 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002064}
2065
2066void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002067 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002068 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002069
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002070 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002071
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002072 // Enter the preprocessor block.
2073 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002074
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002075 // If the preprocessor has a preprocessing record, emit it.
2076 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002077 using namespace llvm;
2078
2079 // Set up the abbreviation for
2080 unsigned InclusionAbbrev = 0;
2081 {
2082 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2083 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2089 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2090 }
2091
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002092 unsigned FirstPreprocessorEntityID
2093 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2094 + NUM_PREDEF_PP_ENTITY_IDS;
2095 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002096 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00002097 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2098 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00002099 E != EEnd;
2100 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002101 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002102
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00002103 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
2104 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002105
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002106 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002107 // Record this macro definition's ID.
2108 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002109
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002110 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002111 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2112 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002113 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002114
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002115 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002116 Record.push_back(ME->isBuiltinMacro());
2117 if (ME->isBuiltinMacro())
2118 AddIdentifierRef(ME->getName(), Record);
2119 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002120 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002121 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002122 continue;
2123 }
2124
2125 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2126 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002127 Record.push_back(ID->getFileName().size());
2128 Record.push_back(ID->wasInQuotes());
2129 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002130 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002131 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002132 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002133 // Check that the FileEntry is not null because it was not resolved and
2134 // we create a PCH even with compiler errors.
2135 if (ID->getFile())
2136 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002137 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2138 continue;
2139 }
2140
2141 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2142 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002143 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002144
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002145 // Write the offsets table for the preprocessing record.
2146 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002147 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2148
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002149 // Write the offsets table for identifier IDs.
2150 using namespace llvm;
2151 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002152 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002155 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002156
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002157 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002158 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002159 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002160 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2161 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002162 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002163}
2164
Douglas Gregore209e502011-12-06 01:10:29 +00002165unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2166 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2167 if (Known != SubmoduleIDs.end())
2168 return Known->second;
2169
2170 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2171}
2172
Argyrios Kyrtzidis55ea75b2013-03-13 21:13:51 +00002173unsigned ASTWriter::getExistingSubmoduleID(Module *Mod) const {
2174 if (!Mod)
2175 return 0;
2176
2177 llvm::DenseMap<Module *, unsigned>::const_iterator
2178 Known = SubmoduleIDs.find(Mod);
2179 if (Known != SubmoduleIDs.end())
2180 return Known->second;
2181
2182 return 0;
2183}
2184
Douglas Gregor26ced122011-12-01 00:59:36 +00002185/// \brief Compute the number of modules within the given tree (including the
2186/// given module).
2187static unsigned getNumberOfModules(Module *Mod) {
2188 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002189 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2190 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002191 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002192 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002193
2194 return ChildModules + 1;
2195}
2196
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002197void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002198 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002199 // FIXME: This feels like it belongs somewhere else, but there are no
2200 // other consumers of this information.
2201 SourceManager &SrcMgr = PP->getSourceManager();
2202 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2203 for (ASTContext::import_iterator I = Context->local_import_begin(),
2204 IEnd = Context->local_import_end();
2205 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002206 if (Module *ImportedFrom
2207 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2208 SrcMgr))) {
2209 ImportedFrom->Imports.push_back(I->getImportedModule());
2210 }
2211 }
2212
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002213 // Enter the submodule description block.
2214 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2215
2216 // Write the abbreviations needed for the submodules block.
2217 using namespace llvm;
2218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2219 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor63a72682013-03-20 00:22:05 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2230 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2231
2232 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002233 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2235 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2236
2237 Abbrev = new BitCodeAbbrev();
2238 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2240 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002241
2242 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002243 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2245 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2246
2247 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002248 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2250 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2251
Douglas Gregor51f564f2011-12-31 04:05:44 +00002252 Abbrev = new BitCodeAbbrev();
2253 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2255 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2256
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002257 Abbrev = new BitCodeAbbrev();
2258 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2260 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2261
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002262 Abbrev = new BitCodeAbbrev();
2263 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2266 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2267
Douglas Gregor63a72682013-03-20 00:22:05 +00002268 Abbrev = new BitCodeAbbrev();
2269 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Macro name
2271 unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2272
Douglas Gregor906d66a2013-03-20 21:10:35 +00002273 Abbrev = new BitCodeAbbrev();
2274 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Other module
2276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Message
2277 unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2278
Douglas Gregor26ced122011-12-01 00:59:36 +00002279 // Write the submodule metadata block.
2280 RecordData Record;
2281 Record.push_back(getNumberOfModules(WritingModule));
2282 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2283 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2284
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002285 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002286 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002287 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002288 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002289 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002290 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002291 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002292
2293 // Emit the definition of the block.
2294 Record.clear();
2295 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002296 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002297 if (Mod->Parent) {
2298 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2299 Record.push_back(SubmoduleIDs[Mod->Parent]);
2300 } else {
2301 Record.push_back(0);
2302 }
2303 Record.push_back(Mod->IsFramework);
2304 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002305 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002306 Record.push_back(Mod->InferSubmodules);
2307 Record.push_back(Mod->InferExplicitSubmodules);
2308 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor63a72682013-03-20 00:22:05 +00002309 Record.push_back(Mod->ConfigMacrosExhaustive);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002310 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2311
Douglas Gregor51f564f2011-12-31 04:05:44 +00002312 // Emit the requirements.
2313 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2314 Record.clear();
2315 Record.push_back(SUBMODULE_REQUIRES);
2316 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2317 Mod->Requires[I].data(),
2318 Mod->Requires[I].size());
2319 }
2320
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002321 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002322 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002323 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002324 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002325 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002326 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002327 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2328 Record.clear();
2329 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2330 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2331 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002332 }
2333
2334 // Emit the headers.
2335 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2336 Record.clear();
2337 Record.push_back(SUBMODULE_HEADER);
2338 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2339 Mod->Headers[I]->getName());
2340 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002341 // Emit the excluded headers.
2342 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2343 Record.clear();
2344 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2345 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2346 Mod->ExcludedHeaders[I]->getName());
2347 }
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002348 ArrayRef<const FileEntry *>
2349 TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2350 for (unsigned I = 0, N = TopHeaders.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002351 Record.clear();
2352 Record.push_back(SUBMODULE_TOPHEADER);
2353 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002354 TopHeaders[I]->getName());
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002355 }
Douglas Gregor55988682011-12-05 16:33:54 +00002356
2357 // Emit the imports.
2358 if (!Mod->Imports.empty()) {
2359 Record.clear();
2360 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002361 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002362 assert(ImportedID && "Unknown submodule!");
2363 Record.push_back(ImportedID);
2364 }
2365 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2366 }
2367
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002368 // Emit the exports.
2369 if (!Mod->Exports.empty()) {
2370 Record.clear();
2371 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002372 if (Module *Exported = Mod->Exports[I].getPointer()) {
2373 unsigned ExportedID = SubmoduleIDs[Exported];
2374 assert(ExportedID > 0 && "Unknown submodule ID?");
2375 Record.push_back(ExportedID);
2376 } else {
2377 Record.push_back(0);
2378 }
2379
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002380 Record.push_back(Mod->Exports[I].getInt());
2381 }
2382 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2383 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002384
2385 // Emit the link libraries.
2386 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2387 Record.clear();
2388 Record.push_back(SUBMODULE_LINK_LIBRARY);
2389 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2390 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2391 Mod->LinkLibraries[I].Library);
2392 }
2393
Douglas Gregor906d66a2013-03-20 21:10:35 +00002394 // Emit the conflicts.
2395 for (unsigned I = 0, N = Mod->Conflicts.size(); I != N; ++I) {
2396 Record.clear();
2397 Record.push_back(SUBMODULE_CONFLICT);
2398 unsigned OtherID = getSubmoduleID(Mod->Conflicts[I].Other);
2399 assert(OtherID && "Unknown submodule!");
2400 Record.push_back(OtherID);
2401 Stream.EmitRecordWithBlob(ConflictAbbrev, Record,
2402 Mod->Conflicts[I].Message);
2403 }
2404
Douglas Gregor63a72682013-03-20 00:22:05 +00002405 // Emit the configuration macros.
2406 for (unsigned I = 0, N = Mod->ConfigMacros.size(); I != N; ++I) {
2407 Record.clear();
2408 Record.push_back(SUBMODULE_CONFIG_MACRO);
2409 Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record,
2410 Mod->ConfigMacros[I]);
2411 }
2412
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002413 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002414 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2415 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002416 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002417 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002418 }
2419
2420 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002421
2422 assert((NextSubmoduleID - FirstSubmoduleID
2423 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002424}
2425
Douglas Gregor185dbd72011-12-01 02:07:58 +00002426serialization::SubmoduleID
2427ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002428 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002429 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002430
2431 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002432 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002433 Module *OwningMod
2434 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002435 if (!OwningMod)
2436 return 0;
2437
Douglas Gregore209e502011-12-06 01:10:29 +00002438 // Check whether this submodule is part of our own module.
2439 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002440 return 0;
2441
Douglas Gregore209e502011-12-06 01:10:29 +00002442 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002443}
2444
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00002445void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2446 bool isModule) {
2447 // Make sure set diagnostic pragmas don't affect the translation unit that
2448 // imports the module.
2449 // FIXME: Make diagnostic pragma sections work properly with modules.
2450 if (isModule)
2451 return;
2452
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002453 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2454 DiagStateIDMap;
2455 unsigned CurrID = 0;
2456 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002457 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002458 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002459 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2460 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002461 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002462 if (point.Loc.isInvalid())
2463 continue;
2464
2465 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002466 unsigned &DiagStateID = DiagStateIDMap[point.State];
2467 Record.push_back(DiagStateID);
2468
2469 if (DiagStateID == 0) {
2470 DiagStateID = ++CurrID;
2471 for (DiagnosticsEngine::DiagState::const_iterator
2472 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2473 if (I->second.isPragma()) {
2474 Record.push_back(I->first);
2475 Record.push_back(I->second.getMapping());
2476 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002477 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002478 Record.push_back(-1); // mark the end of the diag/map pairs for this
2479 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002480 }
2481 }
2482
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002483 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002484 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002485}
2486
Anders Carlssonc8505782011-03-06 18:41:18 +00002487void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2488 if (CXXBaseSpecifiersOffsets.empty())
2489 return;
2490
2491 RecordData Record;
2492
2493 // Create a blob abbreviation for the C++ base specifiers offsets.
2494 using namespace llvm;
2495
2496 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2497 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2500 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2501
Douglas Gregore92b8a12011-08-04 00:01:48 +00002502 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002503 Record.clear();
2504 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2505 Record.push_back(CXXBaseSpecifiersOffsets.size());
2506 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002507 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002508}
2509
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002510//===----------------------------------------------------------------------===//
2511// Type Serialization
2512//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002513
Sebastian Redl3397c552010-08-18 23:56:27 +00002514/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002515void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002516 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002517 if (Idx.getIndex() == 0) // we haven't seen this type before.
2518 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002519
Douglas Gregor97475832010-10-05 18:37:06 +00002520 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002521
Douglas Gregor2cf26342009-04-09 22:27:44 +00002522 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002523 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002524 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002525 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002526 else if (TypeOffsets.size() < Index) {
2527 TypeOffsets.resize(Index + 1);
2528 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002529 }
2530
2531 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002532
Douglas Gregor2cf26342009-04-09 22:27:44 +00002533 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002534 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002535
Douglas Gregora4923eb2009-11-16 21:35:15 +00002536 if (T.hasLocalNonFastQualifiers()) {
2537 Qualifiers Qs = T.getLocalQualifiers();
2538 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002539 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002540 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002541 } else {
2542 switch (T->getTypeClass()) {
2543 // For all of the concrete, non-dependent types, call the
2544 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002545#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002546 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002547#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002548#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002549 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002550 }
2551
2552 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002553 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002554
2555 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002556 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002557}
2558
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002559//===----------------------------------------------------------------------===//
2560// Declaration Serialization
2561//===----------------------------------------------------------------------===//
2562
Douglas Gregor2cf26342009-04-09 22:27:44 +00002563/// \brief Write the block containing all of the declaration IDs
2564/// lexically declared within the given DeclContext.
2565///
2566/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2567/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002568uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002569 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002570 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002571 return 0;
2572
Douglas Gregorc9490c02009-04-16 22:23:12 +00002573 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002574 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002575 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002576 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002577 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2578 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002579 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002580
Douglas Gregor25123082009-04-22 22:34:57 +00002581 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002582 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002583 return Offset;
2584}
2585
Sebastian Redla4232eb2010-08-18 23:56:21 +00002586void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002587 using namespace llvm;
2588 RecordData Record;
2589
2590 // Write the type offsets array
2591 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002592 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002595 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2596 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2597 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002598 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002599 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002600 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002601 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002602
2603 // Write the declaration offsets array
2604 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002605 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002608 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2609 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2610 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002612 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002613 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002614 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002615}
2616
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002617void ASTWriter::WriteFileDeclIDsMap() {
2618 using namespace llvm;
2619 RecordData Record;
2620
2621 // Join the vectors of DeclIDs from all files.
2622 SmallVector<DeclID, 256> FileSortedIDs;
2623 for (FileDeclIDsTy::iterator
2624 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2625 DeclIDInFileInfo &Info = *FI->second;
2626 Info.FirstDeclIndex = FileSortedIDs.size();
2627 for (LocDeclIDsTy::iterator
2628 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2629 FileSortedIDs.push_back(DI->second);
2630 }
2631
2632 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2633 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002634 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002635 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2636 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2637 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002638 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002639 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2640}
2641
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002642void ASTWriter::WriteComments() {
2643 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002644 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002645 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002646 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2647 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002648 I != E; ++I) {
2649 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002650 AddSourceRange((*I)->getSourceRange(), Record);
2651 Record.push_back((*I)->getKind());
2652 Record.push_back((*I)->isTrailingComment());
2653 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002654 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2655 }
2656 Stream.ExitBlock();
2657}
2658
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002659//===----------------------------------------------------------------------===//
2660// Global Method Pool and Selector Serialization
2661//===----------------------------------------------------------------------===//
2662
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002663namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002664// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002665class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002666 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002667
2668public:
2669 typedef Selector key_type;
2670 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002671
Sebastian Redl5d050072010-08-04 17:20:04 +00002672 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002673 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002674 ObjCMethodList Instance, Factory;
2675 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002676 typedef const data_type& data_type_ref;
2677
Sebastian Redl3397c552010-08-18 23:56:27 +00002678 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002680 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002681 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002682 }
Mike Stump1eb44332009-09-09 15:08:12 +00002683
2684 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002685 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002686 data_type_ref Methods) {
2687 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2688 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002689 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2690 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002691 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002692 if (Method->Method)
2693 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002694 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002695 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002696 if (Method->Method)
2697 DataLen += 4;
2698 clang::io::Emit16(Out, DataLen);
2699 return std::make_pair(KeyLen, DataLen);
2700 }
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Chris Lattner5f9e2722011-07-23 10:55:15 +00002702 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002703 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002704 assert((Start >> 32) == 0 && "Selector key offset too large");
2705 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002706 unsigned N = Sel.getNumArgs();
2707 clang::io::Emit16(Out, N);
2708 if (N == 0)
2709 N = 1;
2710 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002711 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002712 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2713 }
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Chris Lattner5f9e2722011-07-23 10:55:15 +00002715 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002716 data_type_ref Methods, unsigned DataLen) {
2717 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002718 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002719 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002720 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002721 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002722 if (Method->Method)
2723 ++NumInstanceMethods;
2724
2725 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002726 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002727 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002728 if (Method->Method)
2729 ++NumFactoryMethods;
2730
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002731 unsigned InstanceBits = Methods.Instance.getBits();
2732 assert(InstanceBits < 4);
2733 unsigned NumInstanceMethodsAndBits =
2734 (NumInstanceMethods << 2) | InstanceBits;
2735 unsigned FactoryBits = Methods.Factory.getBits();
2736 assert(FactoryBits < 4);
2737 unsigned NumFactoryMethodsAndBits = (NumFactoryMethods << 2) | FactoryBits;
2738 clang::io::Emit16(Out, NumInstanceMethodsAndBits);
2739 clang::io::Emit16(Out, NumFactoryMethodsAndBits);
Sebastian Redl5d050072010-08-04 17:20:04 +00002740 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002741 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002742 if (Method->Method)
2743 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002744 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002745 Method = Method->getNext())
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002746 if (Method->Method)
2747 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002748
2749 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002750 }
2751};
2752} // end anonymous namespace
2753
Sebastian Redl059612d2010-08-03 21:58:15 +00002754/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002755///
2756/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002757/// in an on-disk hash table indexed by the selector. The hash table also
2758/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002759void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002760 using namespace llvm;
2761
Sebastian Redl059612d2010-08-03 21:58:15 +00002762 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002763 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002764 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002765 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002766 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002767 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002768 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002769 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002770
Sebastian Redl059612d2010-08-03 21:58:15 +00002771 // Create the on-disk hash table representation. We walk through every
2772 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002773 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002774 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002775 I = SelectorIDs.begin(), E = SelectorIDs.end();
2776 I != E; ++I) {
2777 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002778 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002779 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002780 I->second,
2781 ObjCMethodList(),
2782 ObjCMethodList()
2783 };
2784 if (F != SemaRef.MethodPool.end()) {
2785 Data.Instance = F->second.first;
2786 Data.Factory = F->second.second;
2787 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002788 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002789 // changed.
2790 if (Chain && I->second < FirstSelectorID) {
2791 // Selector already exists. Did it change?
2792 bool changed = false;
2793 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002794 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002795 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002796 changed = true;
2797 }
2798 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002799 M = M->getNext()) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002800 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002801 changed = true;
2802 }
2803 if (!changed)
2804 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002805 } else if (Data.Instance.Method || Data.Factory.Method) {
2806 // A new method pool entry.
2807 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002808 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002809 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002810 }
2811
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002812 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002813 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002814 uint32_t BucketOffset;
2815 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002816 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002817 llvm::raw_svector_ostream Out(MethodPool);
2818 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002819 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002820 BucketOffset = Generator.Emit(Out, Trait);
2821 }
2822
2823 // Create a blob abbreviation
2824 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002825 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2829 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2830
Douglas Gregor83941df2009-04-25 17:48:32 +00002831 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002832 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002833 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002834 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002835 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002836 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002837
2838 // Create a blob abbreviation for the selector table offsets.
2839 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002840 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2844 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2845
2846 // Write the selector offsets table.
2847 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002848 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002849 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002850 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002851 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002852 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002853 }
2854}
2855
Sebastian Redl3397c552010-08-18 23:56:27 +00002856/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002857void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002858 using namespace llvm;
2859 if (SemaRef.ReferencedSelectors.empty())
2860 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002861
Fariborz Jahanian32019832010-07-23 19:11:11 +00002862 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002863
Sebastian Redl3397c552010-08-18 23:56:27 +00002864 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002865 // very tricky to fix, and given that @selector shouldn't really appear in
2866 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002867 for (DenseMap<Selector, SourceLocation>::iterator S =
2868 SemaRef.ReferencedSelectors.begin(),
2869 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2870 Selector Sel = (*S).first;
2871 SourceLocation Loc = (*S).second;
2872 AddSelectorRef(Sel, Record);
2873 AddSourceLocation(Loc, Record);
2874 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002875 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002876}
2877
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002878//===----------------------------------------------------------------------===//
2879// Identifier Table Serialization
2880//===----------------------------------------------------------------------===//
2881
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002882namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002883class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002884 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002885 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002886 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002887 bool IsModule;
2888
Douglas Gregora92193e2009-04-28 21:18:29 +00002889 /// \brief Determines whether this is an "interesting" identifier
2890 /// that needs a full IdentifierInfo structure written into the hash
2891 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002892 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002893 if (II->isPoisoned() ||
2894 II->isExtensionToken() ||
2895 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002896 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002897 II->getFETokenInfo<void>())
2898 return true;
2899
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002900 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002901 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002902
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002903 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002904 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002905 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002906
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002907 if (Macro || (Macro = PP.getMacroDirectiveHistory(II))) {
2908 if (!IsModule)
2909 return !shouldIgnoreMacro(Macro, IsModule, PP);
2910 SubmoduleID ModID;
2911 if (getFirstPublicSubmoduleMacro(Macro, ModID))
2912 return true;
2913 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002914
2915 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002916 }
2917
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002918 DefMacroDirective *getFirstPublicSubmoduleMacro(MacroDirective *MD,
2919 SubmoduleID &ModID) {
2920 ModID = 0;
2921 if (DefMacroDirective *DefMD = getPublicSubmoduleMacro(MD, ModID))
2922 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2923 return DefMD;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002924 return 0;
2925 }
2926
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002927 DefMacroDirective *getNextPublicSubmoduleMacro(DefMacroDirective *MD,
2928 SubmoduleID &ModID) {
2929 if (DefMacroDirective *
2930 DefMD = getPublicSubmoduleMacro(MD->getPrevious(), ModID))
2931 if (!shouldIgnoreMacro(DefMD, IsModule, PP))
2932 return DefMD;
2933 return 0;
2934 }
2935
2936 /// \brief Traverses the macro directives history and returns the latest
2937 /// macro that is public and not undefined in the same submodule.
2938 /// A macro that is defined in submodule A and undefined in submodule B,
2939 /// will still be considered as defined/exported from submodule A.
2940 DefMacroDirective *getPublicSubmoduleMacro(MacroDirective *MD,
2941 SubmoduleID &ModID) {
2942 if (!MD)
2943 return 0;
2944
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002945 SubmoduleID OrigModID = ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002946 bool isUndefined = false;
2947 Optional<bool> isPublic;
2948 for (; MD; MD = MD->getPrevious()) {
2949 if (MD->isHidden())
2950 continue;
2951
2952 SubmoduleID ThisModID = getSubmoduleID(MD);
2953 if (ThisModID == 0) {
2954 isUndefined = false;
2955 isPublic = Optional<bool>();
2956 continue;
2957 }
2958 if (ThisModID != ModID){
2959 ModID = ThisModID;
2960 isUndefined = false;
2961 isPublic = Optional<bool>();
2962 }
Argyrios Kyrtzidisb2dbfd82013-04-03 05:11:33 +00002963 // We are looking for a definition in a different submodule than the one
2964 // that we started with. If a submodule has re-definitions of the same
2965 // macro, only the last definition will be used as the "exported" one.
2966 if (ModID == OrigModID)
2967 continue;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002968
2969 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2970 if (!isUndefined && (!isPublic.hasValue() || isPublic.getValue()))
2971 return DefMD;
2972 continue;
2973 }
2974
2975 if (isa<UndefMacroDirective>(MD)) {
2976 isUndefined = true;
2977 continue;
2978 }
2979
2980 VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
2981 if (!isPublic.hasValue())
2982 isPublic = VisMD->isPublic();
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002983 }
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002984
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002985 return 0;
2986 }
2987
2988 SubmoduleID getSubmoduleID(MacroDirective *MD) {
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00002989 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2990 MacroInfo *MI = DefMD->getInfo();
2991 if (unsigned ID = MI->getOwningModuleID())
2992 return ID;
2993 return Writer.inferSubmoduleIDFromLocation(MI->getDefinitionLoc());
2994 }
2995 return Writer.inferSubmoduleIDFromLocation(MD->getLocation());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00002996 }
2997
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002998public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002999 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003000 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003001
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003002 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003003 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00003004
Douglas Gregoreee242f2011-10-27 09:33:13 +00003005 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3006 IdentifierResolver &IdResolver, bool IsModule)
3007 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003008
3009 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00003010 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003011 }
Mike Stump1eb44332009-09-09 15:08:12 +00003012
3013 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00003014 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00003015 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00003016 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003017 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003018 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003019 DataLen += 2; // 2 bytes for builtin ID
3020 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003021 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003022 DataLen += 4; // MacroDirectives offset.
3023 if (IsModule) {
3024 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003025 for (DefMacroDirective *
3026 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3027 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003028 DataLen += 4; // MacroInfo ID.
3029 }
3030 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003031 }
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003032 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003033
Douglas Gregoreee242f2011-10-27 09:33:13 +00003034 for (IdentifierResolver::iterator D = IdResolver.begin(II),
3035 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00003036 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003037 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00003038 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00003039 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00003040 // We emit the key length after the data length so that every
3041 // string is preceded by a 16-bit length. This matches the PTH
3042 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00003043 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003044 return std::make_pair(KeyLen, DataLen);
3045 }
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Chris Lattner5f9e2722011-07-23 10:55:15 +00003047 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003048 unsigned KeyLen) {
3049 // Record the location of the key data. This is used when generating
3050 // the mapping from persistent IDs to strings.
3051 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00003052 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003053 }
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Douglas Gregor7143aab2011-09-01 17:04:32 +00003055 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003056 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003057 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00003058 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00003059 clang::io::Emit32(Out, ID << 1);
3060 return;
3061 }
Douglas Gregor5998da52009-04-28 21:32:13 +00003062
Douglas Gregora92193e2009-04-28 21:18:29 +00003063 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003064 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3065 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3066 clang::io::Emit16(Out, Bits);
3067 Bits = 0;
3068 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003069 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003070 Bits = (Bits << 1) | unsigned(IsModule);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003071 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3072 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00003073 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00003074 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00003075 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003076
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003077 if (HadMacroDefinition) {
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003078 clang::io::Emit32(Out, Writer.getMacroDirectivesOffset(II));
3079 if (IsModule) {
3080 // Write the IDs of macros coming from different submodules.
3081 SubmoduleID ModID;
Argyrios Kyrtzidisc56fff72013-03-26 17:17:01 +00003082 for (DefMacroDirective *
3083 DefMD = getFirstPublicSubmoduleMacro(Macro, ModID);
3084 DefMD; DefMD = getNextPublicSubmoduleMacro(DefMD, ModID)) {
3085 MacroID InfoID = Writer.getMacroID(DefMD->getInfo());
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00003086 assert(InfoID);
3087 clang::io::Emit32(Out, InfoID);
3088 }
3089 clang::io::Emit32(Out, 0);
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00003090 }
Douglas Gregor13292642011-12-02 15:45:10 +00003091 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00003092
Douglas Gregor668c1a42009-04-21 22:25:48 +00003093 // Emit the declaration IDs in reverse order, because the
3094 // IdentifierResolver provides the declarations as they would be
3095 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00003096 // "stat"), but the ASTReader adds declarations to the end of the list
3097 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003098 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003099 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
3100 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00003101 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00003102 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003103 D != DEnd; ++D)
Argyrios Kyrtzidis0532df02013-04-26 21:33:35 +00003104 clang::io::Emit32(Out, Writer.getDeclID(getMostRecentLocalDecl(*D)));
3105 }
3106
3107 /// \brief Returns the most recent local decl or the given decl if there are
3108 /// no local ones. The given decl is assumed to be the most recent one.
3109 Decl *getMostRecentLocalDecl(Decl *Orig) {
3110 // The only way a "from AST file" decl would be more recent from a local one
3111 // is if it came from a module.
3112 if (!PP.getLangOpts().Modules)
3113 return Orig;
3114
3115 // Look for a local in the decl chain.
3116 for (Decl *D = Orig; D; D = D->getPreviousDecl()) {
3117 if (!D->isFromASTFile())
3118 return D;
3119 // If we come up a decl from a (chained-)PCH stop since we won't find a
3120 // local one.
3121 if (D->getOwningModuleID() == 0)
3122 break;
3123 }
3124
3125 return Orig;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003126 }
3127};
3128} // end anonymous namespace
3129
Sebastian Redl3397c552010-08-18 23:56:27 +00003130/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003131///
3132/// The identifier table consists of a blob containing string data
3133/// (the actual identifiers themselves) and a separate "offsets" index
3134/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00003135void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3136 IdentifierResolver &IdResolver,
3137 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00003138 using namespace llvm;
3139
3140 // Create and write out the blob that contains the identifier
3141 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00003142 {
Sebastian Redl3397c552010-08-18 23:56:27 +00003143 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003144 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Douglas Gregor92b059e2009-04-28 20:33:11 +00003146 // Look for any identifiers that were named while processing the
3147 // headers, but are otherwise not needed. We add these to the hash
3148 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00003149 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00003150 // file.
3151 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3152 IDEnd = PP.getIdentifierTable().end();
3153 ID != IDEnd; ++ID)
3154 getIdentifierRef(ID->second);
3155
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003156 // Create the on-disk hash table representation. We only store offsets
3157 // for identifiers that appear here for the first time.
3158 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003159 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00003160 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
3161 ID != IDEnd; ++ID) {
3162 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00003163 if (!Chain || !ID->first->isFromAST() ||
3164 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003165 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00003166 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003167 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00003168
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003169 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003170 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00003171 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003172 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003173 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003174 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00003175 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00003176 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003177 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003178 }
3179
3180 // Create a blob abbreviation
3181 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003182 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00003183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00003185 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00003186
3187 // Write the identifier table
3188 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003189 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00003190 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00003191 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00003192 }
3193
3194 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003196 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3200 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3201
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003202#ifndef NDEBUG
3203 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3204 assert(IdentifierOffsets[I] && "Missing identifier offset?");
3205#endif
3206
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003207 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003208 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003209 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00003210 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00003211 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003212 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00003213}
3214
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003215//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003216// DeclContext's Name Lookup Table Serialization
3217//===----------------------------------------------------------------------===//
3218
3219namespace {
3220// Trait used for the on-disk hash table used in the method pool.
3221class ASTDeclContextNameLookupTrait {
3222 ASTWriter &Writer;
3223
3224public:
3225 typedef DeclarationName key_type;
3226 typedef key_type key_type_ref;
3227
3228 typedef DeclContext::lookup_result data_type;
3229 typedef const data_type& data_type_ref;
3230
3231 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3232
3233 unsigned ComputeHash(DeclarationName Name) {
3234 llvm::FoldingSetNodeID ID;
3235 ID.AddInteger(Name.getNameKind());
3236
3237 switch (Name.getNameKind()) {
3238 case DeclarationName::Identifier:
3239 ID.AddString(Name.getAsIdentifierInfo()->getName());
3240 break;
3241 case DeclarationName::ObjCZeroArgSelector:
3242 case DeclarationName::ObjCOneArgSelector:
3243 case DeclarationName::ObjCMultiArgSelector:
3244 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
3245 break;
3246 case DeclarationName::CXXConstructorName:
3247 case DeclarationName::CXXDestructorName:
3248 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003249 break;
3250 case DeclarationName::CXXOperatorName:
3251 ID.AddInteger(Name.getCXXOverloadedOperator());
3252 break;
3253 case DeclarationName::CXXLiteralOperatorName:
3254 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
3255 case DeclarationName::CXXUsingDirective:
3256 break;
3257 }
3258
3259 return ID.ComputeHash();
3260 }
3261
3262 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00003263 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003264 data_type_ref Lookup) {
3265 unsigned KeyLen = 1;
3266 switch (Name.getNameKind()) {
3267 case DeclarationName::Identifier:
3268 case DeclarationName::ObjCZeroArgSelector:
3269 case DeclarationName::ObjCOneArgSelector:
3270 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003271 case DeclarationName::CXXLiteralOperatorName:
3272 KeyLen += 4;
3273 break;
3274 case DeclarationName::CXXOperatorName:
3275 KeyLen += 1;
3276 break;
Douglas Gregore3605012011-08-02 18:32:54 +00003277 case DeclarationName::CXXConstructorName:
3278 case DeclarationName::CXXDestructorName:
3279 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003280 case DeclarationName::CXXUsingDirective:
3281 break;
3282 }
3283 clang::io::Emit16(Out, KeyLen);
3284
3285 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00003286 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003287 clang::io::Emit16(Out, DataLen);
3288
3289 return std::make_pair(KeyLen, DataLen);
3290 }
3291
Chris Lattner5f9e2722011-07-23 10:55:15 +00003292 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003293 using namespace clang::io;
3294
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003295 Emit8(Out, Name.getNameKind());
3296 switch (Name.getNameKind()) {
3297 case DeclarationName::Identifier:
3298 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003299 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003300 case DeclarationName::ObjCZeroArgSelector:
3301 case DeclarationName::ObjCOneArgSelector:
3302 case DeclarationName::ObjCMultiArgSelector:
3303 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003304 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003305 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00003306 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
3307 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003308 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00003309 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003310 case DeclarationName::CXXLiteralOperatorName:
3311 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00003312 return;
Douglas Gregore3605012011-08-02 18:32:54 +00003313 case DeclarationName::CXXConstructorName:
3314 case DeclarationName::CXXDestructorName:
3315 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003316 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003317 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003318 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003319
3320 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003321 }
3322
Chris Lattner5f9e2722011-07-23 10:55:15 +00003323 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003324 data_type Lookup, unsigned DataLen) {
3325 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003326 clang::io::Emit16(Out, Lookup.size());
3327 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3328 I != E; ++I)
3329 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003330
3331 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3332 }
3333};
3334} // end anonymous namespace
3335
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003336/// \brief Write the block containing all of the declaration IDs
3337/// visible from the given DeclContext.
3338///
3339/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003340/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003341uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3342 DeclContext *DC) {
3343 if (DC->getPrimaryContext() != DC)
3344 return 0;
3345
3346 // Since there is no name lookup into functions or methods, don't bother to
3347 // build a visible-declarations table for these entities.
3348 if (DC->isFunctionOrMethod())
3349 return 0;
3350
3351 // If not in C++, we perform name lookup for the translation unit via the
3352 // IdentifierInfo chains, don't bother to build a visible-declarations table.
David Blaikie4e4d0842012-03-11 07:00:24 +00003353 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003354 return 0;
3355
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003356 // Serialize the contents of the mapping used for lookup. Note that,
3357 // although we have two very different code paths, the serialized
3358 // representation is the same for both cases: a declaration name,
3359 // followed by a size, followed by references to the visible
3360 // declarations that have that name.
3361 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003362 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003363 if (!Map || Map->empty())
3364 return 0;
3365
3366 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3367 ASTDeclContextNameLookupTrait Trait(*this);
3368
3369 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003370 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003371 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003372 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3373 D != DEnd; ++D) {
3374 DeclarationName Name = D->first;
3375 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003376 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003377 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3378 // Hash all conversion function names to the same name. The actual
3379 // type information in conversion function name is not used in the
3380 // key (since such type information is not stable across different
3381 // modules), so the intended effect is to coalesce all of the conversion
3382 // functions under a single key.
3383 if (!ConversionName)
3384 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003385 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003386 continue;
3387 }
3388
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003389 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003390 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003391 }
3392
Douglas Gregore5a54b62011-08-30 20:49:19 +00003393 // Add the conversion functions
3394 if (!ConversionDecls.empty()) {
3395 Generator.insert(ConversionName,
3396 DeclContext::lookup_result(ConversionDecls.begin(),
3397 ConversionDecls.end()),
3398 Trait);
3399 }
3400
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003401 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003402 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003403 uint32_t BucketOffset;
3404 {
3405 llvm::raw_svector_ostream Out(LookupTable);
3406 // Make sure that no bucket is at offset 0
3407 clang::io::Emit32(Out, 0);
3408 BucketOffset = Generator.Emit(Out, Trait);
3409 }
3410
3411 // Write the lookup table
3412 RecordData Record;
3413 Record.push_back(DECL_CONTEXT_VISIBLE);
3414 Record.push_back(BucketOffset);
3415 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3416 LookupTable.str());
3417
3418 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3419 ++NumVisibleDeclContexts;
3420 return Offset;
3421}
3422
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003423/// \brief Write an UPDATE_VISIBLE block for the given context.
3424///
3425/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3426/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003427/// (in C++), for namespaces, and for classes with forward-declared unscoped
3428/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003429void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003430 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3431 if (!Map || Map->empty())
3432 return;
3433
3434 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3435 ASTDeclContextNameLookupTrait Trait(*this);
3436
3437 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003438 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3439 D != DEnd; ++D) {
3440 DeclarationName Name = D->first;
3441 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003442 // For any name that appears in this table, the results are complete, i.e.
3443 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003444 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003445 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003446 }
3447
3448 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003449 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003450 uint32_t BucketOffset;
3451 {
3452 llvm::raw_svector_ostream Out(LookupTable);
3453 // Make sure that no bucket is at offset 0
3454 clang::io::Emit32(Out, 0);
3455 BucketOffset = Generator.Emit(Out, Trait);
3456 }
3457
3458 // Write the lookup table
3459 RecordData Record;
3460 Record.push_back(UPDATE_VISIBLE);
3461 Record.push_back(getDeclID(cast<Decl>(DC)));
3462 Record.push_back(BucketOffset);
3463 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3464}
3465
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003466/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3467void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3468 RecordData Record;
3469 Record.push_back(Opts.fp_contract);
3470 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3471}
3472
3473/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3474void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003475 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003476 return;
3477
3478 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3479 RecordData Record;
3480#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3481#include "clang/Basic/OpenCLExtensions.def"
3482 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3483}
3484
Douglas Gregor2171bf12012-01-15 16:58:34 +00003485void ASTWriter::WriteRedeclarations() {
3486 RecordData LocalRedeclChains;
3487 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3488
3489 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3490 Decl *First = Redeclarations[I];
3491 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3492
3493 Decl *MostRecent = First->getMostRecentDecl();
3494
3495 // If we only have a single declaration, there is no point in storing
3496 // a redeclaration chain.
3497 if (First == MostRecent)
3498 continue;
3499
3500 unsigned Offset = LocalRedeclChains.size();
3501 unsigned Size = 0;
3502 LocalRedeclChains.push_back(0); // Placeholder for the size.
3503
3504 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003505 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003506 Prev = Prev->getPreviousDecl()) {
3507 if (!Prev->isFromASTFile()) {
3508 AddDeclRef(Prev, LocalRedeclChains);
3509 ++Size;
3510 }
3511 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003512
3513 if (!First->isFromASTFile() && Chain) {
3514 Decl *FirstFromAST = MostRecent;
3515 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3516 if (Prev->isFromASTFile())
3517 FirstFromAST = Prev;
3518 }
3519
3520 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3521 }
3522
Douglas Gregor2171bf12012-01-15 16:58:34 +00003523 LocalRedeclChains[Offset] = Size;
3524
3525 // Reverse the set of local redeclarations, so that we store them in
3526 // order (since we found them in reverse order).
3527 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3528
Douglas Gregoraa945902013-02-18 15:53:43 +00003529 // Add the mapping from the first ID from the AST to the set of local
3530 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003531 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3532 LocalRedeclsMap.push_back(Info);
3533
3534 assert(N == Redeclarations.size() &&
3535 "Deserialized a declaration we shouldn't have");
3536 }
3537
3538 if (LocalRedeclChains.empty())
3539 return;
3540
3541 // Sort the local redeclarations map by the first declaration ID,
3542 // since the reader will be performing binary searches on this information.
3543 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3544
3545 // Emit the local redeclarations map.
3546 using namespace llvm;
3547 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3548 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3549 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3551 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3552
3553 RecordData Record;
3554 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3555 Record.push_back(LocalRedeclsMap.size());
3556 Stream.EmitRecordWithBlob(AbbrevID, Record,
3557 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3558 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3559
3560 // Emit the redeclaration chains.
3561 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3562}
3563
Douglas Gregorcff9f262012-01-27 01:47:08 +00003564void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003565 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003566 RecordData Categories;
3567
3568 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3569 unsigned Size = 0;
3570 unsigned StartIndex = Categories.size();
3571
3572 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3573
3574 // Allocate space for the size.
3575 Categories.push_back(0);
3576
3577 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003578 for (ObjCInterfaceDecl::known_categories_iterator
3579 Cat = Class->known_categories_begin(),
3580 CatEnd = Class->known_categories_end();
3581 Cat != CatEnd; ++Cat, ++Size) {
3582 assert(getDeclID(*Cat) != 0 && "Bogus category");
3583 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003584 }
3585
3586 // Update the size.
3587 Categories[StartIndex] = Size;
3588
3589 // Record this interface -> category map.
3590 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3591 CategoriesMap.push_back(CatInfo);
3592 }
3593
3594 // Sort the categories map by the definition ID, since the reader will be
3595 // performing binary searches on this information.
3596 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3597
3598 // Emit the categories map.
3599 using namespace llvm;
3600 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3601 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3604 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3605
3606 RecordData Record;
3607 Record.push_back(OBJC_CATEGORIES_MAP);
3608 Record.push_back(CategoriesMap.size());
3609 Stream.EmitRecordWithBlob(AbbrevID, Record,
3610 reinterpret_cast<char*>(CategoriesMap.data()),
3611 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3612
3613 // Emit the category lists.
3614 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3615}
3616
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003617void ASTWriter::WriteMergedDecls() {
3618 if (!Chain || Chain->MergedDecls.empty())
3619 return;
3620
3621 RecordData Record;
3622 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3623 IEnd = Chain->MergedDecls.end();
3624 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003625 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003626 : getDeclID(I->first);
3627 assert(CanonID && "Merged declaration not known?");
3628
3629 Record.push_back(CanonID);
3630 Record.push_back(I->second.size());
3631 Record.append(I->second.begin(), I->second.end());
3632 }
3633 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3634}
3635
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003636//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003637// General Serialization Routines
3638//===----------------------------------------------------------------------===//
3639
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003640/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003641void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3642 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003643 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003644 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3645 e = Attrs.end(); i != e; ++i){
3646 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003647 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003648 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003649
Sean Huntcf807c42010-08-18 23:23:40 +00003650#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003651
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003652 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003653}
3654
John McCallaeeacf72013-05-03 00:10:13 +00003655void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3656 AddSourceLocation(Tok.getLocation(), Record);
3657 Record.push_back(Tok.getLength());
3658
3659 // FIXME: When reading literal tokens, reconstruct the literal pointer
3660 // if it is needed.
3661 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3662 // FIXME: Should translate token kind to a stable encoding.
3663 Record.push_back(Tok.getKind());
3664 // FIXME: Should translate token flags to a stable encoding.
3665 Record.push_back(Tok.getFlags());
3666}
3667
Chris Lattner5f9e2722011-07-23 10:55:15 +00003668void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003669 Record.push_back(Str.size());
3670 Record.insert(Record.end(), Str.begin(), Str.end());
3671}
3672
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003673void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3674 RecordDataImpl &Record) {
3675 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003676 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003677 Record.push_back(*Minor + 1);
3678 else
3679 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003680 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003681 Record.push_back(*Subminor + 1);
3682 else
3683 Record.push_back(0);
3684}
3685
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003686/// \brief Note that the identifier II occurs at the given offset
3687/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003688void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003689 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003690 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003691 // up earlier in the chain and thus don't need an offset.
3692 if (ID >= FirstIdentID)
3693 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003694}
3695
Douglas Gregor83941df2009-04-25 17:48:32 +00003696/// \brief Note that the selector Sel occurs at the given offset
3697/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003698void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003699 unsigned ID = SelectorIDs[Sel];
3700 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003701 // Don't record offsets for selectors that are also available in a different
3702 // file.
3703 if (ID < FirstSelectorID)
3704 return;
3705 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003706}
3707
Sebastian Redla4232eb2010-08-18 23:56:21 +00003708ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003709 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003710 WritingAST(false), DoneWritingDeclsAndTypes(false),
3711 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003712 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003713 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003714 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3715 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003716 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3717 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003718 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003719 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003720 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003721 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003722 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003723 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003724 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3725 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3726 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003727 DeclTypedefAbbrev(0),
3728 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3729 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003730{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003731}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003732
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003733ASTWriter::~ASTWriter() {
3734 for (FileDeclIDsTy::iterator
3735 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3736 delete I->second;
3737}
3738
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003739void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003740 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003741 Module *WritingModule, StringRef isysroot,
3742 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003743 WritingAST = true;
3744
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003745 ASTHasCompilerErrors = hasErrors;
3746
Douglas Gregor2cf26342009-04-09 22:27:44 +00003747 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003748 Stream.Emit((unsigned)'C', 8);
3749 Stream.Emit((unsigned)'P', 8);
3750 Stream.Emit((unsigned)'C', 8);
3751 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003752
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003753 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003754
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003755 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003756 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003757 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003758 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003759 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003760 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003761 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003762
3763 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003764}
3765
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003766template<typename Vector>
3767static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3768 ASTWriter::RecordData &Record) {
3769 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3770 I != E; ++I) {
3771 Writer.AddDeclRef(*I, Record);
3772 }
3773}
3774
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003775void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003776 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003777 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003778 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003779 using namespace llvm;
3780
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003781 bool isModule = WritingModule != 0;
3782
Douglas Gregorecc2c092011-12-01 22:20:10 +00003783 // Make sure that the AST reader knows to finalize itself.
3784 if (Chain)
3785 Chain->finalizeForWriting();
3786
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003787 ASTContext &Context = SemaRef.Context;
3788 Preprocessor &PP = SemaRef.PP;
3789
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003790 // Set up predefined declaration IDs.
3791 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003792 if (Context.ObjCIdDecl)
3793 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003794 if (Context.ObjCSelDecl)
3795 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003796 if (Context.ObjCClassDecl)
3797 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003798 if (Context.ObjCProtocolClassDecl)
3799 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003800 if (Context.Int128Decl)
3801 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3802 if (Context.UInt128Decl)
3803 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003804 if (Context.ObjCInstanceTypeDecl)
3805 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003806 if (Context.BuiltinVaListDecl)
3807 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3808
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003809 if (!Chain) {
3810 // Make sure that we emit IdentifierInfos (and any attached
3811 // declarations) for builtins. We don't need to do this when we're
3812 // emitting chained PCH files, because all of the builtins will be
3813 // in the original PCH file.
3814 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003815 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003816 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003817 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003818 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003819 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3820 getIdentifierRef(&Table.get(BuiltinNames[I]));
3821 }
3822
Douglas Gregoreee242f2011-10-27 09:33:13 +00003823 // If there are any out-of-date identifiers, bring them up to date.
3824 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003825 // Find out-of-date identifiers.
3826 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003827 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3828 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003829 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003830 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003831 OutOfDate.push_back(ID->second);
3832 }
3833
3834 // Update the out-of-date identifiers.
3835 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3836 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3837 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003838 }
3839
Chris Lattner63d65f82009-09-08 18:19:27 +00003840 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003841 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003842 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003843 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003844 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003845
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003846 // Build a record containing all of the file scoped decls in this file.
3847 RecordData UnusedFileScopedDecls;
Argyrios Kyrtzidisfaf01f02013-03-14 04:45:00 +00003848 if (!isModule)
3849 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3850 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003851
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003852 // Build a record containing all of the delegating constructors we still need
3853 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003854 RecordData DelegatingCtorDecls;
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00003855 if (!isModule)
3856 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003857
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003858 // Write the set of weak, undeclared identifiers. We always write the
3859 // entire table, since later PCH files in a PCH chain are only interested in
3860 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003861 RecordData WeakUndeclaredIdentifiers;
3862 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003863 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003864 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3865 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3866 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3867 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3868 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3869 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3870 }
3871 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003872
Richard Smith5ea6ef42013-01-10 23:43:47 +00003873 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003874 // declarations in this header file. Generally, this record will be
3875 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003876 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003877 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003878 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003879 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003880 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3881 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003882 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003883 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003884 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003885 }
3886
Douglas Gregorb81c1702009-04-27 20:06:05 +00003887 // Build a record containing all of the ext_vector declarations.
3888 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003889 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003890
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003891 // Build a record containing all of the VTable uses information.
3892 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003893 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003894 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3895 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3896 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3897 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3898 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003899 }
3900
3901 // Build a record containing all of dynamic classes declarations.
3902 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003903 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003904
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003905 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003906 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003907 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003908 I = SemaRef.PendingInstantiations.begin(),
3909 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3910 AddDeclRef(I->first, PendingInstantiations);
3911 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003912 }
3913 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3914 "There are local ones at end of translation unit!");
3915
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003916 // Build a record containing some declaration references.
3917 RecordData SemaDeclRefs;
3918 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3919 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3920 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3921 }
3922
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003923 RecordData CUDASpecialDeclRefs;
3924 if (Context.getcudaConfigureCallDecl()) {
3925 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3926 }
3927
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003928 // Build a record containing all of the known namespaces.
3929 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003930 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003931 I = SemaRef.KnownNamespaces.begin(),
3932 IEnd = SemaRef.KnownNamespaces.end();
3933 I != IEnd; ++I) {
3934 if (!I->second)
3935 AddDeclRef(I->first, KnownNamespaces);
3936 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003937
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003938 // Build a record of all used, undefined objects that require definitions.
3939 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003940
3941 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003942 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003943 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3944 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003945 AddDeclRef(I->first, UndefinedButUsed);
3946 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003947 }
3948
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003949 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003950 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003951
Sebastian Redl3397c552010-08-18 23:56:27 +00003952 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003953 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003954 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003955
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003956 // This is so that older clang versions, before the introduction
3957 // of the control block, can read and reject the newer PCH format.
3958 Record.clear();
3959 Record.push_back(VERSION_MAJOR);
3960 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3961
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003962 // Create a lexical update block containing all of the declarations in the
3963 // translation unit that do not come from other AST files.
3964 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3965 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3966 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3967 E = TU->noload_decls_end();
3968 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003969 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003970 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003971 }
3972
3973 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3974 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3975 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3976 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3977 Record.clear();
3978 Record.push_back(TU_UPDATE_LEXICAL);
3979 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3980 data(NewGlobalDecls));
3981
3982 // And a visible updates block for the translation unit.
3983 Abv = new llvm::BitCodeAbbrev();
3984 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3985 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3986 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3987 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3988 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3989 WriteDeclContextVisibleUpdate(TU);
3990
3991 // If the translation unit has an anonymous namespace, and we don't already
3992 // have an update block for it, write it as an update block.
3993 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3994 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3995 if (Record.empty()) {
3996 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003997 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003998 }
3999 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004000
4001 // Make sure visible decls, added to DeclContexts previously loaded from
4002 // an AST file, are registered for serialization.
4003 for (SmallVector<const Decl *, 16>::iterator
4004 I = UpdatingVisibleDecls.begin(),
4005 E = UpdatingVisibleDecls.end(); I != E; ++I) {
4006 GetDeclRef(*I);
4007 }
4008
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00004009 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004010 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00004011
Douglas Gregora119da02011-08-02 16:26:37 +00004012 // Form the record of special types.
4013 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00004014 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004015 AddTypeRef(Context.getFILEType(), SpecialTypes);
4016 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4017 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4018 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4019 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00004020 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00004021 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00004022
Douglas Gregor366809a2009-04-26 03:49:13 +00004023 // Keep writing types and declarations until all types and
4024 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00004025 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004026 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004027 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
4028 E = DeclsToRewrite.end();
4029 I != E; ++I)
4030 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004031 while (!DeclTypesToEmit.empty()) {
4032 DeclOrType DOT = DeclTypesToEmit.front();
4033 DeclTypesToEmit.pop();
4034 if (DOT.isType())
4035 WriteType(DOT.getType());
4036 else
4037 WriteDecl(Context, DOT.getDecl());
4038 }
4039 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004040
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004041 DoneWritingDeclsAndTypes = true;
4042
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004043 WriteFileDeclIDsMap();
4044 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00004045 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004046
4047 if (Chain) {
4048 // Write the mapping information describing our module dependencies and how
4049 // each of those modules were mapped into our own offset/ID space, so that
4050 // the reader can build the appropriate mapping to its own offset/ID space.
4051 // The map consists solely of a blob with the following format:
4052 // *(module-name-len:i16 module-name:len*i8
4053 // source-location-offset:i32
4054 // identifier-id:i32
4055 // preprocessed-entity-id:i32
4056 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00004057 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004058 // selector-id:i32
4059 // declaration-id:i32
4060 // c++-base-specifiers-id:i32
4061 // type-id:i32)
4062 //
4063 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
4064 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4066 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00004067 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004068 {
4069 llvm::raw_svector_ostream Out(Buffer);
4070 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00004071 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004072 M != MEnd; ++M) {
4073 StringRef FileName = (*M)->FileName;
4074 io::Emit16(Out, FileName.size());
4075 Out.write(FileName.data(), FileName.size());
4076 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
4077 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00004078 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004079 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00004080 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004081 io::Emit32(Out, (*M)->BaseSelectorID);
4082 io::Emit32(Out, (*M)->BaseDeclID);
4083 io::Emit32(Out, (*M)->BaseTypeIndex);
4084 }
4085 }
4086 Record.clear();
4087 Record.push_back(MODULE_OFFSET_MAP);
4088 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4089 Buffer.data(), Buffer.size());
4090 }
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004091 WritePreprocessor(PP, isModule);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00004092 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00004093 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00004094 WriteReferencedSelectorsPool(SemaRef);
Argyrios Kyrtzidis975d3532013-03-14 04:44:56 +00004095 WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00004096 WriteFPPragmaOptions(SemaRef.getFPOptions());
4097 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00004098
Sebastian Redl1476ed42010-07-16 16:36:56 +00004099 WriteTypeDeclOffsets();
Argyrios Kyrtzidisea744ab2013-03-27 17:17:23 +00004100 WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
Douglas Gregorad1de002009-04-18 05:55:16 +00004101
Anders Carlssonc8505782011-03-06 18:41:18 +00004102 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004103
Douglas Gregore209e502011-12-06 01:10:29 +00004104 // If we're emitting a module, write out the submodule information.
4105 if (WritingModule)
4106 WriteSubmodules(WritingModule);
4107
Douglas Gregora119da02011-08-02 16:26:37 +00004108 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4109
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004110 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00004111 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004112 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00004113
4114 // Write the record containing tentative definitions.
4115 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004116 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00004117
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00004118 // Write the record containing unused file scoped decls.
4119 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004120 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004121
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004122 // Write the record containing weak undeclared identifiers.
4123 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004124 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00004125 WeakUndeclaredIdentifiers);
4126
Richard Smith5ea6ef42013-01-10 23:43:47 +00004127 // Write the record containing locally-scoped extern "C" definitions.
4128 if (!LocallyScopedExternCDecls.empty())
4129 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
4130 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00004131
4132 // Write the record containing ext_vector type names.
4133 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004134 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00004135
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004136 // Write the record containing VTable uses information.
4137 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004138 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004139
4140 // Write the record containing dynamic classes declarations.
4141 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004142 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00004143
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004144 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00004145 if (!PendingInstantiations.empty())
4146 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00004147
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004148 // Write the record containing declaration references of Sema.
4149 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004150 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00004151
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004152 // Write the record containing CUDA-specific declaration references.
4153 if (!CUDASpecialDeclRefs.empty())
4154 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00004155
4156 // Write the delegating constructors.
4157 if (!DelegatingCtorDecls.empty())
4158 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00004159
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004160 // Write the known namespaces.
4161 if (!KnownNamespaces.empty())
4162 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00004163
Nick Lewyckycd0655b2013-02-01 08:13:20 +00004164 // Write the undefined internal functions and variables, and inline functions.
4165 if (!UndefinedButUsed.empty())
4166 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00004167
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004168 // Write the visible updates to DeclContexts.
4169 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
4170 I = UpdatedDeclContexts.begin(),
4171 E = UpdatedDeclContexts.end();
4172 I != E; ++I)
4173 WriteDeclContextVisibleUpdate(*I);
4174
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00004175 if (!WritingModule) {
4176 // Write the submodules that were imported, if any.
4177 RecordData ImportedModules;
4178 for (ASTContext::import_iterator I = Context.local_import_begin(),
4179 IEnd = Context.local_import_end();
4180 I != IEnd; ++I) {
4181 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4182 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
4183 }
4184 if (!ImportedModules.empty()) {
4185 // Sort module IDs.
4186 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
4187
4188 // Unique module IDs.
4189 ImportedModules.erase(std::unique(ImportedModules.begin(),
4190 ImportedModules.end()),
4191 ImportedModules.end());
4192
4193 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4194 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00004195 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004196
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00004197 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00004198 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00004199 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00004200 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00004201 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00004202
Douglas Gregor3e1af842009-04-17 22:13:46 +00004203 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00004204 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00004205 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00004206 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00004207 Record.push_back(NumLexicalDeclContexts);
4208 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004209 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00004210 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00004211}
4212
Douglas Gregor61c5e342011-09-17 00:05:03 +00004213/// \brief Go through the declaration update blocks and resolve declaration
4214/// pointers into declaration IDs.
4215void ASTWriter::ResolveDeclUpdatesBlocks() {
4216 for (DeclUpdateMap::iterator
4217 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4218 const Decl *D = I->first;
4219 UpdateRecord &URec = I->second;
4220
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004221 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00004222 continue; // The decl will be written completely
4223
4224 unsigned Idx = 0, N = URec.size();
4225 while (Idx < N) {
4226 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004227 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4228 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4229 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4230 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
4231 ++Idx;
4232 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004233
Douglas Gregor61c5e342011-09-17 00:05:03 +00004234 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4235 ++Idx;
4236 break;
Richard Smith9dadfab2013-05-11 05:45:24 +00004237
4238 case UPD_CXX_DEDUCED_RETURN_TYPE:
4239 URec[Idx] = GetOrCreateTypeID(
4240 QualType::getFromOpaquePtr(reinterpret_cast<void *>(URec[Idx])));
4241 ++Idx;
4242 break;
Douglas Gregor61c5e342011-09-17 00:05:03 +00004243 }
4244 }
4245 }
4246}
4247
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004248void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004249 if (DeclUpdates.empty())
4250 return;
4251
4252 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00004253 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004254 for (DeclUpdateMap::iterator
4255 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
4256 const Decl *D = I->first;
4257 UpdateRecord &URec = I->second;
4258
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004259 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00004260 continue; // The decl will be written completely,no need to store updates.
4261
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004262 uint64_t Offset = Stream.GetCurrentBitNo();
4263 Stream.EmitRecord(DECL_UPDATES, URec);
4264
4265 OffsetsRecord.push_back(GetDeclRef(D));
4266 OffsetsRecord.push_back(Offset);
4267 }
4268 Stream.ExitBlock();
4269 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
4270}
4271
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00004272void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00004273 if (ReplacedDecls.empty())
4274 return;
4275
4276 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004277 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00004278 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00004279 Record.push_back(I->ID);
4280 Record.push_back(I->Offset);
4281 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004282 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004283 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00004284}
4285
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004286void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004287 Record.push_back(Loc.getRawEncoding());
4288}
4289
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004290void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004291 AddSourceLocation(Range.getBegin(), Record);
4292 AddSourceLocation(Range.getEnd(), Record);
4293}
4294
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004295void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00004296 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00004297 const uint64_t *Words = Value.getRawData();
4298 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00004299}
4300
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004301void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00004302 Record.push_back(Value.isUnsigned());
4303 AddAPInt(Value, Record);
4304}
4305
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004306void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00004307 AddAPInt(Value.bitcastToAPInt(), Record);
4308}
4309
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004310void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004311 Record.push_back(getIdentifierRef(II));
4312}
4313
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004314IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00004315 if (II == 0)
4316 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00004317
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004318 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00004319 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004320 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00004321 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004322}
4323
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004324MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004325 // Don't emit builtin macros like __LINE__ to the AST file unless they
4326 // have been redefined by the header (in which case they are not
4327 // isBuiltinMacro).
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004328 if (MI == 0 || MI->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00004329 return 0;
4330
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004331 MacroID &ID = MacroIDs[MI];
4332 if (ID == 0) {
Douglas Gregora8235d62012-10-09 23:05:51 +00004333 ID = NextMacroID++;
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004334 MacroInfoToEmitData Info = { Name, MI, ID };
4335 MacroInfosToEmit.push_back(Info);
4336 }
Douglas Gregora8235d62012-10-09 23:05:51 +00004337 return ID;
4338}
4339
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00004340MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4341 if (MI == 0 || MI->isBuiltinMacro())
4342 return 0;
4343
4344 assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4345 return MacroIDs[MI];
4346}
4347
4348uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4349 assert(IdentMacroDirectivesOffsetMap[Name] && "not set!");
4350 return IdentMacroDirectivesOffsetMap[Name];
4351}
4352
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004353void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004354 Record.push_back(getSelectorRef(SelRef));
4355}
4356
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004357SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004358 if (Sel.getAsOpaquePtr() == 0) {
4359 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004360 }
4361
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004362 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004363 if (SID == 0 && Chain) {
4364 // This might trigger a ReadSelector callback, which will set the ID for
4365 // this selector.
4366 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004367 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004368 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004369 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004370 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004371 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004372 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004373 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004374}
4375
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004376void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004377 AddDeclRef(Temp->getDestructor(), Record);
4378}
4379
Douglas Gregor7c789c12010-10-29 22:39:52 +00004380void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4381 CXXBaseSpecifier const *BasesEnd,
4382 RecordDataImpl &Record) {
4383 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4384 CXXBaseSpecifiersToWrite.push_back(
4385 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4386 Bases, BasesEnd));
4387 Record.push_back(NextCXXBaseSpecifiersID++);
4388}
4389
Sebastian Redla4232eb2010-08-18 23:56:21 +00004390void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004391 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004392 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004393 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004394 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004395 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004396 break;
4397 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004398 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004399 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004400 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004401 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004402 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004403 break;
4404 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004405 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004406 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004407 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004408 break;
John McCall833ca992009-10-29 08:12:44 +00004409 case TemplateArgument::Null:
4410 case TemplateArgument::Integral:
4411 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004412 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004413 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004414 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004415 break;
4416 }
4417}
4418
Sebastian Redla4232eb2010-08-18 23:56:21 +00004419void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004420 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004421 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004422
4423 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4424 bool InfoHasSameExpr
4425 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4426 Record.push_back(InfoHasSameExpr);
4427 if (InfoHasSameExpr)
4428 return; // Avoid storing the same expr twice.
4429 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004430 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4431 Record);
4432}
4433
Douglas Gregordc355712011-02-25 00:36:19 +00004434void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4435 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004436 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004437 AddTypeRef(QualType(), Record);
4438 return;
4439 }
4440
Douglas Gregordc355712011-02-25 00:36:19 +00004441 AddTypeLoc(TInfo->getTypeLoc(), Record);
4442}
4443
4444void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4445 AddTypeRef(TL.getType(), Record);
4446
John McCalla1ee0c52009-10-16 21:56:05 +00004447 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004448 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004449 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004450}
4451
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004452void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004453 Record.push_back(GetOrCreateTypeID(T));
4454}
4455
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004456TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
Richard Smith9dadfab2013-05-11 05:45:24 +00004457 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004458 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004459 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4460}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004461
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004462TypeID ASTWriter::getTypeID(QualType T) const {
Richard Smith9dadfab2013-05-11 05:45:24 +00004463 assert(Context);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004464 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004465 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004466}
4467
4468TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4469 if (T.isNull())
4470 return TypeIdx();
4471 assert(!T.getLocalFastQualifiers());
4472
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004473 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004474 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004475 if (DoneWritingDeclsAndTypes) {
4476 assert(0 && "New type seen after serializing all the types to emit!");
4477 return TypeIdx();
4478 }
4479
Douglas Gregor366809a2009-04-26 03:49:13 +00004480 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004481 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004482 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004483 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004484 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004485 return Idx;
4486}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004487
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004488TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004489 if (T.isNull())
4490 return TypeIdx();
4491 assert(!T.getLocalFastQualifiers());
4492
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004493 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4494 assert(I != TypeIdxs.end() && "Type not emitted!");
4495 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004496}
4497
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004498void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004499 Record.push_back(GetDeclRef(D));
4500}
4501
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004502DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004503 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4504
Douglas Gregor2cf26342009-04-09 22:27:44 +00004505 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004506 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004507 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004508
4509 // If D comes from an AST file, its declaration ID is already known and
4510 // fixed.
4511 if (D->isFromASTFile())
4512 return D->getGlobalID();
4513
Douglas Gregor97475832010-10-05 18:37:06 +00004514 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004515 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004516 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004517 if (DoneWritingDeclsAndTypes) {
4518 assert(0 && "New decl seen after serializing all the decls to emit!");
4519 return 0;
4520 }
4521
Douglas Gregor2cf26342009-04-09 22:27:44 +00004522 // We haven't seen this declaration before. Give it a new ID and
4523 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004524 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004525 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004526 }
4527
Sebastian Redl681d7232010-07-27 00:17:23 +00004528 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004529}
4530
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004531DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004532 if (D == 0)
4533 return 0;
4534
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004535 // If D comes from an AST file, its declaration ID is already known and
4536 // fixed.
4537 if (D->isFromASTFile())
4538 return D->getGlobalID();
4539
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004540 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4541 return DeclIDs[D];
4542}
4543
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004544static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4545 std::pair<unsigned, serialization::DeclID> R) {
4546 return L.first < R.first;
4547}
4548
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004549void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004550 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004551 assert(D);
4552
4553 SourceLocation Loc = D->getLocation();
4554 if (Loc.isInvalid())
4555 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004556
4557 // We only keep track of the file-level declarations of each file.
4558 if (!D->getLexicalDeclContext()->isFileContext())
4559 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004560 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4561 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004562 if (isa<ParmVarDecl>(D))
4563 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004564
4565 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004566 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004567 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004568 FileID FID;
4569 unsigned Offset;
4570 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004571 if (FID.isInvalid())
4572 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004573 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004574
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004575 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004576 if (!Info)
4577 Info = new DeclIDInFileInfo();
4578
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004579 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004580 LocDeclIDsTy &Decls = Info->DeclIDs;
4581
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004582 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004583 Decls.push_back(LocDecl);
4584 return;
4585 }
4586
4587 LocDeclIDsTy::iterator
4588 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4589
4590 Decls.insert(I, LocDecl);
4591}
4592
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004593void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004594 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004595 Record.push_back(Name.getNameKind());
4596 switch (Name.getNameKind()) {
4597 case DeclarationName::Identifier:
4598 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4599 break;
4600
4601 case DeclarationName::ObjCZeroArgSelector:
4602 case DeclarationName::ObjCOneArgSelector:
4603 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004604 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004605 break;
4606
4607 case DeclarationName::CXXConstructorName:
4608 case DeclarationName::CXXDestructorName:
4609 case DeclarationName::CXXConversionFunctionName:
4610 AddTypeRef(Name.getCXXNameType(), Record);
4611 break;
4612
4613 case DeclarationName::CXXOperatorName:
4614 Record.push_back(Name.getCXXOverloadedOperator());
4615 break;
4616
Sean Hunt3e518bd2009-11-29 07:34:05 +00004617 case DeclarationName::CXXLiteralOperatorName:
4618 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4619 break;
4620
Douglas Gregor2cf26342009-04-09 22:27:44 +00004621 case DeclarationName::CXXUsingDirective:
4622 // No extra data to emit
4623 break;
4624 }
4625}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004626
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004627void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004628 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004629 switch (Name.getNameKind()) {
4630 case DeclarationName::CXXConstructorName:
4631 case DeclarationName::CXXDestructorName:
4632 case DeclarationName::CXXConversionFunctionName:
4633 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4634 break;
4635
4636 case DeclarationName::CXXOperatorName:
4637 AddSourceLocation(
4638 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4639 Record);
4640 AddSourceLocation(
4641 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4642 Record);
4643 break;
4644
4645 case DeclarationName::CXXLiteralOperatorName:
4646 AddSourceLocation(
4647 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4648 Record);
4649 break;
4650
4651 case DeclarationName::Identifier:
4652 case DeclarationName::ObjCZeroArgSelector:
4653 case DeclarationName::ObjCOneArgSelector:
4654 case DeclarationName::ObjCMultiArgSelector:
4655 case DeclarationName::CXXUsingDirective:
4656 break;
4657 }
4658}
4659
4660void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004661 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004662 AddDeclarationName(NameInfo.getName(), Record);
4663 AddSourceLocation(NameInfo.getLoc(), Record);
4664 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4665}
4666
4667void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004668 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004669 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004670 Record.push_back(Info.NumTemplParamLists);
4671 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4672 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4673}
4674
Sebastian Redla4232eb2010-08-18 23:56:21 +00004675void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004676 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004677 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004678 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004679 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004680
4681 // Push each of the NNS's onto a stack for serialization in reverse order.
4682 while (NNS) {
4683 NestedNames.push_back(NNS);
4684 NNS = NNS->getPrefix();
4685 }
4686
4687 Record.push_back(NestedNames.size());
4688 while(!NestedNames.empty()) {
4689 NNS = NestedNames.pop_back_val();
4690 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4691 Record.push_back(Kind);
4692 switch (Kind) {
4693 case NestedNameSpecifier::Identifier:
4694 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4695 break;
4696
4697 case NestedNameSpecifier::Namespace:
4698 AddDeclRef(NNS->getAsNamespace(), Record);
4699 break;
4700
Douglas Gregor14aba762011-02-24 02:36:08 +00004701 case NestedNameSpecifier::NamespaceAlias:
4702 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4703 break;
4704
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004705 case NestedNameSpecifier::TypeSpec:
4706 case NestedNameSpecifier::TypeSpecWithTemplate:
4707 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4708 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4709 break;
4710
4711 case NestedNameSpecifier::Global:
4712 // Don't need to write an associated value.
4713 break;
4714 }
4715 }
4716}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004717
Douglas Gregordc355712011-02-25 00:36:19 +00004718void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4719 RecordDataImpl &Record) {
4720 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004721 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004722 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004723
4724 // Push each of the nested-name-specifiers's onto a stack for
4725 // serialization in reverse order.
4726 while (NNS) {
4727 NestedNames.push_back(NNS);
4728 NNS = NNS.getPrefix();
4729 }
4730
4731 Record.push_back(NestedNames.size());
4732 while(!NestedNames.empty()) {
4733 NNS = NestedNames.pop_back_val();
4734 NestedNameSpecifier::SpecifierKind Kind
4735 = NNS.getNestedNameSpecifier()->getKind();
4736 Record.push_back(Kind);
4737 switch (Kind) {
4738 case NestedNameSpecifier::Identifier:
4739 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4740 AddSourceRange(NNS.getLocalSourceRange(), Record);
4741 break;
4742
4743 case NestedNameSpecifier::Namespace:
4744 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4745 AddSourceRange(NNS.getLocalSourceRange(), Record);
4746 break;
4747
4748 case NestedNameSpecifier::NamespaceAlias:
4749 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4750 AddSourceRange(NNS.getLocalSourceRange(), Record);
4751 break;
4752
4753 case NestedNameSpecifier::TypeSpec:
4754 case NestedNameSpecifier::TypeSpecWithTemplate:
4755 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4756 AddTypeLoc(NNS.getTypeLoc(), Record);
4757 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4758 break;
4759
4760 case NestedNameSpecifier::Global:
4761 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4762 break;
4763 }
4764 }
4765}
4766
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004767void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004768 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004769 Record.push_back(Kind);
4770 switch (Kind) {
4771 case TemplateName::Template:
4772 AddDeclRef(Name.getAsTemplateDecl(), Record);
4773 break;
4774
4775 case TemplateName::OverloadedTemplate: {
4776 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4777 Record.push_back(OvT->size());
4778 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4779 I != E; ++I)
4780 AddDeclRef(*I, Record);
4781 break;
4782 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004783
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004784 case TemplateName::QualifiedTemplate: {
4785 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4786 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4787 Record.push_back(QualT->hasTemplateKeyword());
4788 AddDeclRef(QualT->getTemplateDecl(), Record);
4789 break;
4790 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004791
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004792 case TemplateName::DependentTemplate: {
4793 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4794 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4795 Record.push_back(DepT->isIdentifier());
4796 if (DepT->isIdentifier())
4797 AddIdentifierRef(DepT->getIdentifier(), Record);
4798 else
4799 Record.push_back(DepT->getOperator());
4800 break;
4801 }
John McCall14606042011-06-30 08:33:18 +00004802
4803 case TemplateName::SubstTemplateTemplateParm: {
4804 SubstTemplateTemplateParmStorage *subst
4805 = Name.getAsSubstTemplateTemplateParm();
4806 AddDeclRef(subst->getParameter(), Record);
4807 AddTemplateName(subst->getReplacement(), Record);
4808 break;
4809 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004810
4811 case TemplateName::SubstTemplateTemplateParmPack: {
4812 SubstTemplateTemplateParmPackStorage *SubstPack
4813 = Name.getAsSubstTemplateTemplateParmPack();
4814 AddDeclRef(SubstPack->getParameterPack(), Record);
4815 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4816 break;
4817 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004818 }
4819}
4820
Michael J. Spencer20249a12010-10-21 03:16:25 +00004821void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004822 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004823 Record.push_back(Arg.getKind());
4824 switch (Arg.getKind()) {
4825 case TemplateArgument::Null:
4826 break;
4827 case TemplateArgument::Type:
4828 AddTypeRef(Arg.getAsType(), Record);
4829 break;
4830 case TemplateArgument::Declaration:
4831 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004832 Record.push_back(Arg.isDeclForReferenceParam());
4833 break;
4834 case TemplateArgument::NullPtr:
4835 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004836 break;
4837 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004838 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004839 AddTypeRef(Arg.getIntegralType(), Record);
4840 break;
4841 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004842 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4843 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004844 case TemplateArgument::TemplateExpansion:
4845 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004846 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004847 Record.push_back(*NumExpansions + 1);
4848 else
4849 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004850 break;
4851 case TemplateArgument::Expression:
4852 AddStmt(Arg.getAsExpr());
4853 break;
4854 case TemplateArgument::Pack:
4855 Record.push_back(Arg.pack_size());
4856 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4857 I != E; ++I)
4858 AddTemplateArgument(*I, Record);
4859 break;
4860 }
4861}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004862
4863void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004864ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004865 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004866 assert(TemplateParams && "No TemplateParams!");
4867 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4868 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4869 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4870 Record.push_back(TemplateParams->size());
4871 for (TemplateParameterList::const_iterator
4872 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4873 P != PEnd; ++P)
4874 AddDeclRef(*P, Record);
4875}
4876
4877/// \brief Emit a template argument list.
4878void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004879ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004880 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004881 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004882 Record.push_back(TemplateArgs->size());
4883 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004884 AddTemplateArgument(TemplateArgs->get(i), Record);
4885}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004886
4887
4888void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004889ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004890 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004891 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004892 I = Set.begin(), E = Set.end(); I != E; ++I) {
4893 AddDeclRef(I.getDecl(), Record);
4894 Record.push_back(I.getAccess());
4895 }
4896}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004897
Sebastian Redla4232eb2010-08-18 23:56:21 +00004898void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004899 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004900 Record.push_back(Base.isVirtual());
4901 Record.push_back(Base.isBaseOfClass());
4902 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004903 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004904 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004905 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004906 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4907 : SourceLocation(),
4908 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004909}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004910
Douglas Gregor7c789c12010-10-29 22:39:52 +00004911void ASTWriter::FlushCXXBaseSpecifiers() {
4912 RecordData Record;
4913 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4914 Record.clear();
4915
4916 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004917 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004918 if (Index == CXXBaseSpecifiersOffsets.size())
4919 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4920 else {
4921 if (Index > CXXBaseSpecifiersOffsets.size())
4922 CXXBaseSpecifiersOffsets.resize(Index + 1);
4923 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4924 }
4925
4926 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4927 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4928 Record.push_back(BEnd - B);
4929 for (; B != BEnd; ++B)
4930 AddCXXBaseSpecifier(*B, Record);
4931 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004932
4933 // Flush any expressions that were written as part of the base specifiers.
4934 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004935 }
4936
4937 CXXBaseSpecifiersToWrite.clear();
4938}
4939
Sean Huntcbb67482011-01-08 20:30:50 +00004940void ASTWriter::AddCXXCtorInitializers(
4941 const CXXCtorInitializer * const *CtorInitializers,
4942 unsigned NumCtorInitializers,
4943 RecordDataImpl &Record) {
4944 Record.push_back(NumCtorInitializers);
4945 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4946 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004947
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004948 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004949 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004950 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004951 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004952 } else if (Init->isDelegatingInitializer()) {
4953 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004954 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004955 } else if (Init->isMemberInitializer()){
4956 Record.push_back(CTOR_INITIALIZER_MEMBER);
4957 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004958 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004959 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4960 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004961 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004962
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004963 AddSourceLocation(Init->getMemberLocation(), Record);
4964 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004965 AddSourceLocation(Init->getLParenLoc(), Record);
4966 AddSourceLocation(Init->getRParenLoc(), Record);
4967 Record.push_back(Init->isWritten());
4968 if (Init->isWritten()) {
4969 Record.push_back(Init->getSourceOrder());
4970 } else {
4971 Record.push_back(Init->getNumArrayIndices());
4972 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4973 AddDeclRef(Init->getArrayIndex(i), Record);
4974 }
4975 }
4976}
4977
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004978void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4979 assert(D->DefinitionData);
4980 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004981 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004982 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004983 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004984 Record.push_back(Data.Aggregate);
4985 Record.push_back(Data.PlainOldData);
4986 Record.push_back(Data.Empty);
4987 Record.push_back(Data.Polymorphic);
4988 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004989 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004990 Record.push_back(Data.HasNoNonEmptyBases);
4991 Record.push_back(Data.HasPrivateFields);
4992 Record.push_back(Data.HasProtectedFields);
4993 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004994 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004995 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004996 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004997 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004998 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4999 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5000 Record.push_back(Data.NeedOverloadResolutionForDestructor);
5001 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5002 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5003 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005004 Record.push_back(Data.HasTrivialSpecialMembers);
5005 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00005006 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00005007 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00005008 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00005009 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005010 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00005011 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00005012 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00005013 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5014 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5015 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5016 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00005017 Record.push_back(Data.FailedImplicitMoveConstructor);
5018 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00005019 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005020
5021 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005022 if (Data.NumBases > 0)
5023 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5024 Record);
5025
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005026 // FIXME: Make VBases lazily computed when needed to avoid storing them.
5027 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00005028 if (Data.NumVBases > 0)
5029 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5030 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005031
5032 AddUnresolvedSet(Data.Conversions, Record);
5033 AddUnresolvedSet(Data.VisibleConversions, Record);
5034 // Data.Definition is the owning decl, no need to write it.
5035 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005036
5037 // Add lambda-specific data.
5038 if (Data.IsLambda) {
5039 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00005040 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005041 Record.push_back(Lambda.NumCaptures);
5042 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00005043 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005044 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00005045 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00005046 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5047 LambdaExpr::Capture &Capture = Lambda.Captures[I];
5048 AddSourceLocation(Capture.getLocation(), Record);
5049 Record.push_back(Capture.isImplicit());
5050 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
5051 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
5052 AddDeclRef(Var, Record);
5053 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
5054 : SourceLocation(),
5055 Record);
5056 }
5057 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00005058}
5059
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005060void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005061 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005062 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005063 assert(FirstDeclID == NextDeclID &&
5064 FirstTypeID == NextTypeID &&
5065 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00005066 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00005067 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00005068 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005069 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00005070
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005071 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005072
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005073 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5074 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5075 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00005076 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00005077 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00005078 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005079 NextDeclID = FirstDeclID;
5080 NextTypeID = FirstTypeID;
5081 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005082 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00005083 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00005084 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00005085}
5086
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005087void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005088 // Always keep the highest ID. See \p TypeRead() for more information.
5089 IdentID &StoredID = IdentifierIDs[II];
5090 if (ID > StoredID)
5091 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00005092}
5093
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005094void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005095 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9317ab92013-03-22 21:12:57 +00005096 MacroID &StoredID = MacroIDs[MI];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005097 if (ID > StoredID)
5098 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00005099}
5100
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00005101void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00005102 // Always take the highest-numbered type index. This copes with an interesting
5103 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00005104 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00005105 // keep the higher-numbered entry so that we can properly write it out to
5106 // the AST file.
5107 TypeIdx &StoredIdx = TypeIdxs[T];
5108 if (Idx.getIndex() >= StoredIdx.getIndex())
5109 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00005110}
5111
Sebastian Redl8538e8d2010-08-18 23:57:32 +00005112void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00005113 // Always keep the highest ID. See \p TypeRead() for more information.
5114 SelectorID &StoredID = SelectorIDs[S];
5115 if (ID > StoredID)
5116 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00005117}
Douglas Gregor77424bc2010-10-02 19:29:26 +00005118
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005119void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00005120 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00005121 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00005122 MacroDefinitions[MD] = ID;
5123}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005124
Douglas Gregora015cab2011-12-02 17:30:13 +00005125void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5126 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5127 SubmoduleIDs[Mod] = ID;
5128}
5129
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005130void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00005131 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00005132 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005133 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
5134 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00005135 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005136 // A forward reference was mutated into a definition. Rewrite it.
5137 // FIXME: This happens during template instantiation, should we
5138 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00005139 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005140 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00005141 }
5142}
Douglas Gregora8235d62012-10-09 23:05:51 +00005143
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005144void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005145 assert(!WritingAST && "Already writing the AST!");
5146
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005147 // TU and namespaces are handled elsewhere.
5148 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5149 return;
5150
Douglas Gregor919814d2011-09-09 23:01:35 +00005151 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005152 return; // Not a source decl added to a DeclContext from PCH.
5153
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00005154 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005155 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00005156 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00005157}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005158
5159void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005160 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005161 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00005162 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005163 return; // Not a source member added to a class from PCH.
5164 if (!isa<CXXMethodDecl>(D))
5165 return; // We are interested in lazily declared implicit methods.
5166
5167 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00005168 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005169 UpdateRecord &Record = DeclUpdates[RD];
5170 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005171 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00005172}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005173
5174void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
5175 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005176 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005177 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00005178 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005179 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005180 return; // Not a source specialization added to a template from PCH.
5181
5182 UpdateRecord &Record = DeclUpdates[TD];
5183 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005184 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00005185}
Douglas Gregor89d99802010-11-30 06:16:57 +00005186
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005187void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
5188 const FunctionDecl *D) {
5189 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00005190 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005191 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00005192 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005193 return; // Not a source specialization added to a template from PCH.
5194
5195 UpdateRecord &Record = DeclUpdates[TD];
5196 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00005197 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00005198}
5199
Richard Smith9dadfab2013-05-11 05:45:24 +00005200void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5201 assert(!WritingAST && "Already writing the AST!");
5202 FD = FD->getCanonicalDecl();
5203 if (!FD->isFromASTFile())
5204 return; // Not a function declared in PCH and defined outside.
5205
5206 UpdateRecord &Record = DeclUpdates[FD];
5207 Record.push_back(UPD_CXX_DEDUCED_RETURN_TYPE);
5208 Record.push_back(reinterpret_cast<uint64_t>(ReturnType.getAsOpaquePtr()));
5209}
5210
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005211void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005212 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005213 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00005214 return; // Declaration not imported from PCH.
5215
5216 // Implicit decl from a PCH was defined.
5217 // FIXME: Should implicit definition be a separate FunctionDecl?
5218 RewriteDecl(D);
5219}
5220
Sebastian Redlf79a7192011-04-29 08:19:30 +00005221void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005222 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005223 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00005224 return;
5225
5226 // Since the actual instantiation is delayed, this really means that we need
5227 // to update the instantiation location.
5228 UpdateRecord &Record = DeclUpdates[D];
5229 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
5230 AddSourceLocation(
5231 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
5232}
5233
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005234void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5235 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00005236 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00005237 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005238 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00005239
5240 assert(IFD->getDefinition() && "Category on a class without a definition?");
5241 ObjCClassesWithCategories.insert(
5242 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00005243}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00005244
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00005245
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00005246void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
5247 const ObjCPropertyDecl *OrigProp,
5248 const ObjCCategoryDecl *ClassExt) {
5249 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
5250 if (!D)
5251 return;
5252
5253 assert(!WritingAST && "Already writing the AST!");
5254 if (!D->isFromASTFile())
5255 return; // Declaration not imported from PCH.
5256
5257 RewriteDecl(D);
5258}