blob: 28f78c61810995a8e5a2ee5f721f452773f079da [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"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000053
Sebastian Redlade50002010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000059}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000065}
66
Douglas Gregor2cf26342009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000070
Douglas Gregor2cf26342009-04-09 22:27:44 +000071namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000079
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl3397c552010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikieb219cfc2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregor2cf26342009-04-09 22:27:44 +000095}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110}
111
Sebastian Redl3397c552010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000116}
117
Sebastian Redl3397c552010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133}
134
Sebastian Redl3397c552010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159}
160
Sebastian Redl3397c552010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000175}
176
Sebastian Redl3397c552010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
Richard Smitheefb3d52012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith7bb698a2012-04-21 17:47:47 +0000198 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
199 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
200 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithb9d0b762012-07-27 04:22:15 +0000201 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redl60618fa2011-03-12 11:50:43 +0000203 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205}
206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000209 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000210}
John McCalled976492009-12-04 22:46:56 +0000211
Sebastian Redl3397c552010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000213 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000214 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
215 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000220 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227}
228
Sebastian Redl3397c552010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregorf8af9822012-02-12 18:42:33 +0000230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson395b4752009-06-24 19:06:50 +0000231 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000232 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000233}
234
Sean Huntca63c202011-05-24 22:41:36 +0000235void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
236 Writer.AddTypeRef(T->getBaseType(), Record);
237 Writer.AddTypeRef(T->getUnderlyingType(), Record);
238 Record.push_back(T->getUTTKind());
239 Code = TYPE_UNARY_TRANSFORM;
240}
241
Richard Smith34b41d92011-02-20 03:19:35 +0000242void ASTTypeWriter::VisitAutoType(const AutoType *T) {
243 Writer.AddTypeRef(T->getDeducedType(), Record);
244 Code = TYPE_AUTO;
245}
246
Sebastian Redl3397c552010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000248 Record.push_back(T->isDependentType());
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000249 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000250 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251 "Cannot serialize in the middle of a type definition");
252}
253
Sebastian Redl3397c552010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000256 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000257}
258
Sebastian Redl3397c552010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000260 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000261 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000262}
263
John McCall9d156a72011-01-06 01:58:22 +0000264void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
265 Writer.AddTypeRef(T->getModifiedType(), Record);
266 Writer.AddTypeRef(T->getEquivalentType(), Record);
267 Record.push_back(T->getAttrKind());
268 Code = TYPE_ATTRIBUTED;
269}
270
Mike Stump1eb44332009-09-09 15:08:12 +0000271void
Sebastian Redl3397c552010-08-18 23:56:27 +0000272ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000273 const SubstTemplateTypeParmType *T) {
274 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
275 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000276 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000277}
278
279void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000280ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
281 const SubstTemplateTypeParmPackType *T) {
282 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
283 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
284 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
285}
286
287void
Sebastian Redl3397c552010-08-18 23:56:27 +0000288ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000289 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000290 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000291 Writer.AddTemplateName(T->getTemplateName(), Record);
292 Record.push_back(T->getNumArgs());
293 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
294 ArgI != ArgE; ++ArgI)
295 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000296 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
297 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000298 : T->getCanonicalTypeInternal(),
299 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000300 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000301}
302
303void
Sebastian Redl3397c552010-08-18 23:56:27 +0000304ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000305 VisitArrayType(T);
306 Writer.AddStmt(T->getSizeExpr());
307 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 const DependentSizedExtVectorType *T) {
314 // FIXME: Serialize this type (C++ only)
David Blaikieb219cfc2011-09-23 05:06:16 +0000315 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl3397c552010-08-18 23:56:27 +0000319ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000320 Record.push_back(T->getDepth());
321 Record.push_back(T->getIndex());
322 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000323 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000324 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000325}
326
327void
Sebastian Redl3397c552010-08-18 23:56:27 +0000328ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000329 Record.push_back(T->getKeyword());
330 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
331 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000332 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
333 : T->getCanonicalTypeInternal(),
334 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000335 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000336}
337
338void
Sebastian Redl3397c552010-08-18 23:56:27 +0000339ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000340 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000341 Record.push_back(T->getKeyword());
342 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
343 Writer.AddIdentifierRef(T->getIdentifier(), Record);
344 Record.push_back(T->getNumArgs());
345 for (DependentTemplateSpecializationType::iterator
346 I = T->begin(), E = T->end(); I != E; ++I)
347 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000349}
350
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
352 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000353 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
354 Record.push_back(*NumExpansions + 1);
355 else
356 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000357 Code = TYPE_PACK_EXPANSION;
358}
359
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000360void ASTTypeWriter::VisitParenType(const ParenType *T) {
361 Writer.AddTypeRef(T->getInnerType(), Record);
362 Code = TYPE_PAREN;
363}
364
Sebastian Redl3397c552010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000366 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregora8e0b972012-03-26 15:52:37 +0000373 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000374 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000375 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000376}
377
Sebastian Redl3397c552010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor56ca8a92012-01-17 19:21:53 +0000379 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000381}
382
Sebastian Redl3397c552010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000384 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000385 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000386 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000387 E = T->qual_end(); I != E; ++I)
388 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000392void
Sebastian Redl3397c552010-08-18 23:56:27 +0000393ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000394 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000395 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000396}
397
Eli Friedmanb001de72011-10-06 23:00:33 +0000398void
399ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
400 Writer.AddTypeRef(T->getValueType(), Record);
401 Code = TYPE_ATOMIC;
402}
403
John McCalla1ee0c52009-10-16 21:56:05 +0000404namespace {
405
406class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000407 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000408 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000409
410public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000411 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000412 : Writer(Writer), Record(Record) { }
413
John McCall51bd8032009-10-18 01:05:36 +0000414#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000415#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000417#include "clang/AST/TypeLocNodes.def"
418
John McCall51bd8032009-10-18 01:05:36 +0000419 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
420 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000421};
422
423}
424
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
426 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000429 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
430 if (TL.needsExtraLocalData()) {
431 Record.push_back(TL.getWrittenTypeSpec());
432 Record.push_back(TL.getWrittenSignSpec());
433 Record.push_back(TL.getWrittenWidthSpec());
434 Record.push_back(TL.hasModeAttr());
435 }
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000442}
John McCall51bd8032009-10-18 01:05:36 +0000443void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000451}
John McCall51bd8032009-10-18 01:05:36 +0000452void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000454 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
458 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
459 Record.push_back(TL.getSizeExpr() ? 1 : 0);
460 if (TL.getSizeExpr())
461 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000462}
John McCall51bd8032009-10-18 01:05:36 +0000463void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
464 VisitArrayTypeLoc(TL);
465}
466void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
473 DependentSizedArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
477 DependentSizedExtVectorTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000487 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnara59c0a812012-10-04 21:42:10 +0000488 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
489 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnara796aa442011-03-12 11:17:06 +0000490 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000491 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
492 Writer.AddDeclRef(TL.getArg(i), Record);
493}
494void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
495 VisitFunctionTypeLoc(TL);
496}
497void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
498 VisitFunctionTypeLoc(TL);
499}
John McCalled976492009-12-04 22:46:56 +0000500void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
John McCall51bd8032009-10-18 01:05:36 +0000503void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
506void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000507 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
508 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
509 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000510}
511void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000512 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
513 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
514 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
515 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000516}
517void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
Sean Huntca63c202011-05-24 22:41:36 +0000520void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getKWLoc(), Record);
522 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
525}
Richard Smith34b41d92011-02-20 03:19:35 +0000526void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
John McCall51bd8032009-10-18 01:05:36 +0000529void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
532void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
John McCall9d156a72011-01-06 01:58:22 +0000535void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
537 if (TL.hasAttrOperand()) {
538 SourceRange range = TL.getAttrOperandParensRange();
539 Writer.AddSourceLocation(range.getBegin(), Record);
540 Writer.AddSourceLocation(range.getEnd(), Record);
541 }
542 if (TL.hasAttrExprOperand()) {
543 Expr *operand = TL.getAttrExprOperand();
544 Record.push_back(operand ? 1 : 0);
545 if (operand) Writer.AddStmt(operand);
546 } else if (TL.hasAttrEnumOperand()) {
547 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
548 }
549}
John McCall51bd8032009-10-18 01:05:36 +0000550void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
551 Writer.AddSourceLocation(TL.getNameLoc(), Record);
552}
John McCall49a832b2009-10-18 09:09:24 +0000553void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
554 SubstTemplateTypeParmTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000557void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
558 SubstTemplateTypeParmPackTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getNameLoc(), Record);
560}
John McCall51bd8032009-10-18 01:05:36 +0000561void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
562 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000563 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall833ca992009-10-29 08:12:44 +0000564 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
565 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
566 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
567 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000568 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
569 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000570}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000571void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
573 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
574}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000575void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000576 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000577 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000578}
John McCall3cb0ebd2010-03-10 03:28:59 +0000579void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
580 Writer.AddSourceLocation(TL.getNameLoc(), Record);
581}
Douglas Gregor4714c122010-03-31 17:34:00 +0000582void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara38a42912012-02-06 19:09:27 +0000583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
586}
John McCall33500952010-06-11 00:33:02 +0000587void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
588 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000589 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000590 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnara66581d42012-02-06 22:45:07 +0000591 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara55d23c92012-02-06 14:41:24 +0000592 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000593 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
594 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
595 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000596 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
597 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000598}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000599void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
600 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
601}
John McCall51bd8032009-10-18 01:05:36 +0000602void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
603 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000604}
605void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
606 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000607 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
608 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
609 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
610 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000611}
John McCall54e14c42009-10-22 22:37:11 +0000612void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
613 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000614}
Eli Friedmanb001de72011-10-06 23:00:33 +0000615void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
616 Writer.AddSourceLocation(TL.getKWLoc(), Record);
617 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
618 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
619}
John McCalla1ee0c52009-10-16 21:56:05 +0000620
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000621//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000622// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000623//===----------------------------------------------------------------------===//
624
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000625static void EmitBlockID(unsigned ID, const char *Name,
626 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000627 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000628 Record.clear();
629 Record.push_back(ID);
630 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
631
632 // Emit the block name if present.
633 if (Name == 0 || Name[0] == 0) return;
634 Record.clear();
635 while (*Name)
636 Record.push_back(*Name++);
637 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
638}
639
640static void EmitRecordID(unsigned ID, const char *Name,
641 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000642 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 Record.clear();
644 Record.push_back(ID);
645 while (*Name)
646 Record.push_back(*Name++);
647 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000648}
649
650static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000651 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000652#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000653 RECORD(STMT_STOP);
654 RECORD(STMT_NULL_PTR);
655 RECORD(STMT_NULL);
656 RECORD(STMT_COMPOUND);
657 RECORD(STMT_CASE);
658 RECORD(STMT_DEFAULT);
659 RECORD(STMT_LABEL);
Richard Smith534986f2012-04-14 00:33:13 +0000660 RECORD(STMT_ATTRIBUTED);
Chris Lattner0558df22009-04-27 00:49:53 +0000661 RECORD(STMT_IF);
662 RECORD(STMT_SWITCH);
663 RECORD(STMT_WHILE);
664 RECORD(STMT_DO);
665 RECORD(STMT_FOR);
666 RECORD(STMT_GOTO);
667 RECORD(STMT_INDIRECT_GOTO);
668 RECORD(STMT_CONTINUE);
669 RECORD(STMT_BREAK);
670 RECORD(STMT_RETURN);
671 RECORD(STMT_DECL);
Chad Rosierdf5faf52012-08-25 00:11:56 +0000672 RECORD(STMT_GCCASM);
Chad Rosiercd518a02012-08-24 23:51:02 +0000673 RECORD(STMT_MSASM);
Chris Lattner0558df22009-04-27 00:49:53 +0000674 RECORD(EXPR_PREDEFINED);
675 RECORD(EXPR_DECL_REF);
676 RECORD(EXPR_INTEGER_LITERAL);
677 RECORD(EXPR_FLOATING_LITERAL);
678 RECORD(EXPR_IMAGINARY_LITERAL);
679 RECORD(EXPR_STRING_LITERAL);
680 RECORD(EXPR_CHARACTER_LITERAL);
681 RECORD(EXPR_PAREN);
682 RECORD(EXPR_UNARY_OPERATOR);
683 RECORD(EXPR_SIZEOF_ALIGN_OF);
684 RECORD(EXPR_ARRAY_SUBSCRIPT);
685 RECORD(EXPR_CALL);
686 RECORD(EXPR_MEMBER);
687 RECORD(EXPR_BINARY_OPERATOR);
688 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
689 RECORD(EXPR_CONDITIONAL_OPERATOR);
690 RECORD(EXPR_IMPLICIT_CAST);
691 RECORD(EXPR_CSTYLE_CAST);
692 RECORD(EXPR_COMPOUND_LITERAL);
693 RECORD(EXPR_EXT_VECTOR_ELEMENT);
694 RECORD(EXPR_INIT_LIST);
695 RECORD(EXPR_DESIGNATED_INIT);
696 RECORD(EXPR_IMPLICIT_VALUE_INIT);
697 RECORD(EXPR_VA_ARG);
698 RECORD(EXPR_ADDR_LABEL);
699 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000700 RECORD(EXPR_CHOOSE);
701 RECORD(EXPR_GNU_NULL);
702 RECORD(EXPR_SHUFFLE_VECTOR);
703 RECORD(EXPR_BLOCK);
Peter Collingbournef111d932011-04-15 00:35:48 +0000704 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000705 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beardeb382ec2012-04-19 00:25:12 +0000706 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000707 RECORD(EXPR_OBJC_ARRAY_LITERAL);
708 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000709 RECORD(EXPR_OBJC_ENCODE);
710 RECORD(EXPR_OBJC_SELECTOR_EXPR);
711 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
712 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
713 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
714 RECORD(EXPR_OBJC_KVC_REF_EXPR);
715 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000716 RECORD(STMT_OBJC_FOR_COLLECTION);
717 RECORD(STMT_OBJC_CATCH);
718 RECORD(STMT_OBJC_FINALLY);
719 RECORD(STMT_OBJC_AT_TRY);
720 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
721 RECORD(STMT_OBJC_AT_THROW);
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000722 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000723 RECORD(EXPR_CXX_OPERATOR_CALL);
724 RECORD(EXPR_CXX_CONSTRUCT);
725 RECORD(EXPR_CXX_STATIC_CAST);
726 RECORD(EXPR_CXX_DYNAMIC_CAST);
727 RECORD(EXPR_CXX_REINTERPRET_CAST);
728 RECORD(EXPR_CXX_CONST_CAST);
729 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smith9fcce652012-03-07 08:35:16 +0000730 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000731 RECORD(EXPR_CXX_BOOL_LITERAL);
732 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000733 RECORD(EXPR_CXX_TYPEID_EXPR);
734 RECORD(EXPR_CXX_TYPEID_TYPE);
735 RECORD(EXPR_CXX_UUIDOF_EXPR);
736 RECORD(EXPR_CXX_UUIDOF_TYPE);
737 RECORD(EXPR_CXX_THIS);
738 RECORD(EXPR_CXX_THROW);
739 RECORD(EXPR_CXX_DEFAULT_ARG);
740 RECORD(EXPR_CXX_BIND_TEMPORARY);
741 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
742 RECORD(EXPR_CXX_NEW);
743 RECORD(EXPR_CXX_DELETE);
744 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
745 RECORD(EXPR_EXPR_WITH_CLEANUPS);
746 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
747 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
748 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
749 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
750 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
751 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
752 RECORD(EXPR_CXX_NOEXCEPT);
753 RECORD(EXPR_OPAQUE_VALUE);
754 RECORD(EXPR_BINARY_TYPE_TRAIT);
755 RECORD(EXPR_PACK_EXPANSION);
756 RECORD(EXPR_SIZEOF_PACK);
757 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000758 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000759#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000760}
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Sebastian Redla4232eb2010-08-18 23:56:21 +0000762void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000763 RecordData Record;
764 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000766#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
767#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Sebastian Redl3397c552010-08-18 23:56:27 +0000769 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000770 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000771 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000772 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000773 RECORD(TYPE_OFFSET);
774 RECORD(DECL_OFFSET);
775 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000776 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000777 RECORD(IDENTIFIER_OFFSET);
778 RECORD(IDENTIFIER_TABLE);
779 RECORD(EXTERNAL_DEFINITIONS);
780 RECORD(SPECIAL_TYPES);
781 RECORD(STATISTICS);
782 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000783 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000784 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
785 RECORD(SELECTOR_OFFSETS);
786 RECORD(METHOD_POOL);
787 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000788 RECORD(SOURCE_LOCATION_OFFSETS);
789 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000790 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000791 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000792 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +0000793 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregore95b9192011-08-17 21:07:30 +0000794 RECORD(IMPORTS);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000795 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000796 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000797 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000798 RECORD(SEMA_DECL_REFS);
799 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
800 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
801 RECORD(DECL_REPLACEMENTS);
802 RECORD(UPDATE_VISIBLE);
803 RECORD(DECL_UPDATE_OFFSETS);
804 RECORD(DECL_UPDATES);
805 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
806 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000807 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000808 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000809 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000810 RECORD(FP_PRAGMA_OPTIONS);
811 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000812 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000813 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
814 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000815 RECORD(MODULE_OFFSET_MAP);
816 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000817 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregora1266512011-12-19 21:09:25 +0000818 RECORD(FILE_SORTED_DECLS);
819 RECORD(IMPORTED_MODULES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000820 RECORD(MERGED_DECLARATIONS);
821 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregorcff9f262012-01-27 01:47:08 +0000822 RECORD(OBJC_CATEGORIES);
Douglas Gregora8235d62012-10-09 23:05:51 +0000823 RECORD(MACRO_OFFSET);
824 RECORD(MACRO_UPDATES);
Douglas Gregor2171bf12012-01-15 16:58:34 +0000825
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000826 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000827 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000828 RECORD(SM_SLOC_FILE_ENTRY);
829 RECORD(SM_SLOC_BUFFER_ENTRY);
830 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000831 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000833 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000834 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000835 RECORD(PP_MACRO_OBJECT_LIKE);
836 RECORD(PP_MACRO_FUNCTION_LIKE);
837 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000838
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000839 // Decls and Types block.
840 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000841 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000842 RECORD(TYPE_COMPLEX);
843 RECORD(TYPE_POINTER);
844 RECORD(TYPE_BLOCK_POINTER);
845 RECORD(TYPE_LVALUE_REFERENCE);
846 RECORD(TYPE_RVALUE_REFERENCE);
847 RECORD(TYPE_MEMBER_POINTER);
848 RECORD(TYPE_CONSTANT_ARRAY);
849 RECORD(TYPE_INCOMPLETE_ARRAY);
850 RECORD(TYPE_VARIABLE_ARRAY);
851 RECORD(TYPE_VECTOR);
852 RECORD(TYPE_EXT_VECTOR);
853 RECORD(TYPE_FUNCTION_PROTO);
854 RECORD(TYPE_FUNCTION_NO_PROTO);
855 RECORD(TYPE_TYPEDEF);
856 RECORD(TYPE_TYPEOF_EXPR);
857 RECORD(TYPE_TYPEOF);
858 RECORD(TYPE_RECORD);
859 RECORD(TYPE_ENUM);
860 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000861 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000862 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000863 RECORD(TYPE_DECLTYPE);
864 RECORD(TYPE_ELABORATED);
865 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
866 RECORD(TYPE_UNRESOLVED_USING);
867 RECORD(TYPE_INJECTED_CLASS_NAME);
868 RECORD(TYPE_OBJC_OBJECT);
869 RECORD(TYPE_TEMPLATE_TYPE_PARM);
870 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
871 RECORD(TYPE_DEPENDENT_NAME);
872 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
873 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
874 RECORD(TYPE_PAREN);
875 RECORD(TYPE_PACK_EXPANSION);
876 RECORD(TYPE_ATTRIBUTED);
877 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedmanb001de72011-10-06 23:00:33 +0000878 RECORD(TYPE_ATOMIC);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000879 RECORD(DECL_TYPEDEF);
880 RECORD(DECL_ENUM);
881 RECORD(DECL_RECORD);
882 RECORD(DECL_ENUM_CONSTANT);
883 RECORD(DECL_FUNCTION);
884 RECORD(DECL_OBJC_METHOD);
885 RECORD(DECL_OBJC_INTERFACE);
886 RECORD(DECL_OBJC_PROTOCOL);
887 RECORD(DECL_OBJC_IVAR);
888 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000889 RECORD(DECL_OBJC_CATEGORY);
890 RECORD(DECL_OBJC_CATEGORY_IMPL);
891 RECORD(DECL_OBJC_IMPLEMENTATION);
892 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
893 RECORD(DECL_OBJC_PROPERTY);
894 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000895 RECORD(DECL_FIELD);
896 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000897 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000898 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000899 RECORD(DECL_FILE_SCOPE_ASM);
900 RECORD(DECL_BLOCK);
901 RECORD(DECL_CONTEXT_LEXICAL);
902 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000903 RECORD(DECL_NAMESPACE);
904 RECORD(DECL_NAMESPACE_ALIAS);
905 RECORD(DECL_USING);
906 RECORD(DECL_USING_SHADOW);
907 RECORD(DECL_USING_DIRECTIVE);
908 RECORD(DECL_UNRESOLVED_USING_VALUE);
909 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
910 RECORD(DECL_LINKAGE_SPEC);
911 RECORD(DECL_CXX_RECORD);
912 RECORD(DECL_CXX_METHOD);
913 RECORD(DECL_CXX_CONSTRUCTOR);
914 RECORD(DECL_CXX_DESTRUCTOR);
915 RECORD(DECL_CXX_CONVERSION);
916 RECORD(DECL_ACCESS_SPEC);
917 RECORD(DECL_FRIEND);
918 RECORD(DECL_FRIEND_TEMPLATE);
919 RECORD(DECL_CLASS_TEMPLATE);
920 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
921 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
922 RECORD(DECL_FUNCTION_TEMPLATE);
923 RECORD(DECL_TEMPLATE_TYPE_PARM);
924 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
925 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
926 RECORD(DECL_STATIC_ASSERT);
927 RECORD(DECL_CXX_BASE_SPECIFIERS);
928 RECORD(DECL_INDIRECTFIELD);
929 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
930
Douglas Gregora72d8c42011-06-03 02:27:19 +0000931 // Statements and Exprs can occur in the Decls and Types block.
932 AddStmtsExprs(Stream, Record);
933
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000934 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000935 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000936 RECORD(PPD_MACRO_DEFINITION);
937 RECORD(PPD_INCLUSION_DIRECTIVE);
938
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000939#undef RECORD
940#undef BLOCK
941 Stream.ExitBlock();
942}
943
Douglas Gregore650c8c2009-07-07 00:12:59 +0000944/// \brief Adjusts the given filename to only write out the portion of the
945/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000946///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000947/// \param Filename the file name to adjust.
948///
949/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
950/// the returned filename will be adjusted by this system root.
951///
952/// \returns either the original filename (if it needs no adjustment) or the
953/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000954static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000955adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor832d6202011-07-22 16:35:34 +0000958 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000959 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregore650c8c2009-07-07 00:12:59 +0000961 // Verify that the filename and the system root have the same prefix.
962 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000963 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 if (Filename[Pos] != isysroot[Pos])
965 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 // We hit the end of the filename before we hit the end of the system root.
968 if (!Filename[Pos])
969 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 // If the file name has a '/' at the current position, skip over the '/'.
972 // We distinguish sysroot-based includes from absolute includes by the
973 // absence of '/' at the beginning of sysroot-based includes.
974 if (Filename[Pos] == '/')
975 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Douglas Gregore650c8c2009-07-07 00:12:59 +0000977 return Filename + Pos;
978}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000979
Sebastian Redl3397c552010-08-18 23:56:27 +0000980/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000981void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000982 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000983 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000984
Douglas Gregore650c8c2009-07-07 00:12:59 +0000985 // Metadata
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000986 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000987 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregore95b9192011-08-17 21:07:30 +0000988 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
990 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000991 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
992 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
993 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000994 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregore95b9192011-08-17 21:07:30 +0000995 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregore650c8c2009-07-07 00:12:59 +0000996 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Douglas Gregore650c8c2009-07-07 00:12:59 +0000998 RecordData Record;
Douglas Gregore95b9192011-08-17 21:07:30 +0000999 Record.push_back(METADATA);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001000 Record.push_back(VERSION_MAJOR);
1001 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001002 Record.push_back(CLANG_VERSION_MAJOR);
1003 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +00001004 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00001005 Record.push_back(ASTHasCompilerErrors);
Douglas Gregore95b9192011-08-17 21:07:30 +00001006 const std::string &Triple = Target.getTriple().getTriple();
1007 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1008
1009 if (Chain) {
Douglas Gregore95b9192011-08-17 21:07:30 +00001010 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1011 llvm::SmallVector<char, 128> ModulePaths;
1012 Record.clear();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00001013
1014 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1015 M != MEnd; ++M) {
1016 // Skip modules that weren't directly imported.
1017 if (!(*M)->isDirectlyImported())
1018 continue;
1019
1020 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1021 // FIXME: Write import location, once it matters.
1022 // FIXME: This writes the absolute path for AST files we depend on.
1023 const std::string &FileName = (*M)->FileName;
1024 Record.push_back(FileName.size());
1025 Record.append(FileName.begin(), FileName.end());
1026 }
Douglas Gregore95b9192011-08-17 21:07:30 +00001027 Stream.EmitRecord(IMPORTS, Record);
1028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Douglas Gregor31d375f2011-05-06 21:43:30 +00001030 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +00001031 SourceManager &SM = Context.getSourceManager();
1032 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1033 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001034 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +00001035 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1036 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1037
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001038 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001040 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001041
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001042 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001043 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001044 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001045 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001046 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001047 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +00001048
1049 Record.clear();
1050 Record.push_back(SM.getMainFileID().getOpaqueValue());
1051 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001052 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001053
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001054 // Original PCH directory
1055 if (!OutputFile.empty() && OutputFile != "-") {
1056 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1057 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1059 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1060
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001061 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001062
1063 llvm::sys::fs::make_absolute(OutputPath);
1064 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1065
1066 RecordData Record;
1067 Record.push_back(ORIGINAL_PCH_DIR);
1068 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1069 }
1070
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001071 // Repository branch/version information.
1072 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001073 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001074 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1075 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001076 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001077 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001078 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1079 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001080}
1081
1082/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001083void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001084 RecordData Record;
Douglas Gregor7d5e81b2011-09-13 18:26:39 +00001085#define LANGOPT(Name, Bits, Default, Description) \
1086 Record.push_back(LangOpts.Name);
1087#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1088 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1089#include "clang/Basic/LangOptions.def"
John McCall260611a2012-06-20 06:18:46 +00001090
1091 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1092 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregorb86b8dc2011-11-15 19:35:01 +00001093
1094 Record.push_back(LangOpts.CurrentModule.size());
1095 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001096 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001097}
1098
Douglas Gregor14f79002009-04-10 03:52:48 +00001099//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100// stat cache Serialization
1101//===----------------------------------------------------------------------===//
1102
1103namespace {
1104// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001105class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001106public:
1107 typedef const char * key_type;
1108 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner74e976b2010-11-23 19:28:12 +00001110 typedef struct stat data_type;
1111 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112
1113 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001114 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
1117 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001118 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001119 data_type_ref Data) {
1120 unsigned StrLen = strlen(path);
1121 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001122 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001123 clang::io::Emit8(Out, DataLen);
1124 return std::make_pair(StrLen + 1, DataLen);
1125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Chris Lattner5f9e2722011-07-23 10:55:15 +00001127 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 Out.write(path, KeyLen);
1129 }
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Chris Lattner5f9e2722011-07-23 10:55:15 +00001131 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001132 data_type_ref Data, unsigned DataLen) {
1133 using namespace clang::io;
1134 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner74e976b2010-11-23 19:28:12 +00001136 Emit32(Out, (uint32_t) Data.st_ino);
1137 Emit32(Out, (uint32_t) Data.st_dev);
1138 Emit16(Out, (uint16_t) Data.st_mode);
1139 Emit64(Out, (uint64_t) Data.st_mtime);
1140 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141
1142 assert(Out.tell() - Start == DataLen && "Wrong data length");
1143 }
1144};
1145} // end anonymous namespace
1146
Sebastian Redl3397c552010-08-18 23:56:27 +00001147/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001148void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001149 // Build the on-disk hash table containing information about every
1150 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001151 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001152 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001153 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001155 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001156 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001157 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001160 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001161 SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001162 uint32_t BucketOffset;
1163 {
1164 llvm::raw_svector_ostream Out(StatCacheData);
1165 // Make sure that no bucket is at offset 0
1166 clang::io::Emit32(Out, 0);
1167 BucketOffset = Generator.Emit(Out);
1168 }
1169
1170 // Create a blob abbreviation
1171 using namespace llvm;
1172 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001173 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1177 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1178
1179 // Write the stat cache
1180 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001181 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001182 Record.push_back(BucketOffset);
1183 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001184 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001185}
1186
1187//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001188// Source Manager Serialization
1189//===----------------------------------------------------------------------===//
1190
1191/// \brief Create an abbreviation for the SLocEntry that refers to a
1192/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001193static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001194 using namespace llvm;
1195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001196 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001201 // FileEntry fields.
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora081da52011-11-16 20:05:18 +00001204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregor14f79002009-04-10 03:52:48 +00001208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001209 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001210}
1211
1212/// \brief Create an abbreviation for the SLocEntry that refers to a
1213/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001214static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001215 using namespace llvm;
1216 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001217 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001223 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001224}
1225
1226/// \brief Create an abbreviation for the SLocEntry that refers to a
1227/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001228static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001229 using namespace llvm;
1230 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001231 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001233 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001234}
1235
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001236/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1237/// expansion.
1238static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001239 using namespace llvm;
1240 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001241 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001247 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001248}
1249
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001250namespace {
1251 // Trait used for the on-disk hash table of header search information.
1252 class HeaderFileInfoTrait {
1253 ASTWriter &Writer;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001254
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001255 // Keep track of the framework names we've used during serialization.
1256 SmallVector<char, 128> FrameworkStringData;
1257 llvm::StringMap<unsigned> FrameworkNameOffset;
1258
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001259 public:
Benjamin Kramerfacde172012-06-06 17:32:50 +00001260 HeaderFileInfoTrait(ASTWriter &Writer)
1261 : Writer(Writer) { }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001262
1263 typedef const char *key_type;
1264 typedef key_type key_type_ref;
1265
1266 typedef HeaderFileInfo data_type;
1267 typedef const data_type &data_type_ref;
1268
1269 static unsigned ComputeHash(const char *path) {
1270 // The hash is based only on the filename portion of the key, so that the
1271 // reader can match based on filenames when symlinking or excess path
1272 // elements ("foo/../", "../") change the form of the name. However,
1273 // complete path is still the key.
1274 return llvm::HashString(llvm::sys::path::filename(path));
1275 }
1276
1277 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001278 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001279 data_type_ref Data) {
1280 unsigned StrLen = strlen(path);
1281 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001282 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001283 clang::io::Emit8(Out, DataLen);
1284 return std::make_pair(StrLen + 1, DataLen);
1285 }
1286
Chris Lattner5f9e2722011-07-23 10:55:15 +00001287 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001288 Out.write(path, KeyLen);
1289 }
1290
Chris Lattner5f9e2722011-07-23 10:55:15 +00001291 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001292 data_type_ref Data, unsigned DataLen) {
1293 using namespace clang::io;
1294 uint64_t Start = Out.tell(); (void)Start;
1295
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001296 unsigned char Flags = (Data.isImport << 5)
1297 | (Data.isPragmaOnce << 4)
1298 | (Data.DirInfo << 2)
1299 | (Data.Resolved << 1)
1300 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001301 Emit8(Out, (uint8_t)Flags);
1302 Emit16(Out, (uint16_t) Data.NumIncludes);
1303
1304 if (!Data.ControllingMacro)
1305 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1306 else
1307 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001308
1309 unsigned Offset = 0;
1310 if (!Data.Framework.empty()) {
1311 // If this header refers into a framework, save the framework name.
1312 llvm::StringMap<unsigned>::iterator Pos
1313 = FrameworkNameOffset.find(Data.Framework);
1314 if (Pos == FrameworkNameOffset.end()) {
1315 Offset = FrameworkStringData.size() + 1;
1316 FrameworkStringData.append(Data.Framework.begin(),
1317 Data.Framework.end());
1318 FrameworkStringData.push_back(0);
1319
1320 FrameworkNameOffset[Data.Framework] = Offset;
1321 } else
1322 Offset = Pos->second;
1323 }
1324 Emit32(Out, Offset);
1325
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001326 assert(Out.tell() - Start == DataLen && "Wrong data length");
1327 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001328
1329 const char *strings_begin() const { return FrameworkStringData.begin(); }
1330 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001331 };
1332} // end anonymous namespace
1333
1334/// \brief Write the header search block for the list of files that
1335///
1336/// \param HS The header search structure to save.
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001337void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001338 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001339 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1340
1341 if (FilesByUID.size() > HS.header_file_size())
1342 FilesByUID.resize(HS.header_file_size());
1343
Benjamin Kramerfacde172012-06-06 17:32:50 +00001344 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001345 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001346 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001347 unsigned NumHeaderSearchEntries = 0;
1348 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1349 const FileEntry *File = FilesByUID[UID];
1350 if (!File)
1351 continue;
1352
Argyrios Kyrtzidis590ad932011-11-13 22:08:39 +00001353 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1354 // from the external source if it was not provided already.
1355 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001356 if (HFI.External && Chain)
1357 continue;
1358
1359 // Turn the file name into an absolute path, if it isn't already.
1360 const char *Filename = File->getName();
1361 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1362
1363 // If we performed any translation on the file name at all, we need to
1364 // save this string, since the generator will refer to it later.
1365 if (Filename != File->getName()) {
1366 Filename = strdup(Filename);
1367 SavedStrings.push_back(Filename);
1368 }
1369
1370 Generator.insert(Filename, HFI, GeneratorTrait);
1371 ++NumHeaderSearchEntries;
1372 }
1373
1374 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001375 SmallString<4096> TableData;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001376 uint32_t BucketOffset;
1377 {
1378 llvm::raw_svector_ostream Out(TableData);
1379 // Make sure that no bucket is at offset 0
1380 clang::io::Emit32(Out, 0);
1381 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1382 }
1383
1384 // Create a blob abbreviation
1385 using namespace llvm;
1386 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1387 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1388 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1389 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001390 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001391 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1392 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1393
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001394 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001395 RecordData Record;
1396 Record.push_back(HEADER_SEARCH_TABLE);
1397 Record.push_back(BucketOffset);
1398 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001399 Record.push_back(TableData.size());
1400 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001401 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1402
1403 // Free all of the strings we had to duplicate.
1404 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1405 free((void*)SavedStrings[I]);
1406}
1407
Douglas Gregor14f79002009-04-10 03:52:48 +00001408/// \brief Writes the block containing the serialized form of the
1409/// source manager.
1410///
1411/// TODO: We should probably use an on-disk hash table (stored in a
1412/// blob), indexed based on the file name, so that we only create
1413/// entries for files that we actually need. In the common case (no
1414/// errors), we probably won't have to create file entries for any of
1415/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001416void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001417 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001418 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001419 RecordData Record;
1420
Chris Lattnerf04ad692009-04-10 17:16:57 +00001421 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001422 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001423
1424 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001425 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1426 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1427 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001428 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001429
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001430 // Write out the source location entry table. We skip the first
1431 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001432 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001433 // Write out the offsets of only source location file entries.
1434 // We will go through them in ASTReader::validateFileEntries().
1435 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001436 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001437 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1438 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001439 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001440 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001441 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001442 FileID FID = FileID::get(I);
1443 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001444
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001445 // Record the offset of this source-location entry.
1446 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1447
1448 // Figure out which record code to use.
1449 unsigned Code;
1450 if (SLoc->isFile()) {
Douglas Gregora081da52011-11-16 20:05:18 +00001451 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1452 if (Cache->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001453 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001454 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1455 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001456 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001457 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001458 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001459 Record.clear();
1460 Record.push_back(Code);
1461
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001462 // Starting offset of this entry within this module, so skip the dummy.
1463 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001464 if (SLoc->isFile()) {
1465 const SrcMgr::FileInfo &File = SLoc->getFile();
1466 Record.push_back(File.getIncludeLoc().getRawEncoding());
1467 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1468 Record.push_back(File.hasLineDirectives());
1469
1470 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001471 if (Content->OrigEntry) {
1472 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregora081da52011-11-16 20:05:18 +00001473 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001474
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001475 // The source location entry is a file. The blob associated
1476 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Douglas Gregor2d52be52010-03-21 22:49:54 +00001478 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001479 Record.push_back(Content->OrigEntry->getSize());
1480 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregora081da52011-11-16 20:05:18 +00001481 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001482 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001483
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00001484 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00001485 if (FDI != FileDeclIDs.end()) {
1486 Record.push_back(FDI->second->FirstDeclIndex);
1487 Record.push_back(FDI->second->DeclIDs.size());
1488 } else {
1489 Record.push_back(0);
1490 Record.push_back(0);
1491 }
Douglas Gregora081da52011-11-16 20:05:18 +00001492
Douglas Gregore650c8c2009-07-07 00:12:59 +00001493 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001494 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001495 SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001496
1497 // Ask the file manager to fixup the relative path for us. This will
1498 // honor the working directory.
1499 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1500
1501 // FIXME: This call to make_absolute shouldn't be necessary, the
1502 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001503 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001504 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregore650c8c2009-07-07 00:12:59 +00001506 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001507 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregora081da52011-11-16 20:05:18 +00001508
1509 if (Content->BufferOverridden) {
1510 Record.clear();
1511 Record.push_back(SM_SLOC_BUFFER_BLOB);
1512 const llvm::MemoryBuffer *Buffer
1513 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1514 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1515 StringRef(Buffer->getBufferStart(),
1516 Buffer->getBufferSize() + 1));
1517 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001518 } else {
1519 // The source location entry is a buffer. The blob associated
1520 // with this entry contains the contents of the buffer.
1521
1522 // We add one to the size so that we capture the trailing NULL
1523 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1524 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001525 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001526 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001527 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001528 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001529 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001530 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001531 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001533 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001534 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001535
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001536 if (strcmp(Name, "<built-in>") == 0) {
1537 PreloadSLocs.push_back(SLocEntryOffsets.size());
1538 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001539 }
1540 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001541 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001542 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001543 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1544 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisd21683c2011-08-17 00:31:14 +00001545 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1546 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547
1548 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001549 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001550 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001551 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001553 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001554 }
1555 }
1556
Douglas Gregorc9490c02009-04-16 22:23:12 +00001557 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001558
1559 if (SLocEntryOffsets.empty())
1560 return;
1561
Sebastian Redl3397c552010-08-18 23:56:27 +00001562 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001563 // table is used for lazily loading source-location information.
1564 using namespace llvm;
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1570 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001573 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001574 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001575 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001576 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001577
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001578 Abbrev = new BitCodeAbbrev();
1579 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1582 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1583
1584 Record.clear();
1585 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1586 Record.push_back(SLocFileEntryOffsets.size());
1587 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1588 data(SLocFileEntryOffsets));
1589
Sebastian Redl3397c552010-08-18 23:56:27 +00001590 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001591 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001592 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001593
1594 // Write the line table. It depends on remapping working, so it must come
1595 // after the source location offsets.
1596 if (SourceMgr.hasLineTable()) {
1597 LineTableInfo &LineTable = SourceMgr.getLineTable();
1598
1599 Record.clear();
1600 // Emit the file names
1601 Record.push_back(LineTable.getNumFilenames());
1602 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1603 // Emit the file name
1604 const char *Filename = LineTable.getFilename(I);
1605 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1606 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1607 Record.push_back(FilenameLen);
1608 if (FilenameLen)
1609 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1610 }
1611
1612 // Emit the line entries
1613 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1614 L != LEnd; ++L) {
1615 // Only emit entries for local files.
Douglas Gregor47d9de62012-06-08 16:40:28 +00001616 if (L->first.ID < 0)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001617 continue;
1618
1619 // Emit the file ID
Douglas Gregor47d9de62012-06-08 16:40:28 +00001620 Record.push_back(L->first.ID);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001621
1622 // Emit the line entries
1623 Record.push_back(L->second.size());
1624 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1625 LEEnd = L->second.end();
1626 LE != LEEnd; ++LE) {
1627 Record.push_back(LE->FileOffset);
1628 Record.push_back(LE->LineNo);
1629 Record.push_back(LE->FilenameID);
1630 Record.push_back((unsigned)LE->FileKind);
1631 Record.push_back(LE->IncludeOffset);
1632 }
1633 }
1634 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1635 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001636}
1637
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001638//===----------------------------------------------------------------------===//
1639// Preprocessor Serialization
1640//===----------------------------------------------------------------------===//
1641
Douglas Gregor9c736102011-02-10 18:20:09 +00001642static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1643 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1644 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1645 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1646 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1647 return X.first->getName().compare(Y.first->getName());
1648}
1649
Chris Lattner0b1fb982009-04-10 17:15:23 +00001650/// \brief Writes the block containing the serialized form of the
1651/// preprocessor.
1652///
Douglas Gregor7143aab2011-09-01 17:04:32 +00001653void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001654 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1655 if (PPRec)
1656 WritePreprocessorDetail(*PPRec);
1657
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001658 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001659
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001660 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1661 if (PP.getCounterValue() != 0) {
1662 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001663 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001664 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001665 }
1666
1667 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001668 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Sebastian Redl3397c552010-08-18 23:56:27 +00001670 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001671 // FIXME: use diagnostics subsystem for localization etc.
1672 if (PP.SawDateOrTime())
1673 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Douglas Gregorecdcb882010-10-20 22:00:55 +00001675
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001676 // Loop over all the macro definitions that are live at the end of the file,
1677 // emitting each to the PP section.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001678
Douglas Gregor9c736102011-02-10 18:20:09 +00001679 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001680 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001681 MacrosToEmit;
1682 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001683 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor040a8042011-02-11 00:26:14 +00001684 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001685 I != E; ++I) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001686 if (!IsModule || I->second->isPublic()) {
1687 MacroDefinitionsSeen.insert(I->first);
1688 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor7143aab2011-09-01 17:04:32 +00001689 }
Douglas Gregor9c736102011-02-10 18:20:09 +00001690 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001691
Douglas Gregor9c736102011-02-10 18:20:09 +00001692 // Sort the set of macro definitions that need to be serialized by the
1693 // name of the macro, to provide a stable ordering.
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001694 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor9c736102011-02-10 18:20:09 +00001695 &compareMacroDefinitions);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001696
Douglas Gregora8235d62012-10-09 23:05:51 +00001697 /// \brief Offsets of each of the macros into the bitstream, indexed by
1698 /// the local macro ID
1699 ///
1700 /// For each identifier that is associated with a macro, this map
1701 /// provides the offset into the bitstream where that macro is
1702 /// defined.
1703 std::vector<uint32_t> MacroOffsets;
1704
Douglas Gregor9c736102011-02-10 18:20:09 +00001705 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1706 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001707
Douglas Gregora8235d62012-10-09 23:05:51 +00001708 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1709 MI = MI->getPreviousDefinition()) {
1710 MacroID ID = getMacroRef(MI);
1711 if (!ID)
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001712 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Douglas Gregora8235d62012-10-09 23:05:51 +00001714 // Skip macros from a AST file if we're chaining.
1715 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1716 continue;
1717
1718 if (ID < FirstMacroID) {
1719 // This will have been dealt with via an update record.
1720 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1721 continue;
1722 }
1723
1724 // Record the local offset of this macro.
1725 unsigned Index = ID - FirstMacroID;
1726 if (Index == MacroOffsets.size())
1727 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1728 else {
1729 if (Index > MacroOffsets.size())
1730 MacroOffsets.resize(Index + 1);
1731
1732 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1733 }
1734
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001735 AddIdentifierRef(Name, Record);
Douglas Gregora8235d62012-10-09 23:05:51 +00001736 addMacroRef(MI, Record);
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00001737 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001738 AddSourceLocation(MI->getDefinitionLoc(), Record);
1739 AddSourceLocation(MI->getUndefLoc(), Record);
1740 Record.push_back(MI->isUsed());
1741 Record.push_back(MI->isPublic());
1742 AddSourceLocation(MI->getVisibilityLocation(), Record);
1743 unsigned Code;
1744 if (MI->isObjectLike()) {
1745 Code = PP_MACRO_OBJECT_LIKE;
1746 } else {
1747 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001748
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001749 Record.push_back(MI->isC99Varargs());
1750 Record.push_back(MI->isGNUVarargs());
1751 Record.push_back(MI->getNumArgs());
1752 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1753 I != E; ++I)
1754 AddIdentifierRef(*I, Record);
1755 }
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001757 // If we have a detailed preprocessing record, record the macro definition
1758 // ID that corresponds to this macro.
1759 if (PPRec)
1760 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1761
1762 Stream.EmitRecord(Code, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001763 Record.clear();
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00001764
1765 // Emit the tokens array.
1766 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1767 // Note that we know that the preprocessor does not have any annotation
1768 // tokens in it because they are created by the parser, and thus can't
1769 // be in a macro definition.
1770 const Token &Tok = MI->getReplacementToken(TokNo);
1771
1772 Record.push_back(Tok.getLocation().getRawEncoding());
1773 Record.push_back(Tok.getLength());
1774
1775 // FIXME: When reading literal tokens, reconstruct the literal pointer
1776 // if it is needed.
1777 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1778 // FIXME: Should translate token kind to a stable encoding.
1779 Record.push_back(Tok.getKind());
1780 // FIXME: Should translate token flags to a stable encoding.
1781 Record.push_back(Tok.getFlags());
1782
1783 Stream.EmitRecord(PP_TOKEN, Record);
1784 Record.clear();
1785 }
1786 ++NumMacros;
Chris Lattnerdf961c22009-04-10 18:08:30 +00001787 }
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001788 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001789 Stream.ExitBlock();
Douglas Gregora8235d62012-10-09 23:05:51 +00001790
1791 // Write the offsets table for macro IDs.
1792 using namespace llvm;
1793 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1794 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1798
1799 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1800 Record.clear();
1801 Record.push_back(MACRO_OFFSET);
1802 Record.push_back(MacroOffsets.size());
1803 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1804 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1805 data(MacroOffsets));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001806}
1807
1808void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001809 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001810 return;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001811
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001812 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001813
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001814 // Enter the preprocessor block.
1815 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001816
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001817 // If the preprocessor has a preprocessing record, emit it.
1818 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001819 using namespace llvm;
1820
1821 // Set up the abbreviation for
1822 unsigned InclusionAbbrev = 0;
1823 {
1824 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1825 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1828 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001829 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001830 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1831 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1832 }
1833
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001834 unsigned FirstPreprocessorEntityID
1835 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1836 + NUM_PREDEF_PP_ENTITY_IDS;
1837 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001838 RecordData Record;
Argyrios Kyrtzidisb6441ef2011-09-19 20:40:42 +00001839 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1840 EEnd = PPRec.local_end();
Douglas Gregor7338a922011-08-04 17:06:18 +00001841 E != EEnd;
1842 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001843 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001844
Argyrios Kyrtzidis2dbaca72011-09-19 20:40:25 +00001845 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1846 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001847
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001848 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001849 // Record this macro definition's ID.
1850 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001851
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001852 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001853 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1854 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001855 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001856
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001857 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis8f7c5402011-09-08 17:18:41 +00001858 Record.push_back(ME->isBuiltinMacro());
1859 if (ME->isBuiltinMacro())
1860 AddIdentifierRef(ME->getName(), Record);
1861 else
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001862 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001863 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001864 continue;
1865 }
1866
1867 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1868 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001869 Record.push_back(ID->getFileName().size());
1870 Record.push_back(ID->wasInQuotes());
1871 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidis8dd927c2012-10-02 16:10:46 +00001872 Record.push_back(ID->importedModule());
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00001873 SmallString<64> Buffer;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001874 Buffer += ID->getFileName();
Argyrios Kyrtzidis29f98b42012-03-08 01:08:28 +00001875 // Check that the FileEntry is not null because it was not resolved and
1876 // we create a PCH even with compiler errors.
1877 if (ID->getFile())
1878 Buffer += ID->getFile()->getName();
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001879 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1880 continue;
1881 }
1882
1883 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1884 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001885 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001886
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001887 // Write the offsets table for the preprocessing record.
1888 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001889 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1890
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001891 // Write the offsets table for identifier IDs.
1892 using namespace llvm;
1893 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001894 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001895 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001896 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001897 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001898
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001899 Record.clear();
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001900 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001901 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00001902 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1903 data(PreprocessedEntityOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001904 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001905}
1906
Douglas Gregore209e502011-12-06 01:10:29 +00001907unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1908 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1909 if (Known != SubmoduleIDs.end())
1910 return Known->second;
1911
1912 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1913}
1914
Douglas Gregor26ced122011-12-01 00:59:36 +00001915/// \brief Compute the number of modules within the given tree (including the
1916/// given module).
1917static unsigned getNumberOfModules(Module *Mod) {
1918 unsigned ChildModules = 0;
Douglas Gregorb7a78192012-01-04 23:32:19 +00001919 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1920 SubEnd = Mod->submodule_end();
Douglas Gregor26ced122011-12-01 00:59:36 +00001921 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00001922 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor26ced122011-12-01 00:59:36 +00001923
1924 return ChildModules + 1;
1925}
1926
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001927void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor4bc8738d2011-12-05 16:35:23 +00001928 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor55988682011-12-05 16:33:54 +00001929 // FIXME: This feels like it belongs somewhere else, but there are no
1930 // other consumers of this information.
1931 SourceManager &SrcMgr = PP->getSourceManager();
1932 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1933 for (ASTContext::import_iterator I = Context->local_import_begin(),
1934 IEnd = Context->local_import_end();
1935 I != IEnd; ++I) {
Douglas Gregor55988682011-12-05 16:33:54 +00001936 if (Module *ImportedFrom
1937 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1938 SrcMgr))) {
1939 ImportedFrom->Imports.push_back(I->getImportedModule());
1940 }
1941 }
1942
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001943 // Enter the submodule description block.
1944 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1945
1946 // Write the abbreviations needed for the submodules block.
1947 using namespace llvm;
1948 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1949 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregore209e502011-12-06 01:10:29 +00001950 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora1f1fad2012-01-27 19:52:33 +00001954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor1e123682011-12-05 22:27:44 +00001956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor1e123682011-12-05 22:27:44 +00001957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1959 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1960
1961 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001962 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1964 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1965
1966 Abbrev = new BitCodeAbbrev();
1967 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1968 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1969 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor77d029f2011-12-08 19:11:24 +00001970
1971 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00001972 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
1973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1974 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1975
1976 Abbrev = new BitCodeAbbrev();
Douglas Gregor77d029f2011-12-08 19:11:24 +00001977 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1979 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1980
Douglas Gregor51f564f2011-12-31 04:05:44 +00001981 Abbrev = new BitCodeAbbrev();
1982 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1984 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1985
Douglas Gregor26ced122011-12-01 00:59:36 +00001986 // Write the submodule metadata block.
1987 RecordData Record;
1988 Record.push_back(getNumberOfModules(WritingModule));
1989 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1990 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1991
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001992 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001993 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001994 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001995 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001996 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001997 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00001998 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001999
2000 // Emit the definition of the block.
2001 Record.clear();
2002 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002003 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002004 if (Mod->Parent) {
2005 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2006 Record.push_back(SubmoduleIDs[Mod->Parent]);
2007 } else {
2008 Record.push_back(0);
2009 }
2010 Record.push_back(Mod->IsFramework);
2011 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002012 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002013 Record.push_back(Mod->InferSubmodules);
2014 Record.push_back(Mod->InferExplicitSubmodules);
2015 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002016 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2017
Douglas Gregor51f564f2011-12-31 04:05:44 +00002018 // Emit the requirements.
2019 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2020 Record.clear();
2021 Record.push_back(SUBMODULE_REQUIRES);
2022 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2023 Mod->Requires[I].data(),
2024 Mod->Requires[I].size());
2025 }
2026
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002027 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002028 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002029 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002030 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002031 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002032 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002033 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2034 Record.clear();
2035 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2036 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2037 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002038 }
2039
2040 // Emit the headers.
2041 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2042 Record.clear();
2043 Record.push_back(SUBMODULE_HEADER);
2044 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2045 Mod->Headers[I]->getName());
2046 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002047 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2048 Record.clear();
2049 Record.push_back(SUBMODULE_TOPHEADER);
2050 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2051 Mod->TopHeaders[I]->getName());
2052 }
Douglas Gregor55988682011-12-05 16:33:54 +00002053
2054 // Emit the imports.
2055 if (!Mod->Imports.empty()) {
2056 Record.clear();
2057 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002058 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002059 assert(ImportedID && "Unknown submodule!");
2060 Record.push_back(ImportedID);
2061 }
2062 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2063 }
2064
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002065 // Emit the exports.
2066 if (!Mod->Exports.empty()) {
2067 Record.clear();
2068 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002069 if (Module *Exported = Mod->Exports[I].getPointer()) {
2070 unsigned ExportedID = SubmoduleIDs[Exported];
2071 assert(ExportedID > 0 && "Unknown submodule ID?");
2072 Record.push_back(ExportedID);
2073 } else {
2074 Record.push_back(0);
2075 }
2076
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002077 Record.push_back(Mod->Exports[I].getInt());
2078 }
2079 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2080 }
2081
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002082 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002083 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2084 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002085 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002086 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002087 }
2088
2089 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002090
2091 assert((NextSubmoduleID - FirstSubmoduleID
2092 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002093}
2094
Douglas Gregor185dbd72011-12-01 02:07:58 +00002095serialization::SubmoduleID
2096ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002097 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002098 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002099
2100 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002101 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002102 Module *OwningMod
2103 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002104 if (!OwningMod)
2105 return 0;
2106
Douglas Gregore209e502011-12-06 01:10:29 +00002107 // Check whether this submodule is part of our own module.
2108 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002109 return 0;
2110
Douglas Gregore209e502011-12-06 01:10:29 +00002111 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002112}
2113
David Blaikied6471f72011-09-25 23:23:43 +00002114void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002115 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002116 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002117 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2118 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002119 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002120 if (point.Loc.isInvalid())
2121 continue;
2122
2123 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002124 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002125 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002126 if (I->second.isPragma()) {
2127 Record.push_back(I->first);
2128 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002129 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002130 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002131 Record.push_back(-1); // mark the end of the diag/map pairs for this
2132 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002133 }
2134
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002135 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002136 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002137}
2138
Anders Carlssonc8505782011-03-06 18:41:18 +00002139void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2140 if (CXXBaseSpecifiersOffsets.empty())
2141 return;
2142
2143 RecordData Record;
2144
2145 // Create a blob abbreviation for the C++ base specifiers offsets.
2146 using namespace llvm;
2147
2148 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2149 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2150 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2151 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2152 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2153
Douglas Gregore92b8a12011-08-04 00:01:48 +00002154 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002155 Record.clear();
2156 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2157 Record.push_back(CXXBaseSpecifiersOffsets.size());
2158 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002159 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002160}
2161
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002162//===----------------------------------------------------------------------===//
2163// Type Serialization
2164//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002165
Sebastian Redl3397c552010-08-18 23:56:27 +00002166/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002167void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002168 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002169 if (Idx.getIndex() == 0) // we haven't seen this type before.
2170 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002171
Douglas Gregor97475832010-10-05 18:37:06 +00002172 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002173
Douglas Gregor2cf26342009-04-09 22:27:44 +00002174 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002175 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002176 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002177 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002178 else if (TypeOffsets.size() < Index) {
2179 TypeOffsets.resize(Index + 1);
2180 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002181 }
2182
2183 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Douglas Gregor2cf26342009-04-09 22:27:44 +00002185 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002186 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002187
Douglas Gregora4923eb2009-11-16 21:35:15 +00002188 if (T.hasLocalNonFastQualifiers()) {
2189 Qualifiers Qs = T.getLocalQualifiers();
2190 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002191 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002192 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002193 } else {
2194 switch (T->getTypeClass()) {
2195 // For all of the concrete, non-dependent types, call the
2196 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002197#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002198 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002199#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002200#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002201 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002202 }
2203
2204 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002205 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002206
2207 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002208 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002209}
2210
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002211//===----------------------------------------------------------------------===//
2212// Declaration Serialization
2213//===----------------------------------------------------------------------===//
2214
Douglas Gregor2cf26342009-04-09 22:27:44 +00002215/// \brief Write the block containing all of the declaration IDs
2216/// lexically declared within the given DeclContext.
2217///
2218/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2219/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002220uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002221 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002222 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002223 return 0;
2224
Douglas Gregorc9490c02009-04-16 22:23:12 +00002225 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002226 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002227 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002228 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002229 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2230 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002231 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002232
Douglas Gregor25123082009-04-22 22:34:57 +00002233 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002234 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002235 return Offset;
2236}
2237
Sebastian Redla4232eb2010-08-18 23:56:21 +00002238void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002239 using namespace llvm;
2240 RecordData Record;
2241
2242 // Write the type offsets array
2243 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002244 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2248 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2249 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002250 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002251 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002252 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002253 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002254
2255 // Write the declaration offsets array
2256 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002257 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2261 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2262 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002263 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002264 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002265 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002266 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002267}
2268
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002269void ASTWriter::WriteFileDeclIDsMap() {
2270 using namespace llvm;
2271 RecordData Record;
2272
2273 // Join the vectors of DeclIDs from all files.
2274 SmallVector<DeclID, 256> FileSortedIDs;
2275 for (FileDeclIDsTy::iterator
2276 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2277 DeclIDInFileInfo &Info = *FI->second;
2278 Info.FirstDeclIndex = FileSortedIDs.size();
2279 for (LocDeclIDsTy::iterator
2280 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2281 FileSortedIDs.push_back(DI->second);
2282 }
2283
2284 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2285 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2288 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2289 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002290 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002291 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2292}
2293
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002294void ASTWriter::WriteComments() {
2295 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002296 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002297 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002298 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2299 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002300 I != E; ++I) {
2301 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002302 AddSourceRange((*I)->getSourceRange(), Record);
2303 Record.push_back((*I)->getKind());
2304 Record.push_back((*I)->isTrailingComment());
2305 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002306 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2307 }
2308 Stream.ExitBlock();
2309}
2310
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002311//===----------------------------------------------------------------------===//
2312// Global Method Pool and Selector Serialization
2313//===----------------------------------------------------------------------===//
2314
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002315namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002316// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002317class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002318 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002319
2320public:
2321 typedef Selector key_type;
2322 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Sebastian Redl5d050072010-08-04 17:20:04 +00002324 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002325 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002326 ObjCMethodList Instance, Factory;
2327 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002328 typedef const data_type& data_type_ref;
2329
Sebastian Redl3397c552010-08-18 23:56:27 +00002330 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002332 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002333 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002334 }
Mike Stump1eb44332009-09-09 15:08:12 +00002335
2336 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002337 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002338 data_type_ref Methods) {
2339 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2340 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002341 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2342 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002343 Method = Method->Next)
2344 if (Method->Method)
2345 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002346 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002347 Method = Method->Next)
2348 if (Method->Method)
2349 DataLen += 4;
2350 clang::io::Emit16(Out, DataLen);
2351 return std::make_pair(KeyLen, DataLen);
2352 }
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Chris Lattner5f9e2722011-07-23 10:55:15 +00002354 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002355 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002356 assert((Start >> 32) == 0 && "Selector key offset too large");
2357 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002358 unsigned N = Sel.getNumArgs();
2359 clang::io::Emit16(Out, N);
2360 if (N == 0)
2361 N = 1;
2362 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002363 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002364 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2365 }
Mike Stump1eb44332009-09-09 15:08:12 +00002366
Chris Lattner5f9e2722011-07-23 10:55:15 +00002367 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002368 data_type_ref Methods, unsigned DataLen) {
2369 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002370 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002371 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002372 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002373 Method = Method->Next)
2374 if (Method->Method)
2375 ++NumInstanceMethods;
2376
2377 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002378 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002379 Method = Method->Next)
2380 if (Method->Method)
2381 ++NumFactoryMethods;
2382
2383 clang::io::Emit16(Out, NumInstanceMethods);
2384 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002385 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002386 Method = Method->Next)
2387 if (Method->Method)
2388 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002389 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002390 Method = Method->Next)
2391 if (Method->Method)
2392 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002393
2394 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002395 }
2396};
2397} // end anonymous namespace
2398
Sebastian Redl059612d2010-08-03 21:58:15 +00002399/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002400///
2401/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002402/// in an on-disk hash table indexed by the selector. The hash table also
2403/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002404void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002405 using namespace llvm;
2406
Sebastian Redl059612d2010-08-03 21:58:15 +00002407 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002408 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002409 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002410 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002411 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002412 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002413 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002414 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Sebastian Redl059612d2010-08-03 21:58:15 +00002416 // Create the on-disk hash table representation. We walk through every
2417 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002418 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002419 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002420 I = SelectorIDs.begin(), E = SelectorIDs.end();
2421 I != E; ++I) {
2422 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002423 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002424 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002425 I->second,
2426 ObjCMethodList(),
2427 ObjCMethodList()
2428 };
2429 if (F != SemaRef.MethodPool.end()) {
2430 Data.Instance = F->second.first;
2431 Data.Factory = F->second.second;
2432 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002433 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002434 // changed.
2435 if (Chain && I->second < FirstSelectorID) {
2436 // Selector already exists. Did it change?
2437 bool changed = false;
2438 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2439 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002440 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002441 changed = true;
2442 }
2443 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2444 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002445 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002446 changed = true;
2447 }
2448 if (!changed)
2449 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002450 } else if (Data.Instance.Method || Data.Factory.Method) {
2451 // A new method pool entry.
2452 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002453 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002454 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002455 }
2456
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002457 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002458 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002459 uint32_t BucketOffset;
2460 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002461 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002462 llvm::raw_svector_ostream Out(MethodPool);
2463 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002464 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002465 BucketOffset = Generator.Emit(Out, Trait);
2466 }
2467
2468 // Create a blob abbreviation
2469 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002470 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002472 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002473 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2474 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2475
Douglas Gregor83941df2009-04-25 17:48:32 +00002476 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002477 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002478 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002479 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002480 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002481 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002482
2483 // Create a blob abbreviation for the selector table offsets.
2484 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002485 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002486 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002487 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002488 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2489 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2490
2491 // Write the selector offsets table.
2492 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002493 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002494 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002495 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002496 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002497 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002498 }
2499}
2500
Sebastian Redl3397c552010-08-18 23:56:27 +00002501/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002502void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002503 using namespace llvm;
2504 if (SemaRef.ReferencedSelectors.empty())
2505 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002506
Fariborz Jahanian32019832010-07-23 19:11:11 +00002507 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002508
Sebastian Redl3397c552010-08-18 23:56:27 +00002509 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002510 // very tricky to fix, and given that @selector shouldn't really appear in
2511 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002512 for (DenseMap<Selector, SourceLocation>::iterator S =
2513 SemaRef.ReferencedSelectors.begin(),
2514 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2515 Selector Sel = (*S).first;
2516 SourceLocation Loc = (*S).second;
2517 AddSelectorRef(Sel, Record);
2518 AddSourceLocation(Loc, Record);
2519 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002520 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002521}
2522
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002523//===----------------------------------------------------------------------===//
2524// Identifier Table Serialization
2525//===----------------------------------------------------------------------===//
2526
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002527namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002528class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002529 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002530 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002531 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002532 bool IsModule;
2533
Douglas Gregora92193e2009-04-28 21:18:29 +00002534 /// \brief Determines whether this is an "interesting" identifier
2535 /// that needs a full IdentifierInfo structure written into the hash
2536 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002537 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002538 if (II->isPoisoned() ||
2539 II->isExtensionToken() ||
2540 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002541 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002542 II->getFETokenInfo<void>())
2543 return true;
2544
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002545 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002546 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002547
2548 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2549 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002550 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002551
2552 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002553 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002554
2555 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002556 }
2557
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002558public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002559 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002560 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002561
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002562 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002563 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002564
Douglas Gregoreee242f2011-10-27 09:33:13 +00002565 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2566 IdentifierResolver &IdResolver, bool IsModule)
2567 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002568
2569 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002570 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002571 }
Mike Stump1eb44332009-09-09 15:08:12 +00002572
2573 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002574 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002575 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002576 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002577 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002578 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002579 DataLen += 2; // 2 bytes for builtin ID
2580 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002581 if (hadMacroDefinition(II, Macro)) {
2582 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2583 if (Writer.getMacroRef(M) != 0)
2584 DataLen += 4;
2585 }
2586
2587 DataLen += 4;
2588 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002589
Douglas Gregoreee242f2011-10-27 09:33:13 +00002590 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2591 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002592 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002593 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002594 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002595 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002596 // We emit the key length after the data length so that every
2597 // string is preceded by a 16-bit length. This matches the PTH
2598 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002599 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002600 return std::make_pair(KeyLen, DataLen);
2601 }
Mike Stump1eb44332009-09-09 15:08:12 +00002602
Chris Lattner5f9e2722011-07-23 10:55:15 +00002603 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002604 unsigned KeyLen) {
2605 // Record the location of the key data. This is used when generating
2606 // the mapping from persistent IDs to strings.
2607 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002608 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002609 }
Mike Stump1eb44332009-09-09 15:08:12 +00002610
Douglas Gregor7143aab2011-09-01 17:04:32 +00002611 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002613 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002614 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002615 clang::io::Emit32(Out, ID << 1);
2616 return;
2617 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002618
Douglas Gregora92193e2009-04-28 21:18:29 +00002619 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002620 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2621 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2622 clang::io::Emit16(Out, Bits);
2623 Bits = 0;
2624 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002625 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002626 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2627 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002628 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002629 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002630 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002631
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002632 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002633 // Write all of the macro IDs associated with this identifier.
2634 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2635 if (MacroID ID = Writer.getMacroRef(M))
2636 clang::io::Emit32(Out, ID);
2637 }
2638
2639 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002640 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002641
Douglas Gregor668c1a42009-04-21 22:25:48 +00002642 // Emit the declaration IDs in reverse order, because the
2643 // IdentifierResolver provides the declarations as they would be
2644 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002645 // "stat"), but the ASTReader adds declarations to the end of the list
2646 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002647 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002648 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2649 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002650 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002651 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002652 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002653 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002654 }
2655};
2656} // end anonymous namespace
2657
Sebastian Redl3397c552010-08-18 23:56:27 +00002658/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002659///
2660/// The identifier table consists of a blob containing string data
2661/// (the actual identifiers themselves) and a separate "offsets" index
2662/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002663void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2664 IdentifierResolver &IdResolver,
2665 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002666 using namespace llvm;
2667
2668 // Create and write out the blob that contains the identifier
2669 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002670 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002671 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002672 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor92b059e2009-04-28 20:33:11 +00002674 // Look for any identifiers that were named while processing the
2675 // headers, but are otherwise not needed. We add these to the hash
2676 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002677 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002678 // file.
2679 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2680 IDEnd = PP.getIdentifierTable().end();
2681 ID != IDEnd; ++ID)
2682 getIdentifierRef(ID->second);
2683
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002684 // Create the on-disk hash table representation. We only store offsets
2685 // for identifiers that appear here for the first time.
2686 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002687 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002688 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2689 ID != IDEnd; ++ID) {
2690 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002691 if (!Chain || !ID->first->isFromAST() ||
2692 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002693 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2694 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002695 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002696
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002697 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002698 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002699 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002700 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002701 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002702 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002703 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002704 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002705 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002706 }
2707
2708 // Create a blob abbreviation
2709 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002710 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002713 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002714
2715 // Write the identifier table
2716 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002717 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002718 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002719 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002720 }
2721
2722 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002723 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002724 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002725 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2728 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2729
2730 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002731 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002732 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002733 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002734 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002735 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002736}
2737
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002738//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002739// DeclContext's Name Lookup Table Serialization
2740//===----------------------------------------------------------------------===//
2741
2742namespace {
2743// Trait used for the on-disk hash table used in the method pool.
2744class ASTDeclContextNameLookupTrait {
2745 ASTWriter &Writer;
2746
2747public:
2748 typedef DeclarationName key_type;
2749 typedef key_type key_type_ref;
2750
2751 typedef DeclContext::lookup_result data_type;
2752 typedef const data_type& data_type_ref;
2753
2754 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2755
2756 unsigned ComputeHash(DeclarationName Name) {
2757 llvm::FoldingSetNodeID ID;
2758 ID.AddInteger(Name.getNameKind());
2759
2760 switch (Name.getNameKind()) {
2761 case DeclarationName::Identifier:
2762 ID.AddString(Name.getAsIdentifierInfo()->getName());
2763 break;
2764 case DeclarationName::ObjCZeroArgSelector:
2765 case DeclarationName::ObjCOneArgSelector:
2766 case DeclarationName::ObjCMultiArgSelector:
2767 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2768 break;
2769 case DeclarationName::CXXConstructorName:
2770 case DeclarationName::CXXDestructorName:
2771 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002772 break;
2773 case DeclarationName::CXXOperatorName:
2774 ID.AddInteger(Name.getCXXOverloadedOperator());
2775 break;
2776 case DeclarationName::CXXLiteralOperatorName:
2777 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2778 case DeclarationName::CXXUsingDirective:
2779 break;
2780 }
2781
2782 return ID.ComputeHash();
2783 }
2784
2785 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002786 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002787 data_type_ref Lookup) {
2788 unsigned KeyLen = 1;
2789 switch (Name.getNameKind()) {
2790 case DeclarationName::Identifier:
2791 case DeclarationName::ObjCZeroArgSelector:
2792 case DeclarationName::ObjCOneArgSelector:
2793 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002794 case DeclarationName::CXXLiteralOperatorName:
2795 KeyLen += 4;
2796 break;
2797 case DeclarationName::CXXOperatorName:
2798 KeyLen += 1;
2799 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002800 case DeclarationName::CXXConstructorName:
2801 case DeclarationName::CXXDestructorName:
2802 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002803 case DeclarationName::CXXUsingDirective:
2804 break;
2805 }
2806 clang::io::Emit16(Out, KeyLen);
2807
2808 // 2 bytes for num of decls and 4 for each DeclID.
2809 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2810 clang::io::Emit16(Out, DataLen);
2811
2812 return std::make_pair(KeyLen, DataLen);
2813 }
2814
Chris Lattner5f9e2722011-07-23 10:55:15 +00002815 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002816 using namespace clang::io;
2817
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002818 Emit8(Out, Name.getNameKind());
2819 switch (Name.getNameKind()) {
2820 case DeclarationName::Identifier:
2821 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002822 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002823 case DeclarationName::ObjCZeroArgSelector:
2824 case DeclarationName::ObjCOneArgSelector:
2825 case DeclarationName::ObjCMultiArgSelector:
2826 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002827 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002828 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002829 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2830 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002831 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002832 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002833 case DeclarationName::CXXLiteralOperatorName:
2834 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002835 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002836 case DeclarationName::CXXConstructorName:
2837 case DeclarationName::CXXDestructorName:
2838 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002839 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002840 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002841 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002842
2843 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002844 }
2845
Chris Lattner5f9e2722011-07-23 10:55:15 +00002846 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002847 data_type Lookup, unsigned DataLen) {
2848 uint64_t Start = Out.tell(); (void)Start;
2849 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2850 for (; Lookup.first != Lookup.second; ++Lookup.first)
2851 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2852
2853 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2854 }
2855};
2856} // end anonymous namespace
2857
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002858/// \brief Write the block containing all of the declaration IDs
2859/// visible from the given DeclContext.
2860///
2861/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002862/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002863uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2864 DeclContext *DC) {
2865 if (DC->getPrimaryContext() != DC)
2866 return 0;
2867
2868 // Since there is no name lookup into functions or methods, don't bother to
2869 // build a visible-declarations table for these entities.
2870 if (DC->isFunctionOrMethod())
2871 return 0;
2872
2873 // If not in C++, we perform name lookup for the translation unit via the
2874 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2875 // FIXME: In C++ we need the visible declarations in order to "see" the
2876 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002877 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002878 return 0;
2879
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002880 // Serialize the contents of the mapping used for lookup. Note that,
2881 // although we have two very different code paths, the serialized
2882 // representation is the same for both cases: a declaration name,
2883 // followed by a size, followed by references to the visible
2884 // declarations that have that name.
2885 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002886 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002887 if (!Map || Map->empty())
2888 return 0;
2889
2890 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2891 ASTDeclContextNameLookupTrait Trait(*this);
2892
2893 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002894 DeclarationName ConversionName;
2895 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002896 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2897 D != DEnd; ++D) {
2898 DeclarationName Name = D->first;
2899 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002900 if (Result.first != Result.second) {
2901 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2902 // Hash all conversion function names to the same name. The actual
2903 // type information in conversion function name is not used in the
2904 // key (since such type information is not stable across different
2905 // modules), so the intended effect is to coalesce all of the conversion
2906 // functions under a single key.
2907 if (!ConversionName)
2908 ConversionName = Name;
2909 ConversionDecls.append(Result.first, Result.second);
2910 continue;
2911 }
2912
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002913 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002914 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002915 }
2916
Douglas Gregore5a54b62011-08-30 20:49:19 +00002917 // Add the conversion functions
2918 if (!ConversionDecls.empty()) {
2919 Generator.insert(ConversionName,
2920 DeclContext::lookup_result(ConversionDecls.begin(),
2921 ConversionDecls.end()),
2922 Trait);
2923 }
2924
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002925 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002926 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002927 uint32_t BucketOffset;
2928 {
2929 llvm::raw_svector_ostream Out(LookupTable);
2930 // Make sure that no bucket is at offset 0
2931 clang::io::Emit32(Out, 0);
2932 BucketOffset = Generator.Emit(Out, Trait);
2933 }
2934
2935 // Write the lookup table
2936 RecordData Record;
2937 Record.push_back(DECL_CONTEXT_VISIBLE);
2938 Record.push_back(BucketOffset);
2939 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2940 LookupTable.str());
2941
2942 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2943 ++NumVisibleDeclContexts;
2944 return Offset;
2945}
2946
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002947/// \brief Write an UPDATE_VISIBLE block for the given context.
2948///
2949/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2950/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002951/// (in C++), for namespaces, and for classes with forward-declared unscoped
2952/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002953void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002954 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2955 if (!Map || Map->empty())
2956 return;
2957
2958 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2959 ASTDeclContextNameLookupTrait Trait(*this);
2960
2961 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002962 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2963 D != DEnd; ++D) {
2964 DeclarationName Name = D->first;
2965 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002966 // For any name that appears in this table, the results are complete, i.e.
2967 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002968 if (Result.first != Result.second)
2969 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002970 }
2971
2972 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002973 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002974 uint32_t BucketOffset;
2975 {
2976 llvm::raw_svector_ostream Out(LookupTable);
2977 // Make sure that no bucket is at offset 0
2978 clang::io::Emit32(Out, 0);
2979 BucketOffset = Generator.Emit(Out, Trait);
2980 }
2981
2982 // Write the lookup table
2983 RecordData Record;
2984 Record.push_back(UPDATE_VISIBLE);
2985 Record.push_back(getDeclID(cast<Decl>(DC)));
2986 Record.push_back(BucketOffset);
2987 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2988}
2989
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002990/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2991void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2992 RecordData Record;
2993 Record.push_back(Opts.fp_contract);
2994 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2995}
2996
2997/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2998void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002999 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003000 return;
3001
3002 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3003 RecordData Record;
3004#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3005#include "clang/Basic/OpenCLExtensions.def"
3006 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3007}
3008
Douglas Gregor2171bf12012-01-15 16:58:34 +00003009void ASTWriter::WriteRedeclarations() {
3010 RecordData LocalRedeclChains;
3011 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3012
3013 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3014 Decl *First = Redeclarations[I];
3015 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3016
3017 Decl *MostRecent = First->getMostRecentDecl();
3018
3019 // If we only have a single declaration, there is no point in storing
3020 // a redeclaration chain.
3021 if (First == MostRecent)
3022 continue;
3023
3024 unsigned Offset = LocalRedeclChains.size();
3025 unsigned Size = 0;
3026 LocalRedeclChains.push_back(0); // Placeholder for the size.
3027
3028 // Collect the set of local redeclarations of this declaration.
3029 for (Decl *Prev = MostRecent; Prev != First;
3030 Prev = Prev->getPreviousDecl()) {
3031 if (!Prev->isFromASTFile()) {
3032 AddDeclRef(Prev, LocalRedeclChains);
3033 ++Size;
3034 }
3035 }
3036 LocalRedeclChains[Offset] = Size;
3037
3038 // Reverse the set of local redeclarations, so that we store them in
3039 // order (since we found them in reverse order).
3040 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3041
3042 // Add the mapping from the first ID to the set of local declarations.
3043 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3044 LocalRedeclsMap.push_back(Info);
3045
3046 assert(N == Redeclarations.size() &&
3047 "Deserialized a declaration we shouldn't have");
3048 }
3049
3050 if (LocalRedeclChains.empty())
3051 return;
3052
3053 // Sort the local redeclarations map by the first declaration ID,
3054 // since the reader will be performing binary searches on this information.
3055 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3056
3057 // Emit the local redeclarations map.
3058 using namespace llvm;
3059 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3060 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3063 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3064
3065 RecordData Record;
3066 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3067 Record.push_back(LocalRedeclsMap.size());
3068 Stream.EmitRecordWithBlob(AbbrevID, Record,
3069 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3070 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3071
3072 // Emit the redeclaration chains.
3073 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3074}
3075
Douglas Gregorcff9f262012-01-27 01:47:08 +00003076void ASTWriter::WriteObjCCategories() {
3077 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3078 RecordData Categories;
3079
3080 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3081 unsigned Size = 0;
3082 unsigned StartIndex = Categories.size();
3083
3084 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3085
3086 // Allocate space for the size.
3087 Categories.push_back(0);
3088
3089 // Add the categories.
3090 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3091 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3092 assert(getDeclID(Cat) != 0 && "Bogus category");
3093 AddDeclRef(Cat, Categories);
3094 }
3095
3096 // Update the size.
3097 Categories[StartIndex] = Size;
3098
3099 // Record this interface -> category map.
3100 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3101 CategoriesMap.push_back(CatInfo);
3102 }
3103
3104 // Sort the categories map by the definition ID, since the reader will be
3105 // performing binary searches on this information.
3106 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3107
3108 // Emit the categories map.
3109 using namespace llvm;
3110 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3111 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3112 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3113 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3114 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3115
3116 RecordData Record;
3117 Record.push_back(OBJC_CATEGORIES_MAP);
3118 Record.push_back(CategoriesMap.size());
3119 Stream.EmitRecordWithBlob(AbbrevID, Record,
3120 reinterpret_cast<char*>(CategoriesMap.data()),
3121 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3122
3123 // Emit the category lists.
3124 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3125}
3126
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003127void ASTWriter::WriteMergedDecls() {
3128 if (!Chain || Chain->MergedDecls.empty())
3129 return;
3130
3131 RecordData Record;
3132 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3133 IEnd = Chain->MergedDecls.end();
3134 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003135 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003136 : getDeclID(I->first);
3137 assert(CanonID && "Merged declaration not known?");
3138
3139 Record.push_back(CanonID);
3140 Record.push_back(I->second.size());
3141 Record.append(I->second.begin(), I->second.end());
3142 }
3143 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3144}
3145
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003146//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003147// General Serialization Routines
3148//===----------------------------------------------------------------------===//
3149
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003150/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003151void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3152 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003153 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003154 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3155 e = Attrs.end(); i != e; ++i){
3156 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003157 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003158 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003159
Sean Huntcf807c42010-08-18 23:23:40 +00003160#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003161
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003162 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003163}
3164
Chris Lattner5f9e2722011-07-23 10:55:15 +00003165void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003166 Record.push_back(Str.size());
3167 Record.insert(Record.end(), Str.begin(), Str.end());
3168}
3169
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003170void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3171 RecordDataImpl &Record) {
3172 Record.push_back(Version.getMajor());
3173 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3174 Record.push_back(*Minor + 1);
3175 else
3176 Record.push_back(0);
3177 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3178 Record.push_back(*Subminor + 1);
3179 else
3180 Record.push_back(0);
3181}
3182
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003183/// \brief Note that the identifier II occurs at the given offset
3184/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003185void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003186 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003187 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003188 // up earlier in the chain and thus don't need an offset.
3189 if (ID >= FirstIdentID)
3190 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003191}
3192
Douglas Gregor83941df2009-04-25 17:48:32 +00003193/// \brief Note that the selector Sel occurs at the given offset
3194/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003195void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003196 unsigned ID = SelectorIDs[Sel];
3197 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003198 // Don't record offsets for selectors that are also available in a different
3199 // file.
3200 if (ID < FirstSelectorID)
3201 return;
3202 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003203}
3204
Sebastian Redla4232eb2010-08-18 23:56:21 +00003205ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003206 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003207 WritingAST(false), DoneWritingDeclsAndTypes(false),
3208 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003209 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003210 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003211 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3212 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003213 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3214 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003215 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003216 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003217 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003218 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003219 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003220 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003221 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3222 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3223 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003224 DeclTypedefAbbrev(0),
3225 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3226 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003227{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003228}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003229
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003230ASTWriter::~ASTWriter() {
3231 for (FileDeclIDsTy::iterator
3232 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3233 delete I->second;
3234}
3235
Sebastian Redla4232eb2010-08-18 23:56:21 +00003236void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003237 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003238 Module *WritingModule, StringRef isysroot,
3239 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003240 WritingAST = true;
3241
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003242 ASTHasCompilerErrors = hasErrors;
3243
Douglas Gregor2cf26342009-04-09 22:27:44 +00003244 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003245 Stream.Emit((unsigned)'C', 8);
3246 Stream.Emit((unsigned)'P', 8);
3247 Stream.Emit((unsigned)'C', 8);
3248 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003249
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003250 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003251
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003252 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003253 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003254 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003255 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003256 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003257 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003258 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003259
3260 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003261}
3262
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003263template<typename Vector>
3264static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3265 ASTWriter::RecordData &Record) {
3266 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3267 I != E; ++I) {
3268 Writer.AddDeclRef(*I, Record);
3269 }
3270}
3271
Sebastian Redla4232eb2010-08-18 23:56:21 +00003272void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003273 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003274 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003275 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003276 using namespace llvm;
3277
Douglas Gregorecc2c092011-12-01 22:20:10 +00003278 // Make sure that the AST reader knows to finalize itself.
3279 if (Chain)
3280 Chain->finalizeForWriting();
3281
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003282 ASTContext &Context = SemaRef.Context;
3283 Preprocessor &PP = SemaRef.PP;
3284
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003285 // Set up predefined declaration IDs.
3286 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003287 if (Context.ObjCIdDecl)
3288 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003289 if (Context.ObjCSelDecl)
3290 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003291 if (Context.ObjCClassDecl)
3292 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003293 if (Context.ObjCProtocolClassDecl)
3294 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003295 if (Context.Int128Decl)
3296 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3297 if (Context.UInt128Decl)
3298 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003299 if (Context.ObjCInstanceTypeDecl)
3300 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003301 if (Context.BuiltinVaListDecl)
3302 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3303
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003304 if (!Chain) {
3305 // Make sure that we emit IdentifierInfos (and any attached
3306 // declarations) for builtins. We don't need to do this when we're
3307 // emitting chained PCH files, because all of the builtins will be
3308 // in the original PCH file.
3309 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003310 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003311 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003312 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003313 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003314 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3315 getIdentifierRef(&Table.get(BuiltinNames[I]));
3316 }
3317
Douglas Gregoreee242f2011-10-27 09:33:13 +00003318 // If there are any out-of-date identifiers, bring them up to date.
3319 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3320 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3321 IDEnd = PP.getIdentifierTable().end();
3322 ID != IDEnd; ++ID)
3323 if (ID->second->isOutOfDate())
3324 ExtSource->updateOutOfDateIdentifier(*ID->second);
3325 }
3326
Chris Lattner63d65f82009-09-08 18:19:27 +00003327 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003328 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003329 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003330 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003331 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003332
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003333 // Build a record containing all of the file scoped decls in this file.
3334 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003335 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3336 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003337
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003338 // Build a record containing all of the delegating constructors we still need
3339 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003340 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003341 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003342
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003343 // Write the set of weak, undeclared identifiers. We always write the
3344 // entire table, since later PCH files in a PCH chain are only interested in
3345 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003346 RecordData WeakUndeclaredIdentifiers;
3347 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003348 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003349 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3350 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3351 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3352 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3353 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3354 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3355 }
3356 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003357
Douglas Gregor14c22f22009-04-22 22:18:58 +00003358 // Build a record containing all of the locally-scoped external
3359 // declarations in this header file. Generally, this record will be
3360 // empty.
3361 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003362 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003363 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003364 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003365 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3366 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003367 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003368 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003369 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3370 }
3371
Douglas Gregorb81c1702009-04-27 20:06:05 +00003372 // Build a record containing all of the ext_vector declarations.
3373 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003374 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003375
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003376 // Build a record containing all of the VTable uses information.
3377 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003378 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003379 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3380 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3381 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3382 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3383 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003384 }
3385
3386 // Build a record containing all of dynamic classes declarations.
3387 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003388 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003389
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003390 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003391 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003392 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003393 I = SemaRef.PendingInstantiations.begin(),
3394 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3395 AddDeclRef(I->first, PendingInstantiations);
3396 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003397 }
3398 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3399 "There are local ones at end of translation unit!");
3400
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003401 // Build a record containing some declaration references.
3402 RecordData SemaDeclRefs;
3403 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3404 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3405 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3406 }
3407
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003408 RecordData CUDASpecialDeclRefs;
3409 if (Context.getcudaConfigureCallDecl()) {
3410 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3411 }
3412
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003413 // Build a record containing all of the known namespaces.
3414 RecordData KnownNamespaces;
3415 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3416 I = SemaRef.KnownNamespaces.begin(),
3417 IEnd = SemaRef.KnownNamespaces.end();
3418 I != IEnd; ++I) {
3419 if (!I->second)
3420 AddDeclRef(I->first, KnownNamespaces);
3421 }
3422
Sebastian Redl3397c552010-08-18 23:56:27 +00003423 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003424 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003425 Stream.EnterSubblock(AST_BLOCK_ID, 5);
David Blaikie4e4d0842012-03-11 07:00:24 +00003426 WriteLanguageOptions(Context.getLangOpts());
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003427 WriteMetadata(Context, isysroot, OutputFile);
Douglas Gregor832d6202011-07-22 16:35:34 +00003428 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003429 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003430
3431 // Create a lexical update block containing all of the declarations in the
3432 // translation unit that do not come from other AST files.
3433 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3434 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3435 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3436 E = TU->noload_decls_end();
3437 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003438 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003439 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003440 }
3441
3442 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3443 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3444 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3445 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3446 Record.clear();
3447 Record.push_back(TU_UPDATE_LEXICAL);
3448 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3449 data(NewGlobalDecls));
3450
3451 // And a visible updates block for the translation unit.
3452 Abv = new llvm::BitCodeAbbrev();
3453 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3454 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3455 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3456 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3457 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3458 WriteDeclContextVisibleUpdate(TU);
3459
3460 // If the translation unit has an anonymous namespace, and we don't already
3461 // have an update block for it, write it as an update block.
3462 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3463 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3464 if (Record.empty()) {
3465 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003466 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003467 }
3468 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003469
3470 // Make sure visible decls, added to DeclContexts previously loaded from
3471 // an AST file, are registered for serialization.
3472 for (SmallVector<const Decl *, 16>::iterator
3473 I = UpdatingVisibleDecls.begin(),
3474 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3475 GetDeclRef(*I);
3476 }
3477
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003478 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003479 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003480
Douglas Gregora119da02011-08-02 16:26:37 +00003481 // Form the record of special types.
3482 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003483 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003484 AddTypeRef(Context.getFILEType(), SpecialTypes);
3485 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3486 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3487 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3488 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003489 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003490 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003491
Douglas Gregor366809a2009-04-26 03:49:13 +00003492 // Keep writing types and declarations until all types and
3493 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003494 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003495 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003496 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3497 E = DeclsToRewrite.end();
3498 I != E; ++I)
3499 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003500 while (!DeclTypesToEmit.empty()) {
3501 DeclOrType DOT = DeclTypesToEmit.front();
3502 DeclTypesToEmit.pop();
3503 if (DOT.isType())
3504 WriteType(DOT.getType());
3505 else
3506 WriteDecl(Context, DOT.getDecl());
3507 }
3508 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003509
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003510 DoneWritingDeclsAndTypes = true;
3511
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003512 WriteFileDeclIDsMap();
3513 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003514 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003515
3516 if (Chain) {
3517 // Write the mapping information describing our module dependencies and how
3518 // each of those modules were mapped into our own offset/ID space, so that
3519 // the reader can build the appropriate mapping to its own offset/ID space.
3520 // The map consists solely of a blob with the following format:
3521 // *(module-name-len:i16 module-name:len*i8
3522 // source-location-offset:i32
3523 // identifier-id:i32
3524 // preprocessed-entity-id:i32
3525 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003526 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003527 // selector-id:i32
3528 // declaration-id:i32
3529 // c++-base-specifiers-id:i32
3530 // type-id:i32)
3531 //
3532 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3533 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3534 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3535 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003536 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003537 {
3538 llvm::raw_svector_ostream Out(Buffer);
3539 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003540 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003541 M != MEnd; ++M) {
3542 StringRef FileName = (*M)->FileName;
3543 io::Emit16(Out, FileName.size());
3544 Out.write(FileName.data(), FileName.size());
3545 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3546 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003547 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003548 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003549 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003550 io::Emit32(Out, (*M)->BaseSelectorID);
3551 io::Emit32(Out, (*M)->BaseDeclID);
3552 io::Emit32(Out, (*M)->BaseTypeIndex);
3553 }
3554 }
3555 Record.clear();
3556 Record.push_back(MODULE_OFFSET_MAP);
3557 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3558 Buffer.data(), Buffer.size());
3559 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003560 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003561 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003562 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003563 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003564 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003565 WriteFPPragmaOptions(SemaRef.getFPOptions());
3566 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003567
Sebastian Redl1476ed42010-07-16 16:36:56 +00003568 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003569 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003570
Anders Carlssonc8505782011-03-06 18:41:18 +00003571 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003572
Douglas Gregore209e502011-12-06 01:10:29 +00003573 // If we're emitting a module, write out the submodule information.
3574 if (WritingModule)
3575 WriteSubmodules(WritingModule);
3576
Douglas Gregora119da02011-08-02 16:26:37 +00003577 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3578
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003579 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003580 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003581 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003582
3583 // Write the record containing tentative definitions.
3584 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003585 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003586
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003587 // Write the record containing unused file scoped decls.
3588 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003589 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003590
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003591 // Write the record containing weak undeclared identifiers.
3592 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003593 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003594 WeakUndeclaredIdentifiers);
3595
Douglas Gregor14c22f22009-04-22 22:18:58 +00003596 // Write the record containing locally-scoped external definitions.
3597 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003598 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003599 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003600
3601 // Write the record containing ext_vector type names.
3602 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003603 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003604
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003605 // Write the record containing VTable uses information.
3606 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003607 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003608
3609 // Write the record containing dynamic classes declarations.
3610 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003611 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003612
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003613 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003614 if (!PendingInstantiations.empty())
3615 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003616
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003617 // Write the record containing declaration references of Sema.
3618 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003619 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003620
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003621 // Write the record containing CUDA-specific declaration references.
3622 if (!CUDASpecialDeclRefs.empty())
3623 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003624
3625 // Write the delegating constructors.
3626 if (!DelegatingCtorDecls.empty())
3627 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003628
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003629 // Write the known namespaces.
3630 if (!KnownNamespaces.empty())
3631 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3632
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003633 // Write the visible updates to DeclContexts.
3634 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3635 I = UpdatedDeclContexts.begin(),
3636 E = UpdatedDeclContexts.end();
3637 I != E; ++I)
3638 WriteDeclContextVisibleUpdate(*I);
3639
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003640 if (!WritingModule) {
3641 // Write the submodules that were imported, if any.
3642 RecordData ImportedModules;
3643 for (ASTContext::import_iterator I = Context.local_import_begin(),
3644 IEnd = Context.local_import_end();
3645 I != IEnd; ++I) {
3646 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3647 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3648 }
3649 if (!ImportedModules.empty()) {
3650 // Sort module IDs.
3651 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3652
3653 // Unique module IDs.
3654 ImportedModules.erase(std::unique(ImportedModules.begin(),
3655 ImportedModules.end()),
3656 ImportedModules.end());
3657
3658 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3659 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003660 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003661
3662 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003663 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003664 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003665 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003666 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003667 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003668
Douglas Gregor3e1af842009-04-17 22:13:46 +00003669 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003670 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003671 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003672 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003673 Record.push_back(NumLexicalDeclContexts);
3674 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003675 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003676 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003677}
3678
Douglas Gregora8235d62012-10-09 23:05:51 +00003679void ASTWriter::WriteMacroUpdates() {
3680 if (MacroUpdates.empty())
3681 return;
3682
3683 RecordData Record;
3684 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3685 E = MacroUpdates.end();
3686 I != E; ++I) {
3687 addMacroRef(I->first, Record);
3688 AddSourceLocation(I->second.UndefLoc, Record);
3689 }
3690 Stream.EmitRecord(MACRO_UPDATES, Record);
3691}
3692
Douglas Gregor61c5e342011-09-17 00:05:03 +00003693/// \brief Go through the declaration update blocks and resolve declaration
3694/// pointers into declaration IDs.
3695void ASTWriter::ResolveDeclUpdatesBlocks() {
3696 for (DeclUpdateMap::iterator
3697 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3698 const Decl *D = I->first;
3699 UpdateRecord &URec = I->second;
3700
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003701 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003702 continue; // The decl will be written completely
3703
3704 unsigned Idx = 0, N = URec.size();
3705 while (Idx < N) {
3706 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003707 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3708 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3709 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3710 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3711 ++Idx;
3712 break;
3713
3714 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3715 ++Idx;
3716 break;
3717 }
3718 }
3719 }
3720}
3721
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003722void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003723 if (DeclUpdates.empty())
3724 return;
3725
3726 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003727 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003728 for (DeclUpdateMap::iterator
3729 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3730 const Decl *D = I->first;
3731 UpdateRecord &URec = I->second;
3732
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003733 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003734 continue; // The decl will be written completely,no need to store updates.
3735
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003736 uint64_t Offset = Stream.GetCurrentBitNo();
3737 Stream.EmitRecord(DECL_UPDATES, URec);
3738
3739 OffsetsRecord.push_back(GetDeclRef(D));
3740 OffsetsRecord.push_back(Offset);
3741 }
3742 Stream.ExitBlock();
3743 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3744}
3745
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003746void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003747 if (ReplacedDecls.empty())
3748 return;
3749
3750 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003751 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003752 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003753 Record.push_back(I->ID);
3754 Record.push_back(I->Offset);
3755 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003756 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003757 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003758}
3759
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003760void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003761 Record.push_back(Loc.getRawEncoding());
3762}
3763
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003764void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003765 AddSourceLocation(Range.getBegin(), Record);
3766 AddSourceLocation(Range.getEnd(), Record);
3767}
3768
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003769void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003770 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003771 const uint64_t *Words = Value.getRawData();
3772 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003773}
3774
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003775void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003776 Record.push_back(Value.isUnsigned());
3777 AddAPInt(Value, Record);
3778}
3779
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003780void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003781 AddAPInt(Value.bitcastToAPInt(), Record);
3782}
3783
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003784void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003785 Record.push_back(getIdentifierRef(II));
3786}
3787
Douglas Gregora8235d62012-10-09 23:05:51 +00003788void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3789 Record.push_back(getMacroRef(MI));
3790}
3791
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003792IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003793 if (II == 0)
3794 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003795
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003796 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003797 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003798 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003799 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003800}
3801
Douglas Gregora8235d62012-10-09 23:05:51 +00003802MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3803 // Don't emit builtin macros like __LINE__ to the AST file unless they
3804 // have been redefined by the header (in which case they are not
3805 // isBuiltinMacro).
3806 if (MI == 0 || MI->isBuiltinMacro())
3807 return 0;
3808
3809 MacroID &ID = MacroIDs[MI];
3810 if (ID == 0)
3811 ID = NextMacroID++;
3812 return ID;
3813}
3814
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003815void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003816 Record.push_back(getSelectorRef(SelRef));
3817}
3818
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003819SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003820 if (Sel.getAsOpaquePtr() == 0) {
3821 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003822 }
3823
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003824 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003825 if (SID == 0 && Chain) {
3826 // This might trigger a ReadSelector callback, which will set the ID for
3827 // this selector.
3828 Chain->LoadSelector(Sel);
3829 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003830 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003831 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003832 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003833 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003834}
3835
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003836void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003837 AddDeclRef(Temp->getDestructor(), Record);
3838}
3839
Douglas Gregor7c789c12010-10-29 22:39:52 +00003840void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3841 CXXBaseSpecifier const *BasesEnd,
3842 RecordDataImpl &Record) {
3843 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3844 CXXBaseSpecifiersToWrite.push_back(
3845 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3846 Bases, BasesEnd));
3847 Record.push_back(NextCXXBaseSpecifiersID++);
3848}
3849
Sebastian Redla4232eb2010-08-18 23:56:21 +00003850void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003851 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003852 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003853 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003854 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003855 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003856 break;
3857 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003858 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003859 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003860 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003861 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003862 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003863 break;
3864 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003865 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003866 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003867 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003868 break;
John McCall833ca992009-10-29 08:12:44 +00003869 case TemplateArgument::Null:
3870 case TemplateArgument::Integral:
3871 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003872 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003873 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003874 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003875 break;
3876 }
3877}
3878
Sebastian Redla4232eb2010-08-18 23:56:21 +00003879void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003880 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003881 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003882
3883 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3884 bool InfoHasSameExpr
3885 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3886 Record.push_back(InfoHasSameExpr);
3887 if (InfoHasSameExpr)
3888 return; // Avoid storing the same expr twice.
3889 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003890 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3891 Record);
3892}
3893
Douglas Gregordc355712011-02-25 00:36:19 +00003894void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3895 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003896 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003897 AddTypeRef(QualType(), Record);
3898 return;
3899 }
3900
Douglas Gregordc355712011-02-25 00:36:19 +00003901 AddTypeLoc(TInfo->getTypeLoc(), Record);
3902}
3903
3904void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3905 AddTypeRef(TL.getType(), Record);
3906
John McCalla1ee0c52009-10-16 21:56:05 +00003907 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003908 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003909 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003910}
3911
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003912void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003913 Record.push_back(GetOrCreateTypeID(T));
3914}
3915
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003916TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3917 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003918 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3919}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003920
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003921TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003922 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003923 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003924}
3925
3926TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3927 if (T.isNull())
3928 return TypeIdx();
3929 assert(!T.getLocalFastQualifiers());
3930
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003931 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003932 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003933 if (DoneWritingDeclsAndTypes) {
3934 assert(0 && "New type seen after serializing all the types to emit!");
3935 return TypeIdx();
3936 }
3937
Douglas Gregor366809a2009-04-26 03:49:13 +00003938 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003939 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003940 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003941 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003942 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003943 return Idx;
3944}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003945
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003946TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003947 if (T.isNull())
3948 return TypeIdx();
3949 assert(!T.getLocalFastQualifiers());
3950
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003951 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3952 assert(I != TypeIdxs.end() && "Type not emitted!");
3953 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003954}
3955
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003956void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003957 Record.push_back(GetDeclRef(D));
3958}
3959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003960DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003961 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3962
Douglas Gregor2cf26342009-04-09 22:27:44 +00003963 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003964 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003965 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003966
3967 // If D comes from an AST file, its declaration ID is already known and
3968 // fixed.
3969 if (D->isFromASTFile())
3970 return D->getGlobalID();
3971
Douglas Gregor97475832010-10-05 18:37:06 +00003972 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003973 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003974 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003975 if (DoneWritingDeclsAndTypes) {
3976 assert(0 && "New decl seen after serializing all the decls to emit!");
3977 return 0;
3978 }
3979
Douglas Gregor2cf26342009-04-09 22:27:44 +00003980 // We haven't seen this declaration before. Give it a new ID and
3981 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003982 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003983 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003984 }
3985
Sebastian Redl681d7232010-07-27 00:17:23 +00003986 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003987}
3988
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003989DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003990 if (D == 0)
3991 return 0;
3992
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003993 // If D comes from an AST file, its declaration ID is already known and
3994 // fixed.
3995 if (D->isFromASTFile())
3996 return D->getGlobalID();
3997
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003998 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3999 return DeclIDs[D];
4000}
4001
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004002static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4003 std::pair<unsigned, serialization::DeclID> R) {
4004 return L.first < R.first;
4005}
4006
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004007void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004008 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004009 assert(D);
4010
4011 SourceLocation Loc = D->getLocation();
4012 if (Loc.isInvalid())
4013 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004014
4015 // We only keep track of the file-level declarations of each file.
4016 if (!D->getLexicalDeclContext()->isFileContext())
4017 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004018 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4019 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004020 if (isa<ParmVarDecl>(D))
4021 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004022
4023 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004024 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004025 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004026 FileID FID;
4027 unsigned Offset;
4028 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004029 if (FID.isInvalid())
4030 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004031 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004032
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004033 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004034 if (!Info)
4035 Info = new DeclIDInFileInfo();
4036
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004037 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004038 LocDeclIDsTy &Decls = Info->DeclIDs;
4039
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004040 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004041 Decls.push_back(LocDecl);
4042 return;
4043 }
4044
4045 LocDeclIDsTy::iterator
4046 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4047
4048 Decls.insert(I, LocDecl);
4049}
4050
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004051void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004052 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004053 Record.push_back(Name.getNameKind());
4054 switch (Name.getNameKind()) {
4055 case DeclarationName::Identifier:
4056 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4057 break;
4058
4059 case DeclarationName::ObjCZeroArgSelector:
4060 case DeclarationName::ObjCOneArgSelector:
4061 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004062 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004063 break;
4064
4065 case DeclarationName::CXXConstructorName:
4066 case DeclarationName::CXXDestructorName:
4067 case DeclarationName::CXXConversionFunctionName:
4068 AddTypeRef(Name.getCXXNameType(), Record);
4069 break;
4070
4071 case DeclarationName::CXXOperatorName:
4072 Record.push_back(Name.getCXXOverloadedOperator());
4073 break;
4074
Sean Hunt3e518bd2009-11-29 07:34:05 +00004075 case DeclarationName::CXXLiteralOperatorName:
4076 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4077 break;
4078
Douglas Gregor2cf26342009-04-09 22:27:44 +00004079 case DeclarationName::CXXUsingDirective:
4080 // No extra data to emit
4081 break;
4082 }
4083}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004084
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004085void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004086 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004087 switch (Name.getNameKind()) {
4088 case DeclarationName::CXXConstructorName:
4089 case DeclarationName::CXXDestructorName:
4090 case DeclarationName::CXXConversionFunctionName:
4091 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4092 break;
4093
4094 case DeclarationName::CXXOperatorName:
4095 AddSourceLocation(
4096 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4097 Record);
4098 AddSourceLocation(
4099 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4100 Record);
4101 break;
4102
4103 case DeclarationName::CXXLiteralOperatorName:
4104 AddSourceLocation(
4105 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4106 Record);
4107 break;
4108
4109 case DeclarationName::Identifier:
4110 case DeclarationName::ObjCZeroArgSelector:
4111 case DeclarationName::ObjCOneArgSelector:
4112 case DeclarationName::ObjCMultiArgSelector:
4113 case DeclarationName::CXXUsingDirective:
4114 break;
4115 }
4116}
4117
4118void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004119 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004120 AddDeclarationName(NameInfo.getName(), Record);
4121 AddSourceLocation(NameInfo.getLoc(), Record);
4122 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4123}
4124
4125void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004126 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004127 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004128 Record.push_back(Info.NumTemplParamLists);
4129 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4130 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4131}
4132
Sebastian Redla4232eb2010-08-18 23:56:21 +00004133void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004134 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004135 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004136 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004137 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004138
4139 // Push each of the NNS's onto a stack for serialization in reverse order.
4140 while (NNS) {
4141 NestedNames.push_back(NNS);
4142 NNS = NNS->getPrefix();
4143 }
4144
4145 Record.push_back(NestedNames.size());
4146 while(!NestedNames.empty()) {
4147 NNS = NestedNames.pop_back_val();
4148 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4149 Record.push_back(Kind);
4150 switch (Kind) {
4151 case NestedNameSpecifier::Identifier:
4152 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4153 break;
4154
4155 case NestedNameSpecifier::Namespace:
4156 AddDeclRef(NNS->getAsNamespace(), Record);
4157 break;
4158
Douglas Gregor14aba762011-02-24 02:36:08 +00004159 case NestedNameSpecifier::NamespaceAlias:
4160 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4161 break;
4162
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004163 case NestedNameSpecifier::TypeSpec:
4164 case NestedNameSpecifier::TypeSpecWithTemplate:
4165 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4166 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4167 break;
4168
4169 case NestedNameSpecifier::Global:
4170 // Don't need to write an associated value.
4171 break;
4172 }
4173 }
4174}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004175
Douglas Gregordc355712011-02-25 00:36:19 +00004176void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4177 RecordDataImpl &Record) {
4178 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004179 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004180 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004181
4182 // Push each of the nested-name-specifiers's onto a stack for
4183 // serialization in reverse order.
4184 while (NNS) {
4185 NestedNames.push_back(NNS);
4186 NNS = NNS.getPrefix();
4187 }
4188
4189 Record.push_back(NestedNames.size());
4190 while(!NestedNames.empty()) {
4191 NNS = NestedNames.pop_back_val();
4192 NestedNameSpecifier::SpecifierKind Kind
4193 = NNS.getNestedNameSpecifier()->getKind();
4194 Record.push_back(Kind);
4195 switch (Kind) {
4196 case NestedNameSpecifier::Identifier:
4197 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4198 AddSourceRange(NNS.getLocalSourceRange(), Record);
4199 break;
4200
4201 case NestedNameSpecifier::Namespace:
4202 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4203 AddSourceRange(NNS.getLocalSourceRange(), Record);
4204 break;
4205
4206 case NestedNameSpecifier::NamespaceAlias:
4207 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4208 AddSourceRange(NNS.getLocalSourceRange(), Record);
4209 break;
4210
4211 case NestedNameSpecifier::TypeSpec:
4212 case NestedNameSpecifier::TypeSpecWithTemplate:
4213 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4214 AddTypeLoc(NNS.getTypeLoc(), Record);
4215 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4216 break;
4217
4218 case NestedNameSpecifier::Global:
4219 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4220 break;
4221 }
4222 }
4223}
4224
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004225void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004226 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004227 Record.push_back(Kind);
4228 switch (Kind) {
4229 case TemplateName::Template:
4230 AddDeclRef(Name.getAsTemplateDecl(), Record);
4231 break;
4232
4233 case TemplateName::OverloadedTemplate: {
4234 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4235 Record.push_back(OvT->size());
4236 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4237 I != E; ++I)
4238 AddDeclRef(*I, Record);
4239 break;
4240 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004241
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004242 case TemplateName::QualifiedTemplate: {
4243 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4244 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4245 Record.push_back(QualT->hasTemplateKeyword());
4246 AddDeclRef(QualT->getTemplateDecl(), Record);
4247 break;
4248 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004249
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004250 case TemplateName::DependentTemplate: {
4251 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4252 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4253 Record.push_back(DepT->isIdentifier());
4254 if (DepT->isIdentifier())
4255 AddIdentifierRef(DepT->getIdentifier(), Record);
4256 else
4257 Record.push_back(DepT->getOperator());
4258 break;
4259 }
John McCall14606042011-06-30 08:33:18 +00004260
4261 case TemplateName::SubstTemplateTemplateParm: {
4262 SubstTemplateTemplateParmStorage *subst
4263 = Name.getAsSubstTemplateTemplateParm();
4264 AddDeclRef(subst->getParameter(), Record);
4265 AddTemplateName(subst->getReplacement(), Record);
4266 break;
4267 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004268
4269 case TemplateName::SubstTemplateTemplateParmPack: {
4270 SubstTemplateTemplateParmPackStorage *SubstPack
4271 = Name.getAsSubstTemplateTemplateParmPack();
4272 AddDeclRef(SubstPack->getParameterPack(), Record);
4273 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4274 break;
4275 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004276 }
4277}
4278
Michael J. Spencer20249a12010-10-21 03:16:25 +00004279void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004280 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004281 Record.push_back(Arg.getKind());
4282 switch (Arg.getKind()) {
4283 case TemplateArgument::Null:
4284 break;
4285 case TemplateArgument::Type:
4286 AddTypeRef(Arg.getAsType(), Record);
4287 break;
4288 case TemplateArgument::Declaration:
4289 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004290 Record.push_back(Arg.isDeclForReferenceParam());
4291 break;
4292 case TemplateArgument::NullPtr:
4293 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004294 break;
4295 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004296 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004297 AddTypeRef(Arg.getIntegralType(), Record);
4298 break;
4299 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004300 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4301 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004302 case TemplateArgument::TemplateExpansion:
4303 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004304 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4305 Record.push_back(*NumExpansions + 1);
4306 else
4307 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004308 break;
4309 case TemplateArgument::Expression:
4310 AddStmt(Arg.getAsExpr());
4311 break;
4312 case TemplateArgument::Pack:
4313 Record.push_back(Arg.pack_size());
4314 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4315 I != E; ++I)
4316 AddTemplateArgument(*I, Record);
4317 break;
4318 }
4319}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004320
4321void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004322ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004323 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004324 assert(TemplateParams && "No TemplateParams!");
4325 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4326 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4327 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4328 Record.push_back(TemplateParams->size());
4329 for (TemplateParameterList::const_iterator
4330 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4331 P != PEnd; ++P)
4332 AddDeclRef(*P, Record);
4333}
4334
4335/// \brief Emit a template argument list.
4336void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004337ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004338 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004339 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004340 Record.push_back(TemplateArgs->size());
4341 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004342 AddTemplateArgument(TemplateArgs->get(i), Record);
4343}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004344
4345
4346void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004347ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004348 Record.push_back(Set.size());
4349 for (UnresolvedSetImpl::const_iterator
4350 I = Set.begin(), E = Set.end(); I != E; ++I) {
4351 AddDeclRef(I.getDecl(), Record);
4352 Record.push_back(I.getAccess());
4353 }
4354}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004355
Sebastian Redla4232eb2010-08-18 23:56:21 +00004356void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004357 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004358 Record.push_back(Base.isVirtual());
4359 Record.push_back(Base.isBaseOfClass());
4360 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004361 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004362 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004363 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004364 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4365 : SourceLocation(),
4366 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004367}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004368
Douglas Gregor7c789c12010-10-29 22:39:52 +00004369void ASTWriter::FlushCXXBaseSpecifiers() {
4370 RecordData Record;
4371 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4372 Record.clear();
4373
4374 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004375 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004376 if (Index == CXXBaseSpecifiersOffsets.size())
4377 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4378 else {
4379 if (Index > CXXBaseSpecifiersOffsets.size())
4380 CXXBaseSpecifiersOffsets.resize(Index + 1);
4381 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4382 }
4383
4384 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4385 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4386 Record.push_back(BEnd - B);
4387 for (; B != BEnd; ++B)
4388 AddCXXBaseSpecifier(*B, Record);
4389 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004390
4391 // Flush any expressions that were written as part of the base specifiers.
4392 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004393 }
4394
4395 CXXBaseSpecifiersToWrite.clear();
4396}
4397
Sean Huntcbb67482011-01-08 20:30:50 +00004398void ASTWriter::AddCXXCtorInitializers(
4399 const CXXCtorInitializer * const *CtorInitializers,
4400 unsigned NumCtorInitializers,
4401 RecordDataImpl &Record) {
4402 Record.push_back(NumCtorInitializers);
4403 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4404 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004405
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004406 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004407 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004408 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004409 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004410 } else if (Init->isDelegatingInitializer()) {
4411 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004412 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004413 } else if (Init->isMemberInitializer()){
4414 Record.push_back(CTOR_INITIALIZER_MEMBER);
4415 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004416 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004417 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4418 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004419 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004420
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004421 AddSourceLocation(Init->getMemberLocation(), Record);
4422 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004423 AddSourceLocation(Init->getLParenLoc(), Record);
4424 AddSourceLocation(Init->getRParenLoc(), Record);
4425 Record.push_back(Init->isWritten());
4426 if (Init->isWritten()) {
4427 Record.push_back(Init->getSourceOrder());
4428 } else {
4429 Record.push_back(Init->getNumArrayIndices());
4430 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4431 AddDeclRef(Init->getArrayIndex(i), Record);
4432 }
4433 }
4434}
4435
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004436void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4437 assert(D->DefinitionData);
4438 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004439 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004440 Record.push_back(Data.UserDeclaredConstructor);
4441 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004442 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004443 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004444 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004445 Record.push_back(Data.UserDeclaredDestructor);
4446 Record.push_back(Data.Aggregate);
4447 Record.push_back(Data.PlainOldData);
4448 Record.push_back(Data.Empty);
4449 Record.push_back(Data.Polymorphic);
4450 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004451 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004452 Record.push_back(Data.HasNoNonEmptyBases);
4453 Record.push_back(Data.HasPrivateFields);
4454 Record.push_back(Data.HasProtectedFields);
4455 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004456 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004457 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004458 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004459 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004460 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004461 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004462 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004463 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004464 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004465 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004466 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004467 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004468 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004469 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004470 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004471 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004472 Record.push_back(Data.DeclaredDefaultConstructor);
4473 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004474 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004475 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004476 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004477 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004478 Record.push_back(Data.FailedImplicitMoveConstructor);
4479 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004480 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004481
4482 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004483 if (Data.NumBases > 0)
4484 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4485 Record);
4486
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004487 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4488 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004489 if (Data.NumVBases > 0)
4490 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4491 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004492
4493 AddUnresolvedSet(Data.Conversions, Record);
4494 AddUnresolvedSet(Data.VisibleConversions, Record);
4495 // Data.Definition is the owning decl, no need to write it.
4496 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004497
4498 // Add lambda-specific data.
4499 if (Data.IsLambda) {
4500 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004501 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004502 Record.push_back(Lambda.NumCaptures);
4503 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004504 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004505 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004506 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004507 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4508 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4509 AddSourceLocation(Capture.getLocation(), Record);
4510 Record.push_back(Capture.isImplicit());
4511 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4512 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4513 AddDeclRef(Var, Record);
4514 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4515 : SourceLocation(),
4516 Record);
4517 }
4518 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004519}
4520
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004521void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004522 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004523 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004524 assert(FirstDeclID == NextDeclID &&
4525 FirstTypeID == NextTypeID &&
4526 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004527 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004528 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004529 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004530 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004531
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004532 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004533
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004534 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4535 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4536 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004537 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004538 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004539 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004540 NextDeclID = FirstDeclID;
4541 NextTypeID = FirstTypeID;
4542 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004543 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004544 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004545 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004546}
4547
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004548void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004549 IdentifierIDs[II] = ID;
4550}
4551
Douglas Gregora8235d62012-10-09 23:05:51 +00004552void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4553 MacroIDs[MI] = ID;
4554}
4555
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004556void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004557 // Always take the highest-numbered type index. This copes with an interesting
4558 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004559 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004560 // keep the higher-numbered entry so that we can properly write it out to
4561 // the AST file.
4562 TypeIdx &StoredIdx = TypeIdxs[T];
4563 if (Idx.getIndex() >= StoredIdx.getIndex())
4564 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004565}
4566
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004567void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004568 SelectorIDs[S] = ID;
4569}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004570
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004571void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004572 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004573 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004574 MacroDefinitions[MD] = ID;
4575}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004576
Douglas Gregora015cab2011-12-02 17:30:13 +00004577void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4578 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4579 SubmoduleIDs[Mod] = ID;
4580}
4581
Douglas Gregora8235d62012-10-09 23:05:51 +00004582void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4583 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4584}
4585
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004586void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004587 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004588 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004589 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4590 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004591 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004592 // A forward reference was mutated into a definition. Rewrite it.
4593 // FIXME: This happens during template instantiation, should we
4594 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004595 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004596 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004597 }
4598}
Douglas Gregora8235d62012-10-09 23:05:51 +00004599
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004600void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004601 assert(!WritingAST && "Already writing the AST!");
4602
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004603 // TU and namespaces are handled elsewhere.
4604 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4605 return;
4606
Douglas Gregor919814d2011-09-09 23:01:35 +00004607 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004608 return; // Not a source decl added to a DeclContext from PCH.
4609
4610 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004611 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004612}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004613
4614void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004615 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004616 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004617 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004618 return; // Not a source member added to a class from PCH.
4619 if (!isa<CXXMethodDecl>(D))
4620 return; // We are interested in lazily declared implicit methods.
4621
4622 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004623 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004624 UpdateRecord &Record = DeclUpdates[RD];
4625 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004626 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004627}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004628
4629void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4630 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004631 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004632 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004633 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004634 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004635 return; // Not a source specialization added to a template from PCH.
4636
4637 UpdateRecord &Record = DeclUpdates[TD];
4638 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004639 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004640}
Douglas Gregor89d99802010-11-30 06:16:57 +00004641
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004642void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4643 const FunctionDecl *D) {
4644 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004645 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004646 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004647 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004648 return; // Not a source specialization added to a template from PCH.
4649
4650 UpdateRecord &Record = DeclUpdates[TD];
4651 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004652 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004653}
4654
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004655void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004656 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004657 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004658 return; // Declaration not imported from PCH.
4659
4660 // Implicit decl from a PCH was defined.
4661 // FIXME: Should implicit definition be a separate FunctionDecl?
4662 RewriteDecl(D);
4663}
4664
Sebastian Redlf79a7192011-04-29 08:19:30 +00004665void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004666 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004667 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004668 return;
4669
4670 // Since the actual instantiation is delayed, this really means that we need
4671 // to update the instantiation location.
4672 UpdateRecord &Record = DeclUpdates[D];
4673 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4674 AddSourceLocation(
4675 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4676}
4677
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004678void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4679 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004680 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004681 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004682 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004683
4684 assert(IFD->getDefinition() && "Category on a class without a definition?");
4685 ObjCClassesWithCategories.insert(
4686 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004687}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004688
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004689
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004690void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4691 const ObjCPropertyDecl *OrigProp,
4692 const ObjCCategoryDecl *ClassExt) {
4693 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4694 if (!D)
4695 return;
4696
4697 assert(!WritingAST && "Already writing the AST!");
4698 if (!D->isFromASTFile())
4699 return; // Declaration not imported from PCH.
4700
4701 RewriteDecl(D);
4702}