blob: ea100864e3fcaa62670bd098d64697040adfc83b [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 Gregor4fed3f42009-04-27 18:38:38 +0000806 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000807 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000808 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000809 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000810 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000811 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000812 RECORD(SEMA_DECL_REFS);
813 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
814 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
815 RECORD(DECL_REPLACEMENTS);
816 RECORD(UPDATE_VISIBLE);
817 RECORD(DECL_UPDATE_OFFSETS);
818 RECORD(DECL_UPDATES);
819 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
820 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000821 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000822 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000823 RECORD(FP_PRAGMA_OPTIONS);
824 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000825 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000826 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000827 RECORD(MODULE_OFFSET_MAP);
828 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000829 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000830 RECORD(FILE_SORTED_DECLS);
831 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000832 RECORD(MERGED_DECLARATIONS);
833 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000834 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000835 RECORD(MACRO_OFFSET);
836 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000837
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000838 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000839 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 RECORD(SM_SLOC_FILE_ENTRY);
841 RECORD(SM_SLOC_BUFFER_ENTRY);
842 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000843 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000845 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000846 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000847 RECORD(PP_MACRO_OBJECT_LIKE);
848 RECORD(PP_MACRO_FUNCTION_LIKE);
849 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000850
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000851 // Decls and Types block.
852 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000853 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000854 RECORD(TYPE_COMPLEX);
855 RECORD(TYPE_POINTER);
856 RECORD(TYPE_BLOCK_POINTER);
857 RECORD(TYPE_LVALUE_REFERENCE);
858 RECORD(TYPE_RVALUE_REFERENCE);
859 RECORD(TYPE_MEMBER_POINTER);
860 RECORD(TYPE_CONSTANT_ARRAY);
861 RECORD(TYPE_INCOMPLETE_ARRAY);
862 RECORD(TYPE_VARIABLE_ARRAY);
863 RECORD(TYPE_VECTOR);
864 RECORD(TYPE_EXT_VECTOR);
865 RECORD(TYPE_FUNCTION_PROTO);
866 RECORD(TYPE_FUNCTION_NO_PROTO);
867 RECORD(TYPE_TYPEDEF);
868 RECORD(TYPE_TYPEOF_EXPR);
869 RECORD(TYPE_TYPEOF);
870 RECORD(TYPE_RECORD);
871 RECORD(TYPE_ENUM);
872 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000873 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000874 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000875 RECORD(TYPE_DECLTYPE);
876 RECORD(TYPE_ELABORATED);
877 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
878 RECORD(TYPE_UNRESOLVED_USING);
879 RECORD(TYPE_INJECTED_CLASS_NAME);
880 RECORD(TYPE_OBJC_OBJECT);
881 RECORD(TYPE_TEMPLATE_TYPE_PARM);
882 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
883 RECORD(TYPE_DEPENDENT_NAME);
884 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
885 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
886 RECORD(TYPE_PAREN);
887 RECORD(TYPE_PACK_EXPANSION);
888 RECORD(TYPE_ATTRIBUTED);
889 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000890 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000891 RECORD(DECL_TYPEDEF);
892 RECORD(DECL_ENUM);
893 RECORD(DECL_RECORD);
894 RECORD(DECL_ENUM_CONSTANT);
895 RECORD(DECL_FUNCTION);
896 RECORD(DECL_OBJC_METHOD);
897 RECORD(DECL_OBJC_INTERFACE);
898 RECORD(DECL_OBJC_PROTOCOL);
899 RECORD(DECL_OBJC_IVAR);
900 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000901 RECORD(DECL_OBJC_CATEGORY);
902 RECORD(DECL_OBJC_CATEGORY_IMPL);
903 RECORD(DECL_OBJC_IMPLEMENTATION);
904 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
905 RECORD(DECL_OBJC_PROPERTY);
906 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000907 RECORD(DECL_FIELD);
908 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000909 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000910 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000911 RECORD(DECL_FILE_SCOPE_ASM);
912 RECORD(DECL_BLOCK);
913 RECORD(DECL_CONTEXT_LEXICAL);
914 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000915 RECORD(DECL_NAMESPACE);
916 RECORD(DECL_NAMESPACE_ALIAS);
917 RECORD(DECL_USING);
918 RECORD(DECL_USING_SHADOW);
919 RECORD(DECL_USING_DIRECTIVE);
920 RECORD(DECL_UNRESOLVED_USING_VALUE);
921 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
922 RECORD(DECL_LINKAGE_SPEC);
923 RECORD(DECL_CXX_RECORD);
924 RECORD(DECL_CXX_METHOD);
925 RECORD(DECL_CXX_CONSTRUCTOR);
926 RECORD(DECL_CXX_DESTRUCTOR);
927 RECORD(DECL_CXX_CONVERSION);
928 RECORD(DECL_ACCESS_SPEC);
929 RECORD(DECL_FRIEND);
930 RECORD(DECL_FRIEND_TEMPLATE);
931 RECORD(DECL_CLASS_TEMPLATE);
932 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
933 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
934 RECORD(DECL_FUNCTION_TEMPLATE);
935 RECORD(DECL_TEMPLATE_TYPE_PARM);
936 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
937 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
938 RECORD(DECL_STATIC_ASSERT);
939 RECORD(DECL_CXX_BASE_SPECIFIERS);
940 RECORD(DECL_INDIRECTFIELD);
941 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
942
Douglas Gregora72d8c42011-06-03 02:27:19 +0000943 // Statements and Exprs can occur in the Decls and Types block.
944 AddStmtsExprs(Stream, Record);
945
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000946 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000947 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000948 RECORD(PPD_MACRO_DEFINITION);
949 RECORD(PPD_INCLUSION_DIRECTIVE);
950
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000951#undef RECORD
952#undef BLOCK
953 Stream.ExitBlock();
954}
955
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956/// \brief Adjusts the given filename to only write out the portion of the
957/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000958///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959/// \param Filename the file name to adjust.
960///
961/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
962/// the returned filename will be adjusted by this system root.
963///
964/// \returns either the original filename (if it needs no adjustment) or the
965/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000966static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000967adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Douglas Gregor832d6202011-07-22 16:35:34 +0000970 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 // Verify that the filename and the system root have the same prefix.
974 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000975 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000976 if (Filename[Pos] != isysroot[Pos])
977 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregore650c8c2009-07-07 00:12:59 +0000979 // We hit the end of the filename before we hit the end of the system root.
980 if (!Filename[Pos])
981 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Douglas Gregore650c8c2009-07-07 00:12:59 +0000983 // If the file name has a '/' at the current position, skip over the '/'.
984 // We distinguish sysroot-based includes from absolute includes by the
985 // absence of '/' at the beginning of sysroot-based includes.
986 if (Filename[Pos] == '/')
987 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregore650c8c2009-07-07 00:12:59 +0000989 return Filename + Pos;
990}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000991
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000992/// \brief Write the control block.
Douglas Gregorbbf38312012-10-24 16:50:34 +0000993void ASTWriter::WriteControlBlock(Preprocessor &PP, ASTContext &Context,
994 StringRef isysroot,
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000995 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000996 using namespace llvm;
Douglas Gregor7ae467f2012-10-18 18:27:37 +0000997 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
998 RecordData Record;
Douglas Gregor1d9d9892012-10-18 05:31:06 +0000999
Douglas Gregore650c8c2009-07-07 00:12:59 +00001000 // Metadata
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001001 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
1002 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1003 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1004 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1005 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1006 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1007 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1008 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1009 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1010 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1011 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001012 Record.push_back(VERSION_MAJOR);
1013 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001014 Record.push_back(CLANG_VERSION_MAJOR);
1015 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001016 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001017 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001018 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1019 getClangFullRepositoryVersion());
Douglas Gregore95b9192011-08-17 21:07:30 +00001020
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001021 // Imports
Douglas Gregore95b9192011-08-17 21:07:30 +00001022 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001023 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1024 llvm::SmallVector<char, 128> ModulePaths;
1025 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001026
1027 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1028 M != MEnd; ++M) {
1029 // Skip modules that weren't directly imported.
1030 if (!(*M)->isDirectlyImported())
1031 continue;
1032
1033 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1034 // FIXME: Write import location, once it matters.
1035 // FIXME: This writes the absolute path for AST files we depend on.
1036 const std::string &FileName = (*M)->FileName;
1037 Record.push_back(FileName.size());
1038 Record.append(FileName.begin(), FileName.end());
1039 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001040 Stream.EmitRecord(IMPORTS, Record);
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001043 // Language options.
1044 Record.clear();
1045 const LangOptions &LangOpts = Context.getLangOpts();
1046#define LANGOPT(Name, Bits, Default, Description) \
1047 Record.push_back(LangOpts.Name);
1048#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1049 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1050#include "clang/Basic/LangOptions.def"
1051
1052 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1053 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1054
1055 Record.push_back(LangOpts.CurrentModule.size());
1056 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1057 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1058
Douglas Gregoree097c12012-10-18 17:58:09 +00001059 // Target options.
1060 Record.clear();
Douglas Gregor7ae467f2012-10-18 18:27:37 +00001061 const TargetInfo &Target = Context.getTargetInfo();
1062 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregoree097c12012-10-18 17:58:09 +00001063 AddString(TargetOpts.Triple, Record);
1064 AddString(TargetOpts.CPU, Record);
1065 AddString(TargetOpts.ABI, Record);
1066 AddString(TargetOpts.CXXABI, Record);
1067 AddString(TargetOpts.LinkerVersion, Record);
1068 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1069 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1070 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1071 }
1072 Record.push_back(TargetOpts.Features.size());
1073 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1074 AddString(TargetOpts.Features[I], Record);
1075 }
1076 Stream.EmitRecord(TARGET_OPTIONS, Record);
1077
Douglas Gregor5f3d8222012-10-24 15:17:15 +00001078 // Diagnostic options.
1079 Record.clear();
1080 const DiagnosticOptions &DiagOpts
1081 = Context.getDiagnostics().getDiagnosticOptions();
1082#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1083#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1084 Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1085#include "clang/Basic/DiagnosticOptions.def"
1086 Record.push_back(DiagOpts.Warnings.size());
1087 for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1088 AddString(DiagOpts.Warnings[I], Record);
1089 // Note: we don't serialize the log or serialization file names, because they
1090 // are generally transient files and will almost always be overridden.
1091 Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1092
Douglas Gregor1b2c3c02012-10-24 15:49:58 +00001093 // File system options.
1094 Record.clear();
1095 const FileSystemOptions &FSOpts
1096 = Context.getSourceManager().getFileManager().getFileSystemOptions();
1097 AddString(FSOpts.WorkingDir, Record);
1098 Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1099
Douglas Gregorbbf38312012-10-24 16:50:34 +00001100 // Header search options.
1101 Record.clear();
1102 const HeaderSearchOptions &HSOpts
1103 = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1104 AddString(HSOpts.Sysroot, Record);
1105
1106 // Include entries.
1107 Record.push_back(HSOpts.UserEntries.size());
1108 for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1109 const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1110 AddString(Entry.Path, Record);
1111 Record.push_back(static_cast<unsigned>(Entry.Group));
1112 Record.push_back(Entry.IsUserSupplied);
1113 Record.push_back(Entry.IsFramework);
1114 Record.push_back(Entry.IgnoreSysRoot);
1115 Record.push_back(Entry.IsInternal);
1116 Record.push_back(Entry.ImplicitExternC);
1117 }
1118
1119 // System header prefixes.
1120 Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1121 for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1122 AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1123 Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1124 }
1125
1126 AddString(HSOpts.ResourceDir, Record);
1127 AddString(HSOpts.ModuleCachePath, Record);
1128 Record.push_back(HSOpts.DisableModuleHash);
1129 Record.push_back(HSOpts.UseBuiltinIncludes);
1130 Record.push_back(HSOpts.UseStandardSystemIncludes);
1131 Record.push_back(HSOpts.UseStandardCXXIncludes);
1132 Record.push_back(HSOpts.UseLibcxx);
1133 Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1134
Douglas Gregora71a7d82012-10-24 20:05:57 +00001135 // Preprocessor options.
1136 Record.clear();
1137 const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1138
1139 // Macro definitions.
1140 Record.push_back(PPOpts.Macros.size());
1141 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1142 AddString(PPOpts.Macros[I].first, Record);
1143 Record.push_back(PPOpts.Macros[I].second);
1144 }
1145
1146 // Includes
1147 Record.push_back(PPOpts.Includes.size());
1148 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1149 AddString(PPOpts.Includes[I], Record);
1150
1151 // Macro includes
1152 Record.push_back(PPOpts.MacroIncludes.size());
1153 for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1154 AddString(PPOpts.MacroIncludes[I], Record);
1155
Douglas Gregor4c0c7e82012-10-24 23:41:50 +00001156 Record.push_back(PPOpts.UsePredefines);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001157 AddString(PPOpts.ImplicitPCHInclude, Record);
1158 AddString(PPOpts.ImplicitPTHInclude, Record);
1159 Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1160 Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1161
Douglas Gregor31d375f2011-05-06 21:43:30 +00001162 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001163 SourceManager &SM = Context.getSourceManager();
1164 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1165 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001166 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1167 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001168 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1169 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1170
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001171 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001173 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001174
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001175 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001176 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001177 isysroot);
Douglas Gregora71a7d82012-10-24 20:05:57 +00001178 Record.clear();
Douglas Gregor39c497b2012-10-18 18:36:53 +00001179 Record.push_back(ORIGINAL_FILE);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001180 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregor39c497b2012-10-18 18:36:53 +00001181 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001182 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001183
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001184 // Original PCH directory
1185 if (!OutputFile.empty() && OutputFile != "-") {
1186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1187 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1189 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1190
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001191 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001192
1193 llvm::sys::fs::make_absolute(OutputPath);
1194 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1195
1196 RecordData Record;
1197 Record.push_back(ORIGINAL_PCH_DIR);
1198 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1199 }
1200
Douglas Gregor745e6f12012-10-19 00:38:02 +00001201 WriteInputFiles(Context.SourceMgr, isysroot);
1202 Stream.ExitBlock();
1203}
1204
1205void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1206 using namespace llvm;
1207 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1208 RecordData Record;
1209
1210 // Create input-file abbreviation.
1211 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1212 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
Douglas Gregora930dc92012-10-22 18:42:04 +00001213 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor745e6f12012-10-19 00:38:02 +00001214 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1215 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora930dc92012-10-22 18:42:04 +00001216 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
Douglas Gregor745e6f12012-10-19 00:38:02 +00001217 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1218 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1219
1220 // Write out all of the input files.
1221 std::vector<uint32_t> InputFileOffsets;
1222 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1223 // Get this source location entry.
1224 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
NAKAMURA Takumibacc2c52012-10-19 01:53:57 +00001225 assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
Douglas Gregor745e6f12012-10-19 00:38:02 +00001226
1227 // We only care about file entries that were not overridden.
1228 if (!SLoc->isFile())
1229 continue;
1230 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
Douglas Gregora930dc92012-10-22 18:42:04 +00001231 if (!Cache->OrigEntry)
Douglas Gregor745e6f12012-10-19 00:38:02 +00001232 continue;
1233
Douglas Gregora930dc92012-10-22 18:42:04 +00001234 // Record this entry's offset.
1235 InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1236 InputFileIDs[Cache->OrigEntry] = InputFileOffsets.size();
1237
Douglas Gregor745e6f12012-10-19 00:38:02 +00001238 Record.clear();
1239 Record.push_back(INPUT_FILE);
Douglas Gregora930dc92012-10-22 18:42:04 +00001240 Record.push_back(InputFileOffsets.size());
Douglas Gregor745e6f12012-10-19 00:38:02 +00001241
1242 // Emit size/modification time for this file.
1243 Record.push_back(Cache->OrigEntry->getSize());
1244 Record.push_back(Cache->OrigEntry->getModificationTime());
1245
Douglas Gregora930dc92012-10-22 18:42:04 +00001246 // Whether this file was overridden.
1247 Record.push_back(Cache->BufferOverridden);
1248
Douglas Gregor745e6f12012-10-19 00:38:02 +00001249 // Turn the file name into an absolute path, if it isn't already.
1250 const char *Filename = Cache->OrigEntry->getName();
1251 SmallString<128> FilePath(Filename);
1252
1253 // Ask the file manager to fixup the relative path for us. This will
1254 // honor the working directory.
1255 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1256
1257 // FIXME: This call to make_absolute shouldn't be necessary, the
1258 // call to FixupRelativePath should always return an absolute path.
1259 llvm::sys::fs::make_absolute(FilePath);
1260 Filename = FilePath.c_str();
1261
1262 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1263
1264 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1265 }
1266
Douglas Gregor1d9d9892012-10-18 05:31:06 +00001267 Stream.ExitBlock();
Douglas Gregora930dc92012-10-22 18:42:04 +00001268
1269 // Create input file offsets abbreviation.
1270 BitCodeAbbrev *OffsetsAbbrev = new BitCodeAbbrev();
1271 OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1272 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1273 OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Array
1274 unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1275
1276 // Write input file offsets.
1277 Record.clear();
1278 Record.push_back(INPUT_FILE_OFFSETS);
1279 Record.push_back(InputFileOffsets.size());
1280 Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, data(InputFileOffsets));
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001281}
1282
Douglas Gregor14f79002009-04-10 03:52:48 +00001283//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001284// stat cache Serialization
1285//===----------------------------------------------------------------------===//
1286
1287namespace {
1288// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001289class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001290public:
1291 typedef const char * key_type;
1292 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Chris Lattner74e976b2010-11-23 19:28:12 +00001294 typedef struct stat data_type;
1295 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001296
1297 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001298 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
1301 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001302 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001303 data_type_ref Data) {
1304 unsigned StrLen = strlen(path);
1305 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001306 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001307 clang::io::Emit8(Out, DataLen);
1308 return std::make_pair(StrLen + 1, DataLen);
1309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Chris Lattner5f9e2722011-07-23 10:55:15 +00001311 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001312 Out.write(path, KeyLen);
1313 }
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Chris Lattner5f9e2722011-07-23 10:55:15 +00001315 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001316 data_type_ref Data, unsigned DataLen) {
1317 using namespace clang::io;
1318 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Chris Lattner74e976b2010-11-23 19:28:12 +00001320 Emit32(Out, (uint32_t) Data.st_ino);
1321 Emit32(Out, (uint32_t) Data.st_dev);
1322 Emit16(Out, (uint16_t) Data.st_mode);
1323 Emit64(Out, (uint64_t) Data.st_mtime);
1324 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001325
1326 assert(Out.tell() - Start == DataLen && "Wrong data length");
1327 }
1328};
1329} // end anonymous namespace
1330
Sebastian Redl3397c552010-08-18 23:56:27 +00001331/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001332void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001333 // Build the on-disk hash table containing information about every
1334 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001335 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001336 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001337 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001338 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001339 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001340 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001341 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001344 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001345 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001346 uint32_t BucketOffset;
1347 {
1348 llvm::raw_svector_ostream Out(StatCacheData);
1349 // Make sure that no bucket is at offset 0
1350 clang::io::Emit32(Out, 0);
1351 BucketOffset = Generator.Emit(Out);
1352 }
1353
1354 // Create a blob abbreviation
1355 using namespace llvm;
1356 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001357 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001358 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1361 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1362
1363 // Write the stat cache
1364 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001365 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001366 Record.push_back(BucketOffset);
1367 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001368 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001369}
1370
1371//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001372// Source Manager Serialization
1373//===----------------------------------------------------------------------===//
1374
1375/// \brief Create an abbreviation for the SLocEntry that refers to a
1376/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001377static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001378 using namespace llvm;
1379 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001380 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001385 // FileEntry fields.
Douglas Gregora930dc92012-10-22 18:42:04 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregorc9490c02009-04-16 22:23:12 +00001390 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001391}
1392
1393/// \brief Create an abbreviation for the SLocEntry that refers to a
1394/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001395static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001396 using namespace llvm;
1397 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001398 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001399 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1400 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1401 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1402 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001404 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001405}
1406
1407/// \brief Create an abbreviation for the SLocEntry that refers to a
1408/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001409static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001410 using namespace llvm;
1411 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001412 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001414 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001415}
1416
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001417/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1418/// expansion.
1419static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001420 using namespace llvm;
1421 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001422 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1426 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001428 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001429}
1430
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001431namespace {
1432 // Trait used for the on-disk hash table of header search information.
1433 class HeaderFileInfoTrait {
1434 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001435
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001436 // Keep track of the framework names we've used during serialization.
1437 SmallVector<char, 128> FrameworkStringData;
1438 llvm::StringMap<unsigned> FrameworkNameOffset;
1439
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001440 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001441 HeaderFileInfoTrait(ASTWriter &Writer)
1442 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001443
1444 typedef const char *key_type;
1445 typedef key_type key_type_ref;
1446
1447 typedef HeaderFileInfo data_type;
1448 typedef const data_type &data_type_ref;
1449
1450 static unsigned ComputeHash(const char *path) {
1451 // The hash is based only on the filename portion of the key, so that the
1452 // reader can match based on filenames when symlinking or excess path
1453 // elements ("foo/../", "../") change the form of the name. However,
1454 // complete path is still the key.
1455 return llvm::HashString(llvm::sys::path::filename(path));
1456 }
1457
1458 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001459 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001460 data_type_ref Data) {
1461 unsigned StrLen = strlen(path);
1462 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001463 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001464 clang::io::Emit8(Out, DataLen);
1465 return std::make_pair(StrLen + 1, DataLen);
1466 }
1467
Chris Lattner5f9e2722011-07-23 10:55:15 +00001468 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001469 Out.write(path, KeyLen);
1470 }
1471
Chris Lattner5f9e2722011-07-23 10:55:15 +00001472 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001473 data_type_ref Data, unsigned DataLen) {
1474 using namespace clang::io;
1475 uint64_t Start = Out.tell(); (void)Start;
1476
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001477 unsigned char Flags = (Data.isImport << 5)
1478 | (Data.isPragmaOnce << 4)
1479 | (Data.DirInfo << 2)
1480 | (Data.Resolved << 1)
1481 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001482 Emit8(Out, (uint8_t)Flags);
1483 Emit16(Out, (uint16_t) Data.NumIncludes);
1484
1485 if (!Data.ControllingMacro)
1486 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1487 else
1488 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001489
1490 unsigned Offset = 0;
1491 if (!Data.Framework.empty()) {
1492 // If this header refers into a framework, save the framework name.
1493 llvm::StringMap<unsigned>::iterator Pos
1494 = FrameworkNameOffset.find(Data.Framework);
1495 if (Pos == FrameworkNameOffset.end()) {
1496 Offset = FrameworkStringData.size() + 1;
1497 FrameworkStringData.append(Data.Framework.begin(),
1498 Data.Framework.end());
1499 FrameworkStringData.push_back(0);
1500
1501 FrameworkNameOffset[Data.Framework] = Offset;
1502 } else
1503 Offset = Pos->second;
1504 }
1505 Emit32(Out, Offset);
1506
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001507 assert(Out.tell() - Start == DataLen && "Wrong data length");
1508 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001509
1510 const char *strings_begin() const { return FrameworkStringData.begin(); }
1511 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001512 };
1513} // end anonymous namespace
1514
1515/// \brief Write the header search block for the list of files that
1516///
1517/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001518void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001519 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001520 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1521
1522 if (FilesByUID.size() > HS.header_file_size())
1523 FilesByUID.resize(HS.header_file_size());
1524
Benjamin Kramerfacde172012-06-06 17:32:50 +00001525 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001526 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001527 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001528 unsigned NumHeaderSearchEntries = 0;
1529 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1530 const FileEntry *File = FilesByUID[UID];
1531 if (!File)
1532 continue;
1533
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001534 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1535 // from the external source if it was not provided already.
1536 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001537 if (HFI.External && Chain)
1538 continue;
1539
1540 // Turn the file name into an absolute path, if it isn't already.
1541 const char *Filename = File->getName();
1542 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1543
1544 // If we performed any translation on the file name at all, we need to
1545 // save this string, since the generator will refer to it later.
1546 if (Filename != File->getName()) {
1547 Filename = strdup(Filename);
1548 SavedStrings.push_back(Filename);
1549 }
1550
1551 Generator.insert(Filename, HFI, GeneratorTrait);
1552 ++NumHeaderSearchEntries;
1553 }
1554
1555 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001556 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001557 uint32_t BucketOffset;
1558 {
1559 llvm::raw_svector_ostream Out(TableData);
1560 // Make sure that no bucket is at offset 0
1561 clang::io::Emit32(Out, 0);
1562 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1563 }
1564
1565 // Create a blob abbreviation
1566 using namespace llvm;
1567 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1568 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1573 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1574
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001575 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001576 RecordData Record;
1577 Record.push_back(HEADER_SEARCH_TABLE);
1578 Record.push_back(BucketOffset);
1579 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001580 Record.push_back(TableData.size());
1581 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001582 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1583
1584 // Free all of the strings we had to duplicate.
1585 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1586 free((void*)SavedStrings[I]);
1587}
1588
Douglas Gregor14f79002009-04-10 03:52:48 +00001589/// \brief Writes the block containing the serialized form of the
1590/// source manager.
1591///
1592/// TODO: We should probably use an on-disk hash table (stored in a
1593/// blob), indexed based on the file name, so that we only create
1594/// entries for files that we actually need. In the common case (no
1595/// errors), we probably won't have to create file entries for any of
1596/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001597void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001598 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001599 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001600 RecordData Record;
1601
Chris Lattnerf04ad692009-04-10 17:16:57 +00001602 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001603 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001604
1605 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001606 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1607 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1608 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001609 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001610
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001611 // Write out the source location entry table. We skip the first
1612 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001613 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001614 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001615 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1616 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001617 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001618 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001619 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001620 FileID FID = FileID::get(I);
1621 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001622
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001623 // Record the offset of this source-location entry.
1624 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1625
1626 // Figure out which record code to use.
1627 unsigned Code;
1628 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001629 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1630 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001631 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001632 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001633 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001634 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001635 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001636 Record.clear();
1637 Record.push_back(Code);
1638
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001639 // Starting offset of this entry within this module, so skip the dummy.
1640 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001641 if (SLoc->isFile()) {
1642 const SrcMgr::FileInfo &File = SLoc->getFile();
1643 Record.push_back(File.getIncludeLoc().getRawEncoding());
1644 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1645 Record.push_back(File.hasLineDirectives());
1646
1647 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001648 if (Content->OrigEntry) {
1649 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001650 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001651
Douglas Gregora930dc92012-10-22 18:42:04 +00001652 // The source location entry is a file. Emit input file ID.
1653 assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1654 Record.push_back(InputFileIDs[Content->OrigEntry]);
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001656 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001657
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001658 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001659 if (FDI != FileDeclIDs.end()) {
1660 Record.push_back(FDI->second->FirstDeclIndex);
1661 Record.push_back(FDI->second->DeclIDs.size());
1662 } else {
1663 Record.push_back(0);
1664 Record.push_back(0);
1665 }
Douglas Gregora081da52011-11-16 20:05:18 +00001666
Douglas Gregora930dc92012-10-22 18:42:04 +00001667 Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
Douglas Gregora081da52011-11-16 20:05:18 +00001668
1669 if (Content->BufferOverridden) {
1670 Record.clear();
1671 Record.push_back(SM_SLOC_BUFFER_BLOB);
1672 const llvm::MemoryBuffer *Buffer
1673 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1674 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1675 StringRef(Buffer->getBufferStart(),
1676 Buffer->getBufferSize() + 1));
1677 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001678 } else {
1679 // The source location entry is a buffer. The blob associated
1680 // with this entry contains the contents of the buffer.
1681
1682 // We add one to the size so that we capture the trailing NULL
1683 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1684 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001685 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001686 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001687 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001688 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001689 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001690 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001691 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001692 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001693 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001694 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001695
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001696 if (strcmp(Name, "<built-in>") == 0) {
1697 PreloadSLocs.push_back(SLocEntryOffsets.size());
1698 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001699 }
1700 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001701 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001702 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001703 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1704 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001705 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1706 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001707
1708 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001709 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001710 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001711 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001712 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001713 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001714 }
1715 }
1716
Douglas Gregorc9490c02009-04-16 22:23:12 +00001717 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001718
1719 if (SLocEntryOffsets.empty())
1720 return;
1721
Sebastian Redl3397c552010-08-18 23:56:27 +00001722 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001723 // table is used for lazily loading source-location information.
1724 using namespace llvm;
1725 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001726 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1730 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001731
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001732 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001733 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001734 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001735 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001736 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001737
Sebastian Redl3397c552010-08-18 23:56:27 +00001738 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001739 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001740 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001741
1742 // Write the line table. It depends on remapping working, so it must come
1743 // after the source location offsets.
1744 if (SourceMgr.hasLineTable()) {
1745 LineTableInfo &LineTable = SourceMgr.getLineTable();
1746
1747 Record.clear();
1748 // Emit the file names
1749 Record.push_back(LineTable.getNumFilenames());
1750 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1751 // Emit the file name
1752 const char *Filename = LineTable.getFilename(I);
1753 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1754 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1755 Record.push_back(FilenameLen);
1756 if (FilenameLen)
1757 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1758 }
1759
1760 // Emit the line entries
1761 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1762 L != LEnd; ++L) {
1763 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001764 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001765 continue;
1766
1767 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001768 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001769
1770 // Emit the line entries
1771 Record.push_back(L->second.size());
1772 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1773 LEEnd = L->second.end();
1774 LE != LEEnd; ++LE) {
1775 Record.push_back(LE->FileOffset);
1776 Record.push_back(LE->LineNo);
1777 Record.push_back(LE->FilenameID);
1778 Record.push_back((unsigned)LE->FileKind);
1779 Record.push_back(LE->IncludeOffset);
1780 }
1781 }
1782 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1783 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001784}
1785
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001786//===----------------------------------------------------------------------===//
1787// Preprocessor Serialization
1788//===----------------------------------------------------------------------===//
1789
Douglas Gregor9c736102011-02-10 18:20:09 +00001790static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1791 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1792 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1793 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1794 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1795 return X.first->getName().compare(Y.first->getName());
1796}
1797
Chris Lattner0b1fb982009-04-10 17:15:23 +00001798/// \brief Writes the block containing the serialized form of the
1799/// preprocessor.
1800///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001801void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001802 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1803 if (PPRec)
1804 WritePreprocessorDetail(*PPRec);
1805
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001806 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001807
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001808 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1809 if (PP.getCounterValue() != 0) {
1810 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001811 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001812 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001813 }
1814
1815 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001816 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Sebastian Redl3397c552010-08-18 23:56:27 +00001818 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001819 // FIXME: use diagnostics subsystem for localization etc.
1820 if (PP.SawDateOrTime())
1821 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Douglas Gregorecdcb882010-10-20 22:00:55 +00001823
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001824 // Loop over all the macro definitions that are live at the end of the file,
1825 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001826
Douglas Gregor9c736102011-02-10 18:20:09 +00001827 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001828 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001829 MacrosToEmit;
1830 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001831 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001832 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001833 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001834 if (!IsModule || I->second->isPublic()) {
1835 MacroDefinitionsSeen.insert(I->first);
1836 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001837 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001838 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001839
Douglas Gregor9c736102011-02-10 18:20:09 +00001840 // Sort the set of macro definitions that need to be serialized by the
1841 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001842 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001843 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001844
Douglas Gregora8235d62012-10-09 23:05:51 +00001845 /// \brief Offsets of each of the macros into the bitstream, indexed by
1846 /// the local macro ID
1847 ///
1848 /// For each identifier that is associated with a macro, this map
1849 /// provides the offset into the bitstream where that macro is
1850 /// defined.
1851 std::vector<uint32_t> MacroOffsets;
1852
Douglas Gregor9c736102011-02-10 18:20:09 +00001853 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1854 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001855
Douglas Gregora8235d62012-10-09 23:05:51 +00001856 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1857 MI = MI->getPreviousDefinition()) {
1858 MacroID ID = getMacroRef(MI);
1859 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001860 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001861
Douglas Gregora8235d62012-10-09 23:05:51 +00001862 // Skip macros from a AST file if we're chaining.
1863 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1864 continue;
1865
1866 if (ID < FirstMacroID) {
1867 // This will have been dealt with via an update record.
1868 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1869 continue;
1870 }
1871
1872 // Record the local offset of this macro.
1873 unsigned Index = ID - FirstMacroID;
1874 if (Index == MacroOffsets.size())
1875 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1876 else {
1877 if (Index > MacroOffsets.size())
1878 MacroOffsets.resize(Index + 1);
1879
1880 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1881 }
1882
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001883 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001884 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001885 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001886 AddSourceLocation(MI->getDefinitionLoc(), Record);
1887 AddSourceLocation(MI->getUndefLoc(), Record);
1888 Record.push_back(MI->isUsed());
1889 Record.push_back(MI->isPublic());
1890 AddSourceLocation(MI->getVisibilityLocation(), Record);
1891 unsigned Code;
1892 if (MI->isObjectLike()) {
1893 Code = PP_MACRO_OBJECT_LIKE;
1894 } else {
1895 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001896
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001897 Record.push_back(MI->isC99Varargs());
1898 Record.push_back(MI->isGNUVarargs());
1899 Record.push_back(MI->getNumArgs());
1900 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1901 I != E; ++I)
1902 AddIdentifierRef(*I, Record);
1903 }
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001905 // If we have a detailed preprocessing record, record the macro definition
1906 // ID that corresponds to this macro.
1907 if (PPRec)
1908 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1909
1910 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001911 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001912
1913 // Emit the tokens array.
1914 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1915 // Note that we know that the preprocessor does not have any annotation
1916 // tokens in it because they are created by the parser, and thus can't
1917 // be in a macro definition.
1918 const Token &Tok = MI->getReplacementToken(TokNo);
1919
1920 Record.push_back(Tok.getLocation().getRawEncoding());
1921 Record.push_back(Tok.getLength());
1922
1923 // FIXME: When reading literal tokens, reconstruct the literal pointer
1924 // if it is needed.
1925 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1926 // FIXME: Should translate token kind to a stable encoding.
1927 Record.push_back(Tok.getKind());
1928 // FIXME: Should translate token flags to a stable encoding.
1929 Record.push_back(Tok.getFlags());
1930
1931 Stream.EmitRecord(PP_TOKEN, Record);
1932 Record.clear();
1933 }
1934 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001935 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001936 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001937 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001938
1939 // Write the offsets table for macro IDs.
1940 using namespace llvm;
1941 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1942 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1945 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1946
1947 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1948 Record.clear();
1949 Record.push_back(MACRO_OFFSET);
1950 Record.push_back(MacroOffsets.size());
1951 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1952 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1953 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001954}
1955
1956void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001957 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001958 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001959
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001960 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001961
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001962 // Enter the preprocessor block.
1963 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001964
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001965 // If the preprocessor has a preprocessing record, emit it.
1966 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001967 using namespace llvm;
1968
1969 // Set up the abbreviation for
1970 unsigned InclusionAbbrev = 0;
1971 {
1972 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1973 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1979 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1980 }
1981
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001982 unsigned FirstPreprocessorEntityID
1983 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1984 + NUM_PREDEF_PP_ENTITY_IDS;
1985 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001986 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001987 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1988 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001989 E != EEnd;
1990 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001991 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001992
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001993 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1994 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001995
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001996 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001997 // Record this macro definition's ID.
1998 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001999
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002000 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002001 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2002 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002003 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00002004
Chandler Carruth9e5bb852011-07-14 08:20:46 +00002005 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00002006 Record.push_back(ME->isBuiltinMacro());
2007 if (ME->isBuiltinMacro())
2008 AddIdentifierRef(ME->getName(), Record);
2009 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002010 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00002011 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002012 continue;
2013 }
2014
2015 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
2016 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002017 Record.push_back(ID->getFileName().size());
2018 Record.push_back(ID->wasInQuotes());
2019 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00002020 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002021 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002022 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00002023 // Check that the FileEntry is not null because it was not resolved and
2024 // we create a PCH even with compiler errors.
2025 if (ID->getFile())
2026 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00002027 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2028 continue;
2029 }
2030
2031 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2032 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00002033 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00002034
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002035 // Write the offsets table for the preprocessing record.
2036 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002037 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2038
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002039 // Write the offsets table for identifier IDs.
2040 using namespace llvm;
2041 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002042 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002045 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00002046
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002047 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002048 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00002049 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00002050 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2051 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002052 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00002053}
2054
Douglas Gregore209e502011-12-06 01:10:29 +00002055unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2056 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2057 if (Known != SubmoduleIDs.end())
2058 return Known->second;
2059
2060 return SubmoduleIDs[Mod] = NextSubmoduleID++;
2061}
2062
Douglas Gregor26ced122011-12-01 00:59:36 +00002063/// \brief Compute the number of modules within the given tree (including the
2064/// given module).
2065static unsigned getNumberOfModules(Module *Mod) {
2066 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00002067 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2068 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00002069 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002070 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00002071
2072 return ChildModules + 1;
2073}
2074
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002075void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00002076 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00002077 // FIXME: This feels like it belongs somewhere else, but there are no
2078 // other consumers of this information.
2079 SourceManager &SrcMgr = PP->getSourceManager();
2080 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2081 for (ASTContext::import_iterator I = Context->local_import_begin(),
2082 IEnd = Context->local_import_end();
2083 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00002084 if (Module *ImportedFrom
2085 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
2086 SrcMgr))) {
2087 ImportedFrom->Imports.push_back(I->getImportedModule());
2088 }
2089 }
2090
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002091 // Enter the submodule description block.
2092 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
2093
2094 // Write the abbreviations needed for the submodules block.
2095 using namespace llvm;
2096 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2097 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00002098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002099 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2100 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00002104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00002105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2107 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2108
2109 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002110 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002111 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2112 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2113
2114 Abbrev = new BitCodeAbbrev();
2115 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2116 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2117 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00002118
2119 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002120 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2122 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2123
2124 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002125 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2127 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2128
Douglas Gregor51f564f2011-12-31 04:05:44 +00002129 Abbrev = new BitCodeAbbrev();
2130 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2131 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2132 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2133
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002134 Abbrev = new BitCodeAbbrev();
2135 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2137 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2138
Douglas Gregor26ced122011-12-01 00:59:36 +00002139 // Write the submodule metadata block.
2140 RecordData Record;
2141 Record.push_back(getNumberOfModules(WritingModule));
2142 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2143 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2144
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002145 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002146 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002147 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002148 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002149 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002150 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002151 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002152
2153 // Emit the definition of the block.
2154 Record.clear();
2155 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002156 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002157 if (Mod->Parent) {
2158 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2159 Record.push_back(SubmoduleIDs[Mod->Parent]);
2160 } else {
2161 Record.push_back(0);
2162 }
2163 Record.push_back(Mod->IsFramework);
2164 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002165 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002166 Record.push_back(Mod->InferSubmodules);
2167 Record.push_back(Mod->InferExplicitSubmodules);
2168 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002169 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2170
Douglas Gregor51f564f2011-12-31 04:05:44 +00002171 // Emit the requirements.
2172 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2173 Record.clear();
2174 Record.push_back(SUBMODULE_REQUIRES);
2175 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2176 Mod->Requires[I].data(),
2177 Mod->Requires[I].size());
2178 }
2179
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002180 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002181 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002182 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002183 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002184 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002185 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002186 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2187 Record.clear();
2188 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2189 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2190 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002191 }
2192
2193 // Emit the headers.
2194 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2195 Record.clear();
2196 Record.push_back(SUBMODULE_HEADER);
2197 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2198 Mod->Headers[I]->getName());
2199 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002200 // Emit the excluded headers.
2201 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2202 Record.clear();
2203 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2204 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2205 Mod->ExcludedHeaders[I]->getName());
2206 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002207 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2208 Record.clear();
2209 Record.push_back(SUBMODULE_TOPHEADER);
2210 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2211 Mod->TopHeaders[I]->getName());
2212 }
Douglas Gregor55988682011-12-05 16:33:54 +00002213
2214 // Emit the imports.
2215 if (!Mod->Imports.empty()) {
2216 Record.clear();
2217 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002218 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002219 assert(ImportedID && "Unknown submodule!");
2220 Record.push_back(ImportedID);
2221 }
2222 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2223 }
2224
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002225 // Emit the exports.
2226 if (!Mod->Exports.empty()) {
2227 Record.clear();
2228 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002229 if (Module *Exported = Mod->Exports[I].getPointer()) {
2230 unsigned ExportedID = SubmoduleIDs[Exported];
2231 assert(ExportedID > 0 && "Unknown submodule ID?");
2232 Record.push_back(ExportedID);
2233 } else {
2234 Record.push_back(0);
2235 }
2236
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002237 Record.push_back(Mod->Exports[I].getInt());
2238 }
2239 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2240 }
2241
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002242 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002243 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2244 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002245 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002246 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002247 }
2248
2249 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002250
2251 assert((NextSubmoduleID - FirstSubmoduleID
2252 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002253}
2254
Douglas Gregor185dbd72011-12-01 02:07:58 +00002255serialization::SubmoduleID
2256ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002257 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002258 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002259
2260 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002261 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002262 Module *OwningMod
2263 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002264 if (!OwningMod)
2265 return 0;
2266
Douglas Gregore209e502011-12-06 01:10:29 +00002267 // Check whether this submodule is part of our own module.
2268 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002269 return 0;
2270
Douglas Gregore209e502011-12-06 01:10:29 +00002271 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002272}
2273
David Blaikied6471f72011-09-25 23:23:43 +00002274void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002275 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002276 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002277 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2278 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002279 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002280 if (point.Loc.isInvalid())
2281 continue;
2282
2283 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002284 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002285 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002286 if (I->second.isPragma()) {
2287 Record.push_back(I->first);
2288 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002289 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002290 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002291 Record.push_back(-1); // mark the end of the diag/map pairs for this
2292 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002293 }
2294
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002295 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002296 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002297}
2298
Anders Carlssonc8505782011-03-06 18:41:18 +00002299void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2300 if (CXXBaseSpecifiersOffsets.empty())
2301 return;
2302
2303 RecordData Record;
2304
2305 // Create a blob abbreviation for the C++ base specifiers offsets.
2306 using namespace llvm;
2307
2308 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2309 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2312 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2313
Douglas Gregore92b8a12011-08-04 00:01:48 +00002314 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002315 Record.clear();
2316 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2317 Record.push_back(CXXBaseSpecifiersOffsets.size());
2318 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002319 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002320}
2321
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002322//===----------------------------------------------------------------------===//
2323// Type Serialization
2324//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002325
Sebastian Redl3397c552010-08-18 23:56:27 +00002326/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002327void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002328 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002329 if (Idx.getIndex() == 0) // we haven't seen this type before.
2330 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Douglas Gregor97475832010-10-05 18:37:06 +00002332 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002333
Douglas Gregor2cf26342009-04-09 22:27:44 +00002334 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002335 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002336 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002337 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002338 else if (TypeOffsets.size() < Index) {
2339 TypeOffsets.resize(Index + 1);
2340 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002341 }
2342
2343 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Douglas Gregor2cf26342009-04-09 22:27:44 +00002345 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002346 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002347
Douglas Gregora4923eb2009-11-16 21:35:15 +00002348 if (T.hasLocalNonFastQualifiers()) {
2349 Qualifiers Qs = T.getLocalQualifiers();
2350 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002351 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002352 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002353 } else {
2354 switch (T->getTypeClass()) {
2355 // For all of the concrete, non-dependent types, call the
2356 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002357#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002358 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002359#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002361 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002362 }
2363
2364 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002365 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002366
2367 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002368 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002369}
2370
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002371//===----------------------------------------------------------------------===//
2372// Declaration Serialization
2373//===----------------------------------------------------------------------===//
2374
Douglas Gregor2cf26342009-04-09 22:27:44 +00002375/// \brief Write the block containing all of the declaration IDs
2376/// lexically declared within the given DeclContext.
2377///
2378/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2379/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002380uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002381 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002382 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002383 return 0;
2384
Douglas Gregorc9490c02009-04-16 22:23:12 +00002385 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002386 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002387 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002388 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002389 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2390 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002391 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002392
Douglas Gregor25123082009-04-22 22:34:57 +00002393 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002394 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002395 return Offset;
2396}
2397
Sebastian Redla4232eb2010-08-18 23:56:21 +00002398void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002399 using namespace llvm;
2400 RecordData Record;
2401
2402 // Write the type offsets array
2403 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002404 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002406 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002407 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2408 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2409 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002410 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002411 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002412 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002413 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002414
2415 // Write the declaration offsets array
2416 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002417 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2421 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2422 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002423 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002424 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002425 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002426 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002427}
2428
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002429void ASTWriter::WriteFileDeclIDsMap() {
2430 using namespace llvm;
2431 RecordData Record;
2432
2433 // Join the vectors of DeclIDs from all files.
2434 SmallVector<DeclID, 256> FileSortedIDs;
2435 for (FileDeclIDsTy::iterator
2436 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2437 DeclIDInFileInfo &Info = *FI->second;
2438 Info.FirstDeclIndex = FileSortedIDs.size();
2439 for (LocDeclIDsTy::iterator
2440 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2441 FileSortedIDs.push_back(DI->second);
2442 }
2443
2444 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2445 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2448 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2449 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002450 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002451 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2452}
2453
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002454void ASTWriter::WriteComments() {
2455 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002456 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002457 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002458 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2459 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002460 I != E; ++I) {
2461 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002462 AddSourceRange((*I)->getSourceRange(), Record);
2463 Record.push_back((*I)->getKind());
2464 Record.push_back((*I)->isTrailingComment());
2465 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002466 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2467 }
2468 Stream.ExitBlock();
2469}
2470
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002471//===----------------------------------------------------------------------===//
2472// Global Method Pool and Selector Serialization
2473//===----------------------------------------------------------------------===//
2474
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002475namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002476// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002477class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002478 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002479
2480public:
2481 typedef Selector key_type;
2482 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Sebastian Redl5d050072010-08-04 17:20:04 +00002484 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002485 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002486 ObjCMethodList Instance, Factory;
2487 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002488 typedef const data_type& data_type_ref;
2489
Sebastian Redl3397c552010-08-18 23:56:27 +00002490 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002491
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002492 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002493 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002494 }
Mike Stump1eb44332009-09-09 15:08:12 +00002495
2496 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002497 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002498 data_type_ref Methods) {
2499 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2500 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002501 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2502 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002503 Method = Method->Next)
2504 if (Method->Method)
2505 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002506 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002507 Method = Method->Next)
2508 if (Method->Method)
2509 DataLen += 4;
2510 clang::io::Emit16(Out, DataLen);
2511 return std::make_pair(KeyLen, DataLen);
2512 }
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattner5f9e2722011-07-23 10:55:15 +00002514 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002515 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002516 assert((Start >> 32) == 0 && "Selector key offset too large");
2517 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002518 unsigned N = Sel.getNumArgs();
2519 clang::io::Emit16(Out, N);
2520 if (N == 0)
2521 N = 1;
2522 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002523 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002524 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2525 }
Mike Stump1eb44332009-09-09 15:08:12 +00002526
Chris Lattner5f9e2722011-07-23 10:55:15 +00002527 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002528 data_type_ref Methods, unsigned DataLen) {
2529 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002530 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002531 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002532 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002533 Method = Method->Next)
2534 if (Method->Method)
2535 ++NumInstanceMethods;
2536
2537 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002538 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002539 Method = Method->Next)
2540 if (Method->Method)
2541 ++NumFactoryMethods;
2542
2543 clang::io::Emit16(Out, NumInstanceMethods);
2544 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002545 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002546 Method = Method->Next)
2547 if (Method->Method)
2548 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002549 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002550 Method = Method->Next)
2551 if (Method->Method)
2552 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002553
2554 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002555 }
2556};
2557} // end anonymous namespace
2558
Sebastian Redl059612d2010-08-03 21:58:15 +00002559/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002560///
2561/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002562/// in an on-disk hash table indexed by the selector. The hash table also
2563/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002564void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002565 using namespace llvm;
2566
Sebastian Redl059612d2010-08-03 21:58:15 +00002567 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002568 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002569 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002570 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002571 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002572 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002573 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002574 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002575
Sebastian Redl059612d2010-08-03 21:58:15 +00002576 // Create the on-disk hash table representation. We walk through every
2577 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002578 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002579 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002580 I = SelectorIDs.begin(), E = SelectorIDs.end();
2581 I != E; ++I) {
2582 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002583 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002584 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002585 I->second,
2586 ObjCMethodList(),
2587 ObjCMethodList()
2588 };
2589 if (F != SemaRef.MethodPool.end()) {
2590 Data.Instance = F->second.first;
2591 Data.Factory = F->second.second;
2592 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002593 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002594 // changed.
2595 if (Chain && I->second < FirstSelectorID) {
2596 // Selector already exists. Did it change?
2597 bool changed = false;
2598 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2599 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002600 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002601 changed = true;
2602 }
2603 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2604 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002605 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002606 changed = true;
2607 }
2608 if (!changed)
2609 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002610 } else if (Data.Instance.Method || Data.Factory.Method) {
2611 // A new method pool entry.
2612 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002613 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002614 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002615 }
2616
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002617 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002618 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002619 uint32_t BucketOffset;
2620 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002621 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002622 llvm::raw_svector_ostream Out(MethodPool);
2623 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002624 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002625 BucketOffset = Generator.Emit(Out, Trait);
2626 }
2627
2628 // Create a blob abbreviation
2629 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002630 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2634 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2635
Douglas Gregor83941df2009-04-25 17:48:32 +00002636 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002637 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002638 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002639 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002640 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002641 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002642
2643 // Create a blob abbreviation for the selector table offsets.
2644 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002645 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002647 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002648 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2649 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2650
2651 // Write the selector offsets table.
2652 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002653 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002654 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002655 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002656 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002657 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002658 }
2659}
2660
Sebastian Redl3397c552010-08-18 23:56:27 +00002661/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002662void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002663 using namespace llvm;
2664 if (SemaRef.ReferencedSelectors.empty())
2665 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002666
Fariborz Jahanian32019832010-07-23 19:11:11 +00002667 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002668
Sebastian Redl3397c552010-08-18 23:56:27 +00002669 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002670 // very tricky to fix, and given that @selector shouldn't really appear in
2671 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002672 for (DenseMap<Selector, SourceLocation>::iterator S =
2673 SemaRef.ReferencedSelectors.begin(),
2674 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2675 Selector Sel = (*S).first;
2676 SourceLocation Loc = (*S).second;
2677 AddSelectorRef(Sel, Record);
2678 AddSourceLocation(Loc, Record);
2679 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002680 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002681}
2682
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002683//===----------------------------------------------------------------------===//
2684// Identifier Table Serialization
2685//===----------------------------------------------------------------------===//
2686
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002687namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002688class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002689 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002690 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002691 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002692 bool IsModule;
2693
Douglas Gregora92193e2009-04-28 21:18:29 +00002694 /// \brief Determines whether this is an "interesting" identifier
2695 /// that needs a full IdentifierInfo structure written into the hash
2696 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002697 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002698 if (II->isPoisoned() ||
2699 II->isExtensionToken() ||
2700 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002701 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002702 II->getFETokenInfo<void>())
2703 return true;
2704
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002705 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002706 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002707
2708 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2709 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002710 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002711
2712 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002713 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002714
2715 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002716 }
2717
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002718public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002719 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002720 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002721
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002723 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Douglas Gregoreee242f2011-10-27 09:33:13 +00002725 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2726 IdentifierResolver &IdResolver, bool IsModule)
2727 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002728
2729 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002730 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002731 }
Mike Stump1eb44332009-09-09 15:08:12 +00002732
2733 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002734 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002735 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002736 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002737 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002738 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002739 DataLen += 2; // 2 bytes for builtin ID
2740 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002741 if (hadMacroDefinition(II, Macro)) {
2742 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2743 if (Writer.getMacroRef(M) != 0)
2744 DataLen += 4;
2745 }
2746
2747 DataLen += 4;
2748 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002749
Douglas Gregoreee242f2011-10-27 09:33:13 +00002750 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2751 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002752 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002753 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002754 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002755 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002756 // We emit the key length after the data length so that every
2757 // string is preceded by a 16-bit length. This matches the PTH
2758 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002759 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002760 return std::make_pair(KeyLen, DataLen);
2761 }
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Chris Lattner5f9e2722011-07-23 10:55:15 +00002763 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002764 unsigned KeyLen) {
2765 // Record the location of the key data. This is used when generating
2766 // the mapping from persistent IDs to strings.
2767 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002768 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002769 }
Mike Stump1eb44332009-09-09 15:08:12 +00002770
Douglas Gregor7143aab2011-09-01 17:04:32 +00002771 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002772 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002773 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002774 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002775 clang::io::Emit32(Out, ID << 1);
2776 return;
2777 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002778
Douglas Gregora92193e2009-04-28 21:18:29 +00002779 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002780 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2781 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2782 clang::io::Emit16(Out, Bits);
2783 Bits = 0;
2784 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002785 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002786 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2787 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002788 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002789 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002790 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002791
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002792 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002793 // Write all of the macro IDs associated with this identifier.
2794 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2795 if (MacroID ID = Writer.getMacroRef(M))
2796 clang::io::Emit32(Out, ID);
2797 }
2798
2799 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002800 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002801
Douglas Gregor668c1a42009-04-21 22:25:48 +00002802 // Emit the declaration IDs in reverse order, because the
2803 // IdentifierResolver provides the declarations as they would be
2804 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002805 // "stat"), but the ASTReader adds declarations to the end of the list
2806 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002807 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002808 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2809 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002810 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002811 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002812 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002813 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002814 }
2815};
2816} // end anonymous namespace
2817
Sebastian Redl3397c552010-08-18 23:56:27 +00002818/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002819///
2820/// The identifier table consists of a blob containing string data
2821/// (the actual identifiers themselves) and a separate "offsets" index
2822/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002823void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2824 IdentifierResolver &IdResolver,
2825 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002826 using namespace llvm;
2827
2828 // Create and write out the blob that contains the identifier
2829 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002830 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002831 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002832 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Douglas Gregor92b059e2009-04-28 20:33:11 +00002834 // Look for any identifiers that were named while processing the
2835 // headers, but are otherwise not needed. We add these to the hash
2836 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002837 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002838 // file.
2839 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2840 IDEnd = PP.getIdentifierTable().end();
2841 ID != IDEnd; ++ID)
2842 getIdentifierRef(ID->second);
2843
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002844 // Create the on-disk hash table representation. We only store offsets
2845 // for identifiers that appear here for the first time.
2846 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002847 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002848 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2849 ID != IDEnd; ++ID) {
2850 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002851 if (!Chain || !ID->first->isFromAST() ||
2852 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002853 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2854 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002855 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002856
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002857 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002858 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002859 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002860 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002861 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002862 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002863 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002864 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002865 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002866 }
2867
2868 // Create a blob abbreviation
2869 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002870 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002872 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002873 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002874
2875 // Write the identifier table
2876 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002877 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002878 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002879 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002880 }
2881
2882 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002883 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002884 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2888 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2889
2890 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002891 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002892 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002893 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002894 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002895 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002896}
2897
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002898//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002899// DeclContext's Name Lookup Table Serialization
2900//===----------------------------------------------------------------------===//
2901
2902namespace {
2903// Trait used for the on-disk hash table used in the method pool.
2904class ASTDeclContextNameLookupTrait {
2905 ASTWriter &Writer;
2906
2907public:
2908 typedef DeclarationName key_type;
2909 typedef key_type key_type_ref;
2910
2911 typedef DeclContext::lookup_result data_type;
2912 typedef const data_type& data_type_ref;
2913
2914 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2915
2916 unsigned ComputeHash(DeclarationName Name) {
2917 llvm::FoldingSetNodeID ID;
2918 ID.AddInteger(Name.getNameKind());
2919
2920 switch (Name.getNameKind()) {
2921 case DeclarationName::Identifier:
2922 ID.AddString(Name.getAsIdentifierInfo()->getName());
2923 break;
2924 case DeclarationName::ObjCZeroArgSelector:
2925 case DeclarationName::ObjCOneArgSelector:
2926 case DeclarationName::ObjCMultiArgSelector:
2927 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2928 break;
2929 case DeclarationName::CXXConstructorName:
2930 case DeclarationName::CXXDestructorName:
2931 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002932 break;
2933 case DeclarationName::CXXOperatorName:
2934 ID.AddInteger(Name.getCXXOverloadedOperator());
2935 break;
2936 case DeclarationName::CXXLiteralOperatorName:
2937 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2938 case DeclarationName::CXXUsingDirective:
2939 break;
2940 }
2941
2942 return ID.ComputeHash();
2943 }
2944
2945 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002946 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002947 data_type_ref Lookup) {
2948 unsigned KeyLen = 1;
2949 switch (Name.getNameKind()) {
2950 case DeclarationName::Identifier:
2951 case DeclarationName::ObjCZeroArgSelector:
2952 case DeclarationName::ObjCOneArgSelector:
2953 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002954 case DeclarationName::CXXLiteralOperatorName:
2955 KeyLen += 4;
2956 break;
2957 case DeclarationName::CXXOperatorName:
2958 KeyLen += 1;
2959 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002960 case DeclarationName::CXXConstructorName:
2961 case DeclarationName::CXXDestructorName:
2962 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002963 case DeclarationName::CXXUsingDirective:
2964 break;
2965 }
2966 clang::io::Emit16(Out, KeyLen);
2967
2968 // 2 bytes for num of decls and 4 for each DeclID.
2969 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2970 clang::io::Emit16(Out, DataLen);
2971
2972 return std::make_pair(KeyLen, DataLen);
2973 }
2974
Chris Lattner5f9e2722011-07-23 10:55:15 +00002975 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002976 using namespace clang::io;
2977
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002978 Emit8(Out, Name.getNameKind());
2979 switch (Name.getNameKind()) {
2980 case DeclarationName::Identifier:
2981 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002982 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002983 case DeclarationName::ObjCZeroArgSelector:
2984 case DeclarationName::ObjCOneArgSelector:
2985 case DeclarationName::ObjCMultiArgSelector:
2986 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002987 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002988 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002989 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2990 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002991 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002992 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002993 case DeclarationName::CXXLiteralOperatorName:
2994 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002995 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002996 case DeclarationName::CXXConstructorName:
2997 case DeclarationName::CXXDestructorName:
2998 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002999 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00003000 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003001 }
Benjamin Kramer59313312012-09-19 13:40:40 +00003002
3003 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003004 }
3005
Chris Lattner5f9e2722011-07-23 10:55:15 +00003006 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003007 data_type Lookup, unsigned DataLen) {
3008 uint64_t Start = Out.tell(); (void)Start;
3009 clang::io::Emit16(Out, Lookup.second - Lookup.first);
3010 for (; Lookup.first != Lookup.second; ++Lookup.first)
3011 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
3012
3013 assert(Out.tell() - Start == DataLen && "Data length is wrong");
3014 }
3015};
3016} // end anonymous namespace
3017
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003018/// \brief Write the block containing all of the declaration IDs
3019/// visible from the given DeclContext.
3020///
3021/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003022/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003023uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3024 DeclContext *DC) {
3025 if (DC->getPrimaryContext() != DC)
3026 return 0;
3027
3028 // Since there is no name lookup into functions or methods, don't bother to
3029 // build a visible-declarations table for these entities.
3030 if (DC->isFunctionOrMethod())
3031 return 0;
3032
3033 // If not in C++, we perform name lookup for the translation unit via the
3034 // IdentifierInfo chains, don't bother to build a visible-declarations table.
3035 // FIXME: In C++ we need the visible declarations in order to "see" the
3036 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00003037 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003038 return 0;
3039
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003040 // Serialize the contents of the mapping used for lookup. Note that,
3041 // although we have two very different code paths, the serialized
3042 // representation is the same for both cases: a declaration name,
3043 // followed by a size, followed by references to the visible
3044 // declarations that have that name.
3045 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00003046 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003047 if (!Map || Map->empty())
3048 return 0;
3049
3050 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3051 ASTDeclContextNameLookupTrait Trait(*this);
3052
3053 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00003054 DeclarationName ConversionName;
3055 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003056 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3057 D != DEnd; ++D) {
3058 DeclarationName Name = D->first;
3059 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00003060 if (Result.first != Result.second) {
3061 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3062 // Hash all conversion function names to the same name. The actual
3063 // type information in conversion function name is not used in the
3064 // key (since such type information is not stable across different
3065 // modules), so the intended effect is to coalesce all of the conversion
3066 // functions under a single key.
3067 if (!ConversionName)
3068 ConversionName = Name;
3069 ConversionDecls.append(Result.first, Result.second);
3070 continue;
3071 }
3072
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003073 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00003074 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003075 }
3076
Douglas Gregore5a54b62011-08-30 20:49:19 +00003077 // Add the conversion functions
3078 if (!ConversionDecls.empty()) {
3079 Generator.insert(ConversionName,
3080 DeclContext::lookup_result(ConversionDecls.begin(),
3081 ConversionDecls.end()),
3082 Trait);
3083 }
3084
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003085 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003086 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00003087 uint32_t BucketOffset;
3088 {
3089 llvm::raw_svector_ostream Out(LookupTable);
3090 // Make sure that no bucket is at offset 0
3091 clang::io::Emit32(Out, 0);
3092 BucketOffset = Generator.Emit(Out, Trait);
3093 }
3094
3095 // Write the lookup table
3096 RecordData Record;
3097 Record.push_back(DECL_CONTEXT_VISIBLE);
3098 Record.push_back(BucketOffset);
3099 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3100 LookupTable.str());
3101
3102 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3103 ++NumVisibleDeclContexts;
3104 return Offset;
3105}
3106
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003107/// \brief Write an UPDATE_VISIBLE block for the given context.
3108///
3109/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3110/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00003111/// (in C++), for namespaces, and for classes with forward-declared unscoped
3112/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003113void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003114 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3115 if (!Map || Map->empty())
3116 return;
3117
3118 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3119 ASTDeclContextNameLookupTrait Trait(*this);
3120
3121 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003122 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3123 D != DEnd; ++D) {
3124 DeclarationName Name = D->first;
3125 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00003126 // For any name that appears in this table, the results are complete, i.e.
3127 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00003128 if (Result.first != Result.second)
3129 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003130 }
3131
3132 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003133 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003134 uint32_t BucketOffset;
3135 {
3136 llvm::raw_svector_ostream Out(LookupTable);
3137 // Make sure that no bucket is at offset 0
3138 clang::io::Emit32(Out, 0);
3139 BucketOffset = Generator.Emit(Out, Trait);
3140 }
3141
3142 // Write the lookup table
3143 RecordData Record;
3144 Record.push_back(UPDATE_VISIBLE);
3145 Record.push_back(getDeclID(cast<Decl>(DC)));
3146 Record.push_back(BucketOffset);
3147 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3148}
3149
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003150/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3151void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3152 RecordData Record;
3153 Record.push_back(Opts.fp_contract);
3154 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3155}
3156
3157/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3158void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003159 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003160 return;
3161
3162 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3163 RecordData Record;
3164#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3165#include "clang/Basic/OpenCLExtensions.def"
3166 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3167}
3168
Douglas Gregor2171bf12012-01-15 16:58:34 +00003169void ASTWriter::WriteRedeclarations() {
3170 RecordData LocalRedeclChains;
3171 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3172
3173 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3174 Decl *First = Redeclarations[I];
3175 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3176
3177 Decl *MostRecent = First->getMostRecentDecl();
3178
3179 // If we only have a single declaration, there is no point in storing
3180 // a redeclaration chain.
3181 if (First == MostRecent)
3182 continue;
3183
3184 unsigned Offset = LocalRedeclChains.size();
3185 unsigned Size = 0;
3186 LocalRedeclChains.push_back(0); // Placeholder for the size.
3187
3188 // Collect the set of local redeclarations of this declaration.
3189 for (Decl *Prev = MostRecent; Prev != First;
3190 Prev = Prev->getPreviousDecl()) {
3191 if (!Prev->isFromASTFile()) {
3192 AddDeclRef(Prev, LocalRedeclChains);
3193 ++Size;
3194 }
3195 }
3196 LocalRedeclChains[Offset] = Size;
3197
3198 // Reverse the set of local redeclarations, so that we store them in
3199 // order (since we found them in reverse order).
3200 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3201
3202 // Add the mapping from the first ID to the set of local declarations.
3203 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3204 LocalRedeclsMap.push_back(Info);
3205
3206 assert(N == Redeclarations.size() &&
3207 "Deserialized a declaration we shouldn't have");
3208 }
3209
3210 if (LocalRedeclChains.empty())
3211 return;
3212
3213 // Sort the local redeclarations map by the first declaration ID,
3214 // since the reader will be performing binary searches on this information.
3215 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3216
3217 // Emit the local redeclarations map.
3218 using namespace llvm;
3219 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3220 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3223 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3224
3225 RecordData Record;
3226 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3227 Record.push_back(LocalRedeclsMap.size());
3228 Stream.EmitRecordWithBlob(AbbrevID, Record,
3229 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3230 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3231
3232 // Emit the redeclaration chains.
3233 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3234}
3235
Douglas Gregorcff9f262012-01-27 01:47:08 +00003236void ASTWriter::WriteObjCCategories() {
3237 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3238 RecordData Categories;
3239
3240 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3241 unsigned Size = 0;
3242 unsigned StartIndex = Categories.size();
3243
3244 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3245
3246 // Allocate space for the size.
3247 Categories.push_back(0);
3248
3249 // Add the categories.
3250 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3251 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3252 assert(getDeclID(Cat) != 0 && "Bogus category");
3253 AddDeclRef(Cat, Categories);
3254 }
3255
3256 // Update the size.
3257 Categories[StartIndex] = Size;
3258
3259 // Record this interface -> category map.
3260 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3261 CategoriesMap.push_back(CatInfo);
3262 }
3263
3264 // Sort the categories map by the definition ID, since the reader will be
3265 // performing binary searches on this information.
3266 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3267
3268 // Emit the categories map.
3269 using namespace llvm;
3270 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3271 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3274 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3275
3276 RecordData Record;
3277 Record.push_back(OBJC_CATEGORIES_MAP);
3278 Record.push_back(CategoriesMap.size());
3279 Stream.EmitRecordWithBlob(AbbrevID, Record,
3280 reinterpret_cast<char*>(CategoriesMap.data()),
3281 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3282
3283 // Emit the category lists.
3284 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3285}
3286
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003287void ASTWriter::WriteMergedDecls() {
3288 if (!Chain || Chain->MergedDecls.empty())
3289 return;
3290
3291 RecordData Record;
3292 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3293 IEnd = Chain->MergedDecls.end();
3294 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003295 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003296 : getDeclID(I->first);
3297 assert(CanonID && "Merged declaration not known?");
3298
3299 Record.push_back(CanonID);
3300 Record.push_back(I->second.size());
3301 Record.append(I->second.begin(), I->second.end());
3302 }
3303 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3304}
3305
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003306//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003307// General Serialization Routines
3308//===----------------------------------------------------------------------===//
3309
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003310/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003311void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3312 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003313 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003314 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3315 e = Attrs.end(); i != e; ++i){
3316 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003317 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003318 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003319
Sean Huntcf807c42010-08-18 23:23:40 +00003320#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003321
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003322 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003323}
3324
Chris Lattner5f9e2722011-07-23 10:55:15 +00003325void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003326 Record.push_back(Str.size());
3327 Record.insert(Record.end(), Str.begin(), Str.end());
3328}
3329
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003330void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3331 RecordDataImpl &Record) {
3332 Record.push_back(Version.getMajor());
3333 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3334 Record.push_back(*Minor + 1);
3335 else
3336 Record.push_back(0);
3337 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3338 Record.push_back(*Subminor + 1);
3339 else
3340 Record.push_back(0);
3341}
3342
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003343/// \brief Note that the identifier II occurs at the given offset
3344/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003345void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003346 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003347 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003348 // up earlier in the chain and thus don't need an offset.
3349 if (ID >= FirstIdentID)
3350 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003351}
3352
Douglas Gregor83941df2009-04-25 17:48:32 +00003353/// \brief Note that the selector Sel occurs at the given offset
3354/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003355void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003356 unsigned ID = SelectorIDs[Sel];
3357 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003358 // Don't record offsets for selectors that are also available in a different
3359 // file.
3360 if (ID < FirstSelectorID)
3361 return;
3362 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003363}
3364
Sebastian Redla4232eb2010-08-18 23:56:21 +00003365ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003366 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003367 WritingAST(false), DoneWritingDeclsAndTypes(false),
3368 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003369 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003370 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003371 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3372 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003373 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3374 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003375 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003376 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003377 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003378 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003379 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003380 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003381 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3382 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3383 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003384 DeclTypedefAbbrev(0),
3385 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3386 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003387{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003388}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003389
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003390ASTWriter::~ASTWriter() {
3391 for (FileDeclIDsTy::iterator
3392 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3393 delete I->second;
3394}
3395
Sebastian Redla4232eb2010-08-18 23:56:21 +00003396void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003397 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003398 Module *WritingModule, StringRef isysroot,
3399 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003400 WritingAST = true;
3401
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003402 ASTHasCompilerErrors = hasErrors;
3403
Douglas Gregor2cf26342009-04-09 22:27:44 +00003404 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003405 Stream.Emit((unsigned)'C', 8);
3406 Stream.Emit((unsigned)'P', 8);
3407 Stream.Emit((unsigned)'C', 8);
3408 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003409
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003410 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003411
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003412 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003413 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003414 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003415 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003416 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003417 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003418 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003419
3420 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003421}
3422
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003423template<typename Vector>
3424static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3425 ASTWriter::RecordData &Record) {
3426 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3427 I != E; ++I) {
3428 Writer.AddDeclRef(*I, Record);
3429 }
3430}
3431
Sebastian Redla4232eb2010-08-18 23:56:21 +00003432void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003433 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003434 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003435 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003436 using namespace llvm;
3437
Douglas Gregorecc2c092011-12-01 22:20:10 +00003438 // Make sure that the AST reader knows to finalize itself.
3439 if (Chain)
3440 Chain->finalizeForWriting();
3441
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003442 ASTContext &Context = SemaRef.Context;
3443 Preprocessor &PP = SemaRef.PP;
3444
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003445 // Set up predefined declaration IDs.
3446 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003447 if (Context.ObjCIdDecl)
3448 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003449 if (Context.ObjCSelDecl)
3450 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003451 if (Context.ObjCClassDecl)
3452 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003453 if (Context.ObjCProtocolClassDecl)
3454 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003455 if (Context.Int128Decl)
3456 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3457 if (Context.UInt128Decl)
3458 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003459 if (Context.ObjCInstanceTypeDecl)
3460 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003461 if (Context.BuiltinVaListDecl)
3462 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3463
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003464 if (!Chain) {
3465 // Make sure that we emit IdentifierInfos (and any attached
3466 // declarations) for builtins. We don't need to do this when we're
3467 // emitting chained PCH files, because all of the builtins will be
3468 // in the original PCH file.
3469 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003470 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003471 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003472 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003473 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003474 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3475 getIdentifierRef(&Table.get(BuiltinNames[I]));
3476 }
3477
Douglas Gregoreee242f2011-10-27 09:33:13 +00003478 // If there are any out-of-date identifiers, bring them up to date.
3479 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3480 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3481 IDEnd = PP.getIdentifierTable().end();
3482 ID != IDEnd; ++ID)
3483 if (ID->second->isOutOfDate())
3484 ExtSource->updateOutOfDateIdentifier(*ID->second);
3485 }
3486
Chris Lattner63d65f82009-09-08 18:19:27 +00003487 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003488 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003489 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003490 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003491 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003492
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003493 // Build a record containing all of the file scoped decls in this file.
3494 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003495 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3496 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003497
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003498 // Build a record containing all of the delegating constructors we still need
3499 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003500 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003501 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003502
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003503 // Write the set of weak, undeclared identifiers. We always write the
3504 // entire table, since later PCH files in a PCH chain are only interested in
3505 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003506 RecordData WeakUndeclaredIdentifiers;
3507 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003508 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003509 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3510 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3511 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3512 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3513 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3514 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3515 }
3516 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003517
Douglas Gregor14c22f22009-04-22 22:18:58 +00003518 // Build a record containing all of the locally-scoped external
3519 // declarations in this header file. Generally, this record will be
3520 // empty.
3521 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003522 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003523 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003524 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003525 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3526 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003527 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003528 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003529 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3530 }
3531
Douglas Gregorb81c1702009-04-27 20:06:05 +00003532 // Build a record containing all of the ext_vector declarations.
3533 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003534 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003535
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003536 // Build a record containing all of the VTable uses information.
3537 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003538 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003539 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3540 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3541 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3542 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3543 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003544 }
3545
3546 // Build a record containing all of dynamic classes declarations.
3547 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003548 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003549
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003550 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003551 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003552 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003553 I = SemaRef.PendingInstantiations.begin(),
3554 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3555 AddDeclRef(I->first, PendingInstantiations);
3556 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003557 }
3558 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3559 "There are local ones at end of translation unit!");
3560
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003561 // Build a record containing some declaration references.
3562 RecordData SemaDeclRefs;
3563 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3564 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3565 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3566 }
3567
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003568 RecordData CUDASpecialDeclRefs;
3569 if (Context.getcudaConfigureCallDecl()) {
3570 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3571 }
3572
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003573 // Build a record containing all of the known namespaces.
3574 RecordData KnownNamespaces;
3575 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3576 I = SemaRef.KnownNamespaces.begin(),
3577 IEnd = SemaRef.KnownNamespaces.end();
3578 I != IEnd; ++I) {
3579 if (!I->second)
3580 AddDeclRef(I->first, KnownNamespaces);
3581 }
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003582
3583 // Write the control block
Douglas Gregorbbf38312012-10-24 16:50:34 +00003584 WriteControlBlock(PP, Context, isysroot, OutputFile);
Douglas Gregor1d9d9892012-10-18 05:31:06 +00003585
Sebastian Redl3397c552010-08-18 23:56:27 +00003586 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003587 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003588 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregor832d6202011-07-22 16:35:34 +00003589 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003590 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003591
3592 // Create a lexical update block containing all of the declarations in the
3593 // translation unit that do not come from other AST files.
3594 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3595 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3596 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3597 E = TU->noload_decls_end();
3598 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003599 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003600 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003601 }
3602
3603 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3604 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3605 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3606 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3607 Record.clear();
3608 Record.push_back(TU_UPDATE_LEXICAL);
3609 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3610 data(NewGlobalDecls));
3611
3612 // And a visible updates block for the translation unit.
3613 Abv = new llvm::BitCodeAbbrev();
3614 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3615 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3616 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3617 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3618 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3619 WriteDeclContextVisibleUpdate(TU);
3620
3621 // If the translation unit has an anonymous namespace, and we don't already
3622 // have an update block for it, write it as an update block.
3623 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3624 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3625 if (Record.empty()) {
3626 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003627 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003628 }
3629 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003630
3631 // Make sure visible decls, added to DeclContexts previously loaded from
3632 // an AST file, are registered for serialization.
3633 for (SmallVector<const Decl *, 16>::iterator
3634 I = UpdatingVisibleDecls.begin(),
3635 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3636 GetDeclRef(*I);
3637 }
3638
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003639 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003640 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003641
Douglas Gregora119da02011-08-02 16:26:37 +00003642 // Form the record of special types.
3643 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003644 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003645 AddTypeRef(Context.getFILEType(), SpecialTypes);
3646 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3647 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3648 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3649 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003650 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003651 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003652
Douglas Gregor366809a2009-04-26 03:49:13 +00003653 // Keep writing types and declarations until all types and
3654 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003655 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003656 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003657 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3658 E = DeclsToRewrite.end();
3659 I != E; ++I)
3660 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003661 while (!DeclTypesToEmit.empty()) {
3662 DeclOrType DOT = DeclTypesToEmit.front();
3663 DeclTypesToEmit.pop();
3664 if (DOT.isType())
3665 WriteType(DOT.getType());
3666 else
3667 WriteDecl(Context, DOT.getDecl());
3668 }
3669 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003670
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003671 DoneWritingDeclsAndTypes = true;
3672
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003673 WriteFileDeclIDsMap();
3674 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003675 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003676
3677 if (Chain) {
3678 // Write the mapping information describing our module dependencies and how
3679 // each of those modules were mapped into our own offset/ID space, so that
3680 // the reader can build the appropriate mapping to its own offset/ID space.
3681 // The map consists solely of a blob with the following format:
3682 // *(module-name-len:i16 module-name:len*i8
3683 // source-location-offset:i32
3684 // identifier-id:i32
3685 // preprocessed-entity-id:i32
3686 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003687 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003688 // selector-id:i32
3689 // declaration-id:i32
3690 // c++-base-specifiers-id:i32
3691 // type-id:i32)
3692 //
3693 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3694 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3696 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003697 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003698 {
3699 llvm::raw_svector_ostream Out(Buffer);
3700 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003701 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003702 M != MEnd; ++M) {
3703 StringRef FileName = (*M)->FileName;
3704 io::Emit16(Out, FileName.size());
3705 Out.write(FileName.data(), FileName.size());
3706 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3707 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003708 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003709 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003710 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003711 io::Emit32(Out, (*M)->BaseSelectorID);
3712 io::Emit32(Out, (*M)->BaseDeclID);
3713 io::Emit32(Out, (*M)->BaseTypeIndex);
3714 }
3715 }
3716 Record.clear();
3717 Record.push_back(MODULE_OFFSET_MAP);
3718 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3719 Buffer.data(), Buffer.size());
3720 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003721 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003722 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003723 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003724 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003725 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003726 WriteFPPragmaOptions(SemaRef.getFPOptions());
3727 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003728
Sebastian Redl1476ed42010-07-16 16:36:56 +00003729 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003730 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003731
Anders Carlssonc8505782011-03-06 18:41:18 +00003732 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003733
Douglas Gregore209e502011-12-06 01:10:29 +00003734 // If we're emitting a module, write out the submodule information.
3735 if (WritingModule)
3736 WriteSubmodules(WritingModule);
3737
Douglas Gregora119da02011-08-02 16:26:37 +00003738 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3739
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003740 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003741 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003742 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003743
3744 // Write the record containing tentative definitions.
3745 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003746 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003747
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003748 // Write the record containing unused file scoped decls.
3749 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003750 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003751
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003752 // Write the record containing weak undeclared identifiers.
3753 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003754 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003755 WeakUndeclaredIdentifiers);
3756
Douglas Gregor14c22f22009-04-22 22:18:58 +00003757 // Write the record containing locally-scoped external definitions.
3758 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003759 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003760 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003761
3762 // Write the record containing ext_vector type names.
3763 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003764 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003765
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003766 // Write the record containing VTable uses information.
3767 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003768 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003769
3770 // Write the record containing dynamic classes declarations.
3771 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003772 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003773
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003774 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003775 if (!PendingInstantiations.empty())
3776 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003777
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003778 // Write the record containing declaration references of Sema.
3779 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003780 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003781
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003782 // Write the record containing CUDA-specific declaration references.
3783 if (!CUDASpecialDeclRefs.empty())
3784 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003785
3786 // Write the delegating constructors.
3787 if (!DelegatingCtorDecls.empty())
3788 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003789
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003790 // Write the known namespaces.
3791 if (!KnownNamespaces.empty())
3792 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3793
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003794 // Write the visible updates to DeclContexts.
3795 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3796 I = UpdatedDeclContexts.begin(),
3797 E = UpdatedDeclContexts.end();
3798 I != E; ++I)
3799 WriteDeclContextVisibleUpdate(*I);
3800
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003801 if (!WritingModule) {
3802 // Write the submodules that were imported, if any.
3803 RecordData ImportedModules;
3804 for (ASTContext::import_iterator I = Context.local_import_begin(),
3805 IEnd = Context.local_import_end();
3806 I != IEnd; ++I) {
3807 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3808 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3809 }
3810 if (!ImportedModules.empty()) {
3811 // Sort module IDs.
3812 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3813
3814 // Unique module IDs.
3815 ImportedModules.erase(std::unique(ImportedModules.begin(),
3816 ImportedModules.end()),
3817 ImportedModules.end());
3818
3819 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3820 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003821 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003822
3823 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003824 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003825 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003826 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003827 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003828 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003829
Douglas Gregor3e1af842009-04-17 22:13:46 +00003830 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003831 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003832 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003833 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003834 Record.push_back(NumLexicalDeclContexts);
3835 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003836 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003837 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003838}
3839
Douglas Gregora8235d62012-10-09 23:05:51 +00003840void ASTWriter::WriteMacroUpdates() {
3841 if (MacroUpdates.empty())
3842 return;
3843
3844 RecordData Record;
3845 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3846 E = MacroUpdates.end();
3847 I != E; ++I) {
3848 addMacroRef(I->first, Record);
3849 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003850 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003851 }
3852 Stream.EmitRecord(MACRO_UPDATES, Record);
3853}
3854
Douglas Gregor61c5e342011-09-17 00:05:03 +00003855/// \brief Go through the declaration update blocks and resolve declaration
3856/// pointers into declaration IDs.
3857void ASTWriter::ResolveDeclUpdatesBlocks() {
3858 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))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003864 continue; // The decl will be written completely
3865
3866 unsigned Idx = 0, N = URec.size();
3867 while (Idx < N) {
3868 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003869 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3870 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3871 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3872 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3873 ++Idx;
3874 break;
3875
3876 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3877 ++Idx;
3878 break;
3879 }
3880 }
3881 }
3882}
3883
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003884void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003885 if (DeclUpdates.empty())
3886 return;
3887
3888 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003889 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003890 for (DeclUpdateMap::iterator
3891 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3892 const Decl *D = I->first;
3893 UpdateRecord &URec = I->second;
3894
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003895 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003896 continue; // The decl will be written completely,no need to store updates.
3897
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003898 uint64_t Offset = Stream.GetCurrentBitNo();
3899 Stream.EmitRecord(DECL_UPDATES, URec);
3900
3901 OffsetsRecord.push_back(GetDeclRef(D));
3902 OffsetsRecord.push_back(Offset);
3903 }
3904 Stream.ExitBlock();
3905 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3906}
3907
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003908void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003909 if (ReplacedDecls.empty())
3910 return;
3911
3912 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003913 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003914 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003915 Record.push_back(I->ID);
3916 Record.push_back(I->Offset);
3917 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003918 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003919 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003920}
3921
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003922void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003923 Record.push_back(Loc.getRawEncoding());
3924}
3925
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003926void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003927 AddSourceLocation(Range.getBegin(), Record);
3928 AddSourceLocation(Range.getEnd(), Record);
3929}
3930
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003931void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003932 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003933 const uint64_t *Words = Value.getRawData();
3934 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003935}
3936
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003937void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003938 Record.push_back(Value.isUnsigned());
3939 AddAPInt(Value, Record);
3940}
3941
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003942void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003943 AddAPInt(Value.bitcastToAPInt(), Record);
3944}
3945
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003946void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003947 Record.push_back(getIdentifierRef(II));
3948}
3949
Douglas Gregora8235d62012-10-09 23:05:51 +00003950void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3951 Record.push_back(getMacroRef(MI));
3952}
3953
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003954IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003955 if (II == 0)
3956 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003957
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003958 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003959 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003960 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003961 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003962}
3963
Douglas Gregora8235d62012-10-09 23:05:51 +00003964MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3965 // Don't emit builtin macros like __LINE__ to the AST file unless they
3966 // have been redefined by the header (in which case they are not
3967 // isBuiltinMacro).
3968 if (MI == 0 || MI->isBuiltinMacro())
3969 return 0;
3970
3971 MacroID &ID = MacroIDs[MI];
3972 if (ID == 0)
3973 ID = NextMacroID++;
3974 return ID;
3975}
3976
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003977void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003978 Record.push_back(getSelectorRef(SelRef));
3979}
3980
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003981SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003982 if (Sel.getAsOpaquePtr() == 0) {
3983 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003984 }
3985
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003986 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003987 if (SID == 0 && Chain) {
3988 // This might trigger a ReadSelector callback, which will set the ID for
3989 // this selector.
3990 Chain->LoadSelector(Sel);
3991 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003992 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003993 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003994 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003995 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003996}
3997
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003998void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003999 AddDeclRef(Temp->getDestructor(), Record);
4000}
4001
Douglas Gregor7c789c12010-10-29 22:39:52 +00004002void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4003 CXXBaseSpecifier const *BasesEnd,
4004 RecordDataImpl &Record) {
4005 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4006 CXXBaseSpecifiersToWrite.push_back(
4007 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4008 Bases, BasesEnd));
4009 Record.push_back(NextCXXBaseSpecifiersID++);
4010}
4011
Sebastian Redla4232eb2010-08-18 23:56:21 +00004012void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004013 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004014 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004015 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00004016 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004017 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00004018 break;
4019 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004020 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00004021 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00004022 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004023 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004024 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004025 break;
4026 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00004027 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00004028 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00004029 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00004030 break;
John McCall833ca992009-10-29 08:12:44 +00004031 case TemplateArgument::Null:
4032 case TemplateArgument::Integral:
4033 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004034 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00004035 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00004036 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00004037 break;
4038 }
4039}
4040
Sebastian Redla4232eb2010-08-18 23:56:21 +00004041void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004042 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004043 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00004044
4045 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4046 bool InfoHasSameExpr
4047 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4048 Record.push_back(InfoHasSameExpr);
4049 if (InfoHasSameExpr)
4050 return; // Avoid storing the same expr twice.
4051 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00004052 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4053 Record);
4054}
4055
Douglas Gregordc355712011-02-25 00:36:19 +00004056void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4057 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00004058 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00004059 AddTypeRef(QualType(), Record);
4060 return;
4061 }
4062
Douglas Gregordc355712011-02-25 00:36:19 +00004063 AddTypeLoc(TInfo->getTypeLoc(), Record);
4064}
4065
4066void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4067 AddTypeRef(TL.getType(), Record);
4068
John McCalla1ee0c52009-10-16 21:56:05 +00004069 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00004070 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00004071 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00004072}
4073
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004074void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00004075 Record.push_back(GetOrCreateTypeID(T));
4076}
4077
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004078TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
4079 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004080 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
4081}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004082
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004083TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00004084 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00004085 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004086}
4087
4088TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
4089 if (T.isNull())
4090 return TypeIdx();
4091 assert(!T.getLocalFastQualifiers());
4092
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00004093 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004094 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004095 if (DoneWritingDeclsAndTypes) {
4096 assert(0 && "New type seen after serializing all the types to emit!");
4097 return TypeIdx();
4098 }
4099
Douglas Gregor366809a2009-04-26 03:49:13 +00004100 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00004101 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004102 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004103 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00004104 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004105 return Idx;
4106}
Douglas Gregor2cf26342009-04-09 22:27:44 +00004107
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004108TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00004109 if (T.isNull())
4110 return TypeIdx();
4111 assert(!T.getLocalFastQualifiers());
4112
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00004113 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4114 assert(I != TypeIdxs.end() && "Type not emitted!");
4115 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004116}
4117
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004118void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004119 Record.push_back(GetDeclRef(D));
4120}
4121
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004122DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004123 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4124
Douglas Gregor2cf26342009-04-09 22:27:44 +00004125 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00004126 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004127 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004128
4129 // If D comes from an AST file, its declaration ID is already known and
4130 // fixed.
4131 if (D->isFromASTFile())
4132 return D->getGlobalID();
4133
Douglas Gregor97475832010-10-05 18:37:06 +00004134 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004135 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00004136 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004137 if (DoneWritingDeclsAndTypes) {
4138 assert(0 && "New decl seen after serializing all the decls to emit!");
4139 return 0;
4140 }
4141
Douglas Gregor2cf26342009-04-09 22:27:44 +00004142 // We haven't seen this declaration before. Give it a new ID and
4143 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004144 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00004145 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00004146 }
4147
Sebastian Redl681d7232010-07-27 00:17:23 +00004148 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004149}
4150
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004151DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004152 if (D == 0)
4153 return 0;
4154
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004155 // If D comes from an AST file, its declaration ID is already known and
4156 // fixed.
4157 if (D->isFromASTFile())
4158 return D->getGlobalID();
4159
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004160 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4161 return DeclIDs[D];
4162}
4163
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004164static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4165 std::pair<unsigned, serialization::DeclID> R) {
4166 return L.first < R.first;
4167}
4168
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004169void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004170 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004171 assert(D);
4172
4173 SourceLocation Loc = D->getLocation();
4174 if (Loc.isInvalid())
4175 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004176
4177 // We only keep track of the file-level declarations of each file.
4178 if (!D->getLexicalDeclContext()->isFileContext())
4179 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004180 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4181 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004182 if (isa<ParmVarDecl>(D))
4183 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004184
4185 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004186 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004187 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004188 FileID FID;
4189 unsigned Offset;
4190 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004191 if (FID.isInvalid())
4192 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004193 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004194
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004195 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004196 if (!Info)
4197 Info = new DeclIDInFileInfo();
4198
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004199 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004200 LocDeclIDsTy &Decls = Info->DeclIDs;
4201
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004202 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004203 Decls.push_back(LocDecl);
4204 return;
4205 }
4206
4207 LocDeclIDsTy::iterator
4208 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4209
4210 Decls.insert(I, LocDecl);
4211}
4212
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004213void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004214 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004215 Record.push_back(Name.getNameKind());
4216 switch (Name.getNameKind()) {
4217 case DeclarationName::Identifier:
4218 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4219 break;
4220
4221 case DeclarationName::ObjCZeroArgSelector:
4222 case DeclarationName::ObjCOneArgSelector:
4223 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004224 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004225 break;
4226
4227 case DeclarationName::CXXConstructorName:
4228 case DeclarationName::CXXDestructorName:
4229 case DeclarationName::CXXConversionFunctionName:
4230 AddTypeRef(Name.getCXXNameType(), Record);
4231 break;
4232
4233 case DeclarationName::CXXOperatorName:
4234 Record.push_back(Name.getCXXOverloadedOperator());
4235 break;
4236
Sean Hunt3e518bd2009-11-29 07:34:05 +00004237 case DeclarationName::CXXLiteralOperatorName:
4238 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4239 break;
4240
Douglas Gregor2cf26342009-04-09 22:27:44 +00004241 case DeclarationName::CXXUsingDirective:
4242 // No extra data to emit
4243 break;
4244 }
4245}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004246
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004247void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004248 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004249 switch (Name.getNameKind()) {
4250 case DeclarationName::CXXConstructorName:
4251 case DeclarationName::CXXDestructorName:
4252 case DeclarationName::CXXConversionFunctionName:
4253 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4254 break;
4255
4256 case DeclarationName::CXXOperatorName:
4257 AddSourceLocation(
4258 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4259 Record);
4260 AddSourceLocation(
4261 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4262 Record);
4263 break;
4264
4265 case DeclarationName::CXXLiteralOperatorName:
4266 AddSourceLocation(
4267 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4268 Record);
4269 break;
4270
4271 case DeclarationName::Identifier:
4272 case DeclarationName::ObjCZeroArgSelector:
4273 case DeclarationName::ObjCOneArgSelector:
4274 case DeclarationName::ObjCMultiArgSelector:
4275 case DeclarationName::CXXUsingDirective:
4276 break;
4277 }
4278}
4279
4280void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004281 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004282 AddDeclarationName(NameInfo.getName(), Record);
4283 AddSourceLocation(NameInfo.getLoc(), Record);
4284 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4285}
4286
4287void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004288 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004289 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004290 Record.push_back(Info.NumTemplParamLists);
4291 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4292 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4293}
4294
Sebastian Redla4232eb2010-08-18 23:56:21 +00004295void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004296 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004297 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004298 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004299 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004300
4301 // Push each of the NNS's onto a stack for serialization in reverse order.
4302 while (NNS) {
4303 NestedNames.push_back(NNS);
4304 NNS = NNS->getPrefix();
4305 }
4306
4307 Record.push_back(NestedNames.size());
4308 while(!NestedNames.empty()) {
4309 NNS = NestedNames.pop_back_val();
4310 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4311 Record.push_back(Kind);
4312 switch (Kind) {
4313 case NestedNameSpecifier::Identifier:
4314 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4315 break;
4316
4317 case NestedNameSpecifier::Namespace:
4318 AddDeclRef(NNS->getAsNamespace(), Record);
4319 break;
4320
Douglas Gregor14aba762011-02-24 02:36:08 +00004321 case NestedNameSpecifier::NamespaceAlias:
4322 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4323 break;
4324
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004325 case NestedNameSpecifier::TypeSpec:
4326 case NestedNameSpecifier::TypeSpecWithTemplate:
4327 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4328 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4329 break;
4330
4331 case NestedNameSpecifier::Global:
4332 // Don't need to write an associated value.
4333 break;
4334 }
4335 }
4336}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004337
Douglas Gregordc355712011-02-25 00:36:19 +00004338void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4339 RecordDataImpl &Record) {
4340 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004341 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004342 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004343
4344 // Push each of the nested-name-specifiers's onto a stack for
4345 // serialization in reverse order.
4346 while (NNS) {
4347 NestedNames.push_back(NNS);
4348 NNS = NNS.getPrefix();
4349 }
4350
4351 Record.push_back(NestedNames.size());
4352 while(!NestedNames.empty()) {
4353 NNS = NestedNames.pop_back_val();
4354 NestedNameSpecifier::SpecifierKind Kind
4355 = NNS.getNestedNameSpecifier()->getKind();
4356 Record.push_back(Kind);
4357 switch (Kind) {
4358 case NestedNameSpecifier::Identifier:
4359 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4360 AddSourceRange(NNS.getLocalSourceRange(), Record);
4361 break;
4362
4363 case NestedNameSpecifier::Namespace:
4364 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4365 AddSourceRange(NNS.getLocalSourceRange(), Record);
4366 break;
4367
4368 case NestedNameSpecifier::NamespaceAlias:
4369 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4370 AddSourceRange(NNS.getLocalSourceRange(), Record);
4371 break;
4372
4373 case NestedNameSpecifier::TypeSpec:
4374 case NestedNameSpecifier::TypeSpecWithTemplate:
4375 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4376 AddTypeLoc(NNS.getTypeLoc(), Record);
4377 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4378 break;
4379
4380 case NestedNameSpecifier::Global:
4381 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4382 break;
4383 }
4384 }
4385}
4386
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004387void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004388 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004389 Record.push_back(Kind);
4390 switch (Kind) {
4391 case TemplateName::Template:
4392 AddDeclRef(Name.getAsTemplateDecl(), Record);
4393 break;
4394
4395 case TemplateName::OverloadedTemplate: {
4396 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4397 Record.push_back(OvT->size());
4398 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4399 I != E; ++I)
4400 AddDeclRef(*I, Record);
4401 break;
4402 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004403
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004404 case TemplateName::QualifiedTemplate: {
4405 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4406 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4407 Record.push_back(QualT->hasTemplateKeyword());
4408 AddDeclRef(QualT->getTemplateDecl(), Record);
4409 break;
4410 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004411
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004412 case TemplateName::DependentTemplate: {
4413 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4414 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4415 Record.push_back(DepT->isIdentifier());
4416 if (DepT->isIdentifier())
4417 AddIdentifierRef(DepT->getIdentifier(), Record);
4418 else
4419 Record.push_back(DepT->getOperator());
4420 break;
4421 }
John McCall14606042011-06-30 08:33:18 +00004422
4423 case TemplateName::SubstTemplateTemplateParm: {
4424 SubstTemplateTemplateParmStorage *subst
4425 = Name.getAsSubstTemplateTemplateParm();
4426 AddDeclRef(subst->getParameter(), Record);
4427 AddTemplateName(subst->getReplacement(), Record);
4428 break;
4429 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004430
4431 case TemplateName::SubstTemplateTemplateParmPack: {
4432 SubstTemplateTemplateParmPackStorage *SubstPack
4433 = Name.getAsSubstTemplateTemplateParmPack();
4434 AddDeclRef(SubstPack->getParameterPack(), Record);
4435 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4436 break;
4437 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004438 }
4439}
4440
Michael J. Spencer20249a12010-10-21 03:16:25 +00004441void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004442 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004443 Record.push_back(Arg.getKind());
4444 switch (Arg.getKind()) {
4445 case TemplateArgument::Null:
4446 break;
4447 case TemplateArgument::Type:
4448 AddTypeRef(Arg.getAsType(), Record);
4449 break;
4450 case TemplateArgument::Declaration:
4451 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004452 Record.push_back(Arg.isDeclForReferenceParam());
4453 break;
4454 case TemplateArgument::NullPtr:
4455 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004456 break;
4457 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004458 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004459 AddTypeRef(Arg.getIntegralType(), Record);
4460 break;
4461 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004462 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4463 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004464 case TemplateArgument::TemplateExpansion:
4465 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004466 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4467 Record.push_back(*NumExpansions + 1);
4468 else
4469 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004470 break;
4471 case TemplateArgument::Expression:
4472 AddStmt(Arg.getAsExpr());
4473 break;
4474 case TemplateArgument::Pack:
4475 Record.push_back(Arg.pack_size());
4476 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4477 I != E; ++I)
4478 AddTemplateArgument(*I, Record);
4479 break;
4480 }
4481}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004482
4483void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004484ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004485 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004486 assert(TemplateParams && "No TemplateParams!");
4487 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4488 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4489 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4490 Record.push_back(TemplateParams->size());
4491 for (TemplateParameterList::const_iterator
4492 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4493 P != PEnd; ++P)
4494 AddDeclRef(*P, Record);
4495}
4496
4497/// \brief Emit a template argument list.
4498void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004499ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004500 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004501 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004502 Record.push_back(TemplateArgs->size());
4503 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004504 AddTemplateArgument(TemplateArgs->get(i), Record);
4505}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004506
4507
4508void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004509ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004510 Record.push_back(Set.size());
4511 for (UnresolvedSetImpl::const_iterator
4512 I = Set.begin(), E = Set.end(); I != E; ++I) {
4513 AddDeclRef(I.getDecl(), Record);
4514 Record.push_back(I.getAccess());
4515 }
4516}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004517
Sebastian Redla4232eb2010-08-18 23:56:21 +00004518void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004519 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004520 Record.push_back(Base.isVirtual());
4521 Record.push_back(Base.isBaseOfClass());
4522 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004523 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004524 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004525 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004526 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4527 : SourceLocation(),
4528 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004529}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004530
Douglas Gregor7c789c12010-10-29 22:39:52 +00004531void ASTWriter::FlushCXXBaseSpecifiers() {
4532 RecordData Record;
4533 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4534 Record.clear();
4535
4536 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004537 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004538 if (Index == CXXBaseSpecifiersOffsets.size())
4539 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4540 else {
4541 if (Index > CXXBaseSpecifiersOffsets.size())
4542 CXXBaseSpecifiersOffsets.resize(Index + 1);
4543 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4544 }
4545
4546 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4547 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4548 Record.push_back(BEnd - B);
4549 for (; B != BEnd; ++B)
4550 AddCXXBaseSpecifier(*B, Record);
4551 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004552
4553 // Flush any expressions that were written as part of the base specifiers.
4554 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004555 }
4556
4557 CXXBaseSpecifiersToWrite.clear();
4558}
4559
Sean Huntcbb67482011-01-08 20:30:50 +00004560void ASTWriter::AddCXXCtorInitializers(
4561 const CXXCtorInitializer * const *CtorInitializers,
4562 unsigned NumCtorInitializers,
4563 RecordDataImpl &Record) {
4564 Record.push_back(NumCtorInitializers);
4565 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4566 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004567
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004568 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004569 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004570 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004571 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004572 } else if (Init->isDelegatingInitializer()) {
4573 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004574 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004575 } else if (Init->isMemberInitializer()){
4576 Record.push_back(CTOR_INITIALIZER_MEMBER);
4577 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004578 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004579 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4580 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004581 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004582
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004583 AddSourceLocation(Init->getMemberLocation(), Record);
4584 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004585 AddSourceLocation(Init->getLParenLoc(), Record);
4586 AddSourceLocation(Init->getRParenLoc(), Record);
4587 Record.push_back(Init->isWritten());
4588 if (Init->isWritten()) {
4589 Record.push_back(Init->getSourceOrder());
4590 } else {
4591 Record.push_back(Init->getNumArrayIndices());
4592 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4593 AddDeclRef(Init->getArrayIndex(i), Record);
4594 }
4595 }
4596}
4597
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004598void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4599 assert(D->DefinitionData);
4600 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004601 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004602 Record.push_back(Data.UserDeclaredConstructor);
4603 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004604 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004605 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004606 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004607 Record.push_back(Data.UserDeclaredDestructor);
4608 Record.push_back(Data.Aggregate);
4609 Record.push_back(Data.PlainOldData);
4610 Record.push_back(Data.Empty);
4611 Record.push_back(Data.Polymorphic);
4612 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004613 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004614 Record.push_back(Data.HasNoNonEmptyBases);
4615 Record.push_back(Data.HasPrivateFields);
4616 Record.push_back(Data.HasProtectedFields);
4617 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004618 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004619 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004620 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004621 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004622 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004623 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004624 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004625 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004626 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004627 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004628 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004629 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004630 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004631 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004632 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004633 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004634 Record.push_back(Data.DeclaredDefaultConstructor);
4635 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004636 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004637 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004638 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004639 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004640 Record.push_back(Data.FailedImplicitMoveConstructor);
4641 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004642 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004643
4644 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004645 if (Data.NumBases > 0)
4646 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4647 Record);
4648
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004649 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4650 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004651 if (Data.NumVBases > 0)
4652 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4653 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004654
4655 AddUnresolvedSet(Data.Conversions, Record);
4656 AddUnresolvedSet(Data.VisibleConversions, Record);
4657 // Data.Definition is the owning decl, no need to write it.
4658 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004659
4660 // Add lambda-specific data.
4661 if (Data.IsLambda) {
4662 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004663 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004664 Record.push_back(Lambda.NumCaptures);
4665 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004666 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004667 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004668 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004669 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4670 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4671 AddSourceLocation(Capture.getLocation(), Record);
4672 Record.push_back(Capture.isImplicit());
4673 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4674 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4675 AddDeclRef(Var, Record);
4676 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4677 : SourceLocation(),
4678 Record);
4679 }
4680 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004681}
4682
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004683void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004684 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004685 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004686 assert(FirstDeclID == NextDeclID &&
4687 FirstTypeID == NextTypeID &&
4688 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004689 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004690 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004691 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004692 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004693
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004694 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004695
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004696 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4697 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4698 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004699 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004700 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004701 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004702 NextDeclID = FirstDeclID;
4703 NextTypeID = FirstTypeID;
4704 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004705 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004706 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004707 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004708}
4709
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004710void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004711 IdentifierIDs[II] = ID;
4712}
4713
Douglas Gregora8235d62012-10-09 23:05:51 +00004714void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4715 MacroIDs[MI] = ID;
4716}
4717
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004718void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004719 // Always take the highest-numbered type index. This copes with an interesting
4720 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004721 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004722 // keep the higher-numbered entry so that we can properly write it out to
4723 // the AST file.
4724 TypeIdx &StoredIdx = TypeIdxs[T];
4725 if (Idx.getIndex() >= StoredIdx.getIndex())
4726 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004727}
4728
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004729void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004730 SelectorIDs[S] = ID;
4731}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004732
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004733void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004734 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004735 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004736 MacroDefinitions[MD] = ID;
4737}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004738
Douglas Gregora015cab2011-12-02 17:30:13 +00004739void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4740 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4741 SubmoduleIDs[Mod] = ID;
4742}
4743
Douglas Gregora8235d62012-10-09 23:05:51 +00004744void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4745 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4746}
4747
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004748void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004749 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004750 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004751 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4752 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004753 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004754 // A forward reference was mutated into a definition. Rewrite it.
4755 // FIXME: This happens during template instantiation, should we
4756 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004757 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004758 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004759 }
4760}
Douglas Gregora8235d62012-10-09 23:05:51 +00004761
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004762void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004763 assert(!WritingAST && "Already writing the AST!");
4764
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004765 // TU and namespaces are handled elsewhere.
4766 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4767 return;
4768
Douglas Gregor919814d2011-09-09 23:01:35 +00004769 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004770 return; // Not a source decl added to a DeclContext from PCH.
4771
4772 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004773 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004774}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004775
4776void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004777 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004778 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004779 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004780 return; // Not a source member added to a class from PCH.
4781 if (!isa<CXXMethodDecl>(D))
4782 return; // We are interested in lazily declared implicit methods.
4783
4784 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004785 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004786 UpdateRecord &Record = DeclUpdates[RD];
4787 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004788 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004789}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004790
4791void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4792 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004793 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004794 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004795 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004796 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004797 return; // Not a source specialization added to a template from PCH.
4798
4799 UpdateRecord &Record = DeclUpdates[TD];
4800 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004801 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004802}
Douglas Gregor89d99802010-11-30 06:16:57 +00004803
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004804void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4805 const FunctionDecl *D) {
4806 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004807 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004808 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004809 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004810 return; // Not a source specialization added to a template from PCH.
4811
4812 UpdateRecord &Record = DeclUpdates[TD];
4813 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004814 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004815}
4816
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004817void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004818 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004819 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004820 return; // Declaration not imported from PCH.
4821
4822 // Implicit decl from a PCH was defined.
4823 // FIXME: Should implicit definition be a separate FunctionDecl?
4824 RewriteDecl(D);
4825}
4826
Sebastian Redlf79a7192011-04-29 08:19:30 +00004827void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004828 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004829 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004830 return;
4831
4832 // Since the actual instantiation is delayed, this really means that we need
4833 // to update the instantiation location.
4834 UpdateRecord &Record = DeclUpdates[D];
4835 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4836 AddSourceLocation(
4837 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4838}
4839
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004840void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4841 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004842 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004843 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004844 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004845
4846 assert(IFD->getDefinition() && "Category on a class without a definition?");
4847 ObjCClassesWithCategories.insert(
4848 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004849}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004850
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004851
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004852void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4853 const ObjCPropertyDecl *OrigProp,
4854 const ObjCCategoryDecl *ClassExt) {
4855 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4856 if (!D)
4857 return;
4858
4859 assert(!WritingAST && "Already writing the AST!");
4860 if (!D->isFromASTFile())
4861 return; // Declaration not imported from PCH.
4862
4863 RewriteDecl(D);
4864}