blob: 5a690b3ded86ca4f6d103092972fcfc0370d1281 [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"
Daniel Dunbar2596e422009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000047#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000048#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000049#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000051#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000052#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000053#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000054using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000055using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000056
Sebastian Redlade50002010-07-30 17:03:48 +000057template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000058static StringRef data(const std::vector<T, Allocator> &v) {
59 if (v.empty()) return StringRef();
60 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000062}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000063
64template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000065static StringRef data(const SmallVectorImpl<T> &v) {
66 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000067 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000068}
69
Douglas Gregor2cf26342009-04-09 22:27:44 +000070//===----------------------------------------------------------------------===//
71// Type serialization
72//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000073
Douglas Gregor2cf26342009-04-09 22:27:44 +000074namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000075 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000076 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000077 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000078
79 public:
80 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000083 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000084 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000085
86 void VisitArrayType(const ArrayType *T);
87 void VisitFunctionType(const FunctionType *T);
88 void VisitTagType(const TagType *T);
89
90#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
91#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000092#include "clang/AST/TypeNodes.def"
93 };
94}
95
Sebastian Redl3397c552010-08-18 23:56:27 +000096void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000097 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000098}
99
Sebastian Redl3397c552010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000102 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103}
104
Sebastian Redl3397c552010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000107 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
Sebastian Redl3397c552010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000112 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113}
114
Sebastian Redl3397c552010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000116 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
117 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000118 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000119}
120
Sebastian Redl3397c552010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000122 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000123 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124}
125
Sebastian Redl3397c552010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000127 Writer.AddTypeRef(T->getPointeeType(), Record);
128 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000129 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130}
131
Sebastian Redl3397c552010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 Writer.AddTypeRef(T->getElementType(), Record);
134 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000135 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136}
137
Sebastian Redl3397c552010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139 VisitArrayType(T);
140 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000141 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142}
143
Sebastian Redl3397c552010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000146 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147}
148
Sebastian Redl3397c552010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000154 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155}
156
Sebastian Redl3397c552010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getElementType(), Record);
159 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000160 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000161 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162}
163
Sebastian Redl3397c552010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000166 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167}
168
Sebastian Redl3397c552010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000171 FunctionType::ExtInfo C = T->getExtInfo();
172 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000173 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000174 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000175 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000176 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000177 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178}
179
Sebastian Redl3397c552010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000182 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183}
184
Sebastian Redl3397c552010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000186 VisitFunctionType(T);
187 Record.push_back(T->getNumArgs());
188 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
189 Writer.AddTypeRef(T->getArgType(I), Record);
190 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000191 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000193 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000194 Record.push_back(T->getExceptionSpecType());
195 if (T->getExceptionSpecType() == EST_Dynamic) {
196 Record.push_back(T->getNumExceptions());
197 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
198 Writer.AddTypeRef(T->getExceptionType(I), Record);
199 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
200 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000201 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
203 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000204 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
205 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000206 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000207 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208}
209
Sebastian Redl3397c552010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000211 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000212 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000213}
John McCalled976492009-12-04 22:46:56 +0000214
Sebastian Redl3397c552010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000217 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
218 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000219 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220}
221
Sebastian Redl3397c552010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000223 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000224 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225}
226
Sebastian Redl3397c552010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000229 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000230}
231
Sebastian Redl3397c552010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000233 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000234 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000236}
237
Sean Huntca63c202011-05-24 22:41:36 +0000238void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
239 Writer.AddTypeRef(T->getBaseType(), Record);
240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
241 Record.push_back(T->getUTTKind());
242 Code = TYPE_UNARY_TRANSFORM;
243}
244
Richard Smith34b41d92011-02-20 03:19:35 +0000245void ASTTypeWriter::VisitAutoType(const AutoType *T) {
246 Writer.AddTypeRef(T->getDeducedType(), Record);
247 Code = TYPE_AUTO;
248}
249
Sebastian Redl3397c552010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000251 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000252 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000253 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254 "Cannot serialize in the middle of a type definition");
255}
256
Sebastian Redl3397c552010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000259 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260}
261
Sebastian Redl3397c552010-08-18 23:56:27 +0000262void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000264 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000265}
266
John McCall9d156a72011-01-06 01:58:22 +0000267void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
268 Writer.AddTypeRef(T->getModifiedType(), Record);
269 Writer.AddTypeRef(T->getEquivalentType(), Record);
270 Record.push_back(T->getAttrKind());
271 Code = TYPE_ATTRIBUTED;
272}
273
Mike Stump1eb44332009-09-09 15:08:12 +0000274void
Sebastian Redl3397c552010-08-18 23:56:27 +0000275ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000276 const SubstTemplateTypeParmType *T) {
277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
278 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000280}
281
282void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000283ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
284 const SubstTemplateTypeParmPackType *T) {
285 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
286 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
287 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
288}
289
290void
Sebastian Redl3397c552010-08-18 23:56:27 +0000291ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000292 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000293 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000294 Writer.AddTemplateName(T->getTemplateName(), Record);
295 Record.push_back(T->getNumArgs());
296 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
297 ArgI != ArgE; ++ArgI)
298 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000299 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
300 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000303 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl3397c552010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000308 VisitArrayType(T);
309 Writer.AddStmt(T->getSizeExpr());
310 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000311 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000312}
313
314void
Sebastian Redl3397c552010-08-18 23:56:27 +0000315ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316 const DependentSizedExtVectorType *T) {
317 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000318 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000323 Record.push_back(T->getDepth());
324 Record.push_back(T->getIndex());
325 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000327 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000328}
329
330void
Sebastian Redl3397c552010-08-18 23:56:27 +0000331ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000332 Record.push_back(T->getKeyword());
333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000335 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
336 : T->getCanonicalTypeInternal(),
337 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000338 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000339}
340
341void
Sebastian Redl3397c552010-08-18 23:56:27 +0000342ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000343 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000344 Record.push_back(T->getKeyword());
345 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
346 Writer.AddIdentifierRef(T->getIdentifier(), Record);
347 Record.push_back(T->getNumArgs());
348 for (DependentTemplateSpecializationType::iterator
349 I = T->begin(), E = T->end(); I != E; ++I)
350 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000351 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000352}
353
Douglas Gregor7536dd52010-12-20 02:24:11 +0000354void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
355 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000356 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
357 Record.push_back(*NumExpansions + 1);
358 else
359 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000360 Code = TYPE_PACK_EXPANSION;
361}
362
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000363void ASTTypeWriter::VisitParenType(const ParenType *T) {
364 Writer.AddTypeRef(T->getInnerType(), Record);
365 Code = TYPE_PAREN;
366}
367
Sebastian Redl3397c552010-08-18 23:56:27 +0000368void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000369 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000370 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
371 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000372 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000373}
374
Sebastian Redl3397c552010-08-18 23:56:27 +0000375void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000376 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000377 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000378 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000379}
380
Sebastian Redl3397c552010-08-18 23:56:27 +0000381void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000382 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000384}
385
Sebastian Redl3397c552010-08-18 23:56:27 +0000386void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000387 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000388 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000389 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000390 E = T->qual_end(); I != E; ++I)
391 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000392 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000393}
394
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000395void
Sebastian Redl3397c552010-08-18 23:56:27 +0000396ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000397 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000399}
400
Eli Friedmanb001de72011-10-06 23:00:33 +0000401void
402ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
403 Writer.AddTypeRef(T->getValueType(), Record);
404 Code = TYPE_ATOMIC;
405}
406
John McCalla1ee0c52009-10-16 21:56:05 +0000407namespace {
408
409class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000410 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000411 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000412
413public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000414 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000415 : Writer(Writer), Record(Record) { }
416
John McCall51bd8032009-10-18 01:05:36 +0000417#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000418#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000419 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000420#include "clang/AST/TypeLocNodes.def"
421
John McCall51bd8032009-10-18 01:05:36 +0000422 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
423 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000424};
425
426}
427
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
429 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000432 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
433 if (TL.needsExtraLocalData()) {
434 Record.push_back(TL.getWrittenTypeSpec());
435 Record.push_back(TL.getWrittenSignSpec());
436 Record.push_back(TL.getWrittenWidthSpec());
437 Record.push_back(TL.hasModeAttr());
438 }
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000454}
John McCall51bd8032009-10-18 01:05:36 +0000455void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000457 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000458}
John McCall51bd8032009-10-18 01:05:36 +0000459void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
461 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
462 Record.push_back(TL.getSizeExpr() ? 1 : 0);
463 if (TL.getSizeExpr())
464 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000465}
John McCall51bd8032009-10-18 01:05:36 +0000466void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
473 VisitArrayTypeLoc(TL);
474}
475void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
476 DependentSizedArrayTypeLoc TL) {
477 VisitArrayTypeLoc(TL);
478}
479void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
480 DependentSizedExtVectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
489void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000491 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
492 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000493 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000494 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
495 Writer.AddDeclRef(TL.getArg(i), Record);
496}
497void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
500void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
501 VisitFunctionTypeLoc(TL);
502}
John McCalled976492009-12-04 22:46:56 +0000503void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
John McCall51bd8032009-10-18 01:05:36 +0000506void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getNameLoc(), Record);
508}
509void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000510 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000513}
514void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000515 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000519}
520void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
Sean Huntca63c202011-05-24 22:41:36 +0000523void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getKWLoc(), Record);
525 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
526 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
527 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
528}
Richard Smith34b41d92011-02-20 03:19:35 +0000529void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall51bd8032009-10-18 01:05:36 +0000532void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
535void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getNameLoc(), Record);
537}
John McCall9d156a72011-01-06 01:58:22 +0000538void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
540 if (TL.hasAttrOperand()) {
541 SourceRange range = TL.getAttrOperandParensRange();
542 Writer.AddSourceLocation(range.getBegin(), Record);
543 Writer.AddSourceLocation(range.getEnd(), Record);
544 }
545 if (TL.hasAttrExprOperand()) {
546 Expr *operand = TL.getAttrExprOperand();
547 Record.push_back(operand ? 1 : 0);
548 if (operand) Writer.AddStmt(operand);
549 } else if (TL.hasAttrEnumOperand()) {
550 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
551 }
552}
John McCall51bd8032009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
John McCall49a832b2009-10-18 09:09:24 +0000556void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
557 SubstTemplateTypeParmTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000560void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
561 SubstTemplateTypeParmPackTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
John McCall51bd8032009-10-18 01:05:36 +0000564void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
565 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000566 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000567 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
568 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
569 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
570 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000571 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
572 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000573}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000574void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
576 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
577}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000578void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000579 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000580 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000581}
John McCall3cb0ebd2010-03-10 03:28:59 +0000582void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
Douglas Gregor4714c122010-03-31 17:34:00 +0000585void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000586 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000587 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
589}
John McCall33500952010-06-11 00:33:02 +0000590void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
591 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000593 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000594 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000595 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000596 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
597 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
598 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000599 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
600 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000601}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000602void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
604}
John McCall51bd8032009-10-18 01:05:36 +0000605void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000607}
608void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
609 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000610 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
611 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
612 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
613 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000614}
John McCall54e14c42009-10-22 22:37:11 +0000615void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000617}
Eli Friedmanb001de72011-10-06 23:00:33 +0000618void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
619 Writer.AddSourceLocation(TL.getKWLoc(), Record);
620 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
621 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
622}
John McCalla1ee0c52009-10-16 21:56:05 +0000623
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000624//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000625// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000626//===----------------------------------------------------------------------===//
627
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000628static void EmitBlockID(unsigned ID, const char *Name,
629 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000630 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000631 Record.clear();
632 Record.push_back(ID);
633 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
634
635 // Emit the block name if present.
636 if (Name == 0 || Name[0] == 0) return;
637 Record.clear();
638 while (*Name)
639 Record.push_back(*Name++);
640 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
641}
642
643static void EmitRecordID(unsigned ID, const char *Name,
644 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000645 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000646 Record.clear();
647 Record.push_back(ID);
648 while (*Name)
649 Record.push_back(*Name++);
650 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000651}
652
653static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000654 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000655#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000656 RECORD(STMT_STOP);
657 RECORD(STMT_NULL_PTR);
658 RECORD(STMT_NULL);
659 RECORD(STMT_COMPOUND);
660 RECORD(STMT_CASE);
661 RECORD(STMT_DEFAULT);
662 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000663 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000664 RECORD(STMT_IF);
665 RECORD(STMT_SWITCH);
666 RECORD(STMT_WHILE);
667 RECORD(STMT_DO);
668 RECORD(STMT_FOR);
669 RECORD(STMT_GOTO);
670 RECORD(STMT_INDIRECT_GOTO);
671 RECORD(STMT_CONTINUE);
672 RECORD(STMT_BREAK);
673 RECORD(STMT_RETURN);
674 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000675 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000676 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000677 RECORD(EXPR_PREDEFINED);
678 RECORD(EXPR_DECL_REF);
679 RECORD(EXPR_INTEGER_LITERAL);
680 RECORD(EXPR_FLOATING_LITERAL);
681 RECORD(EXPR_IMAGINARY_LITERAL);
682 RECORD(EXPR_STRING_LITERAL);
683 RECORD(EXPR_CHARACTER_LITERAL);
684 RECORD(EXPR_PAREN);
685 RECORD(EXPR_UNARY_OPERATOR);
686 RECORD(EXPR_SIZEOF_ALIGN_OF);
687 RECORD(EXPR_ARRAY_SUBSCRIPT);
688 RECORD(EXPR_CALL);
689 RECORD(EXPR_MEMBER);
690 RECORD(EXPR_BINARY_OPERATOR);
691 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
692 RECORD(EXPR_CONDITIONAL_OPERATOR);
693 RECORD(EXPR_IMPLICIT_CAST);
694 RECORD(EXPR_CSTYLE_CAST);
695 RECORD(EXPR_COMPOUND_LITERAL);
696 RECORD(EXPR_EXT_VECTOR_ELEMENT);
697 RECORD(EXPR_INIT_LIST);
698 RECORD(EXPR_DESIGNATED_INIT);
699 RECORD(EXPR_IMPLICIT_VALUE_INIT);
700 RECORD(EXPR_VA_ARG);
701 RECORD(EXPR_ADDR_LABEL);
702 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000703 RECORD(EXPR_CHOOSE);
704 RECORD(EXPR_GNU_NULL);
705 RECORD(EXPR_SHUFFLE_VECTOR);
706 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000707 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000708 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000709 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000710 RECORD(EXPR_OBJC_ARRAY_LITERAL);
711 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000712 RECORD(EXPR_OBJC_ENCODE);
713 RECORD(EXPR_OBJC_SELECTOR_EXPR);
714 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
715 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
716 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
717 RECORD(EXPR_OBJC_KVC_REF_EXPR);
718 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000719 RECORD(STMT_OBJC_FOR_COLLECTION);
720 RECORD(STMT_OBJC_CATCH);
721 RECORD(STMT_OBJC_FINALLY);
722 RECORD(STMT_OBJC_AT_TRY);
723 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
724 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000725 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000726 RECORD(EXPR_CXX_OPERATOR_CALL);
727 RECORD(EXPR_CXX_CONSTRUCT);
728 RECORD(EXPR_CXX_STATIC_CAST);
729 RECORD(EXPR_CXX_DYNAMIC_CAST);
730 RECORD(EXPR_CXX_REINTERPRET_CAST);
731 RECORD(EXPR_CXX_CONST_CAST);
732 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000733 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000734 RECORD(EXPR_CXX_BOOL_LITERAL);
735 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000736 RECORD(EXPR_CXX_TYPEID_EXPR);
737 RECORD(EXPR_CXX_TYPEID_TYPE);
738 RECORD(EXPR_CXX_UUIDOF_EXPR);
739 RECORD(EXPR_CXX_UUIDOF_TYPE);
740 RECORD(EXPR_CXX_THIS);
741 RECORD(EXPR_CXX_THROW);
742 RECORD(EXPR_CXX_DEFAULT_ARG);
743 RECORD(EXPR_CXX_BIND_TEMPORARY);
744 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
745 RECORD(EXPR_CXX_NEW);
746 RECORD(EXPR_CXX_DELETE);
747 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
748 RECORD(EXPR_EXPR_WITH_CLEANUPS);
749 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
751 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
752 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
753 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
754 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
755 RECORD(EXPR_CXX_NOEXCEPT);
756 RECORD(EXPR_OPAQUE_VALUE);
757 RECORD(EXPR_BINARY_TYPE_TRAIT);
758 RECORD(EXPR_PACK_EXPANSION);
759 RECORD(EXPR_SIZEOF_PACK);
760 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000761 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000762#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000763}
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Sebastian Redla4232eb2010-08-18 23:56:21 +0000765void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000766 RecordData Record;
767 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000769#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
770#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000772 // Control Block.
773 BLOCK(CONTROL_BLOCK);
774 RECORD(METADATA);
775 RECORD(IMPORTS);
776 RECORD(LANGUAGE_OPTIONS);
777 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000778 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000779 RECORD(ORIGINAL_PCH_DIR);
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000780 RECORD(ORIGINAL_FILE_ID);
Douglas Gregora930dc92012-10-22 18:42:04 +0000781 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000782 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000783 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000784 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000785 RECORD(PREPROCESSOR_OPTIONS);
786
Douglas Gregorc337fef2012-10-19 00:45:00 +0000787 BLOCK(INPUT_FILES_BLOCK);
788 RECORD(INPUT_FILE);
789
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000790 // AST Top-Level Block.
791 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000792 RECORD(TYPE_OFFSET);
793 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000794 RECORD(IDENTIFIER_OFFSET);
795 RECORD(IDENTIFIER_TABLE);
796 RECORD(EXTERNAL_DEFINITIONS);
797 RECORD(SPECIAL_TYPES);
798 RECORD(STATISTICS);
799 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000800 RECORD(UNUSED_FILESCOPED_DECLS);
Richard Smith5ea6ef42013-01-10 23:43:47 +0000801 RECORD(LOCALLY_SCOPED_EXTERN_C_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000802 RECORD(SELECTOR_OFFSETS);
803 RECORD(METHOD_POOL);
804 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000805 RECORD(SOURCE_LOCATION_OFFSETS);
806 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000807 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000808 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000809 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000810 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000811 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000812 RECORD(SEMA_DECL_REFS);
813 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
814 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
815 RECORD(DECL_REPLACEMENTS);
816 RECORD(UPDATE_VISIBLE);
817 RECORD(DECL_UPDATE_OFFSETS);
818 RECORD(DECL_UPDATES);
819 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
820 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000821 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000822 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000823 RECORD(FP_PRAGMA_OPTIONS);
824 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000825 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000826 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000827 RECORD(MODULE_OFFSET_MAP);
828 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000829 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000830 RECORD(FILE_SORTED_DECLS);
831 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000832 RECORD(MERGED_DECLARATIONS);
833 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000834 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000835 RECORD(MACRO_OFFSET);
836 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000837
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000838 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000839 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 RECORD(SM_SLOC_FILE_ENTRY);
841 RECORD(SM_SLOC_BUFFER_ENTRY);
842 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000843 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000845 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000846 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 RECORD(PP_MACRO_OBJECT_LIKE);
848 RECORD(PP_MACRO_FUNCTION_LIKE);
849 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000850
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000851 // Decls and Types block.
852 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000853 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000854 RECORD(TYPE_COMPLEX);
855 RECORD(TYPE_POINTER);
856 RECORD(TYPE_BLOCK_POINTER);
857 RECORD(TYPE_LVALUE_REFERENCE);
858 RECORD(TYPE_RVALUE_REFERENCE);
859 RECORD(TYPE_MEMBER_POINTER);
860 RECORD(TYPE_CONSTANT_ARRAY);
861 RECORD(TYPE_INCOMPLETE_ARRAY);
862 RECORD(TYPE_VARIABLE_ARRAY);
863 RECORD(TYPE_VECTOR);
864 RECORD(TYPE_EXT_VECTOR);
865 RECORD(TYPE_FUNCTION_PROTO);
866 RECORD(TYPE_FUNCTION_NO_PROTO);
867 RECORD(TYPE_TYPEDEF);
868 RECORD(TYPE_TYPEOF_EXPR);
869 RECORD(TYPE_TYPEOF);
870 RECORD(TYPE_RECORD);
871 RECORD(TYPE_ENUM);
872 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000873 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000874 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000875 RECORD(TYPE_DECLTYPE);
876 RECORD(TYPE_ELABORATED);
877 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
878 RECORD(TYPE_UNRESOLVED_USING);
879 RECORD(TYPE_INJECTED_CLASS_NAME);
880 RECORD(TYPE_OBJC_OBJECT);
881 RECORD(TYPE_TEMPLATE_TYPE_PARM);
882 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
883 RECORD(TYPE_DEPENDENT_NAME);
884 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
886 RECORD(TYPE_PAREN);
887 RECORD(TYPE_PACK_EXPANSION);
888 RECORD(TYPE_ATTRIBUTED);
889 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000890 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000891 RECORD(DECL_TYPEDEF);
892 RECORD(DECL_ENUM);
893 RECORD(DECL_RECORD);
894 RECORD(DECL_ENUM_CONSTANT);
895 RECORD(DECL_FUNCTION);
896 RECORD(DECL_OBJC_METHOD);
897 RECORD(DECL_OBJC_INTERFACE);
898 RECORD(DECL_OBJC_PROTOCOL);
899 RECORD(DECL_OBJC_IVAR);
900 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000901 RECORD(DECL_OBJC_CATEGORY);
902 RECORD(DECL_OBJC_CATEGORY_IMPL);
903 RECORD(DECL_OBJC_IMPLEMENTATION);
904 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
905 RECORD(DECL_OBJC_PROPERTY);
906 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000907 RECORD(DECL_FIELD);
908 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000909 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000910 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000911 RECORD(DECL_FILE_SCOPE_ASM);
912 RECORD(DECL_BLOCK);
913 RECORD(DECL_CONTEXT_LEXICAL);
914 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000915 RECORD(DECL_NAMESPACE);
916 RECORD(DECL_NAMESPACE_ALIAS);
917 RECORD(DECL_USING);
918 RECORD(DECL_USING_SHADOW);
919 RECORD(DECL_USING_DIRECTIVE);
920 RECORD(DECL_UNRESOLVED_USING_VALUE);
921 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
922 RECORD(DECL_LINKAGE_SPEC);
923 RECORD(DECL_CXX_RECORD);
924 RECORD(DECL_CXX_METHOD);
925 RECORD(DECL_CXX_CONSTRUCTOR);
926 RECORD(DECL_CXX_DESTRUCTOR);
927 RECORD(DECL_CXX_CONVERSION);
928 RECORD(DECL_ACCESS_SPEC);
929 RECORD(DECL_FRIEND);
930 RECORD(DECL_FRIEND_TEMPLATE);
931 RECORD(DECL_CLASS_TEMPLATE);
932 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
933 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
934 RECORD(DECL_FUNCTION_TEMPLATE);
935 RECORD(DECL_TEMPLATE_TYPE_PARM);
936 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
937 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
938 RECORD(DECL_STATIC_ASSERT);
939 RECORD(DECL_CXX_BASE_SPECIFIERS);
940 RECORD(DECL_INDIRECTFIELD);
941 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
942
Douglas Gregora72d8c42011-06-03 02:27:19 +0000943 // Statements and Exprs can occur in the Decls and Types block.
944 AddStmtsExprs(Stream, Record);
945
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000946 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000947 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000948 RECORD(PPD_MACRO_DEFINITION);
949 RECORD(PPD_INCLUSION_DIRECTIVE);
950
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000951#undef RECORD
952#undef BLOCK
953 Stream.ExitBlock();
954}
955
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956/// \brief Adjusts the given filename to only write out the portion of the
957/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000958///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959/// \param Filename the file name to adjust.
960///
961/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
962/// the returned filename will be adjusted by this system root.
963///
964/// \returns either the original filename (if it needs no adjustment) or the
965/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000966static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000967adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Douglas Gregor832d6202011-07-22 16:35:34 +0000970 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 // Verify that the filename and the system root have the same prefix.
974 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000975 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 if (Filename[Pos] != isysroot[Pos])
977 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 // We hit the end of the filename before we hit the end of the system root.
980 if (!Filename[Pos])
981 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 // If the file name has a '/' at the current position, skip over the '/'.
984 // We distinguish sysroot-based includes from absolute includes by the
985 // absence of '/' at the beginning of sysroot-based includes.
986 if (Filename[Pos] == '/')
987 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 return Filename + Pos;
990}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000991
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000992/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000993void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
994 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000995 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000996 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000997 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
998 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000999
Douglas Gregore650c8c2009-07-07 00:12:59 +00001000 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001001 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1010 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1011 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001012 Record.push_back(VERSION_MAJOR);
1013 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001014 Record.push_back(CLANG_VERSION_MAJOR);
1015 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001016 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001017 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001018 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1019 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001020
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001021 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001022 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001023 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001024 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001025 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001026
1027 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1028 M != MEnd; ++M) {
1029 // Skip modules that weren't directly imported.
1030 if (!(*M)->isDirectlyImported())
1031 continue;
1032
1033 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001034 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001035 // FIXME: This writes the absolute path for AST files we depend on.
1036 const std::string &FileName = (*M)->FileName;
1037 Record.push_back(FileName.size());
1038 Record.append(FileName.begin(), FileName.end());
1039 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001040 Stream.EmitRecord(IMPORTS, Record);
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001043 // Language options.
1044 Record.clear();
1045 const LangOptions &LangOpts = Context.getLangOpts();
1046#define LANGOPT(Name, Bits, Default, Description) \
1047 Record.push_back(LangOpts.Name);
1048#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1049 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1050#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001051#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1052#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001053
1054 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1055 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1056
1057 Record.push_back(LangOpts.CurrentModule.size());
1058 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1059 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1060
Douglas Gregoree097c12012-10-18 17:58:09 +00001061 // Target options.
1062 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001063 const TargetInfo &Target = Context.getTargetInfo();
1064 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001065 AddString(TargetOpts.Triple, Record);
1066 AddString(TargetOpts.CPU, Record);
1067 AddString(TargetOpts.ABI, Record);
1068 AddString(TargetOpts.CXXABI, Record);
1069 AddString(TargetOpts.LinkerVersion, Record);
1070 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1071 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1072 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1073 }
1074 Record.push_back(TargetOpts.Features.size());
1075 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1076 AddString(TargetOpts.Features[I], Record);
1077 }
1078 Stream.EmitRecord(TARGET_OPTIONS, Record);
1079
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001080 // Diagnostic options.
1081 Record.clear();
1082 const DiagnosticOptions &DiagOpts
1083 = Context.getDiagnostics().getDiagnosticOptions();
1084#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1085#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1086 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1087#include "clang/Basic/DiagnosticOptions.def"
1088 Record.push_back(DiagOpts.Warnings.size());
1089 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1090 AddString(DiagOpts.Warnings[I], Record);
1091 // Note: we don't serialize the log or serialization file names, because they
1092 // are generally transient files and will almost always be overridden.
1093 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1094
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001095 // File system options.
1096 Record.clear();
1097 const FileSystemOptions &FSOpts
1098 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1099 AddString(FSOpts.WorkingDir, Record);
1100 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1101
Douglas Gregorbbf38312012-10-24 16:50:34 +00001102 // Header search options.
1103 Record.clear();
1104 const HeaderSearchOptions &HSOpts
1105 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1106 AddString(HSOpts.Sysroot, Record);
1107
1108 // Include entries.
1109 Record.push_back(HSOpts.UserEntries.size());
1110 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1111 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1112 AddString(Entry.Path, Record);
1113 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001114 Record.push_back(Entry.IsFramework);
1115 Record.push_back(Entry.IgnoreSysRoot);
1116 Record.push_back(Entry.IsInternal);
1117 Record.push_back(Entry.ImplicitExternC);
1118 }
1119
1120 // System header prefixes.
1121 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1122 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1123 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1124 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1125 }
1126
1127 AddString(HSOpts.ResourceDir, Record);
1128 AddString(HSOpts.ModuleCachePath, Record);
1129 Record.push_back(HSOpts.DisableModuleHash);
1130 Record.push_back(HSOpts.UseBuiltinIncludes);
1131 Record.push_back(HSOpts.UseStandardSystemIncludes);
1132 Record.push_back(HSOpts.UseStandardCXXIncludes);
1133 Record.push_back(HSOpts.UseLibcxx);
1134 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1135
Douglas Gregora71a7d82012-10-24 20:05:57 +00001136 // Preprocessor options.
1137 Record.clear();
1138 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1139
1140 // Macro definitions.
1141 Record.push_back(PPOpts.Macros.size());
1142 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1143 AddString(PPOpts.Macros[I].first, Record);
1144 Record.push_back(PPOpts.Macros[I].second);
1145 }
1146
1147 // Includes
1148 Record.push_back(PPOpts.Includes.size());
1149 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1150 AddString(PPOpts.Includes[I], Record);
1151
1152 // Macro includes
1153 Record.push_back(PPOpts.MacroIncludes.size());
1154 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1155 AddString(PPOpts.MacroIncludes[I], Record);
1156
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001157 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001158 AddString(PPOpts.ImplicitPCHInclude, Record);
1159 AddString(PPOpts.ImplicitPTHInclude, Record);
1160 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1161 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1162
Douglas Gregor31d375f2011-05-06 21:43:30 +00001163 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001164 SourceManager &SM = Context.getSourceManager();
1165 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1166 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001167 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1168 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001169 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1170 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1171
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001172 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001174 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001175
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001176 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001177 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001178 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001179 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001180 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001181 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001182 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001183 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001184
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001185 Record.clear();
1186 Record.push_back(SM.getMainFileID().getOpaqueValue());
1187 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1188
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001189 // Original PCH directory
1190 if (!OutputFile.empty() && OutputFile != "-") {
1191 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1192 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1194 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1195
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001196 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001197
1198 llvm::sys::fs::make_absolute(OutputPath);
1199 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1200
1201 RecordData Record;
1202 Record.push_back(ORIGINAL_PCH_DIR);
1203 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1204 }
1205
Douglas Gregor745e6f12012-10-19 00:38:02 +00001206 WriteInputFiles(Context.SourceMgr, isysroot);
1207 Stream.ExitBlock();
1208}
1209
1210void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1211 using namespace llvm;
1212 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1213 RecordData Record;
1214
1215 // Create input-file abbreviation.
1216 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1217 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001218 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001219 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1220 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001221 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001222 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1223 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1224
1225 // Write out all of the input files.
1226 std::vector<uint32_t> InputFileOffsets;
1227 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1228 // Get this source location entry.
1229 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001230 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001231
1232 // We only care about file entries that were not overridden.
1233 if (!SLoc->isFile())
1234 continue;
1235 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001236 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001237 continue;
1238
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001239 uint32_t &InputFileID = InputFileIDs[Cache->OrigEntry];
1240 if (InputFileID != 0)
1241 continue; // already recorded this file.
1242
Douglas Gregora930dc92012-10-22 18:42:04 +00001243 // Record this entry's offset.
1244 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001245
1246 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001247
Douglas Gregor745e6f12012-10-19 00:38:02 +00001248 Record.clear();
1249 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001250 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001251
1252 // Emit size/modification time for this file.
1253 Record.push_back(Cache->OrigEntry->getSize());
1254 Record.push_back(Cache->OrigEntry->getModificationTime());
1255
Douglas Gregora930dc92012-10-22 18:42:04 +00001256 // Whether this file was overridden.
1257 Record.push_back(Cache->BufferOverridden);
1258
Douglas Gregor745e6f12012-10-19 00:38:02 +00001259 // Turn the file name into an absolute path, if it isn't already.
1260 const char *Filename = Cache->OrigEntry->getName();
1261 SmallString<128> FilePath(Filename);
1262
1263 // Ask the file manager to fixup the relative path for us. This will
1264 // honor the working directory.
1265 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1266
1267 // FIXME: This call to make_absolute shouldn't be necessary, the
1268 // call to FixupRelativePath should always return an absolute path.
1269 llvm::sys::fs::make_absolute(FilePath);
1270 Filename = FilePath.c_str();
1271
1272 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1273
1274 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1275 }
1276
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001277 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001278
1279 // Create input file offsets abbreviation.
1280 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1281 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1282 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1283 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1284 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1285
1286 // Write input file offsets.
1287 Record.clear();
1288 Record.push_back(INPUT_FILE_OFFSETS);
1289 Record.push_back(InputFileOffsets.size());
1290 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001291}
1292
Douglas Gregor14f79002009-04-10 03:52:48 +00001293//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001294// stat cache Serialization
1295//===----------------------------------------------------------------------===//
1296
1297namespace {
1298// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001299class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001300public:
1301 typedef const char * key_type;
1302 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chris Lattner74e976b2010-11-23 19:28:12 +00001304 typedef struct stat data_type;
1305 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001306
1307 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001308 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
1311 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001312 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001313 data_type_ref Data) {
1314 unsigned StrLen = strlen(path);
1315 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001316 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001317 clang::io::Emit8(Out, DataLen);
1318 return std::make_pair(StrLen + 1, DataLen);
1319 }
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Chris Lattner5f9e2722011-07-23 10:55:15 +00001321 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001322 Out.write(path, KeyLen);
1323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Chris Lattner5f9e2722011-07-23 10:55:15 +00001325 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001326 data_type_ref Data, unsigned DataLen) {
1327 using namespace clang::io;
1328 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001329
Chris Lattner74e976b2010-11-23 19:28:12 +00001330 Emit32(Out, (uint32_t) Data.st_ino);
1331 Emit32(Out, (uint32_t) Data.st_dev);
1332 Emit16(Out, (uint16_t) Data.st_mode);
1333 Emit64(Out, (uint64_t) Data.st_mtime);
1334 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001335
1336 assert(Out.tell() - Start == DataLen && "Wrong data length");
1337 }
1338};
1339} // end anonymous namespace
1340
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001341//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001342// Source Manager Serialization
1343//===----------------------------------------------------------------------===//
1344
1345/// \brief Create an abbreviation for the SLocEntry that refers to a
1346/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001347static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001348 using namespace llvm;
1349 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001350 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1352 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001355 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001360 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001361}
1362
1363/// \brief Create an abbreviation for the SLocEntry that refers to a
1364/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001365static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001366 using namespace llvm;
1367 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001368 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001369 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1370 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1371 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001374 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001375}
1376
1377/// \brief Create an abbreviation for the SLocEntry that refers to a
1378/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001379static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001380 using namespace llvm;
1381 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001382 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001384 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001385}
1386
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001387/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1388/// expansion.
1389static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001390 using namespace llvm;
1391 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001392 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001393 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1394 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001398 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001399}
1400
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001401namespace {
1402 // Trait used for the on-disk hash table of header search information.
1403 class HeaderFileInfoTrait {
1404 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001405
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001406 // Keep track of the framework names we've used during serialization.
1407 SmallVector<char, 128> FrameworkStringData;
1408 llvm::StringMap<unsigned> FrameworkNameOffset;
1409
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001410 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001411 HeaderFileInfoTrait(ASTWriter &Writer)
1412 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001413
1414 typedef const char *key_type;
1415 typedef key_type key_type_ref;
1416
1417 typedef HeaderFileInfo data_type;
1418 typedef const data_type &data_type_ref;
1419
1420 static unsigned ComputeHash(const char *path) {
1421 // The hash is based only on the filename portion of the key, so that the
1422 // reader can match based on filenames when symlinking or excess path
1423 // elements ("foo/../", "../") change the form of the name. However,
1424 // complete path is still the key.
1425 return llvm::HashString(llvm::sys::path::filename(path));
1426 }
1427
1428 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001429 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001430 data_type_ref Data) {
1431 unsigned StrLen = strlen(path);
1432 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001433 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001434 clang::io::Emit8(Out, DataLen);
1435 return std::make_pair(StrLen + 1, DataLen);
1436 }
1437
Chris Lattner5f9e2722011-07-23 10:55:15 +00001438 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001439 Out.write(path, KeyLen);
1440 }
1441
Chris Lattner5f9e2722011-07-23 10:55:15 +00001442 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001443 data_type_ref Data, unsigned DataLen) {
1444 using namespace clang::io;
1445 uint64_t Start = Out.tell(); (void)Start;
1446
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001447 unsigned char Flags = (Data.isImport << 5)
1448 | (Data.isPragmaOnce << 4)
1449 | (Data.DirInfo << 2)
1450 | (Data.Resolved << 1)
1451 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001452 Emit8(Out, (uint8_t)Flags);
1453 Emit16(Out, (uint16_t) Data.NumIncludes);
1454
1455 if (!Data.ControllingMacro)
1456 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1457 else
1458 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001459
1460 unsigned Offset = 0;
1461 if (!Data.Framework.empty()) {
1462 // If this header refers into a framework, save the framework name.
1463 llvm::StringMap<unsigned>::iterator Pos
1464 = FrameworkNameOffset.find(Data.Framework);
1465 if (Pos == FrameworkNameOffset.end()) {
1466 Offset = FrameworkStringData.size() + 1;
1467 FrameworkStringData.append(Data.Framework.begin(),
1468 Data.Framework.end());
1469 FrameworkStringData.push_back(0);
1470
1471 FrameworkNameOffset[Data.Framework] = Offset;
1472 } else
1473 Offset = Pos->second;
1474 }
1475 Emit32(Out, Offset);
1476
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001477 assert(Out.tell() - Start == DataLen && "Wrong data length");
1478 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001479
1480 const char *strings_begin() const { return FrameworkStringData.begin(); }
1481 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001482 };
1483} // end anonymous namespace
1484
1485/// \brief Write the header search block for the list of files that
1486///
1487/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001488void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001489 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001490 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1491
1492 if (FilesByUID.size() > HS.header_file_size())
1493 FilesByUID.resize(HS.header_file_size());
1494
Benjamin Kramerfacde172012-06-06 17:32:50 +00001495 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001496 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001497 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001498 unsigned NumHeaderSearchEntries = 0;
1499 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1500 const FileEntry *File = FilesByUID[UID];
1501 if (!File)
1502 continue;
1503
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001504 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1505 // from the external source if it was not provided already.
1506 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001507 if (HFI.External && Chain)
1508 continue;
1509
1510 // Turn the file name into an absolute path, if it isn't already.
1511 const char *Filename = File->getName();
1512 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1513
1514 // If we performed any translation on the file name at all, we need to
1515 // save this string, since the generator will refer to it later.
1516 if (Filename != File->getName()) {
1517 Filename = strdup(Filename);
1518 SavedStrings.push_back(Filename);
1519 }
1520
1521 Generator.insert(Filename, HFI, GeneratorTrait);
1522 ++NumHeaderSearchEntries;
1523 }
1524
1525 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001526 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001527 uint32_t BucketOffset;
1528 {
1529 llvm::raw_svector_ostream Out(TableData);
1530 // Make sure that no bucket is at offset 0
1531 clang::io::Emit32(Out, 0);
1532 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1533 }
1534
1535 // Create a blob abbreviation
1536 using namespace llvm;
1537 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1538 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001542 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1543 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1544
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001545 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001546 RecordData Record;
1547 Record.push_back(HEADER_SEARCH_TABLE);
1548 Record.push_back(BucketOffset);
1549 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001550 Record.push_back(TableData.size());
1551 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001552 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1553
1554 // Free all of the strings we had to duplicate.
1555 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001556 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001557}
1558
Douglas Gregor14f79002009-04-10 03:52:48 +00001559/// \brief Writes the block containing the serialized form of the
1560/// source manager.
1561///
1562/// TODO: We should probably use an on-disk hash table (stored in a
1563/// blob), indexed based on the file name, so that we only create
1564/// entries for files that we actually need. In the common case (no
1565/// errors), we probably won't have to create file entries for any of
1566/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001567void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001568 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001569 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570 RecordData Record;
1571
Chris Lattnerf04ad692009-04-10 17:16:57 +00001572 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001573 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001574
1575 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001576 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1577 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1578 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001579 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001580
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001581 // Write out the source location entry table. We skip the first
1582 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001583 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001584 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001585 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1586 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001587 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001588 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001589 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001590 FileID FID = FileID::get(I);
1591 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001592
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001593 // Record the offset of this source-location entry.
1594 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1595
1596 // Figure out which record code to use.
1597 unsigned Code;
1598 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001599 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1600 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001601 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001602 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001603 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001604 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001605 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001606 Record.clear();
1607 Record.push_back(Code);
1608
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001609 // Starting offset of this entry within this module, so skip the dummy.
1610 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001611 if (SLoc->isFile()) {
1612 const SrcMgr::FileInfo &File = SLoc->getFile();
1613 Record.push_back(File.getIncludeLoc().getRawEncoding());
1614 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1615 Record.push_back(File.hasLineDirectives());
1616
1617 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001618 if (Content->OrigEntry) {
1619 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001620 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001621
Douglas Gregora930dc92012-10-22 18:42:04 +00001622 // The source location entry is a file. Emit input file ID.
1623 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1624 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001626 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001627
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001628 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001629 if (FDI != FileDeclIDs.end()) {
1630 Record.push_back(FDI->second->FirstDeclIndex);
1631 Record.push_back(FDI->second->DeclIDs.size());
1632 } else {
1633 Record.push_back(0);
1634 Record.push_back(0);
1635 }
Douglas Gregora081da52011-11-16 20:05:18 +00001636
Douglas Gregora930dc92012-10-22 18:42:04 +00001637 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001638
1639 if (Content->BufferOverridden) {
1640 Record.clear();
1641 Record.push_back(SM_SLOC_BUFFER_BLOB);
1642 const llvm::MemoryBuffer *Buffer
1643 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1644 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1645 StringRef(Buffer->getBufferStart(),
1646 Buffer->getBufferSize() + 1));
1647 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001648 } else {
1649 // The source location entry is a buffer. The blob associated
1650 // with this entry contains the contents of the buffer.
1651
1652 // We add one to the size so that we capture the trailing NULL
1653 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1654 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001655 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001656 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001657 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001658 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001659 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001660 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001661 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001662 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001663 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001664 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001665
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001666 if (strcmp(Name, "<built-in>") == 0) {
1667 PreloadSLocs.push_back(SLocEntryOffsets.size());
1668 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001669 }
1670 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001671 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001672 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001673 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1674 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001675 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1676 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001677
1678 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001679 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001680 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001681 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001682 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001683 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001684 }
1685 }
1686
Douglas Gregorc9490c02009-04-16 22:23:12 +00001687 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688
1689 if (SLocEntryOffsets.empty())
1690 return;
1691
Sebastian Redl3397c552010-08-18 23:56:27 +00001692 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001693 // table is used for lazily loading source-location information.
1694 using namespace llvm;
1695 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001696 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1700 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001702 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001703 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001704 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001705 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001706 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001707
Sebastian Redl3397c552010-08-18 23:56:27 +00001708 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001709 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001710 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001711
1712 // Write the line table. It depends on remapping working, so it must come
1713 // after the source location offsets.
1714 if (SourceMgr.hasLineTable()) {
1715 LineTableInfo &LineTable = SourceMgr.getLineTable();
1716
1717 Record.clear();
1718 // Emit the file names
1719 Record.push_back(LineTable.getNumFilenames());
1720 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1721 // Emit the file name
1722 const char *Filename = LineTable.getFilename(I);
1723 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1724 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1725 Record.push_back(FilenameLen);
1726 if (FilenameLen)
1727 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1728 }
1729
1730 // Emit the line entries
1731 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1732 L != LEnd; ++L) {
1733 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001734 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001735 continue;
1736
1737 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001738 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001739
1740 // Emit the line entries
1741 Record.push_back(L->second.size());
1742 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1743 LEEnd = L->second.end();
1744 LE != LEEnd; ++LE) {
1745 Record.push_back(LE->FileOffset);
1746 Record.push_back(LE->LineNo);
1747 Record.push_back(LE->FilenameID);
1748 Record.push_back((unsigned)LE->FileKind);
1749 Record.push_back(LE->IncludeOffset);
1750 }
1751 }
1752 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1753 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001754}
1755
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001756//===----------------------------------------------------------------------===//
1757// Preprocessor Serialization
1758//===----------------------------------------------------------------------===//
1759
Douglas Gregor9c736102011-02-10 18:20:09 +00001760static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1761 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1762 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1763 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1764 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1765 return X.first->getName().compare(Y.first->getName());
1766}
1767
Chris Lattner0b1fb982009-04-10 17:15:23 +00001768/// \brief Writes the block containing the serialized form of the
1769/// preprocessor.
1770///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001771void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001772 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1773 if (PPRec)
1774 WritePreprocessorDetail(*PPRec);
1775
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001776 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001777
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001778 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1779 if (PP.getCounterValue() != 0) {
1780 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001781 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001782 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001783 }
1784
1785 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001786 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Sebastian Redl3397c552010-08-18 23:56:27 +00001788 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001789 // FIXME: use diagnostics subsystem for localization etc.
1790 if (PP.SawDateOrTime())
1791 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Douglas Gregorecdcb882010-10-20 22:00:55 +00001793
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001794 // Loop over all the macro definitions that are live at the end of the file,
1795 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001796
Douglas Gregor9c736102011-02-10 18:20:09 +00001797 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001798 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001799 MacrosToEmit;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001800 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001801 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001802 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001803 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001804 if (!IsModule || I->second->isPublic()) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001805 MacroDefinitionsSeen.insert(I->first);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001806 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001807 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001808 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001809
Douglas Gregor9c736102011-02-10 18:20:09 +00001810 // Sort the set of macro definitions that need to be serialized by the
1811 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001812 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001813 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001814
Douglas Gregora8235d62012-10-09 23:05:51 +00001815 /// \brief Offsets of each of the macros into the bitstream, indexed by
1816 /// the local macro ID
1817 ///
1818 /// For each identifier that is associated with a macro, this map
1819 /// provides the offset into the bitstream where that macro is
1820 /// defined.
1821 std::vector<uint32_t> MacroOffsets;
1822
Douglas Gregor9c736102011-02-10 18:20:09 +00001823 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1824 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001825
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00001826 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1827 MI = MI->getPreviousDefinition()) {
Douglas Gregora8235d62012-10-09 23:05:51 +00001828 MacroID ID = getMacroRef(MI);
1829 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001830 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Douglas Gregora8235d62012-10-09 23:05:51 +00001832 // Skip macros from a AST file if we're chaining.
1833 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1834 continue;
1835
1836 if (ID < FirstMacroID) {
1837 // This will have been dealt with via an update record.
1838 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1839 continue;
1840 }
1841
1842 // Record the local offset of this macro.
1843 unsigned Index = ID - FirstMacroID;
1844 if (Index == MacroOffsets.size())
1845 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1846 else {
1847 if (Index > MacroOffsets.size())
1848 MacroOffsets.resize(Index + 1);
1849
1850 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1851 }
1852
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001853 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001854 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001855 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001856 AddSourceLocation(MI->getDefinitionLoc(), Record);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001857 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001858 AddSourceLocation(MI->getUndefLoc(), Record);
1859 Record.push_back(MI->isUsed());
1860 Record.push_back(MI->isPublic());
1861 AddSourceLocation(MI->getVisibilityLocation(), Record);
1862 unsigned Code;
1863 if (MI->isObjectLike()) {
1864 Code = PP_MACRO_OBJECT_LIKE;
1865 } else {
1866 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001867
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001868 Record.push_back(MI->isC99Varargs());
1869 Record.push_back(MI->isGNUVarargs());
Eli Friedman4fa4b482012-11-14 02:18:46 +00001870 Record.push_back(MI->hasCommaPasting());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001871 Record.push_back(MI->getNumArgs());
1872 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1873 I != E; ++I)
1874 AddIdentifierRef(*I, Record);
1875 }
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001877 // If we have a detailed preprocessing record, record the macro definition
1878 // ID that corresponds to this macro.
1879 if (PPRec)
1880 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1881
1882 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001883 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001884
1885 // Emit the tokens array.
1886 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1887 // Note that we know that the preprocessor does not have any annotation
1888 // tokens in it because they are created by the parser, and thus can't
1889 // be in a macro definition.
1890 const Token &Tok = MI->getReplacementToken(TokNo);
1891
1892 Record.push_back(Tok.getLocation().getRawEncoding());
1893 Record.push_back(Tok.getLength());
1894
1895 // FIXME: When reading literal tokens, reconstruct the literal pointer
1896 // if it is needed.
1897 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1898 // FIXME: Should translate token kind to a stable encoding.
1899 Record.push_back(Tok.getKind());
1900 // FIXME: Should translate token flags to a stable encoding.
1901 Record.push_back(Tok.getFlags());
1902
1903 Stream.EmitRecord(PP_TOKEN, Record);
1904 Record.clear();
1905 }
1906 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001907 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001908 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001909 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001910
1911 // Write the offsets table for macro IDs.
1912 using namespace llvm;
1913 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1914 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1918
1919 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1920 Record.clear();
1921 Record.push_back(MACRO_OFFSET);
1922 Record.push_back(MacroOffsets.size());
1923 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1924 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1925 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001926}
1927
1928void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001929 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001930 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001931
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001932 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001933
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001934 // Enter the preprocessor block.
1935 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001936
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001937 // If the preprocessor has a preprocessing record, emit it.
1938 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001939 using namespace llvm;
1940
1941 // Set up the abbreviation for
1942 unsigned InclusionAbbrev = 0;
1943 {
1944 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1945 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1951 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1952 }
1953
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001954 unsigned FirstPreprocessorEntityID
1955 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1956 + NUM_PREDEF_PP_ENTITY_IDS;
1957 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001958 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001959 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1960 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001961 E != EEnd;
1962 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001963 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001964
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001965 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1966 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001967
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001968 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001969 // Record this macro definition's ID.
1970 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001971
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001972 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001973 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1974 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001975 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001976
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001977 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001978 Record.push_back(ME->isBuiltinMacro());
1979 if (ME->isBuiltinMacro())
1980 AddIdentifierRef(ME->getName(), Record);
1981 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001982 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001983 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001984 continue;
1985 }
1986
1987 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1988 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001989 Record.push_back(ID->getFileName().size());
1990 Record.push_back(ID->wasInQuotes());
1991 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001992 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001993 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001994 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001995 // Check that the FileEntry is not null because it was not resolved and
1996 // we create a PCH even with compiler errors.
1997 if (ID->getFile())
1998 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001999 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2000 continue;
2001 }
2002
2003 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2004 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002005 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002006
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002007 // Write the offsets table for the preprocessing record.
2008 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002009 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2010
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002011 // Write the offsets table for identifier IDs.
2012 using namespace llvm;
2013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002014 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002017 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002018
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002019 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002020 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002021 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002022 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2023 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002024 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002025}
2026
Douglas Gregore209e502011-12-06 01:10:29 +00002027unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2028 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2029 if (Known != SubmoduleIDs.end())
2030 return Known->second;
2031
2032 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2033}
2034
Douglas Gregor26ced122011-12-01 00:59:36 +00002035/// \brief Compute the number of modules within the given tree (including the
2036/// given module).
2037static unsigned getNumberOfModules(Module *Mod) {
2038 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002039 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2040 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002041 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002042 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002043
2044 return ChildModules + 1;
2045}
2046
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002047void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002048 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002049 // FIXME: This feels like it belongs somewhere else, but there are no
2050 // other consumers of this information.
2051 SourceManager &SrcMgr = PP->getSourceManager();
2052 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2053 for (ASTContext::import_iterator I = Context->local_import_begin(),
2054 IEnd = Context->local_import_end();
2055 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002056 if (Module *ImportedFrom
2057 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2058 SrcMgr))) {
2059 ImportedFrom->Imports.push_back(I->getImportedModule());
2060 }
2061 }
2062
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002063 // Enter the submodule description block.
2064 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2065
2066 // Write the abbreviations needed for the submodules block.
2067 using namespace llvm;
2068 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2069 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002076 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002077 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002078 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2079 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2080
2081 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002082 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2084 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2085
2086 Abbrev = new BitCodeAbbrev();
2087 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2089 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002090
2091 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002092 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2094 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2095
2096 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002097 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2099 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2100
Douglas Gregor51f564f2011-12-31 04:05:44 +00002101 Abbrev = new BitCodeAbbrev();
2102 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2104 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2105
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002106 Abbrev = new BitCodeAbbrev();
2107 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2108 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2109 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2110
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002111 Abbrev = new BitCodeAbbrev();
2112 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2114 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2115 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2116
Douglas Gregor26ced122011-12-01 00:59:36 +00002117 // Write the submodule metadata block.
2118 RecordData Record;
2119 Record.push_back(getNumberOfModules(WritingModule));
2120 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2121 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2122
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002123 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002124 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002125 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002126 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002127 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002128 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002129 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002130
2131 // Emit the definition of the block.
2132 Record.clear();
2133 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002134 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002135 if (Mod->Parent) {
2136 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2137 Record.push_back(SubmoduleIDs[Mod->Parent]);
2138 } else {
2139 Record.push_back(0);
2140 }
2141 Record.push_back(Mod->IsFramework);
2142 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002143 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002144 Record.push_back(Mod->InferSubmodules);
2145 Record.push_back(Mod->InferExplicitSubmodules);
2146 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002147 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2148
Douglas Gregor51f564f2011-12-31 04:05:44 +00002149 // Emit the requirements.
2150 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2151 Record.clear();
2152 Record.push_back(SUBMODULE_REQUIRES);
2153 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2154 Mod->Requires[I].data(),
2155 Mod->Requires[I].size());
2156 }
2157
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002158 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002159 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002160 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002161 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002162 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002163 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002164 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2165 Record.clear();
2166 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2167 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2168 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002169 }
2170
2171 // Emit the headers.
2172 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2173 Record.clear();
2174 Record.push_back(SUBMODULE_HEADER);
2175 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2176 Mod->Headers[I]->getName());
2177 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002178 // Emit the excluded headers.
2179 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2180 Record.clear();
2181 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2182 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2183 Mod->ExcludedHeaders[I]->getName());
2184 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002185 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2186 Record.clear();
2187 Record.push_back(SUBMODULE_TOPHEADER);
2188 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2189 Mod->TopHeaders[I]->getName());
2190 }
Douglas Gregor55988682011-12-05 16:33:54 +00002191
2192 // Emit the imports.
2193 if (!Mod->Imports.empty()) {
2194 Record.clear();
2195 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002196 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002197 assert(ImportedID && "Unknown submodule!");
2198 Record.push_back(ImportedID);
2199 }
2200 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2201 }
2202
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002203 // Emit the exports.
2204 if (!Mod->Exports.empty()) {
2205 Record.clear();
2206 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002207 if (Module *Exported = Mod->Exports[I].getPointer()) {
2208 unsigned ExportedID = SubmoduleIDs[Exported];
2209 assert(ExportedID > 0 && "Unknown submodule ID?");
2210 Record.push_back(ExportedID);
2211 } else {
2212 Record.push_back(0);
2213 }
2214
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002215 Record.push_back(Mod->Exports[I].getInt());
2216 }
2217 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2218 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002219
2220 // Emit the link libraries.
2221 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2222 Record.clear();
2223 Record.push_back(SUBMODULE_LINK_LIBRARY);
2224 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2225 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2226 Mod->LinkLibraries[I].Library);
2227 }
2228
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002229 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002230 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2231 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002232 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002233 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002234 }
2235
2236 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002237
2238 assert((NextSubmoduleID - FirstSubmoduleID
2239 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002240}
2241
Douglas Gregor185dbd72011-12-01 02:07:58 +00002242serialization::SubmoduleID
2243ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002244 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002245 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002246
2247 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002248 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002249 Module *OwningMod
2250 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002251 if (!OwningMod)
2252 return 0;
2253
Douglas Gregore209e502011-12-06 01:10:29 +00002254 // Check whether this submodule is part of our own module.
2255 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002256 return 0;
2257
Douglas Gregore209e502011-12-06 01:10:29 +00002258 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002259}
2260
David Blaikied6471f72011-09-25 23:23:43 +00002261void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002262 // FIXME: Make it work properly with modules.
2263 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2264 DiagStateIDMap;
2265 unsigned CurrID = 0;
2266 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002267 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002268 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002269 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2270 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002271 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002272 if (point.Loc.isInvalid())
2273 continue;
2274
2275 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002276 unsigned &DiagStateID = DiagStateIDMap[point.State];
2277 Record.push_back(DiagStateID);
2278
2279 if (DiagStateID == 0) {
2280 DiagStateID = ++CurrID;
2281 for (DiagnosticsEngine::DiagState::const_iterator
2282 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2283 if (I->second.isPragma()) {
2284 Record.push_back(I->first);
2285 Record.push_back(I->second.getMapping());
2286 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002287 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002288 Record.push_back(-1); // mark the end of the diag/map pairs for this
2289 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002290 }
2291 }
2292
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002293 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002294 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002295}
2296
Anders Carlssonc8505782011-03-06 18:41:18 +00002297void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2298 if (CXXBaseSpecifiersOffsets.empty())
2299 return;
2300
2301 RecordData Record;
2302
2303 // Create a blob abbreviation for the C++ base specifiers offsets.
2304 using namespace llvm;
2305
2306 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2307 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2308 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2310 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2311
Douglas Gregore92b8a12011-08-04 00:01:48 +00002312 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002313 Record.clear();
2314 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2315 Record.push_back(CXXBaseSpecifiersOffsets.size());
2316 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002317 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002318}
2319
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002320//===----------------------------------------------------------------------===//
2321// Type Serialization
2322//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002323
Sebastian Redl3397c552010-08-18 23:56:27 +00002324/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002325void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002326 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002327 if (Idx.getIndex() == 0) // we haven't seen this type before.
2328 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Douglas Gregor97475832010-10-05 18:37:06 +00002330 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002331
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002333 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002334 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002335 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002336 else if (TypeOffsets.size() < Index) {
2337 TypeOffsets.resize(Index + 1);
2338 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002339 }
2340
2341 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Douglas Gregor2cf26342009-04-09 22:27:44 +00002343 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002344 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002345
Douglas Gregora4923eb2009-11-16 21:35:15 +00002346 if (T.hasLocalNonFastQualifiers()) {
2347 Qualifiers Qs = T.getLocalQualifiers();
2348 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002349 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002350 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002351 } else {
2352 switch (T->getTypeClass()) {
2353 // For all of the concrete, non-dependent types, call the
2354 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002355#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002356 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002358#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002359 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360 }
2361
2362 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002363 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002364
2365 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002366 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002367}
2368
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002369//===----------------------------------------------------------------------===//
2370// Declaration Serialization
2371//===----------------------------------------------------------------------===//
2372
Douglas Gregor2cf26342009-04-09 22:27:44 +00002373/// \brief Write the block containing all of the declaration IDs
2374/// lexically declared within the given DeclContext.
2375///
2376/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2377/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002378uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002379 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002380 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002381 return 0;
2382
Douglas Gregorc9490c02009-04-16 22:23:12 +00002383 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002384 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002385 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002386 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002387 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2388 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002389 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002390
Douglas Gregor25123082009-04-22 22:34:57 +00002391 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002392 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002393 return Offset;
2394}
2395
Sebastian Redla4232eb2010-08-18 23:56:21 +00002396void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002397 using namespace llvm;
2398 RecordData Record;
2399
2400 // Write the type offsets array
2401 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002402 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2406 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2407 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002408 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002409 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002410 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002411 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002412
2413 // Write the declaration offsets array
2414 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002415 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2419 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2420 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002421 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002422 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002423 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002424 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002425}
2426
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002427void ASTWriter::WriteFileDeclIDsMap() {
2428 using namespace llvm;
2429 RecordData Record;
2430
2431 // Join the vectors of DeclIDs from all files.
2432 SmallVector<DeclID, 256> FileSortedIDs;
2433 for (FileDeclIDsTy::iterator
2434 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2435 DeclIDInFileInfo &Info = *FI->second;
2436 Info.FirstDeclIndex = FileSortedIDs.size();
2437 for (LocDeclIDsTy::iterator
2438 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2439 FileSortedIDs.push_back(DI->second);
2440 }
2441
2442 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2443 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2446 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2447 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002448 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002449 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2450}
2451
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002452void ASTWriter::WriteComments() {
2453 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002454 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002455 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002456 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2457 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002458 I != E; ++I) {
2459 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002460 AddSourceRange((*I)->getSourceRange(), Record);
2461 Record.push_back((*I)->getKind());
2462 Record.push_back((*I)->isTrailingComment());
2463 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002464 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2465 }
2466 Stream.ExitBlock();
2467}
2468
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002469//===----------------------------------------------------------------------===//
2470// Global Method Pool and Selector Serialization
2471//===----------------------------------------------------------------------===//
2472
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002473namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002474// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002475class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002476 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002477
2478public:
2479 typedef Selector key_type;
2480 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Sebastian Redl5d050072010-08-04 17:20:04 +00002482 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002483 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002484 ObjCMethodList Instance, Factory;
2485 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002486 typedef const data_type& data_type_ref;
2487
Sebastian Redl3397c552010-08-18 23:56:27 +00002488 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002489
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002490 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002491 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002492 }
Mike Stump1eb44332009-09-09 15:08:12 +00002493
2494 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002495 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002496 data_type_ref Methods) {
2497 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2498 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002499 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2500 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002501 Method = Method->Next)
2502 if (Method->Method)
2503 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002504 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002505 Method = Method->Next)
2506 if (Method->Method)
2507 DataLen += 4;
2508 clang::io::Emit16(Out, DataLen);
2509 return std::make_pair(KeyLen, DataLen);
2510 }
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Chris Lattner5f9e2722011-07-23 10:55:15 +00002512 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002513 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002514 assert((Start >> 32) == 0 && "Selector key offset too large");
2515 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002516 unsigned N = Sel.getNumArgs();
2517 clang::io::Emit16(Out, N);
2518 if (N == 0)
2519 N = 1;
2520 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002521 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002522 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2523 }
Mike Stump1eb44332009-09-09 15:08:12 +00002524
Chris Lattner5f9e2722011-07-23 10:55:15 +00002525 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002526 data_type_ref Methods, unsigned DataLen) {
2527 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002528 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002529 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002530 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002531 Method = Method->Next)
2532 if (Method->Method)
2533 ++NumInstanceMethods;
2534
2535 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002536 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002537 Method = Method->Next)
2538 if (Method->Method)
2539 ++NumFactoryMethods;
2540
2541 clang::io::Emit16(Out, NumInstanceMethods);
2542 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002543 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002544 Method = Method->Next)
2545 if (Method->Method)
2546 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002547 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002548 Method = Method->Next)
2549 if (Method->Method)
2550 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002551
2552 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002553 }
2554};
2555} // end anonymous namespace
2556
Sebastian Redl059612d2010-08-03 21:58:15 +00002557/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002558///
2559/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002560/// in an on-disk hash table indexed by the selector. The hash table also
2561/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002562void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002563 using namespace llvm;
2564
Sebastian Redl059612d2010-08-03 21:58:15 +00002565 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002566 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002567 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002568 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002569 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002570 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002571 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002572 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002573
Sebastian Redl059612d2010-08-03 21:58:15 +00002574 // Create the on-disk hash table representation. We walk through every
2575 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002576 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002577 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002578 I = SelectorIDs.begin(), E = SelectorIDs.end();
2579 I != E; ++I) {
2580 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002581 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002582 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002583 I->second,
2584 ObjCMethodList(),
2585 ObjCMethodList()
2586 };
2587 if (F != SemaRef.MethodPool.end()) {
2588 Data.Instance = F->second.first;
2589 Data.Factory = F->second.second;
2590 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002591 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002592 // changed.
2593 if (Chain && I->second < FirstSelectorID) {
2594 // Selector already exists. Did it change?
2595 bool changed = false;
2596 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2597 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002598 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002599 changed = true;
2600 }
2601 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2602 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002603 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002604 changed = true;
2605 }
2606 if (!changed)
2607 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002608 } else if (Data.Instance.Method || Data.Factory.Method) {
2609 // A new method pool entry.
2610 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002611 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002612 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002613 }
2614
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002615 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002616 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002617 uint32_t BucketOffset;
2618 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002619 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002620 llvm::raw_svector_ostream Out(MethodPool);
2621 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002622 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002623 BucketOffset = Generator.Emit(Out, Trait);
2624 }
2625
2626 // Create a blob abbreviation
2627 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002628 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002629 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002630 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2632 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2633
Douglas Gregor83941df2009-04-25 17:48:32 +00002634 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002635 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002636 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002637 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002638 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002639 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002640
2641 // Create a blob abbreviation for the selector table offsets.
2642 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002643 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002644 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2647 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2648
2649 // Write the selector offsets table.
2650 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002651 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002652 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002653 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002654 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002655 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002656 }
2657}
2658
Sebastian Redl3397c552010-08-18 23:56:27 +00002659/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002660void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002661 using namespace llvm;
2662 if (SemaRef.ReferencedSelectors.empty())
2663 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002664
Fariborz Jahanian32019832010-07-23 19:11:11 +00002665 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002666
Sebastian Redl3397c552010-08-18 23:56:27 +00002667 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002668 // very tricky to fix, and given that @selector shouldn't really appear in
2669 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002670 for (DenseMap<Selector, SourceLocation>::iterator S =
2671 SemaRef.ReferencedSelectors.begin(),
2672 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2673 Selector Sel = (*S).first;
2674 SourceLocation Loc = (*S).second;
2675 AddSelectorRef(Sel, Record);
2676 AddSourceLocation(Loc, Record);
2677 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002678 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002679}
2680
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002681//===----------------------------------------------------------------------===//
2682// Identifier Table Serialization
2683//===----------------------------------------------------------------------===//
2684
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002685namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002686class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002687 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002688 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002689 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002690 bool IsModule;
2691
Douglas Gregora92193e2009-04-28 21:18:29 +00002692 /// \brief Determines whether this is an "interesting" identifier
2693 /// that needs a full IdentifierInfo structure written into the hash
2694 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002695 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002696 if (II->isPoisoned() ||
2697 II->isExtensionToken() ||
2698 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002699 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002700 II->getFETokenInfo<void>())
2701 return true;
2702
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002703 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002704 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002705
2706 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2707 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002708 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002709
2710 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002711 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002712
2713 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002714 }
2715
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002716public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002717 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002718 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002719
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002720 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002721 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregoreee242f2011-10-27 09:33:13 +00002723 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2724 IdentifierResolver &IdResolver, bool IsModule)
2725 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002726
2727 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002728 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002729 }
Mike Stump1eb44332009-09-09 15:08:12 +00002730
2731 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002732 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002733 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002734 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002735 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002736 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002737 DataLen += 2; // 2 bytes for builtin ID
2738 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002739 if (hadMacroDefinition(II, Macro)) {
2740 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2741 if (Writer.getMacroRef(M) != 0)
2742 DataLen += 4;
2743 }
2744
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002745 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002746 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002747
Douglas Gregoreee242f2011-10-27 09:33:13 +00002748 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2749 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002750 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002751 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002752 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002753 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002754 // We emit the key length after the data length so that every
2755 // string is preceded by a 16-bit length. This matches the PTH
2756 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002757 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002758 return std::make_pair(KeyLen, DataLen);
2759 }
Mike Stump1eb44332009-09-09 15:08:12 +00002760
Chris Lattner5f9e2722011-07-23 10:55:15 +00002761 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002762 unsigned KeyLen) {
2763 // Record the location of the key data. This is used when generating
2764 // the mapping from persistent IDs to strings.
2765 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002766 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002767 }
Mike Stump1eb44332009-09-09 15:08:12 +00002768
Douglas Gregor7143aab2011-09-01 17:04:32 +00002769 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002770 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002771 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002772 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002773 clang::io::Emit32(Out, ID << 1);
2774 return;
2775 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002776
Douglas Gregora92193e2009-04-28 21:18:29 +00002777 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002778 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2779 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2780 clang::io::Emit16(Out, Bits);
2781 Bits = 0;
2782 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002783 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002784 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2785 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002786 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002787 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002788 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002789
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002790 if (HadMacroDefinition) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002791 // Write all of the macro IDs associated with this identifier.
2792 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2793 if (MacroID ID = Writer.getMacroRef(M))
2794 clang::io::Emit32(Out, ID);
2795 }
2796
2797 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002798 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002799
Douglas Gregor668c1a42009-04-21 22:25:48 +00002800 // Emit the declaration IDs in reverse order, because the
2801 // IdentifierResolver provides the declarations as they would be
2802 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002803 // "stat"), but the ASTReader adds declarations to the end of the list
2804 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002805 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002806 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2807 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002808 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002809 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002810 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002811 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002812 }
2813};
2814} // end anonymous namespace
2815
Sebastian Redl3397c552010-08-18 23:56:27 +00002816/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002817///
2818/// The identifier table consists of a blob containing string data
2819/// (the actual identifiers themselves) and a separate "offsets" index
2820/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002821void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2822 IdentifierResolver &IdResolver,
2823 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002824 using namespace llvm;
2825
2826 // Create and write out the blob that contains the identifier
2827 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002828 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002829 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002830 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002831
Douglas Gregor92b059e2009-04-28 20:33:11 +00002832 // Look for any identifiers that were named while processing the
2833 // headers, but are otherwise not needed. We add these to the hash
2834 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002835 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002836 // file.
2837 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2838 IDEnd = PP.getIdentifierTable().end();
2839 ID != IDEnd; ++ID)
2840 getIdentifierRef(ID->second);
2841
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002842 // Create the on-disk hash table representation. We only store offsets
2843 // for identifiers that appear here for the first time.
2844 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002845 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002846 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2847 ID != IDEnd; ++ID) {
2848 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002849 if (!Chain || !ID->first->isFromAST() ||
2850 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002851 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2852 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002853 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002854
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002855 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002856 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002857 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002858 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002859 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002860 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002861 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002862 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002863 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002864 }
2865
2866 // Create a blob abbreviation
2867 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002868 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002869 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002871 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002872
2873 // Write the identifier table
2874 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002875 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002876 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002877 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002878 }
2879
2880 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002881 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002882 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2886 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2887
2888 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002889 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002890 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002891 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002892 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002893 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002894}
2895
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002896//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002897// DeclContext's Name Lookup Table Serialization
2898//===----------------------------------------------------------------------===//
2899
2900namespace {
2901// Trait used for the on-disk hash table used in the method pool.
2902class ASTDeclContextNameLookupTrait {
2903 ASTWriter &Writer;
2904
2905public:
2906 typedef DeclarationName key_type;
2907 typedef key_type key_type_ref;
2908
2909 typedef DeclContext::lookup_result data_type;
2910 typedef const data_type& data_type_ref;
2911
2912 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2913
2914 unsigned ComputeHash(DeclarationName Name) {
2915 llvm::FoldingSetNodeID ID;
2916 ID.AddInteger(Name.getNameKind());
2917
2918 switch (Name.getNameKind()) {
2919 case DeclarationName::Identifier:
2920 ID.AddString(Name.getAsIdentifierInfo()->getName());
2921 break;
2922 case DeclarationName::ObjCZeroArgSelector:
2923 case DeclarationName::ObjCOneArgSelector:
2924 case DeclarationName::ObjCMultiArgSelector:
2925 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2926 break;
2927 case DeclarationName::CXXConstructorName:
2928 case DeclarationName::CXXDestructorName:
2929 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002930 break;
2931 case DeclarationName::CXXOperatorName:
2932 ID.AddInteger(Name.getCXXOverloadedOperator());
2933 break;
2934 case DeclarationName::CXXLiteralOperatorName:
2935 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2936 case DeclarationName::CXXUsingDirective:
2937 break;
2938 }
2939
2940 return ID.ComputeHash();
2941 }
2942
2943 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002944 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002945 data_type_ref Lookup) {
2946 unsigned KeyLen = 1;
2947 switch (Name.getNameKind()) {
2948 case DeclarationName::Identifier:
2949 case DeclarationName::ObjCZeroArgSelector:
2950 case DeclarationName::ObjCOneArgSelector:
2951 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002952 case DeclarationName::CXXLiteralOperatorName:
2953 KeyLen += 4;
2954 break;
2955 case DeclarationName::CXXOperatorName:
2956 KeyLen += 1;
2957 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002958 case DeclarationName::CXXConstructorName:
2959 case DeclarationName::CXXDestructorName:
2960 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002961 case DeclarationName::CXXUsingDirective:
2962 break;
2963 }
2964 clang::io::Emit16(Out, KeyLen);
2965
2966 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00002967 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002968 clang::io::Emit16(Out, DataLen);
2969
2970 return std::make_pair(KeyLen, DataLen);
2971 }
2972
Chris Lattner5f9e2722011-07-23 10:55:15 +00002973 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002974 using namespace clang::io;
2975
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002976 Emit8(Out, Name.getNameKind());
2977 switch (Name.getNameKind()) {
2978 case DeclarationName::Identifier:
2979 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002980 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002981 case DeclarationName::ObjCZeroArgSelector:
2982 case DeclarationName::ObjCOneArgSelector:
2983 case DeclarationName::ObjCMultiArgSelector:
2984 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002985 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002986 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002987 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2988 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002989 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002990 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002991 case DeclarationName::CXXLiteralOperatorName:
2992 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002993 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002994 case DeclarationName::CXXConstructorName:
2995 case DeclarationName::CXXDestructorName:
2996 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002997 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002998 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002999 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003000
3001 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003002 }
3003
Chris Lattner5f9e2722011-07-23 10:55:15 +00003004 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003005 data_type Lookup, unsigned DataLen) {
3006 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00003007 clang::io::Emit16(Out, Lookup.size());
3008 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
3009 I != E; ++I)
3010 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003011
3012 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3013 }
3014};
3015} // end anonymous namespace
3016
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003017/// \brief Write the block containing all of the declaration IDs
3018/// visible from the given DeclContext.
3019///
3020/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003021/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003022uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3023 DeclContext *DC) {
3024 if (DC->getPrimaryContext() != DC)
3025 return 0;
3026
3027 // Since there is no name lookup into functions or methods, don't bother to
3028 // build a visible-declarations table for these entities.
3029 if (DC->isFunctionOrMethod())
3030 return 0;
3031
3032 // If not in C++, we perform name lookup for the translation unit via the
3033 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3034 // FIXME: In C++ we need the visible declarations in order to "see" the
3035 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003036 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003037 return 0;
3038
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003039 // Serialize the contents of the mapping used for lookup. Note that,
3040 // although we have two very different code paths, the serialized
3041 // representation is the same for both cases: a declaration name,
3042 // followed by a size, followed by references to the visible
3043 // declarations that have that name.
3044 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003045 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003046 if (!Map || Map->empty())
3047 return 0;
3048
3049 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3050 ASTDeclContextNameLookupTrait Trait(*this);
3051
3052 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003053 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003054 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003055 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3056 D != DEnd; ++D) {
3057 DeclarationName Name = D->first;
3058 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003059 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003060 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3061 // Hash all conversion function names to the same name. The actual
3062 // type information in conversion function name is not used in the
3063 // key (since such type information is not stable across different
3064 // modules), so the intended effect is to coalesce all of the conversion
3065 // functions under a single key.
3066 if (!ConversionName)
3067 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003068 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003069 continue;
3070 }
3071
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003072 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003073 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003074 }
3075
Douglas Gregore5a54b62011-08-30 20:49:19 +00003076 // Add the conversion functions
3077 if (!ConversionDecls.empty()) {
3078 Generator.insert(ConversionName,
3079 DeclContext::lookup_result(ConversionDecls.begin(),
3080 ConversionDecls.end()),
3081 Trait);
3082 }
3083
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003084 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003085 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003086 uint32_t BucketOffset;
3087 {
3088 llvm::raw_svector_ostream Out(LookupTable);
3089 // Make sure that no bucket is at offset 0
3090 clang::io::Emit32(Out, 0);
3091 BucketOffset = Generator.Emit(Out, Trait);
3092 }
3093
3094 // Write the lookup table
3095 RecordData Record;
3096 Record.push_back(DECL_CONTEXT_VISIBLE);
3097 Record.push_back(BucketOffset);
3098 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3099 LookupTable.str());
3100
3101 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3102 ++NumVisibleDeclContexts;
3103 return Offset;
3104}
3105
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003106/// \brief Write an UPDATE_VISIBLE block for the given context.
3107///
3108/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3109/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003110/// (in C++), for namespaces, and for classes with forward-declared unscoped
3111/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003112void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003113 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3114 if (!Map || Map->empty())
3115 return;
3116
3117 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3118 ASTDeclContextNameLookupTrait Trait(*this);
3119
3120 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003121 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3122 D != DEnd; ++D) {
3123 DeclarationName Name = D->first;
3124 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003125 // For any name that appears in this table, the results are complete, i.e.
3126 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003127 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003128 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003129 }
3130
3131 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003132 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003133 uint32_t BucketOffset;
3134 {
3135 llvm::raw_svector_ostream Out(LookupTable);
3136 // Make sure that no bucket is at offset 0
3137 clang::io::Emit32(Out, 0);
3138 BucketOffset = Generator.Emit(Out, Trait);
3139 }
3140
3141 // Write the lookup table
3142 RecordData Record;
3143 Record.push_back(UPDATE_VISIBLE);
3144 Record.push_back(getDeclID(cast<Decl>(DC)));
3145 Record.push_back(BucketOffset);
3146 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3147}
3148
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003149/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3150void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3151 RecordData Record;
3152 Record.push_back(Opts.fp_contract);
3153 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3154}
3155
3156/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3157void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003158 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003159 return;
3160
3161 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3162 RecordData Record;
3163#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3164#include "clang/Basic/OpenCLExtensions.def"
3165 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3166}
3167
Douglas Gregor2171bf12012-01-15 16:58:34 +00003168void ASTWriter::WriteRedeclarations() {
3169 RecordData LocalRedeclChains;
3170 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3171
3172 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3173 Decl *First = Redeclarations[I];
3174 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3175
3176 Decl *MostRecent = First->getMostRecentDecl();
3177
3178 // If we only have a single declaration, there is no point in storing
3179 // a redeclaration chain.
3180 if (First == MostRecent)
3181 continue;
3182
3183 unsigned Offset = LocalRedeclChains.size();
3184 unsigned Size = 0;
3185 LocalRedeclChains.push_back(0); // Placeholder for the size.
3186
3187 // Collect the set of local redeclarations of this declaration.
3188 for (Decl *Prev = MostRecent; Prev != First;
3189 Prev = Prev->getPreviousDecl()) {
3190 if (!Prev->isFromASTFile()) {
3191 AddDeclRef(Prev, LocalRedeclChains);
3192 ++Size;
3193 }
3194 }
3195 LocalRedeclChains[Offset] = Size;
3196
3197 // Reverse the set of local redeclarations, so that we store them in
3198 // order (since we found them in reverse order).
3199 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3200
3201 // Add the mapping from the first ID to the set of local declarations.
3202 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3203 LocalRedeclsMap.push_back(Info);
3204
3205 assert(N == Redeclarations.size() &&
3206 "Deserialized a declaration we shouldn't have");
3207 }
3208
3209 if (LocalRedeclChains.empty())
3210 return;
3211
3212 // Sort the local redeclarations map by the first declaration ID,
3213 // since the reader will be performing binary searches on this information.
3214 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3215
3216 // Emit the local redeclarations map.
3217 using namespace llvm;
3218 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3219 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3222 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3223
3224 RecordData Record;
3225 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3226 Record.push_back(LocalRedeclsMap.size());
3227 Stream.EmitRecordWithBlob(AbbrevID, Record,
3228 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3229 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3230
3231 // Emit the redeclaration chains.
3232 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3233}
3234
Douglas Gregorcff9f262012-01-27 01:47:08 +00003235void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003236 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003237 RecordData Categories;
3238
3239 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3240 unsigned Size = 0;
3241 unsigned StartIndex = Categories.size();
3242
3243 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3244
3245 // Allocate space for the size.
3246 Categories.push_back(0);
3247
3248 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003249 for (ObjCInterfaceDecl::known_categories_iterator
3250 Cat = Class->known_categories_begin(),
3251 CatEnd = Class->known_categories_end();
3252 Cat != CatEnd; ++Cat, ++Size) {
3253 assert(getDeclID(*Cat) != 0 && "Bogus category");
3254 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003255 }
3256
3257 // Update the size.
3258 Categories[StartIndex] = Size;
3259
3260 // Record this interface -> category map.
3261 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3262 CategoriesMap.push_back(CatInfo);
3263 }
3264
3265 // Sort the categories map by the definition ID, since the reader will be
3266 // performing binary searches on this information.
3267 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3268
3269 // Emit the categories map.
3270 using namespace llvm;
3271 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3272 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3275 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3276
3277 RecordData Record;
3278 Record.push_back(OBJC_CATEGORIES_MAP);
3279 Record.push_back(CategoriesMap.size());
3280 Stream.EmitRecordWithBlob(AbbrevID, Record,
3281 reinterpret_cast<char*>(CategoriesMap.data()),
3282 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3283
3284 // Emit the category lists.
3285 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3286}
3287
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003288void ASTWriter::WriteMergedDecls() {
3289 if (!Chain || Chain->MergedDecls.empty())
3290 return;
3291
3292 RecordData Record;
3293 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3294 IEnd = Chain->MergedDecls.end();
3295 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003296 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003297 : getDeclID(I->first);
3298 assert(CanonID && "Merged declaration not known?");
3299
3300 Record.push_back(CanonID);
3301 Record.push_back(I->second.size());
3302 Record.append(I->second.begin(), I->second.end());
3303 }
3304 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3305}
3306
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003307//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003308// General Serialization Routines
3309//===----------------------------------------------------------------------===//
3310
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003311/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003312void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3313 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003314 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003315 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3316 e = Attrs.end(); i != e; ++i){
3317 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003318 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003319 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003320
Sean Huntcf807c42010-08-18 23:23:40 +00003321#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003322
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003323 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003324}
3325
Chris Lattner5f9e2722011-07-23 10:55:15 +00003326void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003327 Record.push_back(Str.size());
3328 Record.insert(Record.end(), Str.begin(), Str.end());
3329}
3330
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003331void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3332 RecordDataImpl &Record) {
3333 Record.push_back(Version.getMajor());
3334 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3335 Record.push_back(*Minor + 1);
3336 else
3337 Record.push_back(0);
3338 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3339 Record.push_back(*Subminor + 1);
3340 else
3341 Record.push_back(0);
3342}
3343
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003344/// \brief Note that the identifier II occurs at the given offset
3345/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003346void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003347 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003348 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003349 // up earlier in the chain and thus don't need an offset.
3350 if (ID >= FirstIdentID)
3351 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003352}
3353
Douglas Gregor83941df2009-04-25 17:48:32 +00003354/// \brief Note that the selector Sel occurs at the given offset
3355/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003356void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003357 unsigned ID = SelectorIDs[Sel];
3358 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003359 // Don't record offsets for selectors that are also available in a different
3360 // file.
3361 if (ID < FirstSelectorID)
3362 return;
3363 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003364}
3365
Sebastian Redla4232eb2010-08-18 23:56:21 +00003366ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003367 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003368 WritingAST(false), DoneWritingDeclsAndTypes(false),
3369 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003370 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003371 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003372 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3373 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003374 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3375 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003376 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003377 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003378 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003379 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003380 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003381 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003382 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3383 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3384 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003385 DeclTypedefAbbrev(0),
3386 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3387 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003388{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003389}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003390
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003391ASTWriter::~ASTWriter() {
3392 for (FileDeclIDsTy::iterator
3393 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3394 delete I->second;
3395}
3396
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003397void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003398 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003399 Module *WritingModule, StringRef isysroot,
3400 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003401 WritingAST = true;
3402
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003403 ASTHasCompilerErrors = hasErrors;
3404
Douglas Gregor2cf26342009-04-09 22:27:44 +00003405 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003406 Stream.Emit((unsigned)'C', 8);
3407 Stream.Emit((unsigned)'P', 8);
3408 Stream.Emit((unsigned)'C', 8);
3409 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003410
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003411 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003412
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003413 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003414 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003415 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003416 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003417 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003418 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003419 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003420
3421 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003422}
3423
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003424template<typename Vector>
3425static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3426 ASTWriter::RecordData &Record) {
3427 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3428 I != E; ++I) {
3429 Writer.AddDeclRef(*I, Record);
3430 }
3431}
3432
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003433void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003434 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003435 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003436 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003437 using namespace llvm;
3438
Douglas Gregorecc2c092011-12-01 22:20:10 +00003439 // Make sure that the AST reader knows to finalize itself.
3440 if (Chain)
3441 Chain->finalizeForWriting();
3442
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003443 ASTContext &Context = SemaRef.Context;
3444 Preprocessor &PP = SemaRef.PP;
3445
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003446 // Set up predefined declaration IDs.
3447 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003448 if (Context.ObjCIdDecl)
3449 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003450 if (Context.ObjCSelDecl)
3451 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003452 if (Context.ObjCClassDecl)
3453 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003454 if (Context.ObjCProtocolClassDecl)
3455 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003456 if (Context.Int128Decl)
3457 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3458 if (Context.UInt128Decl)
3459 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003460 if (Context.ObjCInstanceTypeDecl)
3461 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003462 if (Context.BuiltinVaListDecl)
3463 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3464
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003465 if (!Chain) {
3466 // Make sure that we emit IdentifierInfos (and any attached
3467 // declarations) for builtins. We don't need to do this when we're
3468 // emitting chained PCH files, because all of the builtins will be
3469 // in the original PCH file.
3470 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003471 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003472 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003473 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003474 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003475 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3476 getIdentifierRef(&Table.get(BuiltinNames[I]));
3477 }
3478
Douglas Gregoreee242f2011-10-27 09:33:13 +00003479 // If there are any out-of-date identifiers, bring them up to date.
3480 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003481 // Find out-of-date identifiers.
3482 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003483 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3484 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003485 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003486 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003487 OutOfDate.push_back(ID->second);
3488 }
3489
3490 // Update the out-of-date identifiers.
3491 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3492 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3493 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003494 }
3495
Chris Lattner63d65f82009-09-08 18:19:27 +00003496 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003497 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003498 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003499 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003500 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003501
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003502 // Build a record containing all of the file scoped decls in this file.
3503 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003504 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3505 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003506
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003507 // Build a record containing all of the delegating constructors we still need
3508 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003509 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003510 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003511
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003512 // Write the set of weak, undeclared identifiers. We always write the
3513 // entire table, since later PCH files in a PCH chain are only interested in
3514 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003515 RecordData WeakUndeclaredIdentifiers;
3516 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003517 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003518 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3519 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3520 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3521 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3522 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3523 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3524 }
3525 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003526
Richard Smith5ea6ef42013-01-10 23:43:47 +00003527 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003528 // declarations in this header file. Generally, this record will be
3529 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003530 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003531 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003532 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003533 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003534 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3535 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003536 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003537 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003538 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003539 }
3540
Douglas Gregorb81c1702009-04-27 20:06:05 +00003541 // Build a record containing all of the ext_vector declarations.
3542 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003543 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003544
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003545 // Build a record containing all of the VTable uses information.
3546 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003547 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003548 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3549 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3550 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3551 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3552 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003553 }
3554
3555 // Build a record containing all of dynamic classes declarations.
3556 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003557 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003558
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003559 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003560 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003561 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003562 I = SemaRef.PendingInstantiations.begin(),
3563 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3564 AddDeclRef(I->first, PendingInstantiations);
3565 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003566 }
3567 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3568 "There are local ones at end of translation unit!");
3569
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003570 // Build a record containing some declaration references.
3571 RecordData SemaDeclRefs;
3572 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3573 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3574 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3575 }
3576
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003577 RecordData CUDASpecialDeclRefs;
3578 if (Context.getcudaConfigureCallDecl()) {
3579 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3580 }
3581
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003582 // Build a record containing all of the known namespaces.
3583 RecordData KnownNamespaces;
3584 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3585 I = SemaRef.KnownNamespaces.begin(),
3586 IEnd = SemaRef.KnownNamespaces.end();
3587 I != IEnd; ++I) {
3588 if (!I->second)
3589 AddDeclRef(I->first, KnownNamespaces);
3590 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003591
3592 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003593 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003594
Sebastian Redl3397c552010-08-18 23:56:27 +00003595 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003596 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003597 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003598
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003599 // This is so that older clang versions, before the introduction
3600 // of the control block, can read and reject the newer PCH format.
3601 Record.clear();
3602 Record.push_back(VERSION_MAJOR);
3603 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3604
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003605 // Create a lexical update block containing all of the declarations in the
3606 // translation unit that do not come from other AST files.
3607 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3608 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3609 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3610 E = TU->noload_decls_end();
3611 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003612 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003613 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003614 }
3615
3616 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3617 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3618 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3619 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3620 Record.clear();
3621 Record.push_back(TU_UPDATE_LEXICAL);
3622 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3623 data(NewGlobalDecls));
3624
3625 // And a visible updates block for the translation unit.
3626 Abv = new llvm::BitCodeAbbrev();
3627 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3628 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3629 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3630 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3631 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3632 WriteDeclContextVisibleUpdate(TU);
3633
3634 // If the translation unit has an anonymous namespace, and we don't already
3635 // have an update block for it, write it as an update block.
3636 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3637 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3638 if (Record.empty()) {
3639 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003640 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003641 }
3642 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003643
3644 // Make sure visible decls, added to DeclContexts previously loaded from
3645 // an AST file, are registered for serialization.
3646 for (SmallVector<const Decl *, 16>::iterator
3647 I = UpdatingVisibleDecls.begin(),
3648 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3649 GetDeclRef(*I);
3650 }
3651
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003652 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003653 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003654
Douglas Gregora119da02011-08-02 16:26:37 +00003655 // Form the record of special types.
3656 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003657 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003658 AddTypeRef(Context.getFILEType(), SpecialTypes);
3659 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3660 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3661 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3662 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003663 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003664 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003665
Douglas Gregor366809a2009-04-26 03:49:13 +00003666 // Keep writing types and declarations until all types and
3667 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003668 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003669 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003670 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3671 E = DeclsToRewrite.end();
3672 I != E; ++I)
3673 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003674 while (!DeclTypesToEmit.empty()) {
3675 DeclOrType DOT = DeclTypesToEmit.front();
3676 DeclTypesToEmit.pop();
3677 if (DOT.isType())
3678 WriteType(DOT.getType());
3679 else
3680 WriteDecl(Context, DOT.getDecl());
3681 }
3682 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003683
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003684 DoneWritingDeclsAndTypes = true;
3685
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003686 WriteFileDeclIDsMap();
3687 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003688 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003689
3690 if (Chain) {
3691 // Write the mapping information describing our module dependencies and how
3692 // each of those modules were mapped into our own offset/ID space, so that
3693 // the reader can build the appropriate mapping to its own offset/ID space.
3694 // The map consists solely of a blob with the following format:
3695 // *(module-name-len:i16 module-name:len*i8
3696 // source-location-offset:i32
3697 // identifier-id:i32
3698 // preprocessed-entity-id:i32
3699 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003700 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003701 // selector-id:i32
3702 // declaration-id:i32
3703 // c++-base-specifiers-id:i32
3704 // type-id:i32)
3705 //
3706 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3707 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3709 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003710 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003711 {
3712 llvm::raw_svector_ostream Out(Buffer);
3713 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003714 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003715 M != MEnd; ++M) {
3716 StringRef FileName = (*M)->FileName;
3717 io::Emit16(Out, FileName.size());
3718 Out.write(FileName.data(), FileName.size());
3719 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3720 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003721 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003722 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003723 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003724 io::Emit32(Out, (*M)->BaseSelectorID);
3725 io::Emit32(Out, (*M)->BaseDeclID);
3726 io::Emit32(Out, (*M)->BaseTypeIndex);
3727 }
3728 }
3729 Record.clear();
3730 Record.push_back(MODULE_OFFSET_MAP);
3731 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3732 Buffer.data(), Buffer.size());
3733 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003734 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003735 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003736 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003737 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003738 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003739 WriteFPPragmaOptions(SemaRef.getFPOptions());
3740 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003741
Sebastian Redl1476ed42010-07-16 16:36:56 +00003742 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003743 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003744
Anders Carlssonc8505782011-03-06 18:41:18 +00003745 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003746
Douglas Gregore209e502011-12-06 01:10:29 +00003747 // If we're emitting a module, write out the submodule information.
3748 if (WritingModule)
3749 WriteSubmodules(WritingModule);
3750
Douglas Gregora119da02011-08-02 16:26:37 +00003751 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3752
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003753 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003754 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003755 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003756
3757 // Write the record containing tentative definitions.
3758 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003759 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003760
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003761 // Write the record containing unused file scoped decls.
3762 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003763 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003764
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003765 // Write the record containing weak undeclared identifiers.
3766 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003767 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003768 WeakUndeclaredIdentifiers);
3769
Richard Smith5ea6ef42013-01-10 23:43:47 +00003770 // Write the record containing locally-scoped extern "C" definitions.
3771 if (!LocallyScopedExternCDecls.empty())
3772 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
3773 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003774
3775 // Write the record containing ext_vector type names.
3776 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003777 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003778
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003779 // Write the record containing VTable uses information.
3780 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003781 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003782
3783 // Write the record containing dynamic classes declarations.
3784 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003785 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003786
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003787 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003788 if (!PendingInstantiations.empty())
3789 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003790
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003791 // Write the record containing declaration references of Sema.
3792 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003793 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003794
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003795 // Write the record containing CUDA-specific declaration references.
3796 if (!CUDASpecialDeclRefs.empty())
3797 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003798
3799 // Write the delegating constructors.
3800 if (!DelegatingCtorDecls.empty())
3801 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003802
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003803 // Write the known namespaces.
3804 if (!KnownNamespaces.empty())
3805 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3806
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003807 // Write the visible updates to DeclContexts.
3808 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3809 I = UpdatedDeclContexts.begin(),
3810 E = UpdatedDeclContexts.end();
3811 I != E; ++I)
3812 WriteDeclContextVisibleUpdate(*I);
3813
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003814 if (!WritingModule) {
3815 // Write the submodules that were imported, if any.
3816 RecordData ImportedModules;
3817 for (ASTContext::import_iterator I = Context.local_import_begin(),
3818 IEnd = Context.local_import_end();
3819 I != IEnd; ++I) {
3820 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3821 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3822 }
3823 if (!ImportedModules.empty()) {
3824 // Sort module IDs.
3825 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3826
3827 // Unique module IDs.
3828 ImportedModules.erase(std::unique(ImportedModules.begin(),
3829 ImportedModules.end()),
3830 ImportedModules.end());
3831
3832 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3833 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003834 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003835
3836 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003837 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003838 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003839 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003840 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003841 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003842
Douglas Gregor3e1af842009-04-17 22:13:46 +00003843 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003844 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003845 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003846 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003847 Record.push_back(NumLexicalDeclContexts);
3848 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003849 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003850 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003851}
3852
Douglas Gregora8235d62012-10-09 23:05:51 +00003853void ASTWriter::WriteMacroUpdates() {
3854 if (MacroUpdates.empty())
3855 return;
3856
3857 RecordData Record;
3858 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3859 E = MacroUpdates.end();
3860 I != E; ++I) {
3861 addMacroRef(I->first, Record);
3862 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003863 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003864 }
3865 Stream.EmitRecord(MACRO_UPDATES, Record);
3866}
3867
Douglas Gregor61c5e342011-09-17 00:05:03 +00003868/// \brief Go through the declaration update blocks and resolve declaration
3869/// pointers into declaration IDs.
3870void ASTWriter::ResolveDeclUpdatesBlocks() {
3871 for (DeclUpdateMap::iterator
3872 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3873 const Decl *D = I->first;
3874 UpdateRecord &URec = I->second;
3875
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003876 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003877 continue; // The decl will be written completely
3878
3879 unsigned Idx = 0, N = URec.size();
3880 while (Idx < N) {
3881 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003882 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3883 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3884 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3885 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3886 ++Idx;
3887 break;
3888
3889 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3890 ++Idx;
3891 break;
3892 }
3893 }
3894 }
3895}
3896
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003897void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003898 if (DeclUpdates.empty())
3899 return;
3900
3901 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003902 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003903 for (DeclUpdateMap::iterator
3904 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3905 const Decl *D = I->first;
3906 UpdateRecord &URec = I->second;
3907
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003908 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003909 continue; // The decl will be written completely,no need to store updates.
3910
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003911 uint64_t Offset = Stream.GetCurrentBitNo();
3912 Stream.EmitRecord(DECL_UPDATES, URec);
3913
3914 OffsetsRecord.push_back(GetDeclRef(D));
3915 OffsetsRecord.push_back(Offset);
3916 }
3917 Stream.ExitBlock();
3918 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3919}
3920
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003921void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003922 if (ReplacedDecls.empty())
3923 return;
3924
3925 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003926 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003927 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003928 Record.push_back(I->ID);
3929 Record.push_back(I->Offset);
3930 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003931 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003932 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003933}
3934
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003935void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003936 Record.push_back(Loc.getRawEncoding());
3937}
3938
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003939void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003940 AddSourceLocation(Range.getBegin(), Record);
3941 AddSourceLocation(Range.getEnd(), Record);
3942}
3943
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003944void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003945 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003946 const uint64_t *Words = Value.getRawData();
3947 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003948}
3949
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003950void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003951 Record.push_back(Value.isUnsigned());
3952 AddAPInt(Value, Record);
3953}
3954
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003955void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003956 AddAPInt(Value.bitcastToAPInt(), Record);
3957}
3958
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003959void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003960 Record.push_back(getIdentifierRef(II));
3961}
3962
Douglas Gregora8235d62012-10-09 23:05:51 +00003963void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3964 Record.push_back(getMacroRef(MI));
3965}
3966
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003967IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003968 if (II == 0)
3969 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003971 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003972 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003973 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003974 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003975}
3976
Douglas Gregora8235d62012-10-09 23:05:51 +00003977MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3978 // Don't emit builtin macros like __LINE__ to the AST file unless they
3979 // have been redefined by the header (in which case they are not
3980 // isBuiltinMacro).
3981 if (MI == 0 || MI->isBuiltinMacro())
3982 return 0;
3983
3984 MacroID &ID = MacroIDs[MI];
3985 if (ID == 0)
3986 ID = NextMacroID++;
3987 return ID;
3988}
3989
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003990void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003991 Record.push_back(getSelectorRef(SelRef));
3992}
3993
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003994SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003995 if (Sel.getAsOpaquePtr() == 0) {
3996 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003997 }
3998
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003999 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00004000 if (SID == 0 && Chain) {
4001 // This might trigger a ReadSelector callback, which will set the ID for
4002 // this selector.
4003 Chain->LoadSelector(Sel);
4004 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004005 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004006 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004007 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004008 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004009}
4010
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004011void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004012 AddDeclRef(Temp->getDestructor(), Record);
4013}
4014
Douglas Gregor7c789c12010-10-29 22:39:52 +00004015void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4016 CXXBaseSpecifier const *BasesEnd,
4017 RecordDataImpl &Record) {
4018 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4019 CXXBaseSpecifiersToWrite.push_back(
4020 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4021 Bases, BasesEnd));
4022 Record.push_back(NextCXXBaseSpecifiersID++);
4023}
4024
Sebastian Redla4232eb2010-08-18 23:56:21 +00004025void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004026 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004027 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004028 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004029 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004030 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004031 break;
4032 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004033 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004034 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004035 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004036 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004037 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004038 break;
4039 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004040 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004041 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004042 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004043 break;
John McCall833ca992009-10-29 08:12:44 +00004044 case TemplateArgument::Null:
4045 case TemplateArgument::Integral:
4046 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004047 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004048 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004049 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004050 break;
4051 }
4052}
4053
Sebastian Redla4232eb2010-08-18 23:56:21 +00004054void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004055 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004056 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004057
4058 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4059 bool InfoHasSameExpr
4060 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4061 Record.push_back(InfoHasSameExpr);
4062 if (InfoHasSameExpr)
4063 return; // Avoid storing the same expr twice.
4064 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004065 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4066 Record);
4067}
4068
Douglas Gregordc355712011-02-25 00:36:19 +00004069void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4070 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004071 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004072 AddTypeRef(QualType(), Record);
4073 return;
4074 }
4075
Douglas Gregordc355712011-02-25 00:36:19 +00004076 AddTypeLoc(TInfo->getTypeLoc(), Record);
4077}
4078
4079void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4080 AddTypeRef(TL.getType(), Record);
4081
John McCalla1ee0c52009-10-16 21:56:05 +00004082 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004083 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004084 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004085}
4086
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004087void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004088 Record.push_back(GetOrCreateTypeID(T));
4089}
4090
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004091TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4092 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004093 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4094}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004095
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004096TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004097 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004098 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004099}
4100
4101TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4102 if (T.isNull())
4103 return TypeIdx();
4104 assert(!T.getLocalFastQualifiers());
4105
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004106 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004107 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004108 if (DoneWritingDeclsAndTypes) {
4109 assert(0 && "New type seen after serializing all the types to emit!");
4110 return TypeIdx();
4111 }
4112
Douglas Gregor366809a2009-04-26 03:49:13 +00004113 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004114 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004115 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004116 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004117 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004118 return Idx;
4119}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004120
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004121TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004122 if (T.isNull())
4123 return TypeIdx();
4124 assert(!T.getLocalFastQualifiers());
4125
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004126 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4127 assert(I != TypeIdxs.end() && "Type not emitted!");
4128 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004129}
4130
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004131void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004132 Record.push_back(GetDeclRef(D));
4133}
4134
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004135DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004136 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4137
Douglas Gregor2cf26342009-04-09 22:27:44 +00004138 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004139 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004140 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004141
4142 // If D comes from an AST file, its declaration ID is already known and
4143 // fixed.
4144 if (D->isFromASTFile())
4145 return D->getGlobalID();
4146
Douglas Gregor97475832010-10-05 18:37:06 +00004147 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004148 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004149 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004150 if (DoneWritingDeclsAndTypes) {
4151 assert(0 && "New decl seen after serializing all the decls to emit!");
4152 return 0;
4153 }
4154
Douglas Gregor2cf26342009-04-09 22:27:44 +00004155 // We haven't seen this declaration before. Give it a new ID and
4156 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004157 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004158 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004159 }
4160
Sebastian Redl681d7232010-07-27 00:17:23 +00004161 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004162}
4163
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004164DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004165 if (D == 0)
4166 return 0;
4167
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004168 // If D comes from an AST file, its declaration ID is already known and
4169 // fixed.
4170 if (D->isFromASTFile())
4171 return D->getGlobalID();
4172
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004173 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4174 return DeclIDs[D];
4175}
4176
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004177static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4178 std::pair<unsigned, serialization::DeclID> R) {
4179 return L.first < R.first;
4180}
4181
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004182void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004183 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004184 assert(D);
4185
4186 SourceLocation Loc = D->getLocation();
4187 if (Loc.isInvalid())
4188 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004189
4190 // We only keep track of the file-level declarations of each file.
4191 if (!D->getLexicalDeclContext()->isFileContext())
4192 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004193 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4194 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004195 if (isa<ParmVarDecl>(D))
4196 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004197
4198 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004199 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004200 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004201 FileID FID;
4202 unsigned Offset;
4203 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004204 if (FID.isInvalid())
4205 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004206 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004207
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004208 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004209 if (!Info)
4210 Info = new DeclIDInFileInfo();
4211
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004212 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004213 LocDeclIDsTy &Decls = Info->DeclIDs;
4214
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004215 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004216 Decls.push_back(LocDecl);
4217 return;
4218 }
4219
4220 LocDeclIDsTy::iterator
4221 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4222
4223 Decls.insert(I, LocDecl);
4224}
4225
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004226void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004227 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004228 Record.push_back(Name.getNameKind());
4229 switch (Name.getNameKind()) {
4230 case DeclarationName::Identifier:
4231 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4232 break;
4233
4234 case DeclarationName::ObjCZeroArgSelector:
4235 case DeclarationName::ObjCOneArgSelector:
4236 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004237 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004238 break;
4239
4240 case DeclarationName::CXXConstructorName:
4241 case DeclarationName::CXXDestructorName:
4242 case DeclarationName::CXXConversionFunctionName:
4243 AddTypeRef(Name.getCXXNameType(), Record);
4244 break;
4245
4246 case DeclarationName::CXXOperatorName:
4247 Record.push_back(Name.getCXXOverloadedOperator());
4248 break;
4249
Sean Hunt3e518bd2009-11-29 07:34:05 +00004250 case DeclarationName::CXXLiteralOperatorName:
4251 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4252 break;
4253
Douglas Gregor2cf26342009-04-09 22:27:44 +00004254 case DeclarationName::CXXUsingDirective:
4255 // No extra data to emit
4256 break;
4257 }
4258}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004259
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004260void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004261 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004262 switch (Name.getNameKind()) {
4263 case DeclarationName::CXXConstructorName:
4264 case DeclarationName::CXXDestructorName:
4265 case DeclarationName::CXXConversionFunctionName:
4266 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4267 break;
4268
4269 case DeclarationName::CXXOperatorName:
4270 AddSourceLocation(
4271 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4272 Record);
4273 AddSourceLocation(
4274 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4275 Record);
4276 break;
4277
4278 case DeclarationName::CXXLiteralOperatorName:
4279 AddSourceLocation(
4280 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4281 Record);
4282 break;
4283
4284 case DeclarationName::Identifier:
4285 case DeclarationName::ObjCZeroArgSelector:
4286 case DeclarationName::ObjCOneArgSelector:
4287 case DeclarationName::ObjCMultiArgSelector:
4288 case DeclarationName::CXXUsingDirective:
4289 break;
4290 }
4291}
4292
4293void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004294 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004295 AddDeclarationName(NameInfo.getName(), Record);
4296 AddSourceLocation(NameInfo.getLoc(), Record);
4297 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4298}
4299
4300void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004301 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004302 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004303 Record.push_back(Info.NumTemplParamLists);
4304 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4305 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4306}
4307
Sebastian Redla4232eb2010-08-18 23:56:21 +00004308void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004309 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004310 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004311 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004312 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004313
4314 // Push each of the NNS's onto a stack for serialization in reverse order.
4315 while (NNS) {
4316 NestedNames.push_back(NNS);
4317 NNS = NNS->getPrefix();
4318 }
4319
4320 Record.push_back(NestedNames.size());
4321 while(!NestedNames.empty()) {
4322 NNS = NestedNames.pop_back_val();
4323 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4324 Record.push_back(Kind);
4325 switch (Kind) {
4326 case NestedNameSpecifier::Identifier:
4327 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4328 break;
4329
4330 case NestedNameSpecifier::Namespace:
4331 AddDeclRef(NNS->getAsNamespace(), Record);
4332 break;
4333
Douglas Gregor14aba762011-02-24 02:36:08 +00004334 case NestedNameSpecifier::NamespaceAlias:
4335 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4336 break;
4337
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004338 case NestedNameSpecifier::TypeSpec:
4339 case NestedNameSpecifier::TypeSpecWithTemplate:
4340 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4341 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4342 break;
4343
4344 case NestedNameSpecifier::Global:
4345 // Don't need to write an associated value.
4346 break;
4347 }
4348 }
4349}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004350
Douglas Gregordc355712011-02-25 00:36:19 +00004351void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4352 RecordDataImpl &Record) {
4353 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004354 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004355 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004356
4357 // Push each of the nested-name-specifiers's onto a stack for
4358 // serialization in reverse order.
4359 while (NNS) {
4360 NestedNames.push_back(NNS);
4361 NNS = NNS.getPrefix();
4362 }
4363
4364 Record.push_back(NestedNames.size());
4365 while(!NestedNames.empty()) {
4366 NNS = NestedNames.pop_back_val();
4367 NestedNameSpecifier::SpecifierKind Kind
4368 = NNS.getNestedNameSpecifier()->getKind();
4369 Record.push_back(Kind);
4370 switch (Kind) {
4371 case NestedNameSpecifier::Identifier:
4372 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4373 AddSourceRange(NNS.getLocalSourceRange(), Record);
4374 break;
4375
4376 case NestedNameSpecifier::Namespace:
4377 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4378 AddSourceRange(NNS.getLocalSourceRange(), Record);
4379 break;
4380
4381 case NestedNameSpecifier::NamespaceAlias:
4382 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4383 AddSourceRange(NNS.getLocalSourceRange(), Record);
4384 break;
4385
4386 case NestedNameSpecifier::TypeSpec:
4387 case NestedNameSpecifier::TypeSpecWithTemplate:
4388 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4389 AddTypeLoc(NNS.getTypeLoc(), Record);
4390 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4391 break;
4392
4393 case NestedNameSpecifier::Global:
4394 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4395 break;
4396 }
4397 }
4398}
4399
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004400void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004401 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004402 Record.push_back(Kind);
4403 switch (Kind) {
4404 case TemplateName::Template:
4405 AddDeclRef(Name.getAsTemplateDecl(), Record);
4406 break;
4407
4408 case TemplateName::OverloadedTemplate: {
4409 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4410 Record.push_back(OvT->size());
4411 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4412 I != E; ++I)
4413 AddDeclRef(*I, Record);
4414 break;
4415 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004416
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004417 case TemplateName::QualifiedTemplate: {
4418 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4419 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4420 Record.push_back(QualT->hasTemplateKeyword());
4421 AddDeclRef(QualT->getTemplateDecl(), Record);
4422 break;
4423 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004424
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004425 case TemplateName::DependentTemplate: {
4426 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4427 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4428 Record.push_back(DepT->isIdentifier());
4429 if (DepT->isIdentifier())
4430 AddIdentifierRef(DepT->getIdentifier(), Record);
4431 else
4432 Record.push_back(DepT->getOperator());
4433 break;
4434 }
John McCall14606042011-06-30 08:33:18 +00004435
4436 case TemplateName::SubstTemplateTemplateParm: {
4437 SubstTemplateTemplateParmStorage *subst
4438 = Name.getAsSubstTemplateTemplateParm();
4439 AddDeclRef(subst->getParameter(), Record);
4440 AddTemplateName(subst->getReplacement(), Record);
4441 break;
4442 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004443
4444 case TemplateName::SubstTemplateTemplateParmPack: {
4445 SubstTemplateTemplateParmPackStorage *SubstPack
4446 = Name.getAsSubstTemplateTemplateParmPack();
4447 AddDeclRef(SubstPack->getParameterPack(), Record);
4448 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4449 break;
4450 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004451 }
4452}
4453
Michael J. Spencer20249a12010-10-21 03:16:25 +00004454void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004455 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004456 Record.push_back(Arg.getKind());
4457 switch (Arg.getKind()) {
4458 case TemplateArgument::Null:
4459 break;
4460 case TemplateArgument::Type:
4461 AddTypeRef(Arg.getAsType(), Record);
4462 break;
4463 case TemplateArgument::Declaration:
4464 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004465 Record.push_back(Arg.isDeclForReferenceParam());
4466 break;
4467 case TemplateArgument::NullPtr:
4468 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004469 break;
4470 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004471 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004472 AddTypeRef(Arg.getIntegralType(), Record);
4473 break;
4474 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004475 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4476 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004477 case TemplateArgument::TemplateExpansion:
4478 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004479 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4480 Record.push_back(*NumExpansions + 1);
4481 else
4482 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004483 break;
4484 case TemplateArgument::Expression:
4485 AddStmt(Arg.getAsExpr());
4486 break;
4487 case TemplateArgument::Pack:
4488 Record.push_back(Arg.pack_size());
4489 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4490 I != E; ++I)
4491 AddTemplateArgument(*I, Record);
4492 break;
4493 }
4494}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004495
4496void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004497ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004498 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004499 assert(TemplateParams && "No TemplateParams!");
4500 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4501 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4502 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4503 Record.push_back(TemplateParams->size());
4504 for (TemplateParameterList::const_iterator
4505 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4506 P != PEnd; ++P)
4507 AddDeclRef(*P, Record);
4508}
4509
4510/// \brief Emit a template argument list.
4511void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004512ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004513 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004514 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004515 Record.push_back(TemplateArgs->size());
4516 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004517 AddTemplateArgument(TemplateArgs->get(i), Record);
4518}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004519
4520
4521void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004522ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004523 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004524 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004525 I = Set.begin(), E = Set.end(); I != E; ++I) {
4526 AddDeclRef(I.getDecl(), Record);
4527 Record.push_back(I.getAccess());
4528 }
4529}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004530
Sebastian Redla4232eb2010-08-18 23:56:21 +00004531void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004532 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004533 Record.push_back(Base.isVirtual());
4534 Record.push_back(Base.isBaseOfClass());
4535 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004536 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004537 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004538 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004539 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4540 : SourceLocation(),
4541 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004542}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004543
Douglas Gregor7c789c12010-10-29 22:39:52 +00004544void ASTWriter::FlushCXXBaseSpecifiers() {
4545 RecordData Record;
4546 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4547 Record.clear();
4548
4549 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004550 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004551 if (Index == CXXBaseSpecifiersOffsets.size())
4552 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4553 else {
4554 if (Index > CXXBaseSpecifiersOffsets.size())
4555 CXXBaseSpecifiersOffsets.resize(Index + 1);
4556 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4557 }
4558
4559 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4560 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4561 Record.push_back(BEnd - B);
4562 for (; B != BEnd; ++B)
4563 AddCXXBaseSpecifier(*B, Record);
4564 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004565
4566 // Flush any expressions that were written as part of the base specifiers.
4567 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004568 }
4569
4570 CXXBaseSpecifiersToWrite.clear();
4571}
4572
Sean Huntcbb67482011-01-08 20:30:50 +00004573void ASTWriter::AddCXXCtorInitializers(
4574 const CXXCtorInitializer * const *CtorInitializers,
4575 unsigned NumCtorInitializers,
4576 RecordDataImpl &Record) {
4577 Record.push_back(NumCtorInitializers);
4578 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4579 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004580
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004581 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004582 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004583 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004584 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004585 } else if (Init->isDelegatingInitializer()) {
4586 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004587 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004588 } else if (Init->isMemberInitializer()){
4589 Record.push_back(CTOR_INITIALIZER_MEMBER);
4590 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004591 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004592 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4593 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004594 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004595
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004596 AddSourceLocation(Init->getMemberLocation(), Record);
4597 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004598 AddSourceLocation(Init->getLParenLoc(), Record);
4599 AddSourceLocation(Init->getRParenLoc(), Record);
4600 Record.push_back(Init->isWritten());
4601 if (Init->isWritten()) {
4602 Record.push_back(Init->getSourceOrder());
4603 } else {
4604 Record.push_back(Init->getNumArrayIndices());
4605 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4606 AddDeclRef(Init->getArrayIndex(i), Record);
4607 }
4608 }
4609}
4610
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004611void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4612 assert(D->DefinitionData);
4613 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004614 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004615 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004616 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004617 Record.push_back(Data.Aggregate);
4618 Record.push_back(Data.PlainOldData);
4619 Record.push_back(Data.Empty);
4620 Record.push_back(Data.Polymorphic);
4621 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004622 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004623 Record.push_back(Data.HasNoNonEmptyBases);
4624 Record.push_back(Data.HasPrivateFields);
4625 Record.push_back(Data.HasProtectedFields);
4626 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004627 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004628 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004629 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004630 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004631 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4632 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4633 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4634 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4635 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4636 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004637 Record.push_back(Data.HasTrivialSpecialMembers);
4638 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004639 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004640 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004641 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004642 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004643 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004644 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004645 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004646 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4647 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4648 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4649 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004650 Record.push_back(Data.FailedImplicitMoveConstructor);
4651 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004652 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004653
4654 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004655 if (Data.NumBases > 0)
4656 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4657 Record);
4658
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004659 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4660 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004661 if (Data.NumVBases > 0)
4662 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4663 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004664
4665 AddUnresolvedSet(Data.Conversions, Record);
4666 AddUnresolvedSet(Data.VisibleConversions, Record);
4667 // Data.Definition is the owning decl, no need to write it.
4668 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004669
4670 // Add lambda-specific data.
4671 if (Data.IsLambda) {
4672 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004673 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004674 Record.push_back(Lambda.NumCaptures);
4675 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004676 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004677 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004678 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004679 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4680 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4681 AddSourceLocation(Capture.getLocation(), Record);
4682 Record.push_back(Capture.isImplicit());
4683 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4684 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4685 AddDeclRef(Var, Record);
4686 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4687 : SourceLocation(),
4688 Record);
4689 }
4690 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004691}
4692
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004693void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004694 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004695 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004696 assert(FirstDeclID == NextDeclID &&
4697 FirstTypeID == NextTypeID &&
4698 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004699 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004700 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004701 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004702 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004703
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004704 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004705
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004706 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4707 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4708 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004709 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004710 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004711 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004712 NextDeclID = FirstDeclID;
4713 NextTypeID = FirstTypeID;
4714 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004715 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004716 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004717 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004718}
4719
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004720void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004721 IdentifierIDs[II] = ID;
4722}
4723
Douglas Gregora8235d62012-10-09 23:05:51 +00004724void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4725 MacroIDs[MI] = ID;
4726}
4727
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004728void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004729 // Always take the highest-numbered type index. This copes with an interesting
4730 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004731 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004732 // keep the higher-numbered entry so that we can properly write it out to
4733 // the AST file.
4734 TypeIdx &StoredIdx = TypeIdxs[T];
4735 if (Idx.getIndex() >= StoredIdx.getIndex())
4736 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004737}
4738
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004739void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004740 SelectorIDs[S] = ID;
4741}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004742
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004743void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004744 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004745 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004746 MacroDefinitions[MD] = ID;
4747}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004748
Douglas Gregora015cab2011-12-02 17:30:13 +00004749void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4750 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4751 SubmoduleIDs[Mod] = ID;
4752}
4753
Douglas Gregora8235d62012-10-09 23:05:51 +00004754void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4755 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4756}
4757
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004758void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004759 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004760 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004761 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4762 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004763 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004764 // A forward reference was mutated into a definition. Rewrite it.
4765 // FIXME: This happens during template instantiation, should we
4766 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004767 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004768 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004769 }
4770}
Douglas Gregora8235d62012-10-09 23:05:51 +00004771
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004772void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004773 assert(!WritingAST && "Already writing the AST!");
4774
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004775 // TU and namespaces are handled elsewhere.
4776 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4777 return;
4778
Douglas Gregor919814d2011-09-09 23:01:35 +00004779 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004780 return; // Not a source decl added to a DeclContext from PCH.
4781
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00004782 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004783 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004784 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004785}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004786
4787void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004788 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004789 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004790 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004791 return; // Not a source member added to a class from PCH.
4792 if (!isa<CXXMethodDecl>(D))
4793 return; // We are interested in lazily declared implicit methods.
4794
4795 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004796 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004797 UpdateRecord &Record = DeclUpdates[RD];
4798 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004799 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004800}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004801
4802void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4803 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004804 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004805 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004806 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004807 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004808 return; // Not a source specialization added to a template from PCH.
4809
4810 UpdateRecord &Record = DeclUpdates[TD];
4811 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004812 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004813}
Douglas Gregor89d99802010-11-30 06:16:57 +00004814
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004815void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4816 const FunctionDecl *D) {
4817 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004818 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004819 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004820 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004821 return; // Not a source specialization added to a template from PCH.
4822
4823 UpdateRecord &Record = DeclUpdates[TD];
4824 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004825 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004826}
4827
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004828void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004829 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004830 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004831 return; // Declaration not imported from PCH.
4832
4833 // Implicit decl from a PCH was defined.
4834 // FIXME: Should implicit definition be a separate FunctionDecl?
4835 RewriteDecl(D);
4836}
4837
Sebastian Redlf79a7192011-04-29 08:19:30 +00004838void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004839 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004840 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004841 return;
4842
4843 // Since the actual instantiation is delayed, this really means that we need
4844 // to update the instantiation location.
4845 UpdateRecord &Record = DeclUpdates[D];
4846 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4847 AddSourceLocation(
4848 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4849}
4850
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004851void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4852 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004853 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004854 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004855 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004856
4857 assert(IFD->getDefinition() && "Category on a class without a definition?");
4858 ObjCClassesWithCategories.insert(
4859 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004860}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004861
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004862
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004863void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4864 const ObjCPropertyDecl *OrigProp,
4865 const ObjCCategoryDecl *ClassExt) {
4866 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4867 if (!D)
4868 return;
4869
4870 assert(!WritingAST && "Already writing the AST!");
4871 if (!D->isFromASTFile())
4872 return; // Declaration not imported from PCH.
4873
4874 RewriteDecl(D);
4875}