blob: acb0459177fa6051f195aab8c588452d1afcd119 [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);
David Blaikiedc84cd52013-02-20 22:23:23 +0000356 if (Optional<unsigned> NumExpansions = T->getNumExpansions())
Douglas Gregorcded4f62011-01-14 17:04:44 +0000357 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);
Nick Lewyckycd0655b2013-02-01 08:13:20 +0000827 RECORD(UNDEFINED_BUT_USED);
Douglas Gregor837593f2011-08-04 16:39:39 +0000828 RECORD(MODULE_OFFSET_MAP);
829 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000830 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000831 RECORD(FILE_SORTED_DECLS);
832 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000833 RECORD(MERGED_DECLARATIONS);
834 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000835 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000836 RECORD(MACRO_OFFSET);
837 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000838
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000839 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000840 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000841 RECORD(SM_SLOC_FILE_ENTRY);
842 RECORD(SM_SLOC_BUFFER_ENTRY);
843 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000844 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000846 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000847 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000848 RECORD(PP_MACRO_OBJECT_LIKE);
849 RECORD(PP_MACRO_FUNCTION_LIKE);
850 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000851
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000852 // Decls and Types block.
853 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000854 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000855 RECORD(TYPE_COMPLEX);
856 RECORD(TYPE_POINTER);
857 RECORD(TYPE_BLOCK_POINTER);
858 RECORD(TYPE_LVALUE_REFERENCE);
859 RECORD(TYPE_RVALUE_REFERENCE);
860 RECORD(TYPE_MEMBER_POINTER);
861 RECORD(TYPE_CONSTANT_ARRAY);
862 RECORD(TYPE_INCOMPLETE_ARRAY);
863 RECORD(TYPE_VARIABLE_ARRAY);
864 RECORD(TYPE_VECTOR);
865 RECORD(TYPE_EXT_VECTOR);
866 RECORD(TYPE_FUNCTION_PROTO);
867 RECORD(TYPE_FUNCTION_NO_PROTO);
868 RECORD(TYPE_TYPEDEF);
869 RECORD(TYPE_TYPEOF_EXPR);
870 RECORD(TYPE_TYPEOF);
871 RECORD(TYPE_RECORD);
872 RECORD(TYPE_ENUM);
873 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000874 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000875 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000876 RECORD(TYPE_DECLTYPE);
877 RECORD(TYPE_ELABORATED);
878 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
879 RECORD(TYPE_UNRESOLVED_USING);
880 RECORD(TYPE_INJECTED_CLASS_NAME);
881 RECORD(TYPE_OBJC_OBJECT);
882 RECORD(TYPE_TEMPLATE_TYPE_PARM);
883 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
884 RECORD(TYPE_DEPENDENT_NAME);
885 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
886 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
887 RECORD(TYPE_PAREN);
888 RECORD(TYPE_PACK_EXPANSION);
889 RECORD(TYPE_ATTRIBUTED);
890 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000891 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000892 RECORD(DECL_TYPEDEF);
893 RECORD(DECL_ENUM);
894 RECORD(DECL_RECORD);
895 RECORD(DECL_ENUM_CONSTANT);
896 RECORD(DECL_FUNCTION);
897 RECORD(DECL_OBJC_METHOD);
898 RECORD(DECL_OBJC_INTERFACE);
899 RECORD(DECL_OBJC_PROTOCOL);
900 RECORD(DECL_OBJC_IVAR);
901 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000902 RECORD(DECL_OBJC_CATEGORY);
903 RECORD(DECL_OBJC_CATEGORY_IMPL);
904 RECORD(DECL_OBJC_IMPLEMENTATION);
905 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
906 RECORD(DECL_OBJC_PROPERTY);
907 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000908 RECORD(DECL_FIELD);
909 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000910 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000911 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000912 RECORD(DECL_FILE_SCOPE_ASM);
913 RECORD(DECL_BLOCK);
914 RECORD(DECL_CONTEXT_LEXICAL);
915 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000916 RECORD(DECL_NAMESPACE);
917 RECORD(DECL_NAMESPACE_ALIAS);
918 RECORD(DECL_USING);
919 RECORD(DECL_USING_SHADOW);
920 RECORD(DECL_USING_DIRECTIVE);
921 RECORD(DECL_UNRESOLVED_USING_VALUE);
922 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
923 RECORD(DECL_LINKAGE_SPEC);
924 RECORD(DECL_CXX_RECORD);
925 RECORD(DECL_CXX_METHOD);
926 RECORD(DECL_CXX_CONSTRUCTOR);
927 RECORD(DECL_CXX_DESTRUCTOR);
928 RECORD(DECL_CXX_CONVERSION);
929 RECORD(DECL_ACCESS_SPEC);
930 RECORD(DECL_FRIEND);
931 RECORD(DECL_FRIEND_TEMPLATE);
932 RECORD(DECL_CLASS_TEMPLATE);
933 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
934 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
935 RECORD(DECL_FUNCTION_TEMPLATE);
936 RECORD(DECL_TEMPLATE_TYPE_PARM);
937 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
938 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
939 RECORD(DECL_STATIC_ASSERT);
940 RECORD(DECL_CXX_BASE_SPECIFIERS);
941 RECORD(DECL_INDIRECTFIELD);
942 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
943
Douglas Gregora72d8c42011-06-03 02:27:19 +0000944 // Statements and Exprs can occur in the Decls and Types block.
945 AddStmtsExprs(Stream, Record);
946
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000947 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000948 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000949 RECORD(PPD_MACRO_DEFINITION);
950 RECORD(PPD_INCLUSION_DIRECTIVE);
951
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000952#undef RECORD
953#undef BLOCK
954 Stream.ExitBlock();
955}
956
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957/// \brief Adjusts the given filename to only write out the portion of the
958/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000959///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000960/// \param Filename the file name to adjust.
961///
962/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
963/// the returned filename will be adjusted by this system root.
964///
965/// \returns either the original filename (if it needs no adjustment) or the
966/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000967static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000968adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000969 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Douglas Gregor832d6202011-07-22 16:35:34 +0000971 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Douglas Gregore650c8c2009-07-07 00:12:59 +0000974 // Verify that the filename and the system root have the same prefix.
975 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000976 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 if (Filename[Pos] != isysroot[Pos])
978 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregore650c8c2009-07-07 00:12:59 +0000980 // We hit the end of the filename before we hit the end of the system root.
981 if (!Filename[Pos])
982 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Douglas Gregore650c8c2009-07-07 00:12:59 +0000984 // If the file name has a '/' at the current position, skip over the '/'.
985 // We distinguish sysroot-based includes from absolute includes by the
986 // absence of '/' at the beginning of sysroot-based includes.
987 if (Filename[Pos] == '/')
988 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregore650c8c2009-07-07 00:12:59 +0000990 return Filename + Pos;
991}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000992
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000993/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000994void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
995 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000996 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000997 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000998 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
999 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001000
Douglas Gregore650c8c2009-07-07 00:12:59 +00001001 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001002 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1010 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1011 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1012 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001013 Record.push_back(VERSION_MAJOR);
1014 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001015 Record.push_back(CLANG_VERSION_MAJOR);
1016 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001017 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001018 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001019 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1020 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001021
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001022 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001023 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001024 serialization::ModuleManager &Mgr = Chain->getModuleManager();
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001025 SmallVector<char, 128> ModulePaths;
Douglas Gregore95b9192011-08-17 21:07:30 +00001026 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001027
1028 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1029 M != MEnd; ++M) {
1030 // Skip modules that weren't directly imported.
1031 if (!(*M)->isDirectlyImported())
1032 continue;
1033
1034 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
Argyrios Kyrtzidis958bcaf2012-11-15 18:57:22 +00001035 AddSourceLocation((*M)->ImportLoc, Record);
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001036 // FIXME: This writes the absolute path for AST files we depend on.
1037 const std::string &FileName = (*M)->FileName;
1038 Record.push_back(FileName.size());
1039 Record.append(FileName.begin(), FileName.end());
1040 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001041 Stream.EmitRecord(IMPORTS, Record);
1042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001044 // Language options.
1045 Record.clear();
1046 const LangOptions &LangOpts = Context.getLangOpts();
1047#define LANGOPT(Name, Bits, Default, Description) \
1048 Record.push_back(LangOpts.Name);
1049#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1050 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1051#include "clang/Basic/LangOptions.def"
Will Dietz4f45bc02013-01-18 11:30:38 +00001052#define SANITIZER(NAME, ID) Record.push_back(LangOpts.Sanitize.ID);
1053#include "clang/Basic/Sanitizers.def"
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001054
1055 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1056 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1057
1058 Record.push_back(LangOpts.CurrentModule.size());
1059 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Dmitri Gribenko6ebf0912013-02-22 14:21:27 +00001060
1061 // Comment options.
1062 Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1063 for (CommentOptions::BlockCommandNamesTy::const_iterator
1064 I = LangOpts.CommentOpts.BlockCommandNames.begin(),
1065 IEnd = LangOpts.CommentOpts.BlockCommandNames.end();
1066 I != IEnd; ++I) {
1067 AddString(*I, Record);
1068 }
1069
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001070 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1071
Douglas Gregoree097c12012-10-18 17:58:09 +00001072 // Target options.
1073 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001074 const TargetInfo &Target = Context.getTargetInfo();
1075 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001076 AddString(TargetOpts.Triple, Record);
1077 AddString(TargetOpts.CPU, Record);
1078 AddString(TargetOpts.ABI, Record);
1079 AddString(TargetOpts.CXXABI, Record);
1080 AddString(TargetOpts.LinkerVersion, Record);
1081 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1082 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1083 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1084 }
1085 Record.push_back(TargetOpts.Features.size());
1086 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1087 AddString(TargetOpts.Features[I], Record);
1088 }
1089 Stream.EmitRecord(TARGET_OPTIONS, Record);
1090
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001091 // Diagnostic options.
1092 Record.clear();
1093 const DiagnosticOptions &DiagOpts
1094 = Context.getDiagnostics().getDiagnosticOptions();
1095#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1096#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1097 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1098#include "clang/Basic/DiagnosticOptions.def"
1099 Record.push_back(DiagOpts.Warnings.size());
1100 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1101 AddString(DiagOpts.Warnings[I], Record);
1102 // Note: we don't serialize the log or serialization file names, because they
1103 // are generally transient files and will almost always be overridden.
1104 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1105
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001106 // File system options.
1107 Record.clear();
1108 const FileSystemOptions &FSOpts
1109 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1110 AddString(FSOpts.WorkingDir, Record);
1111 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1112
Douglas Gregorbbf38312012-10-24 16:50:34 +00001113 // Header search options.
1114 Record.clear();
1115 const HeaderSearchOptions &HSOpts
1116 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1117 AddString(HSOpts.Sysroot, Record);
1118
1119 // Include entries.
1120 Record.push_back(HSOpts.UserEntries.size());
1121 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1122 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1123 AddString(Entry.Path, Record);
1124 Record.push_back(static_cast<unsigned>(Entry.Group));
Douglas Gregorbbf38312012-10-24 16:50:34 +00001125 Record.push_back(Entry.IsFramework);
1126 Record.push_back(Entry.IgnoreSysRoot);
Douglas Gregorbbf38312012-10-24 16:50:34 +00001127 }
1128
1129 // System header prefixes.
1130 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1131 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1132 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1133 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1134 }
1135
1136 AddString(HSOpts.ResourceDir, Record);
1137 AddString(HSOpts.ModuleCachePath, Record);
1138 Record.push_back(HSOpts.DisableModuleHash);
1139 Record.push_back(HSOpts.UseBuiltinIncludes);
1140 Record.push_back(HSOpts.UseStandardSystemIncludes);
1141 Record.push_back(HSOpts.UseStandardCXXIncludes);
1142 Record.push_back(HSOpts.UseLibcxx);
1143 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1144
Douglas Gregora71a7d82012-10-24 20:05:57 +00001145 // Preprocessor options.
1146 Record.clear();
1147 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1148
1149 // Macro definitions.
1150 Record.push_back(PPOpts.Macros.size());
1151 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1152 AddString(PPOpts.Macros[I].first, Record);
1153 Record.push_back(PPOpts.Macros[I].second);
1154 }
1155
1156 // Includes
1157 Record.push_back(PPOpts.Includes.size());
1158 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1159 AddString(PPOpts.Includes[I], Record);
1160
1161 // Macro includes
1162 Record.push_back(PPOpts.MacroIncludes.size());
1163 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1164 AddString(PPOpts.MacroIncludes[I], Record);
1165
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001166 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001167 AddString(PPOpts.ImplicitPCHInclude, Record);
1168 AddString(PPOpts.ImplicitPTHInclude, Record);
1169 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1170 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1171
Douglas Gregor31d375f2011-05-06 21:43:30 +00001172 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001173 SourceManager &SM = Context.getSourceManager();
1174 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1175 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001176 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1177 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001178 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1179 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1180
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001181 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001183 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001184
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001185 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001186 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001187 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001188 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001189 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001190 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001191 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001192 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001193
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +00001194 Record.clear();
1195 Record.push_back(SM.getMainFileID().getOpaqueValue());
1196 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1197
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001198 // Original PCH directory
1199 if (!OutputFile.empty() && OutputFile != "-") {
1200 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1201 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1203 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1204
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001205 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001206
1207 llvm::sys::fs::make_absolute(OutputPath);
1208 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1209
1210 RecordData Record;
1211 Record.push_back(ORIGINAL_PCH_DIR);
1212 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1213 }
1214
Douglas Gregor745e6f12012-10-19 00:38:02 +00001215 WriteInputFiles(Context.SourceMgr, isysroot);
1216 Stream.ExitBlock();
1217}
1218
1219void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1220 using namespace llvm;
1221 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1222 RecordData Record;
1223
1224 // Create input-file abbreviation.
1225 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1226 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001227 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001228 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1229 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001230 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001231 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1232 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1233
1234 // Write out all of the input files.
1235 std::vector<uint32_t> InputFileOffsets;
1236 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1237 // Get this source location entry.
1238 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001239 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001240
1241 // We only care about file entries that were not overridden.
1242 if (!SLoc->isFile())
1243 continue;
1244 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001245 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001246 continue;
1247
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001248 uint32_t &InputFileID = InputFileIDs[Cache->OrigEntry];
1249 if (InputFileID != 0)
1250 continue; // already recorded this file.
1251
Douglas Gregora930dc92012-10-22 18:42:04 +00001252 // Record this entry's offset.
1253 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
Argyrios Kyrtzidisa89b6182012-12-11 07:48:08 +00001254
1255 InputFileID = InputFileOffsets.size();
Douglas Gregora930dc92012-10-22 18:42:04 +00001256
Douglas Gregor745e6f12012-10-19 00:38:02 +00001257 Record.clear();
1258 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001259 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001260
1261 // Emit size/modification time for this file.
1262 Record.push_back(Cache->OrigEntry->getSize());
1263 Record.push_back(Cache->OrigEntry->getModificationTime());
1264
Douglas Gregora930dc92012-10-22 18:42:04 +00001265 // Whether this file was overridden.
1266 Record.push_back(Cache->BufferOverridden);
1267
Douglas Gregor745e6f12012-10-19 00:38:02 +00001268 // Turn the file name into an absolute path, if it isn't already.
1269 const char *Filename = Cache->OrigEntry->getName();
1270 SmallString<128> FilePath(Filename);
1271
1272 // Ask the file manager to fixup the relative path for us. This will
1273 // honor the working directory.
1274 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1275
1276 // FIXME: This call to make_absolute shouldn't be necessary, the
1277 // call to FixupRelativePath should always return an absolute path.
1278 llvm::sys::fs::make_absolute(FilePath);
1279 Filename = FilePath.c_str();
1280
1281 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1282
1283 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1284 }
1285
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001286 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001287
1288 // Create input file offsets abbreviation.
1289 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1290 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1291 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1292 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1293 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1294
1295 // Write input file offsets.
1296 Record.clear();
1297 Record.push_back(INPUT_FILE_OFFSETS);
1298 Record.push_back(InputFileOffsets.size());
1299 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001300}
1301
Douglas Gregor14f79002009-04-10 03:52:48 +00001302//===----------------------------------------------------------------------===//
1303// Source Manager Serialization
1304//===----------------------------------------------------------------------===//
1305
1306/// \brief Create an abbreviation for the SLocEntry that refers to a
1307/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001308static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001309 using namespace llvm;
1310 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001311 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001316 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001321 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001322}
1323
1324/// \brief Create an abbreviation for the SLocEntry that refers to a
1325/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001326static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001327 using namespace llvm;
1328 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001329 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001330 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1331 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1332 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1333 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1334 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001335 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001336}
1337
1338/// \brief Create an abbreviation for the SLocEntry that refers to a
1339/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001340static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001341 using namespace llvm;
1342 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001343 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001344 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001346}
1347
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001348/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1349/// expansion.
1350static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001351 using namespace llvm;
1352 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001353 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001359 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001360}
1361
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001362namespace {
1363 // Trait used for the on-disk hash table of header search information.
1364 class HeaderFileInfoTrait {
1365 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001366
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001367 // Keep track of the framework names we've used during serialization.
1368 SmallVector<char, 128> FrameworkStringData;
1369 llvm::StringMap<unsigned> FrameworkNameOffset;
1370
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001371 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001372 HeaderFileInfoTrait(ASTWriter &Writer)
1373 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001374
1375 typedef const char *key_type;
1376 typedef key_type key_type_ref;
1377
1378 typedef HeaderFileInfo data_type;
1379 typedef const data_type &data_type_ref;
1380
1381 static unsigned ComputeHash(const char *path) {
1382 // The hash is based only on the filename portion of the key, so that the
1383 // reader can match based on filenames when symlinking or excess path
1384 // elements ("foo/../", "../") change the form of the name. However,
1385 // complete path is still the key.
1386 return llvm::HashString(llvm::sys::path::filename(path));
1387 }
1388
1389 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001390 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001391 data_type_ref Data) {
1392 unsigned StrLen = strlen(path);
1393 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001394 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001395 clang::io::Emit8(Out, DataLen);
1396 return std::make_pair(StrLen + 1, DataLen);
1397 }
1398
Chris Lattner5f9e2722011-07-23 10:55:15 +00001399 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001400 Out.write(path, KeyLen);
1401 }
1402
Chris Lattner5f9e2722011-07-23 10:55:15 +00001403 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001404 data_type_ref Data, unsigned DataLen) {
1405 using namespace clang::io;
1406 uint64_t Start = Out.tell(); (void)Start;
1407
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001408 unsigned char Flags = (Data.isImport << 5)
1409 | (Data.isPragmaOnce << 4)
1410 | (Data.DirInfo << 2)
1411 | (Data.Resolved << 1)
1412 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001413 Emit8(Out, (uint8_t)Flags);
1414 Emit16(Out, (uint16_t) Data.NumIncludes);
1415
1416 if (!Data.ControllingMacro)
1417 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1418 else
1419 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001420
1421 unsigned Offset = 0;
1422 if (!Data.Framework.empty()) {
1423 // If this header refers into a framework, save the framework name.
1424 llvm::StringMap<unsigned>::iterator Pos
1425 = FrameworkNameOffset.find(Data.Framework);
1426 if (Pos == FrameworkNameOffset.end()) {
1427 Offset = FrameworkStringData.size() + 1;
1428 FrameworkStringData.append(Data.Framework.begin(),
1429 Data.Framework.end());
1430 FrameworkStringData.push_back(0);
1431
1432 FrameworkNameOffset[Data.Framework] = Offset;
1433 } else
1434 Offset = Pos->second;
1435 }
1436 Emit32(Out, Offset);
1437
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001438 assert(Out.tell() - Start == DataLen && "Wrong data length");
1439 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001440
1441 const char *strings_begin() const { return FrameworkStringData.begin(); }
1442 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001443 };
1444} // end anonymous namespace
1445
1446/// \brief Write the header search block for the list of files that
1447///
1448/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001449void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001450 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001451 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1452
1453 if (FilesByUID.size() > HS.header_file_size())
1454 FilesByUID.resize(HS.header_file_size());
1455
Benjamin Kramerfacde172012-06-06 17:32:50 +00001456 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001457 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001458 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001459 unsigned NumHeaderSearchEntries = 0;
1460 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1461 const FileEntry *File = FilesByUID[UID];
1462 if (!File)
1463 continue;
1464
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001465 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1466 // from the external source if it was not provided already.
1467 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001468 if (HFI.External && Chain)
1469 continue;
1470
1471 // Turn the file name into an absolute path, if it isn't already.
1472 const char *Filename = File->getName();
1473 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1474
1475 // If we performed any translation on the file name at all, we need to
1476 // save this string, since the generator will refer to it later.
1477 if (Filename != File->getName()) {
1478 Filename = strdup(Filename);
1479 SavedStrings.push_back(Filename);
1480 }
1481
1482 Generator.insert(Filename, HFI, GeneratorTrait);
1483 ++NumHeaderSearchEntries;
1484 }
1485
1486 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001487 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001488 uint32_t BucketOffset;
1489 {
1490 llvm::raw_svector_ostream Out(TableData);
1491 // Make sure that no bucket is at offset 0
1492 clang::io::Emit32(Out, 0);
1493 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1494 }
1495
1496 // Create a blob abbreviation
1497 using namespace llvm;
1498 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1499 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001503 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1504 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1505
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001506 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001507 RecordData Record;
1508 Record.push_back(HEADER_SEARCH_TABLE);
1509 Record.push_back(BucketOffset);
1510 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001511 Record.push_back(TableData.size());
1512 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001513 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1514
1515 // Free all of the strings we had to duplicate.
1516 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
David Greene64444832013-01-15 22:09:43 +00001517 free(const_cast<char *>(SavedStrings[I]));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001518}
1519
Douglas Gregor14f79002009-04-10 03:52:48 +00001520/// \brief Writes the block containing the serialized form of the
1521/// source manager.
1522///
1523/// TODO: We should probably use an on-disk hash table (stored in a
1524/// blob), indexed based on the file name, so that we only create
1525/// entries for files that we actually need. In the common case (no
1526/// errors), we probably won't have to create file entries for any of
1527/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001528void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001529 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001530 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001531 RecordData Record;
1532
Chris Lattnerf04ad692009-04-10 17:16:57 +00001533 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001534 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001535
1536 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001537 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1538 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1539 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001540 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001541
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542 // Write out the source location entry table. We skip the first
1543 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001544 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001546 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1547 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001548 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001549 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001550 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001551 FileID FID = FileID::get(I);
1552 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001553
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554 // Record the offset of this source-location entry.
1555 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1556
1557 // Figure out which record code to use.
1558 unsigned Code;
1559 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001560 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1561 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001562 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001563 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001564 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001566 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001567 Record.clear();
1568 Record.push_back(Code);
1569
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001570 // Starting offset of this entry within this module, so skip the dummy.
1571 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 if (SLoc->isFile()) {
1573 const SrcMgr::FileInfo &File = SLoc->getFile();
1574 Record.push_back(File.getIncludeLoc().getRawEncoding());
1575 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1576 Record.push_back(File.hasLineDirectives());
1577
1578 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001579 if (Content->OrigEntry) {
1580 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001581 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001582
Douglas Gregora930dc92012-10-22 18:42:04 +00001583 // The source location entry is a file. Emit input file ID.
1584 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1585 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001587 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001588
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001589 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001590 if (FDI != FileDeclIDs.end()) {
1591 Record.push_back(FDI->second->FirstDeclIndex);
1592 Record.push_back(FDI->second->DeclIDs.size());
1593 } else {
1594 Record.push_back(0);
1595 Record.push_back(0);
1596 }
Douglas Gregora081da52011-11-16 20:05:18 +00001597
Douglas Gregora930dc92012-10-22 18:42:04 +00001598 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001599
1600 if (Content->BufferOverridden) {
1601 Record.clear();
1602 Record.push_back(SM_SLOC_BUFFER_BLOB);
1603 const llvm::MemoryBuffer *Buffer
1604 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1605 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1606 StringRef(Buffer->getBufferStart(),
1607 Buffer->getBufferSize() + 1));
1608 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001609 } else {
1610 // The source location entry is a buffer. The blob associated
1611 // with this entry contains the contents of the buffer.
1612
1613 // We add one to the size so that we capture the trailing NULL
1614 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1615 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001616 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001617 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001618 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001619 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001620 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001621 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001622 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001623 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001624 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001625 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001626
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001627 if (strcmp(Name, "<built-in>") == 0) {
1628 PreloadSLocs.push_back(SLocEntryOffsets.size());
1629 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001630 }
1631 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001632 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001633 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001634 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1635 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001636 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1637 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001638
1639 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001640 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001641 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001642 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001643 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001644 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001645 }
1646 }
1647
Douglas Gregorc9490c02009-04-16 22:23:12 +00001648 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001649
1650 if (SLocEntryOffsets.empty())
1651 return;
1652
Sebastian Redl3397c552010-08-18 23:56:27 +00001653 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001654 // table is used for lazily loading source-location information.
1655 using namespace llvm;
1656 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001657 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001658 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001659 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001660 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1661 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001663 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001664 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001665 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001666 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001667 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001668
Sebastian Redl3397c552010-08-18 23:56:27 +00001669 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001670 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001671 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001672
1673 // Write the line table. It depends on remapping working, so it must come
1674 // after the source location offsets.
1675 if (SourceMgr.hasLineTable()) {
1676 LineTableInfo &LineTable = SourceMgr.getLineTable();
1677
1678 Record.clear();
1679 // Emit the file names
1680 Record.push_back(LineTable.getNumFilenames());
1681 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1682 // Emit the file name
1683 const char *Filename = LineTable.getFilename(I);
1684 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1685 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1686 Record.push_back(FilenameLen);
1687 if (FilenameLen)
1688 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1689 }
1690
1691 // Emit the line entries
1692 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1693 L != LEnd; ++L) {
1694 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001695 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001696 continue;
1697
1698 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001699 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001700
1701 // Emit the line entries
1702 Record.push_back(L->second.size());
1703 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1704 LEEnd = L->second.end();
1705 LE != LEEnd; ++LE) {
1706 Record.push_back(LE->FileOffset);
1707 Record.push_back(LE->LineNo);
1708 Record.push_back(LE->FilenameID);
1709 Record.push_back((unsigned)LE->FileKind);
1710 Record.push_back(LE->IncludeOffset);
1711 }
1712 }
1713 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1714 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001715}
1716
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001717//===----------------------------------------------------------------------===//
1718// Preprocessor Serialization
1719//===----------------------------------------------------------------------===//
1720
Douglas Gregor9c736102011-02-10 18:20:09 +00001721static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1722 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1723 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1724 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1725 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1726 return X.first->getName().compare(Y.first->getName());
1727}
1728
Chris Lattner0b1fb982009-04-10 17:15:23 +00001729/// \brief Writes the block containing the serialized form of the
1730/// preprocessor.
1731///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001732void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001733 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1734 if (PPRec)
1735 WritePreprocessorDetail(*PPRec);
1736
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001737 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001738
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001739 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1740 if (PP.getCounterValue() != 0) {
1741 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001742 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001743 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001744 }
1745
1746 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001747 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001748
Sebastian Redl3397c552010-08-18 23:56:27 +00001749 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001750 // FIXME: use diagnostics subsystem for localization etc.
1751 if (PP.SawDateOrTime())
1752 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Douglas Gregorecdcb882010-10-20 22:00:55 +00001754
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001755 // Loop over all the macro definitions that are live at the end of the file,
1756 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001757
Douglas Gregor9c736102011-02-10 18:20:09 +00001758 // Construct the list of macro definitions that need to be serialized.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001759 SmallVector<std::pair<const IdentifierInfo *, MacroDirective *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001760 MacrosToEmit;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001761 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001762 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001763 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001764 if (!IsModule || I->second->isPublic()) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001765 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001766 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001767 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001768
Douglas Gregor9c736102011-02-10 18:20:09 +00001769 // Sort the set of macro definitions that need to be serialized by the
1770 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001771 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001772 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001773
Douglas Gregora8235d62012-10-09 23:05:51 +00001774 /// \brief Offsets of each of the macros into the bitstream, indexed by
1775 /// the local macro ID
1776 ///
1777 /// For each identifier that is associated with a macro, this map
1778 /// provides the offset into the bitstream where that macro is
1779 /// defined.
1780 std::vector<uint32_t> MacroOffsets;
1781
Douglas Gregor9c736102011-02-10 18:20:09 +00001782 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1783 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001784
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001785 for (MacroDirective *MD = MacrosToEmit[I].second; MD;
1786 MD = MD->getPrevious()) {
1787 MacroID ID = getMacroRef(MD);
Douglas Gregora8235d62012-10-09 23:05:51 +00001788 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001789 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Douglas Gregora8235d62012-10-09 23:05:51 +00001791 // Skip macros from a AST file if we're chaining.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001792 if (Chain && MD->isImported() && !MD->hasChangedAfterLoad())
Douglas Gregora8235d62012-10-09 23:05:51 +00001793 continue;
1794
1795 if (ID < FirstMacroID) {
1796 // This will have been dealt with via an update record.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001797 assert(MacroUpdates.count(MD) > 0 && "Missing macro update");
Douglas Gregora8235d62012-10-09 23:05:51 +00001798 continue;
1799 }
1800
1801 // Record the local offset of this macro.
1802 unsigned Index = ID - FirstMacroID;
1803 if (Index == MacroOffsets.size())
1804 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1805 else {
1806 if (Index > MacroOffsets.size())
1807 MacroOffsets.resize(Index + 1);
1808
1809 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1810 }
1811
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001812 AddIdentifierRef(Name, Record);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001813 addMacroRef(MD, Record);
1814 const MacroInfo *MI = MD->getInfo();
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001815 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001816 AddSourceLocation(MI->getDefinitionLoc(), Record);
Argyrios Kyrtzidis8169b672013-01-07 19:16:23 +00001817 AddSourceLocation(MI->getDefinitionEndLoc(), Record);
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001818 AddSourceLocation(MD->getUndefLoc(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001819 Record.push_back(MI->isUsed());
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00001820 Record.push_back(MD->isPublic());
1821 AddSourceLocation(MD->getVisibilityLocation(), Record);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001822 unsigned Code;
1823 if (MI->isObjectLike()) {
1824 Code = PP_MACRO_OBJECT_LIKE;
1825 } else {
1826 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001827
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001828 Record.push_back(MI->isC99Varargs());
1829 Record.push_back(MI->isGNUVarargs());
Eli Friedman4fa4b482012-11-14 02:18:46 +00001830 Record.push_back(MI->hasCommaPasting());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001831 Record.push_back(MI->getNumArgs());
1832 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1833 I != E; ++I)
1834 AddIdentifierRef(*I, Record);
1835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001837 // If we have a detailed preprocessing record, record the macro definition
1838 // ID that corresponds to this macro.
1839 if (PPRec)
1840 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1841
1842 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001843 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001844
1845 // Emit the tokens array.
1846 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1847 // Note that we know that the preprocessor does not have any annotation
1848 // tokens in it because they are created by the parser, and thus can't
1849 // be in a macro definition.
1850 const Token &Tok = MI->getReplacementToken(TokNo);
1851
1852 Record.push_back(Tok.getLocation().getRawEncoding());
1853 Record.push_back(Tok.getLength());
1854
1855 // FIXME: When reading literal tokens, reconstruct the literal pointer
1856 // if it is needed.
1857 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1858 // FIXME: Should translate token kind to a stable encoding.
1859 Record.push_back(Tok.getKind());
1860 // FIXME: Should translate token flags to a stable encoding.
1861 Record.push_back(Tok.getFlags());
1862
1863 Stream.EmitRecord(PP_TOKEN, Record);
1864 Record.clear();
1865 }
1866 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001867 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001868 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001869 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001870
1871 // Write the offsets table for macro IDs.
1872 using namespace llvm;
1873 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1874 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1875 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1876 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1877 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1878
1879 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1880 Record.clear();
1881 Record.push_back(MACRO_OFFSET);
1882 Record.push_back(MacroOffsets.size());
1883 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1884 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1885 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001886}
1887
1888void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001889 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001890 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001891
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001892 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001893
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001894 // Enter the preprocessor block.
1895 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001896
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001897 // If the preprocessor has a preprocessing record, emit it.
1898 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001899 using namespace llvm;
1900
1901 // Set up the abbreviation for
1902 unsigned InclusionAbbrev = 0;
1903 {
1904 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1905 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1911 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1912 }
1913
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001914 unsigned FirstPreprocessorEntityID
1915 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1916 + NUM_PREDEF_PP_ENTITY_IDS;
1917 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001918 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001919 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1920 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001921 E != EEnd;
1922 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001923 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001924
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001925 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1926 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001927
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001928 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001929 // Record this macro definition's ID.
1930 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001931
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001932 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001933 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1934 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001935 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001936
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001937 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001938 Record.push_back(ME->isBuiltinMacro());
1939 if (ME->isBuiltinMacro())
1940 AddIdentifierRef(ME->getName(), Record);
1941 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001942 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001943 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001944 continue;
1945 }
1946
1947 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1948 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001949 Record.push_back(ID->getFileName().size());
1950 Record.push_back(ID->wasInQuotes());
1951 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001952 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001953 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001954 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001955 // Check that the FileEntry is not null because it was not resolved and
1956 // we create a PCH even with compiler errors.
1957 if (ID->getFile())
1958 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001959 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1960 continue;
1961 }
1962
1963 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1964 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001965 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001966
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001967 // Write the offsets table for the preprocessing record.
1968 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001969 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1970
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001971 // Write the offsets table for identifier IDs.
1972 using namespace llvm;
1973 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001974 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001977 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001978
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001979 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001980 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001981 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001982 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1983 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001984 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001985}
1986
Douglas Gregore209e502011-12-06 01:10:29 +00001987unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1988 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1989 if (Known != SubmoduleIDs.end())
1990 return Known->second;
1991
1992 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1993}
1994
Douglas Gregor26ced122011-12-01 00:59:36 +00001995/// \brief Compute the number of modules within the given tree (including the
1996/// given module).
1997static unsigned getNumberOfModules(Module *Mod) {
1998 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001999 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2000 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002001 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002002 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002003
2004 return ChildModules + 1;
2005}
2006
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002007void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002008 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002009 // FIXME: This feels like it belongs somewhere else, but there are no
2010 // other consumers of this information.
2011 SourceManager &SrcMgr = PP->getSourceManager();
2012 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2013 for (ASTContext::import_iterator I = Context->local_import_begin(),
2014 IEnd = Context->local_import_end();
2015 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002016 if (Module *ImportedFrom
2017 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2018 SrcMgr))) {
2019 ImportedFrom->Imports.push_back(I->getImportedModule());
2020 }
2021 }
2022
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002023 // Enter the submodule description block.
2024 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2025
2026 // Write the abbreviations needed for the submodules block.
2027 using namespace llvm;
2028 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2029 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2032 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2039 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2040
2041 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002042 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2044 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2045
2046 Abbrev = new BitCodeAbbrev();
2047 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2049 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002050
2051 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002052 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2054 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2055
2056 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002057 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2059 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2060
Douglas Gregor51f564f2011-12-31 04:05:44 +00002061 Abbrev = new BitCodeAbbrev();
2062 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2064 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2065
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002066 Abbrev = new BitCodeAbbrev();
2067 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2069 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2070
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002071 Abbrev = new BitCodeAbbrev();
2072 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2075 unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2076
Douglas Gregor26ced122011-12-01 00:59:36 +00002077 // Write the submodule metadata block.
2078 RecordData Record;
2079 Record.push_back(getNumberOfModules(WritingModule));
2080 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2081 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2082
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002083 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002084 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002085 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002086 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002087 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002088 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002089 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002090
2091 // Emit the definition of the block.
2092 Record.clear();
2093 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002094 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002095 if (Mod->Parent) {
2096 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2097 Record.push_back(SubmoduleIDs[Mod->Parent]);
2098 } else {
2099 Record.push_back(0);
2100 }
2101 Record.push_back(Mod->IsFramework);
2102 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002103 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002104 Record.push_back(Mod->InferSubmodules);
2105 Record.push_back(Mod->InferExplicitSubmodules);
2106 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002107 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2108
Douglas Gregor51f564f2011-12-31 04:05:44 +00002109 // Emit the requirements.
2110 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2111 Record.clear();
2112 Record.push_back(SUBMODULE_REQUIRES);
2113 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2114 Mod->Requires[I].data(),
2115 Mod->Requires[I].size());
2116 }
2117
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002118 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002119 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002120 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002121 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002122 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002123 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002124 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2125 Record.clear();
2126 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2127 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2128 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002129 }
2130
2131 // Emit the headers.
2132 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2133 Record.clear();
2134 Record.push_back(SUBMODULE_HEADER);
2135 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2136 Mod->Headers[I]->getName());
2137 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002138 // Emit the excluded headers.
2139 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2140 Record.clear();
2141 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2142 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2143 Mod->ExcludedHeaders[I]->getName());
2144 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002145 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2146 Record.clear();
2147 Record.push_back(SUBMODULE_TOPHEADER);
2148 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2149 Mod->TopHeaders[I]->getName());
2150 }
Douglas Gregor55988682011-12-05 16:33:54 +00002151
2152 // Emit the imports.
2153 if (!Mod->Imports.empty()) {
2154 Record.clear();
2155 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002156 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002157 assert(ImportedID && "Unknown submodule!");
2158 Record.push_back(ImportedID);
2159 }
2160 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2161 }
2162
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002163 // Emit the exports.
2164 if (!Mod->Exports.empty()) {
2165 Record.clear();
2166 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002167 if (Module *Exported = Mod->Exports[I].getPointer()) {
2168 unsigned ExportedID = SubmoduleIDs[Exported];
2169 assert(ExportedID > 0 && "Unknown submodule ID?");
2170 Record.push_back(ExportedID);
2171 } else {
2172 Record.push_back(0);
2173 }
2174
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002175 Record.push_back(Mod->Exports[I].getInt());
2176 }
2177 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2178 }
Douglas Gregorb6cbe512013-01-14 17:21:00 +00002179
2180 // Emit the link libraries.
2181 for (unsigned I = 0, N = Mod->LinkLibraries.size(); I != N; ++I) {
2182 Record.clear();
2183 Record.push_back(SUBMODULE_LINK_LIBRARY);
2184 Record.push_back(Mod->LinkLibraries[I].IsFramework);
2185 Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record,
2186 Mod->LinkLibraries[I].Library);
2187 }
2188
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002189 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002190 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2191 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002192 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002193 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002194 }
2195
2196 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002197
2198 assert((NextSubmoduleID - FirstSubmoduleID
2199 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002200}
2201
Douglas Gregor185dbd72011-12-01 02:07:58 +00002202serialization::SubmoduleID
2203ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002204 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002205 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002206
2207 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002208 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002209 Module *OwningMod
2210 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002211 if (!OwningMod)
2212 return 0;
2213
Douglas Gregore209e502011-12-06 01:10:29 +00002214 // Check whether this submodule is part of our own module.
2215 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002216 return 0;
2217
Douglas Gregore209e502011-12-06 01:10:29 +00002218 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002219}
2220
David Blaikied6471f72011-09-25 23:23:43 +00002221void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002222 // FIXME: Make it work properly with modules.
2223 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2224 DiagStateIDMap;
2225 unsigned CurrID = 0;
2226 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002227 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002228 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002229 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2230 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002231 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002232 if (point.Loc.isInvalid())
2233 continue;
2234
2235 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002236 unsigned &DiagStateID = DiagStateIDMap[point.State];
2237 Record.push_back(DiagStateID);
2238
2239 if (DiagStateID == 0) {
2240 DiagStateID = ++CurrID;
2241 for (DiagnosticsEngine::DiagState::const_iterator
2242 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2243 if (I->second.isPragma()) {
2244 Record.push_back(I->first);
2245 Record.push_back(I->second.getMapping());
2246 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002247 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002248 Record.push_back(-1); // mark the end of the diag/map pairs for this
2249 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002250 }
2251 }
2252
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002253 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002254 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002255}
2256
Anders Carlssonc8505782011-03-06 18:41:18 +00002257void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2258 if (CXXBaseSpecifiersOffsets.empty())
2259 return;
2260
2261 RecordData Record;
2262
2263 // Create a blob abbreviation for the C++ base specifiers offsets.
2264 using namespace llvm;
2265
2266 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2267 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2269 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2270 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2271
Douglas Gregore92b8a12011-08-04 00:01:48 +00002272 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002273 Record.clear();
2274 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2275 Record.push_back(CXXBaseSpecifiersOffsets.size());
2276 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002277 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002278}
2279
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002280//===----------------------------------------------------------------------===//
2281// Type Serialization
2282//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002283
Sebastian Redl3397c552010-08-18 23:56:27 +00002284/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002285void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002286 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002287 if (Idx.getIndex() == 0) // we haven't seen this type before.
2288 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Douglas Gregor97475832010-10-05 18:37:06 +00002290 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002291
Douglas Gregor2cf26342009-04-09 22:27:44 +00002292 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002293 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002294 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002295 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002296 else if (TypeOffsets.size() < Index) {
2297 TypeOffsets.resize(Index + 1);
2298 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002299 }
2300
2301 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002304 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002305
Douglas Gregora4923eb2009-11-16 21:35:15 +00002306 if (T.hasLocalNonFastQualifiers()) {
2307 Qualifiers Qs = T.getLocalQualifiers();
2308 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002309 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002310 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002311 } else {
2312 switch (T->getTypeClass()) {
2313 // For all of the concrete, non-dependent types, call the
2314 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002315#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002316 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002317#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002318#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002319 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002320 }
2321
2322 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002323 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002324
2325 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002326 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327}
2328
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002329//===----------------------------------------------------------------------===//
2330// Declaration Serialization
2331//===----------------------------------------------------------------------===//
2332
Douglas Gregor2cf26342009-04-09 22:27:44 +00002333/// \brief Write the block containing all of the declaration IDs
2334/// lexically declared within the given DeclContext.
2335///
2336/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2337/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002338uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002339 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002340 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002341 return 0;
2342
Douglas Gregorc9490c02009-04-16 22:23:12 +00002343 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002344 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002345 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002346 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002347 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2348 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002349 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002350
Douglas Gregor25123082009-04-22 22:34:57 +00002351 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002352 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002353 return Offset;
2354}
2355
Sebastian Redla4232eb2010-08-18 23:56:21 +00002356void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002357 using namespace llvm;
2358 RecordData Record;
2359
2360 // Write the type offsets array
2361 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002362 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002365 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2366 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2367 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002368 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002369 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002370 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002371 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002372
2373 // Write the declaration offsets array
2374 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002375 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2379 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2380 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002382 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002383 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002384 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002385}
2386
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002387void ASTWriter::WriteFileDeclIDsMap() {
2388 using namespace llvm;
2389 RecordData Record;
2390
2391 // Join the vectors of DeclIDs from all files.
2392 SmallVector<DeclID, 256> FileSortedIDs;
2393 for (FileDeclIDsTy::iterator
2394 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2395 DeclIDInFileInfo &Info = *FI->second;
2396 Info.FirstDeclIndex = FileSortedIDs.size();
2397 for (LocDeclIDsTy::iterator
2398 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2399 FileSortedIDs.push_back(DI->second);
2400 }
2401
2402 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2403 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2406 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2407 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002408 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002409 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2410}
2411
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002412void ASTWriter::WriteComments() {
2413 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002414 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002415 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002416 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2417 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002418 I != E; ++I) {
2419 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002420 AddSourceRange((*I)->getSourceRange(), Record);
2421 Record.push_back((*I)->getKind());
2422 Record.push_back((*I)->isTrailingComment());
2423 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002424 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2425 }
2426 Stream.ExitBlock();
2427}
2428
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002429//===----------------------------------------------------------------------===//
2430// Global Method Pool and Selector Serialization
2431//===----------------------------------------------------------------------===//
2432
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002433namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002434// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002435class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002436 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002437
2438public:
2439 typedef Selector key_type;
2440 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002441
Sebastian Redl5d050072010-08-04 17:20:04 +00002442 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002443 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002444 ObjCMethodList Instance, Factory;
2445 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002446 typedef const data_type& data_type_ref;
2447
Sebastian Redl3397c552010-08-18 23:56:27 +00002448 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002450 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002451 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002452 }
Mike Stump1eb44332009-09-09 15:08:12 +00002453
2454 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002455 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002456 data_type_ref Methods) {
2457 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2458 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002459 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2460 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002461 Method = Method->Next)
2462 if (Method->Method)
2463 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002464 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002465 Method = Method->Next)
2466 if (Method->Method)
2467 DataLen += 4;
2468 clang::io::Emit16(Out, DataLen);
2469 return std::make_pair(KeyLen, DataLen);
2470 }
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Chris Lattner5f9e2722011-07-23 10:55:15 +00002472 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002473 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002474 assert((Start >> 32) == 0 && "Selector key offset too large");
2475 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002476 unsigned N = Sel.getNumArgs();
2477 clang::io::Emit16(Out, N);
2478 if (N == 0)
2479 N = 1;
2480 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002481 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002482 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2483 }
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Chris Lattner5f9e2722011-07-23 10:55:15 +00002485 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002486 data_type_ref Methods, unsigned DataLen) {
2487 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002488 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002489 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002490 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002491 Method = Method->Next)
2492 if (Method->Method)
2493 ++NumInstanceMethods;
2494
2495 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002496 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002497 Method = Method->Next)
2498 if (Method->Method)
2499 ++NumFactoryMethods;
2500
2501 clang::io::Emit16(Out, NumInstanceMethods);
2502 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002503 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002504 Method = Method->Next)
2505 if (Method->Method)
2506 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002507 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002508 Method = Method->Next)
2509 if (Method->Method)
2510 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002511
2512 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002513 }
2514};
2515} // end anonymous namespace
2516
Sebastian Redl059612d2010-08-03 21:58:15 +00002517/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002518///
2519/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002520/// in an on-disk hash table indexed by the selector. The hash table also
2521/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002522void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002523 using namespace llvm;
2524
Sebastian Redl059612d2010-08-03 21:58:15 +00002525 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002526 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002527 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002528 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002529 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002530 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002531 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002532 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002533
Sebastian Redl059612d2010-08-03 21:58:15 +00002534 // Create the on-disk hash table representation. We walk through every
2535 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002536 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002537 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002538 I = SelectorIDs.begin(), E = SelectorIDs.end();
2539 I != E; ++I) {
2540 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002541 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002542 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002543 I->second,
2544 ObjCMethodList(),
2545 ObjCMethodList()
2546 };
2547 if (F != SemaRef.MethodPool.end()) {
2548 Data.Instance = F->second.first;
2549 Data.Factory = F->second.second;
2550 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002551 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002552 // changed.
2553 if (Chain && I->second < FirstSelectorID) {
2554 // Selector already exists. Did it change?
2555 bool changed = false;
2556 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2557 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002558 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002559 changed = true;
2560 }
2561 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2562 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002563 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002564 changed = true;
2565 }
2566 if (!changed)
2567 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002568 } else if (Data.Instance.Method || Data.Factory.Method) {
2569 // A new method pool entry.
2570 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002571 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002572 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002573 }
2574
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002575 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002576 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002577 uint32_t BucketOffset;
2578 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002579 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002580 llvm::raw_svector_ostream Out(MethodPool);
2581 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002582 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002583 BucketOffset = Generator.Emit(Out, Trait);
2584 }
2585
2586 // Create a blob abbreviation
2587 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002588 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002589 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002590 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002591 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2592 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2593
Douglas Gregor83941df2009-04-25 17:48:32 +00002594 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002595 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002596 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002597 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002598 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002599 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002600
2601 // Create a blob abbreviation for the selector table offsets.
2602 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002603 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002604 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2607 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2608
2609 // Write the selector offsets table.
2610 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002612 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002613 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002614 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002615 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002616 }
2617}
2618
Sebastian Redl3397c552010-08-18 23:56:27 +00002619/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002620void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002621 using namespace llvm;
2622 if (SemaRef.ReferencedSelectors.empty())
2623 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002624
Fariborz Jahanian32019832010-07-23 19:11:11 +00002625 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002626
Sebastian Redl3397c552010-08-18 23:56:27 +00002627 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002628 // very tricky to fix, and given that @selector shouldn't really appear in
2629 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002630 for (DenseMap<Selector, SourceLocation>::iterator S =
2631 SemaRef.ReferencedSelectors.begin(),
2632 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2633 Selector Sel = (*S).first;
2634 SourceLocation Loc = (*S).second;
2635 AddSelectorRef(Sel, Record);
2636 AddSourceLocation(Loc, Record);
2637 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002638 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002639}
2640
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002641//===----------------------------------------------------------------------===//
2642// Identifier Table Serialization
2643//===----------------------------------------------------------------------===//
2644
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002645namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002646class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002647 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002648 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002649 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002650 bool IsModule;
2651
Douglas Gregora92193e2009-04-28 21:18:29 +00002652 /// \brief Determines whether this is an "interesting" identifier
2653 /// that needs a full IdentifierInfo structure written into the hash
2654 /// table.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002655 bool isInterestingIdentifier(IdentifierInfo *II, MacroDirective *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002656 if (II->isPoisoned() ||
2657 II->isExtensionToken() ||
2658 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002659 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002660 II->getFETokenInfo<void>())
2661 return true;
2662
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002663 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002664 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002665
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002666 bool hadMacroDefinition(IdentifierInfo *II, MacroDirective *&Macro) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002667 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002668 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002669
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002670 if (Macro || (Macro = PP.getMacroDirectiveHistory(II)))
2671 return !Macro->getInfo()->isBuiltinMacro() &&
2672 (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002673
2674 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002675 }
2676
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002677public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002678 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002679 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002681 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002682 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Douglas Gregoreee242f2011-10-27 09:33:13 +00002684 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2685 IdentifierResolver &IdResolver, bool IsModule)
2686 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002687
2688 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002689 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002690 }
Mike Stump1eb44332009-09-09 15:08:12 +00002691
2692 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002693 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002694 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002695 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002696 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002697 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002698 DataLen += 2; // 2 bytes for builtin ID
2699 DataLen += 2; // 2 bytes for flags
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002700 if (hadMacroDefinition(II, Macro)) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002701 for (MacroDirective *M = Macro; M; M = M->getPrevious()) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002702 if (Writer.getMacroRef(M) != 0)
2703 DataLen += 4;
2704 }
2705
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002706 DataLen += 4;
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002707 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002708
Douglas Gregoreee242f2011-10-27 09:33:13 +00002709 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2710 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002711 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002712 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002713 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002714 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002715 // We emit the key length after the data length so that every
2716 // string is preceded by a 16-bit length. This matches the PTH
2717 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002718 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002719 return std::make_pair(KeyLen, DataLen);
2720 }
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Chris Lattner5f9e2722011-07-23 10:55:15 +00002722 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002723 unsigned KeyLen) {
2724 // Record the location of the key data. This is used when generating
2725 // the mapping from persistent IDs to strings.
2726 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002727 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002728 }
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Douglas Gregor7143aab2011-09-01 17:04:32 +00002730 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002731 IdentID ID, unsigned) {
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002732 MacroDirective *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002733 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002734 clang::io::Emit32(Out, ID << 1);
2735 return;
2736 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002737
Douglas Gregora92193e2009-04-28 21:18:29 +00002738 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002739 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2740 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2741 clang::io::Emit16(Out, Bits);
2742 Bits = 0;
2743 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002744 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002745 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2746 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002747 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002748 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002749 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002750
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002751 if (HadMacroDefinition) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002752 // Write all of the macro IDs associated with this identifier.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00002753 for (MacroDirective *M = Macro; M; M = M->getPrevious()) {
Argyrios Kyrtzidisdc1088f2013-01-19 03:14:56 +00002754 if (MacroID ID = Writer.getMacroRef(M))
2755 clang::io::Emit32(Out, ID);
2756 }
2757
2758 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002759 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002760
Douglas Gregor668c1a42009-04-21 22:25:48 +00002761 // Emit the declaration IDs in reverse order, because the
2762 // IdentifierResolver provides the declarations as they would be
2763 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002764 // "stat"), but the ASTReader adds declarations to the end of the list
2765 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002766 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002767 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2768 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002769 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002770 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002771 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002772 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002773 }
2774};
2775} // end anonymous namespace
2776
Sebastian Redl3397c552010-08-18 23:56:27 +00002777/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002778///
2779/// The identifier table consists of a blob containing string data
2780/// (the actual identifiers themselves) and a separate "offsets" index
2781/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002782void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2783 IdentifierResolver &IdResolver,
2784 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002785 using namespace llvm;
2786
2787 // Create and write out the blob that contains the identifier
2788 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002789 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002790 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002791 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002792
Douglas Gregor92b059e2009-04-28 20:33:11 +00002793 // Look for any identifiers that were named while processing the
2794 // headers, but are otherwise not needed. We add these to the hash
2795 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002796 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002797 // file.
2798 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2799 IDEnd = PP.getIdentifierTable().end();
2800 ID != IDEnd; ++ID)
2801 getIdentifierRef(ID->second);
2802
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002803 // Create the on-disk hash table representation. We only store offsets
2804 // for identifiers that appear here for the first time.
2805 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002806 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002807 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2808 ID != IDEnd; ++ID) {
2809 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002810 if (!Chain || !ID->first->isFromAST() ||
2811 ID->first->hasChangedSinceDeserialization())
Douglas Gregor2d1ece82013-02-08 21:30:59 +00002812 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
Douglas Gregor7143aab2011-09-01 17:04:32 +00002813 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002814 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002815
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002816 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002817 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002818 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002819 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002820 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002821 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002822 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002823 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002824 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002825 }
2826
2827 // Create a blob abbreviation
2828 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002829 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002831 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002832 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002833
2834 // Write the identifier table
2835 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002836 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002837 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002838 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002839 }
2840
2841 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002842 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002843 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002844 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002845 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2847 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2848
Douglas Gregor2d1ece82013-02-08 21:30:59 +00002849#ifndef NDEBUG
2850 for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
2851 assert(IdentifierOffsets[I] && "Missing identifier offset?");
2852#endif
2853
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002854 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002855 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002856 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002857 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002858 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002859 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002860}
2861
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002862//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002863// DeclContext's Name Lookup Table Serialization
2864//===----------------------------------------------------------------------===//
2865
2866namespace {
2867// Trait used for the on-disk hash table used in the method pool.
2868class ASTDeclContextNameLookupTrait {
2869 ASTWriter &Writer;
2870
2871public:
2872 typedef DeclarationName key_type;
2873 typedef key_type key_type_ref;
2874
2875 typedef DeclContext::lookup_result data_type;
2876 typedef const data_type& data_type_ref;
2877
2878 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2879
2880 unsigned ComputeHash(DeclarationName Name) {
2881 llvm::FoldingSetNodeID ID;
2882 ID.AddInteger(Name.getNameKind());
2883
2884 switch (Name.getNameKind()) {
2885 case DeclarationName::Identifier:
2886 ID.AddString(Name.getAsIdentifierInfo()->getName());
2887 break;
2888 case DeclarationName::ObjCZeroArgSelector:
2889 case DeclarationName::ObjCOneArgSelector:
2890 case DeclarationName::ObjCMultiArgSelector:
2891 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2892 break;
2893 case DeclarationName::CXXConstructorName:
2894 case DeclarationName::CXXDestructorName:
2895 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002896 break;
2897 case DeclarationName::CXXOperatorName:
2898 ID.AddInteger(Name.getCXXOverloadedOperator());
2899 break;
2900 case DeclarationName::CXXLiteralOperatorName:
2901 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2902 case DeclarationName::CXXUsingDirective:
2903 break;
2904 }
2905
2906 return ID.ComputeHash();
2907 }
2908
2909 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002910 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002911 data_type_ref Lookup) {
2912 unsigned KeyLen = 1;
2913 switch (Name.getNameKind()) {
2914 case DeclarationName::Identifier:
2915 case DeclarationName::ObjCZeroArgSelector:
2916 case DeclarationName::ObjCOneArgSelector:
2917 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002918 case DeclarationName::CXXLiteralOperatorName:
2919 KeyLen += 4;
2920 break;
2921 case DeclarationName::CXXOperatorName:
2922 KeyLen += 1;
2923 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002924 case DeclarationName::CXXConstructorName:
2925 case DeclarationName::CXXDestructorName:
2926 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002927 case DeclarationName::CXXUsingDirective:
2928 break;
2929 }
2930 clang::io::Emit16(Out, KeyLen);
2931
2932 // 2 bytes for num of decls and 4 for each DeclID.
David Blaikie3bc93e32012-12-19 00:45:41 +00002933 unsigned DataLen = 2 + 4 * Lookup.size();
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002934 clang::io::Emit16(Out, DataLen);
2935
2936 return std::make_pair(KeyLen, DataLen);
2937 }
2938
Chris Lattner5f9e2722011-07-23 10:55:15 +00002939 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002940 using namespace clang::io;
2941
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002942 Emit8(Out, Name.getNameKind());
2943 switch (Name.getNameKind()) {
2944 case DeclarationName::Identifier:
2945 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002946 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002947 case DeclarationName::ObjCZeroArgSelector:
2948 case DeclarationName::ObjCOneArgSelector:
2949 case DeclarationName::ObjCMultiArgSelector:
2950 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002951 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002952 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002953 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2954 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002955 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002956 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002957 case DeclarationName::CXXLiteralOperatorName:
2958 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002959 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002960 case DeclarationName::CXXConstructorName:
2961 case DeclarationName::CXXDestructorName:
2962 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002963 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002964 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002965 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002966
2967 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002968 }
2969
Chris Lattner5f9e2722011-07-23 10:55:15 +00002970 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002971 data_type Lookup, unsigned DataLen) {
2972 uint64_t Start = Out.tell(); (void)Start;
David Blaikie3bc93e32012-12-19 00:45:41 +00002973 clang::io::Emit16(Out, Lookup.size());
2974 for (DeclContext::lookup_iterator I = Lookup.begin(), E = Lookup.end();
2975 I != E; ++I)
2976 clang::io::Emit32(Out, Writer.GetDeclRef(*I));
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002977
2978 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2979 }
2980};
2981} // end anonymous namespace
2982
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002983/// \brief Write the block containing all of the declaration IDs
2984/// visible from the given DeclContext.
2985///
2986/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002987/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002988uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2989 DeclContext *DC) {
2990 if (DC->getPrimaryContext() != DC)
2991 return 0;
2992
2993 // Since there is no name lookup into functions or methods, don't bother to
2994 // build a visible-declarations table for these entities.
2995 if (DC->isFunctionOrMethod())
2996 return 0;
2997
2998 // If not in C++, we perform name lookup for the translation unit via the
2999 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3000 // FIXME: In C++ we need the visible declarations in order to "see" the
3001 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003002 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003003 return 0;
3004
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003005 // Serialize the contents of the mapping used for lookup. Note that,
3006 // although we have two very different code paths, the serialized
3007 // representation is the same for both cases: a declaration name,
3008 // followed by a size, followed by references to the visible
3009 // declarations that have that name.
3010 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003011 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003012 if (!Map || Map->empty())
3013 return 0;
3014
3015 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3016 ASTDeclContextNameLookupTrait Trait(*this);
3017
3018 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003019 DeclarationName ConversionName;
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003020 SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003021 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3022 D != DEnd; ++D) {
3023 DeclarationName Name = D->first;
3024 DeclContext::lookup_result Result = D->second.getLookupResult();
David Blaikie3bc93e32012-12-19 00:45:41 +00003025 if (!Result.empty()) {
Douglas Gregore5a54b62011-08-30 20:49:19 +00003026 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3027 // Hash all conversion function names to the same name. The actual
3028 // type information in conversion function name is not used in the
3029 // key (since such type information is not stable across different
3030 // modules), so the intended effect is to coalesce all of the conversion
3031 // functions under a single key.
3032 if (!ConversionName)
3033 ConversionName = Name;
David Blaikie3bc93e32012-12-19 00:45:41 +00003034 ConversionDecls.append(Result.begin(), Result.end());
Douglas Gregore5a54b62011-08-30 20:49:19 +00003035 continue;
3036 }
3037
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003038 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003039 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003040 }
3041
Douglas Gregore5a54b62011-08-30 20:49:19 +00003042 // Add the conversion functions
3043 if (!ConversionDecls.empty()) {
3044 Generator.insert(ConversionName,
3045 DeclContext::lookup_result(ConversionDecls.begin(),
3046 ConversionDecls.end()),
3047 Trait);
3048 }
3049
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003050 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003051 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003052 uint32_t BucketOffset;
3053 {
3054 llvm::raw_svector_ostream Out(LookupTable);
3055 // Make sure that no bucket is at offset 0
3056 clang::io::Emit32(Out, 0);
3057 BucketOffset = Generator.Emit(Out, Trait);
3058 }
3059
3060 // Write the lookup table
3061 RecordData Record;
3062 Record.push_back(DECL_CONTEXT_VISIBLE);
3063 Record.push_back(BucketOffset);
3064 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3065 LookupTable.str());
3066
3067 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3068 ++NumVisibleDeclContexts;
3069 return Offset;
3070}
3071
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003072/// \brief Write an UPDATE_VISIBLE block for the given context.
3073///
3074/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3075/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003076/// (in C++), for namespaces, and for classes with forward-declared unscoped
3077/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003078void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003079 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3080 if (!Map || Map->empty())
3081 return;
3082
3083 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3084 ASTDeclContextNameLookupTrait Trait(*this);
3085
3086 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003087 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3088 D != DEnd; ++D) {
3089 DeclarationName Name = D->first;
3090 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003091 // For any name that appears in this table, the results are complete, i.e.
3092 // they overwrite results from previous PCHs. Merging is always a mess.
David Blaikie3bc93e32012-12-19 00:45:41 +00003093 if (!Result.empty())
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003094 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003095 }
3096
3097 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003098 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003099 uint32_t BucketOffset;
3100 {
3101 llvm::raw_svector_ostream Out(LookupTable);
3102 // Make sure that no bucket is at offset 0
3103 clang::io::Emit32(Out, 0);
3104 BucketOffset = Generator.Emit(Out, Trait);
3105 }
3106
3107 // Write the lookup table
3108 RecordData Record;
3109 Record.push_back(UPDATE_VISIBLE);
3110 Record.push_back(getDeclID(cast<Decl>(DC)));
3111 Record.push_back(BucketOffset);
3112 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3113}
3114
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003115/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3116void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3117 RecordData Record;
3118 Record.push_back(Opts.fp_contract);
3119 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3120}
3121
3122/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3123void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003124 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003125 return;
3126
3127 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3128 RecordData Record;
3129#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3130#include "clang/Basic/OpenCLExtensions.def"
3131 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3132}
3133
Douglas Gregor2171bf12012-01-15 16:58:34 +00003134void ASTWriter::WriteRedeclarations() {
3135 RecordData LocalRedeclChains;
3136 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3137
3138 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3139 Decl *First = Redeclarations[I];
3140 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3141
3142 Decl *MostRecent = First->getMostRecentDecl();
3143
3144 // If we only have a single declaration, there is no point in storing
3145 // a redeclaration chain.
3146 if (First == MostRecent)
3147 continue;
3148
3149 unsigned Offset = LocalRedeclChains.size();
3150 unsigned Size = 0;
3151 LocalRedeclChains.push_back(0); // Placeholder for the size.
3152
3153 // Collect the set of local redeclarations of this declaration.
Douglas Gregoraa945902013-02-18 15:53:43 +00003154 for (Decl *Prev = MostRecent; Prev != First;
Douglas Gregor2171bf12012-01-15 16:58:34 +00003155 Prev = Prev->getPreviousDecl()) {
3156 if (!Prev->isFromASTFile()) {
3157 AddDeclRef(Prev, LocalRedeclChains);
3158 ++Size;
3159 }
3160 }
Douglas Gregoraa945902013-02-18 15:53:43 +00003161
3162 if (!First->isFromASTFile() && Chain) {
3163 Decl *FirstFromAST = MostRecent;
3164 for (Decl *Prev = MostRecent; Prev; Prev = Prev->getPreviousDecl()) {
3165 if (Prev->isFromASTFile())
3166 FirstFromAST = Prev;
3167 }
3168
3169 Chain->MergedDecls[FirstFromAST].push_back(getDeclID(First));
3170 }
3171
Douglas Gregor2171bf12012-01-15 16:58:34 +00003172 LocalRedeclChains[Offset] = Size;
3173
3174 // Reverse the set of local redeclarations, so that we store them in
3175 // order (since we found them in reverse order).
3176 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3177
Douglas Gregoraa945902013-02-18 15:53:43 +00003178 // Add the mapping from the first ID from the AST to the set of local
3179 // declarations.
Douglas Gregor2171bf12012-01-15 16:58:34 +00003180 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3181 LocalRedeclsMap.push_back(Info);
3182
3183 assert(N == Redeclarations.size() &&
3184 "Deserialized a declaration we shouldn't have");
3185 }
3186
3187 if (LocalRedeclChains.empty())
3188 return;
3189
3190 // Sort the local redeclarations map by the first declaration ID,
3191 // since the reader will be performing binary searches on this information.
3192 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3193
3194 // Emit the local redeclarations map.
3195 using namespace llvm;
3196 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3197 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3200 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3201
3202 RecordData Record;
3203 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3204 Record.push_back(LocalRedeclsMap.size());
3205 Stream.EmitRecordWithBlob(AbbrevID, Record,
3206 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3207 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3208
3209 // Emit the redeclaration chains.
3210 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3211}
3212
Douglas Gregorcff9f262012-01-27 01:47:08 +00003213void ASTWriter::WriteObjCCategories() {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00003214 SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
Douglas Gregorcff9f262012-01-27 01:47:08 +00003215 RecordData Categories;
3216
3217 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3218 unsigned Size = 0;
3219 unsigned StartIndex = Categories.size();
3220
3221 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3222
3223 // Allocate space for the size.
3224 Categories.push_back(0);
3225
3226 // Add the categories.
Douglas Gregord3297242013-01-16 23:00:23 +00003227 for (ObjCInterfaceDecl::known_categories_iterator
3228 Cat = Class->known_categories_begin(),
3229 CatEnd = Class->known_categories_end();
3230 Cat != CatEnd; ++Cat, ++Size) {
3231 assert(getDeclID(*Cat) != 0 && "Bogus category");
3232 AddDeclRef(*Cat, Categories);
Douglas Gregorcff9f262012-01-27 01:47:08 +00003233 }
3234
3235 // Update the size.
3236 Categories[StartIndex] = Size;
3237
3238 // Record this interface -> category map.
3239 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3240 CategoriesMap.push_back(CatInfo);
3241 }
3242
3243 // Sort the categories map by the definition ID, since the reader will be
3244 // performing binary searches on this information.
3245 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3246
3247 // Emit the categories map.
3248 using namespace llvm;
3249 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3250 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3253 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3254
3255 RecordData Record;
3256 Record.push_back(OBJC_CATEGORIES_MAP);
3257 Record.push_back(CategoriesMap.size());
3258 Stream.EmitRecordWithBlob(AbbrevID, Record,
3259 reinterpret_cast<char*>(CategoriesMap.data()),
3260 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3261
3262 // Emit the category lists.
3263 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3264}
3265
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003266void ASTWriter::WriteMergedDecls() {
3267 if (!Chain || Chain->MergedDecls.empty())
3268 return;
3269
3270 RecordData Record;
3271 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3272 IEnd = Chain->MergedDecls.end();
3273 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003274 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003275 : getDeclID(I->first);
3276 assert(CanonID && "Merged declaration not known?");
3277
3278 Record.push_back(CanonID);
3279 Record.push_back(I->second.size());
3280 Record.append(I->second.begin(), I->second.end());
3281 }
3282 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3283}
3284
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003285//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003286// General Serialization Routines
3287//===----------------------------------------------------------------------===//
3288
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003289/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003290void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3291 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003292 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003293 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3294 e = Attrs.end(); i != e; ++i){
3295 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003296 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003297 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003298
Sean Huntcf807c42010-08-18 23:23:40 +00003299#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003300
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003301 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003302}
3303
Chris Lattner5f9e2722011-07-23 10:55:15 +00003304void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003305 Record.push_back(Str.size());
3306 Record.insert(Record.end(), Str.begin(), Str.end());
3307}
3308
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003309void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3310 RecordDataImpl &Record) {
3311 Record.push_back(Version.getMajor());
David Blaikiedc84cd52013-02-20 22:23:23 +00003312 if (Optional<unsigned> Minor = Version.getMinor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003313 Record.push_back(*Minor + 1);
3314 else
3315 Record.push_back(0);
David Blaikiedc84cd52013-02-20 22:23:23 +00003316 if (Optional<unsigned> Subminor = Version.getSubminor())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003317 Record.push_back(*Subminor + 1);
3318 else
3319 Record.push_back(0);
3320}
3321
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003322/// \brief Note that the identifier II occurs at the given offset
3323/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003324void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003325 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003326 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003327 // up earlier in the chain and thus don't need an offset.
3328 if (ID >= FirstIdentID)
3329 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003330}
3331
Douglas Gregor83941df2009-04-25 17:48:32 +00003332/// \brief Note that the selector Sel occurs at the given offset
3333/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003334void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003335 unsigned ID = SelectorIDs[Sel];
3336 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003337 // Don't record offsets for selectors that are also available in a different
3338 // file.
3339 if (ID < FirstSelectorID)
3340 return;
3341 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003342}
3343
Sebastian Redla4232eb2010-08-18 23:56:21 +00003344ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003345 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003346 WritingAST(false), DoneWritingDeclsAndTypes(false),
3347 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003348 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003349 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003350 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3351 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003352 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3353 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003354 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003355 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003356 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003357 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003358 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003359 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003360 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3361 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3362 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003363 DeclTypedefAbbrev(0),
3364 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3365 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003366{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003367}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003368
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003369ASTWriter::~ASTWriter() {
3370 for (FileDeclIDsTy::iterator
3371 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3372 delete I->second;
3373}
3374
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003375void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003376 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003377 Module *WritingModule, StringRef isysroot,
3378 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003379 WritingAST = true;
3380
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003381 ASTHasCompilerErrors = hasErrors;
3382
Douglas Gregor2cf26342009-04-09 22:27:44 +00003383 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003384 Stream.Emit((unsigned)'C', 8);
3385 Stream.Emit((unsigned)'P', 8);
3386 Stream.Emit((unsigned)'C', 8);
3387 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003388
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003389 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003390
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003391 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003392 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003393 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003394 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003395 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003396 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003397 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003398
3399 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003400}
3401
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003402template<typename Vector>
3403static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3404 ASTWriter::RecordData &Record) {
3405 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3406 I != E; ++I) {
3407 Writer.AddDeclRef(*I, Record);
3408 }
3409}
3410
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003411void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003412 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003413 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003414 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003415 using namespace llvm;
3416
Douglas Gregorecc2c092011-12-01 22:20:10 +00003417 // Make sure that the AST reader knows to finalize itself.
3418 if (Chain)
3419 Chain->finalizeForWriting();
3420
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003421 ASTContext &Context = SemaRef.Context;
3422 Preprocessor &PP = SemaRef.PP;
3423
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003424 // Set up predefined declaration IDs.
3425 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003426 if (Context.ObjCIdDecl)
3427 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003428 if (Context.ObjCSelDecl)
3429 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003430 if (Context.ObjCClassDecl)
3431 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003432 if (Context.ObjCProtocolClassDecl)
3433 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003434 if (Context.Int128Decl)
3435 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3436 if (Context.UInt128Decl)
3437 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003438 if (Context.ObjCInstanceTypeDecl)
3439 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003440 if (Context.BuiltinVaListDecl)
3441 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3442
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003443 if (!Chain) {
3444 // Make sure that we emit IdentifierInfos (and any attached
3445 // declarations) for builtins. We don't need to do this when we're
3446 // emitting chained PCH files, because all of the builtins will be
3447 // in the original PCH file.
3448 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003449 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003450 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003451 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003452 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003453 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3454 getIdentifierRef(&Table.get(BuiltinNames[I]));
3455 }
3456
Douglas Gregoreee242f2011-10-27 09:33:13 +00003457 // If there are any out-of-date identifiers, bring them up to date.
3458 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
Douglas Gregor589dae72013-01-07 16:56:53 +00003459 // Find out-of-date identifiers.
3460 SmallVector<IdentifierInfo *, 4> OutOfDate;
Douglas Gregoreee242f2011-10-27 09:33:13 +00003461 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3462 IDEnd = PP.getIdentifierTable().end();
Douglas Gregor589dae72013-01-07 16:56:53 +00003463 ID != IDEnd; ++ID) {
Douglas Gregoreee242f2011-10-27 09:33:13 +00003464 if (ID->second->isOutOfDate())
Douglas Gregor589dae72013-01-07 16:56:53 +00003465 OutOfDate.push_back(ID->second);
3466 }
3467
3468 // Update the out-of-date identifiers.
3469 for (unsigned I = 0, N = OutOfDate.size(); I != N; ++I) {
3470 ExtSource->updateOutOfDateIdentifier(*OutOfDate[I]);
3471 }
Douglas Gregoreee242f2011-10-27 09:33:13 +00003472 }
3473
Chris Lattner63d65f82009-09-08 18:19:27 +00003474 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003475 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003476 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003477 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003478 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003479
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003480 // Build a record containing all of the file scoped decls in this file.
3481 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003482 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3483 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003484
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003485 // Build a record containing all of the delegating constructors we still need
3486 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003487 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003488 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003489
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003490 // Write the set of weak, undeclared identifiers. We always write the
3491 // entire table, since later PCH files in a PCH chain are only interested in
3492 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003493 RecordData WeakUndeclaredIdentifiers;
3494 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003495 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003496 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3497 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3498 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3499 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3500 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3501 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3502 }
3503 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003504
Richard Smith5ea6ef42013-01-10 23:43:47 +00003505 // Build a record containing all of the locally-scoped extern "C"
Douglas Gregor14c22f22009-04-22 22:18:58 +00003506 // declarations in this header file. Generally, this record will be
3507 // empty.
Richard Smith5ea6ef42013-01-10 23:43:47 +00003508 RecordData LocallyScopedExternCDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003509 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003510 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003511 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Richard Smith5ea6ef42013-01-10 23:43:47 +00003512 TD = SemaRef.LocallyScopedExternCDecls.begin(),
3513 TDEnd = SemaRef.LocallyScopedExternCDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003514 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003515 if (!TD->second->isFromASTFile())
Richard Smith5ea6ef42013-01-10 23:43:47 +00003516 AddDeclRef(TD->second, LocallyScopedExternCDecls);
Douglas Gregorec12ce22011-07-28 14:20:37 +00003517 }
3518
Douglas Gregorb81c1702009-04-27 20:06:05 +00003519 // Build a record containing all of the ext_vector declarations.
3520 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003521 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003522
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003523 // Build a record containing all of the VTable uses information.
3524 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003525 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003526 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3527 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3528 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3529 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3530 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003531 }
3532
3533 // Build a record containing all of dynamic classes declarations.
3534 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003535 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003536
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003537 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003538 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003539 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003540 I = SemaRef.PendingInstantiations.begin(),
3541 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3542 AddDeclRef(I->first, PendingInstantiations);
3543 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003544 }
3545 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3546 "There are local ones at end of translation unit!");
3547
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003548 // Build a record containing some declaration references.
3549 RecordData SemaDeclRefs;
3550 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3551 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3552 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3553 }
3554
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003555 RecordData CUDASpecialDeclRefs;
3556 if (Context.getcudaConfigureCallDecl()) {
3557 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3558 }
3559
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003560 // Build a record containing all of the known namespaces.
3561 RecordData KnownNamespaces;
Nick Lewycky01a41142013-01-26 00:35:08 +00003562 for (llvm::MapVector<NamespaceDecl*, bool>::iterator
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003563 I = SemaRef.KnownNamespaces.begin(),
3564 IEnd = SemaRef.KnownNamespaces.end();
3565 I != IEnd; ++I) {
3566 if (!I->second)
3567 AddDeclRef(I->first, KnownNamespaces);
3568 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003569
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003570 // Build a record of all used, undefined objects that require definitions.
3571 RecordData UndefinedButUsed;
Nick Lewycky995e26b2013-01-31 03:23:57 +00003572
3573 SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003574 SemaRef.getUndefinedButUsed(Undefined);
Nick Lewycky995e26b2013-01-31 03:23:57 +00003575 for (SmallVectorImpl<std::pair<NamedDecl *, SourceLocation> >::iterator
3576 I = Undefined.begin(), E = Undefined.end(); I != E; ++I) {
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003577 AddDeclRef(I->first, UndefinedButUsed);
3578 AddSourceLocation(I->second, UndefinedButUsed);
Nick Lewycky01a41142013-01-26 00:35:08 +00003579 }
3580
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003581 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003582 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003583
Sebastian Redl3397c552010-08-18 23:56:27 +00003584 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003585 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003586 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003587
Argyrios Kyrtzidis5e24f2d2012-12-13 21:38:23 +00003588 // This is so that older clang versions, before the introduction
3589 // of the control block, can read and reject the newer PCH format.
3590 Record.clear();
3591 Record.push_back(VERSION_MAJOR);
3592 Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
3593
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003594 // Create a lexical update block containing all of the declarations in the
3595 // translation unit that do not come from other AST files.
3596 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3597 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3598 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3599 E = TU->noload_decls_end();
3600 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003601 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003602 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003603 }
3604
3605 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3606 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3607 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3608 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3609 Record.clear();
3610 Record.push_back(TU_UPDATE_LEXICAL);
3611 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3612 data(NewGlobalDecls));
3613
3614 // And a visible updates block for the translation unit.
3615 Abv = new llvm::BitCodeAbbrev();
3616 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3617 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3618 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3619 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3620 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3621 WriteDeclContextVisibleUpdate(TU);
3622
3623 // If the translation unit has an anonymous namespace, and we don't already
3624 // have an update block for it, write it as an update block.
3625 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3626 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3627 if (Record.empty()) {
3628 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003629 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003630 }
3631 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003632
3633 // Make sure visible decls, added to DeclContexts previously loaded from
3634 // an AST file, are registered for serialization.
3635 for (SmallVector<const Decl *, 16>::iterator
3636 I = UpdatingVisibleDecls.begin(),
3637 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3638 GetDeclRef(*I);
3639 }
3640
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003641 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003642 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003643
Douglas Gregora119da02011-08-02 16:26:37 +00003644 // Form the record of special types.
3645 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003646 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003647 AddTypeRef(Context.getFILEType(), SpecialTypes);
3648 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3649 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3650 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3651 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003652 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003653 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003654
Douglas Gregor366809a2009-04-26 03:49:13 +00003655 // Keep writing types and declarations until all types and
3656 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003657 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003658 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003659 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3660 E = DeclsToRewrite.end();
3661 I != E; ++I)
3662 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003663 while (!DeclTypesToEmit.empty()) {
3664 DeclOrType DOT = DeclTypesToEmit.front();
3665 DeclTypesToEmit.pop();
3666 if (DOT.isType())
3667 WriteType(DOT.getType());
3668 else
3669 WriteDecl(Context, DOT.getDecl());
3670 }
3671 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003672
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003673 DoneWritingDeclsAndTypes = true;
3674
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003675 WriteFileDeclIDsMap();
3676 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003677 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003678
3679 if (Chain) {
3680 // Write the mapping information describing our module dependencies and how
3681 // each of those modules were mapped into our own offset/ID space, so that
3682 // the reader can build the appropriate mapping to its own offset/ID space.
3683 // The map consists solely of a blob with the following format:
3684 // *(module-name-len:i16 module-name:len*i8
3685 // source-location-offset:i32
3686 // identifier-id:i32
3687 // preprocessed-entity-id:i32
3688 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003689 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003690 // selector-id:i32
3691 // declaration-id:i32
3692 // c++-base-specifiers-id:i32
3693 // type-id:i32)
3694 //
3695 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3696 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3698 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003699 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003700 {
3701 llvm::raw_svector_ostream Out(Buffer);
3702 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003703 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003704 M != MEnd; ++M) {
3705 StringRef FileName = (*M)->FileName;
3706 io::Emit16(Out, FileName.size());
3707 Out.write(FileName.data(), FileName.size());
3708 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3709 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003710 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003711 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003712 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003713 io::Emit32(Out, (*M)->BaseSelectorID);
3714 io::Emit32(Out, (*M)->BaseDeclID);
3715 io::Emit32(Out, (*M)->BaseTypeIndex);
3716 }
3717 }
3718 Record.clear();
3719 Record.push_back(MODULE_OFFSET_MAP);
3720 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3721 Buffer.data(), Buffer.size());
3722 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003723 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003724 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003725 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003726 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003727 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003728 WriteFPPragmaOptions(SemaRef.getFPOptions());
3729 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003730
Sebastian Redl1476ed42010-07-16 16:36:56 +00003731 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003732 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003733
Anders Carlssonc8505782011-03-06 18:41:18 +00003734 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003735
Douglas Gregore209e502011-12-06 01:10:29 +00003736 // If we're emitting a module, write out the submodule information.
3737 if (WritingModule)
3738 WriteSubmodules(WritingModule);
3739
Douglas Gregora119da02011-08-02 16:26:37 +00003740 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3741
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003742 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003743 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003744 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003745
3746 // Write the record containing tentative definitions.
3747 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003748 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003749
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003750 // Write the record containing unused file scoped decls.
3751 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003752 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003753
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003754 // Write the record containing weak undeclared identifiers.
3755 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003756 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003757 WeakUndeclaredIdentifiers);
3758
Richard Smith5ea6ef42013-01-10 23:43:47 +00003759 // Write the record containing locally-scoped extern "C" definitions.
3760 if (!LocallyScopedExternCDecls.empty())
3761 Stream.EmitRecord(LOCALLY_SCOPED_EXTERN_C_DECLS,
3762 LocallyScopedExternCDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003763
3764 // Write the record containing ext_vector type names.
3765 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003766 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003767
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003768 // Write the record containing VTable uses information.
3769 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003770 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003771
3772 // Write the record containing dynamic classes declarations.
3773 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003774 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003775
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003776 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003777 if (!PendingInstantiations.empty())
3778 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003779
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003780 // Write the record containing declaration references of Sema.
3781 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003782 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003783
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003784 // Write the record containing CUDA-specific declaration references.
3785 if (!CUDASpecialDeclRefs.empty())
3786 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003787
3788 // Write the delegating constructors.
3789 if (!DelegatingCtorDecls.empty())
3790 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003791
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003792 // Write the known namespaces.
3793 if (!KnownNamespaces.empty())
3794 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
Nick Lewycky01a41142013-01-26 00:35:08 +00003795
Nick Lewyckycd0655b2013-02-01 08:13:20 +00003796 // Write the undefined internal functions and variables, and inline functions.
3797 if (!UndefinedButUsed.empty())
3798 Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003799
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003800 // Write the visible updates to DeclContexts.
3801 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3802 I = UpdatedDeclContexts.begin(),
3803 E = UpdatedDeclContexts.end();
3804 I != E; ++I)
3805 WriteDeclContextVisibleUpdate(*I);
3806
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003807 if (!WritingModule) {
3808 // Write the submodules that were imported, if any.
3809 RecordData ImportedModules;
3810 for (ASTContext::import_iterator I = Context.local_import_begin(),
3811 IEnd = Context.local_import_end();
3812 I != IEnd; ++I) {
3813 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3814 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3815 }
3816 if (!ImportedModules.empty()) {
3817 // Sort module IDs.
3818 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3819
3820 // Unique module IDs.
3821 ImportedModules.erase(std::unique(ImportedModules.begin(),
3822 ImportedModules.end()),
3823 ImportedModules.end());
3824
3825 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3826 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003827 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003828
3829 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003830 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003831 WriteDeclReplacementsBlock();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003832 WriteRedeclarations();
Douglas Gregoraa945902013-02-18 15:53:43 +00003833 WriteMergedDecls();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003834 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003835
Douglas Gregor3e1af842009-04-17 22:13:46 +00003836 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003837 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003838 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003839 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003840 Record.push_back(NumLexicalDeclContexts);
3841 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003842 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003843 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003844}
3845
Douglas Gregora8235d62012-10-09 23:05:51 +00003846void ASTWriter::WriteMacroUpdates() {
3847 if (MacroUpdates.empty())
3848 return;
3849
3850 RecordData Record;
3851 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3852 E = MacroUpdates.end();
3853 I != E; ++I) {
3854 addMacroRef(I->first, Record);
3855 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003856 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003857 }
3858 Stream.EmitRecord(MACRO_UPDATES, Record);
3859}
3860
Douglas Gregor61c5e342011-09-17 00:05:03 +00003861/// \brief Go through the declaration update blocks and resolve declaration
3862/// pointers into declaration IDs.
3863void ASTWriter::ResolveDeclUpdatesBlocks() {
3864 for (DeclUpdateMap::iterator
3865 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3866 const Decl *D = I->first;
3867 UpdateRecord &URec = I->second;
3868
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003869 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003870 continue; // The decl will be written completely
3871
3872 unsigned Idx = 0, N = URec.size();
3873 while (Idx < N) {
3874 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003875 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3876 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3877 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3878 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3879 ++Idx;
3880 break;
3881
3882 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3883 ++Idx;
3884 break;
3885 }
3886 }
3887 }
3888}
3889
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003890void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003891 if (DeclUpdates.empty())
3892 return;
3893
3894 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003895 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003896 for (DeclUpdateMap::iterator
3897 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3898 const Decl *D = I->first;
3899 UpdateRecord &URec = I->second;
3900
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003901 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003902 continue; // The decl will be written completely,no need to store updates.
3903
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003904 uint64_t Offset = Stream.GetCurrentBitNo();
3905 Stream.EmitRecord(DECL_UPDATES, URec);
3906
3907 OffsetsRecord.push_back(GetDeclRef(D));
3908 OffsetsRecord.push_back(Offset);
3909 }
3910 Stream.ExitBlock();
3911 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3912}
3913
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003914void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003915 if (ReplacedDecls.empty())
3916 return;
3917
3918 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003919 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003920 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003921 Record.push_back(I->ID);
3922 Record.push_back(I->Offset);
3923 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003924 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003925 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003926}
3927
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003928void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003929 Record.push_back(Loc.getRawEncoding());
3930}
3931
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003932void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003933 AddSourceLocation(Range.getBegin(), Record);
3934 AddSourceLocation(Range.getEnd(), Record);
3935}
3936
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003937void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003938 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003939 const uint64_t *Words = Value.getRawData();
3940 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003941}
3942
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003943void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003944 Record.push_back(Value.isUnsigned());
3945 AddAPInt(Value, Record);
3946}
3947
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003948void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003949 AddAPInt(Value.bitcastToAPInt(), Record);
3950}
3951
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003952void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003953 Record.push_back(getIdentifierRef(II));
3954}
3955
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003956void ASTWriter::addMacroRef(MacroDirective *MD, RecordDataImpl &Record) {
3957 Record.push_back(getMacroRef(MD));
Douglas Gregora8235d62012-10-09 23:05:51 +00003958}
3959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003960IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003961 if (II == 0)
3962 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003963
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003964 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003965 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003966 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003967 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003968}
3969
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003970MacroID ASTWriter::getMacroRef(MacroDirective *MD) {
Douglas Gregora8235d62012-10-09 23:05:51 +00003971 // Don't emit builtin macros like __LINE__ to the AST file unless they
3972 // have been redefined by the header (in which case they are not
3973 // isBuiltinMacro).
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003974 if (MD == 0 || MD->getInfo()->isBuiltinMacro())
Douglas Gregora8235d62012-10-09 23:05:51 +00003975 return 0;
3976
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00003977 MacroID &ID = MacroIDs[MD];
Douglas Gregora8235d62012-10-09 23:05:51 +00003978 if (ID == 0)
3979 ID = NextMacroID++;
3980 return ID;
3981}
3982
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003983void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003984 Record.push_back(getSelectorRef(SelRef));
3985}
3986
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003987SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003988 if (Sel.getAsOpaquePtr() == 0) {
3989 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003990 }
3991
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003992 SelectorID SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003993 if (SID == 0 && Chain) {
3994 // This might trigger a ReadSelector callback, which will set the ID for
3995 // this selector.
3996 Chain->LoadSelector(Sel);
Douglas Gregor2d1ece82013-02-08 21:30:59 +00003997 SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003998 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003999 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00004000 SID = NextSelectorID++;
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004001 SelectorIDs[Sel] = SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004002 }
Sebastian Redl5d050072010-08-04 17:20:04 +00004003 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004004}
4005
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004006void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00004007 AddDeclRef(Temp->getDestructor(), Record);
4008}
4009
Douglas Gregor7c789c12010-10-29 22:39:52 +00004010void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4011 CXXBaseSpecifier const *BasesEnd,
4012 RecordDataImpl &Record) {
4013 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4014 CXXBaseSpecifiersToWrite.push_back(
4015 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4016 Bases, BasesEnd));
4017 Record.push_back(NextCXXBaseSpecifiersID++);
4018}
4019
Sebastian Redla4232eb2010-08-18 23:56:21 +00004020void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004021 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004022 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004023 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004024 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004025 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004026 break;
4027 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004028 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004029 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004030 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004031 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004032 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004033 break;
4034 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004035 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004036 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004037 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004038 break;
John McCall833ca992009-10-29 08:12:44 +00004039 case TemplateArgument::Null:
4040 case TemplateArgument::Integral:
4041 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004042 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004043 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004044 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004045 break;
4046 }
4047}
4048
Sebastian Redla4232eb2010-08-18 23:56:21 +00004049void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004050 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004051 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004052
4053 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4054 bool InfoHasSameExpr
4055 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4056 Record.push_back(InfoHasSameExpr);
4057 if (InfoHasSameExpr)
4058 return; // Avoid storing the same expr twice.
4059 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004060 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4061 Record);
4062}
4063
Douglas Gregordc355712011-02-25 00:36:19 +00004064void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4065 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004066 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004067 AddTypeRef(QualType(), Record);
4068 return;
4069 }
4070
Douglas Gregordc355712011-02-25 00:36:19 +00004071 AddTypeLoc(TInfo->getTypeLoc(), Record);
4072}
4073
4074void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4075 AddTypeRef(TL.getType(), Record);
4076
John McCalla1ee0c52009-10-16 21:56:05 +00004077 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004078 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004079 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004080}
4081
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004082void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004083 Record.push_back(GetOrCreateTypeID(T));
4084}
4085
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004086TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4087 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004088 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4089}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004090
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004091TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004092 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004093 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004094}
4095
4096TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4097 if (T.isNull())
4098 return TypeIdx();
4099 assert(!T.getLocalFastQualifiers());
4100
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004101 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004102 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004103 if (DoneWritingDeclsAndTypes) {
4104 assert(0 && "New type seen after serializing all the types to emit!");
4105 return TypeIdx();
4106 }
4107
Douglas Gregor366809a2009-04-26 03:49:13 +00004108 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004109 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004110 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004111 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004112 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004113 return Idx;
4114}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004115
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004116TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004117 if (T.isNull())
4118 return TypeIdx();
4119 assert(!T.getLocalFastQualifiers());
4120
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004121 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4122 assert(I != TypeIdxs.end() && "Type not emitted!");
4123 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004124}
4125
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004126void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004127 Record.push_back(GetDeclRef(D));
4128}
4129
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004130DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004131 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4132
Douglas Gregor2cf26342009-04-09 22:27:44 +00004133 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004134 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004135 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004136
4137 // If D comes from an AST file, its declaration ID is already known and
4138 // fixed.
4139 if (D->isFromASTFile())
4140 return D->getGlobalID();
4141
Douglas Gregor97475832010-10-05 18:37:06 +00004142 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004143 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004144 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004145 if (DoneWritingDeclsAndTypes) {
4146 assert(0 && "New decl seen after serializing all the decls to emit!");
4147 return 0;
4148 }
4149
Douglas Gregor2cf26342009-04-09 22:27:44 +00004150 // We haven't seen this declaration before. Give it a new ID and
4151 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004152 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004153 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004154 }
4155
Sebastian Redl681d7232010-07-27 00:17:23 +00004156 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004157}
4158
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004159DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004160 if (D == 0)
4161 return 0;
4162
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004163 // If D comes from an AST file, its declaration ID is already known and
4164 // fixed.
4165 if (D->isFromASTFile())
4166 return D->getGlobalID();
4167
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004168 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4169 return DeclIDs[D];
4170}
4171
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004172static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4173 std::pair<unsigned, serialization::DeclID> R) {
4174 return L.first < R.first;
4175}
4176
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004177void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004178 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004179 assert(D);
4180
4181 SourceLocation Loc = D->getLocation();
4182 if (Loc.isInvalid())
4183 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004184
4185 // We only keep track of the file-level declarations of each file.
4186 if (!D->getLexicalDeclContext()->isFileContext())
4187 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004188 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4189 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004190 if (isa<ParmVarDecl>(D))
4191 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004192
4193 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004194 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004195 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004196 FileID FID;
4197 unsigned Offset;
4198 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004199 if (FID.isInvalid())
4200 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004201 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004202
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004203 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004204 if (!Info)
4205 Info = new DeclIDInFileInfo();
4206
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004207 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004208 LocDeclIDsTy &Decls = Info->DeclIDs;
4209
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004210 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004211 Decls.push_back(LocDecl);
4212 return;
4213 }
4214
4215 LocDeclIDsTy::iterator
4216 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4217
4218 Decls.insert(I, LocDecl);
4219}
4220
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004221void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004222 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004223 Record.push_back(Name.getNameKind());
4224 switch (Name.getNameKind()) {
4225 case DeclarationName::Identifier:
4226 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4227 break;
4228
4229 case DeclarationName::ObjCZeroArgSelector:
4230 case DeclarationName::ObjCOneArgSelector:
4231 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004232 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004233 break;
4234
4235 case DeclarationName::CXXConstructorName:
4236 case DeclarationName::CXXDestructorName:
4237 case DeclarationName::CXXConversionFunctionName:
4238 AddTypeRef(Name.getCXXNameType(), Record);
4239 break;
4240
4241 case DeclarationName::CXXOperatorName:
4242 Record.push_back(Name.getCXXOverloadedOperator());
4243 break;
4244
Sean Hunt3e518bd2009-11-29 07:34:05 +00004245 case DeclarationName::CXXLiteralOperatorName:
4246 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4247 break;
4248
Douglas Gregor2cf26342009-04-09 22:27:44 +00004249 case DeclarationName::CXXUsingDirective:
4250 // No extra data to emit
4251 break;
4252 }
4253}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004254
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004255void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004256 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004257 switch (Name.getNameKind()) {
4258 case DeclarationName::CXXConstructorName:
4259 case DeclarationName::CXXDestructorName:
4260 case DeclarationName::CXXConversionFunctionName:
4261 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4262 break;
4263
4264 case DeclarationName::CXXOperatorName:
4265 AddSourceLocation(
4266 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4267 Record);
4268 AddSourceLocation(
4269 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4270 Record);
4271 break;
4272
4273 case DeclarationName::CXXLiteralOperatorName:
4274 AddSourceLocation(
4275 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4276 Record);
4277 break;
4278
4279 case DeclarationName::Identifier:
4280 case DeclarationName::ObjCZeroArgSelector:
4281 case DeclarationName::ObjCOneArgSelector:
4282 case DeclarationName::ObjCMultiArgSelector:
4283 case DeclarationName::CXXUsingDirective:
4284 break;
4285 }
4286}
4287
4288void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004289 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004290 AddDeclarationName(NameInfo.getName(), Record);
4291 AddSourceLocation(NameInfo.getLoc(), Record);
4292 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4293}
4294
4295void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004296 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004297 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004298 Record.push_back(Info.NumTemplParamLists);
4299 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4300 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4301}
4302
Sebastian Redla4232eb2010-08-18 23:56:21 +00004303void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004304 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004305 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004306 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004307 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004308
4309 // Push each of the NNS's onto a stack for serialization in reverse order.
4310 while (NNS) {
4311 NestedNames.push_back(NNS);
4312 NNS = NNS->getPrefix();
4313 }
4314
4315 Record.push_back(NestedNames.size());
4316 while(!NestedNames.empty()) {
4317 NNS = NestedNames.pop_back_val();
4318 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4319 Record.push_back(Kind);
4320 switch (Kind) {
4321 case NestedNameSpecifier::Identifier:
4322 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4323 break;
4324
4325 case NestedNameSpecifier::Namespace:
4326 AddDeclRef(NNS->getAsNamespace(), Record);
4327 break;
4328
Douglas Gregor14aba762011-02-24 02:36:08 +00004329 case NestedNameSpecifier::NamespaceAlias:
4330 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4331 break;
4332
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004333 case NestedNameSpecifier::TypeSpec:
4334 case NestedNameSpecifier::TypeSpecWithTemplate:
4335 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4336 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4337 break;
4338
4339 case NestedNameSpecifier::Global:
4340 // Don't need to write an associated value.
4341 break;
4342 }
4343 }
4344}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004345
Douglas Gregordc355712011-02-25 00:36:19 +00004346void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4347 RecordDataImpl &Record) {
4348 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004349 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004350 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004351
4352 // Push each of the nested-name-specifiers's onto a stack for
4353 // serialization in reverse order.
4354 while (NNS) {
4355 NestedNames.push_back(NNS);
4356 NNS = NNS.getPrefix();
4357 }
4358
4359 Record.push_back(NestedNames.size());
4360 while(!NestedNames.empty()) {
4361 NNS = NestedNames.pop_back_val();
4362 NestedNameSpecifier::SpecifierKind Kind
4363 = NNS.getNestedNameSpecifier()->getKind();
4364 Record.push_back(Kind);
4365 switch (Kind) {
4366 case NestedNameSpecifier::Identifier:
4367 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4368 AddSourceRange(NNS.getLocalSourceRange(), Record);
4369 break;
4370
4371 case NestedNameSpecifier::Namespace:
4372 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4373 AddSourceRange(NNS.getLocalSourceRange(), Record);
4374 break;
4375
4376 case NestedNameSpecifier::NamespaceAlias:
4377 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4378 AddSourceRange(NNS.getLocalSourceRange(), Record);
4379 break;
4380
4381 case NestedNameSpecifier::TypeSpec:
4382 case NestedNameSpecifier::TypeSpecWithTemplate:
4383 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4384 AddTypeLoc(NNS.getTypeLoc(), Record);
4385 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4386 break;
4387
4388 case NestedNameSpecifier::Global:
4389 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4390 break;
4391 }
4392 }
4393}
4394
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004395void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004396 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004397 Record.push_back(Kind);
4398 switch (Kind) {
4399 case TemplateName::Template:
4400 AddDeclRef(Name.getAsTemplateDecl(), Record);
4401 break;
4402
4403 case TemplateName::OverloadedTemplate: {
4404 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4405 Record.push_back(OvT->size());
4406 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4407 I != E; ++I)
4408 AddDeclRef(*I, Record);
4409 break;
4410 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004411
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004412 case TemplateName::QualifiedTemplate: {
4413 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4414 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4415 Record.push_back(QualT->hasTemplateKeyword());
4416 AddDeclRef(QualT->getTemplateDecl(), Record);
4417 break;
4418 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004419
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004420 case TemplateName::DependentTemplate: {
4421 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4422 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4423 Record.push_back(DepT->isIdentifier());
4424 if (DepT->isIdentifier())
4425 AddIdentifierRef(DepT->getIdentifier(), Record);
4426 else
4427 Record.push_back(DepT->getOperator());
4428 break;
4429 }
John McCall14606042011-06-30 08:33:18 +00004430
4431 case TemplateName::SubstTemplateTemplateParm: {
4432 SubstTemplateTemplateParmStorage *subst
4433 = Name.getAsSubstTemplateTemplateParm();
4434 AddDeclRef(subst->getParameter(), Record);
4435 AddTemplateName(subst->getReplacement(), Record);
4436 break;
4437 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004438
4439 case TemplateName::SubstTemplateTemplateParmPack: {
4440 SubstTemplateTemplateParmPackStorage *SubstPack
4441 = Name.getAsSubstTemplateTemplateParmPack();
4442 AddDeclRef(SubstPack->getParameterPack(), Record);
4443 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4444 break;
4445 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004446 }
4447}
4448
Michael J. Spencer20249a12010-10-21 03:16:25 +00004449void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004450 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004451 Record.push_back(Arg.getKind());
4452 switch (Arg.getKind()) {
4453 case TemplateArgument::Null:
4454 break;
4455 case TemplateArgument::Type:
4456 AddTypeRef(Arg.getAsType(), Record);
4457 break;
4458 case TemplateArgument::Declaration:
4459 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004460 Record.push_back(Arg.isDeclForReferenceParam());
4461 break;
4462 case TemplateArgument::NullPtr:
4463 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004464 break;
4465 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004466 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004467 AddTypeRef(Arg.getIntegralType(), Record);
4468 break;
4469 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004470 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4471 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004472 case TemplateArgument::TemplateExpansion:
4473 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
David Blaikiedc84cd52013-02-20 22:23:23 +00004474 if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
Douglas Gregor2be29f42011-01-14 23:41:42 +00004475 Record.push_back(*NumExpansions + 1);
4476 else
4477 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004478 break;
4479 case TemplateArgument::Expression:
4480 AddStmt(Arg.getAsExpr());
4481 break;
4482 case TemplateArgument::Pack:
4483 Record.push_back(Arg.pack_size());
4484 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4485 I != E; ++I)
4486 AddTemplateArgument(*I, Record);
4487 break;
4488 }
4489}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004490
4491void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004492ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004493 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004494 assert(TemplateParams && "No TemplateParams!");
4495 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4496 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4497 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4498 Record.push_back(TemplateParams->size());
4499 for (TemplateParameterList::const_iterator
4500 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4501 P != PEnd; ++P)
4502 AddDeclRef(*P, Record);
4503}
4504
4505/// \brief Emit a template argument list.
4506void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004507ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004508 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004509 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004510 Record.push_back(TemplateArgs->size());
4511 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004512 AddTemplateArgument(TemplateArgs->get(i), Record);
4513}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004514
4515
4516void
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004517ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004518 Record.push_back(Set.size());
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00004519 for (ASTUnresolvedSet::const_iterator
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004520 I = Set.begin(), E = Set.end(); I != E; ++I) {
4521 AddDeclRef(I.getDecl(), Record);
4522 Record.push_back(I.getAccess());
4523 }
4524}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004525
Sebastian Redla4232eb2010-08-18 23:56:21 +00004526void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004527 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004528 Record.push_back(Base.isVirtual());
4529 Record.push_back(Base.isBaseOfClass());
4530 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004531 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004532 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004533 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004534 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4535 : SourceLocation(),
4536 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004537}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004538
Douglas Gregor7c789c12010-10-29 22:39:52 +00004539void ASTWriter::FlushCXXBaseSpecifiers() {
4540 RecordData Record;
4541 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4542 Record.clear();
4543
4544 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004545 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004546 if (Index == CXXBaseSpecifiersOffsets.size())
4547 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4548 else {
4549 if (Index > CXXBaseSpecifiersOffsets.size())
4550 CXXBaseSpecifiersOffsets.resize(Index + 1);
4551 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4552 }
4553
4554 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4555 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4556 Record.push_back(BEnd - B);
4557 for (; B != BEnd; ++B)
4558 AddCXXBaseSpecifier(*B, Record);
4559 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004560
4561 // Flush any expressions that were written as part of the base specifiers.
4562 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004563 }
4564
4565 CXXBaseSpecifiersToWrite.clear();
4566}
4567
Sean Huntcbb67482011-01-08 20:30:50 +00004568void ASTWriter::AddCXXCtorInitializers(
4569 const CXXCtorInitializer * const *CtorInitializers,
4570 unsigned NumCtorInitializers,
4571 RecordDataImpl &Record) {
4572 Record.push_back(NumCtorInitializers);
4573 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4574 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004575
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004576 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004577 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004578 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004579 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004580 } else if (Init->isDelegatingInitializer()) {
4581 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004582 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004583 } else if (Init->isMemberInitializer()){
4584 Record.push_back(CTOR_INITIALIZER_MEMBER);
4585 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004586 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004587 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4588 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004589 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004590
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004591 AddSourceLocation(Init->getMemberLocation(), Record);
4592 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004593 AddSourceLocation(Init->getLParenLoc(), Record);
4594 AddSourceLocation(Init->getRParenLoc(), Record);
4595 Record.push_back(Init->isWritten());
4596 if (Init->isWritten()) {
4597 Record.push_back(Init->getSourceOrder());
4598 } else {
4599 Record.push_back(Init->getNumArrayIndices());
4600 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4601 AddDeclRef(Init->getArrayIndex(i), Record);
4602 }
4603 }
4604}
4605
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004606void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4607 assert(D->DefinitionData);
4608 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004609 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004610 Record.push_back(Data.UserDeclaredConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004611 Record.push_back(Data.UserDeclaredSpecialMembers);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004612 Record.push_back(Data.Aggregate);
4613 Record.push_back(Data.PlainOldData);
4614 Record.push_back(Data.Empty);
4615 Record.push_back(Data.Polymorphic);
4616 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004617 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004618 Record.push_back(Data.HasNoNonEmptyBases);
4619 Record.push_back(Data.HasPrivateFields);
4620 Record.push_back(Data.HasProtectedFields);
4621 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004622 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004623 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004624 Record.push_back(Data.HasInClassInitializer);
Richard Smithd5bc8672012-12-08 02:01:17 +00004625 Record.push_back(Data.HasUninitializedReferenceMember);
Richard Smithbc2a35d2012-12-08 08:32:28 +00004626 Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
4627 Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
4628 Record.push_back(Data.NeedOverloadResolutionForDestructor);
4629 Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
4630 Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
4631 Record.push_back(Data.DefaultedDestructorIsDeleted);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004632 Record.push_back(Data.HasTrivialSpecialMembers);
4633 Record.push_back(Data.HasIrrelevantDestructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004634 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004635 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004636 Record.push_back(Data.HasConstexprDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004637 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004638 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004639 Record.push_back(Data.UserProvidedDefaultConstructor);
Richard Smith7d04d3a2012-11-30 05:11:39 +00004640 Record.push_back(Data.DeclaredSpecialMembers);
Richard Smithacf796b2012-11-28 06:23:12 +00004641 Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
4642 Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
4643 Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
4644 Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
Sebastian Redl14c36332011-08-31 13:59:56 +00004645 Record.push_back(Data.FailedImplicitMoveConstructor);
4646 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004647 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004648
4649 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004650 if (Data.NumBases > 0)
4651 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4652 Record);
4653
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004654 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4655 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004656 if (Data.NumVBases > 0)
4657 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4658 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004659
4660 AddUnresolvedSet(Data.Conversions, Record);
4661 AddUnresolvedSet(Data.VisibleConversions, Record);
4662 // Data.Definition is the owning decl, no need to write it.
4663 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004664
4665 // Add lambda-specific data.
4666 if (Data.IsLambda) {
4667 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004668 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004669 Record.push_back(Lambda.NumCaptures);
4670 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004671 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004672 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004673 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004674 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4675 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4676 AddSourceLocation(Capture.getLocation(), Record);
4677 Record.push_back(Capture.isImplicit());
4678 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4679 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4680 AddDeclRef(Var, Record);
4681 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4682 : SourceLocation(),
4683 Record);
4684 }
4685 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004686}
4687
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004688void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004689 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004690 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004691 assert(FirstDeclID == NextDeclID &&
4692 FirstTypeID == NextTypeID &&
4693 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004694 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004695 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004696 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004697 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004698
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004699 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004700
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004701 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4702 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4703 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004704 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004705 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004706 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004707 NextDeclID = FirstDeclID;
4708 NextTypeID = FirstTypeID;
4709 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004710 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004711 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004712 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004713}
4714
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004715void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004716 // Always keep the highest ID. See \p TypeRead() for more information.
4717 IdentID &StoredID = IdentifierIDs[II];
4718 if (ID > StoredID)
4719 StoredID = ID;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004720}
4721
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004722void ASTWriter::MacroRead(serialization::MacroID ID, MacroDirective *MD) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004723 // Always keep the highest ID. See \p TypeRead() for more information.
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004724 MacroID &StoredID = MacroIDs[MD];
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004725 if (ID > StoredID)
4726 StoredID = ID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004727}
4728
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004729void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004730 // Always take the highest-numbered type index. This copes with an interesting
4731 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004732 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004733 // keep the higher-numbered entry so that we can properly write it out to
4734 // the AST file.
4735 TypeIdx &StoredIdx = TypeIdxs[T];
4736 if (Idx.getIndex() >= StoredIdx.getIndex())
4737 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004738}
4739
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004740void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Douglas Gregor2d1ece82013-02-08 21:30:59 +00004741 // Always keep the highest ID. See \p TypeRead() for more information.
4742 SelectorID &StoredID = SelectorIDs[S];
4743 if (ID > StoredID)
4744 StoredID = ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00004745}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004746
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004747void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004748 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004749 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004750 MacroDefinitions[MD] = ID;
4751}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004752
Douglas Gregora015cab2011-12-02 17:30:13 +00004753void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4754 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4755 SubmoduleIDs[Mod] = ID;
4756}
4757
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +00004758void ASTWriter::UndefinedMacro(MacroDirective *MD) {
4759 MacroUpdates[MD].UndefLoc = MD->getUndefLoc();
Douglas Gregora8235d62012-10-09 23:05:51 +00004760}
4761
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004762void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004763 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004764 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004765 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4766 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004767 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004768 // A forward reference was mutated into a definition. Rewrite it.
4769 // FIXME: This happens during template instantiation, should we
4770 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004771 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004772 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004773 }
4774}
Douglas Gregora8235d62012-10-09 23:05:51 +00004775
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004776void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004777 assert(!WritingAST && "Already writing the AST!");
4778
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004779 // TU and namespaces are handled elsewhere.
4780 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4781 return;
4782
Douglas Gregor919814d2011-09-09 23:01:35 +00004783 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004784 return; // Not a source decl added to a DeclContext from PCH.
4785
Douglas Gregor5a04f9f2013-01-21 15:25:38 +00004786 assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004787 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004788 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004789}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004790
4791void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004792 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004793 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004794 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004795 return; // Not a source member added to a class from PCH.
4796 if (!isa<CXXMethodDecl>(D))
4797 return; // We are interested in lazily declared implicit methods.
4798
4799 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004800 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004801 UpdateRecord &Record = DeclUpdates[RD];
4802 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004803 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004804}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004805
4806void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4807 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004808 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004809 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004810 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004811 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004812 return; // Not a source specialization added to a template from PCH.
4813
4814 UpdateRecord &Record = DeclUpdates[TD];
4815 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004816 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004817}
Douglas Gregor89d99802010-11-30 06:16:57 +00004818
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004819void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4820 const FunctionDecl *D) {
4821 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004822 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004823 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004824 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004825 return; // Not a source specialization added to a template from PCH.
4826
4827 UpdateRecord &Record = DeclUpdates[TD];
4828 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004829 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004830}
4831
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004832void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004833 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004834 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004835 return; // Declaration not imported from PCH.
4836
4837 // Implicit decl from a PCH was defined.
4838 // FIXME: Should implicit definition be a separate FunctionDecl?
4839 RewriteDecl(D);
4840}
4841
Sebastian Redlf79a7192011-04-29 08:19:30 +00004842void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004843 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004844 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004845 return;
4846
4847 // Since the actual instantiation is delayed, this really means that we need
4848 // to update the instantiation location.
4849 UpdateRecord &Record = DeclUpdates[D];
4850 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4851 AddSourceLocation(
4852 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4853}
4854
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004855void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4856 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004857 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004858 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004859 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004860
4861 assert(IFD->getDefinition() && "Category on a class without a definition?");
4862 ObjCClassesWithCategories.insert(
4863 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004864}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004865
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004866
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004867void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4868 const ObjCPropertyDecl *OrigProp,
4869 const ObjCCategoryDecl *ClassExt) {
4870 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4871 if (!D)
4872 return;
4873
4874 assert(!WritingAST && "Already writing the AST!");
4875 if (!D->isFromASTFile())
4876 return; // Declaration not imported from PCH.
4877
4878 RewriteDecl(D);
4879}