blob: d04753a037ddce2a93d0706ae497dbf074cf1587 [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 Gregor2b49d1f2012-10-15 06:28:11 +00001986 Abbrev = new BitCodeAbbrev();
1987 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
1988 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1989 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
1990
Douglas Gregor26ced122011-12-01 00:59:36 +00001991 // Write the submodule metadata block.
1992 RecordData Record;
1993 Record.push_back(getNumberOfModules(WritingModule));
1994 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1995 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1996
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001997 // Write all of the submodules.
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001998 std::queue<Module *> Q;
Douglas Gregor392ed2b2011-11-30 17:33:56 +00001999 Q.push(WritingModule);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002000 while (!Q.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002001 Module *Mod = Q.front();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002002 Q.pop();
Douglas Gregore209e502011-12-06 01:10:29 +00002003 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002004
2005 // Emit the definition of the block.
2006 Record.clear();
2007 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregore209e502011-12-06 01:10:29 +00002008 Record.push_back(ID);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002009 if (Mod->Parent) {
2010 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2011 Record.push_back(SubmoduleIDs[Mod->Parent]);
2012 } else {
2013 Record.push_back(0);
2014 }
2015 Record.push_back(Mod->IsFramework);
2016 Record.push_back(Mod->IsExplicit);
Douglas Gregora1f1fad2012-01-27 19:52:33 +00002017 Record.push_back(Mod->IsSystem);
Douglas Gregor1e123682011-12-05 22:27:44 +00002018 Record.push_back(Mod->InferSubmodules);
2019 Record.push_back(Mod->InferExplicitSubmodules);
2020 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002021 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2022
Douglas Gregor51f564f2011-12-31 04:05:44 +00002023 // Emit the requirements.
2024 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2025 Record.clear();
2026 Record.push_back(SUBMODULE_REQUIRES);
2027 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2028 Mod->Requires[I].data(),
2029 Mod->Requires[I].size());
2030 }
2031
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002032 // Emit the umbrella header, if there is one.
Douglas Gregor10694ce2011-12-08 17:39:04 +00002033 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002034 Record.clear();
Douglas Gregor77d029f2011-12-08 19:11:24 +00002035 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002036 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor10694ce2011-12-08 17:39:04 +00002037 UmbrellaHeader->getName());
Douglas Gregor77d029f2011-12-08 19:11:24 +00002038 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2039 Record.clear();
2040 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2041 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2042 UmbrellaDir->getName());
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002043 }
2044
2045 // Emit the headers.
2046 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2047 Record.clear();
2048 Record.push_back(SUBMODULE_HEADER);
2049 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2050 Mod->Headers[I]->getName());
2051 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +00002052 // Emit the excluded headers.
2053 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2054 Record.clear();
2055 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2056 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2057 Mod->ExcludedHeaders[I]->getName());
2058 }
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +00002059 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2060 Record.clear();
2061 Record.push_back(SUBMODULE_TOPHEADER);
2062 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2063 Mod->TopHeaders[I]->getName());
2064 }
Douglas Gregor55988682011-12-05 16:33:54 +00002065
2066 // Emit the imports.
2067 if (!Mod->Imports.empty()) {
2068 Record.clear();
2069 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002070 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor55988682011-12-05 16:33:54 +00002071 assert(ImportedID && "Unknown submodule!");
2072 Record.push_back(ImportedID);
2073 }
2074 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2075 }
2076
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002077 // Emit the exports.
2078 if (!Mod->Exports.empty()) {
2079 Record.clear();
2080 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregorbab9f4a2011-12-12 23:17:57 +00002081 if (Module *Exported = Mod->Exports[I].getPointer()) {
2082 unsigned ExportedID = SubmoduleIDs[Exported];
2083 assert(ExportedID > 0 && "Unknown submodule ID?");
2084 Record.push_back(ExportedID);
2085 } else {
2086 Record.push_back(0);
2087 }
2088
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00002089 Record.push_back(Mod->Exports[I].getInt());
2090 }
2091 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2092 }
2093
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002094 // Queue up the submodules of this module.
Douglas Gregorb7a78192012-01-04 23:32:19 +00002095 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2096 SubEnd = Mod->submodule_end();
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002097 Sub != SubEnd; ++Sub)
Douglas Gregorb7a78192012-01-04 23:32:19 +00002098 Q.push(*Sub);
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002099 }
2100
2101 Stream.ExitBlock();
Douglas Gregore209e502011-12-06 01:10:29 +00002102
2103 assert((NextSubmoduleID - FirstSubmoduleID
2104 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor392ed2b2011-11-30 17:33:56 +00002105}
2106
Douglas Gregor185dbd72011-12-01 02:07:58 +00002107serialization::SubmoduleID
2108ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregore209e502011-12-06 01:10:29 +00002109 if (Loc.isInvalid() || !WritingModule)
Douglas Gregor185dbd72011-12-01 02:07:58 +00002110 return 0; // No submodule
Douglas Gregor55988682011-12-05 16:33:54 +00002111
2112 // Find the module that owns this location.
Douglas Gregor185dbd72011-12-01 02:07:58 +00002113 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor55988682011-12-05 16:33:54 +00002114 Module *OwningMod
2115 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregor185dbd72011-12-01 02:07:58 +00002116 if (!OwningMod)
2117 return 0;
2118
Douglas Gregore209e502011-12-06 01:10:29 +00002119 // Check whether this submodule is part of our own module.
2120 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregor185dbd72011-12-01 02:07:58 +00002121 return 0;
2122
Douglas Gregore209e502011-12-06 01:10:29 +00002123 return getSubmoduleID(OwningMod);
Douglas Gregor185dbd72011-12-01 02:07:58 +00002124}
2125
David Blaikied6471f72011-09-25 23:23:43 +00002126void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002127 RecordData Record;
David Blaikied6471f72011-09-25 23:23:43 +00002128 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002129 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2130 I != E; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +00002131 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002132 if (point.Loc.isInvalid())
2133 continue;
2134
2135 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbarba494c62011-09-29 01:42:25 +00002136 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002137 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbarb1c99c62011-09-29 01:30:00 +00002138 if (I->second.isPragma()) {
2139 Record.push_back(I->first);
2140 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002141 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002142 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002143 Record.push_back(-1); // mark the end of the diag/map pairs for this
2144 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002145 }
2146
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00002147 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002148 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002149}
2150
Anders Carlssonc8505782011-03-06 18:41:18 +00002151void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2152 if (CXXBaseSpecifiersOffsets.empty())
2153 return;
2154
2155 RecordData Record;
2156
2157 // Create a blob abbreviation for the C++ base specifiers offsets.
2158 using namespace llvm;
2159
2160 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2161 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2163 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2164 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2165
Douglas Gregore92b8a12011-08-04 00:01:48 +00002166 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00002167 Record.clear();
2168 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2169 Record.push_back(CXXBaseSpecifiersOffsets.size());
2170 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002171 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00002172}
2173
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002174//===----------------------------------------------------------------------===//
2175// Type Serialization
2176//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00002177
Sebastian Redl3397c552010-08-18 23:56:27 +00002178/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002179void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002180 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002181 if (Idx.getIndex() == 0) // we haven't seen this type before.
2182 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00002183
Douglas Gregor97475832010-10-05 18:37:06 +00002184 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00002185
Douglas Gregor2cf26342009-04-09 22:27:44 +00002186 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002187 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00002188 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00002189 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00002190 else if (TypeOffsets.size() < Index) {
2191 TypeOffsets.resize(Index + 1);
2192 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002193 }
2194
2195 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Douglas Gregor2cf26342009-04-09 22:27:44 +00002197 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00002198 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00002199
Douglas Gregora4923eb2009-11-16 21:35:15 +00002200 if (T.hasLocalNonFastQualifiers()) {
2201 Qualifiers Qs = T.getLocalQualifiers();
2202 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00002203 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002204 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00002205 } else {
2206 switch (T->getTypeClass()) {
2207 // For all of the concrete, non-dependent types, call the
2208 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002209#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00002210 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002211#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00002212#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00002213 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002214 }
2215
2216 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002217 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00002218
2219 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002220 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002221}
2222
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002223//===----------------------------------------------------------------------===//
2224// Declaration Serialization
2225//===----------------------------------------------------------------------===//
2226
Douglas Gregor2cf26342009-04-09 22:27:44 +00002227/// \brief Write the block containing all of the declaration IDs
2228/// lexically declared within the given DeclContext.
2229///
2230/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2231/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002232uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002233 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002234 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002235 return 0;
2236
Douglas Gregorc9490c02009-04-16 22:23:12 +00002237 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002238 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002239 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002240 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002241 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2242 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002243 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002244
Douglas Gregor25123082009-04-22 22:34:57 +00002245 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002246 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002247 return Offset;
2248}
2249
Sebastian Redla4232eb2010-08-18 23:56:21 +00002250void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002251 using namespace llvm;
2252 RecordData Record;
2253
2254 // Write the type offsets array
2255 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002256 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2260 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2261 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002262 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002263 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002264 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002265 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002266
2267 // Write the declaration offsets array
2268 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002269 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2273 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2274 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002275 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002276 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002277 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002278 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002279}
2280
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002281void ASTWriter::WriteFileDeclIDsMap() {
2282 using namespace llvm;
2283 RecordData Record;
2284
2285 // Join the vectors of DeclIDs from all files.
2286 SmallVector<DeclID, 256> FileSortedIDs;
2287 for (FileDeclIDsTy::iterator
2288 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2289 DeclIDInFileInfo &Info = *FI->second;
2290 Info.FirstDeclIndex = FileSortedIDs.size();
2291 for (LocDeclIDsTy::iterator
2292 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2293 FileSortedIDs.push_back(DI->second);
2294 }
2295
2296 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2297 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2300 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2301 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis2093e0b2012-10-02 21:09:13 +00002302 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00002303 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2304}
2305
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002306void ASTWriter::WriteComments() {
2307 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002308 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002309 RecordData Record;
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002310 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2311 E = RawComments.end();
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002312 I != E; ++I) {
2313 Record.clear();
Dmitri Gribenko811c8202012-07-06 18:19:34 +00002314 AddSourceRange((*I)->getSourceRange(), Record);
2315 Record.push_back((*I)->getKind());
2316 Record.push_back((*I)->isTrailingComment());
2317 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00002318 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2319 }
2320 Stream.ExitBlock();
2321}
2322
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002323//===----------------------------------------------------------------------===//
2324// Global Method Pool and Selector Serialization
2325//===----------------------------------------------------------------------===//
2326
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002327namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002328// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002329class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002330 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002331
2332public:
2333 typedef Selector key_type;
2334 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Sebastian Redl5d050072010-08-04 17:20:04 +00002336 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002337 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002338 ObjCMethodList Instance, Factory;
2339 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002340 typedef const data_type& data_type_ref;
2341
Sebastian Redl3397c552010-08-18 23:56:27 +00002342 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002343
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002344 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002345 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002346 }
Mike Stump1eb44332009-09-09 15:08:12 +00002347
2348 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002349 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002350 data_type_ref Methods) {
2351 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2352 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002353 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2354 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002355 Method = Method->Next)
2356 if (Method->Method)
2357 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002358 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002359 Method = Method->Next)
2360 if (Method->Method)
2361 DataLen += 4;
2362 clang::io::Emit16(Out, DataLen);
2363 return std::make_pair(KeyLen, DataLen);
2364 }
Mike Stump1eb44332009-09-09 15:08:12 +00002365
Chris Lattner5f9e2722011-07-23 10:55:15 +00002366 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002367 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002368 assert((Start >> 32) == 0 && "Selector key offset too large");
2369 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002370 unsigned N = Sel.getNumArgs();
2371 clang::io::Emit16(Out, N);
2372 if (N == 0)
2373 N = 1;
2374 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002375 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002376 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2377 }
Mike Stump1eb44332009-09-09 15:08:12 +00002378
Chris Lattner5f9e2722011-07-23 10:55:15 +00002379 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002380 data_type_ref Methods, unsigned DataLen) {
2381 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002382 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002383 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002384 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002385 Method = Method->Next)
2386 if (Method->Method)
2387 ++NumInstanceMethods;
2388
2389 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002390 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002391 Method = Method->Next)
2392 if (Method->Method)
2393 ++NumFactoryMethods;
2394
2395 clang::io::Emit16(Out, NumInstanceMethods);
2396 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002397 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002398 Method = Method->Next)
2399 if (Method->Method)
2400 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002401 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002402 Method = Method->Next)
2403 if (Method->Method)
2404 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002405
2406 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002407 }
2408};
2409} // end anonymous namespace
2410
Sebastian Redl059612d2010-08-03 21:58:15 +00002411/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002412///
2413/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002414/// in an on-disk hash table indexed by the selector. The hash table also
2415/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002416void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002417 using namespace llvm;
2418
Sebastian Redl059612d2010-08-03 21:58:15 +00002419 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002420 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002421 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002422 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002423 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002424 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002425 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002426 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002427
Sebastian Redl059612d2010-08-03 21:58:15 +00002428 // Create the on-disk hash table representation. We walk through every
2429 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002430 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002431 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002432 I = SelectorIDs.begin(), E = SelectorIDs.end();
2433 I != E; ++I) {
2434 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002435 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002436 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002437 I->second,
2438 ObjCMethodList(),
2439 ObjCMethodList()
2440 };
2441 if (F != SemaRef.MethodPool.end()) {
2442 Data.Instance = F->second.first;
2443 Data.Factory = F->second.second;
2444 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002445 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002446 // changed.
2447 if (Chain && I->second < FirstSelectorID) {
2448 // Selector already exists. Did it change?
2449 bool changed = false;
2450 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2451 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002452 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002453 changed = true;
2454 }
2455 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2456 M = M->Next) {
Douglas Gregor919814d2011-09-09 23:01:35 +00002457 if (!M->Method->isFromASTFile())
Sebastian Redle58aa892010-08-04 18:21:41 +00002458 changed = true;
2459 }
2460 if (!changed)
2461 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002462 } else if (Data.Instance.Method || Data.Factory.Method) {
2463 // A new method pool entry.
2464 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002465 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002466 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002467 }
2468
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002469 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002470 SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002471 uint32_t BucketOffset;
2472 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002473 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002474 llvm::raw_svector_ostream Out(MethodPool);
2475 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002476 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002477 BucketOffset = Generator.Emit(Out, Trait);
2478 }
2479
2480 // Create a blob abbreviation
2481 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002483 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002484 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002485 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2486 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2487
Douglas Gregor83941df2009-04-25 17:48:32 +00002488 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002489 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002490 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002491 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002492 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002493 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002494
2495 // Create a blob abbreviation for the selector table offsets.
2496 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002497 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2501 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2502
2503 // Write the selector offsets table.
2504 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002505 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002506 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002507 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002508 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002509 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002510 }
2511}
2512
Sebastian Redl3397c552010-08-18 23:56:27 +00002513/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002514void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002515 using namespace llvm;
2516 if (SemaRef.ReferencedSelectors.empty())
2517 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002518
Fariborz Jahanian32019832010-07-23 19:11:11 +00002519 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002520
Sebastian Redl3397c552010-08-18 23:56:27 +00002521 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002522 // very tricky to fix, and given that @selector shouldn't really appear in
2523 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002524 for (DenseMap<Selector, SourceLocation>::iterator S =
2525 SemaRef.ReferencedSelectors.begin(),
2526 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2527 Selector Sel = (*S).first;
2528 SourceLocation Loc = (*S).second;
2529 AddSelectorRef(Sel, Record);
2530 AddSourceLocation(Loc, Record);
2531 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002532 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002533}
2534
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002535//===----------------------------------------------------------------------===//
2536// Identifier Table Serialization
2537//===----------------------------------------------------------------------===//
2538
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002539namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002540class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002541 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002542 Preprocessor &PP;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002543 IdentifierResolver &IdResolver;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002544 bool IsModule;
2545
Douglas Gregora92193e2009-04-28 21:18:29 +00002546 /// \brief Determines whether this is an "interesting" identifier
2547 /// that needs a full IdentifierInfo structure written into the hash
2548 /// table.
Douglas Gregor7143aab2011-09-01 17:04:32 +00002549 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor7143aab2011-09-01 17:04:32 +00002550 if (II->isPoisoned() ||
2551 II->isExtensionToken() ||
2552 II->getObjCOrBuiltinID() ||
Douglas Gregoreee242f2011-10-27 09:33:13 +00002553 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor7143aab2011-09-01 17:04:32 +00002554 II->getFETokenInfo<void>())
2555 return true;
2556
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002557 return hadMacroDefinition(II, Macro);
Douglas Gregorce835df2011-09-14 22:14:14 +00002558 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002559
2560 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2561 if (!II->hadMacroDefinition())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002562 return false;
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002563
2564 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregoraa93a872011-10-17 15:32:29 +00002565 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002566
2567 return false;
Douglas Gregora92193e2009-04-28 21:18:29 +00002568 }
2569
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002570public:
Douglas Gregor7143aab2011-09-01 17:04:32 +00002571 typedef IdentifierInfo* key_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002572 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002573
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002574 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002575 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Douglas Gregoreee242f2011-10-27 09:33:13 +00002577 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2578 IdentifierResolver &IdResolver, bool IsModule)
2579 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002580
2581 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002582 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002583 }
Mike Stump1eb44332009-09-09 15:08:12 +00002584
2585 std::pair<unsigned,unsigned>
Douglas Gregoreee242f2011-10-27 09:33:13 +00002586 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002587 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002588 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregorce835df2011-09-14 22:14:14 +00002589 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002590 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002591 DataLen += 2; // 2 bytes for builtin ID
2592 DataLen += 2; // 2 bytes for flags
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002593 if (hadMacroDefinition(II, Macro)) {
2594 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2595 if (Writer.getMacroRef(M) != 0)
2596 DataLen += 4;
2597 }
2598
2599 DataLen += 4;
2600 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002601
Douglas Gregoreee242f2011-10-27 09:33:13 +00002602 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2603 DEnd = IdResolver.end();
Douglas Gregora92193e2009-04-28 21:18:29 +00002604 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002605 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002606 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002607 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002608 // We emit the key length after the data length so that every
2609 // string is preceded by a 16-bit length. This matches the PTH
2610 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002611 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002612 return std::make_pair(KeyLen, DataLen);
2613 }
Mike Stump1eb44332009-09-09 15:08:12 +00002614
Chris Lattner5f9e2722011-07-23 10:55:15 +00002615 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002616 unsigned KeyLen) {
2617 // Record the location of the key data. This is used when generating
2618 // the mapping from persistent IDs to strings.
2619 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002620 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002621 }
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Douglas Gregor7143aab2011-09-01 17:04:32 +00002623 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002624 IdentID ID, unsigned) {
Douglas Gregorce835df2011-09-14 22:14:14 +00002625 MacroInfo *Macro = 0;
Douglas Gregor7143aab2011-09-01 17:04:32 +00002626 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002627 clang::io::Emit32(Out, ID << 1);
2628 return;
2629 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002630
Douglas Gregora92193e2009-04-28 21:18:29 +00002631 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002632 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2633 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2634 clang::io::Emit16(Out, Bits);
2635 Bits = 0;
2636 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002637 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002638 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2639 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002640 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002641 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002642 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002643
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002644 if (HadMacroDefinition) {
Douglas Gregor6c6c54a2012-10-11 00:46:49 +00002645 // Write all of the macro IDs associated with this identifier.
2646 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2647 if (MacroID ID = Writer.getMacroRef(M))
2648 clang::io::Emit32(Out, ID);
2649 }
2650
2651 clang::io::Emit32(Out, 0);
Douglas Gregor13292642011-12-02 15:45:10 +00002652 }
Alexander Kornienko4d7e0ce2012-09-25 17:18:14 +00002653
Douglas Gregor668c1a42009-04-21 22:25:48 +00002654 // Emit the declaration IDs in reverse order, because the
2655 // IdentifierResolver provides the declarations as they would be
2656 // visible (e.g., the function "stat" would come before the struct
Douglas Gregoreee242f2011-10-27 09:33:13 +00002657 // "stat"), but the ASTReader adds declarations to the end of the list
2658 // (so we need to see the struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002659 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002660 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2661 IdResolver.end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002662 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregoreee242f2011-10-27 09:33:13 +00002663 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002664 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002665 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002666 }
2667};
2668} // end anonymous namespace
2669
Sebastian Redl3397c552010-08-18 23:56:27 +00002670/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002671///
2672/// The identifier table consists of a blob containing string data
2673/// (the actual identifiers themselves) and a separate "offsets" index
2674/// that maps identifier IDs to locations within the blob.
Douglas Gregoreee242f2011-10-27 09:33:13 +00002675void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2676 IdentifierResolver &IdResolver,
2677 bool IsModule) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002678 using namespace llvm;
2679
2680 // Create and write out the blob that contains the identifier
2681 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002682 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002683 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregoreee242f2011-10-27 09:33:13 +00002684 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Douglas Gregor92b059e2009-04-28 20:33:11 +00002686 // Look for any identifiers that were named while processing the
2687 // headers, but are otherwise not needed. We add these to the hash
2688 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002689 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002690 // file.
2691 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2692 IDEnd = PP.getIdentifierTable().end();
2693 ID != IDEnd; ++ID)
2694 getIdentifierRef(ID->second);
2695
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002696 // Create the on-disk hash table representation. We only store offsets
2697 // for identifiers that appear here for the first time.
2698 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002699 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002700 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2701 ID != IDEnd; ++ID) {
2702 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregoreee242f2011-10-27 09:33:13 +00002703 if (!Chain || !ID->first->isFromAST() ||
2704 ID->first->hasChangedSinceDeserialization())
Douglas Gregor7143aab2011-09-01 17:04:32 +00002705 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2706 Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002707 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002708
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002709 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002710 SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002711 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002712 {
Douglas Gregoreee242f2011-10-27 09:33:13 +00002713 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002714 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002715 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002716 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002717 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002718 }
2719
2720 // Create a blob abbreviation
2721 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002723 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002724 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002725 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002726
2727 // Write the identifier table
2728 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002729 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002730 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002731 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002732 }
2733
2734 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002735 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002736 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002737 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002738 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002739 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2740 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2741
2742 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002743 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002744 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002745 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002746 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002747 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002748}
2749
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002750//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002751// DeclContext's Name Lookup Table Serialization
2752//===----------------------------------------------------------------------===//
2753
2754namespace {
2755// Trait used for the on-disk hash table used in the method pool.
2756class ASTDeclContextNameLookupTrait {
2757 ASTWriter &Writer;
2758
2759public:
2760 typedef DeclarationName key_type;
2761 typedef key_type key_type_ref;
2762
2763 typedef DeclContext::lookup_result data_type;
2764 typedef const data_type& data_type_ref;
2765
2766 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2767
2768 unsigned ComputeHash(DeclarationName Name) {
2769 llvm::FoldingSetNodeID ID;
2770 ID.AddInteger(Name.getNameKind());
2771
2772 switch (Name.getNameKind()) {
2773 case DeclarationName::Identifier:
2774 ID.AddString(Name.getAsIdentifierInfo()->getName());
2775 break;
2776 case DeclarationName::ObjCZeroArgSelector:
2777 case DeclarationName::ObjCOneArgSelector:
2778 case DeclarationName::ObjCMultiArgSelector:
2779 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2780 break;
2781 case DeclarationName::CXXConstructorName:
2782 case DeclarationName::CXXDestructorName:
2783 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002784 break;
2785 case DeclarationName::CXXOperatorName:
2786 ID.AddInteger(Name.getCXXOverloadedOperator());
2787 break;
2788 case DeclarationName::CXXLiteralOperatorName:
2789 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2790 case DeclarationName::CXXUsingDirective:
2791 break;
2792 }
2793
2794 return ID.ComputeHash();
2795 }
2796
2797 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002798 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002799 data_type_ref Lookup) {
2800 unsigned KeyLen = 1;
2801 switch (Name.getNameKind()) {
2802 case DeclarationName::Identifier:
2803 case DeclarationName::ObjCZeroArgSelector:
2804 case DeclarationName::ObjCOneArgSelector:
2805 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002806 case DeclarationName::CXXLiteralOperatorName:
2807 KeyLen += 4;
2808 break;
2809 case DeclarationName::CXXOperatorName:
2810 KeyLen += 1;
2811 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002812 case DeclarationName::CXXConstructorName:
2813 case DeclarationName::CXXDestructorName:
2814 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002815 case DeclarationName::CXXUsingDirective:
2816 break;
2817 }
2818 clang::io::Emit16(Out, KeyLen);
2819
2820 // 2 bytes for num of decls and 4 for each DeclID.
2821 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2822 clang::io::Emit16(Out, DataLen);
2823
2824 return std::make_pair(KeyLen, DataLen);
2825 }
2826
Chris Lattner5f9e2722011-07-23 10:55:15 +00002827 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002828 using namespace clang::io;
2829
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002830 Emit8(Out, Name.getNameKind());
2831 switch (Name.getNameKind()) {
2832 case DeclarationName::Identifier:
2833 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002834 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002835 case DeclarationName::ObjCZeroArgSelector:
2836 case DeclarationName::ObjCOneArgSelector:
2837 case DeclarationName::ObjCMultiArgSelector:
2838 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002839 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002840 case DeclarationName::CXXOperatorName:
Benjamin Kramer59313312012-09-19 13:40:40 +00002841 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2842 "Invalid operator?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002843 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer59313312012-09-19 13:40:40 +00002844 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002845 case DeclarationName::CXXLiteralOperatorName:
2846 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer59313312012-09-19 13:40:40 +00002847 return;
Douglas Gregore3605012011-08-02 18:32:54 +00002848 case DeclarationName::CXXConstructorName:
2849 case DeclarationName::CXXDestructorName:
2850 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002851 case DeclarationName::CXXUsingDirective:
Benjamin Kramer59313312012-09-19 13:40:40 +00002852 return;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002853 }
Benjamin Kramer59313312012-09-19 13:40:40 +00002854
2855 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002856 }
2857
Chris Lattner5f9e2722011-07-23 10:55:15 +00002858 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002859 data_type Lookup, unsigned DataLen) {
2860 uint64_t Start = Out.tell(); (void)Start;
2861 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2862 for (; Lookup.first != Lookup.second; ++Lookup.first)
2863 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2864
2865 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2866 }
2867};
2868} // end anonymous namespace
2869
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002870/// \brief Write the block containing all of the declaration IDs
2871/// visible from the given DeclContext.
2872///
2873/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002874/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002875uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2876 DeclContext *DC) {
2877 if (DC->getPrimaryContext() != DC)
2878 return 0;
2879
2880 // Since there is no name lookup into functions or methods, don't bother to
2881 // build a visible-declarations table for these entities.
2882 if (DC->isFunctionOrMethod())
2883 return 0;
2884
2885 // If not in C++, we perform name lookup for the translation unit via the
2886 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2887 // FIXME: In C++ we need the visible declarations in order to "see" the
2888 // friend declarations, is there a way to do this without writing the table ?
David Blaikie4e4d0842012-03-11 07:00:24 +00002889 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002890 return 0;
2891
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002892 // Serialize the contents of the mapping used for lookup. Note that,
2893 // although we have two very different code paths, the serialized
2894 // representation is the same for both cases: a declaration name,
2895 // followed by a size, followed by references to the visible
2896 // declarations that have that name.
2897 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithc5d3e802012-03-16 06:12:59 +00002898 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002899 if (!Map || Map->empty())
2900 return 0;
2901
2902 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2903 ASTDeclContextNameLookupTrait Trait(*this);
2904
2905 // Create the on-disk hash table representation.
Douglas Gregore5a54b62011-08-30 20:49:19 +00002906 DeclarationName ConversionName;
2907 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002908 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2909 D != DEnd; ++D) {
2910 DeclarationName Name = D->first;
2911 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregore5a54b62011-08-30 20:49:19 +00002912 if (Result.first != Result.second) {
2913 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2914 // Hash all conversion function names to the same name. The actual
2915 // type information in conversion function name is not used in the
2916 // key (since such type information is not stable across different
2917 // modules), so the intended effect is to coalesce all of the conversion
2918 // functions under a single key.
2919 if (!ConversionName)
2920 ConversionName = Name;
2921 ConversionDecls.append(Result.first, Result.second);
2922 continue;
2923 }
2924
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002925 Generator.insert(Name, Result, Trait);
Douglas Gregore5a54b62011-08-30 20:49:19 +00002926 }
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002927 }
2928
Douglas Gregore5a54b62011-08-30 20:49:19 +00002929 // Add the conversion functions
2930 if (!ConversionDecls.empty()) {
2931 Generator.insert(ConversionName,
2932 DeclContext::lookup_result(ConversionDecls.begin(),
2933 ConversionDecls.end()),
2934 Trait);
2935 }
2936
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002937 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002938 SmallString<4096> LookupTable;
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002939 uint32_t BucketOffset;
2940 {
2941 llvm::raw_svector_ostream Out(LookupTable);
2942 // Make sure that no bucket is at offset 0
2943 clang::io::Emit32(Out, 0);
2944 BucketOffset = Generator.Emit(Out, Trait);
2945 }
2946
2947 // Write the lookup table
2948 RecordData Record;
2949 Record.push_back(DECL_CONTEXT_VISIBLE);
2950 Record.push_back(BucketOffset);
2951 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2952 LookupTable.str());
2953
2954 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2955 ++NumVisibleDeclContexts;
2956 return Offset;
2957}
2958
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002959/// \brief Write an UPDATE_VISIBLE block for the given context.
2960///
2961/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2962/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithc5d3e802012-03-16 06:12:59 +00002963/// (in C++), for namespaces, and for classes with forward-declared unscoped
2964/// enumeration members (in C++11).
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002965void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002966 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2967 if (!Map || Map->empty())
2968 return;
2969
2970 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2971 ASTDeclContextNameLookupTrait Trait(*this);
2972
2973 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002974 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2975 D != DEnd; ++D) {
2976 DeclarationName Name = D->first;
2977 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002978 // For any name that appears in this table, the results are complete, i.e.
2979 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidis45118d82011-08-30 19:43:23 +00002980 if (Result.first != Result.second)
2981 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002982 }
2983
2984 // Create the on-disk hash table in a buffer.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00002985 SmallString<4096> LookupTable;
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002986 uint32_t BucketOffset;
2987 {
2988 llvm::raw_svector_ostream Out(LookupTable);
2989 // Make sure that no bucket is at offset 0
2990 clang::io::Emit32(Out, 0);
2991 BucketOffset = Generator.Emit(Out, Trait);
2992 }
2993
2994 // Write the lookup table
2995 RecordData Record;
2996 Record.push_back(UPDATE_VISIBLE);
2997 Record.push_back(getDeclID(cast<Decl>(DC)));
2998 Record.push_back(BucketOffset);
2999 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3000}
3001
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003002/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3003void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3004 RecordData Record;
3005 Record.push_back(Opts.fp_contract);
3006 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3007}
3008
3009/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3010void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003011 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003012 return;
3013
3014 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3015 RecordData Record;
3016#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3017#include "clang/Basic/OpenCLExtensions.def"
3018 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3019}
3020
Douglas Gregor2171bf12012-01-15 16:58:34 +00003021void ASTWriter::WriteRedeclarations() {
3022 RecordData LocalRedeclChains;
3023 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3024
3025 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3026 Decl *First = Redeclarations[I];
3027 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3028
3029 Decl *MostRecent = First->getMostRecentDecl();
3030
3031 // If we only have a single declaration, there is no point in storing
3032 // a redeclaration chain.
3033 if (First == MostRecent)
3034 continue;
3035
3036 unsigned Offset = LocalRedeclChains.size();
3037 unsigned Size = 0;
3038 LocalRedeclChains.push_back(0); // Placeholder for the size.
3039
3040 // Collect the set of local redeclarations of this declaration.
3041 for (Decl *Prev = MostRecent; Prev != First;
3042 Prev = Prev->getPreviousDecl()) {
3043 if (!Prev->isFromASTFile()) {
3044 AddDeclRef(Prev, LocalRedeclChains);
3045 ++Size;
3046 }
3047 }
3048 LocalRedeclChains[Offset] = Size;
3049
3050 // Reverse the set of local redeclarations, so that we store them in
3051 // order (since we found them in reverse order).
3052 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3053
3054 // Add the mapping from the first ID to the set of local declarations.
3055 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3056 LocalRedeclsMap.push_back(Info);
3057
3058 assert(N == Redeclarations.size() &&
3059 "Deserialized a declaration we shouldn't have");
3060 }
3061
3062 if (LocalRedeclChains.empty())
3063 return;
3064
3065 // Sort the local redeclarations map by the first declaration ID,
3066 // since the reader will be performing binary searches on this information.
3067 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3068
3069 // Emit the local redeclarations map.
3070 using namespace llvm;
3071 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3072 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3075 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3076
3077 RecordData Record;
3078 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3079 Record.push_back(LocalRedeclsMap.size());
3080 Stream.EmitRecordWithBlob(AbbrevID, Record,
3081 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3082 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3083
3084 // Emit the redeclaration chains.
3085 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3086}
3087
Douglas Gregorcff9f262012-01-27 01:47:08 +00003088void ASTWriter::WriteObjCCategories() {
3089 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3090 RecordData Categories;
3091
3092 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3093 unsigned Size = 0;
3094 unsigned StartIndex = Categories.size();
3095
3096 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3097
3098 // Allocate space for the size.
3099 Categories.push_back(0);
3100
3101 // Add the categories.
3102 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3103 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3104 assert(getDeclID(Cat) != 0 && "Bogus category");
3105 AddDeclRef(Cat, Categories);
3106 }
3107
3108 // Update the size.
3109 Categories[StartIndex] = Size;
3110
3111 // Record this interface -> category map.
3112 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3113 CategoriesMap.push_back(CatInfo);
3114 }
3115
3116 // Sort the categories map by the definition ID, since the reader will be
3117 // performing binary searches on this information.
3118 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3119
3120 // Emit the categories map.
3121 using namespace llvm;
3122 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3123 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3126 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3127
3128 RecordData Record;
3129 Record.push_back(OBJC_CATEGORIES_MAP);
3130 Record.push_back(CategoriesMap.size());
3131 Stream.EmitRecordWithBlob(AbbrevID, Record,
3132 reinterpret_cast<char*>(CategoriesMap.data()),
3133 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3134
3135 // Emit the category lists.
3136 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3137}
3138
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003139void ASTWriter::WriteMergedDecls() {
3140 if (!Chain || Chain->MergedDecls.empty())
3141 return;
3142
3143 RecordData Record;
3144 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3145 IEnd = Chain->MergedDecls.end();
3146 I != IEnd; ++I) {
Douglas Gregorb6b60c12012-01-05 22:27:05 +00003147 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003148 : getDeclID(I->first);
3149 assert(CanonID && "Merged declaration not known?");
3150
3151 Record.push_back(CanonID);
3152 Record.push_back(I->second.size());
3153 Record.append(I->second.begin(), I->second.end());
3154 }
3155 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3156}
3157
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003158//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00003159// General Serialization Routines
3160//===----------------------------------------------------------------------===//
3161
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003162/// \brief Write a record containing the given attributes.
Alexander Kornienko49908902012-07-09 10:04:07 +00003163void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3164 RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00003165 Record.push_back(Attrs.size());
Alexander Kornienko49908902012-07-09 10:04:07 +00003166 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3167 e = Attrs.end(); i != e; ++i){
3168 const Attr *A = *i;
Sean Huntcf807c42010-08-18 23:23:40 +00003169 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003170 AddSourceRange(A->getRange(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003171
Sean Huntcf807c42010-08-18 23:23:40 +00003172#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00003173
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003174 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003175}
3176
Chris Lattner5f9e2722011-07-23 10:55:15 +00003177void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00003178 Record.push_back(Str.size());
3179 Record.insert(Record.end(), Str.begin(), Str.end());
3180}
3181
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003182void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3183 RecordDataImpl &Record) {
3184 Record.push_back(Version.getMajor());
3185 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3186 Record.push_back(*Minor + 1);
3187 else
3188 Record.push_back(0);
3189 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3190 Record.push_back(*Subminor + 1);
3191 else
3192 Record.push_back(0);
3193}
3194
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003195/// \brief Note that the identifier II occurs at the given offset
3196/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003197void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003198 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00003199 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003200 // up earlier in the chain and thus don't need an offset.
3201 if (ID >= FirstIdentID)
3202 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003203}
3204
Douglas Gregor83941df2009-04-25 17:48:32 +00003205/// \brief Note that the selector Sel occurs at the given offset
3206/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00003207void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00003208 unsigned ID = SelectorIDs[Sel];
3209 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00003210 // Don't record offsets for selectors that are also available in a different
3211 // file.
3212 if (ID < FirstSelectorID)
3213 return;
3214 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00003215}
3216
Sebastian Redla4232eb2010-08-18 23:56:21 +00003217ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore209e502011-12-06 01:10:29 +00003218 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003219 WritingAST(false), DoneWritingDeclsAndTypes(false),
3220 ASTHasCompilerErrors(false),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00003221 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003222 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregora8235d62012-10-09 23:05:51 +00003223 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3224 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor26ced122011-12-01 00:59:36 +00003225 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3226 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00003227 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00003228 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00003229 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003230 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00003231 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003232 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00003233 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3234 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3235 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00003236 DeclTypedefAbbrev(0),
3237 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3238 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00003239{
Sebastian Redl30c514c2010-07-14 23:45:08 +00003240}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003241
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003242ASTWriter::~ASTWriter() {
3243 for (FileDeclIDsTy::iterator
3244 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3245 delete I->second;
3246}
3247
Sebastian Redla4232eb2010-08-18 23:56:21 +00003248void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003249 const std::string &OutputFile,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003250 Module *WritingModule, StringRef isysroot,
3251 bool hasErrors) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003252 WritingAST = true;
3253
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +00003254 ASTHasCompilerErrors = hasErrors;
3255
Douglas Gregor2cf26342009-04-09 22:27:44 +00003256 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00003257 Stream.Emit((unsigned)'C', 8);
3258 Stream.Emit((unsigned)'P', 8);
3259 Stream.Emit((unsigned)'C', 8);
3260 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00003261
Chris Lattnerb145b1e2009-04-26 22:26:21 +00003262 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003263
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003264 Context = &SemaRef.Context;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003265 PP = &SemaRef.PP;
Douglas Gregore209e502011-12-06 01:10:29 +00003266 this->WritingModule = WritingModule;
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003267 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003268 Context = 0;
Douglas Gregor185dbd72011-12-01 02:07:58 +00003269 PP = 0;
Douglas Gregore209e502011-12-06 01:10:29 +00003270 this->WritingModule = 0;
Douglas Gregor61c5e342011-09-17 00:05:03 +00003271
3272 WritingAST = false;
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003273}
3274
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003275template<typename Vector>
3276static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3277 ASTWriter::RecordData &Record) {
3278 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3279 I != E; ++I) {
3280 Writer.AddDeclRef(*I, Record);
3281 }
3282}
3283
Sebastian Redla4232eb2010-08-18 23:56:21 +00003284void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003285 StringRef isysroot,
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003286 const std::string &OutputFile,
Douglas Gregor1a4761e2011-11-30 23:21:26 +00003287 Module *WritingModule) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003288 using namespace llvm;
3289
Douglas Gregorecc2c092011-12-01 22:20:10 +00003290 // Make sure that the AST reader knows to finalize itself.
3291 if (Chain)
3292 Chain->finalizeForWriting();
3293
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003294 ASTContext &Context = SemaRef.Context;
3295 Preprocessor &PP = SemaRef.PP;
3296
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003297 // Set up predefined declaration IDs.
3298 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor4dfd02a2011-08-12 05:46:01 +00003299 if (Context.ObjCIdDecl)
3300 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor7a27ea52011-08-12 06:17:30 +00003301 if (Context.ObjCSelDecl)
3302 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor79d67262011-08-12 05:59:41 +00003303 if (Context.ObjCClassDecl)
3304 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregora6ea10e2012-01-17 18:09:05 +00003305 if (Context.ObjCProtocolClassDecl)
3306 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor772eeae2011-08-12 06:49:56 +00003307 if (Context.Int128Decl)
3308 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3309 if (Context.UInt128Decl)
3310 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregore97179c2011-09-08 01:46:34 +00003311 if (Context.ObjCInstanceTypeDecl)
3312 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Ingec5613b22012-06-16 03:34:49 +00003313 if (Context.BuiltinVaListDecl)
3314 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3315
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003316 if (!Chain) {
3317 // Make sure that we emit IdentifierInfos (and any attached
3318 // declarations) for builtins. We don't need to do this when we're
3319 // emitting chained PCH files, because all of the builtins will be
3320 // in the original PCH file.
3321 // FIXME: Modules won't like this at all.
Douglas Gregor2deaea32009-04-22 18:49:13 +00003322 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003323 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003324 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikie4e4d0842012-03-11 07:00:24 +00003325 Context.getLangOpts().NoBuiltin);
Douglas Gregor2deaea32009-04-22 18:49:13 +00003326 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3327 getIdentifierRef(&Table.get(BuiltinNames[I]));
3328 }
3329
Douglas Gregoreee242f2011-10-27 09:33:13 +00003330 // If there are any out-of-date identifiers, bring them up to date.
3331 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3332 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3333 IDEnd = PP.getIdentifierTable().end();
3334 ID != IDEnd; ++ID)
3335 if (ID->second->isOutOfDate())
3336 ExtSource->updateOutOfDateIdentifier(*ID->second);
3337 }
3338
Chris Lattner63d65f82009-09-08 18:19:27 +00003339 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00003340 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00003341 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003342 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003343 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003344
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003345 // Build a record containing all of the file scoped decls in this file.
3346 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003347 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3348 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00003349
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003350 // Build a record containing all of the delegating constructors we still need
3351 // to resolve.
Sean Huntebcbe1d2011-05-04 23:29:54 +00003352 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003353 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003354
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003355 // Write the set of weak, undeclared identifiers. We always write the
3356 // entire table, since later PCH files in a PCH chain are only interested in
3357 // the results at the end of the chain.
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003358 RecordData WeakUndeclaredIdentifiers;
3359 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003360 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003361 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3362 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3363 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3364 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3365 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3366 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3367 }
3368 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003369
Douglas Gregor14c22f22009-04-22 22:18:58 +00003370 // Build a record containing all of the locally-scoped external
3371 // declarations in this header file. Generally, this record will be
3372 // empty.
3373 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003374 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00003375 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00003376 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00003377 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3378 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00003379 TD != TDEnd; ++TD) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003380 if (!TD->second->isFromASTFile())
Douglas Gregorec12ce22011-07-28 14:20:37 +00003381 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3382 }
3383
Douglas Gregorb81c1702009-04-27 20:06:05 +00003384 // Build a record containing all of the ext_vector declarations.
3385 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003386 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003387
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003388 // Build a record containing all of the VTable uses information.
3389 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003390 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00003391 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3392 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3393 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3394 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3395 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003396 }
3397
3398 // Build a record containing all of dynamic classes declarations.
3399 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003400 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003401
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003402 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003403 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003404 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003405 I = SemaRef.PendingInstantiations.begin(),
3406 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3407 AddDeclRef(I->first, PendingInstantiations);
3408 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003409 }
3410 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3411 "There are local ones at end of translation unit!");
3412
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003413 // Build a record containing some declaration references.
3414 RecordData SemaDeclRefs;
3415 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3416 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3417 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3418 }
3419
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003420 RecordData CUDASpecialDeclRefs;
3421 if (Context.getcudaConfigureCallDecl()) {
3422 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3423 }
3424
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003425 // Build a record containing all of the known namespaces.
3426 RecordData KnownNamespaces;
3427 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3428 I = SemaRef.KnownNamespaces.begin(),
3429 IEnd = SemaRef.KnownNamespaces.end();
3430 I != IEnd; ++I) {
3431 if (!I->second)
3432 AddDeclRef(I->first, KnownNamespaces);
3433 }
3434
Sebastian Redl3397c552010-08-18 23:56:27 +00003435 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00003436 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003437 Stream.EnterSubblock(AST_BLOCK_ID, 5);
David Blaikie4e4d0842012-03-11 07:00:24 +00003438 WriteLanguageOptions(Context.getLangOpts());
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003439 WriteMetadata(Context, isysroot, OutputFile);
Douglas Gregor832d6202011-07-22 16:35:34 +00003440 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00003441 WriteStatCache(*StatCalls);
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003442
3443 // Create a lexical update block containing all of the declarations in the
3444 // translation unit that do not come from other AST files.
3445 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3446 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3447 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3448 E = TU->noload_decls_end();
3449 I != E; ++I) {
Douglas Gregor919814d2011-09-09 23:01:35 +00003450 if (!(*I)->isFromASTFile())
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003451 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003452 }
3453
3454 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3455 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3456 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3457 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3458 Record.clear();
3459 Record.push_back(TU_UPDATE_LEXICAL);
3460 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3461 data(NewGlobalDecls));
3462
3463 // And a visible updates block for the translation unit.
3464 Abv = new llvm::BitCodeAbbrev();
3465 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3466 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3467 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3468 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3469 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3470 WriteDeclContextVisibleUpdate(TU);
3471
3472 // If the translation unit has an anonymous namespace, and we don't already
3473 // have an update block for it, write it as an update block.
3474 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3475 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3476 if (Record.empty()) {
3477 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor61c5e342011-09-17 00:05:03 +00003478 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003479 }
3480 }
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003481
3482 // Make sure visible decls, added to DeclContexts previously loaded from
3483 // an AST file, are registered for serialization.
3484 for (SmallVector<const Decl *, 16>::iterator
3485 I = UpdatingVisibleDecls.begin(),
3486 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3487 GetDeclRef(*I);
3488 }
3489
Argyrios Kyrtzidis67bc4ba2011-11-14 04:52:24 +00003490 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor61c5e342011-09-17 00:05:03 +00003491 ResolveDeclUpdatesBlocks();
Douglas Gregor61c5e342011-09-17 00:05:03 +00003492
Douglas Gregora119da02011-08-02 16:26:37 +00003493 // Form the record of special types.
3494 RecordData SpecialTypes;
Douglas Gregora119da02011-08-02 16:26:37 +00003495 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003496 AddTypeRef(Context.getFILEType(), SpecialTypes);
3497 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3498 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3499 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3500 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregora119da02011-08-02 16:26:37 +00003501 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindolae2d4f4e2011-11-13 21:51:09 +00003502 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregor185dbd72011-12-01 02:07:58 +00003503
Douglas Gregor366809a2009-04-26 03:49:13 +00003504 // Keep writing types and declarations until all types and
3505 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00003506 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003507 WriteDeclsBlockAbbrevs();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003508 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3509 E = DeclsToRewrite.end();
3510 I != E; ++I)
3511 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003512 while (!DeclTypesToEmit.empty()) {
3513 DeclOrType DOT = DeclTypesToEmit.front();
3514 DeclTypesToEmit.pop();
3515 if (DOT.isType())
3516 WriteType(DOT.getType());
3517 else
3518 WriteDecl(Context, DOT.getDecl());
3519 }
3520 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003521
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003522 DoneWritingDeclsAndTypes = true;
3523
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003524 WriteFileDeclIDsMap();
3525 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +00003526 WriteComments();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003527
3528 if (Chain) {
3529 // Write the mapping information describing our module dependencies and how
3530 // each of those modules were mapped into our own offset/ID space, so that
3531 // the reader can build the appropriate mapping to its own offset/ID space.
3532 // The map consists solely of a blob with the following format:
3533 // *(module-name-len:i16 module-name:len*i8
3534 // source-location-offset:i32
3535 // identifier-id:i32
3536 // preprocessed-entity-id:i32
3537 // macro-definition-id:i32
Douglas Gregor26ced122011-12-01 00:59:36 +00003538 // submodule-id:i32
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003539 // selector-id:i32
3540 // declaration-id:i32
3541 // c++-base-specifiers-id:i32
3542 // type-id:i32)
3543 //
3544 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3545 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3546 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3547 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00003548 SmallString<2048> Buffer;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003549 {
3550 llvm::raw_svector_ostream Out(Buffer);
3551 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregoraf13bfc2011-12-02 18:58:38 +00003552 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003553 M != MEnd; ++M) {
3554 StringRef FileName = (*M)->FileName;
3555 io::Emit16(Out, FileName.size());
3556 Out.write(FileName.data(), FileName.size());
3557 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3558 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregora8235d62012-10-09 23:05:51 +00003559 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003560 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor26ced122011-12-01 00:59:36 +00003561 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00003562 io::Emit32(Out, (*M)->BaseSelectorID);
3563 io::Emit32(Out, (*M)->BaseDeclID);
3564 io::Emit32(Out, (*M)->BaseTypeIndex);
3565 }
3566 }
3567 Record.clear();
3568 Record.push_back(MODULE_OFFSET_MAP);
3569 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3570 Buffer.data(), Buffer.size());
3571 }
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003572 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00003573 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00003574 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00003575 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregora8cc6ce2011-11-30 04:39:39 +00003576 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003577 WriteFPPragmaOptions(SemaRef.getFPOptions());
3578 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00003579
Sebastian Redl1476ed42010-07-16 16:36:56 +00003580 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003581 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00003582
Anders Carlssonc8505782011-03-06 18:41:18 +00003583 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003584
Douglas Gregore209e502011-12-06 01:10:29 +00003585 // If we're emitting a module, write out the submodule information.
3586 if (WritingModule)
3587 WriteSubmodules(WritingModule);
3588
Douglas Gregora119da02011-08-02 16:26:37 +00003589 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3590
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003591 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00003592 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003593 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00003594
3595 // Write the record containing tentative definitions.
3596 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003597 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00003598
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003599 // Write the record containing unused file scoped decls.
3600 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003601 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003602
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003603 // Write the record containing weak undeclared identifiers.
3604 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003605 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00003606 WeakUndeclaredIdentifiers);
3607
Douglas Gregor14c22f22009-04-22 22:18:58 +00003608 // Write the record containing locally-scoped external definitions.
3609 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003610 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00003611 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00003612
3613 // Write the record containing ext_vector type names.
3614 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003615 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003616
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003617 // Write the record containing VTable uses information.
3618 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003619 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003620
3621 // Write the record containing dynamic classes declarations.
3622 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003623 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003624
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003625 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003626 if (!PendingInstantiations.empty())
3627 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003628
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003629 // Write the record containing declaration references of Sema.
3630 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003631 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003632
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003633 // Write the record containing CUDA-specific declaration references.
3634 if (!CUDASpecialDeclRefs.empty())
3635 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003636
3637 // Write the delegating constructors.
3638 if (!DelegatingCtorDecls.empty())
3639 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003640
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003641 // Write the known namespaces.
3642 if (!KnownNamespaces.empty())
3643 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3644
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003645 // Write the visible updates to DeclContexts.
3646 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3647 I = UpdatedDeclContexts.begin(),
3648 E = UpdatedDeclContexts.end();
3649 I != E; ++I)
3650 WriteDeclContextVisibleUpdate(*I);
3651
Douglas Gregorc5e0f9b2011-12-03 01:15:29 +00003652 if (!WritingModule) {
3653 // Write the submodules that were imported, if any.
3654 RecordData ImportedModules;
3655 for (ASTContext::import_iterator I = Context.local_import_begin(),
3656 IEnd = Context.local_import_end();
3657 I != IEnd; ++I) {
3658 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3659 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3660 }
3661 if (!ImportedModules.empty()) {
3662 // Sort module IDs.
3663 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3664
3665 // Unique module IDs.
3666 ImportedModules.erase(std::unique(ImportedModules.begin(),
3667 ImportedModules.end()),
3668 ImportedModules.end());
3669
3670 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3671 }
Douglas Gregorf6137e42011-12-03 00:59:55 +00003672 }
Douglas Gregora8235d62012-10-09 23:05:51 +00003673
3674 WriteMacroUpdates();
Douglas Gregor6bf2b9f2011-08-12 00:15:20 +00003675 WriteDeclUpdatesBlocks();
Douglas Gregorb7c324f2011-08-12 01:39:19 +00003676 WriteDeclReplacementsBlock();
Douglas Gregorc3cfd2a2011-12-22 21:40:42 +00003677 WriteMergedDecls();
Douglas Gregor2171bf12012-01-15 16:58:34 +00003678 WriteRedeclarations();
Douglas Gregorcff9f262012-01-27 01:47:08 +00003679 WriteObjCCategories();
Douglas Gregora1be2782011-12-17 23:38:30 +00003680
Douglas Gregor3e1af842009-04-17 22:13:46 +00003681 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003682 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003683 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003684 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003685 Record.push_back(NumLexicalDeclContexts);
3686 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003687 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003688 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003689}
3690
Douglas Gregora8235d62012-10-09 23:05:51 +00003691void ASTWriter::WriteMacroUpdates() {
3692 if (MacroUpdates.empty())
3693 return;
3694
3695 RecordData Record;
3696 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3697 E = MacroUpdates.end();
3698 I != E; ++I) {
3699 addMacroRef(I->first, Record);
3700 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregor54c8a402012-10-12 00:16:50 +00003701 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregora8235d62012-10-09 23:05:51 +00003702 }
3703 Stream.EmitRecord(MACRO_UPDATES, Record);
3704}
3705
Douglas Gregor61c5e342011-09-17 00:05:03 +00003706/// \brief Go through the declaration update blocks and resolve declaration
3707/// pointers into declaration IDs.
3708void ASTWriter::ResolveDeclUpdatesBlocks() {
3709 for (DeclUpdateMap::iterator
3710 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3711 const Decl *D = I->first;
3712 UpdateRecord &URec = I->second;
3713
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003714 if (isRewritten(D))
Douglas Gregor61c5e342011-09-17 00:05:03 +00003715 continue; // The decl will be written completely
3716
3717 unsigned Idx = 0, N = URec.size();
3718 while (Idx < N) {
3719 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003720 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3721 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3722 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3723 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3724 ++Idx;
3725 break;
3726
3727 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3728 ++Idx;
3729 break;
3730 }
3731 }
3732 }
3733}
3734
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003735void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003736 if (DeclUpdates.empty())
3737 return;
3738
3739 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003740 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003741 for (DeclUpdateMap::iterator
3742 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3743 const Decl *D = I->first;
3744 UpdateRecord &URec = I->second;
3745
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00003746 if (isRewritten(D))
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003747 continue; // The decl will be written completely,no need to store updates.
3748
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003749 uint64_t Offset = Stream.GetCurrentBitNo();
3750 Stream.EmitRecord(DECL_UPDATES, URec);
3751
3752 OffsetsRecord.push_back(GetDeclRef(D));
3753 OffsetsRecord.push_back(Offset);
3754 }
3755 Stream.ExitBlock();
3756 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3757}
3758
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003759void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003760 if (ReplacedDecls.empty())
3761 return;
3762
3763 RecordData Record;
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003764 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003765 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidisef23b602011-10-31 07:20:15 +00003766 Record.push_back(I->ID);
3767 Record.push_back(I->Offset);
3768 Record.push_back(I->Loc);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003769 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003770 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003771}
3772
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003773void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003774 Record.push_back(Loc.getRawEncoding());
3775}
3776
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003777void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003778 AddSourceLocation(Range.getBegin(), Record);
3779 AddSourceLocation(Range.getEnd(), Record);
3780}
3781
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003782void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003783 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003784 const uint64_t *Words = Value.getRawData();
3785 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003786}
3787
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003788void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003789 Record.push_back(Value.isUnsigned());
3790 AddAPInt(Value, Record);
3791}
3792
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003793void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003794 AddAPInt(Value.bitcastToAPInt(), Record);
3795}
3796
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003797void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003798 Record.push_back(getIdentifierRef(II));
3799}
3800
Douglas Gregora8235d62012-10-09 23:05:51 +00003801void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3802 Record.push_back(getMacroRef(MI));
3803}
3804
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003805IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003806 if (II == 0)
3807 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003808
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003809 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003810 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003811 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003812 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003813}
3814
Douglas Gregora8235d62012-10-09 23:05:51 +00003815MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3816 // Don't emit builtin macros like __LINE__ to the AST file unless they
3817 // have been redefined by the header (in which case they are not
3818 // isBuiltinMacro).
3819 if (MI == 0 || MI->isBuiltinMacro())
3820 return 0;
3821
3822 MacroID &ID = MacroIDs[MI];
3823 if (ID == 0)
3824 ID = NextMacroID++;
3825 return ID;
3826}
3827
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003828void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003829 Record.push_back(getSelectorRef(SelRef));
3830}
3831
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003832SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003833 if (Sel.getAsOpaquePtr() == 0) {
3834 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003835 }
3836
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003837 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003838 if (SID == 0 && Chain) {
3839 // This might trigger a ReadSelector callback, which will set the ID for
3840 // this selector.
3841 Chain->LoadSelector(Sel);
3842 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003843 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003844 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003845 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003846 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003847}
3848
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003849void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003850 AddDeclRef(Temp->getDestructor(), Record);
3851}
3852
Douglas Gregor7c789c12010-10-29 22:39:52 +00003853void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3854 CXXBaseSpecifier const *BasesEnd,
3855 RecordDataImpl &Record) {
3856 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3857 CXXBaseSpecifiersToWrite.push_back(
3858 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3859 Bases, BasesEnd));
3860 Record.push_back(NextCXXBaseSpecifiersID++);
3861}
3862
Sebastian Redla4232eb2010-08-18 23:56:21 +00003863void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003864 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003865 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003866 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003867 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003868 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003869 break;
3870 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003871 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003872 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003873 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003874 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003875 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003876 break;
3877 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003878 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003879 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003880 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003881 break;
John McCall833ca992009-10-29 08:12:44 +00003882 case TemplateArgument::Null:
3883 case TemplateArgument::Integral:
3884 case TemplateArgument::Declaration:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003885 case TemplateArgument::NullPtr:
John McCall833ca992009-10-29 08:12:44 +00003886 case TemplateArgument::Pack:
Eli Friedmand7a6b162012-09-26 02:36:12 +00003887 // FIXME: Is this right?
John McCall833ca992009-10-29 08:12:44 +00003888 break;
3889 }
3890}
3891
Sebastian Redla4232eb2010-08-18 23:56:21 +00003892void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003893 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003894 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003895
3896 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3897 bool InfoHasSameExpr
3898 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3899 Record.push_back(InfoHasSameExpr);
3900 if (InfoHasSameExpr)
3901 return; // Avoid storing the same expr twice.
3902 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003903 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3904 Record);
3905}
3906
Douglas Gregordc355712011-02-25 00:36:19 +00003907void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3908 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003909 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003910 AddTypeRef(QualType(), Record);
3911 return;
3912 }
3913
Douglas Gregordc355712011-02-25 00:36:19 +00003914 AddTypeLoc(TInfo->getTypeLoc(), Record);
3915}
3916
3917void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3918 AddTypeRef(TL.getType(), Record);
3919
John McCalla1ee0c52009-10-16 21:56:05 +00003920 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003921 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003922 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003923}
3924
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003925void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003926 Record.push_back(GetOrCreateTypeID(T));
3927}
3928
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003929TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3930 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003931 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3932}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003933
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003934TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregor3b8043b2011-08-09 15:13:55 +00003935 return MakeTypeID(*Context, T,
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003936 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003937}
3938
3939TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3940 if (T.isNull())
3941 return TypeIdx();
3942 assert(!T.getLocalFastQualifiers());
3943
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003944 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003945 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003946 if (DoneWritingDeclsAndTypes) {
3947 assert(0 && "New type seen after serializing all the types to emit!");
3948 return TypeIdx();
3949 }
3950
Douglas Gregor366809a2009-04-26 03:49:13 +00003951 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003952 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003953 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003954 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003955 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003956 return Idx;
3957}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003958
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003959TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003960 if (T.isNull())
3961 return TypeIdx();
3962 assert(!T.getLocalFastQualifiers());
3963
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003964 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3965 assert(I != TypeIdxs.end() && "Type not emitted!");
3966 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003967}
3968
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003969void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003970 Record.push_back(GetDeclRef(D));
3971}
3972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003973DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00003974 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3975
Douglas Gregor2cf26342009-04-09 22:27:44 +00003976 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003977 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003978 }
Douglas Gregor1c7946a2012-01-05 22:33:30 +00003979
3980 // If D comes from an AST file, its declaration ID is already known and
3981 // fixed.
3982 if (D->isFromASTFile())
3983 return D->getGlobalID();
3984
Douglas Gregor97475832010-10-05 18:37:06 +00003985 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003986 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003987 if (ID == 0) {
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00003988 if (DoneWritingDeclsAndTypes) {
3989 assert(0 && "New decl seen after serializing all the decls to emit!");
3990 return 0;
3991 }
3992
Douglas Gregor2cf26342009-04-09 22:27:44 +00003993 // We haven't seen this declaration before. Give it a new ID and
3994 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003995 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003996 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00003997 }
3998
Sebastian Redl681d7232010-07-27 00:17:23 +00003999 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00004000}
4001
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004002DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004003 if (D == 0)
4004 return 0;
4005
Douglas Gregor1c7946a2012-01-05 22:33:30 +00004006 // If D comes from an AST file, its declaration ID is already known and
4007 // fixed.
4008 if (D->isFromASTFile())
4009 return D->getGlobalID();
4010
Douglas Gregor3251ceb2009-04-20 20:36:09 +00004011 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4012 return DeclIDs[D];
4013}
4014
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004015static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4016 std::pair<unsigned, serialization::DeclID> R) {
4017 return L.first < R.first;
4018}
4019
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004020void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004021 assert(ID);
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004022 assert(D);
4023
4024 SourceLocation Loc = D->getLocation();
4025 if (Loc.isInvalid())
4026 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004027
4028 // We only keep track of the file-level declarations of each file.
4029 if (!D->getLexicalDeclContext()->isFileContext())
4030 return;
Argyrios Kyrtzidis69015c22012-02-24 19:45:46 +00004031 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4032 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidis8cceefa2012-02-24 01:12:38 +00004033 if (isa<ParmVarDecl>(D))
4034 return;
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004035
4036 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidis19645d22011-10-28 23:57:43 +00004037 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004038 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004039 FileID FID;
4040 unsigned Offset;
4041 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004042 if (FID.isInvalid())
4043 return;
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004044 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004045
Argyrios Kyrtzidisa2ea4d92012-10-02 21:09:17 +00004046 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004047 if (!Info)
4048 Info = new DeclIDInFileInfo();
4049
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004050 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004051 LocDeclIDsTy &Decls = Info->DeclIDs;
4052
Argyrios Kyrtzidisfab8d5b2011-10-28 23:57:47 +00004053 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis10f3df52011-10-28 22:54:21 +00004054 Decls.push_back(LocDecl);
4055 return;
4056 }
4057
4058 LocDeclIDsTy::iterator
4059 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4060
4061 Decls.insert(I, LocDecl);
4062}
4063
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004064void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00004065 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00004066 Record.push_back(Name.getNameKind());
4067 switch (Name.getNameKind()) {
4068 case DeclarationName::Identifier:
4069 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4070 break;
4071
4072 case DeclarationName::ObjCZeroArgSelector:
4073 case DeclarationName::ObjCOneArgSelector:
4074 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00004075 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00004076 break;
4077
4078 case DeclarationName::CXXConstructorName:
4079 case DeclarationName::CXXDestructorName:
4080 case DeclarationName::CXXConversionFunctionName:
4081 AddTypeRef(Name.getCXXNameType(), Record);
4082 break;
4083
4084 case DeclarationName::CXXOperatorName:
4085 Record.push_back(Name.getCXXOverloadedOperator());
4086 break;
4087
Sean Hunt3e518bd2009-11-29 07:34:05 +00004088 case DeclarationName::CXXLiteralOperatorName:
4089 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4090 break;
4091
Douglas Gregor2cf26342009-04-09 22:27:44 +00004092 case DeclarationName::CXXUsingDirective:
4093 // No extra data to emit
4094 break;
4095 }
4096}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004097
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004098void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004099 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004100 switch (Name.getNameKind()) {
4101 case DeclarationName::CXXConstructorName:
4102 case DeclarationName::CXXDestructorName:
4103 case DeclarationName::CXXConversionFunctionName:
4104 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4105 break;
4106
4107 case DeclarationName::CXXOperatorName:
4108 AddSourceLocation(
4109 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4110 Record);
4111 AddSourceLocation(
4112 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4113 Record);
4114 break;
4115
4116 case DeclarationName::CXXLiteralOperatorName:
4117 AddSourceLocation(
4118 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4119 Record);
4120 break;
4121
4122 case DeclarationName::Identifier:
4123 case DeclarationName::ObjCZeroArgSelector:
4124 case DeclarationName::ObjCOneArgSelector:
4125 case DeclarationName::ObjCMultiArgSelector:
4126 case DeclarationName::CXXUsingDirective:
4127 break;
4128 }
4129}
4130
4131void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004132 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004133 AddDeclarationName(NameInfo.getName(), Record);
4134 AddSourceLocation(NameInfo.getLoc(), Record);
4135 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4136}
4137
4138void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004139 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004140 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00004141 Record.push_back(Info.NumTemplParamLists);
4142 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4143 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4144}
4145
Sebastian Redla4232eb2010-08-18 23:56:21 +00004146void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004147 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004148 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004149 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004150 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004151
4152 // Push each of the NNS's onto a stack for serialization in reverse order.
4153 while (NNS) {
4154 NestedNames.push_back(NNS);
4155 NNS = NNS->getPrefix();
4156 }
4157
4158 Record.push_back(NestedNames.size());
4159 while(!NestedNames.empty()) {
4160 NNS = NestedNames.pop_back_val();
4161 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4162 Record.push_back(Kind);
4163 switch (Kind) {
4164 case NestedNameSpecifier::Identifier:
4165 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4166 break;
4167
4168 case NestedNameSpecifier::Namespace:
4169 AddDeclRef(NNS->getAsNamespace(), Record);
4170 break;
4171
Douglas Gregor14aba762011-02-24 02:36:08 +00004172 case NestedNameSpecifier::NamespaceAlias:
4173 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4174 break;
4175
Chris Lattner6ad9ac02010-05-07 21:43:38 +00004176 case NestedNameSpecifier::TypeSpec:
4177 case NestedNameSpecifier::TypeSpecWithTemplate:
4178 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4179 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4180 break;
4181
4182 case NestedNameSpecifier::Global:
4183 // Don't need to write an associated value.
4184 break;
4185 }
4186 }
4187}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004188
Douglas Gregordc355712011-02-25 00:36:19 +00004189void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4190 RecordDataImpl &Record) {
4191 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00004192 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004193 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00004194
4195 // Push each of the nested-name-specifiers's onto a stack for
4196 // serialization in reverse order.
4197 while (NNS) {
4198 NestedNames.push_back(NNS);
4199 NNS = NNS.getPrefix();
4200 }
4201
4202 Record.push_back(NestedNames.size());
4203 while(!NestedNames.empty()) {
4204 NNS = NestedNames.pop_back_val();
4205 NestedNameSpecifier::SpecifierKind Kind
4206 = NNS.getNestedNameSpecifier()->getKind();
4207 Record.push_back(Kind);
4208 switch (Kind) {
4209 case NestedNameSpecifier::Identifier:
4210 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4211 AddSourceRange(NNS.getLocalSourceRange(), Record);
4212 break;
4213
4214 case NestedNameSpecifier::Namespace:
4215 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4216 AddSourceRange(NNS.getLocalSourceRange(), Record);
4217 break;
4218
4219 case NestedNameSpecifier::NamespaceAlias:
4220 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4221 AddSourceRange(NNS.getLocalSourceRange(), Record);
4222 break;
4223
4224 case NestedNameSpecifier::TypeSpec:
4225 case NestedNameSpecifier::TypeSpecWithTemplate:
4226 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4227 AddTypeLoc(NNS.getTypeLoc(), Record);
4228 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4229 break;
4230
4231 case NestedNameSpecifier::Global:
4232 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4233 break;
4234 }
4235 }
4236}
4237
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004238void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00004239 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004240 Record.push_back(Kind);
4241 switch (Kind) {
4242 case TemplateName::Template:
4243 AddDeclRef(Name.getAsTemplateDecl(), Record);
4244 break;
4245
4246 case TemplateName::OverloadedTemplate: {
4247 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4248 Record.push_back(OvT->size());
4249 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4250 I != E; ++I)
4251 AddDeclRef(*I, Record);
4252 break;
4253 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004254
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004255 case TemplateName::QualifiedTemplate: {
4256 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4257 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4258 Record.push_back(QualT->hasTemplateKeyword());
4259 AddDeclRef(QualT->getTemplateDecl(), Record);
4260 break;
4261 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00004262
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004263 case TemplateName::DependentTemplate: {
4264 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4265 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4266 Record.push_back(DepT->isIdentifier());
4267 if (DepT->isIdentifier())
4268 AddIdentifierRef(DepT->getIdentifier(), Record);
4269 else
4270 Record.push_back(DepT->getOperator());
4271 break;
4272 }
John McCall14606042011-06-30 08:33:18 +00004273
4274 case TemplateName::SubstTemplateTemplateParm: {
4275 SubstTemplateTemplateParmStorage *subst
4276 = Name.getAsSubstTemplateTemplateParm();
4277 AddDeclRef(subst->getParameter(), Record);
4278 AddTemplateName(subst->getReplacement(), Record);
4279 break;
4280 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004281
4282 case TemplateName::SubstTemplateTemplateParmPack: {
4283 SubstTemplateTemplateParmPackStorage *SubstPack
4284 = Name.getAsSubstTemplateTemplateParmPack();
4285 AddDeclRef(SubstPack->getParameterPack(), Record);
4286 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4287 break;
4288 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004289 }
4290}
4291
Michael J. Spencer20249a12010-10-21 03:16:25 +00004292void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004293 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004294 Record.push_back(Arg.getKind());
4295 switch (Arg.getKind()) {
4296 case TemplateArgument::Null:
4297 break;
4298 case TemplateArgument::Type:
4299 AddTypeRef(Arg.getAsType(), Record);
4300 break;
4301 case TemplateArgument::Declaration:
4302 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmand7a6b162012-09-26 02:36:12 +00004303 Record.push_back(Arg.isDeclForReferenceParam());
4304 break;
4305 case TemplateArgument::NullPtr:
4306 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004307 break;
4308 case TemplateArgument::Integral:
Benjamin Kramer85524372012-06-07 15:09:51 +00004309 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004310 AddTypeRef(Arg.getIntegralType(), Record);
4311 break;
4312 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00004313 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4314 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00004315 case TemplateArgument::TemplateExpansion:
4316 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00004317 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4318 Record.push_back(*NumExpansions + 1);
4319 else
4320 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00004321 break;
4322 case TemplateArgument::Expression:
4323 AddStmt(Arg.getAsExpr());
4324 break;
4325 case TemplateArgument::Pack:
4326 Record.push_back(Arg.pack_size());
4327 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4328 I != E; ++I)
4329 AddTemplateArgument(*I, Record);
4330 break;
4331 }
4332}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004333
4334void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004335ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004336 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004337 assert(TemplateParams && "No TemplateParams!");
4338 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4339 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4340 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4341 Record.push_back(TemplateParams->size());
4342 for (TemplateParameterList::const_iterator
4343 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4344 P != PEnd; ++P)
4345 AddDeclRef(*P, Record);
4346}
4347
4348/// \brief Emit a template argument list.
4349void
Sebastian Redla4232eb2010-08-18 23:56:21 +00004350ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004351 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004352 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00004353 Record.push_back(TemplateArgs->size());
4354 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00004355 AddTemplateArgument(TemplateArgs->get(i), Record);
4356}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004357
4358
4359void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004360ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00004361 Record.push_back(Set.size());
4362 for (UnresolvedSetImpl::const_iterator
4363 I = Set.begin(), E = Set.end(); I != E; ++I) {
4364 AddDeclRef(I.getDecl(), Record);
4365 Record.push_back(I.getAccess());
4366 }
4367}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004368
Sebastian Redla4232eb2010-08-18 23:56:21 +00004369void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004370 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004371 Record.push_back(Base.isVirtual());
4372 Record.push_back(Base.isBaseOfClass());
4373 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00004374 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00004375 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004376 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00004377 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4378 : SourceLocation(),
4379 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00004380}
Sebastian Redl30c514c2010-07-14 23:45:08 +00004381
Douglas Gregor7c789c12010-10-29 22:39:52 +00004382void ASTWriter::FlushCXXBaseSpecifiers() {
4383 RecordData Record;
4384 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4385 Record.clear();
4386
4387 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00004388 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00004389 if (Index == CXXBaseSpecifiersOffsets.size())
4390 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4391 else {
4392 if (Index > CXXBaseSpecifiersOffsets.size())
4393 CXXBaseSpecifiersOffsets.resize(Index + 1);
4394 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4395 }
4396
4397 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4398 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4399 Record.push_back(BEnd - B);
4400 for (; B != BEnd; ++B)
4401 AddCXXBaseSpecifier(*B, Record);
4402 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00004403
4404 // Flush any expressions that were written as part of the base specifiers.
4405 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00004406 }
4407
4408 CXXBaseSpecifiersToWrite.clear();
4409}
4410
Sean Huntcbb67482011-01-08 20:30:50 +00004411void ASTWriter::AddCXXCtorInitializers(
4412 const CXXCtorInitializer * const *CtorInitializers,
4413 unsigned NumCtorInitializers,
4414 RecordDataImpl &Record) {
4415 Record.push_back(NumCtorInitializers);
4416 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4417 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004418
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004419 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00004420 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregor76852c22011-11-01 01:16:03 +00004421 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004422 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00004423 } else if (Init->isDelegatingInitializer()) {
4424 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregor76852c22011-11-01 01:16:03 +00004425 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Sean Hunt156b6402011-05-04 01:19:08 +00004426 } else if (Init->isMemberInitializer()){
4427 Record.push_back(CTOR_INITIALIZER_MEMBER);
4428 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004429 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00004430 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4431 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004432 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00004433
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004434 AddSourceLocation(Init->getMemberLocation(), Record);
4435 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00004436 AddSourceLocation(Init->getLParenLoc(), Record);
4437 AddSourceLocation(Init->getRParenLoc(), Record);
4438 Record.push_back(Init->isWritten());
4439 if (Init->isWritten()) {
4440 Record.push_back(Init->getSourceOrder());
4441 } else {
4442 Record.push_back(Init->getNumArrayIndices());
4443 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4444 AddDeclRef(Init->getArrayIndex(i), Record);
4445 }
4446 }
4447}
4448
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004449void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4450 assert(D->DefinitionData);
4451 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004452 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004453 Record.push_back(Data.UserDeclaredConstructor);
4454 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004455 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004456 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004457 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004458 Record.push_back(Data.UserDeclaredDestructor);
4459 Record.push_back(Data.Aggregate);
4460 Record.push_back(Data.PlainOldData);
4461 Record.push_back(Data.Empty);
4462 Record.push_back(Data.Polymorphic);
4463 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00004464 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00004465 Record.push_back(Data.HasNoNonEmptyBases);
4466 Record.push_back(Data.HasPrivateFields);
4467 Record.push_back(Data.HasProtectedFields);
4468 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00004469 Record.push_back(Data.HasMutableFields);
Richard Smithdfefb842012-02-25 07:33:38 +00004470 Record.push_back(Data.HasOnlyCMembers);
Richard Smithd079abf2012-05-07 01:07:30 +00004471 Record.push_back(Data.HasInClassInitializer);
Sean Hunt023df372011-05-09 18:22:59 +00004472 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith6b8bc072011-08-10 18:11:37 +00004473 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004474 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smithdfefb842012-02-25 07:33:38 +00004475 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004476 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004477 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004478 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00004479 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004480 Record.push_back(Data.HasTrivialDestructor);
Richard Smithdfefb842012-02-25 07:33:38 +00004481 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00004482 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004483 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00004484 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004485 Record.push_back(Data.DeclaredDefaultConstructor);
4486 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregor58e97972011-09-06 16:38:46 +00004487 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004488 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregor58e97972011-09-06 16:38:46 +00004489 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004490 Record.push_back(Data.DeclaredDestructor);
Sebastian Redl14c36332011-08-31 13:59:56 +00004491 Record.push_back(Data.FailedImplicitMoveConstructor);
4492 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smithdfefb842012-02-25 07:33:38 +00004493 // IsLambda bit is already saved.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004494
4495 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004496 if (Data.NumBases > 0)
4497 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4498 Record);
4499
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004500 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4501 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00004502 if (Data.NumVBases > 0)
4503 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4504 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004505
4506 AddUnresolvedSet(Data.Conversions, Record);
4507 AddUnresolvedSet(Data.VisibleConversions, Record);
4508 // Data.Definition is the owning decl, no need to write it.
4509 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004510
4511 // Add lambda-specific data.
4512 if (Data.IsLambda) {
4513 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregorf4b7de12012-02-21 19:11:17 +00004514 Record.push_back(Lambda.Dependent);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004515 Record.push_back(Lambda.NumCaptures);
4516 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor9e8c92a2012-02-20 19:44:39 +00004517 Record.push_back(Lambda.ManglingNumber);
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004518 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedman8da8a662012-09-19 01:18:11 +00004519 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00004520 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4521 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4522 AddSourceLocation(Capture.getLocation(), Record);
4523 Record.push_back(Capture.isImplicit());
4524 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4525 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4526 AddDeclRef(Var, Record);
4527 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4528 : SourceLocation(),
4529 Record);
4530 }
4531 }
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00004532}
4533
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004534void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004535 assert(Reader && "Cannot remove chain");
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004536 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004537 assert(FirstDeclID == NextDeclID &&
4538 FirstTypeID == NextTypeID &&
4539 FirstIdentID == NextIdentID &&
Douglas Gregora8235d62012-10-09 23:05:51 +00004540 FirstMacroID == NextMacroID &&
Douglas Gregor26ced122011-12-01 00:59:36 +00004541 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004542 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004543 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004544
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004545 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004546
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004547 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4548 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4549 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregora8235d62012-10-09 23:05:51 +00004550 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor26ced122011-12-01 00:59:36 +00004551 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregor10bc00f2011-08-18 04:12:04 +00004552 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004553 NextDeclID = FirstDeclID;
4554 NextTypeID = FirstTypeID;
4555 NextIdentID = FirstIdentID;
Douglas Gregora8235d62012-10-09 23:05:51 +00004556 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004557 NextSelectorID = FirstSelectorID;
Douglas Gregor26ced122011-12-01 00:59:36 +00004558 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004559}
4560
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004561void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004562 IdentifierIDs[II] = ID;
4563}
4564
Douglas Gregora8235d62012-10-09 23:05:51 +00004565void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4566 MacroIDs[MI] = ID;
4567}
4568
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004569void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004570 // Always take the highest-numbered type index. This copes with an interesting
4571 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004572 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004573 // keep the higher-numbered entry so that we can properly write it out to
4574 // the AST file.
4575 TypeIdx &StoredIdx = TypeIdxs[T];
4576 if (Idx.getIndex() >= StoredIdx.getIndex())
4577 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004578}
4579
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004580void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004581 SelectorIDs[S] = ID;
4582}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004583
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004584void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004585 MacroDefinition *MD) {
Argyrios Kyrtzidise24692b2011-09-15 18:02:56 +00004586 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor77424bc2010-10-02 19:29:26 +00004587 MacroDefinitions[MD] = ID;
4588}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004589
Douglas Gregora015cab2011-12-02 17:30:13 +00004590void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4591 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4592 SubmoduleIDs[Mod] = ID;
4593}
4594
Douglas Gregora8235d62012-10-09 23:05:51 +00004595void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4596 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4597}
4598
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004599void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCall5e1cdac2011-10-07 06:10:15 +00004600 assert(D->isCompleteDefinition());
Douglas Gregor61c5e342011-09-17 00:05:03 +00004601 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004602 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4603 // We are interested when a PCH decl is modified.
Douglas Gregor919814d2011-09-09 23:01:35 +00004604 if (RD->isFromASTFile()) {
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004605 // A forward reference was mutated into a definition. Rewrite it.
4606 // FIXME: This happens during template instantiation, should we
4607 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004608 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004609 }
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004610 }
4611}
Douglas Gregora8235d62012-10-09 23:05:51 +00004612
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004613void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004614 assert(!WritingAST && "Already writing the AST!");
4615
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004616 // TU and namespaces are handled elsewhere.
4617 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4618 return;
4619
Douglas Gregor919814d2011-09-09 23:01:35 +00004620 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004621 return; // Not a source decl added to a DeclContext from PCH.
4622
4623 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis7518b372012-07-02 19:19:01 +00004624 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004625}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004626
4627void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004628 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004629 assert(D->isImplicit());
Douglas Gregor919814d2011-09-09 23:01:35 +00004630 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004631 return; // Not a source member added to a class from PCH.
4632 if (!isa<CXXMethodDecl>(D))
4633 return; // We are interested in lazily declared implicit methods.
4634
4635 // A decl coming from PCH was modified.
John McCall5e1cdac2011-10-07 06:10:15 +00004636 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004637 UpdateRecord &Record = DeclUpdates[RD];
4638 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004639 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004640}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004641
4642void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4643 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004644 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004645 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004646 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004647 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +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));
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004653}
Douglas Gregor89d99802010-11-30 06:16:57 +00004654
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004655void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4656 const FunctionDecl *D) {
4657 // The specializations set is kept in the canonical template.
Douglas Gregor61c5e342011-09-17 00:05:03 +00004658 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004659 TD = TD->getCanonicalDecl();
Douglas Gregor919814d2011-09-09 23:01:35 +00004660 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004661 return; // Not a source specialization added to a template from PCH.
4662
4663 UpdateRecord &Record = DeclUpdates[TD];
4664 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor61c5e342011-09-17 00:05:03 +00004665 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004666}
4667
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004668void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004669 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004670 if (!D->isFromASTFile())
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004671 return; // Declaration not imported from PCH.
4672
4673 // Implicit decl from a PCH was defined.
4674 // FIXME: Should implicit definition be a separate FunctionDecl?
4675 RewriteDecl(D);
4676}
4677
Sebastian Redlf79a7192011-04-29 08:19:30 +00004678void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004679 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004680 if (!D->isFromASTFile())
Sebastian Redlf79a7192011-04-29 08:19:30 +00004681 return;
4682
4683 // Since the actual instantiation is delayed, this really means that we need
4684 // to update the instantiation location.
4685 UpdateRecord &Record = DeclUpdates[D];
4686 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4687 AddSourceLocation(
4688 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4689}
4690
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004691void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4692 const ObjCInterfaceDecl *IFD) {
Douglas Gregor61c5e342011-09-17 00:05:03 +00004693 assert(!WritingAST && "Already writing the AST!");
Douglas Gregor919814d2011-09-09 23:01:35 +00004694 if (!IFD->isFromASTFile())
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004695 return; // Declaration not imported from PCH.
Douglas Gregorcff9f262012-01-27 01:47:08 +00004696
4697 assert(IFD->getDefinition() && "Category on a class without a definition?");
4698 ObjCClassesWithCategories.insert(
4699 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidise6b8d682011-09-01 00:58:55 +00004700}
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +00004701
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +00004702
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +00004703void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4704 const ObjCPropertyDecl *OrigProp,
4705 const ObjCCategoryDecl *ClassExt) {
4706 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4707 if (!D)
4708 return;
4709
4710 assert(!WritingAST && "Already writing the AST!");
4711 if (!D->isFromASTFile())
4712 return; // Declaration not imported from PCH.
4713
4714 RewriteDecl(D);
4715}