blob: a2e8b71123b10d1c33ff1eba0a28a497d7ab5414 [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 Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Douglas Gregorbbf38312012-10-24 16:50:34 +000028#include "clang/Lex/HeaderSearchOptions.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Douglas Gregora71a7d82012-10-24 20:05:57 +000032#include "clang/Lex/PreprocessorOptions.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000033#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000034#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000035#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000036#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000037#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000038#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000039#include "clang/Basic/TargetInfo.h"
Douglas Gregor57016dd2012-10-16 23:40:58 +000040#include "clang/Basic/TargetOptions.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000041#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000042#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000043#include "llvm/ADT/APFloat.h"
44#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000045#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000046#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000047#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000048#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000049#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000051#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000052#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000053#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000054using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000055using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000056
Sebastian Redlade50002010-07-30 17:03:48 +000057template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000058static StringRef data(const std::vector<T, Allocator> &v) {
59 if (v.empty()) return StringRef();
60 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000062}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000063
64template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000065static StringRef data(const SmallVectorImpl<T> &v) {
66 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000067 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000068}
69
Douglas Gregor2cf26342009-04-09 22:27:44 +000070//===----------------------------------------------------------------------===//
71// Type serialization
72//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000073
Douglas Gregor2cf26342009-04-09 22:27:44 +000074namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000075 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000076 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000077 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000078
79 public:
80 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000083 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000084 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000085
86 void VisitArrayType(const ArrayType *T);
87 void VisitFunctionType(const FunctionType *T);
88 void VisitTagType(const TagType *T);
89
90#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
91#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000092#include "clang/AST/TypeNodes.def"
93 };
94}
95
Sebastian Redl3397c552010-08-18 23:56:27 +000096void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000097 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000098}
99
Sebastian Redl3397c552010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000102 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103}
104
Sebastian Redl3397c552010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000107 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
Sebastian Redl3397c552010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000112 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113}
114
Sebastian Redl3397c552010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000116 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
117 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000118 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000119}
120
Sebastian Redl3397c552010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000122 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000123 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124}
125
Sebastian Redl3397c552010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000127 Writer.AddTypeRef(T->getPointeeType(), Record);
128 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000129 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130}
131
Sebastian Redl3397c552010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 Writer.AddTypeRef(T->getElementType(), Record);
134 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000135 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136}
137
Sebastian Redl3397c552010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139 VisitArrayType(T);
140 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000141 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142}
143
Sebastian Redl3397c552010-08-18 23:56:27 +0000144void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000146 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147}
148
Sebastian Redl3397c552010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000151 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
152 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000153 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000154 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155}
156
Sebastian Redl3397c552010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getElementType(), Record);
159 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000160 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000161 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162}
163
Sebastian Redl3397c552010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000166 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167}
168
Sebastian Redl3397c552010-08-18 23:56:27 +0000169void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000171 FunctionType::ExtInfo C = T->getExtInfo();
172 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000173 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000174 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000175 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000176 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000177 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178}
179
Sebastian Redl3397c552010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000182 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183}
184
Sebastian Redl3397c552010-08-18 23:56:27 +0000185void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000186 VisitFunctionType(T);
187 Record.push_back(T->getNumArgs());
188 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
189 Writer.AddTypeRef(T->getArgType(I), Record);
190 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000191 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000193 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000194 Record.push_back(T->getExceptionSpecType());
195 if (T->getExceptionSpecType() == EST_Dynamic) {
196 Record.push_back(T->getNumExceptions());
197 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
198 Writer.AddTypeRef(T->getExceptionType(I), Record);
199 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
200 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000201 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
203 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000204 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
205 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000206 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000207 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208}
209
Sebastian Redl3397c552010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000211 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000212 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000213}
John McCalled976492009-12-04 22:46:56 +0000214
Sebastian Redl3397c552010-08-18 23:56:27 +0000215void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000217 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
218 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000219 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220}
221
Sebastian Redl3397c552010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000223 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000224 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225}
226
Sebastian Redl3397c552010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000229 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000230}
231
Sebastian Redl3397c552010-08-18 23:56:27 +0000232void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000233 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000234 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000236}
237
Sean Huntca63c202011-05-24 22:41:36 +0000238void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
239 Writer.AddTypeRef(T->getBaseType(), Record);
240 Writer.AddTypeRef(T->getUnderlyingType(), Record);
241 Record.push_back(T->getUTTKind());
242 Code = TYPE_UNARY_TRANSFORM;
243}
244
Richard Smith34b41d92011-02-20 03:19:35 +0000245void ASTTypeWriter::VisitAutoType(const AutoType *T) {
246 Writer.AddTypeRef(T->getDeducedType(), Record);
247 Code = TYPE_AUTO;
248}
249
Sebastian Redl3397c552010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000251 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000252 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000253 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254 "Cannot serialize in the middle of a type definition");
255}
256
Sebastian Redl3397c552010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000259 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260}
261
Sebastian Redl3397c552010-08-18 23:56:27 +0000262void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000264 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000265}
266
John McCall9d156a72011-01-06 01:58:22 +0000267void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
268 Writer.AddTypeRef(T->getModifiedType(), Record);
269 Writer.AddTypeRef(T->getEquivalentType(), Record);
270 Record.push_back(T->getAttrKind());
271 Code = TYPE_ATTRIBUTED;
272}
273
Mike Stump1eb44332009-09-09 15:08:12 +0000274void
Sebastian Redl3397c552010-08-18 23:56:27 +0000275ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000276 const SubstTemplateTypeParmType *T) {
277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
278 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000280}
281
282void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000283ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
284 const SubstTemplateTypeParmPackType *T) {
285 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
286 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
287 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
288}
289
290void
Sebastian Redl3397c552010-08-18 23:56:27 +0000291ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000292 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000293 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000294 Writer.AddTemplateName(T->getTemplateName(), Record);
295 Record.push_back(T->getNumArgs());
296 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
297 ArgI != ArgE; ++ArgI)
298 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000299 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
300 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000303 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl3397c552010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000308 VisitArrayType(T);
309 Writer.AddStmt(T->getSizeExpr());
310 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000311 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000312}
313
314void
Sebastian Redl3397c552010-08-18 23:56:27 +0000315ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316 const DependentSizedExtVectorType *T) {
317 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000318 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000323 Record.push_back(T->getDepth());
324 Record.push_back(T->getIndex());
325 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000327 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000328}
329
330void
Sebastian Redl3397c552010-08-18 23:56:27 +0000331ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000332 Record.push_back(T->getKeyword());
333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000335 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
336 : T->getCanonicalTypeInternal(),
337 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000338 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000339}
340
341void
Sebastian Redl3397c552010-08-18 23:56:27 +0000342ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000343 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000344 Record.push_back(T->getKeyword());
345 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
346 Writer.AddIdentifierRef(T->getIdentifier(), Record);
347 Record.push_back(T->getNumArgs());
348 for (DependentTemplateSpecializationType::iterator
349 I = T->begin(), E = T->end(); I != E; ++I)
350 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000351 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000352}
353
Douglas Gregor7536dd52010-12-20 02:24:11 +0000354void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
355 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000356 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
357 Record.push_back(*NumExpansions + 1);
358 else
359 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000360 Code = TYPE_PACK_EXPANSION;
361}
362
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000363void ASTTypeWriter::VisitParenType(const ParenType *T) {
364 Writer.AddTypeRef(T->getInnerType(), Record);
365 Code = TYPE_PAREN;
366}
367
Sebastian Redl3397c552010-08-18 23:56:27 +0000368void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000369 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000370 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
371 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000372 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000373}
374
Sebastian Redl3397c552010-08-18 23:56:27 +0000375void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000376 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000377 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000378 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000379}
380
Sebastian Redl3397c552010-08-18 23:56:27 +0000381void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000382 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000384}
385
Sebastian Redl3397c552010-08-18 23:56:27 +0000386void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000387 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000388 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000389 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000390 E = T->qual_end(); I != E; ++I)
391 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000392 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000393}
394
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000395void
Sebastian Redl3397c552010-08-18 23:56:27 +0000396ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000397 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000398 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000399}
400
Eli Friedmanb001de72011-10-06 23:00:33 +0000401void
402ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
403 Writer.AddTypeRef(T->getValueType(), Record);
404 Code = TYPE_ATOMIC;
405}
406
John McCalla1ee0c52009-10-16 21:56:05 +0000407namespace {
408
409class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000410 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000411 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000412
413public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000414 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000415 : Writer(Writer), Record(Record) { }
416
John McCall51bd8032009-10-18 01:05:36 +0000417#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000418#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000419 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000420#include "clang/AST/TypeLocNodes.def"
421
John McCall51bd8032009-10-18 01:05:36 +0000422 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
423 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000424};
425
426}
427
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
429 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000432 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
433 if (TL.needsExtraLocalData()) {
434 Record.push_back(TL.getWrittenTypeSpec());
435 Record.push_back(TL.getWrittenSignSpec());
436 Record.push_back(TL.getWrittenWidthSpec());
437 Record.push_back(TL.hasModeAttr());
438 }
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000454}
John McCall51bd8032009-10-18 01:05:36 +0000455void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000457 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000458}
John McCall51bd8032009-10-18 01:05:36 +0000459void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
461 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
462 Record.push_back(TL.getSizeExpr() ? 1 : 0);
463 if (TL.getSizeExpr())
464 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000465}
John McCall51bd8032009-10-18 01:05:36 +0000466void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
473 VisitArrayTypeLoc(TL);
474}
475void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
476 DependentSizedArrayTypeLoc TL) {
477 VisitArrayTypeLoc(TL);
478}
479void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
480 DependentSizedExtVectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
489void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000491 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
492 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000493 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000494 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
495 Writer.AddDeclRef(TL.getArg(i), Record);
496}
497void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
500void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
501 VisitFunctionTypeLoc(TL);
502}
John McCalled976492009-12-04 22:46:56 +0000503void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
John McCall51bd8032009-10-18 01:05:36 +0000506void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getNameLoc(), Record);
508}
509void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000510 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000513}
514void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000515 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000519}
520void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
Sean Huntca63c202011-05-24 22:41:36 +0000523void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getKWLoc(), Record);
525 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
526 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
527 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
528}
Richard Smith34b41d92011-02-20 03:19:35 +0000529void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall51bd8032009-10-18 01:05:36 +0000532void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
535void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getNameLoc(), Record);
537}
John McCall9d156a72011-01-06 01:58:22 +0000538void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
540 if (TL.hasAttrOperand()) {
541 SourceRange range = TL.getAttrOperandParensRange();
542 Writer.AddSourceLocation(range.getBegin(), Record);
543 Writer.AddSourceLocation(range.getEnd(), Record);
544 }
545 if (TL.hasAttrExprOperand()) {
546 Expr *operand = TL.getAttrExprOperand();
547 Record.push_back(operand ? 1 : 0);
548 if (operand) Writer.AddStmt(operand);
549 } else if (TL.hasAttrEnumOperand()) {
550 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
551 }
552}
John McCall51bd8032009-10-18 01:05:36 +0000553void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
John McCall49a832b2009-10-18 09:09:24 +0000556void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
557 SubstTemplateTypeParmTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getNameLoc(), Record);
559}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000560void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
561 SubstTemplateTypeParmPackTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getNameLoc(), Record);
563}
John McCall51bd8032009-10-18 01:05:36 +0000564void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
565 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000566 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000567 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
568 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
569 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
570 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000571 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
572 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000573}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000574void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
576 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
577}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000578void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000579 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000580 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000581}
John McCall3cb0ebd2010-03-10 03:28:59 +0000582void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
Douglas Gregor4714c122010-03-31 17:34:00 +0000585void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000586 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000587 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
589}
John McCall33500952010-06-11 00:33:02 +0000590void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
591 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000593 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000594 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000595 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000596 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
597 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
598 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000599 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
600 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000601}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000602void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
604}
John McCall51bd8032009-10-18 01:05:36 +0000605void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
606 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000607}
608void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
609 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000610 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
611 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
612 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
613 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000614}
John McCall54e14c42009-10-22 22:37:11 +0000615void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000617}
Eli Friedmanb001de72011-10-06 23:00:33 +0000618void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
619 Writer.AddSourceLocation(TL.getKWLoc(), Record);
620 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
621 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
622}
John McCalla1ee0c52009-10-16 21:56:05 +0000623
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000624//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000625// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000626//===----------------------------------------------------------------------===//
627
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000628static void EmitBlockID(unsigned ID, const char *Name,
629 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000630 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000631 Record.clear();
632 Record.push_back(ID);
633 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
634
635 // Emit the block name if present.
636 if (Name == 0 || Name[0] == 0) return;
637 Record.clear();
638 while (*Name)
639 Record.push_back(*Name++);
640 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
641}
642
643static void EmitRecordID(unsigned ID, const char *Name,
644 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000645 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000646 Record.clear();
647 Record.push_back(ID);
648 while (*Name)
649 Record.push_back(*Name++);
650 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000651}
652
653static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000654 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000655#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000656 RECORD(STMT_STOP);
657 RECORD(STMT_NULL_PTR);
658 RECORD(STMT_NULL);
659 RECORD(STMT_COMPOUND);
660 RECORD(STMT_CASE);
661 RECORD(STMT_DEFAULT);
662 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000663 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000664 RECORD(STMT_IF);
665 RECORD(STMT_SWITCH);
666 RECORD(STMT_WHILE);
667 RECORD(STMT_DO);
668 RECORD(STMT_FOR);
669 RECORD(STMT_GOTO);
670 RECORD(STMT_INDIRECT_GOTO);
671 RECORD(STMT_CONTINUE);
672 RECORD(STMT_BREAK);
673 RECORD(STMT_RETURN);
674 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000675 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000676 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000677 RECORD(EXPR_PREDEFINED);
678 RECORD(EXPR_DECL_REF);
679 RECORD(EXPR_INTEGER_LITERAL);
680 RECORD(EXPR_FLOATING_LITERAL);
681 RECORD(EXPR_IMAGINARY_LITERAL);
682 RECORD(EXPR_STRING_LITERAL);
683 RECORD(EXPR_CHARACTER_LITERAL);
684 RECORD(EXPR_PAREN);
685 RECORD(EXPR_UNARY_OPERATOR);
686 RECORD(EXPR_SIZEOF_ALIGN_OF);
687 RECORD(EXPR_ARRAY_SUBSCRIPT);
688 RECORD(EXPR_CALL);
689 RECORD(EXPR_MEMBER);
690 RECORD(EXPR_BINARY_OPERATOR);
691 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
692 RECORD(EXPR_CONDITIONAL_OPERATOR);
693 RECORD(EXPR_IMPLICIT_CAST);
694 RECORD(EXPR_CSTYLE_CAST);
695 RECORD(EXPR_COMPOUND_LITERAL);
696 RECORD(EXPR_EXT_VECTOR_ELEMENT);
697 RECORD(EXPR_INIT_LIST);
698 RECORD(EXPR_DESIGNATED_INIT);
699 RECORD(EXPR_IMPLICIT_VALUE_INIT);
700 RECORD(EXPR_VA_ARG);
701 RECORD(EXPR_ADDR_LABEL);
702 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000703 RECORD(EXPR_CHOOSE);
704 RECORD(EXPR_GNU_NULL);
705 RECORD(EXPR_SHUFFLE_VECTOR);
706 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000707 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000708 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000709 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000710 RECORD(EXPR_OBJC_ARRAY_LITERAL);
711 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000712 RECORD(EXPR_OBJC_ENCODE);
713 RECORD(EXPR_OBJC_SELECTOR_EXPR);
714 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
715 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
716 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
717 RECORD(EXPR_OBJC_KVC_REF_EXPR);
718 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000719 RECORD(STMT_OBJC_FOR_COLLECTION);
720 RECORD(STMT_OBJC_CATCH);
721 RECORD(STMT_OBJC_FINALLY);
722 RECORD(STMT_OBJC_AT_TRY);
723 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
724 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000725 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000726 RECORD(EXPR_CXX_OPERATOR_CALL);
727 RECORD(EXPR_CXX_CONSTRUCT);
728 RECORD(EXPR_CXX_STATIC_CAST);
729 RECORD(EXPR_CXX_DYNAMIC_CAST);
730 RECORD(EXPR_CXX_REINTERPRET_CAST);
731 RECORD(EXPR_CXX_CONST_CAST);
732 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000733 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000734 RECORD(EXPR_CXX_BOOL_LITERAL);
735 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000736 RECORD(EXPR_CXX_TYPEID_EXPR);
737 RECORD(EXPR_CXX_TYPEID_TYPE);
738 RECORD(EXPR_CXX_UUIDOF_EXPR);
739 RECORD(EXPR_CXX_UUIDOF_TYPE);
740 RECORD(EXPR_CXX_THIS);
741 RECORD(EXPR_CXX_THROW);
742 RECORD(EXPR_CXX_DEFAULT_ARG);
743 RECORD(EXPR_CXX_BIND_TEMPORARY);
744 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
745 RECORD(EXPR_CXX_NEW);
746 RECORD(EXPR_CXX_DELETE);
747 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
748 RECORD(EXPR_EXPR_WITH_CLEANUPS);
749 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
750 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
751 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
752 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
753 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
754 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
755 RECORD(EXPR_CXX_NOEXCEPT);
756 RECORD(EXPR_OPAQUE_VALUE);
757 RECORD(EXPR_BINARY_TYPE_TRAIT);
758 RECORD(EXPR_PACK_EXPANSION);
759 RECORD(EXPR_SIZEOF_PACK);
760 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000761 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000762#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000763}
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Sebastian Redla4232eb2010-08-18 23:56:21 +0000765void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000766 RecordData Record;
767 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000769#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
770#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000772 // Control Block.
773 BLOCK(CONTROL_BLOCK);
774 RECORD(METADATA);
775 RECORD(IMPORTS);
776 RECORD(LANGUAGE_OPTIONS);
777 RECORD(TARGET_OPTIONS);
Douglas Gregor39c497b2012-10-18 18:36:53 +0000778 RECORD(ORIGINAL_FILE);
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000779 RECORD(ORIGINAL_PCH_DIR);
Douglas Gregora930dc92012-10-22 18:42:04 +0000780 RECORD(INPUT_FILE_OFFSETS);
Douglas Gregor5f3d8222012-10-24 15:17:15 +0000781 RECORD(DIAGNOSTIC_OPTIONS);
Douglas Gregor1b2c3c02012-10-24 15:49:58 +0000782 RECORD(FILE_SYSTEM_OPTIONS);
Douglas Gregorbbf38312012-10-24 16:50:34 +0000783 RECORD(HEADER_SEARCH_OPTIONS);
Douglas Gregora71a7d82012-10-24 20:05:57 +0000784 RECORD(PREPROCESSOR_OPTIONS);
785
Douglas Gregorc337fef2012-10-19 00:45:00 +0000786 BLOCK(INPUT_FILES_BLOCK);
787 RECORD(INPUT_FILE);
788
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000789 // AST Top-Level Block.
790 BLOCK(AST_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000791 RECORD(TYPE_OFFSET);
792 RECORD(DECL_OFFSET);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000793 RECORD(IDENTIFIER_OFFSET);
794 RECORD(IDENTIFIER_TABLE);
795 RECORD(EXTERNAL_DEFINITIONS);
796 RECORD(SPECIAL_TYPES);
797 RECORD(STATISTICS);
798 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000799 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000800 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
801 RECORD(SELECTOR_OFFSETS);
802 RECORD(METHOD_POOL);
803 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000804 RECORD(SOURCE_LOCATION_OFFSETS);
805 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000806 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000807 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000808 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000809 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000810 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000811 RECORD(SEMA_DECL_REFS);
812 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
813 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
814 RECORD(DECL_REPLACEMENTS);
815 RECORD(UPDATE_VISIBLE);
816 RECORD(DECL_UPDATE_OFFSETS);
817 RECORD(DECL_UPDATES);
818 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
819 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000820 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000821 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000822 RECORD(FP_PRAGMA_OPTIONS);
823 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000824 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000825 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000826 RECORD(MODULE_OFFSET_MAP);
827 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000828 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000829 RECORD(FILE_SORTED_DECLS);
830 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000831 RECORD(MERGED_DECLARATIONS);
832 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000833 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000834 RECORD(MACRO_OFFSET);
835 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000836
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000837 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000838 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000839 RECORD(SM_SLOC_FILE_ENTRY);
840 RECORD(SM_SLOC_BUFFER_ENTRY);
841 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000842 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000844 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000845 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000846 RECORD(PP_MACRO_OBJECT_LIKE);
847 RECORD(PP_MACRO_FUNCTION_LIKE);
848 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000849
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000850 // Decls and Types block.
851 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000852 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000853 RECORD(TYPE_COMPLEX);
854 RECORD(TYPE_POINTER);
855 RECORD(TYPE_BLOCK_POINTER);
856 RECORD(TYPE_LVALUE_REFERENCE);
857 RECORD(TYPE_RVALUE_REFERENCE);
858 RECORD(TYPE_MEMBER_POINTER);
859 RECORD(TYPE_CONSTANT_ARRAY);
860 RECORD(TYPE_INCOMPLETE_ARRAY);
861 RECORD(TYPE_VARIABLE_ARRAY);
862 RECORD(TYPE_VECTOR);
863 RECORD(TYPE_EXT_VECTOR);
864 RECORD(TYPE_FUNCTION_PROTO);
865 RECORD(TYPE_FUNCTION_NO_PROTO);
866 RECORD(TYPE_TYPEDEF);
867 RECORD(TYPE_TYPEOF_EXPR);
868 RECORD(TYPE_TYPEOF);
869 RECORD(TYPE_RECORD);
870 RECORD(TYPE_ENUM);
871 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000872 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000873 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000874 RECORD(TYPE_DECLTYPE);
875 RECORD(TYPE_ELABORATED);
876 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
877 RECORD(TYPE_UNRESOLVED_USING);
878 RECORD(TYPE_INJECTED_CLASS_NAME);
879 RECORD(TYPE_OBJC_OBJECT);
880 RECORD(TYPE_TEMPLATE_TYPE_PARM);
881 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
882 RECORD(TYPE_DEPENDENT_NAME);
883 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
884 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
885 RECORD(TYPE_PAREN);
886 RECORD(TYPE_PACK_EXPANSION);
887 RECORD(TYPE_ATTRIBUTED);
888 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000889 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000890 RECORD(DECL_TYPEDEF);
891 RECORD(DECL_ENUM);
892 RECORD(DECL_RECORD);
893 RECORD(DECL_ENUM_CONSTANT);
894 RECORD(DECL_FUNCTION);
895 RECORD(DECL_OBJC_METHOD);
896 RECORD(DECL_OBJC_INTERFACE);
897 RECORD(DECL_OBJC_PROTOCOL);
898 RECORD(DECL_OBJC_IVAR);
899 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000900 RECORD(DECL_OBJC_CATEGORY);
901 RECORD(DECL_OBJC_CATEGORY_IMPL);
902 RECORD(DECL_OBJC_IMPLEMENTATION);
903 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
904 RECORD(DECL_OBJC_PROPERTY);
905 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000906 RECORD(DECL_FIELD);
907 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000908 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000909 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000910 RECORD(DECL_FILE_SCOPE_ASM);
911 RECORD(DECL_BLOCK);
912 RECORD(DECL_CONTEXT_LEXICAL);
913 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000914 RECORD(DECL_NAMESPACE);
915 RECORD(DECL_NAMESPACE_ALIAS);
916 RECORD(DECL_USING);
917 RECORD(DECL_USING_SHADOW);
918 RECORD(DECL_USING_DIRECTIVE);
919 RECORD(DECL_UNRESOLVED_USING_VALUE);
920 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
921 RECORD(DECL_LINKAGE_SPEC);
922 RECORD(DECL_CXX_RECORD);
923 RECORD(DECL_CXX_METHOD);
924 RECORD(DECL_CXX_CONSTRUCTOR);
925 RECORD(DECL_CXX_DESTRUCTOR);
926 RECORD(DECL_CXX_CONVERSION);
927 RECORD(DECL_ACCESS_SPEC);
928 RECORD(DECL_FRIEND);
929 RECORD(DECL_FRIEND_TEMPLATE);
930 RECORD(DECL_CLASS_TEMPLATE);
931 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
932 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
933 RECORD(DECL_FUNCTION_TEMPLATE);
934 RECORD(DECL_TEMPLATE_TYPE_PARM);
935 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
936 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
937 RECORD(DECL_STATIC_ASSERT);
938 RECORD(DECL_CXX_BASE_SPECIFIERS);
939 RECORD(DECL_INDIRECTFIELD);
940 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
941
Douglas Gregora72d8c42011-06-03 02:27:19 +0000942 // Statements and Exprs can occur in the Decls and Types block.
943 AddStmtsExprs(Stream, Record);
944
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000945 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000946 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000947 RECORD(PPD_MACRO_DEFINITION);
948 RECORD(PPD_INCLUSION_DIRECTIVE);
949
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000950#undef RECORD
951#undef BLOCK
952 Stream.ExitBlock();
953}
954
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955/// \brief Adjusts the given filename to only write out the portion of the
956/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000957///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958/// \param Filename the file name to adjust.
959///
960/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
961/// the returned filename will be adjusted by this system root.
962///
963/// \returns either the original filename (if it needs no adjustment) or the
964/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000965static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000966adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregor832d6202011-07-22 16:35:34 +0000969 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 // Verify that the filename and the system root have the same prefix.
973 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000974 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 if (Filename[Pos] != isysroot[Pos])
976 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregore650c8c2009-07-07 00:12:59 +0000978 // We hit the end of the filename before we hit the end of the system root.
979 if (!Filename[Pos])
980 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Douglas Gregore650c8c2009-07-07 00:12:59 +0000982 // If the file name has a '/' at the current position, skip over the '/'.
983 // We distinguish sysroot-based includes from absolute includes by the
984 // absence of '/' at the beginning of sysroot-based includes.
985 if (Filename[Pos] == '/')
986 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Douglas Gregore650c8c2009-07-07 00:12:59 +0000988 return Filename + Pos;
989}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000990
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000991/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000992void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
993 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000994 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000995 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000996 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
997 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000998
Douglas Gregore650c8c2009-07-07 00:12:59 +0000999 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001000 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1001 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1009 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1010 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001011 Record.push_back(VERSION_MAJOR);
1012 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001013 Record.push_back(CLANG_VERSION_MAJOR);
1014 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001015 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001016 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001017 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1018 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001019
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001020 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001021 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001022 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1023 llvm::SmallVector<char, 128> ModulePaths;
1024 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001025
1026 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1027 M != MEnd; ++M) {
1028 // Skip modules that weren't directly imported.
1029 if (!(*M)->isDirectlyImported())
1030 continue;
1031
1032 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1033 // FIXME: Write import location, once it matters.
1034 // FIXME: This writes the absolute path for AST files we depend on.
1035 const std::string &FileName = (*M)->FileName;
1036 Record.push_back(FileName.size());
1037 Record.append(FileName.begin(), FileName.end());
1038 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001039 Stream.EmitRecord(IMPORTS, Record);
1040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001042 // Language options.
1043 Record.clear();
1044 const LangOptions &LangOpts = Context.getLangOpts();
1045#define LANGOPT(Name, Bits, Default, Description) \
1046 Record.push_back(LangOpts.Name);
1047#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1048 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1049#include "clang/Basic/LangOptions.def"
1050
1051 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1052 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1053
1054 Record.push_back(LangOpts.CurrentModule.size());
1055 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1056 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1057
Douglas Gregoree097c12012-10-18 17:58:09 +00001058 // Target options.
1059 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001060 const TargetInfo &Target = Context.getTargetInfo();
1061 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001062 AddString(TargetOpts.Triple, Record);
1063 AddString(TargetOpts.CPU, Record);
1064 AddString(TargetOpts.ABI, Record);
1065 AddString(TargetOpts.CXXABI, Record);
1066 AddString(TargetOpts.LinkerVersion, Record);
1067 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1068 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1069 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1070 }
1071 Record.push_back(TargetOpts.Features.size());
1072 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1073 AddString(TargetOpts.Features[I], Record);
1074 }
1075 Stream.EmitRecord(TARGET_OPTIONS, Record);
1076
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001077 // Diagnostic options.
1078 Record.clear();
1079 const DiagnosticOptions &DiagOpts
1080 = Context.getDiagnostics().getDiagnosticOptions();
1081#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1082#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1083 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1084#include "clang/Basic/DiagnosticOptions.def"
1085 Record.push_back(DiagOpts.Warnings.size());
1086 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1087 AddString(DiagOpts.Warnings[I], Record);
1088 // Note: we don't serialize the log or serialization file names, because they
1089 // are generally transient files and will almost always be overridden.
1090 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1091
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001092 // File system options.
1093 Record.clear();
1094 const FileSystemOptions &FSOpts
1095 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1096 AddString(FSOpts.WorkingDir, Record);
1097 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1098
Douglas Gregorbbf38312012-10-24 16:50:34 +00001099 // Header search options.
1100 Record.clear();
1101 const HeaderSearchOptions &HSOpts
1102 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1103 AddString(HSOpts.Sysroot, Record);
1104
1105 // Include entries.
1106 Record.push_back(HSOpts.UserEntries.size());
1107 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1108 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1109 AddString(Entry.Path, Record);
1110 Record.push_back(static_cast<unsigned>(Entry.Group));
1111 Record.push_back(Entry.IsUserSupplied);
1112 Record.push_back(Entry.IsFramework);
1113 Record.push_back(Entry.IgnoreSysRoot);
1114 Record.push_back(Entry.IsInternal);
1115 Record.push_back(Entry.ImplicitExternC);
1116 }
1117
1118 // System header prefixes.
1119 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1120 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1121 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1122 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1123 }
1124
1125 AddString(HSOpts.ResourceDir, Record);
1126 AddString(HSOpts.ModuleCachePath, Record);
1127 Record.push_back(HSOpts.DisableModuleHash);
1128 Record.push_back(HSOpts.UseBuiltinIncludes);
1129 Record.push_back(HSOpts.UseStandardSystemIncludes);
1130 Record.push_back(HSOpts.UseStandardCXXIncludes);
1131 Record.push_back(HSOpts.UseLibcxx);
1132 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1133
Douglas Gregora71a7d82012-10-24 20:05:57 +00001134 // Preprocessor options.
1135 Record.clear();
1136 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1137
1138 // Macro definitions.
1139 Record.push_back(PPOpts.Macros.size());
1140 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1141 AddString(PPOpts.Macros[I].first, Record);
1142 Record.push_back(PPOpts.Macros[I].second);
1143 }
1144
1145 // Includes
1146 Record.push_back(PPOpts.Includes.size());
1147 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1148 AddString(PPOpts.Includes[I], Record);
1149
1150 // Macro includes
1151 Record.push_back(PPOpts.MacroIncludes.size());
1152 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1153 AddString(PPOpts.MacroIncludes[I], Record);
1154
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001155 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001156 AddString(PPOpts.ImplicitPCHInclude, Record);
1157 AddString(PPOpts.ImplicitPTHInclude, Record);
1158 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1159 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1160
Douglas Gregor31d375f2011-05-06 21:43:30 +00001161 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001162 SourceManager &SM = Context.getSourceManager();
1163 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1164 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001165 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1166 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001167 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1168 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1169
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001170 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001172 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001173
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001174 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001175 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001176 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001177 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001178 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001179 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001180 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001181 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001182
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001183 // Original PCH directory
1184 if (!OutputFile.empty() && OutputFile != "-") {
1185 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1186 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1188 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1189
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001190 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001191
1192 llvm::sys::fs::make_absolute(OutputPath);
1193 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1194
1195 RecordData Record;
1196 Record.push_back(ORIGINAL_PCH_DIR);
1197 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1198 }
1199
Douglas Gregor745e6f12012-10-19 00:38:02 +00001200 WriteInputFiles(Context.SourceMgr, isysroot);
1201 Stream.ExitBlock();
1202}
1203
1204void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1205 using namespace llvm;
1206 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1207 RecordData Record;
1208
1209 // Create input-file abbreviation.
1210 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1211 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001212 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001213 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1214 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001215 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001216 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1217 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1218
1219 // Write out all of the input files.
1220 std::vector<uint32_t> InputFileOffsets;
1221 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1222 // Get this source location entry.
1223 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001224 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001225
1226 // We only care about file entries that were not overridden.
1227 if (!SLoc->isFile())
1228 continue;
1229 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001230 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001231 continue;
1232
Douglas Gregora930dc92012-10-22 18:42:04 +00001233 // Record this entry's offset.
1234 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1235 InputFileIDs[Cache->OrigEntry] = InputFileOffsets.size();
1236
Douglas Gregor745e6f12012-10-19 00:38:02 +00001237 Record.clear();
1238 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001239 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001240
1241 // Emit size/modification time for this file.
1242 Record.push_back(Cache->OrigEntry->getSize());
1243 Record.push_back(Cache->OrigEntry->getModificationTime());
1244
Douglas Gregora930dc92012-10-22 18:42:04 +00001245 // Whether this file was overridden.
1246 Record.push_back(Cache->BufferOverridden);
1247
Douglas Gregor745e6f12012-10-19 00:38:02 +00001248 // Turn the file name into an absolute path, if it isn't already.
1249 const char *Filename = Cache->OrigEntry->getName();
1250 SmallString<128> FilePath(Filename);
1251
1252 // Ask the file manager to fixup the relative path for us. This will
1253 // honor the working directory.
1254 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1255
1256 // FIXME: This call to make_absolute shouldn't be necessary, the
1257 // call to FixupRelativePath should always return an absolute path.
1258 llvm::sys::fs::make_absolute(FilePath);
1259 Filename = FilePath.c_str();
1260
1261 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1262
1263 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1264 }
1265
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001266 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001267
1268 // Create input file offsets abbreviation.
1269 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1270 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1271 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1272 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1273 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1274
1275 // Write input file offsets.
1276 Record.clear();
1277 Record.push_back(INPUT_FILE_OFFSETS);
1278 Record.push_back(InputFileOffsets.size());
1279 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001280}
1281
Douglas Gregor14f79002009-04-10 03:52:48 +00001282//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001283// stat cache Serialization
1284//===----------------------------------------------------------------------===//
1285
1286namespace {
1287// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001288class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001289public:
1290 typedef const char * key_type;
1291 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattner74e976b2010-11-23 19:28:12 +00001293 typedef struct stat data_type;
1294 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001295
1296 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001297 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001298 }
Mike Stump1eb44332009-09-09 15:08:12 +00001299
1300 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001301 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001302 data_type_ref Data) {
1303 unsigned StrLen = strlen(path);
1304 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001305 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001306 clang::io::Emit8(Out, DataLen);
1307 return std::make_pair(StrLen + 1, DataLen);
1308 }
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Chris Lattner5f9e2722011-07-23 10:55:15 +00001310 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001311 Out.write(path, KeyLen);
1312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Chris Lattner5f9e2722011-07-23 10:55:15 +00001314 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001315 data_type_ref Data, unsigned DataLen) {
1316 using namespace clang::io;
1317 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattner74e976b2010-11-23 19:28:12 +00001319 Emit32(Out, (uint32_t) Data.st_ino);
1320 Emit32(Out, (uint32_t) Data.st_dev);
1321 Emit16(Out, (uint16_t) Data.st_mode);
1322 Emit64(Out, (uint64_t) Data.st_mtime);
1323 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001324
1325 assert(Out.tell() - Start == DataLen && "Wrong data length");
1326 }
1327};
1328} // end anonymous namespace
1329
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001330//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001331// Source Manager Serialization
1332//===----------------------------------------------------------------------===//
1333
1334/// \brief Create an abbreviation for the SLocEntry that refers to a
1335/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001336static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001337 using namespace llvm;
1338 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001339 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1343 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001344 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001347 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1348 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001349 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001350}
1351
1352/// \brief Create an abbreviation for the SLocEntry that refers to a
1353/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001354static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001355 using namespace llvm;
1356 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001357 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1361 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1362 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001363 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001364}
1365
1366/// \brief Create an abbreviation for the SLocEntry that refers to a
1367/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001368static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001369 using namespace llvm;
1370 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001371 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001372 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001373 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001374}
1375
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001376/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1377/// expansion.
1378static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001379 using namespace llvm;
1380 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001381 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001387 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001388}
1389
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001390namespace {
1391 // Trait used for the on-disk hash table of header search information.
1392 class HeaderFileInfoTrait {
1393 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001394
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001395 // Keep track of the framework names we've used during serialization.
1396 SmallVector<char, 128> FrameworkStringData;
1397 llvm::StringMap<unsigned> FrameworkNameOffset;
1398
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001399 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001400 HeaderFileInfoTrait(ASTWriter &Writer)
1401 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001402
1403 typedef const char *key_type;
1404 typedef key_type key_type_ref;
1405
1406 typedef HeaderFileInfo data_type;
1407 typedef const data_type &data_type_ref;
1408
1409 static unsigned ComputeHash(const char *path) {
1410 // The hash is based only on the filename portion of the key, so that the
1411 // reader can match based on filenames when symlinking or excess path
1412 // elements ("foo/../", "../") change the form of the name. However,
1413 // complete path is still the key.
1414 return llvm::HashString(llvm::sys::path::filename(path));
1415 }
1416
1417 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001418 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001419 data_type_ref Data) {
1420 unsigned StrLen = strlen(path);
1421 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001422 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001423 clang::io::Emit8(Out, DataLen);
1424 return std::make_pair(StrLen + 1, DataLen);
1425 }
1426
Chris Lattner5f9e2722011-07-23 10:55:15 +00001427 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001428 Out.write(path, KeyLen);
1429 }
1430
Chris Lattner5f9e2722011-07-23 10:55:15 +00001431 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001432 data_type_ref Data, unsigned DataLen) {
1433 using namespace clang::io;
1434 uint64_t Start = Out.tell(); (void)Start;
1435
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001436 unsigned char Flags = (Data.isImport << 5)
1437 | (Data.isPragmaOnce << 4)
1438 | (Data.DirInfo << 2)
1439 | (Data.Resolved << 1)
1440 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001441 Emit8(Out, (uint8_t)Flags);
1442 Emit16(Out, (uint16_t) Data.NumIncludes);
1443
1444 if (!Data.ControllingMacro)
1445 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1446 else
1447 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001448
1449 unsigned Offset = 0;
1450 if (!Data.Framework.empty()) {
1451 // If this header refers into a framework, save the framework name.
1452 llvm::StringMap<unsigned>::iterator Pos
1453 = FrameworkNameOffset.find(Data.Framework);
1454 if (Pos == FrameworkNameOffset.end()) {
1455 Offset = FrameworkStringData.size() + 1;
1456 FrameworkStringData.append(Data.Framework.begin(),
1457 Data.Framework.end());
1458 FrameworkStringData.push_back(0);
1459
1460 FrameworkNameOffset[Data.Framework] = Offset;
1461 } else
1462 Offset = Pos->second;
1463 }
1464 Emit32(Out, Offset);
1465
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001466 assert(Out.tell() - Start == DataLen && "Wrong data length");
1467 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001468
1469 const char *strings_begin() const { return FrameworkStringData.begin(); }
1470 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001471 };
1472} // end anonymous namespace
1473
1474/// \brief Write the header search block for the list of files that
1475///
1476/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001477void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001478 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001479 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1480
1481 if (FilesByUID.size() > HS.header_file_size())
1482 FilesByUID.resize(HS.header_file_size());
1483
Benjamin Kramerfacde172012-06-06 17:32:50 +00001484 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001485 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001486 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001487 unsigned NumHeaderSearchEntries = 0;
1488 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1489 const FileEntry *File = FilesByUID[UID];
1490 if (!File)
1491 continue;
1492
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001493 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1494 // from the external source if it was not provided already.
1495 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001496 if (HFI.External && Chain)
1497 continue;
1498
1499 // Turn the file name into an absolute path, if it isn't already.
1500 const char *Filename = File->getName();
1501 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1502
1503 // If we performed any translation on the file name at all, we need to
1504 // save this string, since the generator will refer to it later.
1505 if (Filename != File->getName()) {
1506 Filename = strdup(Filename);
1507 SavedStrings.push_back(Filename);
1508 }
1509
1510 Generator.insert(Filename, HFI, GeneratorTrait);
1511 ++NumHeaderSearchEntries;
1512 }
1513
1514 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001515 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001516 uint32_t BucketOffset;
1517 {
1518 llvm::raw_svector_ostream Out(TableData);
1519 // Make sure that no bucket is at offset 0
1520 clang::io::Emit32(Out, 0);
1521 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1522 }
1523
1524 // Create a blob abbreviation
1525 using namespace llvm;
1526 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1527 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001530 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001531 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1532 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1533
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001534 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001535 RecordData Record;
1536 Record.push_back(HEADER_SEARCH_TABLE);
1537 Record.push_back(BucketOffset);
1538 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001539 Record.push_back(TableData.size());
1540 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001541 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1542
1543 // Free all of the strings we had to duplicate.
1544 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1545 free((void*)SavedStrings[I]);
1546}
1547
Douglas Gregor14f79002009-04-10 03:52:48 +00001548/// \brief Writes the block containing the serialized form of the
1549/// source manager.
1550///
1551/// TODO: We should probably use an on-disk hash table (stored in a
1552/// blob), indexed based on the file name, so that we only create
1553/// entries for files that we actually need. In the common case (no
1554/// errors), we probably won't have to create file entries for any of
1555/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001556void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001557 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001558 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001559 RecordData Record;
1560
Chris Lattnerf04ad692009-04-10 17:16:57 +00001561 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001562 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001563
1564 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001565 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1566 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1567 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001568 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001569
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570 // Write out the source location entry table. We skip the first
1571 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001572 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001573 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001574 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1575 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001576 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001577 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001578 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001579 FileID FID = FileID::get(I);
1580 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001581
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001582 // Record the offset of this source-location entry.
1583 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1584
1585 // Figure out which record code to use.
1586 unsigned Code;
1587 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001588 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1589 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001590 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001591 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001592 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001593 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001594 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001595 Record.clear();
1596 Record.push_back(Code);
1597
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001598 // Starting offset of this entry within this module, so skip the dummy.
1599 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001600 if (SLoc->isFile()) {
1601 const SrcMgr::FileInfo &File = SLoc->getFile();
1602 Record.push_back(File.getIncludeLoc().getRawEncoding());
1603 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1604 Record.push_back(File.hasLineDirectives());
1605
1606 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001607 if (Content->OrigEntry) {
1608 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001609 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001610
Douglas Gregora930dc92012-10-22 18:42:04 +00001611 // The source location entry is a file. Emit input file ID.
1612 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1613 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001615 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001616
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001617 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001618 if (FDI != FileDeclIDs.end()) {
1619 Record.push_back(FDI->second->FirstDeclIndex);
1620 Record.push_back(FDI->second->DeclIDs.size());
1621 } else {
1622 Record.push_back(0);
1623 Record.push_back(0);
1624 }
Douglas Gregora081da52011-11-16 20:05:18 +00001625
Douglas Gregora930dc92012-10-22 18:42:04 +00001626 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001627
1628 if (Content->BufferOverridden) {
1629 Record.clear();
1630 Record.push_back(SM_SLOC_BUFFER_BLOB);
1631 const llvm::MemoryBuffer *Buffer
1632 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1633 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1634 StringRef(Buffer->getBufferStart(),
1635 Buffer->getBufferSize() + 1));
1636 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001637 } else {
1638 // The source location entry is a buffer. The blob associated
1639 // with this entry contains the contents of the buffer.
1640
1641 // We add one to the size so that we capture the trailing NULL
1642 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1643 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001644 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001645 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001646 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001647 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001648 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001649 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001650 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001651 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001652 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001653 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001654
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001655 if (strcmp(Name, "<built-in>") == 0) {
1656 PreloadSLocs.push_back(SLocEntryOffsets.size());
1657 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001658 }
1659 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001660 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001661 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001662 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1663 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001664 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1665 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001666
1667 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001668 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001669 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001670 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001671 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001672 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001673 }
1674 }
1675
Douglas Gregorc9490c02009-04-16 22:23:12 +00001676 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001677
1678 if (SLocEntryOffsets.empty())
1679 return;
1680
Sebastian Redl3397c552010-08-18 23:56:27 +00001681 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001682 // table is used for lazily loading source-location information.
1683 using namespace llvm;
1684 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001685 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1689 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001691 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001692 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001693 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001694 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001695 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001696
Sebastian Redl3397c552010-08-18 23:56:27 +00001697 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001698 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001699 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001700
1701 // Write the line table. It depends on remapping working, so it must come
1702 // after the source location offsets.
1703 if (SourceMgr.hasLineTable()) {
1704 LineTableInfo &LineTable = SourceMgr.getLineTable();
1705
1706 Record.clear();
1707 // Emit the file names
1708 Record.push_back(LineTable.getNumFilenames());
1709 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1710 // Emit the file name
1711 const char *Filename = LineTable.getFilename(I);
1712 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1713 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1714 Record.push_back(FilenameLen);
1715 if (FilenameLen)
1716 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1717 }
1718
1719 // Emit the line entries
1720 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1721 L != LEnd; ++L) {
1722 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001723 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001724 continue;
1725
1726 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001727 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001728
1729 // Emit the line entries
1730 Record.push_back(L->second.size());
1731 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1732 LEEnd = L->second.end();
1733 LE != LEEnd; ++LE) {
1734 Record.push_back(LE->FileOffset);
1735 Record.push_back(LE->LineNo);
1736 Record.push_back(LE->FilenameID);
1737 Record.push_back((unsigned)LE->FileKind);
1738 Record.push_back(LE->IncludeOffset);
1739 }
1740 }
1741 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1742 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001743}
1744
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001745//===----------------------------------------------------------------------===//
1746// Preprocessor Serialization
1747//===----------------------------------------------------------------------===//
1748
Douglas Gregor9c736102011-02-10 18:20:09 +00001749static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1750 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1751 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1752 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1753 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1754 return X.first->getName().compare(Y.first->getName());
1755}
1756
Chris Lattner0b1fb982009-04-10 17:15:23 +00001757/// \brief Writes the block containing the serialized form of the
1758/// preprocessor.
1759///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001760void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001761 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1762 if (PPRec)
1763 WritePreprocessorDetail(*PPRec);
1764
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001765 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001766
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001767 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1768 if (PP.getCounterValue() != 0) {
1769 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001770 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001771 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001772 }
1773
1774 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001775 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Sebastian Redl3397c552010-08-18 23:56:27 +00001777 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001778 // FIXME: use diagnostics subsystem for localization etc.
1779 if (PP.SawDateOrTime())
1780 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Douglas Gregorecdcb882010-10-20 22:00:55 +00001782
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001783 // Loop over all the macro definitions that are live at the end of the file,
1784 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001785
Douglas Gregor9c736102011-02-10 18:20:09 +00001786 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001787 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001788 MacrosToEmit;
1789 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001790 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001791 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001792 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001793 if (!IsModule || I->second->isPublic()) {
1794 MacroDefinitionsSeen.insert(I->first);
1795 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001796 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001797 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001798
Douglas Gregor9c736102011-02-10 18:20:09 +00001799 // Sort the set of macro definitions that need to be serialized by the
1800 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001801 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001802 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001803
Douglas Gregora8235d62012-10-09 23:05:51 +00001804 /// \brief Offsets of each of the macros into the bitstream, indexed by
1805 /// the local macro ID
1806 ///
1807 /// For each identifier that is associated with a macro, this map
1808 /// provides the offset into the bitstream where that macro is
1809 /// defined.
1810 std::vector<uint32_t> MacroOffsets;
1811
Douglas Gregor9c736102011-02-10 18:20:09 +00001812 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1813 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001814
Douglas Gregora8235d62012-10-09 23:05:51 +00001815 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1816 MI = MI->getPreviousDefinition()) {
1817 MacroID ID = getMacroRef(MI);
1818 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001819 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Douglas Gregora8235d62012-10-09 23:05:51 +00001821 // Skip macros from a AST file if we're chaining.
1822 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1823 continue;
1824
1825 if (ID < FirstMacroID) {
1826 // This will have been dealt with via an update record.
1827 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1828 continue;
1829 }
1830
1831 // Record the local offset of this macro.
1832 unsigned Index = ID - FirstMacroID;
1833 if (Index == MacroOffsets.size())
1834 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1835 else {
1836 if (Index > MacroOffsets.size())
1837 MacroOffsets.resize(Index + 1);
1838
1839 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1840 }
1841
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001842 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001843 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001844 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001845 AddSourceLocation(MI->getDefinitionLoc(), Record);
1846 AddSourceLocation(MI->getUndefLoc(), Record);
1847 Record.push_back(MI->isUsed());
1848 Record.push_back(MI->isPublic());
1849 AddSourceLocation(MI->getVisibilityLocation(), Record);
1850 unsigned Code;
1851 if (MI->isObjectLike()) {
1852 Code = PP_MACRO_OBJECT_LIKE;
1853 } else {
1854 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001855
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001856 Record.push_back(MI->isC99Varargs());
1857 Record.push_back(MI->isGNUVarargs());
1858 Record.push_back(MI->getNumArgs());
1859 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1860 I != E; ++I)
1861 AddIdentifierRef(*I, Record);
1862 }
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001864 // If we have a detailed preprocessing record, record the macro definition
1865 // ID that corresponds to this macro.
1866 if (PPRec)
1867 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1868
1869 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001870 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001871
1872 // Emit the tokens array.
1873 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1874 // Note that we know that the preprocessor does not have any annotation
1875 // tokens in it because they are created by the parser, and thus can't
1876 // be in a macro definition.
1877 const Token &Tok = MI->getReplacementToken(TokNo);
1878
1879 Record.push_back(Tok.getLocation().getRawEncoding());
1880 Record.push_back(Tok.getLength());
1881
1882 // FIXME: When reading literal tokens, reconstruct the literal pointer
1883 // if it is needed.
1884 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1885 // FIXME: Should translate token kind to a stable encoding.
1886 Record.push_back(Tok.getKind());
1887 // FIXME: Should translate token flags to a stable encoding.
1888 Record.push_back(Tok.getFlags());
1889
1890 Stream.EmitRecord(PP_TOKEN, Record);
1891 Record.clear();
1892 }
1893 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001894 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001895 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001896 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001897
1898 // Write the offsets table for macro IDs.
1899 using namespace llvm;
1900 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1901 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1905
1906 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1907 Record.clear();
1908 Record.push_back(MACRO_OFFSET);
1909 Record.push_back(MacroOffsets.size());
1910 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1911 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1912 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001913}
1914
1915void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001916 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001917 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001918
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001919 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001920
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001921 // Enter the preprocessor block.
1922 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001923
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001924 // If the preprocessor has a preprocessing record, emit it.
1925 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001926 using namespace llvm;
1927
1928 // Set up the abbreviation for
1929 unsigned InclusionAbbrev = 0;
1930 {
1931 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1932 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1938 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1939 }
1940
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001941 unsigned FirstPreprocessorEntityID
1942 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1943 + NUM_PREDEF_PP_ENTITY_IDS;
1944 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001945 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001946 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1947 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001948 E != EEnd;
1949 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001950 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001951
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001952 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1953 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001954
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001955 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001956 // Record this macro definition's ID.
1957 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001958
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001959 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001960 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1961 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001962 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001963
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001964 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001965 Record.push_back(ME->isBuiltinMacro());
1966 if (ME->isBuiltinMacro())
1967 AddIdentifierRef(ME->getName(), Record);
1968 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001969 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001970 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001971 continue;
1972 }
1973
1974 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1975 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001976 Record.push_back(ID->getFileName().size());
1977 Record.push_back(ID->wasInQuotes());
1978 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001979 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001980 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001981 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001982 // Check that the FileEntry is not null because it was not resolved and
1983 // we create a PCH even with compiler errors.
1984 if (ID->getFile())
1985 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001986 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1987 continue;
1988 }
1989
1990 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1991 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001992 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001993
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001994 // Write the offsets table for the preprocessing record.
1995 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001996 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1997
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001998 // Write the offsets table for identifier IDs.
1999 using namespace llvm;
2000 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002001 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002004 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002005
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002006 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002007 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002008 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002009 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2010 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002011 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002012}
2013
Douglas Gregore209e502011-12-06 01:10:29 +00002014unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2015 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2016 if (Known != SubmoduleIDs.end())
2017 return Known->second;
2018
2019 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2020}
2021
Douglas Gregor26ced122011-12-01 00:59:36 +00002022/// \brief Compute the number of modules within the given tree (including the
2023/// given module).
2024static unsigned getNumberOfModules(Module *Mod) {
2025 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002026 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2027 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002028 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002029 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002030
2031 return ChildModules + 1;
2032}
2033
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002034void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002035 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002036 // FIXME: This feels like it belongs somewhere else, but there are no
2037 // other consumers of this information.
2038 SourceManager &SrcMgr = PP->getSourceManager();
2039 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2040 for (ASTContext::import_iterator I = Context->local_import_begin(),
2041 IEnd = Context->local_import_end();
2042 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002043 if (Module *ImportedFrom
2044 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2045 SrcMgr))) {
2046 ImportedFrom->Imports.push_back(I->getImportedModule());
2047 }
2048 }
2049
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002050 // Enter the submodule description block.
2051 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2052
2053 // Write the abbreviations needed for the submodules block.
2054 using namespace llvm;
2055 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2056 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2066 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2067
2068 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002069 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002070 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2071 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2072
2073 Abbrev = new BitCodeAbbrev();
2074 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2075 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2076 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002077
2078 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002079 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2081 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2082
2083 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002084 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2086 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2087
Douglas Gregor51f564f2011-12-31 04:05:44 +00002088 Abbrev = new BitCodeAbbrev();
2089 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2090 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2091 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2092
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002093 Abbrev = new BitCodeAbbrev();
2094 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2095 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2096 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2097
Douglas Gregor26ced122011-12-01 00:59:36 +00002098 // Write the submodule metadata block.
2099 RecordData Record;
2100 Record.push_back(getNumberOfModules(WritingModule));
2101 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2102 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2103
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002104 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002105 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002106 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002107 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002108 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002109 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002110 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002111
2112 // Emit the definition of the block.
2113 Record.clear();
2114 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002115 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002116 if (Mod->Parent) {
2117 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2118 Record.push_back(SubmoduleIDs[Mod->Parent]);
2119 } else {
2120 Record.push_back(0);
2121 }
2122 Record.push_back(Mod->IsFramework);
2123 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002124 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002125 Record.push_back(Mod->InferSubmodules);
2126 Record.push_back(Mod->InferExplicitSubmodules);
2127 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002128 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2129
Douglas Gregor51f564f2011-12-31 04:05:44 +00002130 // Emit the requirements.
2131 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2132 Record.clear();
2133 Record.push_back(SUBMODULE_REQUIRES);
2134 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2135 Mod->Requires[I].data(),
2136 Mod->Requires[I].size());
2137 }
2138
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002139 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002140 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002141 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002142 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002143 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002144 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002145 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2146 Record.clear();
2147 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2148 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2149 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002150 }
2151
2152 // Emit the headers.
2153 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2154 Record.clear();
2155 Record.push_back(SUBMODULE_HEADER);
2156 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2157 Mod->Headers[I]->getName());
2158 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002159 // Emit the excluded headers.
2160 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2161 Record.clear();
2162 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2163 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2164 Mod->ExcludedHeaders[I]->getName());
2165 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002166 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2167 Record.clear();
2168 Record.push_back(SUBMODULE_TOPHEADER);
2169 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2170 Mod->TopHeaders[I]->getName());
2171 }
Douglas Gregor55988682011-12-05 16:33:54 +00002172
2173 // Emit the imports.
2174 if (!Mod->Imports.empty()) {
2175 Record.clear();
2176 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002177 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002178 assert(ImportedID && "Unknown submodule!");
2179 Record.push_back(ImportedID);
2180 }
2181 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2182 }
2183
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002184 // Emit the exports.
2185 if (!Mod->Exports.empty()) {
2186 Record.clear();
2187 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002188 if (Module *Exported = Mod->Exports[I].getPointer()) {
2189 unsigned ExportedID = SubmoduleIDs[Exported];
2190 assert(ExportedID > 0 && "Unknown submodule ID?");
2191 Record.push_back(ExportedID);
2192 } else {
2193 Record.push_back(0);
2194 }
2195
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002196 Record.push_back(Mod->Exports[I].getInt());
2197 }
2198 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2199 }
2200
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002201 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002202 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2203 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002204 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002205 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002206 }
2207
2208 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002209
2210 assert((NextSubmoduleID - FirstSubmoduleID
2211 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002212}
2213
Douglas Gregor185dbd72011-12-01 02:07:58 +00002214serialization::SubmoduleID
2215ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002216 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002217 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002218
2219 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002220 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002221 Module *OwningMod
2222 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002223 if (!OwningMod)
2224 return 0;
2225
Douglas Gregore209e502011-12-06 01:10:29 +00002226 // Check whether this submodule is part of our own module.
2227 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002228 return 0;
2229
Douglas Gregore209e502011-12-06 01:10:29 +00002230 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002231}
2232
David Blaikied6471f72011-09-25 23:23:43 +00002233void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002234 // FIXME: Make it work properly with modules.
2235 llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2236 DiagStateIDMap;
2237 unsigned CurrID = 0;
2238 DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002239 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002240 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002241 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2242 I != E; ++I) {
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002243 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002244 if (point.Loc.isInvalid())
2245 continue;
2246
2247 Record.push_back(point.Loc.getRawEncoding());
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002248 unsigned &DiagStateID = DiagStateIDMap[point.State];
2249 Record.push_back(DiagStateID);
2250
2251 if (DiagStateID == 0) {
2252 DiagStateID = ++CurrID;
2253 for (DiagnosticsEngine::DiagState::const_iterator
2254 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
2255 if (I->second.isPragma()) {
2256 Record.push_back(I->first);
2257 Record.push_back(I->second.getMapping());
2258 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002259 }
Argyrios Kyrtzidis33e15762012-10-30 00:27:21 +00002260 Record.push_back(-1); // mark the end of the diag/map pairs for this
2261 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002262 }
2263 }
2264
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002265 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002266 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002267}
2268
Anders Carlssonc8505782011-03-06 18:41:18 +00002269void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2270 if (CXXBaseSpecifiersOffsets.empty())
2271 return;
2272
2273 RecordData Record;
2274
2275 // Create a blob abbreviation for the C++ base specifiers offsets.
2276 using namespace llvm;
2277
2278 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2279 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2282 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2283
Douglas Gregore92b8a12011-08-04 00:01:48 +00002284 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002285 Record.clear();
2286 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2287 Record.push_back(CXXBaseSpecifiersOffsets.size());
2288 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002289 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002290}
2291
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002292//===----------------------------------------------------------------------===//
2293// Type Serialization
2294//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002295
Sebastian Redl3397c552010-08-18 23:56:27 +00002296/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002297void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002298 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002299 if (Idx.getIndex() == 0) // we haven't seen this type before.
2300 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002301
Douglas Gregor97475832010-10-05 18:37:06 +00002302 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002303
Douglas Gregor2cf26342009-04-09 22:27:44 +00002304 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002305 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002306 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002307 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002308 else if (TypeOffsets.size() < Index) {
2309 TypeOffsets.resize(Index + 1);
2310 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002311 }
2312
2313 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregor2cf26342009-04-09 22:27:44 +00002315 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002316 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002317
Douglas Gregora4923eb2009-11-16 21:35:15 +00002318 if (T.hasLocalNonFastQualifiers()) {
2319 Qualifiers Qs = T.getLocalQualifiers();
2320 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002321 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002322 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002323 } else {
2324 switch (T->getTypeClass()) {
2325 // For all of the concrete, non-dependent types, call the
2326 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002327#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002328 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002329#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002330#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002331 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332 }
2333
2334 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002335 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002336
2337 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002338 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002339}
2340
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002341//===----------------------------------------------------------------------===//
2342// Declaration Serialization
2343//===----------------------------------------------------------------------===//
2344
Douglas Gregor2cf26342009-04-09 22:27:44 +00002345/// \brief Write the block containing all of the declaration IDs
2346/// lexically declared within the given DeclContext.
2347///
2348/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2349/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002350uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002351 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002352 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002353 return 0;
2354
Douglas Gregorc9490c02009-04-16 22:23:12 +00002355 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002356 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002357 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002358 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002359 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2360 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002361 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002362
Douglas Gregor25123082009-04-22 22:34:57 +00002363 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002364 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002365 return Offset;
2366}
2367
Sebastian Redla4232eb2010-08-18 23:56:21 +00002368void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002369 using namespace llvm;
2370 RecordData Record;
2371
2372 // Write the type offsets array
2373 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002374 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2378 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2379 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002380 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002381 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002382 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002383 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002384
2385 // Write the declaration offsets array
2386 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002387 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2391 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2392 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002393 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002394 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002395 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002396 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002397}
2398
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002399void ASTWriter::WriteFileDeclIDsMap() {
2400 using namespace llvm;
2401 RecordData Record;
2402
2403 // Join the vectors of DeclIDs from all files.
2404 SmallVector<DeclID, 256> FileSortedIDs;
2405 for (FileDeclIDsTy::iterator
2406 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2407 DeclIDInFileInfo &Info = *FI->second;
2408 Info.FirstDeclIndex = FileSortedIDs.size();
2409 for (LocDeclIDsTy::iterator
2410 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2411 FileSortedIDs.push_back(DI->second);
2412 }
2413
2414 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2415 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002416 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2418 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2419 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002420 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002421 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2422}
2423
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002424void ASTWriter::WriteComments() {
2425 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002426 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002427 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002428 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2429 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002430 I != E; ++I) {
2431 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002432 AddSourceRange((*I)->getSourceRange(), Record);
2433 Record.push_back((*I)->getKind());
2434 Record.push_back((*I)->isTrailingComment());
2435 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002436 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2437 }
2438 Stream.ExitBlock();
2439}
2440
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002441//===----------------------------------------------------------------------===//
2442// Global Method Pool and Selector Serialization
2443//===----------------------------------------------------------------------===//
2444
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002445namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002446// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002447class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002448 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002449
2450public:
2451 typedef Selector key_type;
2452 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002453
Sebastian Redl5d050072010-08-04 17:20:04 +00002454 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002455 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002456 ObjCMethodList Instance, Factory;
2457 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002458 typedef const data_type& data_type_ref;
2459
Sebastian Redl3397c552010-08-18 23:56:27 +00002460 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002462 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002463 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002464 }
Mike Stump1eb44332009-09-09 15:08:12 +00002465
2466 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002467 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002468 data_type_ref Methods) {
2469 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2470 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002471 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2472 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002473 Method = Method->Next)
2474 if (Method->Method)
2475 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002476 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002477 Method = Method->Next)
2478 if (Method->Method)
2479 DataLen += 4;
2480 clang::io::Emit16(Out, DataLen);
2481 return std::make_pair(KeyLen, DataLen);
2482 }
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Chris Lattner5f9e2722011-07-23 10:55:15 +00002484 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002485 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002486 assert((Start >> 32) == 0 && "Selector key offset too large");
2487 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002488 unsigned N = Sel.getNumArgs();
2489 clang::io::Emit16(Out, N);
2490 if (N == 0)
2491 N = 1;
2492 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002493 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002494 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2495 }
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Chris Lattner5f9e2722011-07-23 10:55:15 +00002497 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002498 data_type_ref Methods, unsigned DataLen) {
2499 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002500 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002501 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002502 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002503 Method = Method->Next)
2504 if (Method->Method)
2505 ++NumInstanceMethods;
2506
2507 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002508 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002509 Method = Method->Next)
2510 if (Method->Method)
2511 ++NumFactoryMethods;
2512
2513 clang::io::Emit16(Out, NumInstanceMethods);
2514 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002515 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002516 Method = Method->Next)
2517 if (Method->Method)
2518 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002519 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002520 Method = Method->Next)
2521 if (Method->Method)
2522 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002523
2524 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002525 }
2526};
2527} // end anonymous namespace
2528
Sebastian Redl059612d2010-08-03 21:58:15 +00002529/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002530///
2531/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002532/// in an on-disk hash table indexed by the selector. The hash table also
2533/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002534void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002535 using namespace llvm;
2536
Sebastian Redl059612d2010-08-03 21:58:15 +00002537 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002538 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002539 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002540 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002541 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002542 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002543 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002544 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002545
Sebastian Redl059612d2010-08-03 21:58:15 +00002546 // Create the on-disk hash table representation. We walk through every
2547 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002548 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002549 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002550 I = SelectorIDs.begin(), E = SelectorIDs.end();
2551 I != E; ++I) {
2552 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002553 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002554 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002555 I->second,
2556 ObjCMethodList(),
2557 ObjCMethodList()
2558 };
2559 if (F != SemaRef.MethodPool.end()) {
2560 Data.Instance = F->second.first;
2561 Data.Factory = F->second.second;
2562 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002563 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002564 // changed.
2565 if (Chain && I->second < FirstSelectorID) {
2566 // Selector already exists. Did it change?
2567 bool changed = false;
2568 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2569 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002570 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002571 changed = true;
2572 }
2573 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2574 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002575 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002576 changed = true;
2577 }
2578 if (!changed)
2579 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002580 } else if (Data.Instance.Method || Data.Factory.Method) {
2581 // A new method pool entry.
2582 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002583 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002584 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002585 }
2586
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002587 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002588 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002589 uint32_t BucketOffset;
2590 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002591 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002592 llvm::raw_svector_ostream Out(MethodPool);
2593 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002594 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002595 BucketOffset = Generator.Emit(Out, Trait);
2596 }
2597
2598 // Create a blob abbreviation
2599 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002600 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002601 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002602 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002603 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2604 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2605
Douglas Gregor83941df2009-04-25 17:48:32 +00002606 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002607 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002608 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002609 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002610 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002611 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002612
2613 // Create a blob abbreviation for the selector table offsets.
2614 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002615 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002616 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002617 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002618 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2619 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2620
2621 // Write the selector offsets table.
2622 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002623 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002624 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002625 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002626 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002627 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002628 }
2629}
2630
Sebastian Redl3397c552010-08-18 23:56:27 +00002631/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002632void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002633 using namespace llvm;
2634 if (SemaRef.ReferencedSelectors.empty())
2635 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002636
Fariborz Jahanian32019832010-07-23 19:11:11 +00002637 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002638
Sebastian Redl3397c552010-08-18 23:56:27 +00002639 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002640 // very tricky to fix, and given that @selector shouldn't really appear in
2641 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002642 for (DenseMap<Selector, SourceLocation>::iterator S =
2643 SemaRef.ReferencedSelectors.begin(),
2644 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2645 Selector Sel = (*S).first;
2646 SourceLocation Loc = (*S).second;
2647 AddSelectorRef(Sel, Record);
2648 AddSourceLocation(Loc, Record);
2649 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002650 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002651}
2652
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002653//===----------------------------------------------------------------------===//
2654// Identifier Table Serialization
2655//===----------------------------------------------------------------------===//
2656
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002657namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002658class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002659 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002660 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002661 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002662 bool IsModule;
2663
Douglas Gregora92193e2009-04-28 21:18:29 +00002664 /// \brief Determines whether this is an "interesting" identifier
2665 /// that needs a full IdentifierInfo structure written into the hash
2666 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002667 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002668 if (II->isPoisoned() ||
2669 II->isExtensionToken() ||
2670 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002671 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002672 II->getFETokenInfo<void>())
2673 return true;
2674
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002675 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002676 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002677
2678 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2679 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002680 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002681
2682 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002683 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002684
2685 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002686 }
2687
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002688public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002689 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002690 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002692 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002693 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002694
Douglas Gregoreee242f2011-10-27 09:33:13 +00002695 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2696 IdentifierResolver &IdResolver, bool IsModule)
2697 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002698
2699 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002700 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002701 }
Mike Stump1eb44332009-09-09 15:08:12 +00002702
2703 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002704 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002705 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002706 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002707 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002708 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002709 DataLen += 2; // 2 bytes for builtin ID
2710 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002711 if (hadMacroDefinition(II, Macro)) {
2712 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2713 if (Writer.getMacroRef(M) != 0)
2714 DataLen += 4;
2715 }
2716
2717 DataLen += 4;
2718 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002719
Douglas Gregoreee242f2011-10-27 09:33:13 +00002720 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2721 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002722 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002723 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002724 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002725 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002726 // We emit the key length after the data length so that every
2727 // string is preceded by a 16-bit length. This matches the PTH
2728 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002729 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002730 return std::make_pair(KeyLen, DataLen);
2731 }
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002734 unsigned KeyLen) {
2735 // Record the location of the key data. This is used when generating
2736 // the mapping from persistent IDs to strings.
2737 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002738 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002739 }
Mike Stump1eb44332009-09-09 15:08:12 +00002740
Douglas Gregor7143aab2011-09-01 17:04:32 +00002741 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002742 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002743 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002744 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002745 clang::io::Emit32(Out, ID << 1);
2746 return;
2747 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002748
Douglas Gregora92193e2009-04-28 21:18:29 +00002749 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002750 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2751 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2752 clang::io::Emit16(Out, Bits);
2753 Bits = 0;
2754 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002755 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002756 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2757 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002758 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002759 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002760 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002761
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002762 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002763 // Write all of the macro IDs associated with this identifier.
2764 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2765 if (MacroID ID = Writer.getMacroRef(M))
2766 clang::io::Emit32(Out, ID);
2767 }
2768
2769 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002770 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002771
Douglas Gregor668c1a42009-04-21 22:25:48 +00002772 // Emit the declaration IDs in reverse order, because the
2773 // IdentifierResolver provides the declarations as they would be
2774 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002775 // "stat"), but the ASTReader adds declarations to the end of the list
2776 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002777 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002778 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2779 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002780 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002781 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002782 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002783 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002784 }
2785};
2786} // end anonymous namespace
2787
Sebastian Redl3397c552010-08-18 23:56:27 +00002788/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002789///
2790/// The identifier table consists of a blob containing string data
2791/// (the actual identifiers themselves) and a separate "offsets" index
2792/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002793void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2794 IdentifierResolver &IdResolver,
2795 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002796 using namespace llvm;
2797
2798 // Create and write out the blob that contains the identifier
2799 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002800 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002801 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002802 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Douglas Gregor92b059e2009-04-28 20:33:11 +00002804 // Look for any identifiers that were named while processing the
2805 // headers, but are otherwise not needed. We add these to the hash
2806 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002807 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002808 // file.
2809 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2810 IDEnd = PP.getIdentifierTable().end();
2811 ID != IDEnd; ++ID)
2812 getIdentifierRef(ID->second);
2813
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002814 // Create the on-disk hash table representation. We only store offsets
2815 // for identifiers that appear here for the first time.
2816 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002817 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002818 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2819 ID != IDEnd; ++ID) {
2820 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002821 if (!Chain || !ID->first->isFromAST() ||
2822 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002823 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2824 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002825 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002826
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002827 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002828 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002829 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002830 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002831 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002832 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002833 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002834 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002835 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002836 }
2837
2838 // Create a blob abbreviation
2839 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002840 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002843 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002844
2845 // Write the identifier table
2846 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002847 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002848 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002849 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002850 }
2851
2852 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002853 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002854 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002855 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002856 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2858 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2859
2860 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002861 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002862 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002863 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002864 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002865 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002866}
2867
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002868//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002869// DeclContext's Name Lookup Table Serialization
2870//===----------------------------------------------------------------------===//
2871
2872namespace {
2873// Trait used for the on-disk hash table used in the method pool.
2874class ASTDeclContextNameLookupTrait {
2875 ASTWriter &Writer;
2876
2877public:
2878 typedef DeclarationName key_type;
2879 typedef key_type key_type_ref;
2880
2881 typedef DeclContext::lookup_result data_type;
2882 typedef const data_type& data_type_ref;
2883
2884 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2885
2886 unsigned ComputeHash(DeclarationName Name) {
2887 llvm::FoldingSetNodeID ID;
2888 ID.AddInteger(Name.getNameKind());
2889
2890 switch (Name.getNameKind()) {
2891 case DeclarationName::Identifier:
2892 ID.AddString(Name.getAsIdentifierInfo()->getName());
2893 break;
2894 case DeclarationName::ObjCZeroArgSelector:
2895 case DeclarationName::ObjCOneArgSelector:
2896 case DeclarationName::ObjCMultiArgSelector:
2897 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2898 break;
2899 case DeclarationName::CXXConstructorName:
2900 case DeclarationName::CXXDestructorName:
2901 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002902 break;
2903 case DeclarationName::CXXOperatorName:
2904 ID.AddInteger(Name.getCXXOverloadedOperator());
2905 break;
2906 case DeclarationName::CXXLiteralOperatorName:
2907 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2908 case DeclarationName::CXXUsingDirective:
2909 break;
2910 }
2911
2912 return ID.ComputeHash();
2913 }
2914
2915 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002916 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002917 data_type_ref Lookup) {
2918 unsigned KeyLen = 1;
2919 switch (Name.getNameKind()) {
2920 case DeclarationName::Identifier:
2921 case DeclarationName::ObjCZeroArgSelector:
2922 case DeclarationName::ObjCOneArgSelector:
2923 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002924 case DeclarationName::CXXLiteralOperatorName:
2925 KeyLen += 4;
2926 break;
2927 case DeclarationName::CXXOperatorName:
2928 KeyLen += 1;
2929 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002930 case DeclarationName::CXXConstructorName:
2931 case DeclarationName::CXXDestructorName:
2932 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002933 case DeclarationName::CXXUsingDirective:
2934 break;
2935 }
2936 clang::io::Emit16(Out, KeyLen);
2937
2938 // 2 bytes for num of decls and 4 for each DeclID.
2939 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2940 clang::io::Emit16(Out, DataLen);
2941
2942 return std::make_pair(KeyLen, DataLen);
2943 }
2944
Chris Lattner5f9e2722011-07-23 10:55:15 +00002945 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002946 using namespace clang::io;
2947
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002948 Emit8(Out, Name.getNameKind());
2949 switch (Name.getNameKind()) {
2950 case DeclarationName::Identifier:
2951 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002952 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002953 case DeclarationName::ObjCZeroArgSelector:
2954 case DeclarationName::ObjCOneArgSelector:
2955 case DeclarationName::ObjCMultiArgSelector:
2956 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002957 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002958 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002959 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2960 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002961 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002962 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002963 case DeclarationName::CXXLiteralOperatorName:
2964 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002965 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002966 case DeclarationName::CXXConstructorName:
2967 case DeclarationName::CXXDestructorName:
2968 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002969 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002970 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002971 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002972
2973 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002974 }
2975
Chris Lattner5f9e2722011-07-23 10:55:15 +00002976 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002977 data_type Lookup, unsigned DataLen) {
2978 uint64_t Start = Out.tell(); (void)Start;
2979 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2980 for (; Lookup.first != Lookup.second; ++Lookup.first)
2981 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2982
2983 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2984 }
2985};
2986} // end anonymous namespace
2987
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002988/// \brief Write the block containing all of the declaration IDs
2989/// visible from the given DeclContext.
2990///
2991/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002992/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002993uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2994 DeclContext *DC) {
2995 if (DC->getPrimaryContext() != DC)
2996 return 0;
2997
2998 // Since there is no name lookup into functions or methods, don't bother to
2999 // build a visible-declarations table for these entities.
3000 if (DC->isFunctionOrMethod())
3001 return 0;
3002
3003 // If not in C++, we perform name lookup for the translation unit via the
3004 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3005 // FIXME: In C++ we need the visible declarations in order to "see" the
3006 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003007 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003008 return 0;
3009
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003010 // Serialize the contents of the mapping used for lookup. Note that,
3011 // although we have two very different code paths, the serialized
3012 // representation is the same for both cases: a declaration name,
3013 // followed by a size, followed by references to the visible
3014 // declarations that have that name.
3015 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003016 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003017 if (!Map || Map->empty())
3018 return 0;
3019
3020 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3021 ASTDeclContextNameLookupTrait Trait(*this);
3022
3023 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003024 DeclarationName ConversionName;
3025 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003026 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3027 D != DEnd; ++D) {
3028 DeclarationName Name = D->first;
3029 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00003030 if (Result.first != Result.second) {
3031 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3032 // Hash all conversion function names to the same name. The actual
3033 // type information in conversion function name is not used in the
3034 // key (since such type information is not stable across different
3035 // modules), so the intended effect is to coalesce all of the conversion
3036 // functions under a single key.
3037 if (!ConversionName)
3038 ConversionName = Name;
3039 ConversionDecls.append(Result.first, Result.second);
3040 continue;
3041 }
3042
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003043 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003044 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003045 }
3046
Douglas Gregore5a54b62011-08-30 20:49:19 +00003047 // Add the conversion functions
3048 if (!ConversionDecls.empty()) {
3049 Generator.insert(ConversionName,
3050 DeclContext::lookup_result(ConversionDecls.begin(),
3051 ConversionDecls.end()),
3052 Trait);
3053 }
3054
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003055 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003056 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003057 uint32_t BucketOffset;
3058 {
3059 llvm::raw_svector_ostream Out(LookupTable);
3060 // Make sure that no bucket is at offset 0
3061 clang::io::Emit32(Out, 0);
3062 BucketOffset = Generator.Emit(Out, Trait);
3063 }
3064
3065 // Write the lookup table
3066 RecordData Record;
3067 Record.push_back(DECL_CONTEXT_VISIBLE);
3068 Record.push_back(BucketOffset);
3069 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3070 LookupTable.str());
3071
3072 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3073 ++NumVisibleDeclContexts;
3074 return Offset;
3075}
3076
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003077/// \brief Write an UPDATE_VISIBLE block for the given context.
3078///
3079/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3080/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003081/// (in C++), for namespaces, and for classes with forward-declared unscoped
3082/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003083void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003084 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3085 if (!Map || Map->empty())
3086 return;
3087
3088 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3089 ASTDeclContextNameLookupTrait Trait(*this);
3090
3091 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003092 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3093 D != DEnd; ++D) {
3094 DeclarationName Name = D->first;
3095 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003096 // For any name that appears in this table, the results are complete, i.e.
3097 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003098 if (Result.first != Result.second)
3099 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003100 }
3101
3102 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003103 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003104 uint32_t BucketOffset;
3105 {
3106 llvm::raw_svector_ostream Out(LookupTable);
3107 // Make sure that no bucket is at offset 0
3108 clang::io::Emit32(Out, 0);
3109 BucketOffset = Generator.Emit(Out, Trait);
3110 }
3111
3112 // Write the lookup table
3113 RecordData Record;
3114 Record.push_back(UPDATE_VISIBLE);
3115 Record.push_back(getDeclID(cast<Decl>(DC)));
3116 Record.push_back(BucketOffset);
3117 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3118}
3119
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003120/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3121void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3122 RecordData Record;
3123 Record.push_back(Opts.fp_contract);
3124 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3125}
3126
3127/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3128void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003129 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003130 return;
3131
3132 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3133 RecordData Record;
3134#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3135#include "clang/Basic/OpenCLExtensions.def"
3136 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3137}
3138
Douglas Gregor2171bf12012-01-15 16:58:34 +00003139void ASTWriter::WriteRedeclarations() {
3140 RecordData LocalRedeclChains;
3141 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3142
3143 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3144 Decl *First = Redeclarations[I];
3145 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3146
3147 Decl *MostRecent = First->getMostRecentDecl();
3148
3149 // If we only have a single declaration, there is no point in storing
3150 // a redeclaration chain.
3151 if (First == MostRecent)
3152 continue;
3153
3154 unsigned Offset = LocalRedeclChains.size();
3155 unsigned Size = 0;
3156 LocalRedeclChains.push_back(0); // Placeholder for the size.
3157
3158 // Collect the set of local redeclarations of this declaration.
3159 for (Decl *Prev = MostRecent; Prev != First;
3160 Prev = Prev->getPreviousDecl()) {
3161 if (!Prev->isFromASTFile()) {
3162 AddDeclRef(Prev, LocalRedeclChains);
3163 ++Size;
3164 }
3165 }
3166 LocalRedeclChains[Offset] = Size;
3167
3168 // Reverse the set of local redeclarations, so that we store them in
3169 // order (since we found them in reverse order).
3170 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3171
3172 // Add the mapping from the first ID to the set of local declarations.
3173 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3174 LocalRedeclsMap.push_back(Info);
3175
3176 assert(N == Redeclarations.size() &&
3177 "Deserialized a declaration we shouldn't have");
3178 }
3179
3180 if (LocalRedeclChains.empty())
3181 return;
3182
3183 // Sort the local redeclarations map by the first declaration ID,
3184 // since the reader will be performing binary searches on this information.
3185 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3186
3187 // Emit the local redeclarations map.
3188 using namespace llvm;
3189 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3190 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3193 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3194
3195 RecordData Record;
3196 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3197 Record.push_back(LocalRedeclsMap.size());
3198 Stream.EmitRecordWithBlob(AbbrevID, Record,
3199 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3200 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3201
3202 // Emit the redeclaration chains.
3203 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3204}
3205
Douglas Gregorcff9f262012-01-27 01:47:08 +00003206void ASTWriter::WriteObjCCategories() {
3207 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3208 RecordData Categories;
3209
3210 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3211 unsigned Size = 0;
3212 unsigned StartIndex = Categories.size();
3213
3214 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3215
3216 // Allocate space for the size.
3217 Categories.push_back(0);
3218
3219 // Add the categories.
3220 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3221 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3222 assert(getDeclID(Cat) != 0 && "Bogus category");
3223 AddDeclRef(Cat, Categories);
3224 }
3225
3226 // Update the size.
3227 Categories[StartIndex] = Size;
3228
3229 // Record this interface -> category map.
3230 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3231 CategoriesMap.push_back(CatInfo);
3232 }
3233
3234 // Sort the categories map by the definition ID, since the reader will be
3235 // performing binary searches on this information.
3236 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3237
3238 // Emit the categories map.
3239 using namespace llvm;
3240 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3241 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3244 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3245
3246 RecordData Record;
3247 Record.push_back(OBJC_CATEGORIES_MAP);
3248 Record.push_back(CategoriesMap.size());
3249 Stream.EmitRecordWithBlob(AbbrevID, Record,
3250 reinterpret_cast<char*>(CategoriesMap.data()),
3251 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3252
3253 // Emit the category lists.
3254 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3255}
3256
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003257void ASTWriter::WriteMergedDecls() {
3258 if (!Chain || Chain->MergedDecls.empty())
3259 return;
3260
3261 RecordData Record;
3262 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3263 IEnd = Chain->MergedDecls.end();
3264 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003265 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003266 : getDeclID(I->first);
3267 assert(CanonID && "Merged declaration not known?");
3268
3269 Record.push_back(CanonID);
3270 Record.push_back(I->second.size());
3271 Record.append(I->second.begin(), I->second.end());
3272 }
3273 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3274}
3275
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003276//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003277// General Serialization Routines
3278//===----------------------------------------------------------------------===//
3279
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003280/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003281void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3282 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003283 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003284 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3285 e = Attrs.end(); i != e; ++i){
3286 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003287 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003288 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003289
Sean Huntcf807c42010-08-18 23:23:40 +00003290#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003291
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003292 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003293}
3294
Chris Lattner5f9e2722011-07-23 10:55:15 +00003295void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003296 Record.push_back(Str.size());
3297 Record.insert(Record.end(), Str.begin(), Str.end());
3298}
3299
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003300void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3301 RecordDataImpl &Record) {
3302 Record.push_back(Version.getMajor());
3303 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3304 Record.push_back(*Minor + 1);
3305 else
3306 Record.push_back(0);
3307 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3308 Record.push_back(*Subminor + 1);
3309 else
3310 Record.push_back(0);
3311}
3312
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003313/// \brief Note that the identifier II occurs at the given offset
3314/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003315void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003316 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003317 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003318 // up earlier in the chain and thus don't need an offset.
3319 if (ID >= FirstIdentID)
3320 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003321}
3322
Douglas Gregor83941df2009-04-25 17:48:32 +00003323/// \brief Note that the selector Sel occurs at the given offset
3324/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003325void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003326 unsigned ID = SelectorIDs[Sel];
3327 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003328 // Don't record offsets for selectors that are also available in a different
3329 // file.
3330 if (ID < FirstSelectorID)
3331 return;
3332 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003333}
3334
Sebastian Redla4232eb2010-08-18 23:56:21 +00003335ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003336 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003337 WritingAST(false), DoneWritingDeclsAndTypes(false),
3338 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003339 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003340 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003341 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3342 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003343 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3344 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003345 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003346 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003347 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003348 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003349 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003350 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003351 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3352 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3353 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003354 DeclTypedefAbbrev(0),
3355 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3356 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003357{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003358}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003359
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003360ASTWriter::~ASTWriter() {
3361 for (FileDeclIDsTy::iterator
3362 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3363 delete I->second;
3364}
3365
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003366void ASTWriter::WriteAST(Sema &SemaRef,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003367 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003368 Module *WritingModule, StringRef isysroot,
3369 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003370 WritingAST = true;
3371
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003372 ASTHasCompilerErrors = hasErrors;
3373
Douglas Gregor2cf26342009-04-09 22:27:44 +00003374 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003375 Stream.Emit((unsigned)'C', 8);
3376 Stream.Emit((unsigned)'P', 8);
3377 Stream.Emit((unsigned)'C', 8);
3378 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003379
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003380 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003381
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003382 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003383 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003384 this->WritingModule = WritingModule;
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003385 WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003386 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003387 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003388 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003389
3390 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003391}
3392
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003393template<typename Vector>
3394static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3395 ASTWriter::RecordData &Record) {
3396 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3397 I != E; ++I) {
3398 Writer.AddDeclRef(*I, Record);
3399 }
3400}
3401
Argyrios Kyrtzidis4182ed62012-10-31 20:59:50 +00003402void ASTWriter::WriteASTCore(Sema &SemaRef,
Douglas Gregor832d6202011-07-22 16:35:34 +00003403 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003404 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003405 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003406 using namespace llvm;
3407
Douglas Gregorecc2c092011-12-01 22:20:10 +00003408 // Make sure that the AST reader knows to finalize itself.
3409 if (Chain)
3410 Chain->finalizeForWriting();
3411
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003412 ASTContext &Context = SemaRef.Context;
3413 Preprocessor &PP = SemaRef.PP;
3414
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003415 // Set up predefined declaration IDs.
3416 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003417 if (Context.ObjCIdDecl)
3418 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003419 if (Context.ObjCSelDecl)
3420 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003421 if (Context.ObjCClassDecl)
3422 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003423 if (Context.ObjCProtocolClassDecl)
3424 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003425 if (Context.Int128Decl)
3426 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3427 if (Context.UInt128Decl)
3428 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003429 if (Context.ObjCInstanceTypeDecl)
3430 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003431 if (Context.BuiltinVaListDecl)
3432 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3433
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003434 if (!Chain) {
3435 // Make sure that we emit IdentifierInfos (and any attached
3436 // declarations) for builtins. We don't need to do this when we're
3437 // emitting chained PCH files, because all of the builtins will be
3438 // in the original PCH file.
3439 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003440 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003441 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003442 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003443 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003444 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3445 getIdentifierRef(&Table.get(BuiltinNames[I]));
3446 }
3447
Douglas Gregoreee242f2011-10-27 09:33:13 +00003448 // If there are any out-of-date identifiers, bring them up to date.
3449 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3450 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3451 IDEnd = PP.getIdentifierTable().end();
3452 ID != IDEnd; ++ID)
3453 if (ID->second->isOutOfDate())
3454 ExtSource->updateOutOfDateIdentifier(*ID->second);
3455 }
3456
Chris Lattner63d65f82009-09-08 18:19:27 +00003457 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003458 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003459 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003460 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003461 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003462
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003463 // Build a record containing all of the file scoped decls in this file.
3464 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003465 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3466 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003467
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003468 // Build a record containing all of the delegating constructors we still need
3469 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003470 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003471 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003472
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003473 // Write the set of weak, undeclared identifiers. We always write the
3474 // entire table, since later PCH files in a PCH chain are only interested in
3475 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003476 RecordData WeakUndeclaredIdentifiers;
3477 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003478 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003479 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3480 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3481 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3482 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3483 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3484 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3485 }
3486 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003487
Douglas Gregor14c22f22009-04-22 22:18:58 +00003488 // Build a record containing all of the locally-scoped external
3489 // declarations in this header file. Generally, this record will be
3490 // empty.
3491 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003492 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003493 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003494 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003495 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3496 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003497 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003498 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003499 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3500 }
3501
Douglas Gregorb81c1702009-04-27 20:06:05 +00003502 // Build a record containing all of the ext_vector declarations.
3503 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003504 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003505
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003506 // Build a record containing all of the VTable uses information.
3507 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003508 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003509 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3510 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3511 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3512 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3513 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003514 }
3515
3516 // Build a record containing all of dynamic classes declarations.
3517 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003518 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003519
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003520 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003521 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003522 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003523 I = SemaRef.PendingInstantiations.begin(),
3524 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3525 AddDeclRef(I->first, PendingInstantiations);
3526 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003527 }
3528 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3529 "There are local ones at end of translation unit!");
3530
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003531 // Build a record containing some declaration references.
3532 RecordData SemaDeclRefs;
3533 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3534 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3535 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3536 }
3537
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003538 RecordData CUDASpecialDeclRefs;
3539 if (Context.getcudaConfigureCallDecl()) {
3540 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3541 }
3542
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003543 // Build a record containing all of the known namespaces.
3544 RecordData KnownNamespaces;
3545 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3546 I = SemaRef.KnownNamespaces.begin(),
3547 IEnd = SemaRef.KnownNamespaces.end();
3548 I != IEnd; ++I) {
3549 if (!I->second)
3550 AddDeclRef(I->first, KnownNamespaces);
3551 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003552
3553 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003554 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003555
Sebastian Redl3397c552010-08-18 23:56:27 +00003556 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003557 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003558 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003559
3560 // Create a lexical update block containing all of the declarations in the
3561 // translation unit that do not come from other AST files.
3562 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3563 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3564 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3565 E = TU->noload_decls_end();
3566 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003567 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003568 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003569 }
3570
3571 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3572 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3573 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3574 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3575 Record.clear();
3576 Record.push_back(TU_UPDATE_LEXICAL);
3577 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3578 data(NewGlobalDecls));
3579
3580 // And a visible updates block for the translation unit.
3581 Abv = new llvm::BitCodeAbbrev();
3582 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3583 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3584 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3585 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3586 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3587 WriteDeclContextVisibleUpdate(TU);
3588
3589 // If the translation unit has an anonymous namespace, and we don't already
3590 // have an update block for it, write it as an update block.
3591 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3592 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3593 if (Record.empty()) {
3594 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003595 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003596 }
3597 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003598
3599 // Make sure visible decls, added to DeclContexts previously loaded from
3600 // an AST file, are registered for serialization.
3601 for (SmallVector<const Decl *, 16>::iterator
3602 I = UpdatingVisibleDecls.begin(),
3603 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3604 GetDeclRef(*I);
3605 }
3606
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003607 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003608 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003609
Douglas Gregora119da02011-08-02 16:26:37 +00003610 // Form the record of special types.
3611 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003612 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003613 AddTypeRef(Context.getFILEType(), SpecialTypes);
3614 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3615 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3616 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3617 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003618 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003619 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003620
Douglas Gregor366809a2009-04-26 03:49:13 +00003621 // Keep writing types and declarations until all types and
3622 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003623 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003624 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003625 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3626 E = DeclsToRewrite.end();
3627 I != E; ++I)
3628 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003629 while (!DeclTypesToEmit.empty()) {
3630 DeclOrType DOT = DeclTypesToEmit.front();
3631 DeclTypesToEmit.pop();
3632 if (DOT.isType())
3633 WriteType(DOT.getType());
3634 else
3635 WriteDecl(Context, DOT.getDecl());
3636 }
3637 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003638
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003639 DoneWritingDeclsAndTypes = true;
3640
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003641 WriteFileDeclIDsMap();
3642 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003643 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003644
3645 if (Chain) {
3646 // Write the mapping information describing our module dependencies and how
3647 // each of those modules were mapped into our own offset/ID space, so that
3648 // the reader can build the appropriate mapping to its own offset/ID space.
3649 // The map consists solely of a blob with the following format:
3650 // *(module-name-len:i16 module-name:len*i8
3651 // source-location-offset:i32
3652 // identifier-id:i32
3653 // preprocessed-entity-id:i32
3654 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003655 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003656 // selector-id:i32
3657 // declaration-id:i32
3658 // c++-base-specifiers-id:i32
3659 // type-id:i32)
3660 //
3661 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3662 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3664 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003665 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003666 {
3667 llvm::raw_svector_ostream Out(Buffer);
3668 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003669 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003670 M != MEnd; ++M) {
3671 StringRef FileName = (*M)->FileName;
3672 io::Emit16(Out, FileName.size());
3673 Out.write(FileName.data(), FileName.size());
3674 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3675 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003676 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003677 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003678 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003679 io::Emit32(Out, (*M)->BaseSelectorID);
3680 io::Emit32(Out, (*M)->BaseDeclID);
3681 io::Emit32(Out, (*M)->BaseTypeIndex);
3682 }
3683 }
3684 Record.clear();
3685 Record.push_back(MODULE_OFFSET_MAP);
3686 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3687 Buffer.data(), Buffer.size());
3688 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003689 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003690 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003691 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003692 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003693 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003694 WriteFPPragmaOptions(SemaRef.getFPOptions());
3695 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003696
Sebastian Redl1476ed42010-07-16 16:36:56 +00003697 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003698 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003699
Anders Carlssonc8505782011-03-06 18:41:18 +00003700 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003701
Douglas Gregore209e502011-12-06 01:10:29 +00003702 // If we're emitting a module, write out the submodule information.
3703 if (WritingModule)
3704 WriteSubmodules(WritingModule);
3705
Douglas Gregora119da02011-08-02 16:26:37 +00003706 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3707
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003708 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003709 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003710 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003711
3712 // Write the record containing tentative definitions.
3713 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003714 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003715
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003716 // Write the record containing unused file scoped decls.
3717 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003718 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003719
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003720 // Write the record containing weak undeclared identifiers.
3721 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003722 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003723 WeakUndeclaredIdentifiers);
3724
Douglas Gregor14c22f22009-04-22 22:18:58 +00003725 // Write the record containing locally-scoped external definitions.
3726 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003727 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003728 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003729
3730 // Write the record containing ext_vector type names.
3731 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003732 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003733
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003734 // Write the record containing VTable uses information.
3735 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003736 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003737
3738 // Write the record containing dynamic classes declarations.
3739 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003740 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003741
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003742 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003743 if (!PendingInstantiations.empty())
3744 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003745
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003746 // Write the record containing declaration references of Sema.
3747 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003748 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003749
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003750 // Write the record containing CUDA-specific declaration references.
3751 if (!CUDASpecialDeclRefs.empty())
3752 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003753
3754 // Write the delegating constructors.
3755 if (!DelegatingCtorDecls.empty())
3756 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003757
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003758 // Write the known namespaces.
3759 if (!KnownNamespaces.empty())
3760 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3761
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003762 // Write the visible updates to DeclContexts.
3763 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3764 I = UpdatedDeclContexts.begin(),
3765 E = UpdatedDeclContexts.end();
3766 I != E; ++I)
3767 WriteDeclContextVisibleUpdate(*I);
3768
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003769 if (!WritingModule) {
3770 // Write the submodules that were imported, if any.
3771 RecordData ImportedModules;
3772 for (ASTContext::import_iterator I = Context.local_import_begin(),
3773 IEnd = Context.local_import_end();
3774 I != IEnd; ++I) {
3775 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3776 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3777 }
3778 if (!ImportedModules.empty()) {
3779 // Sort module IDs.
3780 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3781
3782 // Unique module IDs.
3783 ImportedModules.erase(std::unique(ImportedModules.begin(),
3784 ImportedModules.end()),
3785 ImportedModules.end());
3786
3787 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3788 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003789 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003790
3791 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003792 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003793 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003794 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003795 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003796 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003797
Douglas Gregor3e1af842009-04-17 22:13:46 +00003798 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003799 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003800 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003801 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003802 Record.push_back(NumLexicalDeclContexts);
3803 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003804 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003805 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003806}
3807
Douglas Gregora8235d62012-10-09 23:05:51 +00003808void ASTWriter::WriteMacroUpdates() {
3809 if (MacroUpdates.empty())
3810 return;
3811
3812 RecordData Record;
3813 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3814 E = MacroUpdates.end();
3815 I != E; ++I) {
3816 addMacroRef(I->first, Record);
3817 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003818 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003819 }
3820 Stream.EmitRecord(MACRO_UPDATES, Record);
3821}
3822
Douglas Gregor61c5e342011-09-17 00:05:03 +00003823/// \brief Go through the declaration update blocks and resolve declaration
3824/// pointers into declaration IDs.
3825void ASTWriter::ResolveDeclUpdatesBlocks() {
3826 for (DeclUpdateMap::iterator
3827 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3828 const Decl *D = I->first;
3829 UpdateRecord &URec = I->second;
3830
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003831 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003832 continue; // The decl will be written completely
3833
3834 unsigned Idx = 0, N = URec.size();
3835 while (Idx < N) {
3836 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003837 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3838 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3839 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3840 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3841 ++Idx;
3842 break;
3843
3844 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3845 ++Idx;
3846 break;
3847 }
3848 }
3849 }
3850}
3851
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003852void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003853 if (DeclUpdates.empty())
3854 return;
3855
3856 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003857 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003858 for (DeclUpdateMap::iterator
3859 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3860 const Decl *D = I->first;
3861 UpdateRecord &URec = I->second;
3862
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003863 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003864 continue; // The decl will be written completely,no need to store updates.
3865
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003866 uint64_t Offset = Stream.GetCurrentBitNo();
3867 Stream.EmitRecord(DECL_UPDATES, URec);
3868
3869 OffsetsRecord.push_back(GetDeclRef(D));
3870 OffsetsRecord.push_back(Offset);
3871 }
3872 Stream.ExitBlock();
3873 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3874}
3875
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003876void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003877 if (ReplacedDecls.empty())
3878 return;
3879
3880 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003881 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003882 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003883 Record.push_back(I->ID);
3884 Record.push_back(I->Offset);
3885 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003886 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003887 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003888}
3889
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003890void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003891 Record.push_back(Loc.getRawEncoding());
3892}
3893
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003894void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003895 AddSourceLocation(Range.getBegin(), Record);
3896 AddSourceLocation(Range.getEnd(), Record);
3897}
3898
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003899void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003900 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003901 const uint64_t *Words = Value.getRawData();
3902 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003903}
3904
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003905void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003906 Record.push_back(Value.isUnsigned());
3907 AddAPInt(Value, Record);
3908}
3909
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003910void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003911 AddAPInt(Value.bitcastToAPInt(), Record);
3912}
3913
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003914void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003915 Record.push_back(getIdentifierRef(II));
3916}
3917
Douglas Gregora8235d62012-10-09 23:05:51 +00003918void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3919 Record.push_back(getMacroRef(MI));
3920}
3921
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003922IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003923 if (II == 0)
3924 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003925
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003926 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003927 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003928 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003929 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003930}
3931
Douglas Gregora8235d62012-10-09 23:05:51 +00003932MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3933 // Don't emit builtin macros like __LINE__ to the AST file unless they
3934 // have been redefined by the header (in which case they are not
3935 // isBuiltinMacro).
3936 if (MI == 0 || MI->isBuiltinMacro())
3937 return 0;
3938
3939 MacroID &ID = MacroIDs[MI];
3940 if (ID == 0)
3941 ID = NextMacroID++;
3942 return ID;
3943}
3944
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003945void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003946 Record.push_back(getSelectorRef(SelRef));
3947}
3948
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003949SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003950 if (Sel.getAsOpaquePtr() == 0) {
3951 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003952 }
3953
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003954 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003955 if (SID == 0 && Chain) {
3956 // This might trigger a ReadSelector callback, which will set the ID for
3957 // this selector.
3958 Chain->LoadSelector(Sel);
3959 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003960 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003961 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003962 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003963 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003964}
3965
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003966void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003967 AddDeclRef(Temp->getDestructor(), Record);
3968}
3969
Douglas Gregor7c789c12010-10-29 22:39:52 +00003970void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3971 CXXBaseSpecifier const *BasesEnd,
3972 RecordDataImpl &Record) {
3973 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3974 CXXBaseSpecifiersToWrite.push_back(
3975 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3976 Bases, BasesEnd));
3977 Record.push_back(NextCXXBaseSpecifiersID++);
3978}
3979
Sebastian Redla4232eb2010-08-18 23:56:21 +00003980void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003981 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003982 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003983 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003984 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003985 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003986 break;
3987 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003988 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003989 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003990 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003991 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003992 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003993 break;
3994 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003995 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003996 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003997 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003998 break;
John McCall833ca992009-10-29 08:12:44 +00003999 case TemplateArgument::Null:
4000 case TemplateArgument::Integral:
4001 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004002 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004003 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004004 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004005 break;
4006 }
4007}
4008
Sebastian Redla4232eb2010-08-18 23:56:21 +00004009void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004010 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004011 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004012
4013 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4014 bool InfoHasSameExpr
4015 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4016 Record.push_back(InfoHasSameExpr);
4017 if (InfoHasSameExpr)
4018 return; // Avoid storing the same expr twice.
4019 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004020 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4021 Record);
4022}
4023
Douglas Gregordc355712011-02-25 00:36:19 +00004024void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4025 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004026 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004027 AddTypeRef(QualType(), Record);
4028 return;
4029 }
4030
Douglas Gregordc355712011-02-25 00:36:19 +00004031 AddTypeLoc(TInfo->getTypeLoc(), Record);
4032}
4033
4034void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4035 AddTypeRef(TL.getType(), Record);
4036
John McCalla1ee0c52009-10-16 21:56:05 +00004037 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004038 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004039 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004040}
4041
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004042void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004043 Record.push_back(GetOrCreateTypeID(T));
4044}
4045
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004046TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4047 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004048 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4049}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004050
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004051TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004052 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004053 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004054}
4055
4056TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4057 if (T.isNull())
4058 return TypeIdx();
4059 assert(!T.getLocalFastQualifiers());
4060
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004061 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004062 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004063 if (DoneWritingDeclsAndTypes) {
4064 assert(0 && "New type seen after serializing all the types to emit!");
4065 return TypeIdx();
4066 }
4067
Douglas Gregor366809a2009-04-26 03:49:13 +00004068 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004069 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004070 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004071 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004072 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004073 return Idx;
4074}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004075
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004076TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004077 if (T.isNull())
4078 return TypeIdx();
4079 assert(!T.getLocalFastQualifiers());
4080
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004081 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4082 assert(I != TypeIdxs.end() && "Type not emitted!");
4083 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004084}
4085
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004086void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004087 Record.push_back(GetDeclRef(D));
4088}
4089
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004090DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004091 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4092
Douglas Gregor2cf26342009-04-09 22:27:44 +00004093 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004094 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004095 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004096
4097 // If D comes from an AST file, its declaration ID is already known and
4098 // fixed.
4099 if (D->isFromASTFile())
4100 return D->getGlobalID();
4101
Douglas Gregor97475832010-10-05 18:37:06 +00004102 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004103 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004104 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004105 if (DoneWritingDeclsAndTypes) {
4106 assert(0 && "New decl seen after serializing all the decls to emit!");
4107 return 0;
4108 }
4109
Douglas Gregor2cf26342009-04-09 22:27:44 +00004110 // We haven't seen this declaration before. Give it a new ID and
4111 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004112 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004113 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004114 }
4115
Sebastian Redl681d7232010-07-27 00:17:23 +00004116 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004117}
4118
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004119DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004120 if (D == 0)
4121 return 0;
4122
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004123 // If D comes from an AST file, its declaration ID is already known and
4124 // fixed.
4125 if (D->isFromASTFile())
4126 return D->getGlobalID();
4127
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004128 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4129 return DeclIDs[D];
4130}
4131
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004132static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4133 std::pair<unsigned, serialization::DeclID> R) {
4134 return L.first < R.first;
4135}
4136
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004137void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004138 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004139 assert(D);
4140
4141 SourceLocation Loc = D->getLocation();
4142 if (Loc.isInvalid())
4143 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004144
4145 // We only keep track of the file-level declarations of each file.
4146 if (!D->getLexicalDeclContext()->isFileContext())
4147 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004148 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4149 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004150 if (isa<ParmVarDecl>(D))
4151 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004152
4153 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004154 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004155 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004156 FileID FID;
4157 unsigned Offset;
4158 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004159 if (FID.isInvalid())
4160 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004161 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004162
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004163 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004164 if (!Info)
4165 Info = new DeclIDInFileInfo();
4166
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004167 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004168 LocDeclIDsTy &Decls = Info->DeclIDs;
4169
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004170 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004171 Decls.push_back(LocDecl);
4172 return;
4173 }
4174
4175 LocDeclIDsTy::iterator
4176 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4177
4178 Decls.insert(I, LocDecl);
4179}
4180
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004181void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004182 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004183 Record.push_back(Name.getNameKind());
4184 switch (Name.getNameKind()) {
4185 case DeclarationName::Identifier:
4186 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4187 break;
4188
4189 case DeclarationName::ObjCZeroArgSelector:
4190 case DeclarationName::ObjCOneArgSelector:
4191 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004192 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004193 break;
4194
4195 case DeclarationName::CXXConstructorName:
4196 case DeclarationName::CXXDestructorName:
4197 case DeclarationName::CXXConversionFunctionName:
4198 AddTypeRef(Name.getCXXNameType(), Record);
4199 break;
4200
4201 case DeclarationName::CXXOperatorName:
4202 Record.push_back(Name.getCXXOverloadedOperator());
4203 break;
4204
Sean Hunt3e518bd2009-11-29 07:34:05 +00004205 case DeclarationName::CXXLiteralOperatorName:
4206 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4207 break;
4208
Douglas Gregor2cf26342009-04-09 22:27:44 +00004209 case DeclarationName::CXXUsingDirective:
4210 // No extra data to emit
4211 break;
4212 }
4213}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004214
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004215void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004216 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004217 switch (Name.getNameKind()) {
4218 case DeclarationName::CXXConstructorName:
4219 case DeclarationName::CXXDestructorName:
4220 case DeclarationName::CXXConversionFunctionName:
4221 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4222 break;
4223
4224 case DeclarationName::CXXOperatorName:
4225 AddSourceLocation(
4226 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4227 Record);
4228 AddSourceLocation(
4229 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4230 Record);
4231 break;
4232
4233 case DeclarationName::CXXLiteralOperatorName:
4234 AddSourceLocation(
4235 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4236 Record);
4237 break;
4238
4239 case DeclarationName::Identifier:
4240 case DeclarationName::ObjCZeroArgSelector:
4241 case DeclarationName::ObjCOneArgSelector:
4242 case DeclarationName::ObjCMultiArgSelector:
4243 case DeclarationName::CXXUsingDirective:
4244 break;
4245 }
4246}
4247
4248void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004249 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004250 AddDeclarationName(NameInfo.getName(), Record);
4251 AddSourceLocation(NameInfo.getLoc(), Record);
4252 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4253}
4254
4255void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004256 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004257 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004258 Record.push_back(Info.NumTemplParamLists);
4259 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4260 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4261}
4262
Sebastian Redla4232eb2010-08-18 23:56:21 +00004263void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004264 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004265 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004266 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004267 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004268
4269 // Push each of the NNS's onto a stack for serialization in reverse order.
4270 while (NNS) {
4271 NestedNames.push_back(NNS);
4272 NNS = NNS->getPrefix();
4273 }
4274
4275 Record.push_back(NestedNames.size());
4276 while(!NestedNames.empty()) {
4277 NNS = NestedNames.pop_back_val();
4278 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4279 Record.push_back(Kind);
4280 switch (Kind) {
4281 case NestedNameSpecifier::Identifier:
4282 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4283 break;
4284
4285 case NestedNameSpecifier::Namespace:
4286 AddDeclRef(NNS->getAsNamespace(), Record);
4287 break;
4288
Douglas Gregor14aba762011-02-24 02:36:08 +00004289 case NestedNameSpecifier::NamespaceAlias:
4290 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4291 break;
4292
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004293 case NestedNameSpecifier::TypeSpec:
4294 case NestedNameSpecifier::TypeSpecWithTemplate:
4295 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4296 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4297 break;
4298
4299 case NestedNameSpecifier::Global:
4300 // Don't need to write an associated value.
4301 break;
4302 }
4303 }
4304}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004305
Douglas Gregordc355712011-02-25 00:36:19 +00004306void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4307 RecordDataImpl &Record) {
4308 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004309 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004310 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004311
4312 // Push each of the nested-name-specifiers's onto a stack for
4313 // serialization in reverse order.
4314 while (NNS) {
4315 NestedNames.push_back(NNS);
4316 NNS = NNS.getPrefix();
4317 }
4318
4319 Record.push_back(NestedNames.size());
4320 while(!NestedNames.empty()) {
4321 NNS = NestedNames.pop_back_val();
4322 NestedNameSpecifier::SpecifierKind Kind
4323 = NNS.getNestedNameSpecifier()->getKind();
4324 Record.push_back(Kind);
4325 switch (Kind) {
4326 case NestedNameSpecifier::Identifier:
4327 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4328 AddSourceRange(NNS.getLocalSourceRange(), Record);
4329 break;
4330
4331 case NestedNameSpecifier::Namespace:
4332 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4333 AddSourceRange(NNS.getLocalSourceRange(), Record);
4334 break;
4335
4336 case NestedNameSpecifier::NamespaceAlias:
4337 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4338 AddSourceRange(NNS.getLocalSourceRange(), Record);
4339 break;
4340
4341 case NestedNameSpecifier::TypeSpec:
4342 case NestedNameSpecifier::TypeSpecWithTemplate:
4343 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4344 AddTypeLoc(NNS.getTypeLoc(), Record);
4345 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4346 break;
4347
4348 case NestedNameSpecifier::Global:
4349 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4350 break;
4351 }
4352 }
4353}
4354
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004355void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004356 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004357 Record.push_back(Kind);
4358 switch (Kind) {
4359 case TemplateName::Template:
4360 AddDeclRef(Name.getAsTemplateDecl(), Record);
4361 break;
4362
4363 case TemplateName::OverloadedTemplate: {
4364 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4365 Record.push_back(OvT->size());
4366 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4367 I != E; ++I)
4368 AddDeclRef(*I, Record);
4369 break;
4370 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004371
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004372 case TemplateName::QualifiedTemplate: {
4373 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4374 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4375 Record.push_back(QualT->hasTemplateKeyword());
4376 AddDeclRef(QualT->getTemplateDecl(), Record);
4377 break;
4378 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004379
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004380 case TemplateName::DependentTemplate: {
4381 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4382 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4383 Record.push_back(DepT->isIdentifier());
4384 if (DepT->isIdentifier())
4385 AddIdentifierRef(DepT->getIdentifier(), Record);
4386 else
4387 Record.push_back(DepT->getOperator());
4388 break;
4389 }
John McCall14606042011-06-30 08:33:18 +00004390
4391 case TemplateName::SubstTemplateTemplateParm: {
4392 SubstTemplateTemplateParmStorage *subst
4393 = Name.getAsSubstTemplateTemplateParm();
4394 AddDeclRef(subst->getParameter(), Record);
4395 AddTemplateName(subst->getReplacement(), Record);
4396 break;
4397 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004398
4399 case TemplateName::SubstTemplateTemplateParmPack: {
4400 SubstTemplateTemplateParmPackStorage *SubstPack
4401 = Name.getAsSubstTemplateTemplateParmPack();
4402 AddDeclRef(SubstPack->getParameterPack(), Record);
4403 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4404 break;
4405 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004406 }
4407}
4408
Michael J. Spencer20249a12010-10-21 03:16:25 +00004409void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004410 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004411 Record.push_back(Arg.getKind());
4412 switch (Arg.getKind()) {
4413 case TemplateArgument::Null:
4414 break;
4415 case TemplateArgument::Type:
4416 AddTypeRef(Arg.getAsType(), Record);
4417 break;
4418 case TemplateArgument::Declaration:
4419 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004420 Record.push_back(Arg.isDeclForReferenceParam());
4421 break;
4422 case TemplateArgument::NullPtr:
4423 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004424 break;
4425 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004426 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004427 AddTypeRef(Arg.getIntegralType(), Record);
4428 break;
4429 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004430 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4431 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004432 case TemplateArgument::TemplateExpansion:
4433 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004434 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4435 Record.push_back(*NumExpansions + 1);
4436 else
4437 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004438 break;
4439 case TemplateArgument::Expression:
4440 AddStmt(Arg.getAsExpr());
4441 break;
4442 case TemplateArgument::Pack:
4443 Record.push_back(Arg.pack_size());
4444 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4445 I != E; ++I)
4446 AddTemplateArgument(*I, Record);
4447 break;
4448 }
4449}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004450
4451void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004452ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004453 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004454 assert(TemplateParams && "No TemplateParams!");
4455 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4456 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4457 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4458 Record.push_back(TemplateParams->size());
4459 for (TemplateParameterList::const_iterator
4460 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4461 P != PEnd; ++P)
4462 AddDeclRef(*P, Record);
4463}
4464
4465/// \brief Emit a template argument list.
4466void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004467ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004468 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004469 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004470 Record.push_back(TemplateArgs->size());
4471 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004472 AddTemplateArgument(TemplateArgs->get(i), Record);
4473}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004474
4475
4476void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004477ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004478 Record.push_back(Set.size());
4479 for (UnresolvedSetImpl::const_iterator
4480 I = Set.begin(), E = Set.end(); I != E; ++I) {
4481 AddDeclRef(I.getDecl(), Record);
4482 Record.push_back(I.getAccess());
4483 }
4484}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004485
Sebastian Redla4232eb2010-08-18 23:56:21 +00004486void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004487 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004488 Record.push_back(Base.isVirtual());
4489 Record.push_back(Base.isBaseOfClass());
4490 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004491 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004492 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004493 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004494 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4495 : SourceLocation(),
4496 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004497}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004498
Douglas Gregor7c789c12010-10-29 22:39:52 +00004499void ASTWriter::FlushCXXBaseSpecifiers() {
4500 RecordData Record;
4501 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4502 Record.clear();
4503
4504 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004505 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004506 if (Index == CXXBaseSpecifiersOffsets.size())
4507 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4508 else {
4509 if (Index > CXXBaseSpecifiersOffsets.size())
4510 CXXBaseSpecifiersOffsets.resize(Index + 1);
4511 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4512 }
4513
4514 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4515 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4516 Record.push_back(BEnd - B);
4517 for (; B != BEnd; ++B)
4518 AddCXXBaseSpecifier(*B, Record);
4519 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004520
4521 // Flush any expressions that were written as part of the base specifiers.
4522 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004523 }
4524
4525 CXXBaseSpecifiersToWrite.clear();
4526}
4527
Sean Huntcbb67482011-01-08 20:30:50 +00004528void ASTWriter::AddCXXCtorInitializers(
4529 const CXXCtorInitializer * const *CtorInitializers,
4530 unsigned NumCtorInitializers,
4531 RecordDataImpl &Record) {
4532 Record.push_back(NumCtorInitializers);
4533 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4534 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004535
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004536 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004537 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004538 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004539 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004540 } else if (Init->isDelegatingInitializer()) {
4541 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004542 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004543 } else if (Init->isMemberInitializer()){
4544 Record.push_back(CTOR_INITIALIZER_MEMBER);
4545 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004546 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004547 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4548 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004549 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004550
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004551 AddSourceLocation(Init->getMemberLocation(), Record);
4552 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004553 AddSourceLocation(Init->getLParenLoc(), Record);
4554 AddSourceLocation(Init->getRParenLoc(), Record);
4555 Record.push_back(Init->isWritten());
4556 if (Init->isWritten()) {
4557 Record.push_back(Init->getSourceOrder());
4558 } else {
4559 Record.push_back(Init->getNumArrayIndices());
4560 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4561 AddDeclRef(Init->getArrayIndex(i), Record);
4562 }
4563 }
4564}
4565
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004566void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4567 assert(D->DefinitionData);
4568 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004569 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004570 Record.push_back(Data.UserDeclaredConstructor);
4571 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004572 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004573 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004574 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004575 Record.push_back(Data.UserDeclaredDestructor);
4576 Record.push_back(Data.Aggregate);
4577 Record.push_back(Data.PlainOldData);
4578 Record.push_back(Data.Empty);
4579 Record.push_back(Data.Polymorphic);
4580 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004581 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004582 Record.push_back(Data.HasNoNonEmptyBases);
4583 Record.push_back(Data.HasPrivateFields);
4584 Record.push_back(Data.HasProtectedFields);
4585 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004586 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004587 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004588 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004589 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004590 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004591 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004592 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004593 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004594 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004595 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004596 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004597 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004598 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004599 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004600 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004601 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004602 Record.push_back(Data.DeclaredDefaultConstructor);
4603 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004604 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004605 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004606 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004607 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004608 Record.push_back(Data.FailedImplicitMoveConstructor);
4609 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004610 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004611
4612 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004613 if (Data.NumBases > 0)
4614 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4615 Record);
4616
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004617 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4618 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004619 if (Data.NumVBases > 0)
4620 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4621 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004622
4623 AddUnresolvedSet(Data.Conversions, Record);
4624 AddUnresolvedSet(Data.VisibleConversions, Record);
4625 // Data.Definition is the owning decl, no need to write it.
4626 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004627
4628 // Add lambda-specific data.
4629 if (Data.IsLambda) {
4630 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004631 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004632 Record.push_back(Lambda.NumCaptures);
4633 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004634 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004635 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004636 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004637 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4638 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4639 AddSourceLocation(Capture.getLocation(), Record);
4640 Record.push_back(Capture.isImplicit());
4641 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4642 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4643 AddDeclRef(Var, Record);
4644 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4645 : SourceLocation(),
4646 Record);
4647 }
4648 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004649}
4650
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004651void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004652 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004653 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004654 assert(FirstDeclID == NextDeclID &&
4655 FirstTypeID == NextTypeID &&
4656 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004657 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004658 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004659 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004660 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004661
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004662 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004663
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004664 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4665 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4666 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004667 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004668 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004669 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004670 NextDeclID = FirstDeclID;
4671 NextTypeID = FirstTypeID;
4672 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004673 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004674 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004675 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004676}
4677
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004678void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004679 IdentifierIDs[II] = ID;
4680}
4681
Douglas Gregora8235d62012-10-09 23:05:51 +00004682void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4683 MacroIDs[MI] = ID;
4684}
4685
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004686void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004687 // Always take the highest-numbered type index. This copes with an interesting
4688 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004689 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004690 // keep the higher-numbered entry so that we can properly write it out to
4691 // the AST file.
4692 TypeIdx &StoredIdx = TypeIdxs[T];
4693 if (Idx.getIndex() >= StoredIdx.getIndex())
4694 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004695}
4696
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004697void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004698 SelectorIDs[S] = ID;
4699}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004700
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004701void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004702 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004703 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004704 MacroDefinitions[MD] = ID;
4705}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004706
Douglas Gregora015cab2011-12-02 17:30:13 +00004707void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4708 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4709 SubmoduleIDs[Mod] = ID;
4710}
4711
Douglas Gregora8235d62012-10-09 23:05:51 +00004712void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4713 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4714}
4715
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004716void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004717 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004718 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004719 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4720 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004721 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004722 // A forward reference was mutated into a definition. Rewrite it.
4723 // FIXME: This happens during template instantiation, should we
4724 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004725 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004726 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004727 }
4728}
Douglas Gregora8235d62012-10-09 23:05:51 +00004729
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004730void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004731 assert(!WritingAST && "Already writing the AST!");
4732
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004733 // TU and namespaces are handled elsewhere.
4734 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4735 return;
4736
Douglas Gregor919814d2011-09-09 23:01:35 +00004737 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004738 return; // Not a source decl added to a DeclContext from PCH.
4739
4740 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004741 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004742}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004743
4744void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004745 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004746 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004747 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004748 return; // Not a source member added to a class from PCH.
4749 if (!isa<CXXMethodDecl>(D))
4750 return; // We are interested in lazily declared implicit methods.
4751
4752 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004753 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004754 UpdateRecord &Record = DeclUpdates[RD];
4755 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004756 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004757}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004758
4759void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4760 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004761 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004762 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004763 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004764 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004765 return; // Not a source specialization added to a template from PCH.
4766
4767 UpdateRecord &Record = DeclUpdates[TD];
4768 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004769 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004770}
Douglas Gregor89d99802010-11-30 06:16:57 +00004771
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004772void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4773 const FunctionDecl *D) {
4774 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004775 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004776 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004777 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004778 return; // Not a source specialization added to a template from PCH.
4779
4780 UpdateRecord &Record = DeclUpdates[TD];
4781 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004782 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004783}
4784
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004785void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004786 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004787 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004788 return; // Declaration not imported from PCH.
4789
4790 // Implicit decl from a PCH was defined.
4791 // FIXME: Should implicit definition be a separate FunctionDecl?
4792 RewriteDecl(D);
4793}
4794
Sebastian Redlf79a7192011-04-29 08:19:30 +00004795void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004796 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004797 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004798 return;
4799
4800 // Since the actual instantiation is delayed, this really means that we need
4801 // to update the instantiation location.
4802 UpdateRecord &Record = DeclUpdates[D];
4803 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4804 AddSourceLocation(
4805 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4806}
4807
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004808void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4809 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004810 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004811 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004812 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004813
4814 assert(IFD->getDefinition() && "Category on a class without a definition?");
4815 ObjCClassesWithCategories.insert(
4816 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004817}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004818
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004819
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004820void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4821 const ObjCPropertyDecl *OrigProp,
4822 const ObjCCategoryDecl *ClassExt) {
4823 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4824 if (!D)
4825 return;
4826
4827 assert(!WritingAST && "Already writing the AST!");
4828 if (!D->isFromASTFile())
4829 return; // Declaration not imported from PCH.
4830
4831 RewriteDecl(D);
4832}