blob: 88c1f70021efd2e9802090e77272083c81884a0a [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());
198 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000200}
201
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000205}
John McCallb96ec562009-12-04 22:46:56 +0000206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212}
213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217}
218
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000226 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000227 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000228}
229
Alexis Hunte852b102011-05-24 22:41:36 +0000230void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
231 Writer.AddTypeRef(T->getBaseType(), Record);
232 Writer.AddTypeRef(T->getUnderlyingType(), Record);
233 Record.push_back(T->getUTTKind());
234 Code = TYPE_UNARY_TRANSFORM;
235}
236
Richard Smith30482bc2011-02-20 03:19:35 +0000237void ASTTypeWriter::VisitAutoType(const AutoType *T) {
238 Writer.AddTypeRef(T->getDeducedType(), Record);
239 Code = TYPE_AUTO;
240}
241
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000242void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000243 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000244 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000245 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 "Cannot serialize in the middle of a type definition");
247}
248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000249void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000250 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000251 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000252}
253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000256 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000257}
258
John McCall81904512011-01-06 01:58:22 +0000259void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
260 Writer.AddTypeRef(T->getModifiedType(), Record);
261 Writer.AddTypeRef(T->getEquivalentType(), Record);
262 Record.push_back(T->getAttrKind());
263 Code = TYPE_ATTRIBUTED;
264}
265
Mike Stump11289f42009-09-09 15:08:12 +0000266void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000267ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000268 const SubstTemplateTypeParmType *T) {
269 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
270 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000271 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000272}
273
274void
Douglas Gregorada4b792011-01-14 02:55:32 +0000275ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
276 const SubstTemplateTypeParmPackType *T) {
277 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
278 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
279 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
280}
281
282void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000283ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000284 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000285 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000286 Writer.AddTemplateName(T->getTemplateName(), Record);
287 Record.push_back(T->getNumArgs());
288 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
289 ArgI != ArgE; ++ArgI)
290 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000291 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
292 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000293 : T->getCanonicalTypeInternal(),
294 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000295 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000296}
297
298void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000299ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000300 VisitArrayType(T);
301 Writer.AddStmt(T->getSizeExpr());
302 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000303 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000308 const DependentSizedExtVectorType *T) {
309 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000310 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000311}
312
313void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000314ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000315 Record.push_back(T->getDepth());
316 Record.push_back(T->getIndex());
317 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000318 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000319 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000323ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000324 Record.push_back(T->getKeyword());
325 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
326 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000327 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
328 : T->getCanonicalTypeInternal(),
329 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000330 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331}
332
333void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000334ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000335 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000336 Record.push_back(T->getKeyword());
337 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
338 Writer.AddIdentifierRef(T->getIdentifier(), Record);
339 Record.push_back(T->getNumArgs());
340 for (DependentTemplateSpecializationType::iterator
341 I = T->begin(), E = T->end(); I != E; ++I)
342 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000344}
345
Douglas Gregord2fa7662010-12-20 02:24:11 +0000346void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
347 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000348 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
349 Record.push_back(*NumExpansions + 1);
350 else
351 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000352 Code = TYPE_PACK_EXPANSION;
353}
354
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000355void ASTTypeWriter::VisitParenType(const ParenType *T) {
356 Writer.AddTypeRef(T->getInnerType(), Record);
357 Code = TYPE_PAREN;
358}
359
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000360void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000361 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000362 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
363 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000364 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000365}
366
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000367void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000368 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000369 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000370 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000371}
372
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000375 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000376}
377
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000379 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000380 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000381 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000382 E = T->qual_end(); I != E; ++I)
383 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000384 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000385}
386
Steve Narofffb4330f2009-06-17 22:40:22 +0000387void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000388ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000389 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000390 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000391}
392
Eli Friedman0dfb8892011-10-06 23:00:33 +0000393void
394ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
395 Writer.AddTypeRef(T->getValueType(), Record);
396 Code = TYPE_ATOMIC;
397}
398
John McCall8f115c62009-10-16 21:56:05 +0000399namespace {
400
401class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000402 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000403 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000404
405public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000406 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000407 : Writer(Writer), Record(Record) { }
408
John McCall17001972009-10-18 01:05:36 +0000409#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000410#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000411 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000412#include "clang/AST/TypeLocNodes.def"
413
John McCall17001972009-10-18 01:05:36 +0000414 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
415 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000416};
417
418}
419
John McCall17001972009-10-18 01:05:36 +0000420void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
421 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000422}
John McCall17001972009-10-18 01:05:36 +0000423void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000424 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
425 if (TL.needsExtraLocalData()) {
426 Record.push_back(TL.getWrittenTypeSpec());
427 Record.push_back(TL.getWrittenSignSpec());
428 Record.push_back(TL.getWrittenWidthSpec());
429 Record.push_back(TL.hasModeAttr());
430 }
John McCall8f115c62009-10-16 21:56:05 +0000431}
John McCall17001972009-10-18 01:05:36 +0000432void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
433 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000434}
John McCall17001972009-10-18 01:05:36 +0000435void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
436 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000437}
John McCall17001972009-10-18 01:05:36 +0000438void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000443}
John McCall17001972009-10-18 01:05:36 +0000444void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000446}
John McCall17001972009-10-18 01:05:36 +0000447void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000449 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000450}
John McCall17001972009-10-18 01:05:36 +0000451void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
453 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
454 Record.push_back(TL.getSizeExpr() ? 1 : 0);
455 if (TL.getSizeExpr())
456 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000457}
John McCall17001972009-10-18 01:05:36 +0000458void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
459 VisitArrayTypeLoc(TL);
460}
461void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
465 VisitArrayTypeLoc(TL);
466}
467void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
468 DependentSizedArrayTypeLoc TL) {
469 VisitArrayTypeLoc(TL);
470}
471void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
472 DependentSizedExtVectorTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getNameLoc(), Record);
474}
475void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
478void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000482 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
483 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000484 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000485 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
486 Writer.AddDeclRef(TL.getArg(i), Record);
487}
488void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
489 VisitFunctionTypeLoc(TL);
490}
491void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
492 VisitFunctionTypeLoc(TL);
493}
John McCallb96ec562009-12-04 22:46:56 +0000494void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
John McCall17001972009-10-18 01:05:36 +0000497void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getNameLoc(), Record);
499}
500void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000501 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
502 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
503 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000504}
505void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000506 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
507 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
508 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
509 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000510}
511void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
Alexis Hunte852b102011-05-24 22:41:36 +0000514void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getKWLoc(), Record);
516 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
517 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
518 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
519}
Richard Smith30482bc2011-02-20 03:19:35 +0000520void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
John McCall17001972009-10-18 01:05:36 +0000523void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getNameLoc(), Record);
525}
526void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
527 Writer.AddSourceLocation(TL.getNameLoc(), Record);
528}
John McCall81904512011-01-06 01:58:22 +0000529void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
531 if (TL.hasAttrOperand()) {
532 SourceRange range = TL.getAttrOperandParensRange();
533 Writer.AddSourceLocation(range.getBegin(), Record);
534 Writer.AddSourceLocation(range.getEnd(), Record);
535 }
536 if (TL.hasAttrExprOperand()) {
537 Expr *operand = TL.getAttrExprOperand();
538 Record.push_back(operand ? 1 : 0);
539 if (operand) Writer.AddStmt(operand);
540 } else if (TL.hasAttrEnumOperand()) {
541 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
542 }
543}
John McCall17001972009-10-18 01:05:36 +0000544void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
545 Writer.AddSourceLocation(TL.getNameLoc(), Record);
546}
John McCallcebee162009-10-18 09:09:24 +0000547void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
548 SubstTemplateTypeParmTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
Douglas Gregorada4b792011-01-14 02:55:32 +0000551void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
552 SubstTemplateTypeParmPackTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
John McCall17001972009-10-18 01:05:36 +0000555void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
556 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000557 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000558 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
559 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
560 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
561 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000562 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
563 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000564}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000565void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
567 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
568}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000569void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000570 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000571 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000572}
John McCalle78aac42010-03-10 03:28:59 +0000573void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000576void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000579 Writer.AddSourceLocation(TL.getNameLoc(), Record);
580}
John McCallc392f372010-06-11 00:33:02 +0000581void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
582 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000583 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000584 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000585 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000586 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000587 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
588 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
589 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000590 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
591 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000592}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000593void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
594 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
595}
John McCall17001972009-10-18 01:05:36 +0000596void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
597 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000598}
599void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
600 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000601 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
602 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
603 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
604 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000605}
John McCallfc93cf92009-10-22 22:37:11 +0000606void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
607 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000608}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000609void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
610 Writer.AddSourceLocation(TL.getKWLoc(), Record);
611 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
612 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
613}
John McCall8f115c62009-10-16 21:56:05 +0000614
Chris Lattner19cea4e2009-04-22 05:57:30 +0000615//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000616// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000617//===----------------------------------------------------------------------===//
618
Chris Lattner28fa4e62009-04-26 22:26:21 +0000619static void EmitBlockID(unsigned ID, const char *Name,
620 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000621 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000622 Record.clear();
623 Record.push_back(ID);
624 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
625
626 // Emit the block name if present.
627 if (Name == 0 || Name[0] == 0) return;
628 Record.clear();
629 while (*Name)
630 Record.push_back(*Name++);
631 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
632}
633
634static void EmitRecordID(unsigned ID, const char *Name,
635 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000636 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000637 Record.clear();
638 Record.push_back(ID);
639 while (*Name)
640 Record.push_back(*Name++);
641 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000642}
643
644static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000645 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000646#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000647 RECORD(STMT_STOP);
648 RECORD(STMT_NULL_PTR);
649 RECORD(STMT_NULL);
650 RECORD(STMT_COMPOUND);
651 RECORD(STMT_CASE);
652 RECORD(STMT_DEFAULT);
653 RECORD(STMT_LABEL);
654 RECORD(STMT_IF);
655 RECORD(STMT_SWITCH);
656 RECORD(STMT_WHILE);
657 RECORD(STMT_DO);
658 RECORD(STMT_FOR);
659 RECORD(STMT_GOTO);
660 RECORD(STMT_INDIRECT_GOTO);
661 RECORD(STMT_CONTINUE);
662 RECORD(STMT_BREAK);
663 RECORD(STMT_RETURN);
664 RECORD(STMT_DECL);
665 RECORD(STMT_ASM);
666 RECORD(EXPR_PREDEFINED);
667 RECORD(EXPR_DECL_REF);
668 RECORD(EXPR_INTEGER_LITERAL);
669 RECORD(EXPR_FLOATING_LITERAL);
670 RECORD(EXPR_IMAGINARY_LITERAL);
671 RECORD(EXPR_STRING_LITERAL);
672 RECORD(EXPR_CHARACTER_LITERAL);
673 RECORD(EXPR_PAREN);
674 RECORD(EXPR_UNARY_OPERATOR);
675 RECORD(EXPR_SIZEOF_ALIGN_OF);
676 RECORD(EXPR_ARRAY_SUBSCRIPT);
677 RECORD(EXPR_CALL);
678 RECORD(EXPR_MEMBER);
679 RECORD(EXPR_BINARY_OPERATOR);
680 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
681 RECORD(EXPR_CONDITIONAL_OPERATOR);
682 RECORD(EXPR_IMPLICIT_CAST);
683 RECORD(EXPR_CSTYLE_CAST);
684 RECORD(EXPR_COMPOUND_LITERAL);
685 RECORD(EXPR_EXT_VECTOR_ELEMENT);
686 RECORD(EXPR_INIT_LIST);
687 RECORD(EXPR_DESIGNATED_INIT);
688 RECORD(EXPR_IMPLICIT_VALUE_INIT);
689 RECORD(EXPR_VA_ARG);
690 RECORD(EXPR_ADDR_LABEL);
691 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000692 RECORD(EXPR_CHOOSE);
693 RECORD(EXPR_GNU_NULL);
694 RECORD(EXPR_SHUFFLE_VECTOR);
695 RECORD(EXPR_BLOCK);
696 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000697 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000698 RECORD(EXPR_OBJC_STRING_LITERAL);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000699 RECORD(EXPR_OBJC_NUMERIC_LITERAL);
700 RECORD(EXPR_OBJC_ARRAY_LITERAL);
701 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000702 RECORD(EXPR_OBJC_ENCODE);
703 RECORD(EXPR_OBJC_SELECTOR_EXPR);
704 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
705 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
706 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
707 RECORD(EXPR_OBJC_KVC_REF_EXPR);
708 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000709 RECORD(STMT_OBJC_FOR_COLLECTION);
710 RECORD(STMT_OBJC_CATCH);
711 RECORD(STMT_OBJC_FINALLY);
712 RECORD(STMT_OBJC_AT_TRY);
713 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
714 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000715 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000716 RECORD(EXPR_CXX_OPERATOR_CALL);
717 RECORD(EXPR_CXX_CONSTRUCT);
718 RECORD(EXPR_CXX_STATIC_CAST);
719 RECORD(EXPR_CXX_DYNAMIC_CAST);
720 RECORD(EXPR_CXX_REINTERPRET_CAST);
721 RECORD(EXPR_CXX_CONST_CAST);
722 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
723 RECORD(EXPR_CXX_BOOL_LITERAL);
724 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000725 RECORD(EXPR_CXX_TYPEID_EXPR);
726 RECORD(EXPR_CXX_TYPEID_TYPE);
727 RECORD(EXPR_CXX_UUIDOF_EXPR);
728 RECORD(EXPR_CXX_UUIDOF_TYPE);
729 RECORD(EXPR_CXX_THIS);
730 RECORD(EXPR_CXX_THROW);
731 RECORD(EXPR_CXX_DEFAULT_ARG);
732 RECORD(EXPR_CXX_BIND_TEMPORARY);
733 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
734 RECORD(EXPR_CXX_NEW);
735 RECORD(EXPR_CXX_DELETE);
736 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
737 RECORD(EXPR_EXPR_WITH_CLEANUPS);
738 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
739 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
740 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
741 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
742 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
743 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
744 RECORD(EXPR_CXX_NOEXCEPT);
745 RECORD(EXPR_OPAQUE_VALUE);
746 RECORD(EXPR_BINARY_TYPE_TRAIT);
747 RECORD(EXPR_PACK_EXPANSION);
748 RECORD(EXPR_SIZEOF_PACK);
749 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000750 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000751#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000752}
Mike Stump11289f42009-09-09 15:08:12 +0000753
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000754void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000755 RecordData Record;
756 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000757
Sebastian Redl539c5062010-08-18 23:57:32 +0000758#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
759#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000760
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000761 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000762 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000763 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000764 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000765 RECORD(TYPE_OFFSET);
766 RECORD(DECL_OFFSET);
767 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000768 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000769 RECORD(IDENTIFIER_OFFSET);
770 RECORD(IDENTIFIER_TABLE);
771 RECORD(EXTERNAL_DEFINITIONS);
772 RECORD(SPECIAL_TYPES);
773 RECORD(STATISTICS);
774 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000775 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000776 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
777 RECORD(SELECTOR_OFFSETS);
778 RECORD(METHOD_POOL);
779 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000780 RECORD(SOURCE_LOCATION_OFFSETS);
781 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000782 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000783 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000784 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000785 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000786 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000787 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000788 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000789 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000790 RECORD(SEMA_DECL_REFS);
791 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
792 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
793 RECORD(DECL_REPLACEMENTS);
794 RECORD(UPDATE_VISIBLE);
795 RECORD(DECL_UPDATE_OFFSETS);
796 RECORD(DECL_UPDATES);
797 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
798 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000799 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000800 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000801 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000802 RECORD(FP_PRAGMA_OPTIONS);
803 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000804 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000805 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
806 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000807 RECORD(MODULE_OFFSET_MAP);
808 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000809 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000810 RECORD(FILE_SORTED_DECLS);
811 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000812 RECORD(MERGED_DECLARATIONS);
813 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000814 RECORD(OBJC_CATEGORIES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000815
Chris Lattner28fa4e62009-04-26 22:26:21 +0000816 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000817 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000818 RECORD(SM_SLOC_FILE_ENTRY);
819 RECORD(SM_SLOC_BUFFER_ENTRY);
820 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000821 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner28fa4e62009-04-26 22:26:21 +0000823 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000824 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000825 RECORD(PP_MACRO_OBJECT_LIKE);
826 RECORD(PP_MACRO_FUNCTION_LIKE);
827 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000828
Douglas Gregor12bfa382009-10-17 00:13:19 +0000829 // Decls and Types block.
830 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000832 RECORD(TYPE_COMPLEX);
833 RECORD(TYPE_POINTER);
834 RECORD(TYPE_BLOCK_POINTER);
835 RECORD(TYPE_LVALUE_REFERENCE);
836 RECORD(TYPE_RVALUE_REFERENCE);
837 RECORD(TYPE_MEMBER_POINTER);
838 RECORD(TYPE_CONSTANT_ARRAY);
839 RECORD(TYPE_INCOMPLETE_ARRAY);
840 RECORD(TYPE_VARIABLE_ARRAY);
841 RECORD(TYPE_VECTOR);
842 RECORD(TYPE_EXT_VECTOR);
843 RECORD(TYPE_FUNCTION_PROTO);
844 RECORD(TYPE_FUNCTION_NO_PROTO);
845 RECORD(TYPE_TYPEDEF);
846 RECORD(TYPE_TYPEOF_EXPR);
847 RECORD(TYPE_TYPEOF);
848 RECORD(TYPE_RECORD);
849 RECORD(TYPE_ENUM);
850 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000851 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000852 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000853 RECORD(TYPE_DECLTYPE);
854 RECORD(TYPE_ELABORATED);
855 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
856 RECORD(TYPE_UNRESOLVED_USING);
857 RECORD(TYPE_INJECTED_CLASS_NAME);
858 RECORD(TYPE_OBJC_OBJECT);
859 RECORD(TYPE_TEMPLATE_TYPE_PARM);
860 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
861 RECORD(TYPE_DEPENDENT_NAME);
862 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
863 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
864 RECORD(TYPE_PAREN);
865 RECORD(TYPE_PACK_EXPANSION);
866 RECORD(TYPE_ATTRIBUTED);
867 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000868 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000869 RECORD(DECL_TYPEDEF);
870 RECORD(DECL_ENUM);
871 RECORD(DECL_RECORD);
872 RECORD(DECL_ENUM_CONSTANT);
873 RECORD(DECL_FUNCTION);
874 RECORD(DECL_OBJC_METHOD);
875 RECORD(DECL_OBJC_INTERFACE);
876 RECORD(DECL_OBJC_PROTOCOL);
877 RECORD(DECL_OBJC_IVAR);
878 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000879 RECORD(DECL_OBJC_CATEGORY);
880 RECORD(DECL_OBJC_CATEGORY_IMPL);
881 RECORD(DECL_OBJC_IMPLEMENTATION);
882 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
883 RECORD(DECL_OBJC_PROPERTY);
884 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000885 RECORD(DECL_FIELD);
886 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000887 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000888 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000889 RECORD(DECL_FILE_SCOPE_ASM);
890 RECORD(DECL_BLOCK);
891 RECORD(DECL_CONTEXT_LEXICAL);
892 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000893 RECORD(DECL_NAMESPACE);
894 RECORD(DECL_NAMESPACE_ALIAS);
895 RECORD(DECL_USING);
896 RECORD(DECL_USING_SHADOW);
897 RECORD(DECL_USING_DIRECTIVE);
898 RECORD(DECL_UNRESOLVED_USING_VALUE);
899 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
900 RECORD(DECL_LINKAGE_SPEC);
901 RECORD(DECL_CXX_RECORD);
902 RECORD(DECL_CXX_METHOD);
903 RECORD(DECL_CXX_CONSTRUCTOR);
904 RECORD(DECL_CXX_DESTRUCTOR);
905 RECORD(DECL_CXX_CONVERSION);
906 RECORD(DECL_ACCESS_SPEC);
907 RECORD(DECL_FRIEND);
908 RECORD(DECL_FRIEND_TEMPLATE);
909 RECORD(DECL_CLASS_TEMPLATE);
910 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
911 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
912 RECORD(DECL_FUNCTION_TEMPLATE);
913 RECORD(DECL_TEMPLATE_TYPE_PARM);
914 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
915 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
916 RECORD(DECL_STATIC_ASSERT);
917 RECORD(DECL_CXX_BASE_SPECIFIERS);
918 RECORD(DECL_INDIRECTFIELD);
919 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
920
Douglas Gregor03412ba2011-06-03 02:27:19 +0000921 // Statements and Exprs can occur in the Decls and Types block.
922 AddStmtsExprs(Stream, Record);
923
Douglas Gregor92a96f52011-02-08 21:58:10 +0000924 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000925 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000926 RECORD(PPD_MACRO_DEFINITION);
927 RECORD(PPD_INCLUSION_DIRECTIVE);
928
Chris Lattner28fa4e62009-04-26 22:26:21 +0000929#undef RECORD
930#undef BLOCK
931 Stream.ExitBlock();
932}
933
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000934/// \brief Adjusts the given filename to only write out the portion of the
935/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000936///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000937/// \param Filename the file name to adjust.
938///
939/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
940/// the returned filename will be adjusted by this system root.
941///
942/// \returns either the original filename (if it needs no adjustment) or the
943/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000944static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000945adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000946 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000947
Douglas Gregorc567ba22011-07-22 16:35:34 +0000948 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000949 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000950
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000951 // Verify that the filename and the system root have the same prefix.
952 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000953 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954 if (Filename[Pos] != isysroot[Pos])
955 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000956
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 // We hit the end of the filename before we hit the end of the system root.
958 if (!Filename[Pos])
959 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000960
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000961 // If the file name has a '/' at the current position, skip over the '/'.
962 // We distinguish sysroot-based includes from absolute includes by the
963 // absence of '/' at the beginning of sysroot-based includes.
964 if (Filename[Pos] == '/')
965 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000966
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000967 return Filename + Pos;
968}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000969
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000970/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000971void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000972 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000973 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000974
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000975 // Metadata
Douglas Gregore8bbc122011-09-02 00:18:52 +0000976 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000977 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000978 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000979 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
980 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000981 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
982 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
983 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregor29cc6422011-08-17 21:07:30 +0000984 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000986
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000987 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000988 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000989 Record.push_back(VERSION_MAJOR);
990 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000991 Record.push_back(CLANG_VERSION_MAJOR);
992 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +0000993 Record.push_back(!isysroot.empty());
Douglas Gregor29cc6422011-08-17 21:07:30 +0000994 const std::string &Triple = Target.getTriple().getTriple();
995 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
996
997 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +0000998 serialization::ModuleManager &Mgr = Chain->getModuleManager();
999 llvm::SmallVector<char, 128> ModulePaths;
1000 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001001
1002 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1003 M != MEnd; ++M) {
1004 // Skip modules that weren't directly imported.
1005 if (!(*M)->isDirectlyImported())
1006 continue;
1007
1008 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1009 // FIXME: Write import location, once it matters.
1010 // FIXME: This writes the absolute path for AST files we depend on.
1011 const std::string &FileName = (*M)->FileName;
1012 Record.push_back(FileName.size());
1013 Record.append(FileName.begin(), FileName.end());
1014 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001015 Stream.EmitRecord(IMPORTS, Record);
1016 }
Mike Stump11289f42009-09-09 15:08:12 +00001017
Douglas Gregora3b20262011-05-06 21:43:30 +00001018 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001019 SourceManager &SM = Context.getSourceManager();
1020 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1021 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001022 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001023 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1024 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1025
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001026 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001027
Michael J. Spencer740857f2010-12-21 16:45:57 +00001028 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001029
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001030 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001031 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001032 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001033 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001034 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001035 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001036
1037 Record.clear();
1038 Record.push_back(SM.getMainFileID().getOpaqueValue());
1039 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001040 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001041
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001042 // Original PCH directory
1043 if (!OutputFile.empty() && OutputFile != "-") {
1044 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1045 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1047 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1048
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001049 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001050
1051 llvm::sys::fs::make_absolute(OutputPath);
1052 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1053
1054 RecordData Record;
1055 Record.push_back(ORIGINAL_PCH_DIR);
1056 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1057 }
1058
Ted Kremenek18e066f2010-01-22 22:12:47 +00001059 // Repository branch/version information.
1060 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001061 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001062 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1063 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001064 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001065 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001066 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1067 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001068}
1069
1070/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001071void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001072 RecordData Record;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00001073#define LANGOPT(Name, Bits, Default, Description) \
1074 Record.push_back(LangOpts.Name);
1075#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1076 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1077#include "clang/Basic/LangOptions.def"
Douglas Gregor7d106e42011-11-15 19:35:01 +00001078
1079 Record.push_back(LangOpts.CurrentModule.size());
1080 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl539c5062010-08-18 23:57:32 +00001081 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001082}
1083
Douglas Gregora7f71a92009-04-10 03:52:48 +00001084//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001085// stat cache Serialization
1086//===----------------------------------------------------------------------===//
1087
1088namespace {
1089// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001090class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001091public:
1092 typedef const char * key_type;
1093 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattner2a6fa472010-11-23 19:28:12 +00001095 typedef struct stat data_type;
1096 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001097
1098 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001099 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001100 }
Mike Stump11289f42009-09-09 15:08:12 +00001101
1102 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001103 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001104 data_type_ref Data) {
1105 unsigned StrLen = strlen(path);
1106 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001107 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001108 clang::io::Emit8(Out, DataLen);
1109 return std::make_pair(StrLen + 1, DataLen);
1110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001112 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 Out.write(path, KeyLen);
1114 }
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001116 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001117 data_type_ref Data, unsigned DataLen) {
1118 using namespace clang::io;
1119 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001120
Chris Lattner2a6fa472010-11-23 19:28:12 +00001121 Emit32(Out, (uint32_t) Data.st_ino);
1122 Emit32(Out, (uint32_t) Data.st_dev);
1123 Emit16(Out, (uint16_t) Data.st_mode);
1124 Emit64(Out, (uint64_t) Data.st_mtime);
1125 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001126
1127 assert(Out.tell() - Start == DataLen && "Wrong data length");
1128 }
1129};
1130} // end anonymous namespace
1131
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001132/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001133void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001134 // Build the on-disk hash table containing information about every
1135 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001136 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001137 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001138 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001139 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001140 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001141 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001142 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001143 }
Mike Stump11289f42009-09-09 15:08:12 +00001144
Douglas Gregorc5046832009-04-27 18:38:38 +00001145 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001146 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001147 uint32_t BucketOffset;
1148 {
1149 llvm::raw_svector_ostream Out(StatCacheData);
1150 // Make sure that no bucket is at offset 0
1151 clang::io::Emit32(Out, 0);
1152 BucketOffset = Generator.Emit(Out);
1153 }
1154
1155 // Create a blob abbreviation
1156 using namespace llvm;
1157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001158 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1162 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1163
1164 // Write the stat cache
1165 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001166 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001167 Record.push_back(BucketOffset);
1168 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001169 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001170}
1171
1172//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001173// Source Manager Serialization
1174//===----------------------------------------------------------------------===//
1175
1176/// \brief Create an abbreviation for the SLocEntry that refers to a
1177/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001178static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001179 using namespace llvm;
1180 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001181 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001186 // FileEntry fields.
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001194 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001195}
1196
1197/// \brief Create an abbreviation for the SLocEntry that refers to a
1198/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001199static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001200 using namespace llvm;
1201 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001202 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1205 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1206 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001208 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001209}
1210
1211/// \brief Create an abbreviation for the SLocEntry that refers to a
1212/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001213static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001214 using namespace llvm;
1215 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001216 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001218 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001219}
1220
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001221/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1222/// expansion.
1223static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001224 using namespace llvm;
1225 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001226 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001232 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001233}
1234
Douglas Gregor09b69892011-02-10 17:09:37 +00001235namespace {
1236 // Trait used for the on-disk hash table of header search information.
1237 class HeaderFileInfoTrait {
1238 ASTWriter &Writer;
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001239 const HeaderSearch &HS;
Douglas Gregor09b69892011-02-10 17:09:37 +00001240
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001241 // Keep track of the framework names we've used during serialization.
1242 SmallVector<char, 128> FrameworkStringData;
1243 llvm::StringMap<unsigned> FrameworkNameOffset;
1244
Douglas Gregor09b69892011-02-10 17:09:37 +00001245 public:
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001246 HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
Douglas Gregor09b69892011-02-10 17:09:37 +00001247 : Writer(Writer), HS(HS) { }
1248
1249 typedef const char *key_type;
1250 typedef key_type key_type_ref;
1251
1252 typedef HeaderFileInfo data_type;
1253 typedef const data_type &data_type_ref;
1254
1255 static unsigned ComputeHash(const char *path) {
1256 // The hash is based only on the filename portion of the key, so that the
1257 // reader can match based on filenames when symlinking or excess path
1258 // elements ("foo/../", "../") change the form of the name. However,
1259 // complete path is still the key.
1260 return llvm::HashString(llvm::sys::path::filename(path));
1261 }
1262
1263 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001264 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001265 data_type_ref Data) {
1266 unsigned StrLen = strlen(path);
1267 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001268 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001269 clang::io::Emit8(Out, DataLen);
1270 return std::make_pair(StrLen + 1, DataLen);
1271 }
1272
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001273 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001274 Out.write(path, KeyLen);
1275 }
1276
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001277 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001278 data_type_ref Data, unsigned DataLen) {
1279 using namespace clang::io;
1280 uint64_t Start = Out.tell(); (void)Start;
1281
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001282 unsigned char Flags = (Data.isImport << 5)
1283 | (Data.isPragmaOnce << 4)
1284 | (Data.DirInfo << 2)
1285 | (Data.Resolved << 1)
1286 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001287 Emit8(Out, (uint8_t)Flags);
1288 Emit16(Out, (uint16_t) Data.NumIncludes);
1289
1290 if (!Data.ControllingMacro)
1291 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1292 else
1293 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001294
1295 unsigned Offset = 0;
1296 if (!Data.Framework.empty()) {
1297 // If this header refers into a framework, save the framework name.
1298 llvm::StringMap<unsigned>::iterator Pos
1299 = FrameworkNameOffset.find(Data.Framework);
1300 if (Pos == FrameworkNameOffset.end()) {
1301 Offset = FrameworkStringData.size() + 1;
1302 FrameworkStringData.append(Data.Framework.begin(),
1303 Data.Framework.end());
1304 FrameworkStringData.push_back(0);
1305
1306 FrameworkNameOffset[Data.Framework] = Offset;
1307 } else
1308 Offset = Pos->second;
1309 }
1310 Emit32(Out, Offset);
1311
Douglas Gregor09b69892011-02-10 17:09:37 +00001312 assert(Out.tell() - Start == DataLen && "Wrong data length");
1313 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001314
1315 const char *strings_begin() const { return FrameworkStringData.begin(); }
1316 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001317 };
1318} // end anonymous namespace
1319
1320/// \brief Write the header search block for the list of files that
1321///
1322/// \param HS The header search structure to save.
1323///
1324/// \param Chain Whether we're creating a chained AST file.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001325void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001326 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001327 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1328
1329 if (FilesByUID.size() > HS.header_file_size())
1330 FilesByUID.resize(HS.header_file_size());
1331
1332 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1333 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001334 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001335 unsigned NumHeaderSearchEntries = 0;
1336 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1337 const FileEntry *File = FilesByUID[UID];
1338 if (!File)
1339 continue;
1340
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001341 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1342 // from the external source if it was not provided already.
1343 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001344 if (HFI.External && Chain)
1345 continue;
1346
1347 // Turn the file name into an absolute path, if it isn't already.
1348 const char *Filename = File->getName();
1349 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1350
1351 // If we performed any translation on the file name at all, we need to
1352 // save this string, since the generator will refer to it later.
1353 if (Filename != File->getName()) {
1354 Filename = strdup(Filename);
1355 SavedStrings.push_back(Filename);
1356 }
1357
1358 Generator.insert(Filename, HFI, GeneratorTrait);
1359 ++NumHeaderSearchEntries;
1360 }
1361
1362 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001363 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001364 uint32_t BucketOffset;
1365 {
1366 llvm::raw_svector_ostream Out(TableData);
1367 // Make sure that no bucket is at offset 0
1368 clang::io::Emit32(Out, 0);
1369 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1370 }
1371
1372 // Create a blob abbreviation
1373 using namespace llvm;
1374 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1375 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1380 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1381
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001382 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001383 RecordData Record;
1384 Record.push_back(HEADER_SEARCH_TABLE);
1385 Record.push_back(BucketOffset);
1386 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001387 Record.push_back(TableData.size());
1388 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001389 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1390
1391 // Free all of the strings we had to duplicate.
1392 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1393 free((void*)SavedStrings[I]);
1394}
1395
Douglas Gregora7f71a92009-04-10 03:52:48 +00001396/// \brief Writes the block containing the serialized form of the
1397/// source manager.
1398///
1399/// TODO: We should probably use an on-disk hash table (stored in a
1400/// blob), indexed based on the file name, so that we only create
1401/// entries for files that we actually need. In the common case (no
1402/// errors), we probably won't have to create file entries for any of
1403/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001404void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001405 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001406 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001407 RecordData Record;
1408
Chris Lattner0910e3b2009-04-10 17:16:57 +00001409 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001410 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001411
1412 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001413 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1414 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1415 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001416 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001417
Douglas Gregor258ae542009-04-27 06:38:32 +00001418 // Write out the source location entry table. We skip the first
1419 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001420 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001421 // Write out the offsets of only source location file entries.
1422 // We will go through them in ASTReader::validateFileEntries().
1423 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001424 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001425 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1426 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001427 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001428 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001429 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001430
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 // Record the offset of this source-location entry.
1432 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1433
1434 // Figure out which record code to use.
1435 unsigned Code;
1436 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001437 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1438 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001439 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001440 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1441 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001442 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001443 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001444 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001445 Record.clear();
1446 Record.push_back(Code);
1447
Douglas Gregor925296b2011-07-19 16:10:42 +00001448 // Starting offset of this entry within this module, so skip the dummy.
1449 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001450 if (SLoc->isFile()) {
1451 const SrcMgr::FileInfo &File = SLoc->getFile();
1452 Record.push_back(File.getIncludeLoc().getRawEncoding());
1453 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1454 Record.push_back(File.hasLineDirectives());
1455
1456 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001457 if (Content->OrigEntry) {
1458 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001459 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001460
Douglas Gregor258ae542009-04-27 06:38:32 +00001461 // The source location entry is a file. The blob associated
1462 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001463
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001464 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001465 Record.push_back(Content->OrigEntry->getSize());
1466 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001467 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001468 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001469
1470 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1471 if (FDI != FileDeclIDs.end()) {
1472 Record.push_back(FDI->second->FirstDeclIndex);
1473 Record.push_back(FDI->second->DeclIDs.size());
1474 } else {
1475 Record.push_back(0);
1476 Record.push_back(0);
1477 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001478
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001479 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001480 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001481 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001482
1483 // Ask the file manager to fixup the relative path for us. This will
1484 // honor the working directory.
1485 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1486
1487 // FIXME: This call to make_absolute shouldn't be necessary, the
1488 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001489 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001490 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001491
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001492 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001493 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001494
1495 if (Content->BufferOverridden) {
1496 Record.clear();
1497 Record.push_back(SM_SLOC_BUFFER_BLOB);
1498 const llvm::MemoryBuffer *Buffer
1499 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1500 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1501 StringRef(Buffer->getBufferStart(),
1502 Buffer->getBufferSize() + 1));
1503 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001504 } else {
1505 // The source location entry is a buffer. The blob associated
1506 // with this entry contains the contents of the buffer.
1507
1508 // We add one to the size so that we capture the trailing NULL
1509 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1510 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001511 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001512 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001513 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001514 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001515 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001516 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001517 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001518 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001519 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001520 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001521
Douglas Gregor925296b2011-07-19 16:10:42 +00001522 if (strcmp(Name, "<built-in>") == 0) {
1523 PreloadSLocs.push_back(SLocEntryOffsets.size());
1524 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001525 }
1526 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001527 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001528 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001529 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1530 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001531 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1532 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001533
1534 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001535 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001536 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001537 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001538 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001539 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001540 }
1541 }
1542
Douglas Gregor8f45df52009-04-16 22:23:12 +00001543 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001544
1545 if (SLocEntryOffsets.empty())
1546 return;
1547
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001548 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001549 // table is used for lazily loading source-location information.
1550 using namespace llvm;
1551 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001552 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001553 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001554 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001555 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1556 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001557
Douglas Gregor258ae542009-04-27 06:38:32 +00001558 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001559 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001560 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001561 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001562 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001563
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001564 Abbrev = new BitCodeAbbrev();
1565 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1568 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1569
1570 Record.clear();
1571 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1572 Record.push_back(SLocFileEntryOffsets.size());
1573 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1574 data(SLocFileEntryOffsets));
1575
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001576 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001577 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001578 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001579
1580 // Write the line table. It depends on remapping working, so it must come
1581 // after the source location offsets.
1582 if (SourceMgr.hasLineTable()) {
1583 LineTableInfo &LineTable = SourceMgr.getLineTable();
1584
1585 Record.clear();
1586 // Emit the file names
1587 Record.push_back(LineTable.getNumFilenames());
1588 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1589 // Emit the file name
1590 const char *Filename = LineTable.getFilename(I);
1591 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1592 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1593 Record.push_back(FilenameLen);
1594 if (FilenameLen)
1595 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1596 }
1597
1598 // Emit the line entries
1599 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1600 L != LEnd; ++L) {
1601 // Only emit entries for local files.
1602 if (L->first < 0)
1603 continue;
1604
1605 // Emit the file ID
1606 Record.push_back(L->first);
1607
1608 // Emit the line entries
1609 Record.push_back(L->second.size());
1610 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1611 LEEnd = L->second.end();
1612 LE != LEEnd; ++LE) {
1613 Record.push_back(LE->FileOffset);
1614 Record.push_back(LE->LineNo);
1615 Record.push_back(LE->FilenameID);
1616 Record.push_back((unsigned)LE->FileKind);
1617 Record.push_back(LE->IncludeOffset);
1618 }
1619 }
1620 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1621 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001622}
1623
Douglas Gregorc5046832009-04-27 18:38:38 +00001624//===----------------------------------------------------------------------===//
1625// Preprocessor Serialization
1626//===----------------------------------------------------------------------===//
1627
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001628static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1629 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1630 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1631 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1632 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1633 return X.first->getName().compare(Y.first->getName());
1634}
1635
Chris Lattnereeffaef2009-04-10 17:15:23 +00001636/// \brief Writes the block containing the serialized form of the
1637/// preprocessor.
1638///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001639void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001640 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1641 if (PPRec)
1642 WritePreprocessorDetail(*PPRec);
1643
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001644 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001645
Chris Lattner0af3ba12009-04-13 01:29:17 +00001646 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1647 if (PP.getCounterValue() != 0) {
1648 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001649 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001650 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001651 }
1652
1653 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001654 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001655
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001656 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001657 // FIXME: use diagnostics subsystem for localization etc.
1658 if (PP.SawDateOrTime())
1659 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001660
Douglas Gregor796d76a2010-10-20 22:00:55 +00001661
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001662 // Loop over all the macro definitions that are live at the end of the file,
1663 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001664
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001665 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001666 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001667 MacrosToEmit;
1668 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001669 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1670 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001671 I != E; ++I) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001672 const IdentifierInfo *Name = I->first;
Douglas Gregorebf00492011-10-17 15:32:29 +00001673 if (!IsModule || I->second->isPublic()) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001674 MacroDefinitionsSeen.insert(Name);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001675 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1676 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001677 }
1678
1679 // Sort the set of macro definitions that need to be serialized by the
1680 // name of the macro, to provide a stable ordering.
1681 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1682 &compareMacroDefinitions);
1683
Douglas Gregor68051a72011-02-11 00:26:14 +00001684 // Resolve any identifiers that defined macros at the time they were
1685 // deserialized, adding them to the list of macros to emit (if appropriate).
1686 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1687 IdentifierInfo *Name
1688 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1689 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1690 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1691 }
1692
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001693 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1694 const IdentifierInfo *Name = MacrosToEmit[I].first;
1695 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001696 if (!MI)
1697 continue;
1698
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001699 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001700 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001701 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001702
1703 // FIXME: There is a (probably minor) optimization we could do here, if
1704 // the macro comes from the original PCH but the identifier comes from a
1705 // chained PCH, by storing the offset into the original PCH rather than
1706 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001707 if (MI->isBuiltinMacro() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001708 (Chain &&
1709 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1710 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001711 continue;
1712
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001713 AddIdentifierRef(Name, Record);
1714 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001715 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1716 Record.push_back(MI->isUsed());
Douglas Gregorebf00492011-10-17 15:32:29 +00001717 Record.push_back(MI->isPublic());
1718 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001719 unsigned Code;
1720 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001721 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001722 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001723 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001724
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001725 Record.push_back(MI->isC99Varargs());
1726 Record.push_back(MI->isGNUVarargs());
1727 Record.push_back(MI->getNumArgs());
1728 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1729 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001730 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001731 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001732
Douglas Gregoraae92242010-03-19 21:51:54 +00001733 // If we have a detailed preprocessing record, record the macro definition
1734 // ID that corresponds to this macro.
1735 if (PPRec)
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001736 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001737
Douglas Gregor8f45df52009-04-16 22:23:12 +00001738 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001739 Record.clear();
1740
Chris Lattner2199f5b2009-04-10 18:08:30 +00001741 // Emit the tokens array.
1742 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1743 // Note that we know that the preprocessor does not have any annotation
1744 // tokens in it because they are created by the parser, and thus can't be
1745 // in a macro definition.
1746 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001747
Chris Lattner2199f5b2009-04-10 18:08:30 +00001748 Record.push_back(Tok.getLocation().getRawEncoding());
1749 Record.push_back(Tok.getLength());
1750
Chris Lattner2199f5b2009-04-10 18:08:30 +00001751 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1752 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001753 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001754 // FIXME: Should translate token kind to a stable encoding.
1755 Record.push_back(Tok.getKind());
1756 // FIXME: Should translate token flags to a stable encoding.
1757 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001758
Sebastian Redl539c5062010-08-18 23:57:32 +00001759 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001760 Record.clear();
1761 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001762 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001763 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001764 Stream.ExitBlock();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001765}
1766
1767void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001768 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001769 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001770
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001771 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001772
Douglas Gregor92a96f52011-02-08 21:58:10 +00001773 // Enter the preprocessor block.
1774 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001775
Douglas Gregoraae92242010-03-19 21:51:54 +00001776 // If the preprocessor has a preprocessing record, emit it.
1777 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001778 using namespace llvm;
1779
1780 // Set up the abbreviation for
1781 unsigned InclusionAbbrev = 0;
1782 {
1783 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1784 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001785 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1786 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1787 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1789 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1790 }
1791
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001792 unsigned FirstPreprocessorEntityID
1793 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1794 + NUM_PREDEF_PP_ENTITY_IDS;
1795 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001796 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001797 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1798 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001799 E != EEnd;
1800 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001801 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001802
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001803 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1804 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001805
Douglas Gregor92a96f52011-02-08 21:58:10 +00001806 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001807 // Record this macro definition's ID.
1808 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001809
Douglas Gregor92a96f52011-02-08 21:58:10 +00001810 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001811 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1812 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001813 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001814
Chandler Carrutha88a22182011-07-14 08:20:46 +00001815 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001816 Record.push_back(ME->isBuiltinMacro());
1817 if (ME->isBuiltinMacro())
1818 AddIdentifierRef(ME->getName(), Record);
1819 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001820 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001821 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001822 continue;
1823 }
1824
1825 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1826 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001827 Record.push_back(ID->getFileName().size());
1828 Record.push_back(ID->wasInQuotes());
1829 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001830 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001831 Buffer += ID->getFileName();
1832 Buffer += ID->getFile()->getName();
1833 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1834 continue;
1835 }
1836
1837 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1838 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001839 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001840
Douglas Gregoraae92242010-03-19 21:51:54 +00001841 // Write the offsets table for the preprocessing record.
1842 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001843 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1844
Douglas Gregoraae92242010-03-19 21:51:54 +00001845 // Write the offsets table for identifier IDs.
1846 using namespace llvm;
1847 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001848 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001850 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001851 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001852
Douglas Gregoraae92242010-03-19 21:51:54 +00001853 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001854 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001855 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001856 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1857 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001858 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001859}
1860
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001861unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1862 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1863 if (Known != SubmoduleIDs.end())
1864 return Known->second;
1865
1866 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1867}
1868
Douglas Gregor253eefe2011-12-01 00:59:36 +00001869/// \brief Compute the number of modules within the given tree (including the
1870/// given module).
1871static unsigned getNumberOfModules(Module *Mod) {
1872 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001873 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1874 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001875 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001876 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001877
1878 return ChildModules + 1;
1879}
1880
Douglas Gregorde3ef502011-11-30 23:21:26 +00001881void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001882 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001883 // FIXME: This feels like it belongs somewhere else, but there are no
1884 // other consumers of this information.
1885 SourceManager &SrcMgr = PP->getSourceManager();
1886 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1887 for (ASTContext::import_iterator I = Context->local_import_begin(),
1888 IEnd = Context->local_import_end();
1889 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001890 if (Module *ImportedFrom
1891 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1892 SrcMgr))) {
1893 ImportedFrom->Imports.push_back(I->getImportedModule());
1894 }
1895 }
1896
Douglas Gregor69021972011-11-30 17:33:56 +00001897 // Enter the submodule description block.
1898 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1899
1900 // Write the abbreviations needed for the submodules block.
1901 using namespace llvm;
1902 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1903 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00001908 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1909 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00001910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00001911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00001912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1913 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1914
1915 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001916 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00001917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1918 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1919
1920 Abbrev = new BitCodeAbbrev();
1921 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1923 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00001924
1925 Abbrev = new BitCodeAbbrev();
1926 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1928 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1929
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001930 Abbrev = new BitCodeAbbrev();
1931 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1933 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1934
Douglas Gregor253eefe2011-12-01 00:59:36 +00001935 // Write the submodule metadata block.
1936 RecordData Record;
1937 Record.push_back(getNumberOfModules(WritingModule));
1938 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1939 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1940
Douglas Gregor69021972011-11-30 17:33:56 +00001941 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001942 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00001943 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00001944 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001945 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00001946 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001947 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00001948
1949 // Emit the definition of the block.
1950 Record.clear();
1951 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001952 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00001953 if (Mod->Parent) {
1954 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1955 Record.push_back(SubmoduleIDs[Mod->Parent]);
1956 } else {
1957 Record.push_back(0);
1958 }
1959 Record.push_back(Mod->IsFramework);
1960 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00001961 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00001962 Record.push_back(Mod->InferSubmodules);
1963 Record.push_back(Mod->InferExplicitSubmodules);
1964 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00001965 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1966
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001967 // Emit the requirements.
1968 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1969 Record.clear();
1970 Record.push_back(SUBMODULE_REQUIRES);
1971 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1972 Mod->Requires[I].data(),
1973 Mod->Requires[I].size());
1974 }
1975
Douglas Gregor69021972011-11-30 17:33:56 +00001976 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00001977 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00001978 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001979 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00001980 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00001981 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00001982 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1983 Record.clear();
1984 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1985 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1986 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00001987 }
1988
1989 // Emit the headers.
1990 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
1991 Record.clear();
1992 Record.push_back(SUBMODULE_HEADER);
1993 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
1994 Mod->Headers[I]->getName());
1995 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001996
1997 // Emit the imports.
1998 if (!Mod->Imports.empty()) {
1999 Record.clear();
2000 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002001 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002002 assert(ImportedID && "Unknown submodule!");
2003 Record.push_back(ImportedID);
2004 }
2005 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2006 }
2007
Douglas Gregor24bb9232011-12-02 18:58:38 +00002008 // Emit the exports.
2009 if (!Mod->Exports.empty()) {
2010 Record.clear();
2011 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002012 if (Module *Exported = Mod->Exports[I].getPointer()) {
2013 unsigned ExportedID = SubmoduleIDs[Exported];
2014 assert(ExportedID > 0 && "Unknown submodule ID?");
2015 Record.push_back(ExportedID);
2016 } else {
2017 Record.push_back(0);
2018 }
2019
Douglas Gregor24bb9232011-12-02 18:58:38 +00002020 Record.push_back(Mod->Exports[I].getInt());
2021 }
2022 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2023 }
2024
Douglas Gregor69021972011-11-30 17:33:56 +00002025 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002026 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2027 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002028 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002029 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002030 }
2031
2032 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002033
2034 assert((NextSubmoduleID - FirstSubmoduleID
2035 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002036}
2037
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002038serialization::SubmoduleID
2039ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002040 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002041 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002042
2043 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002044 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002045 Module *OwningMod
2046 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002047 if (!OwningMod)
2048 return 0;
2049
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002050 // Check whether this submodule is part of our own module.
2051 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002052 return 0;
2053
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002054 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002055}
2056
David Blaikie9c902b52011-09-25 23:23:43 +00002057void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002058 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002059 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002060 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2061 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002062 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002063 if (point.Loc.isInvalid())
2064 continue;
2065
2066 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002067 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002068 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002069 if (I->second.isPragma()) {
2070 Record.push_back(I->first);
2071 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002072 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002073 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002074 Record.push_back(-1); // mark the end of the diag/map pairs for this
2075 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002076 }
2077
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002078 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002079 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002080}
2081
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002082void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2083 if (CXXBaseSpecifiersOffsets.empty())
2084 return;
2085
2086 RecordData Record;
2087
2088 // Create a blob abbreviation for the C++ base specifiers offsets.
2089 using namespace llvm;
2090
2091 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2092 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2095 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2096
Douglas Gregorc27b2872011-08-04 00:01:48 +00002097 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002098 Record.clear();
2099 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2100 Record.push_back(CXXBaseSpecifiersOffsets.size());
2101 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002102 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002103}
2104
Douglas Gregorc5046832009-04-27 18:38:38 +00002105//===----------------------------------------------------------------------===//
2106// Type Serialization
2107//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002108
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002109/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002110void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002111 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002112 if (Idx.getIndex() == 0) // we haven't seen this type before.
2113 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002114
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002115 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002116
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002117 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002118 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002119 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002120 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002121 else if (TypeOffsets.size() < Index) {
2122 TypeOffsets.resize(Index + 1);
2123 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002124 }
2125
2126 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002127
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002128 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002129 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002130
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002131 if (T.hasLocalNonFastQualifiers()) {
2132 Qualifiers Qs = T.getLocalQualifiers();
2133 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002134 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002135 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002136 } else {
2137 switch (T->getTypeClass()) {
2138 // For all of the concrete, non-dependent types, call the
2139 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002140#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002141 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002142#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002143#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002144 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002145 }
2146
2147 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002148 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002149
2150 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002151 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002152}
2153
Douglas Gregorc5046832009-04-27 18:38:38 +00002154//===----------------------------------------------------------------------===//
2155// Declaration Serialization
2156//===----------------------------------------------------------------------===//
2157
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002158/// \brief Write the block containing all of the declaration IDs
2159/// lexically declared within the given DeclContext.
2160///
2161/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2162/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002163uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002164 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002165 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002166 return 0;
2167
Douglas Gregor8f45df52009-04-16 22:23:12 +00002168 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002169 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002171 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002172 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2173 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002174 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002175
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002176 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002177 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002178 return Offset;
2179}
2180
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002181void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002182 using namespace llvm;
2183 RecordData Record;
2184
2185 // Write the type offsets array
2186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002187 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2191 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2192 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002193 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002194 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002195 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002196 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002197
2198 // Write the declaration offsets array
2199 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002200 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2204 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2205 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002206 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002207 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002208 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002209 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002210}
2211
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002212void ASTWriter::WriteFileDeclIDsMap() {
2213 using namespace llvm;
2214 RecordData Record;
2215
2216 // Join the vectors of DeclIDs from all files.
2217 SmallVector<DeclID, 256> FileSortedIDs;
2218 for (FileDeclIDsTy::iterator
2219 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2220 DeclIDInFileInfo &Info = *FI->second;
2221 Info.FirstDeclIndex = FileSortedIDs.size();
2222 for (LocDeclIDsTy::iterator
2223 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2224 FileSortedIDs.push_back(DI->second);
2225 }
2226
2227 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2228 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2230 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2231 Record.push_back(FILE_SORTED_DECLS);
2232 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2233}
2234
Douglas Gregorc5046832009-04-27 18:38:38 +00002235//===----------------------------------------------------------------------===//
2236// Global Method Pool and Selector Serialization
2237//===----------------------------------------------------------------------===//
2238
Douglas Gregore84a9da2009-04-20 20:36:09 +00002239namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002240// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002241class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002242 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002243
2244public:
2245 typedef Selector key_type;
2246 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002247
Sebastian Redl834bb972010-08-04 17:20:04 +00002248 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002249 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002250 ObjCMethodList Instance, Factory;
2251 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002252 typedef const data_type& data_type_ref;
2253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002254 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002255
Douglas Gregorc78d3462009-04-24 21:10:55 +00002256 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002257 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002258 }
Mike Stump11289f42009-09-09 15:08:12 +00002259
2260 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002261 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002262 data_type_ref Methods) {
2263 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2264 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002265 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2266 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002267 Method = Method->Next)
2268 if (Method->Method)
2269 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002270 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002271 Method = Method->Next)
2272 if (Method->Method)
2273 DataLen += 4;
2274 clang::io::Emit16(Out, DataLen);
2275 return std::make_pair(KeyLen, DataLen);
2276 }
Mike Stump11289f42009-09-09 15:08:12 +00002277
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002278 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002279 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002280 assert((Start >> 32) == 0 && "Selector key offset too large");
2281 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002282 unsigned N = Sel.getNumArgs();
2283 clang::io::Emit16(Out, N);
2284 if (N == 0)
2285 N = 1;
2286 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002287 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002288 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2289 }
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002291 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002292 data_type_ref Methods, unsigned DataLen) {
2293 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002294 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002295 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002296 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002297 Method = Method->Next)
2298 if (Method->Method)
2299 ++NumInstanceMethods;
2300
2301 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002302 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002303 Method = Method->Next)
2304 if (Method->Method)
2305 ++NumFactoryMethods;
2306
2307 clang::io::Emit16(Out, NumInstanceMethods);
2308 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002309 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002310 Method = Method->Next)
2311 if (Method->Method)
2312 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002313 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002314 Method = Method->Next)
2315 if (Method->Method)
2316 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002317
2318 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002319 }
2320};
2321} // end anonymous namespace
2322
Sebastian Redla19a67f2010-08-03 21:58:15 +00002323/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002324///
2325/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002326/// in an on-disk hash table indexed by the selector. The hash table also
2327/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002328void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002329 using namespace llvm;
2330
Sebastian Redla19a67f2010-08-03 21:58:15 +00002331 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002332 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002333 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002334 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002335 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002336 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002337 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002338 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002339
Sebastian Redla19a67f2010-08-03 21:58:15 +00002340 // Create the on-disk hash table representation. We walk through every
2341 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002342 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002343 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002344 I = SelectorIDs.begin(), E = SelectorIDs.end();
2345 I != E; ++I) {
2346 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002347 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002348 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002349 I->second,
2350 ObjCMethodList(),
2351 ObjCMethodList()
2352 };
2353 if (F != SemaRef.MethodPool.end()) {
2354 Data.Instance = F->second.first;
2355 Data.Factory = F->second.second;
2356 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002357 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002358 // changed.
2359 if (Chain && I->second < FirstSelectorID) {
2360 // Selector already exists. Did it change?
2361 bool changed = false;
2362 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2363 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002364 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002365 changed = true;
2366 }
2367 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2368 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002369 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002370 changed = true;
2371 }
2372 if (!changed)
2373 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002374 } else if (Data.Instance.Method || Data.Factory.Method) {
2375 // A new method pool entry.
2376 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002377 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002378 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002379 }
2380
Douglas Gregorc78d3462009-04-24 21:10:55 +00002381 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002382 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002383 uint32_t BucketOffset;
2384 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002385 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002386 llvm::raw_svector_ostream Out(MethodPool);
2387 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002388 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002389 BucketOffset = Generator.Emit(Out, Trait);
2390 }
2391
2392 // Create a blob abbreviation
2393 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002394 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002395 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002396 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002397 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2398 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2399
Douglas Gregor95c13f52009-04-25 17:48:32 +00002400 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002401 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002402 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002403 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002404 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002405 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002406
2407 // Create a blob abbreviation for the selector table offsets.
2408 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002409 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002410 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2413 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2414
2415 // Write the selector offsets table.
2416 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002417 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002418 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002419 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002420 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002421 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002422 }
2423}
2424
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002425/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002426void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002427 using namespace llvm;
2428 if (SemaRef.ReferencedSelectors.empty())
2429 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002430
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002431 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002432
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002433 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002434 // very tricky to fix, and given that @selector shouldn't really appear in
2435 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002436 for (DenseMap<Selector, SourceLocation>::iterator S =
2437 SemaRef.ReferencedSelectors.begin(),
2438 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2439 Selector Sel = (*S).first;
2440 SourceLocation Loc = (*S).second;
2441 AddSelectorRef(Sel, Record);
2442 AddSourceLocation(Loc, Record);
2443 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002444 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002445}
2446
Douglas Gregorc5046832009-04-27 18:38:38 +00002447//===----------------------------------------------------------------------===//
2448// Identifier Table Serialization
2449//===----------------------------------------------------------------------===//
2450
Douglas Gregorc78d3462009-04-24 21:10:55 +00002451namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002452class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002453 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002454 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002455 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002456 bool IsModule;
2457
Douglas Gregor1d583f22009-04-28 21:18:29 +00002458 /// \brief Determines whether this is an "interesting" identifier
2459 /// that needs a full IdentifierInfo structure written into the hash
2460 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002461 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002462 if (II->isPoisoned() ||
2463 II->isExtensionToken() ||
2464 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002465 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002466 II->getFETokenInfo<void>())
2467 return true;
2468
Douglas Gregord7910e92011-09-14 22:14:14 +00002469 return hasMacroDefinition(II, Macro);
2470 }
2471
2472 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002473 if (!II->hasMacroDefinition())
2474 return false;
2475
Douglas Gregord7910e92011-09-14 22:14:14 +00002476 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002477 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002478
Douglas Gregord7910e92011-09-14 22:14:14 +00002479 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002480 }
2481
Douglas Gregore84a9da2009-04-20 20:36:09 +00002482public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002483 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002484 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002485
Sebastian Redl539c5062010-08-18 23:57:32 +00002486 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002487 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002488
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002489 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2490 IdentifierResolver &IdResolver, bool IsModule)
2491 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002492
2493 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002494 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002495 }
Mike Stump11289f42009-09-09 15:08:12 +00002496
2497 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002498 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002499 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002500 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002501 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002502 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002503 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregord7910e92011-09-14 22:14:14 +00002504 if (hasMacroDefinition(II, Macro))
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002505 DataLen += 8;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002506
2507 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2508 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002509 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002510 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002511 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002512 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002513 // We emit the key length after the data length so that every
2514 // string is preceded by a 16-bit length. This matches the PTH
2515 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002516 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002517 return std::make_pair(KeyLen, DataLen);
2518 }
Mike Stump11289f42009-09-09 15:08:12 +00002519
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002520 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002521 unsigned KeyLen) {
2522 // Record the location of the key data. This is used when generating
2523 // the mapping from persistent IDs to strings.
2524 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002525 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002526 }
Mike Stump11289f42009-09-09 15:08:12 +00002527
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002528 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002529 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002530 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002531 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002532 clang::io::Emit32(Out, ID << 1);
2533 return;
2534 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002535
Douglas Gregor1d583f22009-04-28 21:18:29 +00002536 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002537 uint32_t Bits = 0;
Douglas Gregord7910e92011-09-14 22:14:14 +00002538 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregorb9256522009-04-28 21:32:13 +00002539 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topperdec792e2011-12-19 05:04:33 +00002540 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregord7910e92011-09-14 22:14:14 +00002541 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002542 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2543 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002544 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002545 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002546 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002547
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002548 if (HasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002549 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002550 clang::io::Emit32(Out,
2551 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2552 }
2553
Douglas Gregora868bbd2009-04-21 22:25:48 +00002554 // Emit the declaration IDs in reverse order, because the
2555 // IdentifierResolver provides the declarations as they would be
2556 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002557 // "stat"), but the ASTReader adds declarations to the end of the list
2558 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002559 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002560 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2561 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002562 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002563 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002564 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002565 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002566 }
2567};
2568} // end anonymous namespace
2569
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002570/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002571///
2572/// The identifier table consists of a blob containing string data
2573/// (the actual identifiers themselves) and a separate "offsets" index
2574/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002575void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2576 IdentifierResolver &IdResolver,
2577 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002578 using namespace llvm;
2579
2580 // Create and write out the blob that contains the identifier
2581 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002582 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002583 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002584 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002585
Douglas Gregore6648fb2009-04-28 20:33:11 +00002586 // Look for any identifiers that were named while processing the
2587 // headers, but are otherwise not needed. We add these to the hash
2588 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002589 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002590 // file.
2591 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2592 IDEnd = PP.getIdentifierTable().end();
2593 ID != IDEnd; ++ID)
2594 getIdentifierRef(ID->second);
2595
Sebastian Redlff4a2952010-07-23 23:49:55 +00002596 // Create the on-disk hash table representation. We only store offsets
2597 // for identifiers that appear here for the first time.
2598 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002599 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002600 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2601 ID != IDEnd; ++ID) {
2602 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002603 if (!Chain || !ID->first->isFromAST() ||
2604 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002605 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2606 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002607 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002608
Douglas Gregore84a9da2009-04-20 20:36:09 +00002609 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002610 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002611 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002612 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002613 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002614 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002615 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002616 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002617 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002618 }
2619
2620 // Create a blob abbreviation
2621 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002622 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002623 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002625 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002626
2627 // Write the identifier table
2628 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002629 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002630 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002631 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002632 }
2633
2634 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002635 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002636 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002639 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2640 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2641
2642 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002643 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002644 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002645 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002646 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002647 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002648}
2649
Douglas Gregorc5046832009-04-27 18:38:38 +00002650//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002651// DeclContext's Name Lookup Table Serialization
2652//===----------------------------------------------------------------------===//
2653
2654namespace {
2655// Trait used for the on-disk hash table used in the method pool.
2656class ASTDeclContextNameLookupTrait {
2657 ASTWriter &Writer;
2658
2659public:
2660 typedef DeclarationName key_type;
2661 typedef key_type key_type_ref;
2662
2663 typedef DeclContext::lookup_result data_type;
2664 typedef const data_type& data_type_ref;
2665
2666 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2667
2668 unsigned ComputeHash(DeclarationName Name) {
2669 llvm::FoldingSetNodeID ID;
2670 ID.AddInteger(Name.getNameKind());
2671
2672 switch (Name.getNameKind()) {
2673 case DeclarationName::Identifier:
2674 ID.AddString(Name.getAsIdentifierInfo()->getName());
2675 break;
2676 case DeclarationName::ObjCZeroArgSelector:
2677 case DeclarationName::ObjCOneArgSelector:
2678 case DeclarationName::ObjCMultiArgSelector:
2679 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2680 break;
2681 case DeclarationName::CXXConstructorName:
2682 case DeclarationName::CXXDestructorName:
2683 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002684 break;
2685 case DeclarationName::CXXOperatorName:
2686 ID.AddInteger(Name.getCXXOverloadedOperator());
2687 break;
2688 case DeclarationName::CXXLiteralOperatorName:
2689 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2690 case DeclarationName::CXXUsingDirective:
2691 break;
2692 }
2693
2694 return ID.ComputeHash();
2695 }
2696
2697 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002698 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002699 data_type_ref Lookup) {
2700 unsigned KeyLen = 1;
2701 switch (Name.getNameKind()) {
2702 case DeclarationName::Identifier:
2703 case DeclarationName::ObjCZeroArgSelector:
2704 case DeclarationName::ObjCOneArgSelector:
2705 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002706 case DeclarationName::CXXLiteralOperatorName:
2707 KeyLen += 4;
2708 break;
2709 case DeclarationName::CXXOperatorName:
2710 KeyLen += 1;
2711 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002712 case DeclarationName::CXXConstructorName:
2713 case DeclarationName::CXXDestructorName:
2714 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002715 case DeclarationName::CXXUsingDirective:
2716 break;
2717 }
2718 clang::io::Emit16(Out, KeyLen);
2719
2720 // 2 bytes for num of decls and 4 for each DeclID.
2721 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2722 clang::io::Emit16(Out, DataLen);
2723
2724 return std::make_pair(KeyLen, DataLen);
2725 }
2726
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002727 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002728 using namespace clang::io;
2729
2730 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2731 Emit8(Out, Name.getNameKind());
2732 switch (Name.getNameKind()) {
2733 case DeclarationName::Identifier:
2734 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2735 break;
2736 case DeclarationName::ObjCZeroArgSelector:
2737 case DeclarationName::ObjCOneArgSelector:
2738 case DeclarationName::ObjCMultiArgSelector:
2739 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2740 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002741 case DeclarationName::CXXOperatorName:
2742 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2743 Emit8(Out, Name.getCXXOverloadedOperator());
2744 break;
2745 case DeclarationName::CXXLiteralOperatorName:
2746 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2747 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002748 case DeclarationName::CXXConstructorName:
2749 case DeclarationName::CXXDestructorName:
2750 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002751 case DeclarationName::CXXUsingDirective:
2752 break;
2753 }
2754 }
2755
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002756 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002757 data_type Lookup, unsigned DataLen) {
2758 uint64_t Start = Out.tell(); (void)Start;
2759 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2760 for (; Lookup.first != Lookup.second; ++Lookup.first)
2761 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2762
2763 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2764 }
2765};
2766} // end anonymous namespace
2767
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002768/// \brief Write the block containing all of the declaration IDs
2769/// visible from the given DeclContext.
2770///
2771/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002772/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002773uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2774 DeclContext *DC) {
2775 if (DC->getPrimaryContext() != DC)
2776 return 0;
2777
2778 // Since there is no name lookup into functions or methods, don't bother to
2779 // build a visible-declarations table for these entities.
2780 if (DC->isFunctionOrMethod())
2781 return 0;
2782
2783 // If not in C++, we perform name lookup for the translation unit via the
2784 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2785 // FIXME: In C++ we need the visible declarations in order to "see" the
2786 // friend declarations, is there a way to do this without writing the table ?
2787 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2788 return 0;
2789
2790 // Force the DeclContext to build a its name-lookup table.
Douglas Gregora3e59b42011-08-24 21:56:08 +00002791 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002792 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002793
2794 // Serialize the contents of the mapping used for lookup. Note that,
2795 // although we have two very different code paths, the serialized
2796 // representation is the same for both cases: a declaration name,
2797 // followed by a size, followed by references to the visible
2798 // declarations that have that name.
2799 uint64_t Offset = Stream.GetCurrentBitNo();
2800 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2801 if (!Map || Map->empty())
2802 return 0;
2803
2804 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2805 ASTDeclContextNameLookupTrait Trait(*this);
2806
2807 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002808 DeclarationName ConversionName;
2809 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002810 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2811 D != DEnd; ++D) {
2812 DeclarationName Name = D->first;
2813 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002814 if (Result.first != Result.second) {
2815 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2816 // Hash all conversion function names to the same name. The actual
2817 // type information in conversion function name is not used in the
2818 // key (since such type information is not stable across different
2819 // modules), so the intended effect is to coalesce all of the conversion
2820 // functions under a single key.
2821 if (!ConversionName)
2822 ConversionName = Name;
2823 ConversionDecls.append(Result.first, Result.second);
2824 continue;
2825 }
2826
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002827 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002828 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002829 }
2830
Douglas Gregor05ef9312011-08-30 20:49:19 +00002831 // Add the conversion functions
2832 if (!ConversionDecls.empty()) {
2833 Generator.insert(ConversionName,
2834 DeclContext::lookup_result(ConversionDecls.begin(),
2835 ConversionDecls.end()),
2836 Trait);
2837 }
2838
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002839 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002840 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002841 uint32_t BucketOffset;
2842 {
2843 llvm::raw_svector_ostream Out(LookupTable);
2844 // Make sure that no bucket is at offset 0
2845 clang::io::Emit32(Out, 0);
2846 BucketOffset = Generator.Emit(Out, Trait);
2847 }
2848
2849 // Write the lookup table
2850 RecordData Record;
2851 Record.push_back(DECL_CONTEXT_VISIBLE);
2852 Record.push_back(BucketOffset);
2853 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2854 LookupTable.str());
2855
2856 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2857 ++NumVisibleDeclContexts;
2858 return Offset;
2859}
2860
Sebastian Redla4071b42010-08-24 00:50:09 +00002861/// \brief Write an UPDATE_VISIBLE block for the given context.
2862///
2863/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2864/// DeclContext in a dependent AST file. As such, they only exist for the TU
2865/// (in C++) and for namespaces.
2866void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002867 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2868 if (!Map || Map->empty())
2869 return;
2870
2871 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2872 ASTDeclContextNameLookupTrait Trait(*this);
2873
2874 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002875 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2876 D != DEnd; ++D) {
2877 DeclarationName Name = D->first;
2878 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002879 // For any name that appears in this table, the results are complete, i.e.
2880 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002881 if (Result.first != Result.second)
2882 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002883 }
2884
2885 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002886 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00002887 uint32_t BucketOffset;
2888 {
2889 llvm::raw_svector_ostream Out(LookupTable);
2890 // Make sure that no bucket is at offset 0
2891 clang::io::Emit32(Out, 0);
2892 BucketOffset = Generator.Emit(Out, Trait);
2893 }
2894
2895 // Write the lookup table
2896 RecordData Record;
2897 Record.push_back(UPDATE_VISIBLE);
2898 Record.push_back(getDeclID(cast<Decl>(DC)));
2899 Record.push_back(BucketOffset);
2900 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2901}
2902
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002903/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2904void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2905 RecordData Record;
2906 Record.push_back(Opts.fp_contract);
2907 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2908}
2909
2910/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2911void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2912 if (!SemaRef.Context.getLangOptions().OpenCL)
2913 return;
2914
2915 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2916 RecordData Record;
2917#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2918#include "clang/Basic/OpenCLExtensions.def"
2919 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2920}
2921
Douglas Gregor358cd442012-01-15 16:58:34 +00002922void ASTWriter::WriteRedeclarations() {
2923 RecordData LocalRedeclChains;
2924 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2925
2926 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2927 Decl *First = Redeclarations[I];
2928 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2929
2930 Decl *MostRecent = First->getMostRecentDecl();
2931
2932 // If we only have a single declaration, there is no point in storing
2933 // a redeclaration chain.
2934 if (First == MostRecent)
2935 continue;
2936
2937 unsigned Offset = LocalRedeclChains.size();
2938 unsigned Size = 0;
2939 LocalRedeclChains.push_back(0); // Placeholder for the size.
2940
2941 // Collect the set of local redeclarations of this declaration.
2942 for (Decl *Prev = MostRecent; Prev != First;
2943 Prev = Prev->getPreviousDecl()) {
2944 if (!Prev->isFromASTFile()) {
2945 AddDeclRef(Prev, LocalRedeclChains);
2946 ++Size;
2947 }
2948 }
2949 LocalRedeclChains[Offset] = Size;
2950
2951 // Reverse the set of local redeclarations, so that we store them in
2952 // order (since we found them in reverse order).
2953 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2954
2955 // Add the mapping from the first ID to the set of local declarations.
2956 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2957 LocalRedeclsMap.push_back(Info);
2958
2959 assert(N == Redeclarations.size() &&
2960 "Deserialized a declaration we shouldn't have");
2961 }
2962
2963 if (LocalRedeclChains.empty())
2964 return;
2965
2966 // Sort the local redeclarations map by the first declaration ID,
2967 // since the reader will be performing binary searches on this information.
2968 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2969
2970 // Emit the local redeclarations map.
2971 using namespace llvm;
2972 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2973 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2976 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
2977
2978 RecordData Record;
2979 Record.push_back(LOCAL_REDECLARATIONS_MAP);
2980 Record.push_back(LocalRedeclsMap.size());
2981 Stream.EmitRecordWithBlob(AbbrevID, Record,
2982 reinterpret_cast<char*>(LocalRedeclsMap.data()),
2983 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
2984
2985 // Emit the redeclaration chains.
2986 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
2987}
2988
Douglas Gregor404cdde2012-01-27 01:47:08 +00002989void ASTWriter::WriteObjCCategories() {
2990 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
2991 RecordData Categories;
2992
2993 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
2994 unsigned Size = 0;
2995 unsigned StartIndex = Categories.size();
2996
2997 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
2998
2999 // Allocate space for the size.
3000 Categories.push_back(0);
3001
3002 // Add the categories.
3003 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3004 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3005 assert(getDeclID(Cat) != 0 && "Bogus category");
3006 AddDeclRef(Cat, Categories);
3007 }
3008
3009 // Update the size.
3010 Categories[StartIndex] = Size;
3011
3012 // Record this interface -> category map.
3013 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3014 CategoriesMap.push_back(CatInfo);
3015 }
3016
3017 // Sort the categories map by the definition ID, since the reader will be
3018 // performing binary searches on this information.
3019 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3020
3021 // Emit the categories map.
3022 using namespace llvm;
3023 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3024 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3027 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3028
3029 RecordData Record;
3030 Record.push_back(OBJC_CATEGORIES_MAP);
3031 Record.push_back(CategoriesMap.size());
3032 Stream.EmitRecordWithBlob(AbbrevID, Record,
3033 reinterpret_cast<char*>(CategoriesMap.data()),
3034 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3035
3036 // Emit the category lists.
3037 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3038}
3039
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003040void ASTWriter::WriteMergedDecls() {
3041 if (!Chain || Chain->MergedDecls.empty())
3042 return;
3043
3044 RecordData Record;
3045 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3046 IEnd = Chain->MergedDecls.end();
3047 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003048 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003049 : getDeclID(I->first);
3050 assert(CanonID && "Merged declaration not known?");
3051
3052 Record.push_back(CanonID);
3053 Record.push_back(I->second.size());
3054 Record.append(I->second.begin(), I->second.end());
3055 }
3056 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3057}
3058
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003059//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003060// General Serialization Routines
3061//===----------------------------------------------------------------------===//
3062
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003063/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003064void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003065 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003066 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
3067 const Attr * A = *i;
3068 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003069 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003070
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003071#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003072
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003073 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003074}
3075
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003076void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003077 Record.push_back(Str.size());
3078 Record.insert(Record.end(), Str.begin(), Str.end());
3079}
3080
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003081void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3082 RecordDataImpl &Record) {
3083 Record.push_back(Version.getMajor());
3084 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3085 Record.push_back(*Minor + 1);
3086 else
3087 Record.push_back(0);
3088 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3089 Record.push_back(*Subminor + 1);
3090 else
3091 Record.push_back(0);
3092}
3093
Douglas Gregore84a9da2009-04-20 20:36:09 +00003094/// \brief Note that the identifier II occurs at the given offset
3095/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003096void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003097 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003098 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003099 // up earlier in the chain and thus don't need an offset.
3100 if (ID >= FirstIdentID)
3101 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003102}
3103
Douglas Gregor95c13f52009-04-25 17:48:32 +00003104/// \brief Note that the selector Sel occurs at the given offset
3105/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003106void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003107 unsigned ID = SelectorIDs[Sel];
3108 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003109 // Don't record offsets for selectors that are also available in a different
3110 // file.
3111 if (ID < FirstSelectorID)
3112 return;
3113 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003114}
3115
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003116ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003117 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
3118 WritingAST(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003119 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003120 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003121 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003122 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3123 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003124 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003125 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003126 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003127 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003128 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003129 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003130 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3131 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3132 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003133 DeclTypedefAbbrev(0),
3134 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3135 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003136{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003137}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003138
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003139ASTWriter::~ASTWriter() {
3140 for (FileDeclIDsTy::iterator
3141 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3142 delete I->second;
3143}
3144
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003145void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003146 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003147 Module *WritingModule, StringRef isysroot) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003148 WritingAST = true;
3149
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003150 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003151 Stream.Emit((unsigned)'C', 8);
3152 Stream.Emit((unsigned)'P', 8);
3153 Stream.Emit((unsigned)'C', 8);
3154 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003155
Chris Lattner28fa4e62009-04-26 22:26:21 +00003156 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003157
Douglas Gregoreda8e122011-08-09 15:13:55 +00003158 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003159 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003160 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003161 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003162 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003163 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003164 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003165
3166 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003167}
3168
Douglas Gregora94a1542011-07-27 21:45:57 +00003169template<typename Vector>
3170static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3171 ASTWriter::RecordData &Record) {
3172 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3173 I != E; ++I) {
3174 Writer.AddDeclRef(*I, Record);
3175 }
3176}
3177
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003178void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003179 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003180 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003181 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003182 using namespace llvm;
3183
Douglas Gregorcf68c582011-12-01 22:20:10 +00003184 // Make sure that the AST reader knows to finalize itself.
3185 if (Chain)
3186 Chain->finalizeForWriting();
3187
Sebastian Redl143413f2010-07-12 22:02:52 +00003188 ASTContext &Context = SemaRef.Context;
3189 Preprocessor &PP = SemaRef.PP;
3190
Douglas Gregordab42432011-08-12 00:15:20 +00003191 // Set up predefined declaration IDs.
3192 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003193 if (Context.ObjCIdDecl)
3194 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003195 if (Context.ObjCSelDecl)
3196 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003197 if (Context.ObjCClassDecl)
3198 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003199 if (Context.ObjCProtocolClassDecl)
3200 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003201 if (Context.Int128Decl)
3202 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3203 if (Context.UInt128Decl)
3204 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003205 if (Context.ObjCInstanceTypeDecl)
3206 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003207
Douglas Gregor851443c2011-08-12 01:39:19 +00003208 if (!Chain) {
3209 // Make sure that we emit IdentifierInfos (and any attached
3210 // declarations) for builtins. We don't need to do this when we're
3211 // emitting chained PCH files, because all of the builtins will be
3212 // in the original PCH file.
3213 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003214 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003215 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003216 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
3217 Context.getLangOptions().NoBuiltin);
3218 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3219 getIdentifierRef(&Table.get(BuiltinNames[I]));
3220 }
3221
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003222 // If there are any out-of-date identifiers, bring them up to date.
3223 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3224 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3225 IDEnd = PP.getIdentifierTable().end();
3226 ID != IDEnd; ++ID)
3227 if (ID->second->isOutOfDate())
3228 ExtSource->updateOutOfDateIdentifier(*ID->second);
3229 }
3230
Chris Lattner0c797362009-09-08 18:19:27 +00003231 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003232 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003233 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003234 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003235 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003236
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003237 // Build a record containing all of the file scoped decls in this file.
3238 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003239 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3240 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003241
Douglas Gregor851443c2011-08-12 01:39:19 +00003242 // Build a record containing all of the delegating constructors we still need
3243 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003244 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003245 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003246
Douglas Gregor851443c2011-08-12 01:39:19 +00003247 // Write the set of weak, undeclared identifiers. We always write the
3248 // entire table, since later PCH files in a PCH chain are only interested in
3249 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003250 RecordData WeakUndeclaredIdentifiers;
3251 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003252 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003253 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3254 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3255 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3256 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3257 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3258 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3259 }
3260 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003261
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003262 // Build a record containing all of the locally-scoped external
3263 // declarations in this header file. Generally, this record will be
3264 // empty.
3265 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003266 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003267 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003268 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003269 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3270 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003271 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003272 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003273 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3274 }
3275
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003276 // Build a record containing all of the ext_vector declarations.
3277 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003278 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003279
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003280 // Build a record containing all of the VTable uses information.
3281 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003282 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003283 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3284 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3285 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3286 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3287 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003288 }
3289
3290 // Build a record containing all of dynamic classes declarations.
3291 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003292 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003293
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003294 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003295 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003296 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003297 I = SemaRef.PendingInstantiations.begin(),
3298 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3299 AddDeclRef(I->first, PendingInstantiations);
3300 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003301 }
3302 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3303 "There are local ones at end of translation unit!");
3304
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003305 // Build a record containing some declaration references.
3306 RecordData SemaDeclRefs;
3307 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3308 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3309 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3310 }
3311
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003312 RecordData CUDASpecialDeclRefs;
3313 if (Context.getcudaConfigureCallDecl()) {
3314 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3315 }
3316
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003317 // Build a record containing all of the known namespaces.
3318 RecordData KnownNamespaces;
3319 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3320 I = SemaRef.KnownNamespaces.begin(),
3321 IEnd = SemaRef.KnownNamespaces.end();
3322 I != IEnd; ++I) {
3323 if (!I->second)
3324 AddDeclRef(I->first, KnownNamespaces);
3325 }
3326
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003327 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003328 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003329 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003330 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00003331 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorc567ba22011-07-22 16:35:34 +00003332 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003333 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003334
3335 // Create a lexical update block containing all of the declarations in the
3336 // translation unit that do not come from other AST files.
3337 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3338 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3339 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3340 E = TU->noload_decls_end();
3341 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003342 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003343 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003344 }
3345
3346 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3347 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3348 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3349 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3350 Record.clear();
3351 Record.push_back(TU_UPDATE_LEXICAL);
3352 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3353 data(NewGlobalDecls));
3354
3355 // And a visible updates block for the translation unit.
3356 Abv = new llvm::BitCodeAbbrev();
3357 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3358 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3359 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3360 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3361 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3362 WriteDeclContextVisibleUpdate(TU);
3363
3364 // If the translation unit has an anonymous namespace, and we don't already
3365 // have an update block for it, write it as an update block.
3366 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3367 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3368 if (Record.empty()) {
3369 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003370 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003371 }
3372 }
3373
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003374 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003375 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003376
Douglas Gregor5204bde2011-08-02 16:26:37 +00003377 // Form the record of special types.
3378 RecordData SpecialTypes;
3379 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003380 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003381 AddTypeRef(Context.getFILEType(), SpecialTypes);
3382 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3383 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3384 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3385 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003386 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003387 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003388
Douglas Gregor1970d882009-04-26 03:49:13 +00003389 // Keep writing types and declarations until all types and
3390 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003391 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003392 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003393 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3394 E = DeclsToRewrite.end();
3395 I != E; ++I)
3396 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003397 while (!DeclTypesToEmit.empty()) {
3398 DeclOrType DOT = DeclTypesToEmit.front();
3399 DeclTypesToEmit.pop();
3400 if (DOT.isType())
3401 WriteType(DOT.getType());
3402 else
3403 WriteDecl(Context, DOT.getDecl());
3404 }
3405 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003406
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003407 WriteFileDeclIDsMap();
3408 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
3409
3410 if (Chain) {
3411 // Write the mapping information describing our module dependencies and how
3412 // each of those modules were mapped into our own offset/ID space, so that
3413 // the reader can build the appropriate mapping to its own offset/ID space.
3414 // The map consists solely of a blob with the following format:
3415 // *(module-name-len:i16 module-name:len*i8
3416 // source-location-offset:i32
3417 // identifier-id:i32
3418 // preprocessed-entity-id:i32
3419 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003420 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003421 // selector-id:i32
3422 // declaration-id:i32
3423 // c++-base-specifiers-id:i32
3424 // type-id:i32)
3425 //
3426 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3427 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3429 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003430 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003431 {
3432 llvm::raw_svector_ostream Out(Buffer);
3433 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003434 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003435 M != MEnd; ++M) {
3436 StringRef FileName = (*M)->FileName;
3437 io::Emit16(Out, FileName.size());
3438 Out.write(FileName.data(), FileName.size());
3439 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3440 io::Emit32(Out, (*M)->BaseIdentifierID);
3441 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003442 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003443 io::Emit32(Out, (*M)->BaseSelectorID);
3444 io::Emit32(Out, (*M)->BaseDeclID);
3445 io::Emit32(Out, (*M)->BaseTypeIndex);
3446 }
3447 }
3448 Record.clear();
3449 Record.push_back(MODULE_OFFSET_MAP);
3450 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3451 Buffer.data(), Buffer.size());
3452 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003453 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003454 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003455 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003456 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003457 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003458 WriteFPPragmaOptions(SemaRef.getFPOptions());
3459 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003460
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003461 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003462 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003463
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003464 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003465
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003466 // If we're emitting a module, write out the submodule information.
3467 if (WritingModule)
3468 WriteSubmodules(WritingModule);
3469
Douglas Gregor5204bde2011-08-02 16:26:37 +00003470 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3471
Douglas Gregord4df8652009-04-22 22:02:47 +00003472 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003473 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003474 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003475
3476 // Write the record containing tentative definitions.
3477 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003478 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003479
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003480 // Write the record containing unused file scoped decls.
3481 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003482 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003483
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003484 // Write the record containing weak undeclared identifiers.
3485 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003486 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003487 WeakUndeclaredIdentifiers);
3488
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003489 // Write the record containing locally-scoped external definitions.
3490 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003491 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003492 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003493
3494 // Write the record containing ext_vector type names.
3495 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003496 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003497
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003498 // Write the record containing VTable uses information.
3499 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003500 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003501
3502 // Write the record containing dynamic classes declarations.
3503 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003504 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003505
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003506 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003507 if (!PendingInstantiations.empty())
3508 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003509
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003510 // Write the record containing declaration references of Sema.
3511 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003512 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003513
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003514 // Write the record containing CUDA-specific declaration references.
3515 if (!CUDASpecialDeclRefs.empty())
3516 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003517
3518 // Write the delegating constructors.
3519 if (!DelegatingCtorDecls.empty())
3520 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003521
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003522 // Write the known namespaces.
3523 if (!KnownNamespaces.empty())
3524 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3525
Douglas Gregor851443c2011-08-12 01:39:19 +00003526 // Write the visible updates to DeclContexts.
3527 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3528 I = UpdatedDeclContexts.begin(),
3529 E = UpdatedDeclContexts.end();
3530 I != E; ++I)
3531 WriteDeclContextVisibleUpdate(*I);
3532
Douglas Gregor959bb062011-12-03 01:15:29 +00003533 if (!WritingModule) {
3534 // Write the submodules that were imported, if any.
3535 RecordData ImportedModules;
3536 for (ASTContext::import_iterator I = Context.local_import_begin(),
3537 IEnd = Context.local_import_end();
3538 I != IEnd; ++I) {
3539 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3540 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3541 }
3542 if (!ImportedModules.empty()) {
3543 // Sort module IDs.
3544 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3545
3546 // Unique module IDs.
3547 ImportedModules.erase(std::unique(ImportedModules.begin(),
3548 ImportedModules.end()),
3549 ImportedModules.end());
3550
3551 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3552 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003553 }
3554
Douglas Gregordab42432011-08-12 00:15:20 +00003555 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003556 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003557 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003558 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003559 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003560
Douglas Gregor08f01292009-04-17 22:13:46 +00003561 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003562 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003563 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003564 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003565 Record.push_back(NumLexicalDeclContexts);
3566 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003567 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003568 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003569}
3570
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003571/// \brief Go through the declaration update blocks and resolve declaration
3572/// pointers into declaration IDs.
3573void ASTWriter::ResolveDeclUpdatesBlocks() {
3574 for (DeclUpdateMap::iterator
3575 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3576 const Decl *D = I->first;
3577 UpdateRecord &URec = I->second;
3578
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003579 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003580 continue; // The decl will be written completely
3581
3582 unsigned Idx = 0, N = URec.size();
3583 while (Idx < N) {
3584 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003585 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3586 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3587 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3588 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3589 ++Idx;
3590 break;
3591
3592 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3593 ++Idx;
3594 break;
3595 }
3596 }
3597 }
3598}
3599
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003600void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003601 if (DeclUpdates.empty())
3602 return;
3603
3604 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003605 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003606 for (DeclUpdateMap::iterator
3607 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3608 const Decl *D = I->first;
3609 UpdateRecord &URec = I->second;
3610
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003611 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003612 continue; // The decl will be written completely,no need to store updates.
3613
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003614 uint64_t Offset = Stream.GetCurrentBitNo();
3615 Stream.EmitRecord(DECL_UPDATES, URec);
3616
3617 OffsetsRecord.push_back(GetDeclRef(D));
3618 OffsetsRecord.push_back(Offset);
3619 }
3620 Stream.ExitBlock();
3621 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3622}
3623
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003624void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003625 if (ReplacedDecls.empty())
3626 return;
3627
3628 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003629 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003630 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003631 Record.push_back(I->ID);
3632 Record.push_back(I->Offset);
3633 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003634 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003635 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003636}
3637
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003638void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003639 Record.push_back(Loc.getRawEncoding());
3640}
3641
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003642void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003643 AddSourceLocation(Range.getBegin(), Record);
3644 AddSourceLocation(Range.getEnd(), Record);
3645}
3646
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003647void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003648 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003649 const uint64_t *Words = Value.getRawData();
3650 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003651}
3652
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003653void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003654 Record.push_back(Value.isUnsigned());
3655 AddAPInt(Value, Record);
3656}
3657
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003658void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003659 AddAPInt(Value.bitcastToAPInt(), Record);
3660}
3661
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003662void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003663 Record.push_back(getIdentifierRef(II));
3664}
3665
Sebastian Redl539c5062010-08-18 23:57:32 +00003666IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003667 if (II == 0)
3668 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003669
Sebastian Redl539c5062010-08-18 23:57:32 +00003670 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003671 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003672 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003673 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003674}
3675
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003676void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003677 Record.push_back(getSelectorRef(SelRef));
3678}
3679
Sebastian Redl539c5062010-08-18 23:57:32 +00003680SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003681 if (Sel.getAsOpaquePtr() == 0) {
3682 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003683 }
3684
Sebastian Redl539c5062010-08-18 23:57:32 +00003685 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003686 if (SID == 0 && Chain) {
3687 // This might trigger a ReadSelector callback, which will set the ID for
3688 // this selector.
3689 Chain->LoadSelector(Sel);
3690 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003691 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003692 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003693 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003694 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003695}
3696
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003697void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003698 AddDeclRef(Temp->getDestructor(), Record);
3699}
3700
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003701void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3702 CXXBaseSpecifier const *BasesEnd,
3703 RecordDataImpl &Record) {
3704 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3705 CXXBaseSpecifiersToWrite.push_back(
3706 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3707 Bases, BasesEnd));
3708 Record.push_back(NextCXXBaseSpecifiersID++);
3709}
3710
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003711void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003712 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003713 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003714 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003715 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003716 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003717 break;
3718 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003719 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003720 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003721 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003722 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003723 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003724 break;
3725 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003726 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003727 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003728 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003729 break;
John McCall0ad16662009-10-29 08:12:44 +00003730 case TemplateArgument::Null:
3731 case TemplateArgument::Integral:
3732 case TemplateArgument::Declaration:
3733 case TemplateArgument::Pack:
3734 break;
3735 }
3736}
3737
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003738void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003739 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003740 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003741
3742 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3743 bool InfoHasSameExpr
3744 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3745 Record.push_back(InfoHasSameExpr);
3746 if (InfoHasSameExpr)
3747 return; // Avoid storing the same expr twice.
3748 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003749 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3750 Record);
3751}
3752
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003753void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3754 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003755 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003756 AddTypeRef(QualType(), Record);
3757 return;
3758 }
3759
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003760 AddTypeLoc(TInfo->getTypeLoc(), Record);
3761}
3762
3763void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3764 AddTypeRef(TL.getType(), Record);
3765
John McCall8f115c62009-10-16 21:56:05 +00003766 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003767 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003768 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003769}
3770
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003771void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003772 Record.push_back(GetOrCreateTypeID(T));
3773}
3774
Douglas Gregoreda8e122011-08-09 15:13:55 +00003775TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3776 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003777 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3778}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003779
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003780TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003781 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003782 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003783}
3784
3785TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3786 if (T.isNull())
3787 return TypeIdx();
3788 assert(!T.getLocalFastQualifiers());
3789
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003790 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003791 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003792 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003793 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003794 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003795 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003796 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003797 return Idx;
3798}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003799
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003800TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003801 if (T.isNull())
3802 return TypeIdx();
3803 assert(!T.getLocalFastQualifiers());
3804
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003805 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3806 assert(I != TypeIdxs.end() && "Type not emitted!");
3807 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003808}
3809
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003810void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003811 Record.push_back(GetDeclRef(D));
3812}
3813
Sebastian Redl539c5062010-08-18 23:57:32 +00003814DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003815 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3816
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003817 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003818 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003819 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00003820
3821 // If D comes from an AST file, its declaration ID is already known and
3822 // fixed.
3823 if (D->isFromASTFile())
3824 return D->getGlobalID();
3825
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003826 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003827 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003828 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003829 // We haven't seen this declaration before. Give it a new ID and
3830 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003831 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003832 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003833 }
3834
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003835 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003836}
3837
Sebastian Redl539c5062010-08-18 23:57:32 +00003838DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003839 if (D == 0)
3840 return 0;
3841
Douglas Gregorb3163e52012-01-05 22:33:30 +00003842 // If D comes from an AST file, its declaration ID is already known and
3843 // fixed.
3844 if (D->isFromASTFile())
3845 return D->getGlobalID();
3846
Douglas Gregore84a9da2009-04-20 20:36:09 +00003847 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3848 return DeclIDs[D];
3849}
3850
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003851static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3852 std::pair<unsigned, serialization::DeclID> R) {
3853 return L.first < R.first;
3854}
3855
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003856void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003857 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003858 assert(D);
3859
3860 SourceLocation Loc = D->getLocation();
3861 if (Loc.isInvalid())
3862 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003863
3864 // We only keep track of the file-level declarations of each file.
3865 if (!D->getLexicalDeclContext()->isFileContext())
3866 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00003867 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3868 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00003869 if (isa<ParmVarDecl>(D))
3870 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003871
3872 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003873 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003874 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003875 FileID FID;
3876 unsigned Offset;
3877 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003878 if (FID.isInvalid())
3879 return;
3880 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3881 assert(Entry->isFile());
3882
3883 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3884 if (!Info)
3885 Info = new DeclIDInFileInfo();
3886
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003887 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003888 LocDeclIDsTy &Decls = Info->DeclIDs;
3889
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003890 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003891 Decls.push_back(LocDecl);
3892 return;
3893 }
3894
3895 LocDeclIDsTy::iterator
3896 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3897
3898 Decls.insert(I, LocDecl);
3899}
3900
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003901void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003902 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003903 Record.push_back(Name.getNameKind());
3904 switch (Name.getNameKind()) {
3905 case DeclarationName::Identifier:
3906 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3907 break;
3908
3909 case DeclarationName::ObjCZeroArgSelector:
3910 case DeclarationName::ObjCOneArgSelector:
3911 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003912 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003913 break;
3914
3915 case DeclarationName::CXXConstructorName:
3916 case DeclarationName::CXXDestructorName:
3917 case DeclarationName::CXXConversionFunctionName:
3918 AddTypeRef(Name.getCXXNameType(), Record);
3919 break;
3920
3921 case DeclarationName::CXXOperatorName:
3922 Record.push_back(Name.getCXXOverloadedOperator());
3923 break;
3924
Alexis Hunt3d221f22009-11-29 07:34:05 +00003925 case DeclarationName::CXXLiteralOperatorName:
3926 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3927 break;
3928
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003929 case DeclarationName::CXXUsingDirective:
3930 // No extra data to emit
3931 break;
3932 }
3933}
Chris Lattnerca025db2010-05-07 21:43:38 +00003934
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003935void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003936 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003937 switch (Name.getNameKind()) {
3938 case DeclarationName::CXXConstructorName:
3939 case DeclarationName::CXXDestructorName:
3940 case DeclarationName::CXXConversionFunctionName:
3941 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3942 break;
3943
3944 case DeclarationName::CXXOperatorName:
3945 AddSourceLocation(
3946 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3947 Record);
3948 AddSourceLocation(
3949 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3950 Record);
3951 break;
3952
3953 case DeclarationName::CXXLiteralOperatorName:
3954 AddSourceLocation(
3955 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3956 Record);
3957 break;
3958
3959 case DeclarationName::Identifier:
3960 case DeclarationName::ObjCZeroArgSelector:
3961 case DeclarationName::ObjCOneArgSelector:
3962 case DeclarationName::ObjCMultiArgSelector:
3963 case DeclarationName::CXXUsingDirective:
3964 break;
3965 }
3966}
3967
3968void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003969 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003970 AddDeclarationName(NameInfo.getName(), Record);
3971 AddSourceLocation(NameInfo.getLoc(), Record);
3972 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3973}
3974
3975void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003976 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003977 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003978 Record.push_back(Info.NumTemplParamLists);
3979 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3980 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3981}
3982
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003983void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003984 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003985 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003986 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003987 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00003988
3989 // Push each of the NNS's onto a stack for serialization in reverse order.
3990 while (NNS) {
3991 NestedNames.push_back(NNS);
3992 NNS = NNS->getPrefix();
3993 }
3994
3995 Record.push_back(NestedNames.size());
3996 while(!NestedNames.empty()) {
3997 NNS = NestedNames.pop_back_val();
3998 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3999 Record.push_back(Kind);
4000 switch (Kind) {
4001 case NestedNameSpecifier::Identifier:
4002 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4003 break;
4004
4005 case NestedNameSpecifier::Namespace:
4006 AddDeclRef(NNS->getAsNamespace(), Record);
4007 break;
4008
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004009 case NestedNameSpecifier::NamespaceAlias:
4010 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4011 break;
4012
Chris Lattnerca025db2010-05-07 21:43:38 +00004013 case NestedNameSpecifier::TypeSpec:
4014 case NestedNameSpecifier::TypeSpecWithTemplate:
4015 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4016 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4017 break;
4018
4019 case NestedNameSpecifier::Global:
4020 // Don't need to write an associated value.
4021 break;
4022 }
4023 }
4024}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004025
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004026void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4027 RecordDataImpl &Record) {
4028 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004029 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004030 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004031
4032 // Push each of the nested-name-specifiers's onto a stack for
4033 // serialization in reverse order.
4034 while (NNS) {
4035 NestedNames.push_back(NNS);
4036 NNS = NNS.getPrefix();
4037 }
4038
4039 Record.push_back(NestedNames.size());
4040 while(!NestedNames.empty()) {
4041 NNS = NestedNames.pop_back_val();
4042 NestedNameSpecifier::SpecifierKind Kind
4043 = NNS.getNestedNameSpecifier()->getKind();
4044 Record.push_back(Kind);
4045 switch (Kind) {
4046 case NestedNameSpecifier::Identifier:
4047 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4048 AddSourceRange(NNS.getLocalSourceRange(), Record);
4049 break;
4050
4051 case NestedNameSpecifier::Namespace:
4052 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4053 AddSourceRange(NNS.getLocalSourceRange(), Record);
4054 break;
4055
4056 case NestedNameSpecifier::NamespaceAlias:
4057 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4058 AddSourceRange(NNS.getLocalSourceRange(), Record);
4059 break;
4060
4061 case NestedNameSpecifier::TypeSpec:
4062 case NestedNameSpecifier::TypeSpecWithTemplate:
4063 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4064 AddTypeLoc(NNS.getTypeLoc(), Record);
4065 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4066 break;
4067
4068 case NestedNameSpecifier::Global:
4069 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4070 break;
4071 }
4072 }
4073}
4074
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004075void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004076 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004077 Record.push_back(Kind);
4078 switch (Kind) {
4079 case TemplateName::Template:
4080 AddDeclRef(Name.getAsTemplateDecl(), Record);
4081 break;
4082
4083 case TemplateName::OverloadedTemplate: {
4084 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4085 Record.push_back(OvT->size());
4086 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4087 I != E; ++I)
4088 AddDeclRef(*I, Record);
4089 break;
4090 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004091
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004092 case TemplateName::QualifiedTemplate: {
4093 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4094 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4095 Record.push_back(QualT->hasTemplateKeyword());
4096 AddDeclRef(QualT->getTemplateDecl(), Record);
4097 break;
4098 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004099
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004100 case TemplateName::DependentTemplate: {
4101 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4102 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4103 Record.push_back(DepT->isIdentifier());
4104 if (DepT->isIdentifier())
4105 AddIdentifierRef(DepT->getIdentifier(), Record);
4106 else
4107 Record.push_back(DepT->getOperator());
4108 break;
4109 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004110
4111 case TemplateName::SubstTemplateTemplateParm: {
4112 SubstTemplateTemplateParmStorage *subst
4113 = Name.getAsSubstTemplateTemplateParm();
4114 AddDeclRef(subst->getParameter(), Record);
4115 AddTemplateName(subst->getReplacement(), Record);
4116 break;
4117 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004118
4119 case TemplateName::SubstTemplateTemplateParmPack: {
4120 SubstTemplateTemplateParmPackStorage *SubstPack
4121 = Name.getAsSubstTemplateTemplateParmPack();
4122 AddDeclRef(SubstPack->getParameterPack(), Record);
4123 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4124 break;
4125 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004126 }
4127}
4128
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004129void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004130 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004131 Record.push_back(Arg.getKind());
4132 switch (Arg.getKind()) {
4133 case TemplateArgument::Null:
4134 break;
4135 case TemplateArgument::Type:
4136 AddTypeRef(Arg.getAsType(), Record);
4137 break;
4138 case TemplateArgument::Declaration:
4139 AddDeclRef(Arg.getAsDecl(), Record);
4140 break;
4141 case TemplateArgument::Integral:
4142 AddAPSInt(*Arg.getAsIntegral(), Record);
4143 AddTypeRef(Arg.getIntegralType(), Record);
4144 break;
4145 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004146 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4147 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004148 case TemplateArgument::TemplateExpansion:
4149 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004150 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4151 Record.push_back(*NumExpansions + 1);
4152 else
4153 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004154 break;
4155 case TemplateArgument::Expression:
4156 AddStmt(Arg.getAsExpr());
4157 break;
4158 case TemplateArgument::Pack:
4159 Record.push_back(Arg.pack_size());
4160 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4161 I != E; ++I)
4162 AddTemplateArgument(*I, Record);
4163 break;
4164 }
4165}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004166
4167void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004168ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004169 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004170 assert(TemplateParams && "No TemplateParams!");
4171 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4172 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4173 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4174 Record.push_back(TemplateParams->size());
4175 for (TemplateParameterList::const_iterator
4176 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4177 P != PEnd; ++P)
4178 AddDeclRef(*P, Record);
4179}
4180
4181/// \brief Emit a template argument list.
4182void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004183ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004184 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004185 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004186 Record.push_back(TemplateArgs->size());
4187 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004188 AddTemplateArgument(TemplateArgs->get(i), Record);
4189}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004190
4191
4192void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004193ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004194 Record.push_back(Set.size());
4195 for (UnresolvedSetImpl::const_iterator
4196 I = Set.begin(), E = Set.end(); I != E; ++I) {
4197 AddDeclRef(I.getDecl(), Record);
4198 Record.push_back(I.getAccess());
4199 }
4200}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004201
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004202void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004203 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004204 Record.push_back(Base.isVirtual());
4205 Record.push_back(Base.isBaseOfClass());
4206 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004207 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004208 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004209 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004210 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4211 : SourceLocation(),
4212 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004213}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004214
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004215void ASTWriter::FlushCXXBaseSpecifiers() {
4216 RecordData Record;
4217 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4218 Record.clear();
4219
4220 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004221 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004222 if (Index == CXXBaseSpecifiersOffsets.size())
4223 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4224 else {
4225 if (Index > CXXBaseSpecifiersOffsets.size())
4226 CXXBaseSpecifiersOffsets.resize(Index + 1);
4227 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4228 }
4229
4230 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4231 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4232 Record.push_back(BEnd - B);
4233 for (; B != BEnd; ++B)
4234 AddCXXBaseSpecifier(*B, Record);
4235 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004236
4237 // Flush any expressions that were written as part of the base specifiers.
4238 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004239 }
4240
4241 CXXBaseSpecifiersToWrite.clear();
4242}
4243
Alexis Hunt1d792652011-01-08 20:30:50 +00004244void ASTWriter::AddCXXCtorInitializers(
4245 const CXXCtorInitializer * const *CtorInitializers,
4246 unsigned NumCtorInitializers,
4247 RecordDataImpl &Record) {
4248 Record.push_back(NumCtorInitializers);
4249 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4250 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004251
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004252 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004253 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004254 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004255 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004256 } else if (Init->isDelegatingInitializer()) {
4257 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004258 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004259 } else if (Init->isMemberInitializer()){
4260 Record.push_back(CTOR_INITIALIZER_MEMBER);
4261 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004262 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004263 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4264 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004265 }
Francois Pichetd583da02010-12-04 09:14:42 +00004266
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004267 AddSourceLocation(Init->getMemberLocation(), Record);
4268 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004269 AddSourceLocation(Init->getLParenLoc(), Record);
4270 AddSourceLocation(Init->getRParenLoc(), Record);
4271 Record.push_back(Init->isWritten());
4272 if (Init->isWritten()) {
4273 Record.push_back(Init->getSourceOrder());
4274 } else {
4275 Record.push_back(Init->getNumArrayIndices());
4276 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4277 AddDeclRef(Init->getArrayIndex(i), Record);
4278 }
4279 }
4280}
4281
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004282void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4283 assert(D->DefinitionData);
4284 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004285 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004286 Record.push_back(Data.UserDeclaredConstructor);
4287 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004288 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004289 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004290 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004291 Record.push_back(Data.UserDeclaredDestructor);
4292 Record.push_back(Data.Aggregate);
4293 Record.push_back(Data.PlainOldData);
4294 Record.push_back(Data.Empty);
4295 Record.push_back(Data.Polymorphic);
4296 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004297 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004298 Record.push_back(Data.HasNoNonEmptyBases);
4299 Record.push_back(Data.HasPrivateFields);
4300 Record.push_back(Data.HasProtectedFields);
4301 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004302 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004303 Record.push_back(Data.HasOnlyCMembers);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004304 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004305 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004306 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
4307 Record.push_back(Data.DefaultedCopyConstructorIsConstexpr);
4308 Record.push_back(Data.DefaultedMoveConstructorIsConstexpr);
4309 Record.push_back(Data.HasConstexprDefaultConstructor);
4310 Record.push_back(Data.HasConstexprCopyConstructor);
4311 Record.push_back(Data.HasConstexprMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004312 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004313 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004314 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004315 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004316 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004317 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004318 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004319 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004320 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004321 Record.push_back(Data.DeclaredDefaultConstructor);
4322 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004323 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004324 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004325 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004326 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004327 Record.push_back(Data.FailedImplicitMoveConstructor);
4328 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004329 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004330
4331 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004332 if (Data.NumBases > 0)
4333 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4334 Record);
4335
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004336 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4337 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004338 if (Data.NumVBases > 0)
4339 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4340 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004341
4342 AddUnresolvedSet(Data.Conversions, Record);
4343 AddUnresolvedSet(Data.VisibleConversions, Record);
4344 // Data.Definition is the owning decl, no need to write it.
4345 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004346
4347 // Add lambda-specific data.
4348 if (Data.IsLambda) {
4349 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004350 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004351 Record.push_back(Lambda.NumCaptures);
4352 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004353 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004354 AddDeclRef(Lambda.ContextDecl, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004355 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4356 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4357 AddSourceLocation(Capture.getLocation(), Record);
4358 Record.push_back(Capture.isImplicit());
4359 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4360 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4361 AddDeclRef(Var, Record);
4362 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4363 : SourceLocation(),
4364 Record);
4365 }
4366 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004367}
4368
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004369void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004370 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004371 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004372 assert(FirstDeclID == NextDeclID &&
4373 FirstTypeID == NextTypeID &&
4374 FirstIdentID == NextIdentID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004375 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004376 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004377 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004378
Sebastian Redl07a89a82010-07-30 00:29:29 +00004379 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004380
Douglas Gregordf0c1512011-08-18 04:12:04 +00004381 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4382 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4383 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004384 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004385 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004386 NextDeclID = FirstDeclID;
4387 NextTypeID = FirstTypeID;
4388 NextIdentID = FirstIdentID;
4389 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004390 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004391}
4392
Sebastian Redl539c5062010-08-18 23:57:32 +00004393void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004394 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00004395 if (II->hasMacroDefinition())
4396 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004397}
4398
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004399void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004400 // Always take the highest-numbered type index. This copes with an interesting
4401 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004402 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004403 // keep the higher-numbered entry so that we can properly write it out to
4404 // the AST file.
4405 TypeIdx &StoredIdx = TypeIdxs[T];
4406 if (Idx.getIndex() >= StoredIdx.getIndex())
4407 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004408}
4409
Sebastian Redl539c5062010-08-18 23:57:32 +00004410void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004411 SelectorIDs[S] = ID;
4412}
Douglas Gregor91096292010-10-02 19:29:26 +00004413
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004414void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004415 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004416 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004417 MacroDefinitions[MD] = ID;
4418}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004419
Douglas Gregor0abc2622011-12-20 22:06:13 +00004420void ASTWriter::MacroVisible(IdentifierInfo *II) {
4421 DeserializedMacroNames.push_back(II);
4422}
4423
Douglas Gregore37a85a2011-12-02 17:30:13 +00004424void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4425 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4426 SubmoduleIDs[Mod] = ID;
4427}
4428
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004429void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004430 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004431 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004432 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4433 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004434 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004435 // A forward reference was mutated into a definition. Rewrite it.
4436 // FIXME: This happens during template instantiation, should we
4437 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004438 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004439 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004440 }
4441}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004442void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004443 assert(!WritingAST && "Already writing the AST!");
4444
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004445 // TU and namespaces are handled elsewhere.
4446 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4447 return;
4448
Douglas Gregorb3722e22011-09-09 23:01:35 +00004449 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004450 return; // Not a source decl added to a DeclContext from PCH.
4451
4452 AddUpdatedDeclContext(DC);
4453}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004454
4455void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004456 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004457 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004458 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004459 return; // Not a source member added to a class from PCH.
4460 if (!isa<CXXMethodDecl>(D))
4461 return; // We are interested in lazily declared implicit methods.
4462
4463 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004464 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004465 UpdateRecord &Record = DeclUpdates[RD];
4466 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004467 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004468}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004469
4470void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4471 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004472 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004473 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004474 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004475 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004476 return; // Not a source specialization added to a template from PCH.
4477
4478 UpdateRecord &Record = DeclUpdates[TD];
4479 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004480 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004481}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004482
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004483void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4484 const FunctionDecl *D) {
4485 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004486 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004487 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004488 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004489 return; // Not a source specialization added to a template from PCH.
4490
4491 UpdateRecord &Record = DeclUpdates[TD];
4492 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004493 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004494}
4495
Sebastian Redlab238a72011-04-24 16:28:06 +00004496void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004497 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004498 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004499 return; // Declaration not imported from PCH.
4500
4501 // Implicit decl from a PCH was defined.
4502 // FIXME: Should implicit definition be a separate FunctionDecl?
4503 RewriteDecl(D);
4504}
4505
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004506void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004507 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004508 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004509 return;
4510
4511 // Since the actual instantiation is delayed, this really means that we need
4512 // to update the instantiation location.
4513 UpdateRecord &Record = DeclUpdates[D];
4514 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4515 AddSourceLocation(
4516 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4517}
4518
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004519void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4520 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004521 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004522 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004523 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004524
4525 assert(IFD->getDefinition() && "Category on a class without a definition?");
4526 ObjCClassesWithCategories.insert(
4527 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004528}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004529
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004530
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004531void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4532 const ObjCPropertyDecl *OrigProp,
4533 const ObjCCategoryDecl *ClassExt) {
4534 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4535 if (!D)
4536 return;
4537
4538 assert(!WritingAST && "Already writing the AST!");
4539 if (!D->isFromASTFile())
4540 return; // Declaration not imported from PCH.
4541
4542 RewriteDecl(D);
4543}