blob: de5816df9e33ad28b1ce3d345dc7f11a9209ed92 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-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 Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053
Sebastian Redl3df5a082010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-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 Kramerd47a12a2011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000059}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000065}
66
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000070
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-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 Gregoref84c4b2009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121}
122
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133}
134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139}
140
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-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 Smith5e580292012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-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 Smith8b987a92012-04-21 17:47:47 +0000198 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
199 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
200 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000201 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000202 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000203}
204
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000206 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000207 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000208}
John McCallb96ec562009-12-04 22:46:56 +0000209
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000211 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000212 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
213 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000214 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000215}
216
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000218 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000219 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220}
221
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000224 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225}
226
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000227void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000229 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000231}
232
Alexis Hunte852b102011-05-24 22:41:36 +0000233void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
234 Writer.AddTypeRef(T->getBaseType(), Record);
235 Writer.AddTypeRef(T->getUnderlyingType(), Record);
236 Record.push_back(T->getUTTKind());
237 Code = TYPE_UNARY_TRANSFORM;
238}
239
Richard Smith30482bc2011-02-20 03:19:35 +0000240void ASTTypeWriter::VisitAutoType(const AutoType *T) {
241 Writer.AddTypeRef(T->getDeducedType(), Record);
242 Code = TYPE_AUTO;
243}
244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000245void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000246 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000247 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000248 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249 "Cannot serialize in the middle of a type definition");
250}
251
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000252void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000254 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255}
256
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000257void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000259 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260}
261
John McCall81904512011-01-06 01:58:22 +0000262void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
263 Writer.AddTypeRef(T->getModifiedType(), Record);
264 Writer.AddTypeRef(T->getEquivalentType(), Record);
265 Record.push_back(T->getAttrKind());
266 Code = TYPE_ATTRIBUTED;
267}
268
Mike Stump11289f42009-09-09 15:08:12 +0000269void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000271 const SubstTemplateTypeParmType *T) {
272 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
273 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000274 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000275}
276
277void
Douglas Gregorada4b792011-01-14 02:55:32 +0000278ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
279 const SubstTemplateTypeParmPackType *T) {
280 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
281 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
282 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
283}
284
285void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000287 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000288 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000289 Writer.AddTemplateName(T->getTemplateName(), Record);
290 Record.push_back(T->getNumArgs());
291 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
292 ArgI != ArgE; ++ArgI)
293 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000294 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
295 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000296 : T->getCanonicalTypeInternal(),
297 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000298 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000299}
300
301void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000302ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000303 VisitArrayType(T);
304 Writer.AddStmt(T->getSizeExpr());
305 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000306 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000307}
308
309void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000310ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000311 const DependentSizedExtVectorType *T) {
312 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000313 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000314}
315
316void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000317ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000318 Record.push_back(T->getDepth());
319 Record.push_back(T->getIndex());
320 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000321 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000322 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000323}
324
325void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000326ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000327 Record.push_back(T->getKeyword());
328 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
329 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000330 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
331 : T->getCanonicalTypeInternal(),
332 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000333 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000334}
335
336void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000337ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000338 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000339 Record.push_back(T->getKeyword());
340 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
341 Writer.AddIdentifierRef(T->getIdentifier(), Record);
342 Record.push_back(T->getNumArgs());
343 for (DependentTemplateSpecializationType::iterator
344 I = T->begin(), E = T->end(); I != E; ++I)
345 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000346 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000347}
348
Douglas Gregord2fa7662010-12-20 02:24:11 +0000349void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
350 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000351 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
352 Record.push_back(*NumExpansions + 1);
353 else
354 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000355 Code = TYPE_PACK_EXPANSION;
356}
357
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000358void ASTTypeWriter::VisitParenType(const ParenType *T) {
359 Writer.AddTypeRef(T->getInnerType(), Record);
360 Code = TYPE_PAREN;
361}
362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000364 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000365 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
366 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000367 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000368}
369
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000370void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000371 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000372 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000373 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000374}
375
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000376void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000377 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000378 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000379}
380
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000381void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000382 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000383 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000384 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000385 E = T->qual_end(); I != E; ++I)
386 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000387 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000388}
389
Steve Narofffb4330f2009-06-17 22:40:22 +0000390void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000391ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000392 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000393 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000394}
395
Eli Friedman0dfb8892011-10-06 23:00:33 +0000396void
397ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
398 Writer.AddTypeRef(T->getValueType(), Record);
399 Code = TYPE_ATOMIC;
400}
401
John McCall8f115c62009-10-16 21:56:05 +0000402namespace {
403
404class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000405 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000406 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000407
408public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000409 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000410 : Writer(Writer), Record(Record) { }
411
John McCall17001972009-10-18 01:05:36 +0000412#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000413#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000414 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000415#include "clang/AST/TypeLocNodes.def"
416
John McCall17001972009-10-18 01:05:36 +0000417 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
418 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000419};
420
421}
422
John McCall17001972009-10-18 01:05:36 +0000423void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
424 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000425}
John McCall17001972009-10-18 01:05:36 +0000426void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000427 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
428 if (TL.needsExtraLocalData()) {
429 Record.push_back(TL.getWrittenTypeSpec());
430 Record.push_back(TL.getWrittenSignSpec());
431 Record.push_back(TL.getWrittenWidthSpec());
432 Record.push_back(TL.hasModeAttr());
433 }
John McCall8f115c62009-10-16 21:56:05 +0000434}
John McCall17001972009-10-18 01:05:36 +0000435void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
436 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000437}
John McCall17001972009-10-18 01:05:36 +0000438void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000443}
John McCall17001972009-10-18 01:05:36 +0000444void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000446}
John McCall17001972009-10-18 01:05:36 +0000447void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000449}
John McCall17001972009-10-18 01:05:36 +0000450void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000452 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000453}
John McCall17001972009-10-18 01:05:36 +0000454void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
456 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
457 Record.push_back(TL.getSizeExpr() ? 1 : 0);
458 if (TL.getSizeExpr())
459 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000460}
John McCall17001972009-10-18 01:05:36 +0000461void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
465 VisitArrayTypeLoc(TL);
466}
467void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
471 DependentSizedArrayTypeLoc TL) {
472 VisitArrayTypeLoc(TL);
473}
474void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
475 DependentSizedExtVectorTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
478void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000485 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
486 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000487 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000488 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
489 Writer.AddDeclRef(TL.getArg(i), Record);
490}
491void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
492 VisitFunctionTypeLoc(TL);
493}
494void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
495 VisitFunctionTypeLoc(TL);
496}
John McCallb96ec562009-12-04 22:46:56 +0000497void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getNameLoc(), Record);
499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
503void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000504 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
505 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
506 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000507}
508void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000509 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
510 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
511 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
512 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000513}
514void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
Alexis Hunte852b102011-05-24 22:41:36 +0000517void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getKWLoc(), Record);
519 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
520 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
521 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
522}
Richard Smith30482bc2011-02-20 03:19:35 +0000523void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getNameLoc(), Record);
525}
John McCall17001972009-10-18 01:05:36 +0000526void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
529void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall81904512011-01-06 01:58:22 +0000532void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
533 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
534 if (TL.hasAttrOperand()) {
535 SourceRange range = TL.getAttrOperandParensRange();
536 Writer.AddSourceLocation(range.getBegin(), Record);
537 Writer.AddSourceLocation(range.getEnd(), Record);
538 }
539 if (TL.hasAttrExprOperand()) {
540 Expr *operand = TL.getAttrExprOperand();
541 Record.push_back(operand ? 1 : 0);
542 if (operand) Writer.AddStmt(operand);
543 } else if (TL.hasAttrEnumOperand()) {
544 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
545 }
546}
John McCall17001972009-10-18 01:05:36 +0000547void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getNameLoc(), Record);
549}
John McCallcebee162009-10-18 09:09:24 +0000550void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
551 SubstTemplateTypeParmTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
Douglas Gregorada4b792011-01-14 02:55:32 +0000554void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
555 SubstTemplateTypeParmPackTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
John McCall17001972009-10-18 01:05:36 +0000558void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
559 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000560 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000561 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
562 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
563 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
564 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000565 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
566 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000567}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000568void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
569 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
570 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
571}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000572void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000573 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000574 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000575}
John McCalle78aac42010-03-10 03:28:59 +0000576void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000579void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000580 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000581 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000582 Writer.AddSourceLocation(TL.getNameLoc(), Record);
583}
John McCallc392f372010-06-11 00:33:02 +0000584void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
585 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000586 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000587 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000588 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000589 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000590 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
591 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
592 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000593 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
594 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000595}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000596void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
597 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
598}
John McCall17001972009-10-18 01:05:36 +0000599void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
600 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000601}
602void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
603 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000604 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
605 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
606 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
607 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000608}
John McCallfc93cf92009-10-22 22:37:11 +0000609void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000611}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000612void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
613 Writer.AddSourceLocation(TL.getKWLoc(), Record);
614 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
615 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
616}
John McCall8f115c62009-10-16 21:56:05 +0000617
Chris Lattner19cea4e2009-04-22 05:57:30 +0000618//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000619// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000620//===----------------------------------------------------------------------===//
621
Chris Lattner28fa4e62009-04-26 22:26:21 +0000622static void EmitBlockID(unsigned ID, const char *Name,
623 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000624 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000625 Record.clear();
626 Record.push_back(ID);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
628
629 // Emit the block name if present.
630 if (Name == 0 || Name[0] == 0) return;
631 Record.clear();
632 while (*Name)
633 Record.push_back(*Name++);
634 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
635}
636
637static void EmitRecordID(unsigned ID, const char *Name,
638 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000639 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000640 Record.clear();
641 Record.push_back(ID);
642 while (*Name)
643 Record.push_back(*Name++);
644 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000645}
646
647static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000648 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000649#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000650 RECORD(STMT_STOP);
651 RECORD(STMT_NULL_PTR);
652 RECORD(STMT_NULL);
653 RECORD(STMT_COMPOUND);
654 RECORD(STMT_CASE);
655 RECORD(STMT_DEFAULT);
656 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000657 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000658 RECORD(STMT_IF);
659 RECORD(STMT_SWITCH);
660 RECORD(STMT_WHILE);
661 RECORD(STMT_DO);
662 RECORD(STMT_FOR);
663 RECORD(STMT_GOTO);
664 RECORD(STMT_INDIRECT_GOTO);
665 RECORD(STMT_CONTINUE);
666 RECORD(STMT_BREAK);
667 RECORD(STMT_RETURN);
668 RECORD(STMT_DECL);
669 RECORD(STMT_ASM);
670 RECORD(EXPR_PREDEFINED);
671 RECORD(EXPR_DECL_REF);
672 RECORD(EXPR_INTEGER_LITERAL);
673 RECORD(EXPR_FLOATING_LITERAL);
674 RECORD(EXPR_IMAGINARY_LITERAL);
675 RECORD(EXPR_STRING_LITERAL);
676 RECORD(EXPR_CHARACTER_LITERAL);
677 RECORD(EXPR_PAREN);
678 RECORD(EXPR_UNARY_OPERATOR);
679 RECORD(EXPR_SIZEOF_ALIGN_OF);
680 RECORD(EXPR_ARRAY_SUBSCRIPT);
681 RECORD(EXPR_CALL);
682 RECORD(EXPR_MEMBER);
683 RECORD(EXPR_BINARY_OPERATOR);
684 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
685 RECORD(EXPR_CONDITIONAL_OPERATOR);
686 RECORD(EXPR_IMPLICIT_CAST);
687 RECORD(EXPR_CSTYLE_CAST);
688 RECORD(EXPR_COMPOUND_LITERAL);
689 RECORD(EXPR_EXT_VECTOR_ELEMENT);
690 RECORD(EXPR_INIT_LIST);
691 RECORD(EXPR_DESIGNATED_INIT);
692 RECORD(EXPR_IMPLICIT_VALUE_INIT);
693 RECORD(EXPR_VA_ARG);
694 RECORD(EXPR_ADDR_LABEL);
695 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000696 RECORD(EXPR_CHOOSE);
697 RECORD(EXPR_GNU_NULL);
698 RECORD(EXPR_SHUFFLE_VECTOR);
699 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000700 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000701 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000702 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000703 RECORD(EXPR_OBJC_ARRAY_LITERAL);
704 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000705 RECORD(EXPR_OBJC_ENCODE);
706 RECORD(EXPR_OBJC_SELECTOR_EXPR);
707 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
708 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
709 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
710 RECORD(EXPR_OBJC_KVC_REF_EXPR);
711 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000712 RECORD(STMT_OBJC_FOR_COLLECTION);
713 RECORD(STMT_OBJC_CATCH);
714 RECORD(STMT_OBJC_FINALLY);
715 RECORD(STMT_OBJC_AT_TRY);
716 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
717 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000718 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000719 RECORD(EXPR_CXX_OPERATOR_CALL);
720 RECORD(EXPR_CXX_CONSTRUCT);
721 RECORD(EXPR_CXX_STATIC_CAST);
722 RECORD(EXPR_CXX_DYNAMIC_CAST);
723 RECORD(EXPR_CXX_REINTERPRET_CAST);
724 RECORD(EXPR_CXX_CONST_CAST);
725 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000726 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000727 RECORD(EXPR_CXX_BOOL_LITERAL);
728 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000729 RECORD(EXPR_CXX_TYPEID_EXPR);
730 RECORD(EXPR_CXX_TYPEID_TYPE);
731 RECORD(EXPR_CXX_UUIDOF_EXPR);
732 RECORD(EXPR_CXX_UUIDOF_TYPE);
733 RECORD(EXPR_CXX_THIS);
734 RECORD(EXPR_CXX_THROW);
735 RECORD(EXPR_CXX_DEFAULT_ARG);
736 RECORD(EXPR_CXX_BIND_TEMPORARY);
737 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
738 RECORD(EXPR_CXX_NEW);
739 RECORD(EXPR_CXX_DELETE);
740 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
741 RECORD(EXPR_EXPR_WITH_CLEANUPS);
742 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
743 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
744 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
745 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
746 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
747 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
748 RECORD(EXPR_CXX_NOEXCEPT);
749 RECORD(EXPR_OPAQUE_VALUE);
750 RECORD(EXPR_BINARY_TYPE_TRAIT);
751 RECORD(EXPR_PACK_EXPANSION);
752 RECORD(EXPR_SIZEOF_PACK);
753 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000754 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000755#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000756}
Mike Stump11289f42009-09-09 15:08:12 +0000757
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000758void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000759 RecordData Record;
760 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000761
Sebastian Redl539c5062010-08-18 23:57:32 +0000762#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
763#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000764
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000765 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000766 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000767 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000768 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000769 RECORD(TYPE_OFFSET);
770 RECORD(DECL_OFFSET);
771 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000772 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(IDENTIFIER_OFFSET);
774 RECORD(IDENTIFIER_TABLE);
775 RECORD(EXTERNAL_DEFINITIONS);
776 RECORD(SPECIAL_TYPES);
777 RECORD(STATISTICS);
778 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000779 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000780 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
781 RECORD(SELECTOR_OFFSETS);
782 RECORD(METHOD_POOL);
783 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000784 RECORD(SOURCE_LOCATION_OFFSETS);
785 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000786 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000787 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000788 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000789 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000790 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000791 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000792 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000793 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000794 RECORD(SEMA_DECL_REFS);
795 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
796 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
797 RECORD(DECL_REPLACEMENTS);
798 RECORD(UPDATE_VISIBLE);
799 RECORD(DECL_UPDATE_OFFSETS);
800 RECORD(DECL_UPDATES);
801 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
802 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000803 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000804 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000805 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000806 RECORD(FP_PRAGMA_OPTIONS);
807 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000808 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000809 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
810 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000811 RECORD(MODULE_OFFSET_MAP);
812 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000813 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000814 RECORD(FILE_SORTED_DECLS);
815 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000816 RECORD(MERGED_DECLARATIONS);
817 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000818 RECORD(OBJC_CATEGORIES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000819
Chris Lattner28fa4e62009-04-26 22:26:21 +0000820 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000821 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 RECORD(SM_SLOC_FILE_ENTRY);
823 RECORD(SM_SLOC_BUFFER_ENTRY);
824 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000825 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattner28fa4e62009-04-26 22:26:21 +0000827 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000828 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000829 RECORD(PP_MACRO_OBJECT_LIKE);
830 RECORD(PP_MACRO_FUNCTION_LIKE);
831 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000832
Douglas Gregor12bfa382009-10-17 00:13:19 +0000833 // Decls and Types block.
834 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000835 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000836 RECORD(TYPE_COMPLEX);
837 RECORD(TYPE_POINTER);
838 RECORD(TYPE_BLOCK_POINTER);
839 RECORD(TYPE_LVALUE_REFERENCE);
840 RECORD(TYPE_RVALUE_REFERENCE);
841 RECORD(TYPE_MEMBER_POINTER);
842 RECORD(TYPE_CONSTANT_ARRAY);
843 RECORD(TYPE_INCOMPLETE_ARRAY);
844 RECORD(TYPE_VARIABLE_ARRAY);
845 RECORD(TYPE_VECTOR);
846 RECORD(TYPE_EXT_VECTOR);
847 RECORD(TYPE_FUNCTION_PROTO);
848 RECORD(TYPE_FUNCTION_NO_PROTO);
849 RECORD(TYPE_TYPEDEF);
850 RECORD(TYPE_TYPEOF_EXPR);
851 RECORD(TYPE_TYPEOF);
852 RECORD(TYPE_RECORD);
853 RECORD(TYPE_ENUM);
854 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000855 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000856 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000857 RECORD(TYPE_DECLTYPE);
858 RECORD(TYPE_ELABORATED);
859 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
860 RECORD(TYPE_UNRESOLVED_USING);
861 RECORD(TYPE_INJECTED_CLASS_NAME);
862 RECORD(TYPE_OBJC_OBJECT);
863 RECORD(TYPE_TEMPLATE_TYPE_PARM);
864 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
865 RECORD(TYPE_DEPENDENT_NAME);
866 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
867 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
868 RECORD(TYPE_PAREN);
869 RECORD(TYPE_PACK_EXPANSION);
870 RECORD(TYPE_ATTRIBUTED);
871 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000872 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000873 RECORD(DECL_TYPEDEF);
874 RECORD(DECL_ENUM);
875 RECORD(DECL_RECORD);
876 RECORD(DECL_ENUM_CONSTANT);
877 RECORD(DECL_FUNCTION);
878 RECORD(DECL_OBJC_METHOD);
879 RECORD(DECL_OBJC_INTERFACE);
880 RECORD(DECL_OBJC_PROTOCOL);
881 RECORD(DECL_OBJC_IVAR);
882 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000883 RECORD(DECL_OBJC_CATEGORY);
884 RECORD(DECL_OBJC_CATEGORY_IMPL);
885 RECORD(DECL_OBJC_IMPLEMENTATION);
886 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
887 RECORD(DECL_OBJC_PROPERTY);
888 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000889 RECORD(DECL_FIELD);
890 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000891 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000892 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000893 RECORD(DECL_FILE_SCOPE_ASM);
894 RECORD(DECL_BLOCK);
895 RECORD(DECL_CONTEXT_LEXICAL);
896 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000897 RECORD(DECL_NAMESPACE);
898 RECORD(DECL_NAMESPACE_ALIAS);
899 RECORD(DECL_USING);
900 RECORD(DECL_USING_SHADOW);
901 RECORD(DECL_USING_DIRECTIVE);
902 RECORD(DECL_UNRESOLVED_USING_VALUE);
903 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
904 RECORD(DECL_LINKAGE_SPEC);
905 RECORD(DECL_CXX_RECORD);
906 RECORD(DECL_CXX_METHOD);
907 RECORD(DECL_CXX_CONSTRUCTOR);
908 RECORD(DECL_CXX_DESTRUCTOR);
909 RECORD(DECL_CXX_CONVERSION);
910 RECORD(DECL_ACCESS_SPEC);
911 RECORD(DECL_FRIEND);
912 RECORD(DECL_FRIEND_TEMPLATE);
913 RECORD(DECL_CLASS_TEMPLATE);
914 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
915 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
916 RECORD(DECL_FUNCTION_TEMPLATE);
917 RECORD(DECL_TEMPLATE_TYPE_PARM);
918 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
919 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
920 RECORD(DECL_STATIC_ASSERT);
921 RECORD(DECL_CXX_BASE_SPECIFIERS);
922 RECORD(DECL_INDIRECTFIELD);
923 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
924
Douglas Gregor03412ba2011-06-03 02:27:19 +0000925 // Statements and Exprs can occur in the Decls and Types block.
926 AddStmtsExprs(Stream, Record);
927
Douglas Gregor92a96f52011-02-08 21:58:10 +0000928 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000929 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000930 RECORD(PPD_MACRO_DEFINITION);
931 RECORD(PPD_INCLUSION_DIRECTIVE);
932
Chris Lattner28fa4e62009-04-26 22:26:21 +0000933#undef RECORD
934#undef BLOCK
935 Stream.ExitBlock();
936}
937
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000938/// \brief Adjusts the given filename to only write out the portion of the
939/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000940///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000941/// \param Filename the file name to adjust.
942///
943/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
944/// the returned filename will be adjusted by this system root.
945///
946/// \returns either the original filename (if it needs no adjustment) or the
947/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000948static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000949adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000950 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000951
Douglas Gregorc567ba22011-07-22 16:35:34 +0000952 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000953 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000954
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000955 // Verify that the filename and the system root have the same prefix.
956 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000957 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 if (Filename[Pos] != isysroot[Pos])
959 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000960
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000961 // We hit the end of the filename before we hit the end of the system root.
962 if (!Filename[Pos])
963 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000964
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965 // If the file name has a '/' at the current position, skip over the '/'.
966 // We distinguish sysroot-based includes from absolute includes by the
967 // absence of '/' at the beginning of sysroot-based includes.
968 if (Filename[Pos] == '/')
969 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000971 return Filename + Pos;
972}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000973
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000974/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000975void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000976 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000977 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000978
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000979 // Metadata
Douglas Gregore8bbc122011-09-02 00:18:52 +0000980 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000981 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000982 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000983 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
984 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
986 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
987 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +0000988 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregor29cc6422011-08-17 21:07:30 +0000989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000990 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000991
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000992 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000993 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000994 Record.push_back(VERSION_MAJOR);
995 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000996 Record.push_back(CLANG_VERSION_MAJOR);
997 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +0000998 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +0000999 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor29cc6422011-08-17 21:07:30 +00001000 const std::string &Triple = Target.getTriple().getTriple();
1001 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1002
1003 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001004 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1005 llvm::SmallVector<char, 128> ModulePaths;
1006 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001007
1008 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1009 M != MEnd; ++M) {
1010 // Skip modules that weren't directly imported.
1011 if (!(*M)->isDirectlyImported())
1012 continue;
1013
1014 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1015 // FIXME: Write import location, once it matters.
1016 // FIXME: This writes the absolute path for AST files we depend on.
1017 const std::string &FileName = (*M)->FileName;
1018 Record.push_back(FileName.size());
1019 Record.append(FileName.begin(), FileName.end());
1020 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001021 Stream.EmitRecord(IMPORTS, Record);
1022 }
Mike Stump11289f42009-09-09 15:08:12 +00001023
Douglas Gregora3b20262011-05-06 21:43:30 +00001024 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001025 SourceManager &SM = Context.getSourceManager();
1026 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1027 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001028 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001029 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1030 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1031
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001032 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001033
Michael J. Spencer740857f2010-12-21 16:45:57 +00001034 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001035
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001036 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001037 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001038 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001039 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001040 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001041 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001042
1043 Record.clear();
1044 Record.push_back(SM.getMainFileID().getOpaqueValue());
1045 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001046 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001047
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001048 // Original PCH directory
1049 if (!OutputFile.empty() && OutputFile != "-") {
1050 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1051 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1053 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1054
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001055 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001056
1057 llvm::sys::fs::make_absolute(OutputPath);
1058 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1059
1060 RecordData Record;
1061 Record.push_back(ORIGINAL_PCH_DIR);
1062 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1063 }
1064
Ted Kremenek18e066f2010-01-22 22:12:47 +00001065 // Repository branch/version information.
1066 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001067 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001068 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1069 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001070 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001071 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001072 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1073 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001074}
1075
1076/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001077void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001078 RecordData Record;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00001079#define LANGOPT(Name, Bits, Default, Description) \
1080 Record.push_back(LangOpts.Name);
1081#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1082 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1083#include "clang/Basic/LangOptions.def"
Douglas Gregor7d106e42011-11-15 19:35:01 +00001084
1085 Record.push_back(LangOpts.CurrentModule.size());
1086 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl539c5062010-08-18 23:57:32 +00001087 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001088}
1089
Douglas Gregora7f71a92009-04-10 03:52:48 +00001090//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001091// stat cache Serialization
1092//===----------------------------------------------------------------------===//
1093
1094namespace {
1095// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001096class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001097public:
1098 typedef const char * key_type;
1099 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001100
Chris Lattner2a6fa472010-11-23 19:28:12 +00001101 typedef struct stat data_type;
1102 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001103
1104 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001105 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
1108 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001109 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 data_type_ref Data) {
1111 unsigned StrLen = strlen(path);
1112 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001113 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001114 clang::io::Emit8(Out, DataLen);
1115 return std::make_pair(StrLen + 1, DataLen);
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001118 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001119 Out.write(path, KeyLen);
1120 }
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001122 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001123 data_type_ref Data, unsigned DataLen) {
1124 using namespace clang::io;
1125 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner2a6fa472010-11-23 19:28:12 +00001127 Emit32(Out, (uint32_t) Data.st_ino);
1128 Emit32(Out, (uint32_t) Data.st_dev);
1129 Emit16(Out, (uint16_t) Data.st_mode);
1130 Emit64(Out, (uint64_t) Data.st_mtime);
1131 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001132
1133 assert(Out.tell() - Start == DataLen && "Wrong data length");
1134 }
1135};
1136} // end anonymous namespace
1137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001138/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001139void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001140 // Build the on-disk hash table containing information about every
1141 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001142 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001143 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001144 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001145 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001146 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001147 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001148 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregorc5046832009-04-27 18:38:38 +00001151 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001152 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001153 uint32_t BucketOffset;
1154 {
1155 llvm::raw_svector_ostream Out(StatCacheData);
1156 // Make sure that no bucket is at offset 0
1157 clang::io::Emit32(Out, 0);
1158 BucketOffset = Generator.Emit(Out);
1159 }
1160
1161 // Create a blob abbreviation
1162 using namespace llvm;
1163 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001164 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001165 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1168 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1169
1170 // Write the stat cache
1171 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001172 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001173 Record.push_back(BucketOffset);
1174 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001175 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001176}
1177
1178//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001179// Source Manager Serialization
1180//===----------------------------------------------------------------------===//
1181
1182/// \brief Create an abbreviation for the SLocEntry that refers to a
1183/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001184static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 using namespace llvm;
1186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001192 // FileEntry fields.
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001200 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001201}
1202
1203/// \brief Create an abbreviation for the SLocEntry that refers to a
1204/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001205static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001214 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001215}
1216
1217/// \brief Create an abbreviation for the SLocEntry that refers to a
1218/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001219static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001224 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225}
1226
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001227/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1228/// expansion.
1229static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230 using namespace llvm;
1231 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001232 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001238 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001239}
1240
Douglas Gregor09b69892011-02-10 17:09:37 +00001241namespace {
1242 // Trait used for the on-disk hash table of header search information.
1243 class HeaderFileInfoTrait {
1244 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001245
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001246 // Keep track of the framework names we've used during serialization.
1247 SmallVector<char, 128> FrameworkStringData;
1248 llvm::StringMap<unsigned> FrameworkNameOffset;
1249
Douglas Gregor09b69892011-02-10 17:09:37 +00001250 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001251 HeaderFileInfoTrait(ASTWriter &Writer)
1252 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001253
1254 typedef const char *key_type;
1255 typedef key_type key_type_ref;
1256
1257 typedef HeaderFileInfo data_type;
1258 typedef const data_type &data_type_ref;
1259
1260 static unsigned ComputeHash(const char *path) {
1261 // The hash is based only on the filename portion of the key, so that the
1262 // reader can match based on filenames when symlinking or excess path
1263 // elements ("foo/../", "../") change the form of the name. However,
1264 // complete path is still the key.
1265 return llvm::HashString(llvm::sys::path::filename(path));
1266 }
1267
1268 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001269 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001270 data_type_ref Data) {
1271 unsigned StrLen = strlen(path);
1272 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001273 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001274 clang::io::Emit8(Out, DataLen);
1275 return std::make_pair(StrLen + 1, DataLen);
1276 }
1277
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001278 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001279 Out.write(path, KeyLen);
1280 }
1281
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001282 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001283 data_type_ref Data, unsigned DataLen) {
1284 using namespace clang::io;
1285 uint64_t Start = Out.tell(); (void)Start;
1286
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001287 unsigned char Flags = (Data.isImport << 5)
1288 | (Data.isPragmaOnce << 4)
1289 | (Data.DirInfo << 2)
1290 | (Data.Resolved << 1)
1291 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001292 Emit8(Out, (uint8_t)Flags);
1293 Emit16(Out, (uint16_t) Data.NumIncludes);
1294
1295 if (!Data.ControllingMacro)
1296 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1297 else
1298 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001299
1300 unsigned Offset = 0;
1301 if (!Data.Framework.empty()) {
1302 // If this header refers into a framework, save the framework name.
1303 llvm::StringMap<unsigned>::iterator Pos
1304 = FrameworkNameOffset.find(Data.Framework);
1305 if (Pos == FrameworkNameOffset.end()) {
1306 Offset = FrameworkStringData.size() + 1;
1307 FrameworkStringData.append(Data.Framework.begin(),
1308 Data.Framework.end());
1309 FrameworkStringData.push_back(0);
1310
1311 FrameworkNameOffset[Data.Framework] = Offset;
1312 } else
1313 Offset = Pos->second;
1314 }
1315 Emit32(Out, Offset);
1316
Douglas Gregor09b69892011-02-10 17:09:37 +00001317 assert(Out.tell() - Start == DataLen && "Wrong data length");
1318 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001319
1320 const char *strings_begin() const { return FrameworkStringData.begin(); }
1321 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001322 };
1323} // end anonymous namespace
1324
1325/// \brief Write the header search block for the list of files that
1326///
1327/// \param HS The header search structure to save.
1328///
1329/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001330void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001331 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001332 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1333
1334 if (FilesByUID.size() > HS.header_file_size())
1335 FilesByUID.resize(HS.header_file_size());
1336
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001337 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001338 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001339 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001340 unsigned NumHeaderSearchEntries = 0;
1341 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1342 const FileEntry *File = FilesByUID[UID];
1343 if (!File)
1344 continue;
1345
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001346 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1347 // from the external source if it was not provided already.
1348 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001349 if (HFI.External && Chain)
1350 continue;
1351
1352 // Turn the file name into an absolute path, if it isn't already.
1353 const char *Filename = File->getName();
1354 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1355
1356 // If we performed any translation on the file name at all, we need to
1357 // save this string, since the generator will refer to it later.
1358 if (Filename != File->getName()) {
1359 Filename = strdup(Filename);
1360 SavedStrings.push_back(Filename);
1361 }
1362
1363 Generator.insert(Filename, HFI, GeneratorTrait);
1364 ++NumHeaderSearchEntries;
1365 }
1366
1367 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001368 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001369 uint32_t BucketOffset;
1370 {
1371 llvm::raw_svector_ostream Out(TableData);
1372 // Make sure that no bucket is at offset 0
1373 clang::io::Emit32(Out, 0);
1374 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1375 }
1376
1377 // Create a blob abbreviation
1378 using namespace llvm;
1379 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1380 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1385 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1386
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001387 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001388 RecordData Record;
1389 Record.push_back(HEADER_SEARCH_TABLE);
1390 Record.push_back(BucketOffset);
1391 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001392 Record.push_back(TableData.size());
1393 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001394 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1395
1396 // Free all of the strings we had to duplicate.
1397 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1398 free((void*)SavedStrings[I]);
1399}
1400
Douglas Gregora7f71a92009-04-10 03:52:48 +00001401/// \brief Writes the block containing the serialized form of the
1402/// source manager.
1403///
1404/// TODO: We should probably use an on-disk hash table (stored in a
1405/// blob), indexed based on the file name, so that we only create
1406/// entries for files that we actually need. In the common case (no
1407/// errors), we probably won't have to create file entries for any of
1408/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001409void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001410 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001411 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001412 RecordData Record;
1413
Chris Lattner0910e3b2009-04-10 17:16:57 +00001414 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001415 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001416
1417 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001418 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1419 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1420 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001421 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001422
Douglas Gregor258ae542009-04-27 06:38:32 +00001423 // Write out the source location entry table. We skip the first
1424 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001425 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001426 // Write out the offsets of only source location file entries.
1427 // We will go through them in ASTReader::validateFileEntries().
1428 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001429 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001430 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1431 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001432 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001433 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001434 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001435
Douglas Gregor258ae542009-04-27 06:38:32 +00001436 // Record the offset of this source-location entry.
1437 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1438
1439 // Figure out which record code to use.
1440 unsigned Code;
1441 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001442 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1443 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001444 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001445 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1446 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001447 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001448 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001449 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001450 Record.clear();
1451 Record.push_back(Code);
1452
Douglas Gregor925296b2011-07-19 16:10:42 +00001453 // Starting offset of this entry within this module, so skip the dummy.
1454 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001455 if (SLoc->isFile()) {
1456 const SrcMgr::FileInfo &File = SLoc->getFile();
1457 Record.push_back(File.getIncludeLoc().getRawEncoding());
1458 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1459 Record.push_back(File.hasLineDirectives());
1460
1461 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001462 if (Content->OrigEntry) {
1463 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001464 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001465
Douglas Gregor258ae542009-04-27 06:38:32 +00001466 // The source location entry is a file. The blob associated
1467 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001468
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001469 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001470 Record.push_back(Content->OrigEntry->getSize());
1471 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001472 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001473 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001474
1475 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1476 if (FDI != FileDeclIDs.end()) {
1477 Record.push_back(FDI->second->FirstDeclIndex);
1478 Record.push_back(FDI->second->DeclIDs.size());
1479 } else {
1480 Record.push_back(0);
1481 Record.push_back(0);
1482 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001483
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001484 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001485 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001486 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001487
1488 // Ask the file manager to fixup the relative path for us. This will
1489 // honor the working directory.
1490 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1491
1492 // FIXME: This call to make_absolute shouldn't be necessary, the
1493 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001494 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001495 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001496
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001497 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001498 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001499
1500 if (Content->BufferOverridden) {
1501 Record.clear();
1502 Record.push_back(SM_SLOC_BUFFER_BLOB);
1503 const llvm::MemoryBuffer *Buffer
1504 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1505 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1506 StringRef(Buffer->getBufferStart(),
1507 Buffer->getBufferSize() + 1));
1508 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001509 } else {
1510 // The source location entry is a buffer. The blob associated
1511 // with this entry contains the contents of the buffer.
1512
1513 // We add one to the size so that we capture the trailing NULL
1514 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1515 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001516 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001517 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001518 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001519 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001520 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001521 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001522 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001523 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001524 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001525 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001526
Douglas Gregor925296b2011-07-19 16:10:42 +00001527 if (strcmp(Name, "<built-in>") == 0) {
1528 PreloadSLocs.push_back(SLocEntryOffsets.size());
1529 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001530 }
1531 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001532 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001533 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001534 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1535 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001536 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1537 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001538
1539 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001540 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001541 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001542 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001543 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001544 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001545 }
1546 }
1547
Douglas Gregor8f45df52009-04-16 22:23:12 +00001548 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001549
1550 if (SLocEntryOffsets.empty())
1551 return;
1552
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001553 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001554 // table is used for lazily loading source-location information.
1555 using namespace llvm;
1556 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001557 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1561 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregor258ae542009-04-27 06:38:32 +00001563 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001564 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001565 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001566 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001567 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001568
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001569 Abbrev = new BitCodeAbbrev();
1570 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1573 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1574
1575 Record.clear();
1576 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1577 Record.push_back(SLocFileEntryOffsets.size());
1578 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1579 data(SLocFileEntryOffsets));
1580
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001581 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001582 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001583 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001584
1585 // Write the line table. It depends on remapping working, so it must come
1586 // after the source location offsets.
1587 if (SourceMgr.hasLineTable()) {
1588 LineTableInfo &LineTable = SourceMgr.getLineTable();
1589
1590 Record.clear();
1591 // Emit the file names
1592 Record.push_back(LineTable.getNumFilenames());
1593 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1594 // Emit the file name
1595 const char *Filename = LineTable.getFilename(I);
1596 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1597 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1598 Record.push_back(FilenameLen);
1599 if (FilenameLen)
1600 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1601 }
1602
1603 // Emit the line entries
1604 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1605 L != LEnd; ++L) {
1606 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001607 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001608 continue;
1609
1610 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001611 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001612
1613 // Emit the line entries
1614 Record.push_back(L->second.size());
1615 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1616 LEEnd = L->second.end();
1617 LE != LEEnd; ++LE) {
1618 Record.push_back(LE->FileOffset);
1619 Record.push_back(LE->LineNo);
1620 Record.push_back(LE->FilenameID);
1621 Record.push_back((unsigned)LE->FileKind);
1622 Record.push_back(LE->IncludeOffset);
1623 }
1624 }
1625 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1626 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001627}
1628
Douglas Gregorc5046832009-04-27 18:38:38 +00001629//===----------------------------------------------------------------------===//
1630// Preprocessor Serialization
1631//===----------------------------------------------------------------------===//
1632
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001633static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1634 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1635 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1636 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1637 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1638 return X.first->getName().compare(Y.first->getName());
1639}
1640
Chris Lattnereeffaef2009-04-10 17:15:23 +00001641/// \brief Writes the block containing the serialized form of the
1642/// preprocessor.
1643///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001644void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001645 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1646 if (PPRec)
1647 WritePreprocessorDetail(*PPRec);
1648
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001649 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001650
Chris Lattner0af3ba12009-04-13 01:29:17 +00001651 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1652 if (PP.getCounterValue() != 0) {
1653 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001654 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001655 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001656 }
1657
1658 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001659 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001660
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001661 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001662 // FIXME: use diagnostics subsystem for localization etc.
1663 if (PP.SawDateOrTime())
1664 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001665
Douglas Gregor796d76a2010-10-20 22:00:55 +00001666
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001667 // Loop over all the macro definitions that are live at the end of the file,
1668 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001669
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001670 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001671 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001672 MacrosToEmit;
1673 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001674 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1675 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001676 I != E; ++I) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001677 const IdentifierInfo *Name = I->first;
Douglas Gregorebf00492011-10-17 15:32:29 +00001678 if (!IsModule || I->second->isPublic()) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001679 MacroDefinitionsSeen.insert(Name);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001680 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1681 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001682 }
1683
1684 // Sort the set of macro definitions that need to be serialized by the
1685 // name of the macro, to provide a stable ordering.
1686 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1687 &compareMacroDefinitions);
1688
Douglas Gregor68051a72011-02-11 00:26:14 +00001689 // Resolve any identifiers that defined macros at the time they were
1690 // deserialized, adding them to the list of macros to emit (if appropriate).
1691 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1692 IdentifierInfo *Name
1693 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1694 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1695 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1696 }
1697
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001698 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1699 const IdentifierInfo *Name = MacrosToEmit[I].first;
1700 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001701 if (!MI)
1702 continue;
1703
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001704 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001705 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001706 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001707
1708 // FIXME: There is a (probably minor) optimization we could do here, if
1709 // the macro comes from the original PCH but the identifier comes from a
1710 // chained PCH, by storing the offset into the original PCH rather than
1711 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001712 if (MI->isBuiltinMacro() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001713 (Chain &&
1714 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1715 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001716 continue;
1717
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001718 AddIdentifierRef(Name, Record);
1719 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001720 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1721 Record.push_back(MI->isUsed());
Douglas Gregorebf00492011-10-17 15:32:29 +00001722 Record.push_back(MI->isPublic());
1723 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001724 unsigned Code;
1725 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001726 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001727 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001728 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001729
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001730 Record.push_back(MI->isC99Varargs());
1731 Record.push_back(MI->isGNUVarargs());
1732 Record.push_back(MI->getNumArgs());
1733 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1734 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001735 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001736 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001737
Douglas Gregoraae92242010-03-19 21:51:54 +00001738 // If we have a detailed preprocessing record, record the macro definition
1739 // ID that corresponds to this macro.
1740 if (PPRec)
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001741 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001742
Douglas Gregor8f45df52009-04-16 22:23:12 +00001743 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001744 Record.clear();
1745
Chris Lattner2199f5b2009-04-10 18:08:30 +00001746 // Emit the tokens array.
1747 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1748 // Note that we know that the preprocessor does not have any annotation
1749 // tokens in it because they are created by the parser, and thus can't be
1750 // in a macro definition.
1751 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001752
Chris Lattner2199f5b2009-04-10 18:08:30 +00001753 Record.push_back(Tok.getLocation().getRawEncoding());
1754 Record.push_back(Tok.getLength());
1755
Chris Lattner2199f5b2009-04-10 18:08:30 +00001756 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1757 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001758 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001759 // FIXME: Should translate token kind to a stable encoding.
1760 Record.push_back(Tok.getKind());
1761 // FIXME: Should translate token flags to a stable encoding.
1762 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001763
Sebastian Redl539c5062010-08-18 23:57:32 +00001764 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001765 Record.clear();
1766 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001767 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001768 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001769 Stream.ExitBlock();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001770}
1771
1772void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001773 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001774 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001775
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001776 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001777
Douglas Gregor92a96f52011-02-08 21:58:10 +00001778 // Enter the preprocessor block.
1779 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001780
Douglas Gregoraae92242010-03-19 21:51:54 +00001781 // If the preprocessor has a preprocessing record, emit it.
1782 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001783 using namespace llvm;
1784
1785 // Set up the abbreviation for
1786 unsigned InclusionAbbrev = 0;
1787 {
1788 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1789 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1794 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1795 }
1796
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001797 unsigned FirstPreprocessorEntityID
1798 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1799 + NUM_PREDEF_PP_ENTITY_IDS;
1800 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001801 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001802 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1803 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001804 E != EEnd;
1805 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001806 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001807
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001808 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1809 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001810
Douglas Gregor92a96f52011-02-08 21:58:10 +00001811 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001812 // Record this macro definition's ID.
1813 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001814
Douglas Gregor92a96f52011-02-08 21:58:10 +00001815 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001816 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1817 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001818 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001819
Chandler Carrutha88a22182011-07-14 08:20:46 +00001820 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001821 Record.push_back(ME->isBuiltinMacro());
1822 if (ME->isBuiltinMacro())
1823 AddIdentifierRef(ME->getName(), Record);
1824 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001825 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001826 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001827 continue;
1828 }
1829
1830 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1831 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001832 Record.push_back(ID->getFileName().size());
1833 Record.push_back(ID->wasInQuotes());
1834 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001835 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001836 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001837 // Check that the FileEntry is not null because it was not resolved and
1838 // we create a PCH even with compiler errors.
1839 if (ID->getFile())
1840 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001841 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1842 continue;
1843 }
1844
1845 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1846 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001847 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001848
Douglas Gregoraae92242010-03-19 21:51:54 +00001849 // Write the offsets table for the preprocessing record.
1850 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001851 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1852
Douglas Gregoraae92242010-03-19 21:51:54 +00001853 // Write the offsets table for identifier IDs.
1854 using namespace llvm;
1855 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001856 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001857 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001858 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001859 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001860
Douglas Gregoraae92242010-03-19 21:51:54 +00001861 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001862 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001863 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001864 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1865 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001866 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001867}
1868
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001869unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1870 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1871 if (Known != SubmoduleIDs.end())
1872 return Known->second;
1873
1874 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1875}
1876
Douglas Gregor253eefe2011-12-01 00:59:36 +00001877/// \brief Compute the number of modules within the given tree (including the
1878/// given module).
1879static unsigned getNumberOfModules(Module *Mod) {
1880 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001881 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1882 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001883 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001884 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001885
1886 return ChildModules + 1;
1887}
1888
Douglas Gregorde3ef502011-11-30 23:21:26 +00001889void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001890 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001891 // FIXME: This feels like it belongs somewhere else, but there are no
1892 // other consumers of this information.
1893 SourceManager &SrcMgr = PP->getSourceManager();
1894 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1895 for (ASTContext::import_iterator I = Context->local_import_begin(),
1896 IEnd = Context->local_import_end();
1897 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001898 if (Module *ImportedFrom
1899 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1900 SrcMgr))) {
1901 ImportedFrom->Imports.push_back(I->getImportedModule());
1902 }
1903 }
1904
Douglas Gregor69021972011-11-30 17:33:56 +00001905 // Enter the submodule description block.
1906 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1907
1908 // Write the abbreviations needed for the submodules block.
1909 using namespace llvm;
1910 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1911 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00001913 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00001916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00001918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00001919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00001920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1921 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1922
1923 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001924 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00001925 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1926 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1927
1928 Abbrev = new BitCodeAbbrev();
1929 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1930 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1931 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00001932
1933 Abbrev = new BitCodeAbbrev();
1934 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1936 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1937
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001938 Abbrev = new BitCodeAbbrev();
1939 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1941 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1942
Douglas Gregor253eefe2011-12-01 00:59:36 +00001943 // Write the submodule metadata block.
1944 RecordData Record;
1945 Record.push_back(getNumberOfModules(WritingModule));
1946 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1947 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1948
Douglas Gregor69021972011-11-30 17:33:56 +00001949 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001950 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00001951 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00001952 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001953 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00001954 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001955 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00001956
1957 // Emit the definition of the block.
1958 Record.clear();
1959 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001960 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00001961 if (Mod->Parent) {
1962 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1963 Record.push_back(SubmoduleIDs[Mod->Parent]);
1964 } else {
1965 Record.push_back(0);
1966 }
1967 Record.push_back(Mod->IsFramework);
1968 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00001969 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00001970 Record.push_back(Mod->InferSubmodules);
1971 Record.push_back(Mod->InferExplicitSubmodules);
1972 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00001973 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1974
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001975 // Emit the requirements.
1976 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1977 Record.clear();
1978 Record.push_back(SUBMODULE_REQUIRES);
1979 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1980 Mod->Requires[I].data(),
1981 Mod->Requires[I].size());
1982 }
1983
Douglas Gregor69021972011-11-30 17:33:56 +00001984 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00001985 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00001986 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001987 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00001988 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00001989 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00001990 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1991 Record.clear();
1992 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1993 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1994 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00001995 }
1996
1997 // Emit the headers.
1998 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1999 Record.clear();
2000 Record.push_back(SUBMODULE_HEADER);
2001 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2002 Mod->Headers[I]->getName());
2003 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002004
2005 // Emit the imports.
2006 if (!Mod->Imports.empty()) {
2007 Record.clear();
2008 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002009 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002010 assert(ImportedID && "Unknown submodule!");
2011 Record.push_back(ImportedID);
2012 }
2013 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2014 }
2015
Douglas Gregor24bb9232011-12-02 18:58:38 +00002016 // Emit the exports.
2017 if (!Mod->Exports.empty()) {
2018 Record.clear();
2019 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002020 if (Module *Exported = Mod->Exports[I].getPointer()) {
2021 unsigned ExportedID = SubmoduleIDs[Exported];
2022 assert(ExportedID > 0 && "Unknown submodule ID?");
2023 Record.push_back(ExportedID);
2024 } else {
2025 Record.push_back(0);
2026 }
2027
Douglas Gregor24bb9232011-12-02 18:58:38 +00002028 Record.push_back(Mod->Exports[I].getInt());
2029 }
2030 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2031 }
2032
Douglas Gregor69021972011-11-30 17:33:56 +00002033 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002034 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2035 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002036 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002037 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002038 }
2039
2040 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002041
2042 assert((NextSubmoduleID - FirstSubmoduleID
2043 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002044}
2045
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002046serialization::SubmoduleID
2047ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002048 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002049 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002050
2051 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002052 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002053 Module *OwningMod
2054 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002055 if (!OwningMod)
2056 return 0;
2057
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002058 // Check whether this submodule is part of our own module.
2059 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002060 return 0;
2061
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002062 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002063}
2064
David Blaikie9c902b52011-09-25 23:23:43 +00002065void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002066 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002067 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002068 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2069 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002070 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002071 if (point.Loc.isInvalid())
2072 continue;
2073
2074 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002075 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002076 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002077 if (I->second.isPragma()) {
2078 Record.push_back(I->first);
2079 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002080 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002081 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002082 Record.push_back(-1); // mark the end of the diag/map pairs for this
2083 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002084 }
2085
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002086 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002087 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002088}
2089
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002090void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2091 if (CXXBaseSpecifiersOffsets.empty())
2092 return;
2093
2094 RecordData Record;
2095
2096 // Create a blob abbreviation for the C++ base specifiers offsets.
2097 using namespace llvm;
2098
2099 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2100 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2101 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2102 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2103 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2104
Douglas Gregorc27b2872011-08-04 00:01:48 +00002105 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002106 Record.clear();
2107 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2108 Record.push_back(CXXBaseSpecifiersOffsets.size());
2109 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002110 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002111}
2112
Douglas Gregorc5046832009-04-27 18:38:38 +00002113//===----------------------------------------------------------------------===//
2114// Type Serialization
2115//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002117/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002118void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002119 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002120 if (Idx.getIndex() == 0) // we haven't seen this type before.
2121 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002122
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002123 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002124
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002125 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002126 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002127 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002128 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002129 else if (TypeOffsets.size() < Index) {
2130 TypeOffsets.resize(Index + 1);
2131 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002132 }
2133
2134 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002135
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002136 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002137 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002138
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002139 if (T.hasLocalNonFastQualifiers()) {
2140 Qualifiers Qs = T.getLocalQualifiers();
2141 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002142 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002143 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002144 } else {
2145 switch (T->getTypeClass()) {
2146 // For all of the concrete, non-dependent types, call the
2147 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002148#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002149 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002151#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002152 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153 }
2154
2155 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002156 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002157
2158 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002159 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002160}
2161
Douglas Gregorc5046832009-04-27 18:38:38 +00002162//===----------------------------------------------------------------------===//
2163// Declaration Serialization
2164//===----------------------------------------------------------------------===//
2165
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002166/// \brief Write the block containing all of the declaration IDs
2167/// lexically declared within the given DeclContext.
2168///
2169/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2170/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002171uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002172 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002173 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002174 return 0;
2175
Douglas Gregor8f45df52009-04-16 22:23:12 +00002176 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002177 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002178 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002179 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002180 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2181 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002182 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002183
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002184 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002185 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002186 return Offset;
2187}
2188
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002189void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002190 using namespace llvm;
2191 RecordData Record;
2192
2193 // Write the type offsets array
2194 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002195 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2199 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2200 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002201 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002202 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002203 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002204 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002205
2206 // Write the declaration offsets array
2207 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002208 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2212 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2213 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002214 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002215 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002216 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002217 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002218}
2219
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002220void ASTWriter::WriteFileDeclIDsMap() {
2221 using namespace llvm;
2222 RecordData Record;
2223
2224 // Join the vectors of DeclIDs from all files.
2225 SmallVector<DeclID, 256> FileSortedIDs;
2226 for (FileDeclIDsTy::iterator
2227 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2228 DeclIDInFileInfo &Info = *FI->second;
2229 Info.FirstDeclIndex = FileSortedIDs.size();
2230 for (LocDeclIDsTy::iterator
2231 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2232 FileSortedIDs.push_back(DI->second);
2233 }
2234
2235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2236 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2238 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2239 Record.push_back(FILE_SORTED_DECLS);
2240 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2241}
2242
Douglas Gregorc5046832009-04-27 18:38:38 +00002243//===----------------------------------------------------------------------===//
2244// Global Method Pool and Selector Serialization
2245//===----------------------------------------------------------------------===//
2246
Douglas Gregore84a9da2009-04-20 20:36:09 +00002247namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002248// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002249class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002250 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002251
2252public:
2253 typedef Selector key_type;
2254 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002255
Sebastian Redl834bb972010-08-04 17:20:04 +00002256 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002257 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002258 ObjCMethodList Instance, Factory;
2259 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002260 typedef const data_type& data_type_ref;
2261
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002262 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002263
Douglas Gregorc78d3462009-04-24 21:10:55 +00002264 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002265 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002266 }
Mike Stump11289f42009-09-09 15:08:12 +00002267
2268 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002269 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002270 data_type_ref Methods) {
2271 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2272 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002273 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2274 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002275 Method = Method->Next)
2276 if (Method->Method)
2277 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002278 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002279 Method = Method->Next)
2280 if (Method->Method)
2281 DataLen += 4;
2282 clang::io::Emit16(Out, DataLen);
2283 return std::make_pair(KeyLen, DataLen);
2284 }
Mike Stump11289f42009-09-09 15:08:12 +00002285
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002286 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002287 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002288 assert((Start >> 32) == 0 && "Selector key offset too large");
2289 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002290 unsigned N = Sel.getNumArgs();
2291 clang::io::Emit16(Out, N);
2292 if (N == 0)
2293 N = 1;
2294 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002295 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002296 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2297 }
Mike Stump11289f42009-09-09 15:08:12 +00002298
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002299 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002300 data_type_ref Methods, unsigned DataLen) {
2301 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002302 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002303 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002304 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002305 Method = Method->Next)
2306 if (Method->Method)
2307 ++NumInstanceMethods;
2308
2309 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002310 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002311 Method = Method->Next)
2312 if (Method->Method)
2313 ++NumFactoryMethods;
2314
2315 clang::io::Emit16(Out, NumInstanceMethods);
2316 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002317 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002318 Method = Method->Next)
2319 if (Method->Method)
2320 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002321 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002322 Method = Method->Next)
2323 if (Method->Method)
2324 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002325
2326 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002327 }
2328};
2329} // end anonymous namespace
2330
Sebastian Redla19a67f2010-08-03 21:58:15 +00002331/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002332///
2333/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002334/// in an on-disk hash table indexed by the selector. The hash table also
2335/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002336void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002337 using namespace llvm;
2338
Sebastian Redla19a67f2010-08-03 21:58:15 +00002339 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002340 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002341 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002342 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002343 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002344 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002345 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002346 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002347
Sebastian Redla19a67f2010-08-03 21:58:15 +00002348 // Create the on-disk hash table representation. We walk through every
2349 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002350 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002351 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002352 I = SelectorIDs.begin(), E = SelectorIDs.end();
2353 I != E; ++I) {
2354 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002355 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002356 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002357 I->second,
2358 ObjCMethodList(),
2359 ObjCMethodList()
2360 };
2361 if (F != SemaRef.MethodPool.end()) {
2362 Data.Instance = F->second.first;
2363 Data.Factory = F->second.second;
2364 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002365 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002366 // changed.
2367 if (Chain && I->second < FirstSelectorID) {
2368 // Selector already exists. Did it change?
2369 bool changed = false;
2370 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2371 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002372 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002373 changed = true;
2374 }
2375 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2376 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002377 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002378 changed = true;
2379 }
2380 if (!changed)
2381 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002382 } else if (Data.Instance.Method || Data.Factory.Method) {
2383 // A new method pool entry.
2384 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002385 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002386 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002387 }
2388
Douglas Gregorc78d3462009-04-24 21:10:55 +00002389 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002390 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002391 uint32_t BucketOffset;
2392 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002393 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002394 llvm::raw_svector_ostream Out(MethodPool);
2395 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002396 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002397 BucketOffset = Generator.Emit(Out, Trait);
2398 }
2399
2400 // Create a blob abbreviation
2401 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002403 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002404 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002405 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2406 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2407
Douglas Gregor95c13f52009-04-25 17:48:32 +00002408 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002409 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002410 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002411 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002412 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002413 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002414
2415 // Create a blob abbreviation for the selector table offsets.
2416 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002417 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002419 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002420 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2421 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2422
2423 // Write the selector offsets table.
2424 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002425 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002426 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002427 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002428 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002429 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002430 }
2431}
2432
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002433/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002434void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002435 using namespace llvm;
2436 if (SemaRef.ReferencedSelectors.empty())
2437 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002438
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002439 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002440
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002441 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002442 // very tricky to fix, and given that @selector shouldn't really appear in
2443 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002444 for (DenseMap<Selector, SourceLocation>::iterator S =
2445 SemaRef.ReferencedSelectors.begin(),
2446 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2447 Selector Sel = (*S).first;
2448 SourceLocation Loc = (*S).second;
2449 AddSelectorRef(Sel, Record);
2450 AddSourceLocation(Loc, Record);
2451 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002452 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002453}
2454
Douglas Gregorc5046832009-04-27 18:38:38 +00002455//===----------------------------------------------------------------------===//
2456// Identifier Table Serialization
2457//===----------------------------------------------------------------------===//
2458
Douglas Gregorc78d3462009-04-24 21:10:55 +00002459namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002460class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002461 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002462 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002463 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002464 bool IsModule;
2465
Douglas Gregor1d583f22009-04-28 21:18:29 +00002466 /// \brief Determines whether this is an "interesting" identifier
2467 /// that needs a full IdentifierInfo structure written into the hash
2468 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002469 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002470 if (II->isPoisoned() ||
2471 II->isExtensionToken() ||
2472 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002473 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002474 II->getFETokenInfo<void>())
2475 return true;
2476
Douglas Gregord7910e92011-09-14 22:14:14 +00002477 return hasMacroDefinition(II, Macro);
2478 }
2479
2480 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002481 if (!II->hasMacroDefinition())
2482 return false;
2483
Douglas Gregord7910e92011-09-14 22:14:14 +00002484 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002485 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002486
Douglas Gregord7910e92011-09-14 22:14:14 +00002487 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002488 }
2489
Douglas Gregore84a9da2009-04-20 20:36:09 +00002490public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002491 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002492 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002493
Sebastian Redl539c5062010-08-18 23:57:32 +00002494 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002495 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002496
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002497 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2498 IdentifierResolver &IdResolver, bool IsModule)
2499 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002500
2501 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002502 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002503 }
Mike Stump11289f42009-09-09 15:08:12 +00002504
2505 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002506 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002507 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002508 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002509 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002510 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002511 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregord7910e92011-09-14 22:14:14 +00002512 if (hasMacroDefinition(II, Macro))
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002513 DataLen += 8;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002514
2515 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2516 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002517 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002518 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002519 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002520 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002521 // We emit the key length after the data length so that every
2522 // string is preceded by a 16-bit length. This matches the PTH
2523 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002524 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002525 return std::make_pair(KeyLen, DataLen);
2526 }
Mike Stump11289f42009-09-09 15:08:12 +00002527
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002528 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002529 unsigned KeyLen) {
2530 // Record the location of the key data. This is used when generating
2531 // the mapping from persistent IDs to strings.
2532 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002533 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002534 }
Mike Stump11289f42009-09-09 15:08:12 +00002535
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002536 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002537 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002538 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002539 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002540 clang::io::Emit32(Out, ID << 1);
2541 return;
2542 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002543
Douglas Gregor1d583f22009-04-28 21:18:29 +00002544 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002545 uint32_t Bits = 0;
Douglas Gregord7910e92011-09-14 22:14:14 +00002546 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregorb9256522009-04-28 21:32:13 +00002547 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topperdec792e2011-12-19 05:04:33 +00002548 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregord7910e92011-09-14 22:14:14 +00002549 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002550 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2551 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002552 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002553 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002554 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002555
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002556 if (HasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002557 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002558 clang::io::Emit32(Out,
2559 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2560 }
2561
Douglas Gregora868bbd2009-04-21 22:25:48 +00002562 // Emit the declaration IDs in reverse order, because the
2563 // IdentifierResolver provides the declarations as they would be
2564 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002565 // "stat"), but the ASTReader adds declarations to the end of the list
2566 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002567 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002568 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2569 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002570 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002571 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002572 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002573 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002574 }
2575};
2576} // end anonymous namespace
2577
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002578/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002579///
2580/// The identifier table consists of a blob containing string data
2581/// (the actual identifiers themselves) and a separate "offsets" index
2582/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002583void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2584 IdentifierResolver &IdResolver,
2585 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002586 using namespace llvm;
2587
2588 // Create and write out the blob that contains the identifier
2589 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002590 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002591 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002592 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002593
Douglas Gregore6648fb2009-04-28 20:33:11 +00002594 // Look for any identifiers that were named while processing the
2595 // headers, but are otherwise not needed. We add these to the hash
2596 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002597 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002598 // file.
2599 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2600 IDEnd = PP.getIdentifierTable().end();
2601 ID != IDEnd; ++ID)
2602 getIdentifierRef(ID->second);
2603
Sebastian Redlff4a2952010-07-23 23:49:55 +00002604 // Create the on-disk hash table representation. We only store offsets
2605 // for identifiers that appear here for the first time.
2606 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002607 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002608 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2609 ID != IDEnd; ++ID) {
2610 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002611 if (!Chain || !ID->first->isFromAST() ||
2612 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002613 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2614 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002615 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002616
Douglas Gregore84a9da2009-04-20 20:36:09 +00002617 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002618 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002619 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002620 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002621 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002622 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002623 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002624 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002625 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002626 }
2627
2628 // Create a blob abbreviation
2629 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002630 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002633 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002634
2635 // Write the identifier table
2636 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002637 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002638 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002639 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002640 }
2641
2642 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002643 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002644 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002645 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002646 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002647 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2648 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2649
2650 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002651 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002652 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002653 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002654 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002655 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002656}
2657
Douglas Gregorc5046832009-04-27 18:38:38 +00002658//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002659// DeclContext's Name Lookup Table Serialization
2660//===----------------------------------------------------------------------===//
2661
2662namespace {
2663// Trait used for the on-disk hash table used in the method pool.
2664class ASTDeclContextNameLookupTrait {
2665 ASTWriter &Writer;
2666
2667public:
2668 typedef DeclarationName key_type;
2669 typedef key_type key_type_ref;
2670
2671 typedef DeclContext::lookup_result data_type;
2672 typedef const data_type& data_type_ref;
2673
2674 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2675
2676 unsigned ComputeHash(DeclarationName Name) {
2677 llvm::FoldingSetNodeID ID;
2678 ID.AddInteger(Name.getNameKind());
2679
2680 switch (Name.getNameKind()) {
2681 case DeclarationName::Identifier:
2682 ID.AddString(Name.getAsIdentifierInfo()->getName());
2683 break;
2684 case DeclarationName::ObjCZeroArgSelector:
2685 case DeclarationName::ObjCOneArgSelector:
2686 case DeclarationName::ObjCMultiArgSelector:
2687 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2688 break;
2689 case DeclarationName::CXXConstructorName:
2690 case DeclarationName::CXXDestructorName:
2691 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002692 break;
2693 case DeclarationName::CXXOperatorName:
2694 ID.AddInteger(Name.getCXXOverloadedOperator());
2695 break;
2696 case DeclarationName::CXXLiteralOperatorName:
2697 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2698 case DeclarationName::CXXUsingDirective:
2699 break;
2700 }
2701
2702 return ID.ComputeHash();
2703 }
2704
2705 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002706 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002707 data_type_ref Lookup) {
2708 unsigned KeyLen = 1;
2709 switch (Name.getNameKind()) {
2710 case DeclarationName::Identifier:
2711 case DeclarationName::ObjCZeroArgSelector:
2712 case DeclarationName::ObjCOneArgSelector:
2713 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002714 case DeclarationName::CXXLiteralOperatorName:
2715 KeyLen += 4;
2716 break;
2717 case DeclarationName::CXXOperatorName:
2718 KeyLen += 1;
2719 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002720 case DeclarationName::CXXConstructorName:
2721 case DeclarationName::CXXDestructorName:
2722 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002723 case DeclarationName::CXXUsingDirective:
2724 break;
2725 }
2726 clang::io::Emit16(Out, KeyLen);
2727
2728 // 2 bytes for num of decls and 4 for each DeclID.
2729 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2730 clang::io::Emit16(Out, DataLen);
2731
2732 return std::make_pair(KeyLen, DataLen);
2733 }
2734
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002735 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002736 using namespace clang::io;
2737
2738 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2739 Emit8(Out, Name.getNameKind());
2740 switch (Name.getNameKind()) {
2741 case DeclarationName::Identifier:
2742 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2743 break;
2744 case DeclarationName::ObjCZeroArgSelector:
2745 case DeclarationName::ObjCOneArgSelector:
2746 case DeclarationName::ObjCMultiArgSelector:
2747 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2748 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002749 case DeclarationName::CXXOperatorName:
2750 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2751 Emit8(Out, Name.getCXXOverloadedOperator());
2752 break;
2753 case DeclarationName::CXXLiteralOperatorName:
2754 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2755 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002756 case DeclarationName::CXXConstructorName:
2757 case DeclarationName::CXXDestructorName:
2758 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002759 case DeclarationName::CXXUsingDirective:
2760 break;
2761 }
2762 }
2763
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002764 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002765 data_type Lookup, unsigned DataLen) {
2766 uint64_t Start = Out.tell(); (void)Start;
2767 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2768 for (; Lookup.first != Lookup.second; ++Lookup.first)
2769 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2770
2771 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2772 }
2773};
2774} // end anonymous namespace
2775
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002776/// \brief Write the block containing all of the declaration IDs
2777/// visible from the given DeclContext.
2778///
2779/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002780/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002781uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2782 DeclContext *DC) {
2783 if (DC->getPrimaryContext() != DC)
2784 return 0;
2785
2786 // Since there is no name lookup into functions or methods, don't bother to
2787 // build a visible-declarations table for these entities.
2788 if (DC->isFunctionOrMethod())
2789 return 0;
2790
2791 // If not in C++, we perform name lookup for the translation unit via the
2792 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2793 // FIXME: In C++ we need the visible declarations in order to "see" the
2794 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00002795 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002796 return 0;
2797
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002798 // Serialize the contents of the mapping used for lookup. Note that,
2799 // although we have two very different code paths, the serialized
2800 // representation is the same for both cases: a declaration name,
2801 // followed by a size, followed by references to the visible
2802 // declarations that have that name.
2803 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00002804 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002805 if (!Map || Map->empty())
2806 return 0;
2807
2808 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2809 ASTDeclContextNameLookupTrait Trait(*this);
2810
2811 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002812 DeclarationName ConversionName;
2813 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002814 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2815 D != DEnd; ++D) {
2816 DeclarationName Name = D->first;
2817 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002818 if (Result.first != Result.second) {
2819 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2820 // Hash all conversion function names to the same name. The actual
2821 // type information in conversion function name is not used in the
2822 // key (since such type information is not stable across different
2823 // modules), so the intended effect is to coalesce all of the conversion
2824 // functions under a single key.
2825 if (!ConversionName)
2826 ConversionName = Name;
2827 ConversionDecls.append(Result.first, Result.second);
2828 continue;
2829 }
2830
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002831 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002832 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002833 }
2834
Douglas Gregor05ef9312011-08-30 20:49:19 +00002835 // Add the conversion functions
2836 if (!ConversionDecls.empty()) {
2837 Generator.insert(ConversionName,
2838 DeclContext::lookup_result(ConversionDecls.begin(),
2839 ConversionDecls.end()),
2840 Trait);
2841 }
2842
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002843 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002844 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002845 uint32_t BucketOffset;
2846 {
2847 llvm::raw_svector_ostream Out(LookupTable);
2848 // Make sure that no bucket is at offset 0
2849 clang::io::Emit32(Out, 0);
2850 BucketOffset = Generator.Emit(Out, Trait);
2851 }
2852
2853 // Write the lookup table
2854 RecordData Record;
2855 Record.push_back(DECL_CONTEXT_VISIBLE);
2856 Record.push_back(BucketOffset);
2857 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2858 LookupTable.str());
2859
2860 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2861 ++NumVisibleDeclContexts;
2862 return Offset;
2863}
2864
Sebastian Redla4071b42010-08-24 00:50:09 +00002865/// \brief Write an UPDATE_VISIBLE block for the given context.
2866///
2867/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2868/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00002869/// (in C++), for namespaces, and for classes with forward-declared unscoped
2870/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00002871void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002872 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2873 if (!Map || Map->empty())
2874 return;
2875
2876 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2877 ASTDeclContextNameLookupTrait Trait(*this);
2878
2879 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002880 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2881 D != DEnd; ++D) {
2882 DeclarationName Name = D->first;
2883 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002884 // For any name that appears in this table, the results are complete, i.e.
2885 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002886 if (Result.first != Result.second)
2887 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002888 }
2889
2890 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002891 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00002892 uint32_t BucketOffset;
2893 {
2894 llvm::raw_svector_ostream Out(LookupTable);
2895 // Make sure that no bucket is at offset 0
2896 clang::io::Emit32(Out, 0);
2897 BucketOffset = Generator.Emit(Out, Trait);
2898 }
2899
2900 // Write the lookup table
2901 RecordData Record;
2902 Record.push_back(UPDATE_VISIBLE);
2903 Record.push_back(getDeclID(cast<Decl>(DC)));
2904 Record.push_back(BucketOffset);
2905 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2906}
2907
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002908/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2909void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2910 RecordData Record;
2911 Record.push_back(Opts.fp_contract);
2912 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2913}
2914
2915/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2916void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002917 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002918 return;
2919
2920 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2921 RecordData Record;
2922#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2923#include "clang/Basic/OpenCLExtensions.def"
2924 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2925}
2926
Douglas Gregor358cd442012-01-15 16:58:34 +00002927void ASTWriter::WriteRedeclarations() {
2928 RecordData LocalRedeclChains;
2929 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2930
2931 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2932 Decl *First = Redeclarations[I];
2933 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2934
2935 Decl *MostRecent = First->getMostRecentDecl();
2936
2937 // If we only have a single declaration, there is no point in storing
2938 // a redeclaration chain.
2939 if (First == MostRecent)
2940 continue;
2941
2942 unsigned Offset = LocalRedeclChains.size();
2943 unsigned Size = 0;
2944 LocalRedeclChains.push_back(0); // Placeholder for the size.
2945
2946 // Collect the set of local redeclarations of this declaration.
2947 for (Decl *Prev = MostRecent; Prev != First;
2948 Prev = Prev->getPreviousDecl()) {
2949 if (!Prev->isFromASTFile()) {
2950 AddDeclRef(Prev, LocalRedeclChains);
2951 ++Size;
2952 }
2953 }
2954 LocalRedeclChains[Offset] = Size;
2955
2956 // Reverse the set of local redeclarations, so that we store them in
2957 // order (since we found them in reverse order).
2958 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2959
2960 // Add the mapping from the first ID to the set of local declarations.
2961 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2962 LocalRedeclsMap.push_back(Info);
2963
2964 assert(N == Redeclarations.size() &&
2965 "Deserialized a declaration we shouldn't have");
2966 }
2967
2968 if (LocalRedeclChains.empty())
2969 return;
2970
2971 // Sort the local redeclarations map by the first declaration ID,
2972 // since the reader will be performing binary searches on this information.
2973 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2974
2975 // Emit the local redeclarations map.
2976 using namespace llvm;
2977 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2978 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2981 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2982
2983 RecordData Record;
2984 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2985 Record.push_back(LocalRedeclsMap.size());
2986 Stream.EmitRecordWithBlob(AbbrevID, Record,
2987 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2988 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2989
2990 // Emit the redeclaration chains.
2991 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2992}
2993
Douglas Gregor404cdde2012-01-27 01:47:08 +00002994void ASTWriter::WriteObjCCategories() {
2995 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2996 RecordData Categories;
2997
2998 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2999 unsigned Size = 0;
3000 unsigned StartIndex = Categories.size();
3001
3002 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3003
3004 // Allocate space for the size.
3005 Categories.push_back(0);
3006
3007 // Add the categories.
3008 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3009 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3010 assert(getDeclID(Cat) != 0 && "Bogus category");
3011 AddDeclRef(Cat, Categories);
3012 }
3013
3014 // Update the size.
3015 Categories[StartIndex] = Size;
3016
3017 // Record this interface -> category map.
3018 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3019 CategoriesMap.push_back(CatInfo);
3020 }
3021
3022 // Sort the categories map by the definition ID, since the reader will be
3023 // performing binary searches on this information.
3024 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3025
3026 // Emit the categories map.
3027 using namespace llvm;
3028 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3029 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3032 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3033
3034 RecordData Record;
3035 Record.push_back(OBJC_CATEGORIES_MAP);
3036 Record.push_back(CategoriesMap.size());
3037 Stream.EmitRecordWithBlob(AbbrevID, Record,
3038 reinterpret_cast<char*>(CategoriesMap.data()),
3039 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3040
3041 // Emit the category lists.
3042 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3043}
3044
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003045void ASTWriter::WriteMergedDecls() {
3046 if (!Chain || Chain->MergedDecls.empty())
3047 return;
3048
3049 RecordData Record;
3050 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3051 IEnd = Chain->MergedDecls.end();
3052 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003053 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003054 : getDeclID(I->first);
3055 assert(CanonID && "Merged declaration not known?");
3056
3057 Record.push_back(CanonID);
3058 Record.push_back(I->second.size());
3059 Record.append(I->second.begin(), I->second.end());
3060 }
3061 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3062}
3063
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003064//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003065// General Serialization Routines
3066//===----------------------------------------------------------------------===//
3067
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003068/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003069void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003070 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003071 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3072 const Attr * A = *i;
3073 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003074 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003075
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003076#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003077
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003078 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003079}
3080
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003081void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003082 Record.push_back(Str.size());
3083 Record.insert(Record.end(), Str.begin(), Str.end());
3084}
3085
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003086void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3087 RecordDataImpl &Record) {
3088 Record.push_back(Version.getMajor());
3089 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3090 Record.push_back(*Minor + 1);
3091 else
3092 Record.push_back(0);
3093 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3094 Record.push_back(*Subminor + 1);
3095 else
3096 Record.push_back(0);
3097}
3098
Douglas Gregore84a9da2009-04-20 20:36:09 +00003099/// \brief Note that the identifier II occurs at the given offset
3100/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003101void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003102 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003103 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003104 // up earlier in the chain and thus don't need an offset.
3105 if (ID >= FirstIdentID)
3106 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003107}
3108
Douglas Gregor95c13f52009-04-25 17:48:32 +00003109/// \brief Note that the selector Sel occurs at the given offset
3110/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003111void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003112 unsigned ID = SelectorIDs[Sel];
3113 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003114 // Don't record offsets for selectors that are also available in a different
3115 // file.
3116 if (ID < FirstSelectorID)
3117 return;
3118 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003119}
3120
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003121ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003122 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003123 WritingAST(false), ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003124 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003125 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003126 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003127 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3128 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003129 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003130 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003131 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003132 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003133 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003134 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003135 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3136 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3137 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003138 DeclTypedefAbbrev(0),
3139 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3140 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003141{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003142}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003143
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003144ASTWriter::~ASTWriter() {
3145 for (FileDeclIDsTy::iterator
3146 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3147 delete I->second;
3148}
3149
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003150void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003151 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003152 Module *WritingModule, StringRef isysroot,
3153 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003154 WritingAST = true;
3155
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003156 ASTHasCompilerErrors = hasErrors;
3157
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003158 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003159 Stream.Emit((unsigned)'C', 8);
3160 Stream.Emit((unsigned)'P', 8);
3161 Stream.Emit((unsigned)'C', 8);
3162 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003163
Chris Lattner28fa4e62009-04-26 22:26:21 +00003164 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003165
Douglas Gregoreda8e122011-08-09 15:13:55 +00003166 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003167 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003168 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003169 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003170 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003171 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003172 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003173
3174 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003175}
3176
Douglas Gregora94a1542011-07-27 21:45:57 +00003177template<typename Vector>
3178static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3179 ASTWriter::RecordData &Record) {
3180 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3181 I != E; ++I) {
3182 Writer.AddDeclRef(*I, Record);
3183 }
3184}
3185
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003186void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003187 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003188 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003189 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003190 using namespace llvm;
3191
Douglas Gregorcf68c582011-12-01 22:20:10 +00003192 // Make sure that the AST reader knows to finalize itself.
3193 if (Chain)
3194 Chain->finalizeForWriting();
3195
Sebastian Redl143413f2010-07-12 22:02:52 +00003196 ASTContext &Context = SemaRef.Context;
3197 Preprocessor &PP = SemaRef.PP;
3198
Douglas Gregordab42432011-08-12 00:15:20 +00003199 // Set up predefined declaration IDs.
3200 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003201 if (Context.ObjCIdDecl)
3202 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003203 if (Context.ObjCSelDecl)
3204 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003205 if (Context.ObjCClassDecl)
3206 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003207 if (Context.ObjCProtocolClassDecl)
3208 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003209 if (Context.Int128Decl)
3210 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3211 if (Context.UInt128Decl)
3212 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003213 if (Context.ObjCInstanceTypeDecl)
3214 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003215 if (Context.BuiltinVaListDecl)
3216 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3217
Douglas Gregor851443c2011-08-12 01:39:19 +00003218 if (!Chain) {
3219 // Make sure that we emit IdentifierInfos (and any attached
3220 // declarations) for builtins. We don't need to do this when we're
3221 // emitting chained PCH files, because all of the builtins will be
3222 // in the original PCH file.
3223 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003224 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003225 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003226 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003227 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003228 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3229 getIdentifierRef(&Table.get(BuiltinNames[I]));
3230 }
3231
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003232 // If there are any out-of-date identifiers, bring them up to date.
3233 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3234 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3235 IDEnd = PP.getIdentifierTable().end();
3236 ID != IDEnd; ++ID)
3237 if (ID->second->isOutOfDate())
3238 ExtSource->updateOutOfDateIdentifier(*ID->second);
3239 }
3240
Chris Lattner0c797362009-09-08 18:19:27 +00003241 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003242 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003243 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003244 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003245 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003246
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003247 // Build a record containing all of the file scoped decls in this file.
3248 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003249 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3250 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003251
Douglas Gregor851443c2011-08-12 01:39:19 +00003252 // Build a record containing all of the delegating constructors we still need
3253 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003254 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003255 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003256
Douglas Gregor851443c2011-08-12 01:39:19 +00003257 // Write the set of weak, undeclared identifiers. We always write the
3258 // entire table, since later PCH files in a PCH chain are only interested in
3259 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003260 RecordData WeakUndeclaredIdentifiers;
3261 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003262 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003263 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3264 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3265 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3266 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3267 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3268 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3269 }
3270 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003271
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003272 // Build a record containing all of the locally-scoped external
3273 // declarations in this header file. Generally, this record will be
3274 // empty.
3275 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003276 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003277 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003278 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003279 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3280 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003281 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003282 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003283 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3284 }
3285
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003286 // Build a record containing all of the ext_vector declarations.
3287 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003288 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003289
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003290 // Build a record containing all of the VTable uses information.
3291 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003292 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003293 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3294 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3295 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3296 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3297 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003298 }
3299
3300 // Build a record containing all of dynamic classes declarations.
3301 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003302 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003303
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003304 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003305 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003306 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003307 I = SemaRef.PendingInstantiations.begin(),
3308 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3309 AddDeclRef(I->first, PendingInstantiations);
3310 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003311 }
3312 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3313 "There are local ones at end of translation unit!");
3314
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003315 // Build a record containing some declaration references.
3316 RecordData SemaDeclRefs;
3317 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3318 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3319 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3320 }
3321
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003322 RecordData CUDASpecialDeclRefs;
3323 if (Context.getcudaConfigureCallDecl()) {
3324 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3325 }
3326
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003327 // Build a record containing all of the known namespaces.
3328 RecordData KnownNamespaces;
3329 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3330 I = SemaRef.KnownNamespaces.begin(),
3331 IEnd = SemaRef.KnownNamespaces.end();
3332 I != IEnd; ++I) {
3333 if (!I->second)
3334 AddDeclRef(I->first, KnownNamespaces);
3335 }
3336
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003337 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003338 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003339 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003340 WriteMetadata(Context, isysroot, OutputFile);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003341 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregorc567ba22011-07-22 16:35:34 +00003342 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003343 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003344
3345 // Create a lexical update block containing all of the declarations in the
3346 // translation unit that do not come from other AST files.
3347 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3348 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3349 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3350 E = TU->noload_decls_end();
3351 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003352 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003353 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003354 }
3355
3356 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3357 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3358 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3359 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3360 Record.clear();
3361 Record.push_back(TU_UPDATE_LEXICAL);
3362 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3363 data(NewGlobalDecls));
3364
3365 // And a visible updates block for the translation unit.
3366 Abv = new llvm::BitCodeAbbrev();
3367 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3368 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3369 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3370 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3371 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3372 WriteDeclContextVisibleUpdate(TU);
3373
3374 // If the translation unit has an anonymous namespace, and we don't already
3375 // have an update block for it, write it as an update block.
3376 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3377 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3378 if (Record.empty()) {
3379 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003380 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003381 }
3382 }
3383
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003384 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003385 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003386
Douglas Gregor5204bde2011-08-02 16:26:37 +00003387 // Form the record of special types.
3388 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003389 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003390 AddTypeRef(Context.getFILEType(), SpecialTypes);
3391 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3392 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3393 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3394 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003395 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003396 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003397
Douglas Gregor1970d882009-04-26 03:49:13 +00003398 // Keep writing types and declarations until all types and
3399 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003400 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003401 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003402 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3403 E = DeclsToRewrite.end();
3404 I != E; ++I)
3405 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003406 while (!DeclTypesToEmit.empty()) {
3407 DeclOrType DOT = DeclTypesToEmit.front();
3408 DeclTypesToEmit.pop();
3409 if (DOT.isType())
3410 WriteType(DOT.getType());
3411 else
3412 WriteDecl(Context, DOT.getDecl());
3413 }
3414 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003415
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003416 WriteFileDeclIDsMap();
3417 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3418
3419 if (Chain) {
3420 // Write the mapping information describing our module dependencies and how
3421 // each of those modules were mapped into our own offset/ID space, so that
3422 // the reader can build the appropriate mapping to its own offset/ID space.
3423 // The map consists solely of a blob with the following format:
3424 // *(module-name-len:i16 module-name:len*i8
3425 // source-location-offset:i32
3426 // identifier-id:i32
3427 // preprocessed-entity-id:i32
3428 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003429 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003430 // selector-id:i32
3431 // declaration-id:i32
3432 // c++-base-specifiers-id:i32
3433 // type-id:i32)
3434 //
3435 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3436 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3438 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003439 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003440 {
3441 llvm::raw_svector_ostream Out(Buffer);
3442 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003443 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003444 M != MEnd; ++M) {
3445 StringRef FileName = (*M)->FileName;
3446 io::Emit16(Out, FileName.size());
3447 Out.write(FileName.data(), FileName.size());
3448 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3449 io::Emit32(Out, (*M)->BaseIdentifierID);
3450 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003451 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003452 io::Emit32(Out, (*M)->BaseSelectorID);
3453 io::Emit32(Out, (*M)->BaseDeclID);
3454 io::Emit32(Out, (*M)->BaseTypeIndex);
3455 }
3456 }
3457 Record.clear();
3458 Record.push_back(MODULE_OFFSET_MAP);
3459 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3460 Buffer.data(), Buffer.size());
3461 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003462 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003463 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003464 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003465 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003466 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003467 WriteFPPragmaOptions(SemaRef.getFPOptions());
3468 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003469
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003470 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003471 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003472
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003473 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003474
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003475 // If we're emitting a module, write out the submodule information.
3476 if (WritingModule)
3477 WriteSubmodules(WritingModule);
3478
Douglas Gregor5204bde2011-08-02 16:26:37 +00003479 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3480
Douglas Gregord4df8652009-04-22 22:02:47 +00003481 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003482 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003483 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003484
3485 // Write the record containing tentative definitions.
3486 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003487 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003488
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003489 // Write the record containing unused file scoped decls.
3490 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003491 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003492
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003493 // Write the record containing weak undeclared identifiers.
3494 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003495 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003496 WeakUndeclaredIdentifiers);
3497
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003498 // Write the record containing locally-scoped external definitions.
3499 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003500 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003501 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003502
3503 // Write the record containing ext_vector type names.
3504 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003505 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003506
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003507 // Write the record containing VTable uses information.
3508 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003509 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003510
3511 // Write the record containing dynamic classes declarations.
3512 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003513 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003514
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003515 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003516 if (!PendingInstantiations.empty())
3517 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003518
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003519 // Write the record containing declaration references of Sema.
3520 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003521 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003522
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003523 // Write the record containing CUDA-specific declaration references.
3524 if (!CUDASpecialDeclRefs.empty())
3525 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003526
3527 // Write the delegating constructors.
3528 if (!DelegatingCtorDecls.empty())
3529 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003530
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003531 // Write the known namespaces.
3532 if (!KnownNamespaces.empty())
3533 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3534
Douglas Gregor851443c2011-08-12 01:39:19 +00003535 // Write the visible updates to DeclContexts.
3536 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3537 I = UpdatedDeclContexts.begin(),
3538 E = UpdatedDeclContexts.end();
3539 I != E; ++I)
3540 WriteDeclContextVisibleUpdate(*I);
3541
Douglas Gregor959bb062011-12-03 01:15:29 +00003542 if (!WritingModule) {
3543 // Write the submodules that were imported, if any.
3544 RecordData ImportedModules;
3545 for (ASTContext::import_iterator I = Context.local_import_begin(),
3546 IEnd = Context.local_import_end();
3547 I != IEnd; ++I) {
3548 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3549 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3550 }
3551 if (!ImportedModules.empty()) {
3552 // Sort module IDs.
3553 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3554
3555 // Unique module IDs.
3556 ImportedModules.erase(std::unique(ImportedModules.begin(),
3557 ImportedModules.end()),
3558 ImportedModules.end());
3559
3560 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3561 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003562 }
3563
Douglas Gregordab42432011-08-12 00:15:20 +00003564 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003565 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003566 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003567 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003568 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003569
Douglas Gregor08f01292009-04-17 22:13:46 +00003570 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003571 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003572 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003573 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003574 Record.push_back(NumLexicalDeclContexts);
3575 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003576 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003577 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003578}
3579
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003580/// \brief Go through the declaration update blocks and resolve declaration
3581/// pointers into declaration IDs.
3582void ASTWriter::ResolveDeclUpdatesBlocks() {
3583 for (DeclUpdateMap::iterator
3584 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3585 const Decl *D = I->first;
3586 UpdateRecord &URec = I->second;
3587
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003588 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003589 continue; // The decl will be written completely
3590
3591 unsigned Idx = 0, N = URec.size();
3592 while (Idx < N) {
3593 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003594 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3595 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3596 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3597 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3598 ++Idx;
3599 break;
3600
3601 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3602 ++Idx;
3603 break;
3604 }
3605 }
3606 }
3607}
3608
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003609void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003610 if (DeclUpdates.empty())
3611 return;
3612
3613 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003614 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003615 for (DeclUpdateMap::iterator
3616 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3617 const Decl *D = I->first;
3618 UpdateRecord &URec = I->second;
3619
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003620 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003621 continue; // The decl will be written completely,no need to store updates.
3622
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003623 uint64_t Offset = Stream.GetCurrentBitNo();
3624 Stream.EmitRecord(DECL_UPDATES, URec);
3625
3626 OffsetsRecord.push_back(GetDeclRef(D));
3627 OffsetsRecord.push_back(Offset);
3628 }
3629 Stream.ExitBlock();
3630 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3631}
3632
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003633void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003634 if (ReplacedDecls.empty())
3635 return;
3636
3637 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003638 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003639 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003640 Record.push_back(I->ID);
3641 Record.push_back(I->Offset);
3642 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003643 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003644 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003645}
3646
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003647void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003648 Record.push_back(Loc.getRawEncoding());
3649}
3650
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003651void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003652 AddSourceLocation(Range.getBegin(), Record);
3653 AddSourceLocation(Range.getEnd(), Record);
3654}
3655
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003656void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003657 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003658 const uint64_t *Words = Value.getRawData();
3659 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003660}
3661
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003662void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003663 Record.push_back(Value.isUnsigned());
3664 AddAPInt(Value, Record);
3665}
3666
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003667void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003668 AddAPInt(Value.bitcastToAPInt(), Record);
3669}
3670
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003671void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003672 Record.push_back(getIdentifierRef(II));
3673}
3674
Sebastian Redl539c5062010-08-18 23:57:32 +00003675IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003676 if (II == 0)
3677 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003678
Sebastian Redl539c5062010-08-18 23:57:32 +00003679 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003680 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003681 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003682 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003683}
3684
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003685void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003686 Record.push_back(getSelectorRef(SelRef));
3687}
3688
Sebastian Redl539c5062010-08-18 23:57:32 +00003689SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003690 if (Sel.getAsOpaquePtr() == 0) {
3691 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003692 }
3693
Sebastian Redl539c5062010-08-18 23:57:32 +00003694 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003695 if (SID == 0 && Chain) {
3696 // This might trigger a ReadSelector callback, which will set the ID for
3697 // this selector.
3698 Chain->LoadSelector(Sel);
3699 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003700 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003701 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003702 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003703 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003704}
3705
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003706void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003707 AddDeclRef(Temp->getDestructor(), Record);
3708}
3709
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003710void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3711 CXXBaseSpecifier const *BasesEnd,
3712 RecordDataImpl &Record) {
3713 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3714 CXXBaseSpecifiersToWrite.push_back(
3715 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3716 Bases, BasesEnd));
3717 Record.push_back(NextCXXBaseSpecifiersID++);
3718}
3719
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003720void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003721 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003722 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003723 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003724 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003725 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003726 break;
3727 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003728 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003729 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003730 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003731 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003732 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003733 break;
3734 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003735 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003736 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003737 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003738 break;
John McCall0ad16662009-10-29 08:12:44 +00003739 case TemplateArgument::Null:
3740 case TemplateArgument::Integral:
3741 case TemplateArgument::Declaration:
3742 case TemplateArgument::Pack:
3743 break;
3744 }
3745}
3746
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003747void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003748 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003749 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003750
3751 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3752 bool InfoHasSameExpr
3753 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3754 Record.push_back(InfoHasSameExpr);
3755 if (InfoHasSameExpr)
3756 return; // Avoid storing the same expr twice.
3757 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003758 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3759 Record);
3760}
3761
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003762void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3763 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003764 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003765 AddTypeRef(QualType(), Record);
3766 return;
3767 }
3768
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003769 AddTypeLoc(TInfo->getTypeLoc(), Record);
3770}
3771
3772void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3773 AddTypeRef(TL.getType(), Record);
3774
John McCall8f115c62009-10-16 21:56:05 +00003775 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003776 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003777 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003778}
3779
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003780void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003781 Record.push_back(GetOrCreateTypeID(T));
3782}
3783
Douglas Gregoreda8e122011-08-09 15:13:55 +00003784TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3785 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003786 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3787}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003788
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003789TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003790 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003791 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003792}
3793
3794TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3795 if (T.isNull())
3796 return TypeIdx();
3797 assert(!T.getLocalFastQualifiers());
3798
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003799 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003800 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003801 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003802 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003803 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003804 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003805 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003806 return Idx;
3807}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003808
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003809TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003810 if (T.isNull())
3811 return TypeIdx();
3812 assert(!T.getLocalFastQualifiers());
3813
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003814 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3815 assert(I != TypeIdxs.end() && "Type not emitted!");
3816 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003817}
3818
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003819void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003820 Record.push_back(GetDeclRef(D));
3821}
3822
Sebastian Redl539c5062010-08-18 23:57:32 +00003823DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003824 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3825
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003826 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003827 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003828 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00003829
3830 // If D comes from an AST file, its declaration ID is already known and
3831 // fixed.
3832 if (D->isFromASTFile())
3833 return D->getGlobalID();
3834
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003835 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003836 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003837 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003838 // We haven't seen this declaration before. Give it a new ID and
3839 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003840 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003841 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003842 }
3843
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003844 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003845}
3846
Sebastian Redl539c5062010-08-18 23:57:32 +00003847DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003848 if (D == 0)
3849 return 0;
3850
Douglas Gregorb3163e52012-01-05 22:33:30 +00003851 // If D comes from an AST file, its declaration ID is already known and
3852 // fixed.
3853 if (D->isFromASTFile())
3854 return D->getGlobalID();
3855
Douglas Gregore84a9da2009-04-20 20:36:09 +00003856 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3857 return DeclIDs[D];
3858}
3859
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003860static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3861 std::pair<unsigned, serialization::DeclID> R) {
3862 return L.first < R.first;
3863}
3864
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003865void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003866 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003867 assert(D);
3868
3869 SourceLocation Loc = D->getLocation();
3870 if (Loc.isInvalid())
3871 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003872
3873 // We only keep track of the file-level declarations of each file.
3874 if (!D->getLexicalDeclContext()->isFileContext())
3875 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00003876 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3877 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00003878 if (isa<ParmVarDecl>(D))
3879 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003880
3881 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003882 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003883 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003884 FileID FID;
3885 unsigned Offset;
3886 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003887 if (FID.isInvalid())
3888 return;
3889 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3890 assert(Entry->isFile());
3891
3892 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3893 if (!Info)
3894 Info = new DeclIDInFileInfo();
3895
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003896 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003897 LocDeclIDsTy &Decls = Info->DeclIDs;
3898
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003899 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003900 Decls.push_back(LocDecl);
3901 return;
3902 }
3903
3904 LocDeclIDsTy::iterator
3905 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3906
3907 Decls.insert(I, LocDecl);
3908}
3909
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003910void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003911 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003912 Record.push_back(Name.getNameKind());
3913 switch (Name.getNameKind()) {
3914 case DeclarationName::Identifier:
3915 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3916 break;
3917
3918 case DeclarationName::ObjCZeroArgSelector:
3919 case DeclarationName::ObjCOneArgSelector:
3920 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003921 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003922 break;
3923
3924 case DeclarationName::CXXConstructorName:
3925 case DeclarationName::CXXDestructorName:
3926 case DeclarationName::CXXConversionFunctionName:
3927 AddTypeRef(Name.getCXXNameType(), Record);
3928 break;
3929
3930 case DeclarationName::CXXOperatorName:
3931 Record.push_back(Name.getCXXOverloadedOperator());
3932 break;
3933
Alexis Hunt3d221f22009-11-29 07:34:05 +00003934 case DeclarationName::CXXLiteralOperatorName:
3935 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3936 break;
3937
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003938 case DeclarationName::CXXUsingDirective:
3939 // No extra data to emit
3940 break;
3941 }
3942}
Chris Lattnerca025db2010-05-07 21:43:38 +00003943
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003944void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003945 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003946 switch (Name.getNameKind()) {
3947 case DeclarationName::CXXConstructorName:
3948 case DeclarationName::CXXDestructorName:
3949 case DeclarationName::CXXConversionFunctionName:
3950 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3951 break;
3952
3953 case DeclarationName::CXXOperatorName:
3954 AddSourceLocation(
3955 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3956 Record);
3957 AddSourceLocation(
3958 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3959 Record);
3960 break;
3961
3962 case DeclarationName::CXXLiteralOperatorName:
3963 AddSourceLocation(
3964 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3965 Record);
3966 break;
3967
3968 case DeclarationName::Identifier:
3969 case DeclarationName::ObjCZeroArgSelector:
3970 case DeclarationName::ObjCOneArgSelector:
3971 case DeclarationName::ObjCMultiArgSelector:
3972 case DeclarationName::CXXUsingDirective:
3973 break;
3974 }
3975}
3976
3977void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003978 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003979 AddDeclarationName(NameInfo.getName(), Record);
3980 AddSourceLocation(NameInfo.getLoc(), Record);
3981 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3982}
3983
3984void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003985 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003986 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003987 Record.push_back(Info.NumTemplParamLists);
3988 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3989 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3990}
3991
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003992void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003993 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003994 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003995 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003996 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00003997
3998 // Push each of the NNS's onto a stack for serialization in reverse order.
3999 while (NNS) {
4000 NestedNames.push_back(NNS);
4001 NNS = NNS->getPrefix();
4002 }
4003
4004 Record.push_back(NestedNames.size());
4005 while(!NestedNames.empty()) {
4006 NNS = NestedNames.pop_back_val();
4007 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4008 Record.push_back(Kind);
4009 switch (Kind) {
4010 case NestedNameSpecifier::Identifier:
4011 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4012 break;
4013
4014 case NestedNameSpecifier::Namespace:
4015 AddDeclRef(NNS->getAsNamespace(), Record);
4016 break;
4017
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004018 case NestedNameSpecifier::NamespaceAlias:
4019 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4020 break;
4021
Chris Lattnerca025db2010-05-07 21:43:38 +00004022 case NestedNameSpecifier::TypeSpec:
4023 case NestedNameSpecifier::TypeSpecWithTemplate:
4024 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4025 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4026 break;
4027
4028 case NestedNameSpecifier::Global:
4029 // Don't need to write an associated value.
4030 break;
4031 }
4032 }
4033}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004034
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004035void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4036 RecordDataImpl &Record) {
4037 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004038 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004039 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004040
4041 // Push each of the nested-name-specifiers's onto a stack for
4042 // serialization in reverse order.
4043 while (NNS) {
4044 NestedNames.push_back(NNS);
4045 NNS = NNS.getPrefix();
4046 }
4047
4048 Record.push_back(NestedNames.size());
4049 while(!NestedNames.empty()) {
4050 NNS = NestedNames.pop_back_val();
4051 NestedNameSpecifier::SpecifierKind Kind
4052 = NNS.getNestedNameSpecifier()->getKind();
4053 Record.push_back(Kind);
4054 switch (Kind) {
4055 case NestedNameSpecifier::Identifier:
4056 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4057 AddSourceRange(NNS.getLocalSourceRange(), Record);
4058 break;
4059
4060 case NestedNameSpecifier::Namespace:
4061 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4062 AddSourceRange(NNS.getLocalSourceRange(), Record);
4063 break;
4064
4065 case NestedNameSpecifier::NamespaceAlias:
4066 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4067 AddSourceRange(NNS.getLocalSourceRange(), Record);
4068 break;
4069
4070 case NestedNameSpecifier::TypeSpec:
4071 case NestedNameSpecifier::TypeSpecWithTemplate:
4072 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4073 AddTypeLoc(NNS.getTypeLoc(), Record);
4074 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4075 break;
4076
4077 case NestedNameSpecifier::Global:
4078 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4079 break;
4080 }
4081 }
4082}
4083
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004084void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004085 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004086 Record.push_back(Kind);
4087 switch (Kind) {
4088 case TemplateName::Template:
4089 AddDeclRef(Name.getAsTemplateDecl(), Record);
4090 break;
4091
4092 case TemplateName::OverloadedTemplate: {
4093 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4094 Record.push_back(OvT->size());
4095 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4096 I != E; ++I)
4097 AddDeclRef(*I, Record);
4098 break;
4099 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004100
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004101 case TemplateName::QualifiedTemplate: {
4102 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4103 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4104 Record.push_back(QualT->hasTemplateKeyword());
4105 AddDeclRef(QualT->getTemplateDecl(), Record);
4106 break;
4107 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004108
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004109 case TemplateName::DependentTemplate: {
4110 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4111 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4112 Record.push_back(DepT->isIdentifier());
4113 if (DepT->isIdentifier())
4114 AddIdentifierRef(DepT->getIdentifier(), Record);
4115 else
4116 Record.push_back(DepT->getOperator());
4117 break;
4118 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004119
4120 case TemplateName::SubstTemplateTemplateParm: {
4121 SubstTemplateTemplateParmStorage *subst
4122 = Name.getAsSubstTemplateTemplateParm();
4123 AddDeclRef(subst->getParameter(), Record);
4124 AddTemplateName(subst->getReplacement(), Record);
4125 break;
4126 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004127
4128 case TemplateName::SubstTemplateTemplateParmPack: {
4129 SubstTemplateTemplateParmPackStorage *SubstPack
4130 = Name.getAsSubstTemplateTemplateParmPack();
4131 AddDeclRef(SubstPack->getParameterPack(), Record);
4132 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4133 break;
4134 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004135 }
4136}
4137
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004138void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004139 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004140 Record.push_back(Arg.getKind());
4141 switch (Arg.getKind()) {
4142 case TemplateArgument::Null:
4143 break;
4144 case TemplateArgument::Type:
4145 AddTypeRef(Arg.getAsType(), Record);
4146 break;
4147 case TemplateArgument::Declaration:
4148 AddDeclRef(Arg.getAsDecl(), Record);
4149 break;
4150 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004151 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004152 AddTypeRef(Arg.getIntegralType(), Record);
4153 break;
4154 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004155 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4156 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004157 case TemplateArgument::TemplateExpansion:
4158 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004159 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4160 Record.push_back(*NumExpansions + 1);
4161 else
4162 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004163 break;
4164 case TemplateArgument::Expression:
4165 AddStmt(Arg.getAsExpr());
4166 break;
4167 case TemplateArgument::Pack:
4168 Record.push_back(Arg.pack_size());
4169 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4170 I != E; ++I)
4171 AddTemplateArgument(*I, Record);
4172 break;
4173 }
4174}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004175
4176void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004177ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004178 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004179 assert(TemplateParams && "No TemplateParams!");
4180 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4181 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4182 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4183 Record.push_back(TemplateParams->size());
4184 for (TemplateParameterList::const_iterator
4185 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4186 P != PEnd; ++P)
4187 AddDeclRef(*P, Record);
4188}
4189
4190/// \brief Emit a template argument list.
4191void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004192ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004193 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004194 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004195 Record.push_back(TemplateArgs->size());
4196 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004197 AddTemplateArgument(TemplateArgs->get(i), Record);
4198}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004199
4200
4201void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004202ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004203 Record.push_back(Set.size());
4204 for (UnresolvedSetImpl::const_iterator
4205 I = Set.begin(), E = Set.end(); I != E; ++I) {
4206 AddDeclRef(I.getDecl(), Record);
4207 Record.push_back(I.getAccess());
4208 }
4209}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004210
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004211void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004212 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004213 Record.push_back(Base.isVirtual());
4214 Record.push_back(Base.isBaseOfClass());
4215 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004216 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004217 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004218 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004219 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4220 : SourceLocation(),
4221 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004222}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004223
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004224void ASTWriter::FlushCXXBaseSpecifiers() {
4225 RecordData Record;
4226 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4227 Record.clear();
4228
4229 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004230 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004231 if (Index == CXXBaseSpecifiersOffsets.size())
4232 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4233 else {
4234 if (Index > CXXBaseSpecifiersOffsets.size())
4235 CXXBaseSpecifiersOffsets.resize(Index + 1);
4236 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4237 }
4238
4239 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4240 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4241 Record.push_back(BEnd - B);
4242 for (; B != BEnd; ++B)
4243 AddCXXBaseSpecifier(*B, Record);
4244 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004245
4246 // Flush any expressions that were written as part of the base specifiers.
4247 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004248 }
4249
4250 CXXBaseSpecifiersToWrite.clear();
4251}
4252
Alexis Hunt1d792652011-01-08 20:30:50 +00004253void ASTWriter::AddCXXCtorInitializers(
4254 const CXXCtorInitializer * const *CtorInitializers,
4255 unsigned NumCtorInitializers,
4256 RecordDataImpl &Record) {
4257 Record.push_back(NumCtorInitializers);
4258 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4259 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004260
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004261 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004262 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004263 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004264 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004265 } else if (Init->isDelegatingInitializer()) {
4266 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004267 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004268 } else if (Init->isMemberInitializer()){
4269 Record.push_back(CTOR_INITIALIZER_MEMBER);
4270 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004271 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004272 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4273 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004274 }
Francois Pichetd583da02010-12-04 09:14:42 +00004275
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004276 AddSourceLocation(Init->getMemberLocation(), Record);
4277 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004278 AddSourceLocation(Init->getLParenLoc(), Record);
4279 AddSourceLocation(Init->getRParenLoc(), Record);
4280 Record.push_back(Init->isWritten());
4281 if (Init->isWritten()) {
4282 Record.push_back(Init->getSourceOrder());
4283 } else {
4284 Record.push_back(Init->getNumArrayIndices());
4285 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4286 AddDeclRef(Init->getArrayIndex(i), Record);
4287 }
4288 }
4289}
4290
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004291void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4292 assert(D->DefinitionData);
4293 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004294 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004295 Record.push_back(Data.UserDeclaredConstructor);
4296 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004297 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004298 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004299 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004300 Record.push_back(Data.UserDeclaredDestructor);
4301 Record.push_back(Data.Aggregate);
4302 Record.push_back(Data.PlainOldData);
4303 Record.push_back(Data.Empty);
4304 Record.push_back(Data.Polymorphic);
4305 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004306 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004307 Record.push_back(Data.HasNoNonEmptyBases);
4308 Record.push_back(Data.HasPrivateFields);
4309 Record.push_back(Data.HasProtectedFields);
4310 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004311 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004312 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004313 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004314 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004315 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004316 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004317 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004318 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004319 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004320 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004321 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004322 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004323 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004324 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004325 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004326 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004327 Record.push_back(Data.DeclaredDefaultConstructor);
4328 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004329 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004330 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004331 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004332 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004333 Record.push_back(Data.FailedImplicitMoveConstructor);
4334 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004335 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004336
4337 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004338 if (Data.NumBases > 0)
4339 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4340 Record);
4341
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004342 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4343 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004344 if (Data.NumVBases > 0)
4345 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4346 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004347
4348 AddUnresolvedSet(Data.Conversions, Record);
4349 AddUnresolvedSet(Data.VisibleConversions, Record);
4350 // Data.Definition is the owning decl, no need to write it.
4351 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004352
4353 // Add lambda-specific data.
4354 if (Data.IsLambda) {
4355 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004356 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004357 Record.push_back(Lambda.NumCaptures);
4358 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004359 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004360 AddDeclRef(Lambda.ContextDecl, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004361 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4362 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4363 AddSourceLocation(Capture.getLocation(), Record);
4364 Record.push_back(Capture.isImplicit());
4365 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4366 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4367 AddDeclRef(Var, Record);
4368 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4369 : SourceLocation(),
4370 Record);
4371 }
4372 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004373}
4374
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004375void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004376 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004377 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004378 assert(FirstDeclID == NextDeclID &&
4379 FirstTypeID == NextTypeID &&
4380 FirstIdentID == NextIdentID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004381 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004382 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004383 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004384
Sebastian Redl07a89a82010-07-30 00:29:29 +00004385 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004386
Douglas Gregordf0c1512011-08-18 04:12:04 +00004387 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4388 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4389 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004390 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004391 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004392 NextDeclID = FirstDeclID;
4393 NextTypeID = FirstTypeID;
4394 NextIdentID = FirstIdentID;
4395 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004396 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004397}
4398
Sebastian Redl539c5062010-08-18 23:57:32 +00004399void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004400 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00004401 if (II->hasMacroDefinition())
4402 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004403}
4404
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004405void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004406 // Always take the highest-numbered type index. This copes with an interesting
4407 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004408 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004409 // keep the higher-numbered entry so that we can properly write it out to
4410 // the AST file.
4411 TypeIdx &StoredIdx = TypeIdxs[T];
4412 if (Idx.getIndex() >= StoredIdx.getIndex())
4413 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004414}
4415
Sebastian Redl539c5062010-08-18 23:57:32 +00004416void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004417 SelectorIDs[S] = ID;
4418}
Douglas Gregor91096292010-10-02 19:29:26 +00004419
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004420void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004421 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004422 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004423 MacroDefinitions[MD] = ID;
4424}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004425
Douglas Gregor0abc2622011-12-20 22:06:13 +00004426void ASTWriter::MacroVisible(IdentifierInfo *II) {
4427 DeserializedMacroNames.push_back(II);
4428}
4429
Douglas Gregore37a85a2011-12-02 17:30:13 +00004430void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4431 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4432 SubmoduleIDs[Mod] = ID;
4433}
4434
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004435void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004436 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004437 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004438 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4439 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004440 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004441 // A forward reference was mutated into a definition. Rewrite it.
4442 // FIXME: This happens during template instantiation, should we
4443 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004444 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004445 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004446 }
4447}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004448void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004449 assert(!WritingAST && "Already writing the AST!");
4450
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004451 // TU and namespaces are handled elsewhere.
4452 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4453 return;
4454
Douglas Gregorb3722e22011-09-09 23:01:35 +00004455 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004456 return; // Not a source decl added to a DeclContext from PCH.
4457
4458 AddUpdatedDeclContext(DC);
4459}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004460
4461void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004462 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004463 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004464 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004465 return; // Not a source member added to a class from PCH.
4466 if (!isa<CXXMethodDecl>(D))
4467 return; // We are interested in lazily declared implicit methods.
4468
4469 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004470 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004471 UpdateRecord &Record = DeclUpdates[RD];
4472 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004473 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004474}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004475
4476void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4477 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004478 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004479 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004480 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004481 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004482 return; // Not a source specialization added to a template from PCH.
4483
4484 UpdateRecord &Record = DeclUpdates[TD];
4485 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004486 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004487}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004488
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004489void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4490 const FunctionDecl *D) {
4491 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004492 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004493 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004494 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004495 return; // Not a source specialization added to a template from PCH.
4496
4497 UpdateRecord &Record = DeclUpdates[TD];
4498 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004499 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004500}
4501
Sebastian Redlab238a72011-04-24 16:28:06 +00004502void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004503 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004504 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004505 return; // Declaration not imported from PCH.
4506
4507 // Implicit decl from a PCH was defined.
4508 // FIXME: Should implicit definition be a separate FunctionDecl?
4509 RewriteDecl(D);
4510}
4511
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004512void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004513 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004514 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004515 return;
4516
4517 // Since the actual instantiation is delayed, this really means that we need
4518 // to update the instantiation location.
4519 UpdateRecord &Record = DeclUpdates[D];
4520 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4521 AddSourceLocation(
4522 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4523}
4524
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004525void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4526 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004527 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004528 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004529 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004530
4531 assert(IFD->getDefinition() && "Category on a class without a definition?");
4532 ObjCClassesWithCategories.insert(
4533 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004534}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004535
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004536
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004537void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4538 const ObjCPropertyDecl *OrigProp,
4539 const ObjCCategoryDecl *ClassExt) {
4540 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4541 if (!D)
4542 return;
4543
4544 assert(!WritingAST && "Already writing the AST!");
4545 if (!D->isFromASTFile())
4546 return; // Declaration not imported from PCH.
4547
4548 RewriteDecl(D);
4549}