blob: 28ae08be180d8b681ff815262d09180008d6ee2f [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000022#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000023#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000024#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000025#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000026#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000027#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000028#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000029#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000030#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000031#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000032#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000033#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000034#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000035#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000036#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000037#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000038#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000039#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000047#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000050#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000052using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000053
Sebastian Redl3df5a082010-07-30 17:03:48 +000054template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000055static StringRef data(const std::vector<T, Allocator> &v) {
56 if (v.empty()) return StringRef();
57 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000058 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000059}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000060
61template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000062static StringRef data(const SmallVectorImpl<T> &v) {
63 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000064 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000065}
66
Douglas Gregoref84c4b2009-04-09 22:27:44 +000067//===----------------------------------------------------------------------===//
68// Type serialization
69//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000070
Douglas Gregoref84c4b2009-04-09 22:27:44 +000071namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000072 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000073 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 public:
77 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000078 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000079
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000080 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000081 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082
83 void VisitArrayType(const ArrayType *T);
84 void VisitFunctionType(const FunctionType *T);
85 void VisitTagType(const TagType *T);
86
87#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
88#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000089#include "clang/AST/TypeNodes.def"
90 };
91}
92
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000093void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000094 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095}
96
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000097void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000099 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000100}
101
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000104 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000105}
106
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000109 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000110}
111
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000112void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000113 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
114 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000115 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000116}
117
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000118void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000119 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000120 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000121}
122
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000124 Writer.AddTypeRef(T->getPointeeType(), Record);
125 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000126 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000127}
128
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000130 Writer.AddTypeRef(T->getElementType(), Record);
131 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000132 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000133}
134
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000135void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136 VisitArrayType(T);
137 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000138 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139}
140
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000142 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000148 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
149 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000150 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getElementType(), Record);
156 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000157 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000158 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159}
160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000161void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000162 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000163 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000164}
165
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000167 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000168 FunctionType::ExtInfo C = T->getExtInfo();
169 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000170 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000171 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000172 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000173 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000174 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000175}
176
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000177void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000180}
181
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000183 VisitFunctionType(T);
184 Record.push_back(T->getNumArgs());
185 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
186 Writer.AddTypeRef(T->getArgType(I), Record);
187 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000188 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000189 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000198 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
199 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
200 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000201 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
202 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000203 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205}
206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000209 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000210}
John McCallb96ec562009-12-04 22:46:56 +0000211
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000212void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000213 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000214 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
215 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000216 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217}
218
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000220 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227}
228
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000229void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000230 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000231 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000232 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000233}
234
Alexis Hunte852b102011-05-24 22:41:36 +0000235void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
236 Writer.AddTypeRef(T->getBaseType(), Record);
237 Writer.AddTypeRef(T->getUnderlyingType(), Record);
238 Record.push_back(T->getUTTKind());
239 Code = TYPE_UNARY_TRANSFORM;
240}
241
Richard Smith30482bc2011-02-20 03:19:35 +0000242void ASTTypeWriter::VisitAutoType(const AutoType *T) {
243 Writer.AddTypeRef(T->getDeducedType(), Record);
244 Code = TYPE_AUTO;
245}
246
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000248 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000249 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000250 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 "Cannot serialize in the middle of a type definition");
252}
253
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000254void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000255 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000256 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000257}
258
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000259void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000260 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000261 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000262}
263
John McCall81904512011-01-06 01:58:22 +0000264void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
265 Writer.AddTypeRef(T->getModifiedType(), Record);
266 Writer.AddTypeRef(T->getEquivalentType(), Record);
267 Record.push_back(T->getAttrKind());
268 Code = TYPE_ATTRIBUTED;
269}
270
Mike Stump11289f42009-09-09 15:08:12 +0000271void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000273 const SubstTemplateTypeParmType *T) {
274 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
275 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000276 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000277}
278
279void
Douglas Gregorada4b792011-01-14 02:55:32 +0000280ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
281 const SubstTemplateTypeParmPackType *T) {
282 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
283 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
284 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
285}
286
287void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000288ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000289 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000290 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000291 Writer.AddTemplateName(T->getTemplateName(), Record);
292 Record.push_back(T->getNumArgs());
293 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
294 ArgI != ArgE; ++ArgI)
295 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000296 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
297 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000298 : T->getCanonicalTypeInternal(),
299 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000300 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000301}
302
303void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000304ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000305 VisitArrayType(T);
306 Writer.AddStmt(T->getSizeExpr());
307 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000313 const DependentSizedExtVectorType *T) {
314 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000315 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000320 Record.push_back(T->getDepth());
321 Record.push_back(T->getIndex());
322 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000323 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000324 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000325}
326
327void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000328ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000329 Record.push_back(T->getKeyword());
330 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
331 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000332 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
333 : T->getCanonicalTypeInternal(),
334 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000335 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000336}
337
338void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000339ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000340 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000341 Record.push_back(T->getKeyword());
342 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
343 Writer.AddIdentifierRef(T->getIdentifier(), Record);
344 Record.push_back(T->getNumArgs());
345 for (DependentTemplateSpecializationType::iterator
346 I = T->begin(), E = T->end(); I != E; ++I)
347 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000349}
350
Douglas Gregord2fa7662010-12-20 02:24:11 +0000351void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
352 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000353 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
354 Record.push_back(*NumExpansions + 1);
355 else
356 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000357 Code = TYPE_PACK_EXPANSION;
358}
359
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000360void ASTTypeWriter::VisitParenType(const ParenType *T) {
361 Writer.AddTypeRef(T->getInnerType(), Record);
362 Code = TYPE_PAREN;
363}
364
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000365void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000366 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000367 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
368 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000369 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000370}
371
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000373 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000374 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000375 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000376}
377
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000378void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000379 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000381}
382
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000383void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000384 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000385 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000386 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000387 E = T->qual_end(); I != E; ++I)
388 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000390}
391
Steve Narofffb4330f2009-06-17 22:40:22 +0000392void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000393ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000394 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000395 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000396}
397
Eli Friedman0dfb8892011-10-06 23:00:33 +0000398void
399ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
400 Writer.AddTypeRef(T->getValueType(), Record);
401 Code = TYPE_ATOMIC;
402}
403
John McCall8f115c62009-10-16 21:56:05 +0000404namespace {
405
406class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000407 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000408 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000409
410public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000411 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000412 : Writer(Writer), Record(Record) { }
413
John McCall17001972009-10-18 01:05:36 +0000414#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000415#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000417#include "clang/AST/TypeLocNodes.def"
418
John McCall17001972009-10-18 01:05:36 +0000419 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
420 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000421};
422
423}
424
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
426 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000427}
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000429 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
430 if (TL.needsExtraLocalData()) {
431 Record.push_back(TL.getWrittenTypeSpec());
432 Record.push_back(TL.getWrittenSignSpec());
433 Record.push_back(TL.getWrittenWidthSpec());
434 Record.push_back(TL.hasModeAttr());
435 }
John McCall8f115c62009-10-16 21:56:05 +0000436}
John McCall17001972009-10-18 01:05:36 +0000437void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000439}
John McCall17001972009-10-18 01:05:36 +0000440void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000442}
John McCall17001972009-10-18 01:05:36 +0000443void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000445}
John McCall17001972009-10-18 01:05:36 +0000446void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000448}
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000451}
John McCall17001972009-10-18 01:05:36 +0000452void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000454 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000455}
John McCall17001972009-10-18 01:05:36 +0000456void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
458 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
459 Record.push_back(TL.getSizeExpr() ? 1 : 0);
460 if (TL.getSizeExpr())
461 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000462}
John McCall17001972009-10-18 01:05:36 +0000463void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
464 VisitArrayTypeLoc(TL);
465}
466void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
467 VisitArrayTypeLoc(TL);
468}
469void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
470 VisitArrayTypeLoc(TL);
471}
472void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
473 DependentSizedArrayTypeLoc TL) {
474 VisitArrayTypeLoc(TL);
475}
476void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
477 DependentSizedExtVectorTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
486void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000487 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
488 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000489 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
490 Writer.AddDeclRef(TL.getArg(i), Record);
491}
492void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
493 VisitFunctionTypeLoc(TL);
494}
495void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
496 VisitFunctionTypeLoc(TL);
497}
John McCallb96ec562009-12-04 22:46:56 +0000498void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getNameLoc(), Record);
500}
John McCall17001972009-10-18 01:05:36 +0000501void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
504void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000505 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
506 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
507 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000508}
509void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000510 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
511 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
512 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
513 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000514}
515void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
516 Writer.AddSourceLocation(TL.getNameLoc(), Record);
517}
Alexis Hunte852b102011-05-24 22:41:36 +0000518void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getKWLoc(), Record);
520 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
521 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
522 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
523}
Richard Smith30482bc2011-02-20 03:19:35 +0000524void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
John McCall17001972009-10-18 01:05:36 +0000527void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
530void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
John McCall81904512011-01-06 01:58:22 +0000533void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
535 if (TL.hasAttrOperand()) {
536 SourceRange range = TL.getAttrOperandParensRange();
537 Writer.AddSourceLocation(range.getBegin(), Record);
538 Writer.AddSourceLocation(range.getEnd(), Record);
539 }
540 if (TL.hasAttrExprOperand()) {
541 Expr *operand = TL.getAttrExprOperand();
542 Record.push_back(operand ? 1 : 0);
543 if (operand) Writer.AddStmt(operand);
544 } else if (TL.hasAttrEnumOperand()) {
545 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
546 }
547}
John McCall17001972009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
John McCallcebee162009-10-18 09:09:24 +0000551void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
552 SubstTemplateTypeParmTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getNameLoc(), Record);
554}
Douglas Gregorada4b792011-01-14 02:55:32 +0000555void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
556 SubstTemplateTypeParmPackTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getNameLoc(), Record);
558}
John McCall17001972009-10-18 01:05:36 +0000559void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
560 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000561 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000562 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
563 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
564 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
565 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000566 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
567 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000568}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000569void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
571 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
572}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000573void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000574 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000575 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000576}
John McCalle78aac42010-03-10 03:28:59 +0000577void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
578 Writer.AddSourceLocation(TL.getNameLoc(), Record);
579}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000580void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000581 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000582 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000583 Writer.AddSourceLocation(TL.getNameLoc(), Record);
584}
John McCallc392f372010-06-11 00:33:02 +0000585void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
586 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000587 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000588 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000589 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000590 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000591 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
592 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
593 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000594 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
595 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000596}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000597void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
599}
John McCall17001972009-10-18 01:05:36 +0000600void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000602}
603void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
604 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000605 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
606 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
607 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
608 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000609}
John McCallfc93cf92009-10-22 22:37:11 +0000610void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
611 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000612}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000613void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
614 Writer.AddSourceLocation(TL.getKWLoc(), Record);
615 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
616 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
617}
John McCall8f115c62009-10-16 21:56:05 +0000618
Chris Lattner19cea4e2009-04-22 05:57:30 +0000619//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000620// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000621//===----------------------------------------------------------------------===//
622
Chris Lattner28fa4e62009-04-26 22:26:21 +0000623static void EmitBlockID(unsigned ID, const char *Name,
624 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000625 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000626 Record.clear();
627 Record.push_back(ID);
628 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
629
630 // Emit the block name if present.
631 if (Name == 0 || Name[0] == 0) return;
632 Record.clear();
633 while (*Name)
634 Record.push_back(*Name++);
635 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
636}
637
638static void EmitRecordID(unsigned ID, const char *Name,
639 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000640 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000641 Record.clear();
642 Record.push_back(ID);
643 while (*Name)
644 Record.push_back(*Name++);
645 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000646}
647
648static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000649 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000650#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000651 RECORD(STMT_STOP);
652 RECORD(STMT_NULL_PTR);
653 RECORD(STMT_NULL);
654 RECORD(STMT_COMPOUND);
655 RECORD(STMT_CASE);
656 RECORD(STMT_DEFAULT);
657 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000658 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000659 RECORD(STMT_IF);
660 RECORD(STMT_SWITCH);
661 RECORD(STMT_WHILE);
662 RECORD(STMT_DO);
663 RECORD(STMT_FOR);
664 RECORD(STMT_GOTO);
665 RECORD(STMT_INDIRECT_GOTO);
666 RECORD(STMT_CONTINUE);
667 RECORD(STMT_BREAK);
668 RECORD(STMT_RETURN);
669 RECORD(STMT_DECL);
670 RECORD(STMT_ASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000671 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000672 RECORD(EXPR_PREDEFINED);
673 RECORD(EXPR_DECL_REF);
674 RECORD(EXPR_INTEGER_LITERAL);
675 RECORD(EXPR_FLOATING_LITERAL);
676 RECORD(EXPR_IMAGINARY_LITERAL);
677 RECORD(EXPR_STRING_LITERAL);
678 RECORD(EXPR_CHARACTER_LITERAL);
679 RECORD(EXPR_PAREN);
680 RECORD(EXPR_UNARY_OPERATOR);
681 RECORD(EXPR_SIZEOF_ALIGN_OF);
682 RECORD(EXPR_ARRAY_SUBSCRIPT);
683 RECORD(EXPR_CALL);
684 RECORD(EXPR_MEMBER);
685 RECORD(EXPR_BINARY_OPERATOR);
686 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
687 RECORD(EXPR_CONDITIONAL_OPERATOR);
688 RECORD(EXPR_IMPLICIT_CAST);
689 RECORD(EXPR_CSTYLE_CAST);
690 RECORD(EXPR_COMPOUND_LITERAL);
691 RECORD(EXPR_EXT_VECTOR_ELEMENT);
692 RECORD(EXPR_INIT_LIST);
693 RECORD(EXPR_DESIGNATED_INIT);
694 RECORD(EXPR_IMPLICIT_VALUE_INIT);
695 RECORD(EXPR_VA_ARG);
696 RECORD(EXPR_ADDR_LABEL);
697 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000698 RECORD(EXPR_CHOOSE);
699 RECORD(EXPR_GNU_NULL);
700 RECORD(EXPR_SHUFFLE_VECTOR);
701 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000702 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000703 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000704 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000705 RECORD(EXPR_OBJC_ARRAY_LITERAL);
706 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000707 RECORD(EXPR_OBJC_ENCODE);
708 RECORD(EXPR_OBJC_SELECTOR_EXPR);
709 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
710 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
711 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
712 RECORD(EXPR_OBJC_KVC_REF_EXPR);
713 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000714 RECORD(STMT_OBJC_FOR_COLLECTION);
715 RECORD(STMT_OBJC_CATCH);
716 RECORD(STMT_OBJC_FINALLY);
717 RECORD(STMT_OBJC_AT_TRY);
718 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
719 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000720 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000721 RECORD(EXPR_CXX_OPERATOR_CALL);
722 RECORD(EXPR_CXX_CONSTRUCT);
723 RECORD(EXPR_CXX_STATIC_CAST);
724 RECORD(EXPR_CXX_DYNAMIC_CAST);
725 RECORD(EXPR_CXX_REINTERPRET_CAST);
726 RECORD(EXPR_CXX_CONST_CAST);
727 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000728 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000729 RECORD(EXPR_CXX_BOOL_LITERAL);
730 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000731 RECORD(EXPR_CXX_TYPEID_EXPR);
732 RECORD(EXPR_CXX_TYPEID_TYPE);
733 RECORD(EXPR_CXX_UUIDOF_EXPR);
734 RECORD(EXPR_CXX_UUIDOF_TYPE);
735 RECORD(EXPR_CXX_THIS);
736 RECORD(EXPR_CXX_THROW);
737 RECORD(EXPR_CXX_DEFAULT_ARG);
738 RECORD(EXPR_CXX_BIND_TEMPORARY);
739 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
740 RECORD(EXPR_CXX_NEW);
741 RECORD(EXPR_CXX_DELETE);
742 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
743 RECORD(EXPR_EXPR_WITH_CLEANUPS);
744 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
745 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
746 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
747 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
748 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
749 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
750 RECORD(EXPR_CXX_NOEXCEPT);
751 RECORD(EXPR_OPAQUE_VALUE);
752 RECORD(EXPR_BINARY_TYPE_TRAIT);
753 RECORD(EXPR_PACK_EXPANSION);
754 RECORD(EXPR_SIZEOF_PACK);
755 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000756 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000757#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000758}
Mike Stump11289f42009-09-09 15:08:12 +0000759
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000760void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000761 RecordData Record;
762 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Sebastian Redl539c5062010-08-18 23:57:32 +0000764#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
765#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000766
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000767 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000768 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000769 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000770 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000771 RECORD(TYPE_OFFSET);
772 RECORD(DECL_OFFSET);
773 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000774 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000775 RECORD(IDENTIFIER_OFFSET);
776 RECORD(IDENTIFIER_TABLE);
777 RECORD(EXTERNAL_DEFINITIONS);
778 RECORD(SPECIAL_TYPES);
779 RECORD(STATISTICS);
780 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000781 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000782 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
783 RECORD(SELECTOR_OFFSETS);
784 RECORD(METHOD_POOL);
785 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000786 RECORD(SOURCE_LOCATION_OFFSETS);
787 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000788 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000789 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000790 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000791 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000792 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000793 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000794 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000795 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000796 RECORD(SEMA_DECL_REFS);
797 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
798 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
799 RECORD(DECL_REPLACEMENTS);
800 RECORD(UPDATE_VISIBLE);
801 RECORD(DECL_UPDATE_OFFSETS);
802 RECORD(DECL_UPDATES);
803 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
804 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000805 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000806 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000807 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000808 RECORD(FP_PRAGMA_OPTIONS);
809 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000810 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000811 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
812 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000813 RECORD(MODULE_OFFSET_MAP);
814 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000815 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000816 RECORD(FILE_SORTED_DECLS);
817 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000818 RECORD(MERGED_DECLARATIONS);
819 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000820 RECORD(OBJC_CATEGORIES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000821
Chris Lattner28fa4e62009-04-26 22:26:21 +0000822 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000823 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000824 RECORD(SM_SLOC_FILE_ENTRY);
825 RECORD(SM_SLOC_BUFFER_ENTRY);
826 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000827 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattner28fa4e62009-04-26 22:26:21 +0000829 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000830 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 RECORD(PP_MACRO_OBJECT_LIKE);
832 RECORD(PP_MACRO_FUNCTION_LIKE);
833 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000834
Douglas Gregor12bfa382009-10-17 00:13:19 +0000835 // Decls and Types block.
836 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000837 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000838 RECORD(TYPE_COMPLEX);
839 RECORD(TYPE_POINTER);
840 RECORD(TYPE_BLOCK_POINTER);
841 RECORD(TYPE_LVALUE_REFERENCE);
842 RECORD(TYPE_RVALUE_REFERENCE);
843 RECORD(TYPE_MEMBER_POINTER);
844 RECORD(TYPE_CONSTANT_ARRAY);
845 RECORD(TYPE_INCOMPLETE_ARRAY);
846 RECORD(TYPE_VARIABLE_ARRAY);
847 RECORD(TYPE_VECTOR);
848 RECORD(TYPE_EXT_VECTOR);
849 RECORD(TYPE_FUNCTION_PROTO);
850 RECORD(TYPE_FUNCTION_NO_PROTO);
851 RECORD(TYPE_TYPEDEF);
852 RECORD(TYPE_TYPEOF_EXPR);
853 RECORD(TYPE_TYPEOF);
854 RECORD(TYPE_RECORD);
855 RECORD(TYPE_ENUM);
856 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000857 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000858 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000859 RECORD(TYPE_DECLTYPE);
860 RECORD(TYPE_ELABORATED);
861 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
862 RECORD(TYPE_UNRESOLVED_USING);
863 RECORD(TYPE_INJECTED_CLASS_NAME);
864 RECORD(TYPE_OBJC_OBJECT);
865 RECORD(TYPE_TEMPLATE_TYPE_PARM);
866 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
867 RECORD(TYPE_DEPENDENT_NAME);
868 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
869 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
870 RECORD(TYPE_PAREN);
871 RECORD(TYPE_PACK_EXPANSION);
872 RECORD(TYPE_ATTRIBUTED);
873 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000874 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000875 RECORD(DECL_TYPEDEF);
876 RECORD(DECL_ENUM);
877 RECORD(DECL_RECORD);
878 RECORD(DECL_ENUM_CONSTANT);
879 RECORD(DECL_FUNCTION);
880 RECORD(DECL_OBJC_METHOD);
881 RECORD(DECL_OBJC_INTERFACE);
882 RECORD(DECL_OBJC_PROTOCOL);
883 RECORD(DECL_OBJC_IVAR);
884 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000885 RECORD(DECL_OBJC_CATEGORY);
886 RECORD(DECL_OBJC_CATEGORY_IMPL);
887 RECORD(DECL_OBJC_IMPLEMENTATION);
888 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
889 RECORD(DECL_OBJC_PROPERTY);
890 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000891 RECORD(DECL_FIELD);
892 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000893 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000894 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000895 RECORD(DECL_FILE_SCOPE_ASM);
896 RECORD(DECL_BLOCK);
897 RECORD(DECL_CONTEXT_LEXICAL);
898 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000899 RECORD(DECL_NAMESPACE);
900 RECORD(DECL_NAMESPACE_ALIAS);
901 RECORD(DECL_USING);
902 RECORD(DECL_USING_SHADOW);
903 RECORD(DECL_USING_DIRECTIVE);
904 RECORD(DECL_UNRESOLVED_USING_VALUE);
905 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
906 RECORD(DECL_LINKAGE_SPEC);
907 RECORD(DECL_CXX_RECORD);
908 RECORD(DECL_CXX_METHOD);
909 RECORD(DECL_CXX_CONSTRUCTOR);
910 RECORD(DECL_CXX_DESTRUCTOR);
911 RECORD(DECL_CXX_CONVERSION);
912 RECORD(DECL_ACCESS_SPEC);
913 RECORD(DECL_FRIEND);
914 RECORD(DECL_FRIEND_TEMPLATE);
915 RECORD(DECL_CLASS_TEMPLATE);
916 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
917 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
918 RECORD(DECL_FUNCTION_TEMPLATE);
919 RECORD(DECL_TEMPLATE_TYPE_PARM);
920 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
921 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
922 RECORD(DECL_STATIC_ASSERT);
923 RECORD(DECL_CXX_BASE_SPECIFIERS);
924 RECORD(DECL_INDIRECTFIELD);
925 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
926
Douglas Gregor03412ba2011-06-03 02:27:19 +0000927 // Statements and Exprs can occur in the Decls and Types block.
928 AddStmtsExprs(Stream, Record);
929
Douglas Gregor92a96f52011-02-08 21:58:10 +0000930 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000931 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000932 RECORD(PPD_MACRO_DEFINITION);
933 RECORD(PPD_INCLUSION_DIRECTIVE);
934
Chris Lattner28fa4e62009-04-26 22:26:21 +0000935#undef RECORD
936#undef BLOCK
937 Stream.ExitBlock();
938}
939
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000940/// \brief Adjusts the given filename to only write out the portion of the
941/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000942///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000943/// \param Filename the file name to adjust.
944///
945/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
946/// the returned filename will be adjusted by this system root.
947///
948/// \returns either the original filename (if it needs no adjustment) or the
949/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000950static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000951adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000952 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000953
Douglas Gregorc567ba22011-07-22 16:35:34 +0000954 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000955 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000956
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 // Verify that the filename and the system root have the same prefix.
958 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000959 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000960 if (Filename[Pos] != isysroot[Pos])
961 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000962
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 // We hit the end of the filename before we hit the end of the system root.
964 if (!Filename[Pos])
965 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000966
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000967 // If the file name has a '/' at the current position, skip over the '/'.
968 // We distinguish sysroot-based includes from absolute includes by the
969 // absence of '/' at the beginning of sysroot-based includes.
970 if (Filename[Pos] == '/')
971 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000972
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 return Filename + Pos;
974}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000975
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000976/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000977void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000978 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000979 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000980
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000981 // Metadata
Douglas Gregore8bbc122011-09-02 00:18:52 +0000982 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000983 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000984 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000985 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
986 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000987 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
988 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +0000990 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregor29cc6422011-08-17 21:07:30 +0000991 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000992 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000993
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000994 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000995 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000996 Record.push_back(VERSION_MAJOR);
997 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000998 Record.push_back(CLANG_VERSION_MAJOR);
999 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001000 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001001 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor29cc6422011-08-17 21:07:30 +00001002 const std::string &Triple = Target.getTriple().getTriple();
1003 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1004
1005 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001006 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1007 llvm::SmallVector<char, 128> ModulePaths;
1008 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001009
1010 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1011 M != MEnd; ++M) {
1012 // Skip modules that weren't directly imported.
1013 if (!(*M)->isDirectlyImported())
1014 continue;
1015
1016 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1017 // FIXME: Write import location, once it matters.
1018 // FIXME: This writes the absolute path for AST files we depend on.
1019 const std::string &FileName = (*M)->FileName;
1020 Record.push_back(FileName.size());
1021 Record.append(FileName.begin(), FileName.end());
1022 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001023 Stream.EmitRecord(IMPORTS, Record);
1024 }
Mike Stump11289f42009-09-09 15:08:12 +00001025
Douglas Gregora3b20262011-05-06 21:43:30 +00001026 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001027 SourceManager &SM = Context.getSourceManager();
1028 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1029 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001030 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001031 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1032 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1033
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001034 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001035
Michael J. Spencer740857f2010-12-21 16:45:57 +00001036 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001037
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001038 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001039 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001040 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001041 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001042 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001043 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001044
1045 Record.clear();
1046 Record.push_back(SM.getMainFileID().getOpaqueValue());
1047 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001048 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001049
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001050 // Original PCH directory
1051 if (!OutputFile.empty() && OutputFile != "-") {
1052 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1053 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1055 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1056
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001057 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001058
1059 llvm::sys::fs::make_absolute(OutputPath);
1060 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1061
1062 RecordData Record;
1063 Record.push_back(ORIGINAL_PCH_DIR);
1064 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1065 }
1066
Ted Kremenek18e066f2010-01-22 22:12:47 +00001067 // Repository branch/version information.
1068 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001069 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001070 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1071 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001072 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001073 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001074 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1075 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001076}
1077
1078/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001079void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001080 RecordData Record;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00001081#define LANGOPT(Name, Bits, Default, Description) \
1082 Record.push_back(LangOpts.Name);
1083#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1084 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1085#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +00001086
1087 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1088 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregor7d106e42011-11-15 19:35:01 +00001089
1090 Record.push_back(LangOpts.CurrentModule.size());
1091 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl539c5062010-08-18 23:57:32 +00001092 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001093}
1094
Douglas Gregora7f71a92009-04-10 03:52:48 +00001095//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001096// stat cache Serialization
1097//===----------------------------------------------------------------------===//
1098
1099namespace {
1100// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001101class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001102public:
1103 typedef const char * key_type;
1104 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner2a6fa472010-11-23 19:28:12 +00001106 typedef struct stat data_type;
1107 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001108
1109 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001110 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001111 }
Mike Stump11289f42009-09-09 15:08:12 +00001112
1113 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001114 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001115 data_type_ref Data) {
1116 unsigned StrLen = strlen(path);
1117 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001118 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001119 clang::io::Emit8(Out, DataLen);
1120 return std::make_pair(StrLen + 1, DataLen);
1121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001123 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001124 Out.write(path, KeyLen);
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001127 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001128 data_type_ref Data, unsigned DataLen) {
1129 using namespace clang::io;
1130 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner2a6fa472010-11-23 19:28:12 +00001132 Emit32(Out, (uint32_t) Data.st_ino);
1133 Emit32(Out, (uint32_t) Data.st_dev);
1134 Emit16(Out, (uint16_t) Data.st_mode);
1135 Emit64(Out, (uint64_t) Data.st_mtime);
1136 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001137
1138 assert(Out.tell() - Start == DataLen && "Wrong data length");
1139 }
1140};
1141} // end anonymous namespace
1142
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001143/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001144void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001145 // Build the on-disk hash table containing information about every
1146 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001147 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001148 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001149 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001150 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001151 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001152 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001153 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001154 }
Mike Stump11289f42009-09-09 15:08:12 +00001155
Douglas Gregorc5046832009-04-27 18:38:38 +00001156 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001157 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001158 uint32_t BucketOffset;
1159 {
1160 llvm::raw_svector_ostream Out(StatCacheData);
1161 // Make sure that no bucket is at offset 0
1162 clang::io::Emit32(Out, 0);
1163 BucketOffset = Generator.Emit(Out);
1164 }
1165
1166 // Create a blob abbreviation
1167 using namespace llvm;
1168 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001169 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1173 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1174
1175 // Write the stat cache
1176 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001177 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001178 Record.push_back(BucketOffset);
1179 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001180 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001181}
1182
1183//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184// Source Manager Serialization
1185//===----------------------------------------------------------------------===//
1186
1187/// \brief Create an abbreviation for the SLocEntry that refers to a
1188/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001189static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001190 using namespace llvm;
1191 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001192 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001197 // FileEntry fields.
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001205 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001206}
1207
1208/// \brief Create an abbreviation for the SLocEntry that refers to a
1209/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001210static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001211 using namespace llvm;
1212 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001213 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001219 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001220}
1221
1222/// \brief Create an abbreviation for the SLocEntry that refers to a
1223/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001224static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225 using namespace llvm;
1226 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001227 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001229 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230}
1231
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001232/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1233/// expansion.
1234static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001235 using namespace llvm;
1236 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001237 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001243 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001244}
1245
Douglas Gregor09b69892011-02-10 17:09:37 +00001246namespace {
1247 // Trait used for the on-disk hash table of header search information.
1248 class HeaderFileInfoTrait {
1249 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001250
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001251 // Keep track of the framework names we've used during serialization.
1252 SmallVector<char, 128> FrameworkStringData;
1253 llvm::StringMap<unsigned> FrameworkNameOffset;
1254
Douglas Gregor09b69892011-02-10 17:09:37 +00001255 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001256 HeaderFileInfoTrait(ASTWriter &Writer)
1257 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001258
1259 typedef const char *key_type;
1260 typedef key_type key_type_ref;
1261
1262 typedef HeaderFileInfo data_type;
1263 typedef const data_type &data_type_ref;
1264
1265 static unsigned ComputeHash(const char *path) {
1266 // The hash is based only on the filename portion of the key, so that the
1267 // reader can match based on filenames when symlinking or excess path
1268 // elements ("foo/../", "../") change the form of the name. However,
1269 // complete path is still the key.
1270 return llvm::HashString(llvm::sys::path::filename(path));
1271 }
1272
1273 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001274 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001275 data_type_ref Data) {
1276 unsigned StrLen = strlen(path);
1277 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001278 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001279 clang::io::Emit8(Out, DataLen);
1280 return std::make_pair(StrLen + 1, DataLen);
1281 }
1282
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001283 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001284 Out.write(path, KeyLen);
1285 }
1286
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001287 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001288 data_type_ref Data, unsigned DataLen) {
1289 using namespace clang::io;
1290 uint64_t Start = Out.tell(); (void)Start;
1291
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001292 unsigned char Flags = (Data.isImport << 5)
1293 | (Data.isPragmaOnce << 4)
1294 | (Data.DirInfo << 2)
1295 | (Data.Resolved << 1)
1296 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001297 Emit8(Out, (uint8_t)Flags);
1298 Emit16(Out, (uint16_t) Data.NumIncludes);
1299
1300 if (!Data.ControllingMacro)
1301 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1302 else
1303 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001304
1305 unsigned Offset = 0;
1306 if (!Data.Framework.empty()) {
1307 // If this header refers into a framework, save the framework name.
1308 llvm::StringMap<unsigned>::iterator Pos
1309 = FrameworkNameOffset.find(Data.Framework);
1310 if (Pos == FrameworkNameOffset.end()) {
1311 Offset = FrameworkStringData.size() + 1;
1312 FrameworkStringData.append(Data.Framework.begin(),
1313 Data.Framework.end());
1314 FrameworkStringData.push_back(0);
1315
1316 FrameworkNameOffset[Data.Framework] = Offset;
1317 } else
1318 Offset = Pos->second;
1319 }
1320 Emit32(Out, Offset);
1321
Douglas Gregor09b69892011-02-10 17:09:37 +00001322 assert(Out.tell() - Start == DataLen && "Wrong data length");
1323 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001324
1325 const char *strings_begin() const { return FrameworkStringData.begin(); }
1326 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001327 };
1328} // end anonymous namespace
1329
1330/// \brief Write the header search block for the list of files that
1331///
1332/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001333void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001334 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001335 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1336
1337 if (FilesByUID.size() > HS.header_file_size())
1338 FilesByUID.resize(HS.header_file_size());
1339
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001340 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001341 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001342 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001343 unsigned NumHeaderSearchEntries = 0;
1344 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1345 const FileEntry *File = FilesByUID[UID];
1346 if (!File)
1347 continue;
1348
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001349 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1350 // from the external source if it was not provided already.
1351 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001352 if (HFI.External && Chain)
1353 continue;
1354
1355 // Turn the file name into an absolute path, if it isn't already.
1356 const char *Filename = File->getName();
1357 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1358
1359 // If we performed any translation on the file name at all, we need to
1360 // save this string, since the generator will refer to it later.
1361 if (Filename != File->getName()) {
1362 Filename = strdup(Filename);
1363 SavedStrings.push_back(Filename);
1364 }
1365
1366 Generator.insert(Filename, HFI, GeneratorTrait);
1367 ++NumHeaderSearchEntries;
1368 }
1369
1370 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001371 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001372 uint32_t BucketOffset;
1373 {
1374 llvm::raw_svector_ostream Out(TableData);
1375 // Make sure that no bucket is at offset 0
1376 clang::io::Emit32(Out, 0);
1377 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1378 }
1379
1380 // Create a blob abbreviation
1381 using namespace llvm;
1382 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1383 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001387 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1388 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1389
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001390 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001391 RecordData Record;
1392 Record.push_back(HEADER_SEARCH_TABLE);
1393 Record.push_back(BucketOffset);
1394 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001395 Record.push_back(TableData.size());
1396 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001397 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1398
1399 // Free all of the strings we had to duplicate.
1400 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1401 free((void*)SavedStrings[I]);
1402}
1403
Douglas Gregora7f71a92009-04-10 03:52:48 +00001404/// \brief Writes the block containing the serialized form of the
1405/// source manager.
1406///
1407/// TODO: We should probably use an on-disk hash table (stored in a
1408/// blob), indexed based on the file name, so that we only create
1409/// entries for files that we actually need. In the common case (no
1410/// errors), we probably won't have to create file entries for any of
1411/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001412void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001413 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001414 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001415 RecordData Record;
1416
Chris Lattner0910e3b2009-04-10 17:16:57 +00001417 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001418 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001419
1420 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001421 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1422 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1423 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001424 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001425
Douglas Gregor258ae542009-04-27 06:38:32 +00001426 // Write out the source location entry table. We skip the first
1427 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001428 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001429 // Write out the offsets of only source location file entries.
1430 // We will go through them in ASTReader::validateFileEntries().
1431 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001432 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001433 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1434 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001435 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001436 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001437 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001438
Douglas Gregor258ae542009-04-27 06:38:32 +00001439 // Record the offset of this source-location entry.
1440 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1441
1442 // Figure out which record code to use.
1443 unsigned Code;
1444 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001445 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1446 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001447 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001448 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1449 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001450 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001451 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001452 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001453 Record.clear();
1454 Record.push_back(Code);
1455
Douglas Gregor925296b2011-07-19 16:10:42 +00001456 // Starting offset of this entry within this module, so skip the dummy.
1457 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001458 if (SLoc->isFile()) {
1459 const SrcMgr::FileInfo &File = SLoc->getFile();
1460 Record.push_back(File.getIncludeLoc().getRawEncoding());
1461 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1462 Record.push_back(File.hasLineDirectives());
1463
1464 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001465 if (Content->OrigEntry) {
1466 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001467 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001468
Douglas Gregor258ae542009-04-27 06:38:32 +00001469 // The source location entry is a file. The blob associated
1470 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001471
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001472 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001473 Record.push_back(Content->OrigEntry->getSize());
1474 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001475 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001476 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001477
1478 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1479 if (FDI != FileDeclIDs.end()) {
1480 Record.push_back(FDI->second->FirstDeclIndex);
1481 Record.push_back(FDI->second->DeclIDs.size());
1482 } else {
1483 Record.push_back(0);
1484 Record.push_back(0);
1485 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001486
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001487 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001488 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001489 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001490
1491 // Ask the file manager to fixup the relative path for us. This will
1492 // honor the working directory.
1493 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1494
1495 // FIXME: This call to make_absolute shouldn't be necessary, the
1496 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001497 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001498 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001499
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001500 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001501 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001502
1503 if (Content->BufferOverridden) {
1504 Record.clear();
1505 Record.push_back(SM_SLOC_BUFFER_BLOB);
1506 const llvm::MemoryBuffer *Buffer
1507 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1508 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1509 StringRef(Buffer->getBufferStart(),
1510 Buffer->getBufferSize() + 1));
1511 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001512 } else {
1513 // The source location entry is a buffer. The blob associated
1514 // with this entry contains the contents of the buffer.
1515
1516 // We add one to the size so that we capture the trailing NULL
1517 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1518 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001519 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001520 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001521 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001522 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001523 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001524 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001525 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001526 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001527 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001528 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001529
Douglas Gregor925296b2011-07-19 16:10:42 +00001530 if (strcmp(Name, "<built-in>") == 0) {
1531 PreloadSLocs.push_back(SLocEntryOffsets.size());
1532 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001533 }
1534 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001535 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001536 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001537 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1538 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001539 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1540 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001541
1542 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001543 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001544 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001545 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001546 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001547 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001548 }
1549 }
1550
Douglas Gregor8f45df52009-04-16 22:23:12 +00001551 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001552
1553 if (SLocEntryOffsets.empty())
1554 return;
1555
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001556 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001557 // table is used for lazily loading source-location information.
1558 using namespace llvm;
1559 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001560 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1564 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001565
Douglas Gregor258ae542009-04-27 06:38:32 +00001566 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001567 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001568 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001569 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001570 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001571
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001572 Abbrev = new BitCodeAbbrev();
1573 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1575 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1576 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1577
1578 Record.clear();
1579 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1580 Record.push_back(SLocFileEntryOffsets.size());
1581 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1582 data(SLocFileEntryOffsets));
1583
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001584 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001585 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001586 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001587
1588 // Write the line table. It depends on remapping working, so it must come
1589 // after the source location offsets.
1590 if (SourceMgr.hasLineTable()) {
1591 LineTableInfo &LineTable = SourceMgr.getLineTable();
1592
1593 Record.clear();
1594 // Emit the file names
1595 Record.push_back(LineTable.getNumFilenames());
1596 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1597 // Emit the file name
1598 const char *Filename = LineTable.getFilename(I);
1599 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1600 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1601 Record.push_back(FilenameLen);
1602 if (FilenameLen)
1603 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1604 }
1605
1606 // Emit the line entries
1607 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1608 L != LEnd; ++L) {
1609 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001610 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001611 continue;
1612
1613 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001614 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001615
1616 // Emit the line entries
1617 Record.push_back(L->second.size());
1618 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1619 LEEnd = L->second.end();
1620 LE != LEEnd; ++LE) {
1621 Record.push_back(LE->FileOffset);
1622 Record.push_back(LE->LineNo);
1623 Record.push_back(LE->FilenameID);
1624 Record.push_back((unsigned)LE->FileKind);
1625 Record.push_back(LE->IncludeOffset);
1626 }
1627 }
1628 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1629 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001630}
1631
Douglas Gregorc5046832009-04-27 18:38:38 +00001632//===----------------------------------------------------------------------===//
1633// Preprocessor Serialization
1634//===----------------------------------------------------------------------===//
1635
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001636static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1637 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1638 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1639 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1640 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1641 return X.first->getName().compare(Y.first->getName());
1642}
1643
Chris Lattnereeffaef2009-04-10 17:15:23 +00001644/// \brief Writes the block containing the serialized form of the
1645/// preprocessor.
1646///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001647void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001648 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1649 if (PPRec)
1650 WritePreprocessorDetail(*PPRec);
1651
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001652 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001653
Chris Lattner0af3ba12009-04-13 01:29:17 +00001654 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1655 if (PP.getCounterValue() != 0) {
1656 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001657 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001658 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001659 }
1660
1661 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001662 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001663
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001664 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001665 // FIXME: use diagnostics subsystem for localization etc.
1666 if (PP.SawDateOrTime())
1667 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001668
Douglas Gregor796d76a2010-10-20 22:00:55 +00001669
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001670 // Loop over all the macro definitions that are live at the end of the file,
1671 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001672
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001673 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001674 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001675 MacrosToEmit;
1676 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001677 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1678 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001679 I != E; ++I) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001680 const IdentifierInfo *Name = I->first;
Douglas Gregorebf00492011-10-17 15:32:29 +00001681 if (!IsModule || I->second->isPublic()) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001682 MacroDefinitionsSeen.insert(Name);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001683 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1684 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001685 }
1686
1687 // Sort the set of macro definitions that need to be serialized by the
1688 // name of the macro, to provide a stable ordering.
1689 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1690 &compareMacroDefinitions);
1691
Douglas Gregor68051a72011-02-11 00:26:14 +00001692 // Resolve any identifiers that defined macros at the time they were
1693 // deserialized, adding them to the list of macros to emit (if appropriate).
1694 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1695 IdentifierInfo *Name
1696 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1697 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1698 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1699 }
1700
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001701 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1702 const IdentifierInfo *Name = MacrosToEmit[I].first;
1703 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001704 if (!MI)
1705 continue;
1706
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001707 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001708 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001709 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001710
1711 // FIXME: There is a (probably minor) optimization we could do here, if
1712 // the macro comes from the original PCH but the identifier comes from a
1713 // chained PCH, by storing the offset into the original PCH rather than
1714 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001715 if (MI->isBuiltinMacro() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001716 (Chain &&
1717 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1718 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001719 continue;
1720
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001721 AddIdentifierRef(Name, Record);
1722 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001723 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1724 Record.push_back(MI->isUsed());
Douglas Gregorebf00492011-10-17 15:32:29 +00001725 Record.push_back(MI->isPublic());
1726 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001727 unsigned Code;
1728 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001729 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001730 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001731 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001732
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001733 Record.push_back(MI->isC99Varargs());
1734 Record.push_back(MI->isGNUVarargs());
1735 Record.push_back(MI->getNumArgs());
1736 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1737 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001738 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001739 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001740
Douglas Gregoraae92242010-03-19 21:51:54 +00001741 // If we have a detailed preprocessing record, record the macro definition
1742 // ID that corresponds to this macro.
1743 if (PPRec)
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001744 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001745
Douglas Gregor8f45df52009-04-16 22:23:12 +00001746 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001747 Record.clear();
1748
Chris Lattner2199f5b2009-04-10 18:08:30 +00001749 // Emit the tokens array.
1750 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1751 // Note that we know that the preprocessor does not have any annotation
1752 // tokens in it because they are created by the parser, and thus can't be
1753 // in a macro definition.
1754 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001755
Chris Lattner2199f5b2009-04-10 18:08:30 +00001756 Record.push_back(Tok.getLocation().getRawEncoding());
1757 Record.push_back(Tok.getLength());
1758
Chris Lattner2199f5b2009-04-10 18:08:30 +00001759 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1760 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001761 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001762 // FIXME: Should translate token kind to a stable encoding.
1763 Record.push_back(Tok.getKind());
1764 // FIXME: Should translate token flags to a stable encoding.
1765 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001766
Sebastian Redl539c5062010-08-18 23:57:32 +00001767 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001768 Record.clear();
1769 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001770 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001771 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001772 Stream.ExitBlock();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001773}
1774
1775void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001776 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001777 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001778
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001779 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001780
Douglas Gregor92a96f52011-02-08 21:58:10 +00001781 // Enter the preprocessor block.
1782 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001783
Douglas Gregoraae92242010-03-19 21:51:54 +00001784 // If the preprocessor has a preprocessing record, emit it.
1785 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001786 using namespace llvm;
1787
1788 // Set up the abbreviation for
1789 unsigned InclusionAbbrev = 0;
1790 {
1791 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1792 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1797 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1798 }
1799
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001800 unsigned FirstPreprocessorEntityID
1801 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1802 + NUM_PREDEF_PP_ENTITY_IDS;
1803 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001804 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001805 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1806 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001807 E != EEnd;
1808 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001809 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001810
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001811 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1812 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001813
Douglas Gregor92a96f52011-02-08 21:58:10 +00001814 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001815 // Record this macro definition's ID.
1816 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001817
Douglas Gregor92a96f52011-02-08 21:58:10 +00001818 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001819 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1820 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001821 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001822
Chandler Carrutha88a22182011-07-14 08:20:46 +00001823 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001824 Record.push_back(ME->isBuiltinMacro());
1825 if (ME->isBuiltinMacro())
1826 AddIdentifierRef(ME->getName(), Record);
1827 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001828 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001829 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001830 continue;
1831 }
1832
1833 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1834 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001835 Record.push_back(ID->getFileName().size());
1836 Record.push_back(ID->wasInQuotes());
1837 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001838 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001839 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001840 // Check that the FileEntry is not null because it was not resolved and
1841 // we create a PCH even with compiler errors.
1842 if (ID->getFile())
1843 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001844 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1845 continue;
1846 }
1847
1848 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1849 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001850 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001851
Douglas Gregoraae92242010-03-19 21:51:54 +00001852 // Write the offsets table for the preprocessing record.
1853 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001854 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1855
Douglas Gregoraae92242010-03-19 21:51:54 +00001856 // Write the offsets table for identifier IDs.
1857 using namespace llvm;
1858 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001859 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001860 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001861 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001862 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001863
Douglas Gregoraae92242010-03-19 21:51:54 +00001864 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001865 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001866 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001867 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1868 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001869 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001870}
1871
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001872unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1873 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1874 if (Known != SubmoduleIDs.end())
1875 return Known->second;
1876
1877 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1878}
1879
Douglas Gregor253eefe2011-12-01 00:59:36 +00001880/// \brief Compute the number of modules within the given tree (including the
1881/// given module).
1882static unsigned getNumberOfModules(Module *Mod) {
1883 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001884 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1885 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001886 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001887 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001888
1889 return ChildModules + 1;
1890}
1891
Douglas Gregorde3ef502011-11-30 23:21:26 +00001892void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001893 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001894 // FIXME: This feels like it belongs somewhere else, but there are no
1895 // other consumers of this information.
1896 SourceManager &SrcMgr = PP->getSourceManager();
1897 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1898 for (ASTContext::import_iterator I = Context->local_import_begin(),
1899 IEnd = Context->local_import_end();
1900 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001901 if (Module *ImportedFrom
1902 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1903 SrcMgr))) {
1904 ImportedFrom->Imports.push_back(I->getImportedModule());
1905 }
1906 }
1907
Douglas Gregor69021972011-11-30 17:33:56 +00001908 // Enter the submodule description block.
1909 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1910
1911 // Write the abbreviations needed for the submodules block.
1912 using namespace llvm;
1913 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1914 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00001916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00001919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00001921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00001923 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1924 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1925
1926 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001927 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00001928 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1929 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1930
1931 Abbrev = new BitCodeAbbrev();
1932 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1934 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00001935
1936 Abbrev = new BitCodeAbbrev();
1937 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1939 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1940
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001941 Abbrev = new BitCodeAbbrev();
1942 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1944 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1945
Douglas Gregor253eefe2011-12-01 00:59:36 +00001946 // Write the submodule metadata block.
1947 RecordData Record;
1948 Record.push_back(getNumberOfModules(WritingModule));
1949 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1950 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1951
Douglas Gregor69021972011-11-30 17:33:56 +00001952 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001953 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00001954 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00001955 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001956 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00001957 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001958 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00001959
1960 // Emit the definition of the block.
1961 Record.clear();
1962 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001963 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00001964 if (Mod->Parent) {
1965 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1966 Record.push_back(SubmoduleIDs[Mod->Parent]);
1967 } else {
1968 Record.push_back(0);
1969 }
1970 Record.push_back(Mod->IsFramework);
1971 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00001972 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00001973 Record.push_back(Mod->InferSubmodules);
1974 Record.push_back(Mod->InferExplicitSubmodules);
1975 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00001976 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1977
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001978 // Emit the requirements.
1979 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1980 Record.clear();
1981 Record.push_back(SUBMODULE_REQUIRES);
1982 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1983 Mod->Requires[I].data(),
1984 Mod->Requires[I].size());
1985 }
1986
Douglas Gregor69021972011-11-30 17:33:56 +00001987 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00001988 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00001989 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001990 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00001991 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00001992 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00001993 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1994 Record.clear();
1995 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1996 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1997 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00001998 }
1999
2000 // Emit the headers.
2001 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2002 Record.clear();
2003 Record.push_back(SUBMODULE_HEADER);
2004 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2005 Mod->Headers[I]->getName());
2006 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002007
2008 // Emit the imports.
2009 if (!Mod->Imports.empty()) {
2010 Record.clear();
2011 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002012 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002013 assert(ImportedID && "Unknown submodule!");
2014 Record.push_back(ImportedID);
2015 }
2016 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2017 }
2018
Douglas Gregor24bb9232011-12-02 18:58:38 +00002019 // Emit the exports.
2020 if (!Mod->Exports.empty()) {
2021 Record.clear();
2022 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002023 if (Module *Exported = Mod->Exports[I].getPointer()) {
2024 unsigned ExportedID = SubmoduleIDs[Exported];
2025 assert(ExportedID > 0 && "Unknown submodule ID?");
2026 Record.push_back(ExportedID);
2027 } else {
2028 Record.push_back(0);
2029 }
2030
Douglas Gregor24bb9232011-12-02 18:58:38 +00002031 Record.push_back(Mod->Exports[I].getInt());
2032 }
2033 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2034 }
2035
Douglas Gregor69021972011-11-30 17:33:56 +00002036 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002037 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2038 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002039 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002040 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002041 }
2042
2043 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002044
2045 assert((NextSubmoduleID - FirstSubmoduleID
2046 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002047}
2048
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002049serialization::SubmoduleID
2050ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002051 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002052 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002053
2054 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002055 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002056 Module *OwningMod
2057 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002058 if (!OwningMod)
2059 return 0;
2060
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002061 // Check whether this submodule is part of our own module.
2062 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002063 return 0;
2064
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002065 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002066}
2067
David Blaikie9c902b52011-09-25 23:23:43 +00002068void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002069 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002070 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002071 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2072 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002073 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002074 if (point.Loc.isInvalid())
2075 continue;
2076
2077 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002078 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002079 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002080 if (I->second.isPragma()) {
2081 Record.push_back(I->first);
2082 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002083 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002084 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002085 Record.push_back(-1); // mark the end of the diag/map pairs for this
2086 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002087 }
2088
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002089 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002090 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002091}
2092
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002093void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2094 if (CXXBaseSpecifiersOffsets.empty())
2095 return;
2096
2097 RecordData Record;
2098
2099 // Create a blob abbreviation for the C++ base specifiers offsets.
2100 using namespace llvm;
2101
2102 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2103 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2106 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2107
Douglas Gregorc27b2872011-08-04 00:01:48 +00002108 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002109 Record.clear();
2110 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2111 Record.push_back(CXXBaseSpecifiersOffsets.size());
2112 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002113 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002114}
2115
Douglas Gregorc5046832009-04-27 18:38:38 +00002116//===----------------------------------------------------------------------===//
2117// Type Serialization
2118//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002119
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002120/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002121void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002122 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002123 if (Idx.getIndex() == 0) // we haven't seen this type before.
2124 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002125
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002126 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002127
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002128 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002129 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002130 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002131 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002132 else if (TypeOffsets.size() < Index) {
2133 TypeOffsets.resize(Index + 1);
2134 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002135 }
2136
2137 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002138
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002139 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002140 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002141
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002142 if (T.hasLocalNonFastQualifiers()) {
2143 Qualifiers Qs = T.getLocalQualifiers();
2144 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002145 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002146 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002147 } else {
2148 switch (T->getTypeClass()) {
2149 // For all of the concrete, non-dependent types, call the
2150 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002151#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002152 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002154#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002155 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002156 }
2157
2158 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002159 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002160
2161 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002162 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002163}
2164
Douglas Gregorc5046832009-04-27 18:38:38 +00002165//===----------------------------------------------------------------------===//
2166// Declaration Serialization
2167//===----------------------------------------------------------------------===//
2168
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002169/// \brief Write the block containing all of the declaration IDs
2170/// lexically declared within the given DeclContext.
2171///
2172/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2173/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002174uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002175 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002176 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002177 return 0;
2178
Douglas Gregor8f45df52009-04-16 22:23:12 +00002179 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002180 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002181 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002182 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002183 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2184 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002185 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002186
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002187 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002188 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002189 return Offset;
2190}
2191
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002192void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002193 using namespace llvm;
2194 RecordData Record;
2195
2196 // Write the type offsets array
2197 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002198 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2202 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2203 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002204 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002205 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002206 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002207 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002208
2209 // Write the declaration offsets array
2210 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002211 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2215 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2216 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002217 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002218 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002219 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002220 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002221}
2222
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002223void ASTWriter::WriteFileDeclIDsMap() {
2224 using namespace llvm;
2225 RecordData Record;
2226
2227 // Join the vectors of DeclIDs from all files.
2228 SmallVector<DeclID, 256> FileSortedIDs;
2229 for (FileDeclIDsTy::iterator
2230 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2231 DeclIDInFileInfo &Info = *FI->second;
2232 Info.FirstDeclIndex = FileSortedIDs.size();
2233 for (LocDeclIDsTy::iterator
2234 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2235 FileSortedIDs.push_back(DI->second);
2236 }
2237
2238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2239 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2241 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2242 Record.push_back(FILE_SORTED_DECLS);
2243 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2244}
2245
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002246void ASTWriter::WriteComments() {
2247 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002248 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002249 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002250 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2251 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002252 I != E; ++I) {
2253 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002254 AddSourceRange((*I)->getSourceRange(), Record);
2255 Record.push_back((*I)->getKind());
2256 Record.push_back((*I)->isTrailingComment());
2257 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002258 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2259 }
2260 Stream.ExitBlock();
2261}
2262
Douglas Gregorc5046832009-04-27 18:38:38 +00002263//===----------------------------------------------------------------------===//
2264// Global Method Pool and Selector Serialization
2265//===----------------------------------------------------------------------===//
2266
Douglas Gregore84a9da2009-04-20 20:36:09 +00002267namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002268// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002269class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002270 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002271
2272public:
2273 typedef Selector key_type;
2274 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002275
Sebastian Redl834bb972010-08-04 17:20:04 +00002276 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002277 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002278 ObjCMethodList Instance, Factory;
2279 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002280 typedef const data_type& data_type_ref;
2281
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002282 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002283
Douglas Gregorc78d3462009-04-24 21:10:55 +00002284 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002285 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002286 }
Mike Stump11289f42009-09-09 15:08:12 +00002287
2288 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002289 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002290 data_type_ref Methods) {
2291 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2292 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002293 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2294 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002295 Method = Method->Next)
2296 if (Method->Method)
2297 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002298 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002299 Method = Method->Next)
2300 if (Method->Method)
2301 DataLen += 4;
2302 clang::io::Emit16(Out, DataLen);
2303 return std::make_pair(KeyLen, DataLen);
2304 }
Mike Stump11289f42009-09-09 15:08:12 +00002305
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002306 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002307 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002308 assert((Start >> 32) == 0 && "Selector key offset too large");
2309 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002310 unsigned N = Sel.getNumArgs();
2311 clang::io::Emit16(Out, N);
2312 if (N == 0)
2313 N = 1;
2314 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002315 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002316 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2317 }
Mike Stump11289f42009-09-09 15:08:12 +00002318
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002319 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002320 data_type_ref Methods, unsigned DataLen) {
2321 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002322 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002323 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002324 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002325 Method = Method->Next)
2326 if (Method->Method)
2327 ++NumInstanceMethods;
2328
2329 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002330 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002331 Method = Method->Next)
2332 if (Method->Method)
2333 ++NumFactoryMethods;
2334
2335 clang::io::Emit16(Out, NumInstanceMethods);
2336 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002337 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002338 Method = Method->Next)
2339 if (Method->Method)
2340 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002341 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002342 Method = Method->Next)
2343 if (Method->Method)
2344 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002345
2346 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002347 }
2348};
2349} // end anonymous namespace
2350
Sebastian Redla19a67f2010-08-03 21:58:15 +00002351/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002352///
2353/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002354/// in an on-disk hash table indexed by the selector. The hash table also
2355/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002356void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002357 using namespace llvm;
2358
Sebastian Redla19a67f2010-08-03 21:58:15 +00002359 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002360 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002361 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002362 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002363 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002364 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002365 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002366 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002367
Sebastian Redla19a67f2010-08-03 21:58:15 +00002368 // Create the on-disk hash table representation. We walk through every
2369 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002370 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002371 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002372 I = SelectorIDs.begin(), E = SelectorIDs.end();
2373 I != E; ++I) {
2374 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002375 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002376 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002377 I->second,
2378 ObjCMethodList(),
2379 ObjCMethodList()
2380 };
2381 if (F != SemaRef.MethodPool.end()) {
2382 Data.Instance = F->second.first;
2383 Data.Factory = F->second.second;
2384 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002385 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002386 // changed.
2387 if (Chain && I->second < FirstSelectorID) {
2388 // Selector already exists. Did it change?
2389 bool changed = false;
2390 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2391 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002392 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002393 changed = true;
2394 }
2395 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2396 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002397 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002398 changed = true;
2399 }
2400 if (!changed)
2401 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002402 } else if (Data.Instance.Method || Data.Factory.Method) {
2403 // A new method pool entry.
2404 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002405 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002406 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002407 }
2408
Douglas Gregorc78d3462009-04-24 21:10:55 +00002409 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002410 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002411 uint32_t BucketOffset;
2412 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002413 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002414 llvm::raw_svector_ostream Out(MethodPool);
2415 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002416 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002417 BucketOffset = Generator.Emit(Out, Trait);
2418 }
2419
2420 // Create a blob abbreviation
2421 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002422 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002425 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2426 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2427
Douglas Gregor95c13f52009-04-25 17:48:32 +00002428 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002429 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002430 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002431 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002432 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002433 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002434
2435 // Create a blob abbreviation for the selector table offsets.
2436 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002437 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002440 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2441 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2442
2443 // Write the selector offsets table.
2444 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002445 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002446 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002447 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002448 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002449 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002450 }
2451}
2452
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002453/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002454void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002455 using namespace llvm;
2456 if (SemaRef.ReferencedSelectors.empty())
2457 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002458
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002459 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002460
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002461 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002462 // very tricky to fix, and given that @selector shouldn't really appear in
2463 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002464 for (DenseMap<Selector, SourceLocation>::iterator S =
2465 SemaRef.ReferencedSelectors.begin(),
2466 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2467 Selector Sel = (*S).first;
2468 SourceLocation Loc = (*S).second;
2469 AddSelectorRef(Sel, Record);
2470 AddSourceLocation(Loc, Record);
2471 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002472 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002473}
2474
Douglas Gregorc5046832009-04-27 18:38:38 +00002475//===----------------------------------------------------------------------===//
2476// Identifier Table Serialization
2477//===----------------------------------------------------------------------===//
2478
Douglas Gregorc78d3462009-04-24 21:10:55 +00002479namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002480class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002481 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002482 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002483 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002484 bool IsModule;
2485
Douglas Gregor1d583f22009-04-28 21:18:29 +00002486 /// \brief Determines whether this is an "interesting" identifier
2487 /// that needs a full IdentifierInfo structure written into the hash
2488 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002489 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002490 if (II->isPoisoned() ||
2491 II->isExtensionToken() ||
2492 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002493 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002494 II->getFETokenInfo<void>())
2495 return true;
2496
Douglas Gregord7910e92011-09-14 22:14:14 +00002497 return hasMacroDefinition(II, Macro);
2498 }
2499
2500 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002501 if (!II->hasMacroDefinition())
2502 return false;
2503
Douglas Gregord7910e92011-09-14 22:14:14 +00002504 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002505 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002506
Douglas Gregord7910e92011-09-14 22:14:14 +00002507 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002508 }
2509
Douglas Gregore84a9da2009-04-20 20:36:09 +00002510public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002511 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002512 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002513
Sebastian Redl539c5062010-08-18 23:57:32 +00002514 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002515 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002516
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002517 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2518 IdentifierResolver &IdResolver, bool IsModule)
2519 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002520
2521 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002522 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002523 }
Mike Stump11289f42009-09-09 15:08:12 +00002524
2525 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002526 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002527 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002528 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002529 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002530 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002531 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregord7910e92011-09-14 22:14:14 +00002532 if (hasMacroDefinition(II, Macro))
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002533 DataLen += 8;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002534
2535 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2536 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002537 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002538 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002539 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002540 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002541 // We emit the key length after the data length so that every
2542 // string is preceded by a 16-bit length. This matches the PTH
2543 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002544 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002545 return std::make_pair(KeyLen, DataLen);
2546 }
Mike Stump11289f42009-09-09 15:08:12 +00002547
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002548 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002549 unsigned KeyLen) {
2550 // Record the location of the key data. This is used when generating
2551 // the mapping from persistent IDs to strings.
2552 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002553 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002554 }
Mike Stump11289f42009-09-09 15:08:12 +00002555
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002556 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002557 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002558 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002559 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002560 clang::io::Emit32(Out, ID << 1);
2561 return;
2562 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002563
Douglas Gregor1d583f22009-04-28 21:18:29 +00002564 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002565 uint32_t Bits = 0;
Douglas Gregord7910e92011-09-14 22:14:14 +00002566 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregorb9256522009-04-28 21:32:13 +00002567 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topperdec792e2011-12-19 05:04:33 +00002568 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregord7910e92011-09-14 22:14:14 +00002569 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002570 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2571 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002572 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002573 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002574 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002575
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002576 if (HasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002577 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002578 clang::io::Emit32(Out,
2579 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2580 }
2581
Douglas Gregora868bbd2009-04-21 22:25:48 +00002582 // Emit the declaration IDs in reverse order, because the
2583 // IdentifierResolver provides the declarations as they would be
2584 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002585 // "stat"), but the ASTReader adds declarations to the end of the list
2586 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002587 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002588 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2589 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002590 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002591 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002592 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002593 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002594 }
2595};
2596} // end anonymous namespace
2597
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002598/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002599///
2600/// The identifier table consists of a blob containing string data
2601/// (the actual identifiers themselves) and a separate "offsets" index
2602/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002603void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2604 IdentifierResolver &IdResolver,
2605 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002606 using namespace llvm;
2607
2608 // Create and write out the blob that contains the identifier
2609 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002610 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002611 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002612 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002613
Douglas Gregore6648fb2009-04-28 20:33:11 +00002614 // Look for any identifiers that were named while processing the
2615 // headers, but are otherwise not needed. We add these to the hash
2616 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002617 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002618 // file.
2619 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2620 IDEnd = PP.getIdentifierTable().end();
2621 ID != IDEnd; ++ID)
2622 getIdentifierRef(ID->second);
2623
Sebastian Redlff4a2952010-07-23 23:49:55 +00002624 // Create the on-disk hash table representation. We only store offsets
2625 // for identifiers that appear here for the first time.
2626 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002627 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002628 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2629 ID != IDEnd; ++ID) {
2630 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002631 if (!Chain || !ID->first->isFromAST() ||
2632 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002633 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2634 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002635 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002636
Douglas Gregore84a9da2009-04-20 20:36:09 +00002637 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002638 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002639 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002640 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002641 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002642 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002643 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002644 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002645 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002646 }
2647
2648 // Create a blob abbreviation
2649 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002650 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002652 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002653 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002654
2655 // Write the identifier table
2656 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002657 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002658 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002659 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002660 }
2661
2662 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002663 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002664 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2668 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2669
2670 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002671 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002672 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002673 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002674 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002675 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002676}
2677
Douglas Gregorc5046832009-04-27 18:38:38 +00002678//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002679// DeclContext's Name Lookup Table Serialization
2680//===----------------------------------------------------------------------===//
2681
2682namespace {
2683// Trait used for the on-disk hash table used in the method pool.
2684class ASTDeclContextNameLookupTrait {
2685 ASTWriter &Writer;
2686
2687public:
2688 typedef DeclarationName key_type;
2689 typedef key_type key_type_ref;
2690
2691 typedef DeclContext::lookup_result data_type;
2692 typedef const data_type& data_type_ref;
2693
2694 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2695
2696 unsigned ComputeHash(DeclarationName Name) {
2697 llvm::FoldingSetNodeID ID;
2698 ID.AddInteger(Name.getNameKind());
2699
2700 switch (Name.getNameKind()) {
2701 case DeclarationName::Identifier:
2702 ID.AddString(Name.getAsIdentifierInfo()->getName());
2703 break;
2704 case DeclarationName::ObjCZeroArgSelector:
2705 case DeclarationName::ObjCOneArgSelector:
2706 case DeclarationName::ObjCMultiArgSelector:
2707 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2708 break;
2709 case DeclarationName::CXXConstructorName:
2710 case DeclarationName::CXXDestructorName:
2711 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002712 break;
2713 case DeclarationName::CXXOperatorName:
2714 ID.AddInteger(Name.getCXXOverloadedOperator());
2715 break;
2716 case DeclarationName::CXXLiteralOperatorName:
2717 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2718 case DeclarationName::CXXUsingDirective:
2719 break;
2720 }
2721
2722 return ID.ComputeHash();
2723 }
2724
2725 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002726 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002727 data_type_ref Lookup) {
2728 unsigned KeyLen = 1;
2729 switch (Name.getNameKind()) {
2730 case DeclarationName::Identifier:
2731 case DeclarationName::ObjCZeroArgSelector:
2732 case DeclarationName::ObjCOneArgSelector:
2733 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002734 case DeclarationName::CXXLiteralOperatorName:
2735 KeyLen += 4;
2736 break;
2737 case DeclarationName::CXXOperatorName:
2738 KeyLen += 1;
2739 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002740 case DeclarationName::CXXConstructorName:
2741 case DeclarationName::CXXDestructorName:
2742 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002743 case DeclarationName::CXXUsingDirective:
2744 break;
2745 }
2746 clang::io::Emit16(Out, KeyLen);
2747
2748 // 2 bytes for num of decls and 4 for each DeclID.
2749 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2750 clang::io::Emit16(Out, DataLen);
2751
2752 return std::make_pair(KeyLen, DataLen);
2753 }
2754
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002755 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002756 using namespace clang::io;
2757
2758 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2759 Emit8(Out, Name.getNameKind());
2760 switch (Name.getNameKind()) {
2761 case DeclarationName::Identifier:
2762 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2763 break;
2764 case DeclarationName::ObjCZeroArgSelector:
2765 case DeclarationName::ObjCOneArgSelector:
2766 case DeclarationName::ObjCMultiArgSelector:
2767 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2768 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002769 case DeclarationName::CXXOperatorName:
2770 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2771 Emit8(Out, Name.getCXXOverloadedOperator());
2772 break;
2773 case DeclarationName::CXXLiteralOperatorName:
2774 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2775 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002776 case DeclarationName::CXXConstructorName:
2777 case DeclarationName::CXXDestructorName:
2778 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002779 case DeclarationName::CXXUsingDirective:
2780 break;
2781 }
2782 }
2783
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002784 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002785 data_type Lookup, unsigned DataLen) {
2786 uint64_t Start = Out.tell(); (void)Start;
2787 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2788 for (; Lookup.first != Lookup.second; ++Lookup.first)
2789 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2790
2791 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2792 }
2793};
2794} // end anonymous namespace
2795
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002796/// \brief Write the block containing all of the declaration IDs
2797/// visible from the given DeclContext.
2798///
2799/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002800/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002801uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2802 DeclContext *DC) {
2803 if (DC->getPrimaryContext() != DC)
2804 return 0;
2805
2806 // Since there is no name lookup into functions or methods, don't bother to
2807 // build a visible-declarations table for these entities.
2808 if (DC->isFunctionOrMethod())
2809 return 0;
2810
2811 // If not in C++, we perform name lookup for the translation unit via the
2812 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2813 // FIXME: In C++ we need the visible declarations in order to "see" the
2814 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00002815 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002816 return 0;
2817
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002818 // Serialize the contents of the mapping used for lookup. Note that,
2819 // although we have two very different code paths, the serialized
2820 // representation is the same for both cases: a declaration name,
2821 // followed by a size, followed by references to the visible
2822 // declarations that have that name.
2823 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00002824 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002825 if (!Map || Map->empty())
2826 return 0;
2827
2828 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2829 ASTDeclContextNameLookupTrait Trait(*this);
2830
2831 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002832 DeclarationName ConversionName;
2833 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002834 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2835 D != DEnd; ++D) {
2836 DeclarationName Name = D->first;
2837 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002838 if (Result.first != Result.second) {
2839 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2840 // Hash all conversion function names to the same name. The actual
2841 // type information in conversion function name is not used in the
2842 // key (since such type information is not stable across different
2843 // modules), so the intended effect is to coalesce all of the conversion
2844 // functions under a single key.
2845 if (!ConversionName)
2846 ConversionName = Name;
2847 ConversionDecls.append(Result.first, Result.second);
2848 continue;
2849 }
2850
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002851 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002852 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002853 }
2854
Douglas Gregor05ef9312011-08-30 20:49:19 +00002855 // Add the conversion functions
2856 if (!ConversionDecls.empty()) {
2857 Generator.insert(ConversionName,
2858 DeclContext::lookup_result(ConversionDecls.begin(),
2859 ConversionDecls.end()),
2860 Trait);
2861 }
2862
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002863 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002864 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002865 uint32_t BucketOffset;
2866 {
2867 llvm::raw_svector_ostream Out(LookupTable);
2868 // Make sure that no bucket is at offset 0
2869 clang::io::Emit32(Out, 0);
2870 BucketOffset = Generator.Emit(Out, Trait);
2871 }
2872
2873 // Write the lookup table
2874 RecordData Record;
2875 Record.push_back(DECL_CONTEXT_VISIBLE);
2876 Record.push_back(BucketOffset);
2877 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2878 LookupTable.str());
2879
2880 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2881 ++NumVisibleDeclContexts;
2882 return Offset;
2883}
2884
Sebastian Redla4071b42010-08-24 00:50:09 +00002885/// \brief Write an UPDATE_VISIBLE block for the given context.
2886///
2887/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2888/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00002889/// (in C++), for namespaces, and for classes with forward-declared unscoped
2890/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00002891void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002892 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2893 if (!Map || Map->empty())
2894 return;
2895
2896 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2897 ASTDeclContextNameLookupTrait Trait(*this);
2898
2899 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002900 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2901 D != DEnd; ++D) {
2902 DeclarationName Name = D->first;
2903 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002904 // For any name that appears in this table, the results are complete, i.e.
2905 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002906 if (Result.first != Result.second)
2907 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002908 }
2909
2910 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002911 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00002912 uint32_t BucketOffset;
2913 {
2914 llvm::raw_svector_ostream Out(LookupTable);
2915 // Make sure that no bucket is at offset 0
2916 clang::io::Emit32(Out, 0);
2917 BucketOffset = Generator.Emit(Out, Trait);
2918 }
2919
2920 // Write the lookup table
2921 RecordData Record;
2922 Record.push_back(UPDATE_VISIBLE);
2923 Record.push_back(getDeclID(cast<Decl>(DC)));
2924 Record.push_back(BucketOffset);
2925 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2926}
2927
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002928/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2929void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2930 RecordData Record;
2931 Record.push_back(Opts.fp_contract);
2932 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2933}
2934
2935/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2936void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002937 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002938 return;
2939
2940 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2941 RecordData Record;
2942#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2943#include "clang/Basic/OpenCLExtensions.def"
2944 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2945}
2946
Douglas Gregor358cd442012-01-15 16:58:34 +00002947void ASTWriter::WriteRedeclarations() {
2948 RecordData LocalRedeclChains;
2949 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2950
2951 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2952 Decl *First = Redeclarations[I];
2953 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2954
2955 Decl *MostRecent = First->getMostRecentDecl();
2956
2957 // If we only have a single declaration, there is no point in storing
2958 // a redeclaration chain.
2959 if (First == MostRecent)
2960 continue;
2961
2962 unsigned Offset = LocalRedeclChains.size();
2963 unsigned Size = 0;
2964 LocalRedeclChains.push_back(0); // Placeholder for the size.
2965
2966 // Collect the set of local redeclarations of this declaration.
2967 for (Decl *Prev = MostRecent; Prev != First;
2968 Prev = Prev->getPreviousDecl()) {
2969 if (!Prev->isFromASTFile()) {
2970 AddDeclRef(Prev, LocalRedeclChains);
2971 ++Size;
2972 }
2973 }
2974 LocalRedeclChains[Offset] = Size;
2975
2976 // Reverse the set of local redeclarations, so that we store them in
2977 // order (since we found them in reverse order).
2978 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2979
2980 // Add the mapping from the first ID to the set of local declarations.
2981 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2982 LocalRedeclsMap.push_back(Info);
2983
2984 assert(N == Redeclarations.size() &&
2985 "Deserialized a declaration we shouldn't have");
2986 }
2987
2988 if (LocalRedeclChains.empty())
2989 return;
2990
2991 // Sort the local redeclarations map by the first declaration ID,
2992 // since the reader will be performing binary searches on this information.
2993 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2994
2995 // Emit the local redeclarations map.
2996 using namespace llvm;
2997 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2998 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3001 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3002
3003 RecordData Record;
3004 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3005 Record.push_back(LocalRedeclsMap.size());
3006 Stream.EmitRecordWithBlob(AbbrevID, Record,
3007 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3008 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3009
3010 // Emit the redeclaration chains.
3011 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3012}
3013
Douglas Gregor404cdde2012-01-27 01:47:08 +00003014void ASTWriter::WriteObjCCategories() {
3015 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3016 RecordData Categories;
3017
3018 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3019 unsigned Size = 0;
3020 unsigned StartIndex = Categories.size();
3021
3022 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3023
3024 // Allocate space for the size.
3025 Categories.push_back(0);
3026
3027 // Add the categories.
3028 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3029 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3030 assert(getDeclID(Cat) != 0 && "Bogus category");
3031 AddDeclRef(Cat, Categories);
3032 }
3033
3034 // Update the size.
3035 Categories[StartIndex] = Size;
3036
3037 // Record this interface -> category map.
3038 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3039 CategoriesMap.push_back(CatInfo);
3040 }
3041
3042 // Sort the categories map by the definition ID, since the reader will be
3043 // performing binary searches on this information.
3044 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3045
3046 // Emit the categories map.
3047 using namespace llvm;
3048 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3049 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3052 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3053
3054 RecordData Record;
3055 Record.push_back(OBJC_CATEGORIES_MAP);
3056 Record.push_back(CategoriesMap.size());
3057 Stream.EmitRecordWithBlob(AbbrevID, Record,
3058 reinterpret_cast<char*>(CategoriesMap.data()),
3059 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3060
3061 // Emit the category lists.
3062 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3063}
3064
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003065void ASTWriter::WriteMergedDecls() {
3066 if (!Chain || Chain->MergedDecls.empty())
3067 return;
3068
3069 RecordData Record;
3070 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3071 IEnd = Chain->MergedDecls.end();
3072 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003073 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003074 : getDeclID(I->first);
3075 assert(CanonID && "Merged declaration not known?");
3076
3077 Record.push_back(CanonID);
3078 Record.push_back(I->second.size());
3079 Record.append(I->second.begin(), I->second.end());
3080 }
3081 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3082}
3083
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003084//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003085// General Serialization Routines
3086//===----------------------------------------------------------------------===//
3087
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003088/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003089void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3090 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003091 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003092 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3093 e = Attrs.end(); i != e; ++i){
3094 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003095 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003096 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003097
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003098#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003099
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003100 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003101}
3102
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003103void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003104 Record.push_back(Str.size());
3105 Record.insert(Record.end(), Str.begin(), Str.end());
3106}
3107
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003108void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3109 RecordDataImpl &Record) {
3110 Record.push_back(Version.getMajor());
3111 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3112 Record.push_back(*Minor + 1);
3113 else
3114 Record.push_back(0);
3115 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3116 Record.push_back(*Subminor + 1);
3117 else
3118 Record.push_back(0);
3119}
3120
Douglas Gregore84a9da2009-04-20 20:36:09 +00003121/// \brief Note that the identifier II occurs at the given offset
3122/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003123void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003124 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003125 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003126 // up earlier in the chain and thus don't need an offset.
3127 if (ID >= FirstIdentID)
3128 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003129}
3130
Douglas Gregor95c13f52009-04-25 17:48:32 +00003131/// \brief Note that the selector Sel occurs at the given offset
3132/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003133void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003134 unsigned ID = SelectorIDs[Sel];
3135 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003136 // Don't record offsets for selectors that are also available in a different
3137 // file.
3138 if (ID < FirstSelectorID)
3139 return;
3140 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003141}
3142
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003143ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003144 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003145 WritingAST(false), DoneWritingDeclsAndTypes(false),
3146 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003147 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003148 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003149 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003150 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3151 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003152 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003153 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003154 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003155 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003156 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003157 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003158 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3159 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3160 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003161 DeclTypedefAbbrev(0),
3162 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3163 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003164{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003165}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003166
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003167ASTWriter::~ASTWriter() {
3168 for (FileDeclIDsTy::iterator
3169 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3170 delete I->second;
3171}
3172
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003173void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003174 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003175 Module *WritingModule, StringRef isysroot,
3176 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003177 WritingAST = true;
3178
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003179 ASTHasCompilerErrors = hasErrors;
3180
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003181 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003182 Stream.Emit((unsigned)'C', 8);
3183 Stream.Emit((unsigned)'P', 8);
3184 Stream.Emit((unsigned)'C', 8);
3185 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003186
Chris Lattner28fa4e62009-04-26 22:26:21 +00003187 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003188
Douglas Gregoreda8e122011-08-09 15:13:55 +00003189 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003190 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003191 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003192 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003193 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003194 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003195 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003196
3197 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003198}
3199
Douglas Gregora94a1542011-07-27 21:45:57 +00003200template<typename Vector>
3201static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3202 ASTWriter::RecordData &Record) {
3203 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3204 I != E; ++I) {
3205 Writer.AddDeclRef(*I, Record);
3206 }
3207}
3208
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003209void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003210 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003211 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003212 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003213 using namespace llvm;
3214
Douglas Gregorcf68c582011-12-01 22:20:10 +00003215 // Make sure that the AST reader knows to finalize itself.
3216 if (Chain)
3217 Chain->finalizeForWriting();
3218
Sebastian Redl143413f2010-07-12 22:02:52 +00003219 ASTContext &Context = SemaRef.Context;
3220 Preprocessor &PP = SemaRef.PP;
3221
Douglas Gregordab42432011-08-12 00:15:20 +00003222 // Set up predefined declaration IDs.
3223 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003224 if (Context.ObjCIdDecl)
3225 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003226 if (Context.ObjCSelDecl)
3227 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003228 if (Context.ObjCClassDecl)
3229 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003230 if (Context.ObjCProtocolClassDecl)
3231 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003232 if (Context.Int128Decl)
3233 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3234 if (Context.UInt128Decl)
3235 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003236 if (Context.ObjCInstanceTypeDecl)
3237 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003238 if (Context.BuiltinVaListDecl)
3239 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3240
Douglas Gregor851443c2011-08-12 01:39:19 +00003241 if (!Chain) {
3242 // Make sure that we emit IdentifierInfos (and any attached
3243 // declarations) for builtins. We don't need to do this when we're
3244 // emitting chained PCH files, because all of the builtins will be
3245 // in the original PCH file.
3246 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003247 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003248 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003249 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003250 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003251 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3252 getIdentifierRef(&Table.get(BuiltinNames[I]));
3253 }
3254
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003255 // If there are any out-of-date identifiers, bring them up to date.
3256 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3257 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3258 IDEnd = PP.getIdentifierTable().end();
3259 ID != IDEnd; ++ID)
3260 if (ID->second->isOutOfDate())
3261 ExtSource->updateOutOfDateIdentifier(*ID->second);
3262 }
3263
Chris Lattner0c797362009-09-08 18:19:27 +00003264 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003265 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003266 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003267 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003268 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003269
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003270 // Build a record containing all of the file scoped decls in this file.
3271 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003272 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3273 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003274
Douglas Gregor851443c2011-08-12 01:39:19 +00003275 // Build a record containing all of the delegating constructors we still need
3276 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003277 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003278 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003279
Douglas Gregor851443c2011-08-12 01:39:19 +00003280 // Write the set of weak, undeclared identifiers. We always write the
3281 // entire table, since later PCH files in a PCH chain are only interested in
3282 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003283 RecordData WeakUndeclaredIdentifiers;
3284 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003285 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003286 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3287 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3288 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3289 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3290 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3291 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3292 }
3293 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003294
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003295 // Build a record containing all of the locally-scoped external
3296 // declarations in this header file. Generally, this record will be
3297 // empty.
3298 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003299 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003300 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003301 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003302 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3303 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003304 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003305 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003306 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3307 }
3308
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003309 // Build a record containing all of the ext_vector declarations.
3310 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003311 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003312
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003313 // Build a record containing all of the VTable uses information.
3314 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003315 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003316 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3317 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3318 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3319 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3320 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003321 }
3322
3323 // Build a record containing all of dynamic classes declarations.
3324 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003325 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003326
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003327 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003328 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003329 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003330 I = SemaRef.PendingInstantiations.begin(),
3331 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3332 AddDeclRef(I->first, PendingInstantiations);
3333 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003334 }
3335 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3336 "There are local ones at end of translation unit!");
3337
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003338 // Build a record containing some declaration references.
3339 RecordData SemaDeclRefs;
3340 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3341 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3342 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3343 }
3344
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003345 RecordData CUDASpecialDeclRefs;
3346 if (Context.getcudaConfigureCallDecl()) {
3347 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3348 }
3349
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003350 // Build a record containing all of the known namespaces.
3351 RecordData KnownNamespaces;
3352 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3353 I = SemaRef.KnownNamespaces.begin(),
3354 IEnd = SemaRef.KnownNamespaces.end();
3355 I != IEnd; ++I) {
3356 if (!I->second)
3357 AddDeclRef(I->first, KnownNamespaces);
3358 }
3359
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003360 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003361 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003362 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003363 WriteMetadata(Context, isysroot, OutputFile);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003364 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregorc567ba22011-07-22 16:35:34 +00003365 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003366 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003367
3368 // Create a lexical update block containing all of the declarations in the
3369 // translation unit that do not come from other AST files.
3370 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3371 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3372 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3373 E = TU->noload_decls_end();
3374 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003375 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003376 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003377 }
3378
3379 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3380 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3381 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3382 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3383 Record.clear();
3384 Record.push_back(TU_UPDATE_LEXICAL);
3385 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3386 data(NewGlobalDecls));
3387
3388 // And a visible updates block for the translation unit.
3389 Abv = new llvm::BitCodeAbbrev();
3390 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3391 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3392 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3393 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3394 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3395 WriteDeclContextVisibleUpdate(TU);
3396
3397 // If the translation unit has an anonymous namespace, and we don't already
3398 // have an update block for it, write it as an update block.
3399 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3400 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3401 if (Record.empty()) {
3402 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003403 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003404 }
3405 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003406
3407 // Make sure visible decls, added to DeclContexts previously loaded from
3408 // an AST file, are registered for serialization.
3409 for (SmallVector<const Decl *, 16>::iterator
3410 I = UpdatingVisibleDecls.begin(),
3411 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3412 GetDeclRef(*I);
3413 }
3414
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003415 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003416 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003417
Douglas Gregor5204bde2011-08-02 16:26:37 +00003418 // Form the record of special types.
3419 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003420 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003421 AddTypeRef(Context.getFILEType(), SpecialTypes);
3422 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3423 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3424 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3425 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003426 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003427 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003428
Douglas Gregor1970d882009-04-26 03:49:13 +00003429 // Keep writing types and declarations until all types and
3430 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003431 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003432 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003433 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3434 E = DeclsToRewrite.end();
3435 I != E; ++I)
3436 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003437 while (!DeclTypesToEmit.empty()) {
3438 DeclOrType DOT = DeclTypesToEmit.front();
3439 DeclTypesToEmit.pop();
3440 if (DOT.isType())
3441 WriteType(DOT.getType());
3442 else
3443 WriteDecl(Context, DOT.getDecl());
3444 }
3445 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003446
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003447 DoneWritingDeclsAndTypes = true;
3448
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003449 WriteFileDeclIDsMap();
3450 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003451 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003452
3453 if (Chain) {
3454 // Write the mapping information describing our module dependencies and how
3455 // each of those modules were mapped into our own offset/ID space, so that
3456 // the reader can build the appropriate mapping to its own offset/ID space.
3457 // The map consists solely of a blob with the following format:
3458 // *(module-name-len:i16 module-name:len*i8
3459 // source-location-offset:i32
3460 // identifier-id:i32
3461 // preprocessed-entity-id:i32
3462 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003463 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003464 // selector-id:i32
3465 // declaration-id:i32
3466 // c++-base-specifiers-id:i32
3467 // type-id:i32)
3468 //
3469 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3470 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3471 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3472 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003473 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003474 {
3475 llvm::raw_svector_ostream Out(Buffer);
3476 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003477 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003478 M != MEnd; ++M) {
3479 StringRef FileName = (*M)->FileName;
3480 io::Emit16(Out, FileName.size());
3481 Out.write(FileName.data(), FileName.size());
3482 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3483 io::Emit32(Out, (*M)->BaseIdentifierID);
3484 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003485 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003486 io::Emit32(Out, (*M)->BaseSelectorID);
3487 io::Emit32(Out, (*M)->BaseDeclID);
3488 io::Emit32(Out, (*M)->BaseTypeIndex);
3489 }
3490 }
3491 Record.clear();
3492 Record.push_back(MODULE_OFFSET_MAP);
3493 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3494 Buffer.data(), Buffer.size());
3495 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003496 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003497 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003498 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003499 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003500 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003501 WriteFPPragmaOptions(SemaRef.getFPOptions());
3502 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003503
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003504 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003505 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003506
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003507 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003508
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003509 // If we're emitting a module, write out the submodule information.
3510 if (WritingModule)
3511 WriteSubmodules(WritingModule);
3512
Douglas Gregor5204bde2011-08-02 16:26:37 +00003513 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3514
Douglas Gregord4df8652009-04-22 22:02:47 +00003515 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003516 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003517 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003518
3519 // Write the record containing tentative definitions.
3520 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003521 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003522
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003523 // Write the record containing unused file scoped decls.
3524 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003525 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003526
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003527 // Write the record containing weak undeclared identifiers.
3528 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003529 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003530 WeakUndeclaredIdentifiers);
3531
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003532 // Write the record containing locally-scoped external definitions.
3533 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003534 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003535 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003536
3537 // Write the record containing ext_vector type names.
3538 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003539 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003540
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003541 // Write the record containing VTable uses information.
3542 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003543 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003544
3545 // Write the record containing dynamic classes declarations.
3546 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003547 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003548
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003549 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003550 if (!PendingInstantiations.empty())
3551 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003552
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003553 // Write the record containing declaration references of Sema.
3554 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003555 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003556
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003557 // Write the record containing CUDA-specific declaration references.
3558 if (!CUDASpecialDeclRefs.empty())
3559 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003560
3561 // Write the delegating constructors.
3562 if (!DelegatingCtorDecls.empty())
3563 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003564
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003565 // Write the known namespaces.
3566 if (!KnownNamespaces.empty())
3567 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3568
Douglas Gregor851443c2011-08-12 01:39:19 +00003569 // Write the visible updates to DeclContexts.
3570 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3571 I = UpdatedDeclContexts.begin(),
3572 E = UpdatedDeclContexts.end();
3573 I != E; ++I)
3574 WriteDeclContextVisibleUpdate(*I);
3575
Douglas Gregor959bb062011-12-03 01:15:29 +00003576 if (!WritingModule) {
3577 // Write the submodules that were imported, if any.
3578 RecordData ImportedModules;
3579 for (ASTContext::import_iterator I = Context.local_import_begin(),
3580 IEnd = Context.local_import_end();
3581 I != IEnd; ++I) {
3582 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3583 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3584 }
3585 if (!ImportedModules.empty()) {
3586 // Sort module IDs.
3587 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3588
3589 // Unique module IDs.
3590 ImportedModules.erase(std::unique(ImportedModules.begin(),
3591 ImportedModules.end()),
3592 ImportedModules.end());
3593
3594 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3595 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003596 }
3597
Douglas Gregordab42432011-08-12 00:15:20 +00003598 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003599 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003600 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003601 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003602 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003603
Douglas Gregor08f01292009-04-17 22:13:46 +00003604 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003605 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003606 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003607 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003608 Record.push_back(NumLexicalDeclContexts);
3609 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003610 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003611 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003612}
3613
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003614/// \brief Go through the declaration update blocks and resolve declaration
3615/// pointers into declaration IDs.
3616void ASTWriter::ResolveDeclUpdatesBlocks() {
3617 for (DeclUpdateMap::iterator
3618 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3619 const Decl *D = I->first;
3620 UpdateRecord &URec = I->second;
3621
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003622 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003623 continue; // The decl will be written completely
3624
3625 unsigned Idx = 0, N = URec.size();
3626 while (Idx < N) {
3627 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003628 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3629 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3630 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3631 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3632 ++Idx;
3633 break;
3634
3635 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3636 ++Idx;
3637 break;
3638 }
3639 }
3640 }
3641}
3642
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003643void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003644 if (DeclUpdates.empty())
3645 return;
3646
3647 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003648 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003649 for (DeclUpdateMap::iterator
3650 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3651 const Decl *D = I->first;
3652 UpdateRecord &URec = I->second;
3653
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003654 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003655 continue; // The decl will be written completely,no need to store updates.
3656
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003657 uint64_t Offset = Stream.GetCurrentBitNo();
3658 Stream.EmitRecord(DECL_UPDATES, URec);
3659
3660 OffsetsRecord.push_back(GetDeclRef(D));
3661 OffsetsRecord.push_back(Offset);
3662 }
3663 Stream.ExitBlock();
3664 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3665}
3666
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003667void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003668 if (ReplacedDecls.empty())
3669 return;
3670
3671 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003672 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003673 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003674 Record.push_back(I->ID);
3675 Record.push_back(I->Offset);
3676 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003677 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003678 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003679}
3680
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003681void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003682 Record.push_back(Loc.getRawEncoding());
3683}
3684
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003685void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003686 AddSourceLocation(Range.getBegin(), Record);
3687 AddSourceLocation(Range.getEnd(), Record);
3688}
3689
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003690void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003691 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003692 const uint64_t *Words = Value.getRawData();
3693 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003694}
3695
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003696void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003697 Record.push_back(Value.isUnsigned());
3698 AddAPInt(Value, Record);
3699}
3700
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003701void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003702 AddAPInt(Value.bitcastToAPInt(), Record);
3703}
3704
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003705void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003706 Record.push_back(getIdentifierRef(II));
3707}
3708
Sebastian Redl539c5062010-08-18 23:57:32 +00003709IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003710 if (II == 0)
3711 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003712
Sebastian Redl539c5062010-08-18 23:57:32 +00003713 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003714 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003715 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003716 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003717}
3718
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003719void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003720 Record.push_back(getSelectorRef(SelRef));
3721}
3722
Sebastian Redl539c5062010-08-18 23:57:32 +00003723SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003724 if (Sel.getAsOpaquePtr() == 0) {
3725 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003726 }
3727
Sebastian Redl539c5062010-08-18 23:57:32 +00003728 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003729 if (SID == 0 && Chain) {
3730 // This might trigger a ReadSelector callback, which will set the ID for
3731 // this selector.
3732 Chain->LoadSelector(Sel);
3733 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003734 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003735 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003736 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003737 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003738}
3739
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003740void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003741 AddDeclRef(Temp->getDestructor(), Record);
3742}
3743
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003744void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3745 CXXBaseSpecifier const *BasesEnd,
3746 RecordDataImpl &Record) {
3747 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3748 CXXBaseSpecifiersToWrite.push_back(
3749 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3750 Bases, BasesEnd));
3751 Record.push_back(NextCXXBaseSpecifiersID++);
3752}
3753
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003754void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003755 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003756 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003757 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003758 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003759 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003760 break;
3761 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003762 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003763 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003764 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003765 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003766 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003767 break;
3768 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003769 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003770 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003771 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003772 break;
John McCall0ad16662009-10-29 08:12:44 +00003773 case TemplateArgument::Null:
3774 case TemplateArgument::Integral:
3775 case TemplateArgument::Declaration:
3776 case TemplateArgument::Pack:
3777 break;
3778 }
3779}
3780
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003781void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003782 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003783 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003784
3785 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3786 bool InfoHasSameExpr
3787 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3788 Record.push_back(InfoHasSameExpr);
3789 if (InfoHasSameExpr)
3790 return; // Avoid storing the same expr twice.
3791 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003792 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3793 Record);
3794}
3795
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003796void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3797 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003798 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003799 AddTypeRef(QualType(), Record);
3800 return;
3801 }
3802
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003803 AddTypeLoc(TInfo->getTypeLoc(), Record);
3804}
3805
3806void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3807 AddTypeRef(TL.getType(), Record);
3808
John McCall8f115c62009-10-16 21:56:05 +00003809 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003810 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003811 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003812}
3813
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003814void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003815 Record.push_back(GetOrCreateTypeID(T));
3816}
3817
Douglas Gregoreda8e122011-08-09 15:13:55 +00003818TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3819 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003820 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3821}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003822
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003823TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003824 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003825 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003826}
3827
3828TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3829 if (T.isNull())
3830 return TypeIdx();
3831 assert(!T.getLocalFastQualifiers());
3832
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003833 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003834 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003835 if (DoneWritingDeclsAndTypes) {
3836 assert(0 && "New type seen after serializing all the types to emit!");
3837 return TypeIdx();
3838 }
3839
Douglas Gregor1970d882009-04-26 03:49:13 +00003840 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003841 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003842 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003843 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003844 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003845 return Idx;
3846}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003847
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003848TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003849 if (T.isNull())
3850 return TypeIdx();
3851 assert(!T.getLocalFastQualifiers());
3852
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003853 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3854 assert(I != TypeIdxs.end() && "Type not emitted!");
3855 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003856}
3857
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003858void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003859 Record.push_back(GetDeclRef(D));
3860}
3861
Sebastian Redl539c5062010-08-18 23:57:32 +00003862DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003863 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3864
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003865 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003866 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003867 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00003868
3869 // If D comes from an AST file, its declaration ID is already known and
3870 // fixed.
3871 if (D->isFromASTFile())
3872 return D->getGlobalID();
3873
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003874 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003875 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003876 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003877 if (DoneWritingDeclsAndTypes) {
3878 assert(0 && "New decl seen after serializing all the decls to emit!");
3879 return 0;
3880 }
3881
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003882 // We haven't seen this declaration before. Give it a new ID and
3883 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003884 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003885 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003886 }
3887
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003888 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003889}
3890
Sebastian Redl539c5062010-08-18 23:57:32 +00003891DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003892 if (D == 0)
3893 return 0;
3894
Douglas Gregorb3163e52012-01-05 22:33:30 +00003895 // If D comes from an AST file, its declaration ID is already known and
3896 // fixed.
3897 if (D->isFromASTFile())
3898 return D->getGlobalID();
3899
Douglas Gregore84a9da2009-04-20 20:36:09 +00003900 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3901 return DeclIDs[D];
3902}
3903
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003904static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3905 std::pair<unsigned, serialization::DeclID> R) {
3906 return L.first < R.first;
3907}
3908
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003909void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003910 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003911 assert(D);
3912
3913 SourceLocation Loc = D->getLocation();
3914 if (Loc.isInvalid())
3915 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003916
3917 // We only keep track of the file-level declarations of each file.
3918 if (!D->getLexicalDeclContext()->isFileContext())
3919 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00003920 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3921 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00003922 if (isa<ParmVarDecl>(D))
3923 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003924
3925 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003926 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003927 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003928 FileID FID;
3929 unsigned Offset;
3930 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003931 if (FID.isInvalid())
3932 return;
3933 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3934 assert(Entry->isFile());
3935
3936 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3937 if (!Info)
3938 Info = new DeclIDInFileInfo();
3939
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003940 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003941 LocDeclIDsTy &Decls = Info->DeclIDs;
3942
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003943 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003944 Decls.push_back(LocDecl);
3945 return;
3946 }
3947
3948 LocDeclIDsTy::iterator
3949 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3950
3951 Decls.insert(I, LocDecl);
3952}
3953
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003954void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003955 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003956 Record.push_back(Name.getNameKind());
3957 switch (Name.getNameKind()) {
3958 case DeclarationName::Identifier:
3959 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3960 break;
3961
3962 case DeclarationName::ObjCZeroArgSelector:
3963 case DeclarationName::ObjCOneArgSelector:
3964 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003965 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003966 break;
3967
3968 case DeclarationName::CXXConstructorName:
3969 case DeclarationName::CXXDestructorName:
3970 case DeclarationName::CXXConversionFunctionName:
3971 AddTypeRef(Name.getCXXNameType(), Record);
3972 break;
3973
3974 case DeclarationName::CXXOperatorName:
3975 Record.push_back(Name.getCXXOverloadedOperator());
3976 break;
3977
Alexis Hunt3d221f22009-11-29 07:34:05 +00003978 case DeclarationName::CXXLiteralOperatorName:
3979 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3980 break;
3981
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003982 case DeclarationName::CXXUsingDirective:
3983 // No extra data to emit
3984 break;
3985 }
3986}
Chris Lattnerca025db2010-05-07 21:43:38 +00003987
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003988void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003989 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003990 switch (Name.getNameKind()) {
3991 case DeclarationName::CXXConstructorName:
3992 case DeclarationName::CXXDestructorName:
3993 case DeclarationName::CXXConversionFunctionName:
3994 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3995 break;
3996
3997 case DeclarationName::CXXOperatorName:
3998 AddSourceLocation(
3999 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4000 Record);
4001 AddSourceLocation(
4002 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4003 Record);
4004 break;
4005
4006 case DeclarationName::CXXLiteralOperatorName:
4007 AddSourceLocation(
4008 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4009 Record);
4010 break;
4011
4012 case DeclarationName::Identifier:
4013 case DeclarationName::ObjCZeroArgSelector:
4014 case DeclarationName::ObjCOneArgSelector:
4015 case DeclarationName::ObjCMultiArgSelector:
4016 case DeclarationName::CXXUsingDirective:
4017 break;
4018 }
4019}
4020
4021void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004022 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004023 AddDeclarationName(NameInfo.getName(), Record);
4024 AddSourceLocation(NameInfo.getLoc(), Record);
4025 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4026}
4027
4028void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004029 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004030 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004031 Record.push_back(Info.NumTemplParamLists);
4032 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4033 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4034}
4035
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004036void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004037 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004038 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004039 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004040 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004041
4042 // Push each of the NNS's onto a stack for serialization in reverse order.
4043 while (NNS) {
4044 NestedNames.push_back(NNS);
4045 NNS = NNS->getPrefix();
4046 }
4047
4048 Record.push_back(NestedNames.size());
4049 while(!NestedNames.empty()) {
4050 NNS = NestedNames.pop_back_val();
4051 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4052 Record.push_back(Kind);
4053 switch (Kind) {
4054 case NestedNameSpecifier::Identifier:
4055 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4056 break;
4057
4058 case NestedNameSpecifier::Namespace:
4059 AddDeclRef(NNS->getAsNamespace(), Record);
4060 break;
4061
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004062 case NestedNameSpecifier::NamespaceAlias:
4063 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4064 break;
4065
Chris Lattnerca025db2010-05-07 21:43:38 +00004066 case NestedNameSpecifier::TypeSpec:
4067 case NestedNameSpecifier::TypeSpecWithTemplate:
4068 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4069 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4070 break;
4071
4072 case NestedNameSpecifier::Global:
4073 // Don't need to write an associated value.
4074 break;
4075 }
4076 }
4077}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004078
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004079void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4080 RecordDataImpl &Record) {
4081 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004082 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004083 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004084
4085 // Push each of the nested-name-specifiers's onto a stack for
4086 // serialization in reverse order.
4087 while (NNS) {
4088 NestedNames.push_back(NNS);
4089 NNS = NNS.getPrefix();
4090 }
4091
4092 Record.push_back(NestedNames.size());
4093 while(!NestedNames.empty()) {
4094 NNS = NestedNames.pop_back_val();
4095 NestedNameSpecifier::SpecifierKind Kind
4096 = NNS.getNestedNameSpecifier()->getKind();
4097 Record.push_back(Kind);
4098 switch (Kind) {
4099 case NestedNameSpecifier::Identifier:
4100 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4101 AddSourceRange(NNS.getLocalSourceRange(), Record);
4102 break;
4103
4104 case NestedNameSpecifier::Namespace:
4105 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4106 AddSourceRange(NNS.getLocalSourceRange(), Record);
4107 break;
4108
4109 case NestedNameSpecifier::NamespaceAlias:
4110 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4111 AddSourceRange(NNS.getLocalSourceRange(), Record);
4112 break;
4113
4114 case NestedNameSpecifier::TypeSpec:
4115 case NestedNameSpecifier::TypeSpecWithTemplate:
4116 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4117 AddTypeLoc(NNS.getTypeLoc(), Record);
4118 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4119 break;
4120
4121 case NestedNameSpecifier::Global:
4122 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4123 break;
4124 }
4125 }
4126}
4127
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004128void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004129 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004130 Record.push_back(Kind);
4131 switch (Kind) {
4132 case TemplateName::Template:
4133 AddDeclRef(Name.getAsTemplateDecl(), Record);
4134 break;
4135
4136 case TemplateName::OverloadedTemplate: {
4137 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4138 Record.push_back(OvT->size());
4139 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4140 I != E; ++I)
4141 AddDeclRef(*I, Record);
4142 break;
4143 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004144
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004145 case TemplateName::QualifiedTemplate: {
4146 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4147 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4148 Record.push_back(QualT->hasTemplateKeyword());
4149 AddDeclRef(QualT->getTemplateDecl(), Record);
4150 break;
4151 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004152
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004153 case TemplateName::DependentTemplate: {
4154 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4155 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4156 Record.push_back(DepT->isIdentifier());
4157 if (DepT->isIdentifier())
4158 AddIdentifierRef(DepT->getIdentifier(), Record);
4159 else
4160 Record.push_back(DepT->getOperator());
4161 break;
4162 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004163
4164 case TemplateName::SubstTemplateTemplateParm: {
4165 SubstTemplateTemplateParmStorage *subst
4166 = Name.getAsSubstTemplateTemplateParm();
4167 AddDeclRef(subst->getParameter(), Record);
4168 AddTemplateName(subst->getReplacement(), Record);
4169 break;
4170 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004171
4172 case TemplateName::SubstTemplateTemplateParmPack: {
4173 SubstTemplateTemplateParmPackStorage *SubstPack
4174 = Name.getAsSubstTemplateTemplateParmPack();
4175 AddDeclRef(SubstPack->getParameterPack(), Record);
4176 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4177 break;
4178 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004179 }
4180}
4181
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004182void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004183 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004184 Record.push_back(Arg.getKind());
4185 switch (Arg.getKind()) {
4186 case TemplateArgument::Null:
4187 break;
4188 case TemplateArgument::Type:
4189 AddTypeRef(Arg.getAsType(), Record);
4190 break;
4191 case TemplateArgument::Declaration:
4192 AddDeclRef(Arg.getAsDecl(), Record);
4193 break;
4194 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004195 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004196 AddTypeRef(Arg.getIntegralType(), Record);
4197 break;
4198 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004199 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4200 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004201 case TemplateArgument::TemplateExpansion:
4202 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004203 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4204 Record.push_back(*NumExpansions + 1);
4205 else
4206 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004207 break;
4208 case TemplateArgument::Expression:
4209 AddStmt(Arg.getAsExpr());
4210 break;
4211 case TemplateArgument::Pack:
4212 Record.push_back(Arg.pack_size());
4213 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4214 I != E; ++I)
4215 AddTemplateArgument(*I, Record);
4216 break;
4217 }
4218}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004219
4220void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004221ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004222 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004223 assert(TemplateParams && "No TemplateParams!");
4224 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4225 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4226 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4227 Record.push_back(TemplateParams->size());
4228 for (TemplateParameterList::const_iterator
4229 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4230 P != PEnd; ++P)
4231 AddDeclRef(*P, Record);
4232}
4233
4234/// \brief Emit a template argument list.
4235void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004236ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004237 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004238 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004239 Record.push_back(TemplateArgs->size());
4240 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004241 AddTemplateArgument(TemplateArgs->get(i), Record);
4242}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004243
4244
4245void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004246ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004247 Record.push_back(Set.size());
4248 for (UnresolvedSetImpl::const_iterator
4249 I = Set.begin(), E = Set.end(); I != E; ++I) {
4250 AddDeclRef(I.getDecl(), Record);
4251 Record.push_back(I.getAccess());
4252 }
4253}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004254
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004255void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004256 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004257 Record.push_back(Base.isVirtual());
4258 Record.push_back(Base.isBaseOfClass());
4259 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004260 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004261 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004262 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004263 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4264 : SourceLocation(),
4265 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004266}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004267
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004268void ASTWriter::FlushCXXBaseSpecifiers() {
4269 RecordData Record;
4270 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4271 Record.clear();
4272
4273 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004274 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004275 if (Index == CXXBaseSpecifiersOffsets.size())
4276 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4277 else {
4278 if (Index > CXXBaseSpecifiersOffsets.size())
4279 CXXBaseSpecifiersOffsets.resize(Index + 1);
4280 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4281 }
4282
4283 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4284 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4285 Record.push_back(BEnd - B);
4286 for (; B != BEnd; ++B)
4287 AddCXXBaseSpecifier(*B, Record);
4288 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004289
4290 // Flush any expressions that were written as part of the base specifiers.
4291 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004292 }
4293
4294 CXXBaseSpecifiersToWrite.clear();
4295}
4296
Alexis Hunt1d792652011-01-08 20:30:50 +00004297void ASTWriter::AddCXXCtorInitializers(
4298 const CXXCtorInitializer * const *CtorInitializers,
4299 unsigned NumCtorInitializers,
4300 RecordDataImpl &Record) {
4301 Record.push_back(NumCtorInitializers);
4302 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4303 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004304
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004305 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004306 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004307 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004308 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004309 } else if (Init->isDelegatingInitializer()) {
4310 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004311 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004312 } else if (Init->isMemberInitializer()){
4313 Record.push_back(CTOR_INITIALIZER_MEMBER);
4314 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004315 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004316 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4317 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004318 }
Francois Pichetd583da02010-12-04 09:14:42 +00004319
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004320 AddSourceLocation(Init->getMemberLocation(), Record);
4321 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004322 AddSourceLocation(Init->getLParenLoc(), Record);
4323 AddSourceLocation(Init->getRParenLoc(), Record);
4324 Record.push_back(Init->isWritten());
4325 if (Init->isWritten()) {
4326 Record.push_back(Init->getSourceOrder());
4327 } else {
4328 Record.push_back(Init->getNumArrayIndices());
4329 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4330 AddDeclRef(Init->getArrayIndex(i), Record);
4331 }
4332 }
4333}
4334
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004335void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4336 assert(D->DefinitionData);
4337 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004338 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004339 Record.push_back(Data.UserDeclaredConstructor);
4340 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004341 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004342 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004343 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004344 Record.push_back(Data.UserDeclaredDestructor);
4345 Record.push_back(Data.Aggregate);
4346 Record.push_back(Data.PlainOldData);
4347 Record.push_back(Data.Empty);
4348 Record.push_back(Data.Polymorphic);
4349 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004350 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004351 Record.push_back(Data.HasNoNonEmptyBases);
4352 Record.push_back(Data.HasPrivateFields);
4353 Record.push_back(Data.HasProtectedFields);
4354 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004355 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004356 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004357 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004358 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004359 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004360 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004361 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004362 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004363 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004364 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004365 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004366 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004367 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004368 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004369 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004370 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004371 Record.push_back(Data.DeclaredDefaultConstructor);
4372 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004373 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004374 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004375 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004376 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004377 Record.push_back(Data.FailedImplicitMoveConstructor);
4378 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004379 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004380
4381 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004382 if (Data.NumBases > 0)
4383 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4384 Record);
4385
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004386 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4387 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004388 if (Data.NumVBases > 0)
4389 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4390 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004391
4392 AddUnresolvedSet(Data.Conversions, Record);
4393 AddUnresolvedSet(Data.VisibleConversions, Record);
4394 // Data.Definition is the owning decl, no need to write it.
4395 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004396
4397 // Add lambda-specific data.
4398 if (Data.IsLambda) {
4399 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004400 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004401 Record.push_back(Lambda.NumCaptures);
4402 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004403 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004404 AddDeclRef(Lambda.ContextDecl, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004405 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4406 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4407 AddSourceLocation(Capture.getLocation(), Record);
4408 Record.push_back(Capture.isImplicit());
4409 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4410 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4411 AddDeclRef(Var, Record);
4412 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4413 : SourceLocation(),
4414 Record);
4415 }
4416 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004417}
4418
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004419void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004420 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004421 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004422 assert(FirstDeclID == NextDeclID &&
4423 FirstTypeID == NextTypeID &&
4424 FirstIdentID == NextIdentID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004425 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004426 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004427 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004428
Sebastian Redl07a89a82010-07-30 00:29:29 +00004429 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004430
Douglas Gregordf0c1512011-08-18 04:12:04 +00004431 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4432 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4433 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004434 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004435 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004436 NextDeclID = FirstDeclID;
4437 NextTypeID = FirstTypeID;
4438 NextIdentID = FirstIdentID;
4439 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004440 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004441}
4442
Sebastian Redl539c5062010-08-18 23:57:32 +00004443void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004444 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00004445 if (II->hasMacroDefinition())
4446 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004447}
4448
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004449void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004450 // Always take the highest-numbered type index. This copes with an interesting
4451 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004452 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004453 // keep the higher-numbered entry so that we can properly write it out to
4454 // the AST file.
4455 TypeIdx &StoredIdx = TypeIdxs[T];
4456 if (Idx.getIndex() >= StoredIdx.getIndex())
4457 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004458}
4459
Sebastian Redl539c5062010-08-18 23:57:32 +00004460void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004461 SelectorIDs[S] = ID;
4462}
Douglas Gregor91096292010-10-02 19:29:26 +00004463
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004464void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004465 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004466 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004467 MacroDefinitions[MD] = ID;
4468}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004469
Douglas Gregor0abc2622011-12-20 22:06:13 +00004470void ASTWriter::MacroVisible(IdentifierInfo *II) {
4471 DeserializedMacroNames.push_back(II);
4472}
4473
Douglas Gregore37a85a2011-12-02 17:30:13 +00004474void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4475 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4476 SubmoduleIDs[Mod] = ID;
4477}
4478
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004479void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004480 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004481 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004482 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4483 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004484 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004485 // A forward reference was mutated into a definition. Rewrite it.
4486 // FIXME: This happens during template instantiation, should we
4487 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004488 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004489 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004490 }
4491}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004492void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004493 assert(!WritingAST && "Already writing the AST!");
4494
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004495 // TU and namespaces are handled elsewhere.
4496 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4497 return;
4498
Douglas Gregorb3722e22011-09-09 23:01:35 +00004499 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004500 return; // Not a source decl added to a DeclContext from PCH.
4501
4502 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004503 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004504}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004505
4506void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004507 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004508 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004509 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004510 return; // Not a source member added to a class from PCH.
4511 if (!isa<CXXMethodDecl>(D))
4512 return; // We are interested in lazily declared implicit methods.
4513
4514 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004515 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004516 UpdateRecord &Record = DeclUpdates[RD];
4517 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004518 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004519}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004520
4521void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4522 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004523 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004524 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004525 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004526 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004527 return; // Not a source specialization added to a template from PCH.
4528
4529 UpdateRecord &Record = DeclUpdates[TD];
4530 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004531 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004532}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004533
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004534void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4535 const FunctionDecl *D) {
4536 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004537 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004538 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004539 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004540 return; // Not a source specialization added to a template from PCH.
4541
4542 UpdateRecord &Record = DeclUpdates[TD];
4543 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004544 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004545}
4546
Sebastian Redlab238a72011-04-24 16:28:06 +00004547void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004548 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004549 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004550 return; // Declaration not imported from PCH.
4551
4552 // Implicit decl from a PCH was defined.
4553 // FIXME: Should implicit definition be a separate FunctionDecl?
4554 RewriteDecl(D);
4555}
4556
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004557void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004558 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004559 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004560 return;
4561
4562 // Since the actual instantiation is delayed, this really means that we need
4563 // to update the instantiation location.
4564 UpdateRecord &Record = DeclUpdates[D];
4565 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4566 AddSourceLocation(
4567 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4568}
4569
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004570void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4571 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004572 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004573 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004574 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004575
4576 assert(IFD->getDefinition() && "Category on a class without a definition?");
4577 ObjCClassesWithCategories.insert(
4578 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004579}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004580
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004581
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004582void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4583 const ObjCPropertyDecl *OrigProp,
4584 const ObjCCategoryDecl *ClassExt) {
4585 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4586 if (!D)
4587 return;
4588
4589 assert(!WritingAST && "Already writing the AST!");
4590 if (!D->isFromASTFile())
4591 return; // Declaration not imported from PCH.
4592
4593 RewriteDecl(D);
4594}