blob: 7ee3c9a21b99de7bead662ede8ad265da9f810de [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);
671 RECORD(EXPR_PREDEFINED);
672 RECORD(EXPR_DECL_REF);
673 RECORD(EXPR_INTEGER_LITERAL);
674 RECORD(EXPR_FLOATING_LITERAL);
675 RECORD(EXPR_IMAGINARY_LITERAL);
676 RECORD(EXPR_STRING_LITERAL);
677 RECORD(EXPR_CHARACTER_LITERAL);
678 RECORD(EXPR_PAREN);
679 RECORD(EXPR_UNARY_OPERATOR);
680 RECORD(EXPR_SIZEOF_ALIGN_OF);
681 RECORD(EXPR_ARRAY_SUBSCRIPT);
682 RECORD(EXPR_CALL);
683 RECORD(EXPR_MEMBER);
684 RECORD(EXPR_BINARY_OPERATOR);
685 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
686 RECORD(EXPR_CONDITIONAL_OPERATOR);
687 RECORD(EXPR_IMPLICIT_CAST);
688 RECORD(EXPR_CSTYLE_CAST);
689 RECORD(EXPR_COMPOUND_LITERAL);
690 RECORD(EXPR_EXT_VECTOR_ELEMENT);
691 RECORD(EXPR_INIT_LIST);
692 RECORD(EXPR_DESIGNATED_INIT);
693 RECORD(EXPR_IMPLICIT_VALUE_INIT);
694 RECORD(EXPR_VA_ARG);
695 RECORD(EXPR_ADDR_LABEL);
696 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000697 RECORD(EXPR_CHOOSE);
698 RECORD(EXPR_GNU_NULL);
699 RECORD(EXPR_SHUFFLE_VECTOR);
700 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000701 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000702 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000703 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000704 RECORD(EXPR_OBJC_ARRAY_LITERAL);
705 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000706 RECORD(EXPR_OBJC_ENCODE);
707 RECORD(EXPR_OBJC_SELECTOR_EXPR);
708 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
709 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
710 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
711 RECORD(EXPR_OBJC_KVC_REF_EXPR);
712 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000713 RECORD(STMT_OBJC_FOR_COLLECTION);
714 RECORD(STMT_OBJC_CATCH);
715 RECORD(STMT_OBJC_FINALLY);
716 RECORD(STMT_OBJC_AT_TRY);
717 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
718 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000719 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000720 RECORD(EXPR_CXX_OPERATOR_CALL);
721 RECORD(EXPR_CXX_CONSTRUCT);
722 RECORD(EXPR_CXX_STATIC_CAST);
723 RECORD(EXPR_CXX_DYNAMIC_CAST);
724 RECORD(EXPR_CXX_REINTERPRET_CAST);
725 RECORD(EXPR_CXX_CONST_CAST);
726 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000727 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000728 RECORD(EXPR_CXX_BOOL_LITERAL);
729 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000730 RECORD(EXPR_CXX_TYPEID_EXPR);
731 RECORD(EXPR_CXX_TYPEID_TYPE);
732 RECORD(EXPR_CXX_UUIDOF_EXPR);
733 RECORD(EXPR_CXX_UUIDOF_TYPE);
734 RECORD(EXPR_CXX_THIS);
735 RECORD(EXPR_CXX_THROW);
736 RECORD(EXPR_CXX_DEFAULT_ARG);
737 RECORD(EXPR_CXX_BIND_TEMPORARY);
738 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
739 RECORD(EXPR_CXX_NEW);
740 RECORD(EXPR_CXX_DELETE);
741 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
742 RECORD(EXPR_EXPR_WITH_CLEANUPS);
743 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
744 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
745 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
746 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
747 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
748 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
749 RECORD(EXPR_CXX_NOEXCEPT);
750 RECORD(EXPR_OPAQUE_VALUE);
751 RECORD(EXPR_BINARY_TYPE_TRAIT);
752 RECORD(EXPR_PACK_EXPANSION);
753 RECORD(EXPR_SIZEOF_PACK);
754 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000755 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000756#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000757}
Mike Stump11289f42009-09-09 15:08:12 +0000758
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000759void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000760 RecordData Record;
761 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000762
Sebastian Redl539c5062010-08-18 23:57:32 +0000763#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
764#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000765
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000766 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000767 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000768 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000769 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000770 RECORD(TYPE_OFFSET);
771 RECORD(DECL_OFFSET);
772 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000773 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000774 RECORD(IDENTIFIER_OFFSET);
775 RECORD(IDENTIFIER_TABLE);
776 RECORD(EXTERNAL_DEFINITIONS);
777 RECORD(SPECIAL_TYPES);
778 RECORD(STATISTICS);
779 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000780 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000781 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
782 RECORD(SELECTOR_OFFSETS);
783 RECORD(METHOD_POOL);
784 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000785 RECORD(SOURCE_LOCATION_OFFSETS);
786 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000787 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000788 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000789 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000790 RECORD(PPD_ENTITIES_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000791 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000792 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000793 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000794 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000795 RECORD(SEMA_DECL_REFS);
796 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
797 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
798 RECORD(DECL_REPLACEMENTS);
799 RECORD(UPDATE_VISIBLE);
800 RECORD(DECL_UPDATE_OFFSETS);
801 RECORD(DECL_UPDATES);
802 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
803 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000804 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000805 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000806 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000807 RECORD(FP_PRAGMA_OPTIONS);
808 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000809 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000810 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
811 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000812 RECORD(MODULE_OFFSET_MAP);
813 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000814 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000815 RECORD(FILE_SORTED_DECLS);
816 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000817 RECORD(MERGED_DECLARATIONS);
818 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000819 RECORD(OBJC_CATEGORIES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000820
Chris Lattner28fa4e62009-04-26 22:26:21 +0000821 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000822 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000823 RECORD(SM_SLOC_FILE_ENTRY);
824 RECORD(SM_SLOC_BUFFER_ENTRY);
825 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000826 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner28fa4e62009-04-26 22:26:21 +0000828 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000829 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000830 RECORD(PP_MACRO_OBJECT_LIKE);
831 RECORD(PP_MACRO_FUNCTION_LIKE);
832 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000833
Douglas Gregor12bfa382009-10-17 00:13:19 +0000834 // Decls and Types block.
835 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000836 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000837 RECORD(TYPE_COMPLEX);
838 RECORD(TYPE_POINTER);
839 RECORD(TYPE_BLOCK_POINTER);
840 RECORD(TYPE_LVALUE_REFERENCE);
841 RECORD(TYPE_RVALUE_REFERENCE);
842 RECORD(TYPE_MEMBER_POINTER);
843 RECORD(TYPE_CONSTANT_ARRAY);
844 RECORD(TYPE_INCOMPLETE_ARRAY);
845 RECORD(TYPE_VARIABLE_ARRAY);
846 RECORD(TYPE_VECTOR);
847 RECORD(TYPE_EXT_VECTOR);
848 RECORD(TYPE_FUNCTION_PROTO);
849 RECORD(TYPE_FUNCTION_NO_PROTO);
850 RECORD(TYPE_TYPEDEF);
851 RECORD(TYPE_TYPEOF_EXPR);
852 RECORD(TYPE_TYPEOF);
853 RECORD(TYPE_RECORD);
854 RECORD(TYPE_ENUM);
855 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000856 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000857 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000858 RECORD(TYPE_DECLTYPE);
859 RECORD(TYPE_ELABORATED);
860 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
861 RECORD(TYPE_UNRESOLVED_USING);
862 RECORD(TYPE_INJECTED_CLASS_NAME);
863 RECORD(TYPE_OBJC_OBJECT);
864 RECORD(TYPE_TEMPLATE_TYPE_PARM);
865 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
866 RECORD(TYPE_DEPENDENT_NAME);
867 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
868 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
869 RECORD(TYPE_PAREN);
870 RECORD(TYPE_PACK_EXPANSION);
871 RECORD(TYPE_ATTRIBUTED);
872 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000873 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000874 RECORD(DECL_TYPEDEF);
875 RECORD(DECL_ENUM);
876 RECORD(DECL_RECORD);
877 RECORD(DECL_ENUM_CONSTANT);
878 RECORD(DECL_FUNCTION);
879 RECORD(DECL_OBJC_METHOD);
880 RECORD(DECL_OBJC_INTERFACE);
881 RECORD(DECL_OBJC_PROTOCOL);
882 RECORD(DECL_OBJC_IVAR);
883 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000884 RECORD(DECL_OBJC_CATEGORY);
885 RECORD(DECL_OBJC_CATEGORY_IMPL);
886 RECORD(DECL_OBJC_IMPLEMENTATION);
887 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
888 RECORD(DECL_OBJC_PROPERTY);
889 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000890 RECORD(DECL_FIELD);
891 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000892 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000893 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000894 RECORD(DECL_FILE_SCOPE_ASM);
895 RECORD(DECL_BLOCK);
896 RECORD(DECL_CONTEXT_LEXICAL);
897 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000898 RECORD(DECL_NAMESPACE);
899 RECORD(DECL_NAMESPACE_ALIAS);
900 RECORD(DECL_USING);
901 RECORD(DECL_USING_SHADOW);
902 RECORD(DECL_USING_DIRECTIVE);
903 RECORD(DECL_UNRESOLVED_USING_VALUE);
904 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
905 RECORD(DECL_LINKAGE_SPEC);
906 RECORD(DECL_CXX_RECORD);
907 RECORD(DECL_CXX_METHOD);
908 RECORD(DECL_CXX_CONSTRUCTOR);
909 RECORD(DECL_CXX_DESTRUCTOR);
910 RECORD(DECL_CXX_CONVERSION);
911 RECORD(DECL_ACCESS_SPEC);
912 RECORD(DECL_FRIEND);
913 RECORD(DECL_FRIEND_TEMPLATE);
914 RECORD(DECL_CLASS_TEMPLATE);
915 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
916 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
917 RECORD(DECL_FUNCTION_TEMPLATE);
918 RECORD(DECL_TEMPLATE_TYPE_PARM);
919 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
920 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
921 RECORD(DECL_STATIC_ASSERT);
922 RECORD(DECL_CXX_BASE_SPECIFIERS);
923 RECORD(DECL_INDIRECTFIELD);
924 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
925
Douglas Gregor03412ba2011-06-03 02:27:19 +0000926 // Statements and Exprs can occur in the Decls and Types block.
927 AddStmtsExprs(Stream, Record);
928
Douglas Gregor92a96f52011-02-08 21:58:10 +0000929 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000930 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000931 RECORD(PPD_MACRO_DEFINITION);
932 RECORD(PPD_INCLUSION_DIRECTIVE);
933
Chris Lattner28fa4e62009-04-26 22:26:21 +0000934#undef RECORD
935#undef BLOCK
936 Stream.ExitBlock();
937}
938
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000939/// \brief Adjusts the given filename to only write out the portion of the
940/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000941///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000942/// \param Filename the file name to adjust.
943///
944/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
945/// the returned filename will be adjusted by this system root.
946///
947/// \returns either the original filename (if it needs no adjustment) or the
948/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000949static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000950adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000951 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000952
Douglas Gregorc567ba22011-07-22 16:35:34 +0000953 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000954 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000955
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 // Verify that the filename and the system root have the same prefix.
957 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000958 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000959 if (Filename[Pos] != isysroot[Pos])
960 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000961
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000962 // We hit the end of the filename before we hit the end of the system root.
963 if (!Filename[Pos])
964 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000965
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000966 // If the file name has a '/' at the current position, skip over the '/'.
967 // We distinguish sysroot-based includes from absolute includes by the
968 // absence of '/' at the beginning of sysroot-based includes.
969 if (Filename[Pos] == '/')
970 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000971
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000972 return Filename + Pos;
973}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000974
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000975/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000976void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000977 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000978 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000979
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000980 // Metadata
Douglas Gregore8bbc122011-09-02 00:18:52 +0000981 const TargetInfo &Target = Context.getTargetInfo();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000982 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000983 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000984 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
985 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000986 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
987 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
988 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +0000989 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Has errors
Douglas Gregor29cc6422011-08-17 21:07:30 +0000990 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000991 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000992
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000993 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000994 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000995 Record.push_back(VERSION_MAJOR);
996 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000997 Record.push_back(CLANG_VERSION_MAJOR);
998 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +0000999 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001000 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor29cc6422011-08-17 21:07:30 +00001001 const std::string &Triple = Target.getTriple().getTriple();
1002 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
1003
1004 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001005 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1006 llvm::SmallVector<char, 128> ModulePaths;
1007 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001008
1009 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1010 M != MEnd; ++M) {
1011 // Skip modules that weren't directly imported.
1012 if (!(*M)->isDirectlyImported())
1013 continue;
1014
1015 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1016 // FIXME: Write import location, once it matters.
1017 // FIXME: This writes the absolute path for AST files we depend on.
1018 const std::string &FileName = (*M)->FileName;
1019 Record.push_back(FileName.size());
1020 Record.append(FileName.begin(), FileName.end());
1021 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001022 Stream.EmitRecord(IMPORTS, Record);
1023 }
Mike Stump11289f42009-09-09 15:08:12 +00001024
Douglas Gregora3b20262011-05-06 21:43:30 +00001025 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001026 SourceManager &SM = Context.getSourceManager();
1027 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1028 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001029 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001030 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1031 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1032
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001033 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001034
Michael J. Spencer740857f2010-12-21 16:45:57 +00001035 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001036
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001037 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001038 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001039 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001040 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001041 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001042 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001043
1044 Record.clear();
1045 Record.push_back(SM.getMainFileID().getOpaqueValue());
1046 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001047 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001048
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001049 // Original PCH directory
1050 if (!OutputFile.empty() && OutputFile != "-") {
1051 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1052 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1054 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1055
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001056 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001057
1058 llvm::sys::fs::make_absolute(OutputPath);
1059 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1060
1061 RecordData Record;
1062 Record.push_back(ORIGINAL_PCH_DIR);
1063 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1064 }
1065
Ted Kremenek18e066f2010-01-22 22:12:47 +00001066 // Repository branch/version information.
1067 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001068 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001069 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1070 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001071 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001072 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001073 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1074 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001075}
1076
1077/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001078void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001079 RecordData Record;
Douglas Gregorc2ae8802011-09-13 18:26:39 +00001080#define LANGOPT(Name, Bits, Default, Description) \
1081 Record.push_back(LangOpts.Name);
1082#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1083 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1084#include "clang/Basic/LangOptions.def"
John McCall5fb5df92012-06-20 06:18:46 +00001085
1086 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1087 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
Douglas Gregor7d106e42011-11-15 19:35:01 +00001088
1089 Record.push_back(LangOpts.CurrentModule.size());
1090 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
Sebastian Redl539c5062010-08-18 23:57:32 +00001091 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001092}
1093
Douglas Gregora7f71a92009-04-10 03:52:48 +00001094//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001095// stat cache Serialization
1096//===----------------------------------------------------------------------===//
1097
1098namespace {
1099// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001100class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001101public:
1102 typedef const char * key_type;
1103 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner2a6fa472010-11-23 19:28:12 +00001105 typedef struct stat data_type;
1106 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001107
1108 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001109 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
1112 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001113 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001114 data_type_ref Data) {
1115 unsigned StrLen = strlen(path);
1116 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001117 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001118 clang::io::Emit8(Out, DataLen);
1119 return std::make_pair(StrLen + 1, DataLen);
1120 }
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001122 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001123 Out.write(path, KeyLen);
1124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001126 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001127 data_type_ref Data, unsigned DataLen) {
1128 using namespace clang::io;
1129 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001130
Chris Lattner2a6fa472010-11-23 19:28:12 +00001131 Emit32(Out, (uint32_t) Data.st_ino);
1132 Emit32(Out, (uint32_t) Data.st_dev);
1133 Emit16(Out, (uint16_t) Data.st_mode);
1134 Emit64(Out, (uint64_t) Data.st_mtime);
1135 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001136
1137 assert(Out.tell() - Start == DataLen && "Wrong data length");
1138 }
1139};
1140} // end anonymous namespace
1141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001142/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001143void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001144 // Build the on-disk hash table containing information about every
1145 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001146 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001147 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001148 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001149 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001150 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001151 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001152 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregorc5046832009-04-27 18:38:38 +00001155 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001156 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001157 uint32_t BucketOffset;
1158 {
1159 llvm::raw_svector_ostream Out(StatCacheData);
1160 // Make sure that no bucket is at offset 0
1161 clang::io::Emit32(Out, 0);
1162 BucketOffset = Generator.Emit(Out);
1163 }
1164
1165 // Create a blob abbreviation
1166 using namespace llvm;
1167 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001169 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1172 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1173
1174 // Write the stat cache
1175 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001176 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001177 Record.push_back(BucketOffset);
1178 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001179 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001180}
1181
1182//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001183// Source Manager Serialization
1184//===----------------------------------------------------------------------===//
1185
1186/// \brief Create an abbreviation for the SLocEntry that refers to a
1187/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001188static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001189 using namespace llvm;
1190 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001191 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001196 // FileEntry fields.
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001204 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001205}
1206
1207/// \brief Create an abbreviation for the SLocEntry that refers to a
1208/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001209static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001210 using namespace llvm;
1211 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001212 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001218 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001219}
1220
1221/// \brief Create an abbreviation for the SLocEntry that refers to a
1222/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001223static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001224 using namespace llvm;
1225 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001226 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001228 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001229}
1230
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001231/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1232/// expansion.
1233static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001234 using namespace llvm;
1235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001236 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001242 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001243}
1244
Douglas Gregor09b69892011-02-10 17:09:37 +00001245namespace {
1246 // Trait used for the on-disk hash table of header search information.
1247 class HeaderFileInfoTrait {
1248 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001249
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001250 // Keep track of the framework names we've used during serialization.
1251 SmallVector<char, 128> FrameworkStringData;
1252 llvm::StringMap<unsigned> FrameworkNameOffset;
1253
Douglas Gregor09b69892011-02-10 17:09:37 +00001254 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001255 HeaderFileInfoTrait(ASTWriter &Writer)
1256 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001257
1258 typedef const char *key_type;
1259 typedef key_type key_type_ref;
1260
1261 typedef HeaderFileInfo data_type;
1262 typedef const data_type &data_type_ref;
1263
1264 static unsigned ComputeHash(const char *path) {
1265 // The hash is based only on the filename portion of the key, so that the
1266 // reader can match based on filenames when symlinking or excess path
1267 // elements ("foo/../", "../") change the form of the name. However,
1268 // complete path is still the key.
1269 return llvm::HashString(llvm::sys::path::filename(path));
1270 }
1271
1272 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001273 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001274 data_type_ref Data) {
1275 unsigned StrLen = strlen(path);
1276 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001277 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001278 clang::io::Emit8(Out, DataLen);
1279 return std::make_pair(StrLen + 1, DataLen);
1280 }
1281
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001282 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001283 Out.write(path, KeyLen);
1284 }
1285
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001286 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001287 data_type_ref Data, unsigned DataLen) {
1288 using namespace clang::io;
1289 uint64_t Start = Out.tell(); (void)Start;
1290
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001291 unsigned char Flags = (Data.isImport << 5)
1292 | (Data.isPragmaOnce << 4)
1293 | (Data.DirInfo << 2)
1294 | (Data.Resolved << 1)
1295 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001296 Emit8(Out, (uint8_t)Flags);
1297 Emit16(Out, (uint16_t) Data.NumIncludes);
1298
1299 if (!Data.ControllingMacro)
1300 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1301 else
1302 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001303
1304 unsigned Offset = 0;
1305 if (!Data.Framework.empty()) {
1306 // If this header refers into a framework, save the framework name.
1307 llvm::StringMap<unsigned>::iterator Pos
1308 = FrameworkNameOffset.find(Data.Framework);
1309 if (Pos == FrameworkNameOffset.end()) {
1310 Offset = FrameworkStringData.size() + 1;
1311 FrameworkStringData.append(Data.Framework.begin(),
1312 Data.Framework.end());
1313 FrameworkStringData.push_back(0);
1314
1315 FrameworkNameOffset[Data.Framework] = Offset;
1316 } else
1317 Offset = Pos->second;
1318 }
1319 Emit32(Out, Offset);
1320
Douglas Gregor09b69892011-02-10 17:09:37 +00001321 assert(Out.tell() - Start == DataLen && "Wrong data length");
1322 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001323
1324 const char *strings_begin() const { return FrameworkStringData.begin(); }
1325 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001326 };
1327} // end anonymous namespace
1328
1329/// \brief Write the header search block for the list of files that
1330///
1331/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001332void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001333 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001334 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1335
1336 if (FilesByUID.size() > HS.header_file_size())
1337 FilesByUID.resize(HS.header_file_size());
1338
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001339 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001340 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001341 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001342 unsigned NumHeaderSearchEntries = 0;
1343 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1344 const FileEntry *File = FilesByUID[UID];
1345 if (!File)
1346 continue;
1347
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001348 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1349 // from the external source if it was not provided already.
1350 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001351 if (HFI.External && Chain)
1352 continue;
1353
1354 // Turn the file name into an absolute path, if it isn't already.
1355 const char *Filename = File->getName();
1356 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1357
1358 // If we performed any translation on the file name at all, we need to
1359 // save this string, since the generator will refer to it later.
1360 if (Filename != File->getName()) {
1361 Filename = strdup(Filename);
1362 SavedStrings.push_back(Filename);
1363 }
1364
1365 Generator.insert(Filename, HFI, GeneratorTrait);
1366 ++NumHeaderSearchEntries;
1367 }
1368
1369 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001370 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001371 uint32_t BucketOffset;
1372 {
1373 llvm::raw_svector_ostream Out(TableData);
1374 // Make sure that no bucket is at offset 0
1375 clang::io::Emit32(Out, 0);
1376 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1377 }
1378
1379 // Create a blob abbreviation
1380 using namespace llvm;
1381 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1382 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1383 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1387 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1388
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001389 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001390 RecordData Record;
1391 Record.push_back(HEADER_SEARCH_TABLE);
1392 Record.push_back(BucketOffset);
1393 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001394 Record.push_back(TableData.size());
1395 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001396 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1397
1398 // Free all of the strings we had to duplicate.
1399 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1400 free((void*)SavedStrings[I]);
1401}
1402
Douglas Gregora7f71a92009-04-10 03:52:48 +00001403/// \brief Writes the block containing the serialized form of the
1404/// source manager.
1405///
1406/// TODO: We should probably use an on-disk hash table (stored in a
1407/// blob), indexed based on the file name, so that we only create
1408/// entries for files that we actually need. In the common case (no
1409/// errors), we probably won't have to create file entries for any of
1410/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001411void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001412 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001413 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001414 RecordData Record;
1415
Chris Lattner0910e3b2009-04-10 17:16:57 +00001416 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001417 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001418
1419 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001420 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1421 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1422 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001423 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001424
Douglas Gregor258ae542009-04-27 06:38:32 +00001425 // Write out the source location entry table. We skip the first
1426 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001427 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001428 // Write out the offsets of only source location file entries.
1429 // We will go through them in ASTReader::validateFileEntries().
1430 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001432 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1433 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001434 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001435 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001436 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001437
Douglas Gregor258ae542009-04-27 06:38:32 +00001438 // Record the offset of this source-location entry.
1439 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1440
1441 // Figure out which record code to use.
1442 unsigned Code;
1443 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001444 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1445 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001446 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001447 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1448 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001450 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001451 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001452 Record.clear();
1453 Record.push_back(Code);
1454
Douglas Gregor925296b2011-07-19 16:10:42 +00001455 // Starting offset of this entry within this module, so skip the dummy.
1456 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001457 if (SLoc->isFile()) {
1458 const SrcMgr::FileInfo &File = SLoc->getFile();
1459 Record.push_back(File.getIncludeLoc().getRawEncoding());
1460 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1461 Record.push_back(File.hasLineDirectives());
1462
1463 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001464 if (Content->OrigEntry) {
1465 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001466 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001467
Douglas Gregor258ae542009-04-27 06:38:32 +00001468 // The source location entry is a file. The blob associated
1469 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001470
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001471 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001472 Record.push_back(Content->OrigEntry->getSize());
1473 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001474 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001475 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001476
1477 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(SLoc);
1478 if (FDI != FileDeclIDs.end()) {
1479 Record.push_back(FDI->second->FirstDeclIndex);
1480 Record.push_back(FDI->second->DeclIDs.size());
1481 } else {
1482 Record.push_back(0);
1483 Record.push_back(0);
1484 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001485
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001486 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001487 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001488 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001489
1490 // Ask the file manager to fixup the relative path for us. This will
1491 // honor the working directory.
1492 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1493
1494 // FIXME: This call to make_absolute shouldn't be necessary, the
1495 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001496 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001497 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001498
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001499 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001500 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001501
1502 if (Content->BufferOverridden) {
1503 Record.clear();
1504 Record.push_back(SM_SLOC_BUFFER_BLOB);
1505 const llvm::MemoryBuffer *Buffer
1506 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1507 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1508 StringRef(Buffer->getBufferStart(),
1509 Buffer->getBufferSize() + 1));
1510 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001511 } else {
1512 // The source location entry is a buffer. The blob associated
1513 // with this entry contains the contents of the buffer.
1514
1515 // We add one to the size so that we capture the trailing NULL
1516 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1517 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001518 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001519 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001520 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001521 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001522 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001523 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001524 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001525 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001526 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001527 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001528
Douglas Gregor925296b2011-07-19 16:10:42 +00001529 if (strcmp(Name, "<built-in>") == 0) {
1530 PreloadSLocs.push_back(SLocEntryOffsets.size());
1531 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001532 }
1533 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001534 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001535 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001536 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1537 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001538 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1539 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001540
1541 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001542 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001543 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001544 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001545 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001546 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001547 }
1548 }
1549
Douglas Gregor8f45df52009-04-16 22:23:12 +00001550 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001551
1552 if (SLocEntryOffsets.empty())
1553 return;
1554
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001555 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001556 // table is used for lazily loading source-location information.
1557 using namespace llvm;
1558 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001559 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001561 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1563 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001564
Douglas Gregor258ae542009-04-27 06:38:32 +00001565 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001566 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001567 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001568 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001569 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001570
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001571 Abbrev = new BitCodeAbbrev();
1572 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1574 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1575 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1576
1577 Record.clear();
1578 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1579 Record.push_back(SLocFileEntryOffsets.size());
1580 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1581 data(SLocFileEntryOffsets));
1582
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001583 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001584 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001585 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001586
1587 // Write the line table. It depends on remapping working, so it must come
1588 // after the source location offsets.
1589 if (SourceMgr.hasLineTable()) {
1590 LineTableInfo &LineTable = SourceMgr.getLineTable();
1591
1592 Record.clear();
1593 // Emit the file names
1594 Record.push_back(LineTable.getNumFilenames());
1595 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1596 // Emit the file name
1597 const char *Filename = LineTable.getFilename(I);
1598 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1599 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1600 Record.push_back(FilenameLen);
1601 if (FilenameLen)
1602 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1603 }
1604
1605 // Emit the line entries
1606 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1607 L != LEnd; ++L) {
1608 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001609 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001610 continue;
1611
1612 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001613 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001614
1615 // Emit the line entries
1616 Record.push_back(L->second.size());
1617 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1618 LEEnd = L->second.end();
1619 LE != LEEnd; ++LE) {
1620 Record.push_back(LE->FileOffset);
1621 Record.push_back(LE->LineNo);
1622 Record.push_back(LE->FilenameID);
1623 Record.push_back((unsigned)LE->FileKind);
1624 Record.push_back(LE->IncludeOffset);
1625 }
1626 }
1627 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1628 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001629}
1630
Douglas Gregorc5046832009-04-27 18:38:38 +00001631//===----------------------------------------------------------------------===//
1632// Preprocessor Serialization
1633//===----------------------------------------------------------------------===//
1634
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001635static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1636 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1637 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1638 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1639 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1640 return X.first->getName().compare(Y.first->getName());
1641}
1642
Chris Lattnereeffaef2009-04-10 17:15:23 +00001643/// \brief Writes the block containing the serialized form of the
1644/// preprocessor.
1645///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001646void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001647 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1648 if (PPRec)
1649 WritePreprocessorDetail(*PPRec);
1650
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001651 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001652
Chris Lattner0af3ba12009-04-13 01:29:17 +00001653 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1654 if (PP.getCounterValue() != 0) {
1655 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001656 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001657 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001658 }
1659
1660 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001661 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001662
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001663 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001664 // FIXME: use diagnostics subsystem for localization etc.
1665 if (PP.SawDateOrTime())
1666 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001667
Douglas Gregor796d76a2010-10-20 22:00:55 +00001668
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001669 // Loop over all the macro definitions that are live at the end of the file,
1670 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001671
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001672 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001673 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001674 MacrosToEmit;
1675 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001676 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1677 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001678 I != E; ++I) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001679 const IdentifierInfo *Name = I->first;
Douglas Gregorebf00492011-10-17 15:32:29 +00001680 if (!IsModule || I->second->isPublic()) {
Douglas Gregor0abc2622011-12-20 22:06:13 +00001681 MacroDefinitionsSeen.insert(Name);
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001682 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1683 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001684 }
1685
1686 // Sort the set of macro definitions that need to be serialized by the
1687 // name of the macro, to provide a stable ordering.
1688 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1689 &compareMacroDefinitions);
1690
Douglas Gregor68051a72011-02-11 00:26:14 +00001691 // Resolve any identifiers that defined macros at the time they were
1692 // deserialized, adding them to the list of macros to emit (if appropriate).
1693 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1694 IdentifierInfo *Name
1695 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1696 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1697 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1698 }
1699
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001700 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1701 const IdentifierInfo *Name = MacrosToEmit[I].first;
1702 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001703 if (!MI)
1704 continue;
1705
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001706 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001707 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001708 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001709
1710 // FIXME: There is a (probably minor) optimization we could do here, if
1711 // the macro comes from the original PCH but the identifier comes from a
1712 // chained PCH, by storing the offset into the original PCH rather than
1713 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001714 if (MI->isBuiltinMacro() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00001715 (Chain &&
1716 Name->isFromAST() && !Name->hasChangedSinceDeserialization() &&
1717 MI->isFromAST() && !MI->hasChangedAfterLoad()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001718 continue;
1719
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001720 AddIdentifierRef(Name, Record);
1721 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001722 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1723 Record.push_back(MI->isUsed());
Douglas Gregorebf00492011-10-17 15:32:29 +00001724 Record.push_back(MI->isPublic());
1725 AddSourceLocation(MI->getVisibilityLocation(), Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001726 unsigned Code;
1727 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001728 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001729 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001730 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001731
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001732 Record.push_back(MI->isC99Varargs());
1733 Record.push_back(MI->isGNUVarargs());
1734 Record.push_back(MI->getNumArgs());
1735 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1736 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001737 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001738 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001739
Douglas Gregoraae92242010-03-19 21:51:54 +00001740 // If we have a detailed preprocessing record, record the macro definition
1741 // ID that corresponds to this macro.
1742 if (PPRec)
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001743 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001744
Douglas Gregor8f45df52009-04-16 22:23:12 +00001745 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001746 Record.clear();
1747
Chris Lattner2199f5b2009-04-10 18:08:30 +00001748 // Emit the tokens array.
1749 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1750 // Note that we know that the preprocessor does not have any annotation
1751 // tokens in it because they are created by the parser, and thus can't be
1752 // in a macro definition.
1753 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001754
Chris Lattner2199f5b2009-04-10 18:08:30 +00001755 Record.push_back(Tok.getLocation().getRawEncoding());
1756 Record.push_back(Tok.getLength());
1757
Chris Lattner2199f5b2009-04-10 18:08:30 +00001758 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1759 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001760 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001761 // FIXME: Should translate token kind to a stable encoding.
1762 Record.push_back(Tok.getKind());
1763 // FIXME: Should translate token flags to a stable encoding.
1764 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001765
Sebastian Redl539c5062010-08-18 23:57:32 +00001766 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001767 Record.clear();
1768 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001769 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001770 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001771 Stream.ExitBlock();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001772}
1773
1774void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001775 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001776 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001777
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001778 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001779
Douglas Gregor92a96f52011-02-08 21:58:10 +00001780 // Enter the preprocessor block.
1781 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001782
Douglas Gregoraae92242010-03-19 21:51:54 +00001783 // If the preprocessor has a preprocessing record, emit it.
1784 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001785 using namespace llvm;
1786
1787 // Set up the abbreviation for
1788 unsigned InclusionAbbrev = 0;
1789 {
1790 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1791 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1796 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1797 }
1798
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001799 unsigned FirstPreprocessorEntityID
1800 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1801 + NUM_PREDEF_PP_ENTITY_IDS;
1802 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001803 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001804 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1805 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001806 E != EEnd;
1807 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001808 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001809
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001810 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1811 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001812
Douglas Gregor92a96f52011-02-08 21:58:10 +00001813 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001814 // Record this macro definition's ID.
1815 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001816
Douglas Gregor92a96f52011-02-08 21:58:10 +00001817 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001818 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1819 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001820 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001821
Chandler Carrutha88a22182011-07-14 08:20:46 +00001822 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001823 Record.push_back(ME->isBuiltinMacro());
1824 if (ME->isBuiltinMacro())
1825 AddIdentifierRef(ME->getName(), Record);
1826 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001827 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001828 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001829 continue;
1830 }
1831
1832 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1833 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001834 Record.push_back(ID->getFileName().size());
1835 Record.push_back(ID->wasInQuotes());
1836 Record.push_back(static_cast<unsigned>(ID->getKind()));
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001837 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001838 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001839 // Check that the FileEntry is not null because it was not resolved and
1840 // we create a PCH even with compiler errors.
1841 if (ID->getFile())
1842 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001843 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1844 continue;
1845 }
1846
1847 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1848 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001849 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001850
Douglas Gregoraae92242010-03-19 21:51:54 +00001851 // Write the offsets table for the preprocessing record.
1852 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001853 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1854
Douglas Gregoraae92242010-03-19 21:51:54 +00001855 // Write the offsets table for identifier IDs.
1856 using namespace llvm;
1857 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001858 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001859 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001860 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001861 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001862
Douglas Gregoraae92242010-03-19 21:51:54 +00001863 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001864 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001865 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001866 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1867 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001868 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001869}
1870
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001871unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1872 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1873 if (Known != SubmoduleIDs.end())
1874 return Known->second;
1875
1876 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1877}
1878
Douglas Gregor253eefe2011-12-01 00:59:36 +00001879/// \brief Compute the number of modules within the given tree (including the
1880/// given module).
1881static unsigned getNumberOfModules(Module *Mod) {
1882 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001883 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1884 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001885 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001886 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001887
1888 return ChildModules + 1;
1889}
1890
Douglas Gregorde3ef502011-11-30 23:21:26 +00001891void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001892 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001893 // FIXME: This feels like it belongs somewhere else, but there are no
1894 // other consumers of this information.
1895 SourceManager &SrcMgr = PP->getSourceManager();
1896 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1897 for (ASTContext::import_iterator I = Context->local_import_begin(),
1898 IEnd = Context->local_import_end();
1899 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001900 if (Module *ImportedFrom
1901 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1902 SrcMgr))) {
1903 ImportedFrom->Imports.push_back(I->getImportedModule());
1904 }
1905 }
1906
Douglas Gregor69021972011-11-30 17:33:56 +00001907 // Enter the submodule description block.
1908 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1909
1910 // Write the abbreviations needed for the submodules block.
1911 using namespace llvm;
1912 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1913 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001914 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00001915 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
1916 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
1917 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00001918 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
1919 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00001920 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00001921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1923 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
1924
1925 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001926 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00001927 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1928 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
1929
1930 Abbrev = new BitCodeAbbrev();
1931 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1933 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00001934
1935 Abbrev = new BitCodeAbbrev();
1936 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
1937 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1938 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
1939
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001940 Abbrev = new BitCodeAbbrev();
1941 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
1942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
1943 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
1944
Douglas Gregor253eefe2011-12-01 00:59:36 +00001945 // Write the submodule metadata block.
1946 RecordData Record;
1947 Record.push_back(getNumberOfModules(WritingModule));
1948 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
1949 Stream.EmitRecord(SUBMODULE_METADATA, Record);
1950
Douglas Gregor69021972011-11-30 17:33:56 +00001951 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00001952 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00001953 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00001954 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001955 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00001956 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001957 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00001958
1959 // Emit the definition of the block.
1960 Record.clear();
1961 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001962 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00001963 if (Mod->Parent) {
1964 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
1965 Record.push_back(SubmoduleIDs[Mod->Parent]);
1966 } else {
1967 Record.push_back(0);
1968 }
1969 Record.push_back(Mod->IsFramework);
1970 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00001971 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00001972 Record.push_back(Mod->InferSubmodules);
1973 Record.push_back(Mod->InferExplicitSubmodules);
1974 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00001975 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
1976
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00001977 // Emit the requirements.
1978 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
1979 Record.clear();
1980 Record.push_back(SUBMODULE_REQUIRES);
1981 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
1982 Mod->Requires[I].data(),
1983 Mod->Requires[I].size());
1984 }
1985
Douglas Gregor69021972011-11-30 17:33:56 +00001986 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00001987 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00001988 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00001989 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00001990 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00001991 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00001992 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
1993 Record.clear();
1994 Record.push_back(SUBMODULE_UMBRELLA_DIR);
1995 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
1996 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00001997 }
1998
1999 // Emit the headers.
2000 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2001 Record.clear();
2002 Record.push_back(SUBMODULE_HEADER);
2003 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2004 Mod->Headers[I]->getName());
2005 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002006
2007 // Emit the imports.
2008 if (!Mod->Imports.empty()) {
2009 Record.clear();
2010 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002011 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002012 assert(ImportedID && "Unknown submodule!");
2013 Record.push_back(ImportedID);
2014 }
2015 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2016 }
2017
Douglas Gregor24bb9232011-12-02 18:58:38 +00002018 // Emit the exports.
2019 if (!Mod->Exports.empty()) {
2020 Record.clear();
2021 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002022 if (Module *Exported = Mod->Exports[I].getPointer()) {
2023 unsigned ExportedID = SubmoduleIDs[Exported];
2024 assert(ExportedID > 0 && "Unknown submodule ID?");
2025 Record.push_back(ExportedID);
2026 } else {
2027 Record.push_back(0);
2028 }
2029
Douglas Gregor24bb9232011-12-02 18:58:38 +00002030 Record.push_back(Mod->Exports[I].getInt());
2031 }
2032 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2033 }
2034
Douglas Gregor69021972011-11-30 17:33:56 +00002035 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002036 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2037 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002038 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002039 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002040 }
2041
2042 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002043
2044 assert((NextSubmoduleID - FirstSubmoduleID
2045 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002046}
2047
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002048serialization::SubmoduleID
2049ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002050 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002051 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002052
2053 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002054 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002055 Module *OwningMod
2056 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002057 if (!OwningMod)
2058 return 0;
2059
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002060 // Check whether this submodule is part of our own module.
2061 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002062 return 0;
2063
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002064 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002065}
2066
David Blaikie9c902b52011-09-25 23:23:43 +00002067void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002068 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002069 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002070 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2071 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002072 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002073 if (point.Loc.isInvalid())
2074 continue;
2075
2076 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002077 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002078 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002079 if (I->second.isPragma()) {
2080 Record.push_back(I->first);
2081 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002082 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002083 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002084 Record.push_back(-1); // mark the end of the diag/map pairs for this
2085 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002086 }
2087
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002088 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002089 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002090}
2091
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002092void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2093 if (CXXBaseSpecifiersOffsets.empty())
2094 return;
2095
2096 RecordData Record;
2097
2098 // Create a blob abbreviation for the C++ base specifiers offsets.
2099 using namespace llvm;
2100
2101 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2102 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2105 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2106
Douglas Gregorc27b2872011-08-04 00:01:48 +00002107 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002108 Record.clear();
2109 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2110 Record.push_back(CXXBaseSpecifiersOffsets.size());
2111 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002112 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002113}
2114
Douglas Gregorc5046832009-04-27 18:38:38 +00002115//===----------------------------------------------------------------------===//
2116// Type Serialization
2117//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002118
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002119/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002120void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002121 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002122 if (Idx.getIndex() == 0) // we haven't seen this type before.
2123 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002124
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002125 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002126
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002127 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002128 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002129 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002130 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002131 else if (TypeOffsets.size() < Index) {
2132 TypeOffsets.resize(Index + 1);
2133 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002134 }
2135
2136 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002137
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002138 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002139 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002140
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002141 if (T.hasLocalNonFastQualifiers()) {
2142 Qualifiers Qs = T.getLocalQualifiers();
2143 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002144 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002145 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002146 } else {
2147 switch (T->getTypeClass()) {
2148 // For all of the concrete, non-dependent types, call the
2149 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002150#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002151 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002152#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002153#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002154 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002155 }
2156
2157 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002158 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002159
2160 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002161 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002162}
2163
Douglas Gregorc5046832009-04-27 18:38:38 +00002164//===----------------------------------------------------------------------===//
2165// Declaration Serialization
2166//===----------------------------------------------------------------------===//
2167
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002168/// \brief Write the block containing all of the declaration IDs
2169/// lexically declared within the given DeclContext.
2170///
2171/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2172/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002173uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002174 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002175 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002176 return 0;
2177
Douglas Gregor8f45df52009-04-16 22:23:12 +00002178 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002179 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002180 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002181 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002182 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2183 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002184 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002185
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002186 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002187 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002188 return Offset;
2189}
2190
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002191void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002192 using namespace llvm;
2193 RecordData Record;
2194
2195 // Write the type offsets array
2196 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2201 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2202 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002203 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002204 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002205 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002206 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002207
2208 // Write the declaration offsets array
2209 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2214 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2215 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002216 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002217 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002218 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002219 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002220}
2221
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002222void ASTWriter::WriteFileDeclIDsMap() {
2223 using namespace llvm;
2224 RecordData Record;
2225
2226 // Join the vectors of DeclIDs from all files.
2227 SmallVector<DeclID, 256> FileSortedIDs;
2228 for (FileDeclIDsTy::iterator
2229 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2230 DeclIDInFileInfo &Info = *FI->second;
2231 Info.FirstDeclIndex = FileSortedIDs.size();
2232 for (LocDeclIDsTy::iterator
2233 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2234 FileSortedIDs.push_back(DI->second);
2235 }
2236
2237 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2238 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2240 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2241 Record.push_back(FILE_SORTED_DECLS);
2242 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2243}
2244
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002245void ASTWriter::WriteComments() {
2246 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002247 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002248 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002249 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2250 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002251 I != E; ++I) {
2252 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002253 AddSourceRange((*I)->getSourceRange(), Record);
2254 Record.push_back((*I)->getKind());
2255 Record.push_back((*I)->isTrailingComment());
2256 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002257 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2258 }
2259 Stream.ExitBlock();
2260}
2261
Douglas Gregorc5046832009-04-27 18:38:38 +00002262//===----------------------------------------------------------------------===//
2263// Global Method Pool and Selector Serialization
2264//===----------------------------------------------------------------------===//
2265
Douglas Gregore84a9da2009-04-20 20:36:09 +00002266namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002267// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002268class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002269 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002270
2271public:
2272 typedef Selector key_type;
2273 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002274
Sebastian Redl834bb972010-08-04 17:20:04 +00002275 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002276 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002277 ObjCMethodList Instance, Factory;
2278 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002279 typedef const data_type& data_type_ref;
2280
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002281 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002282
Douglas Gregorc78d3462009-04-24 21:10:55 +00002283 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002284 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002285 }
Mike Stump11289f42009-09-09 15:08:12 +00002286
2287 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002288 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002289 data_type_ref Methods) {
2290 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2291 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002292 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2293 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002294 Method = Method->Next)
2295 if (Method->Method)
2296 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002297 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002298 Method = Method->Next)
2299 if (Method->Method)
2300 DataLen += 4;
2301 clang::io::Emit16(Out, DataLen);
2302 return std::make_pair(KeyLen, DataLen);
2303 }
Mike Stump11289f42009-09-09 15:08:12 +00002304
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002305 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002306 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002307 assert((Start >> 32) == 0 && "Selector key offset too large");
2308 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002309 unsigned N = Sel.getNumArgs();
2310 clang::io::Emit16(Out, N);
2311 if (N == 0)
2312 N = 1;
2313 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002314 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002315 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2316 }
Mike Stump11289f42009-09-09 15:08:12 +00002317
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002318 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002319 data_type_ref Methods, unsigned DataLen) {
2320 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002321 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002322 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002323 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002324 Method = Method->Next)
2325 if (Method->Method)
2326 ++NumInstanceMethods;
2327
2328 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002329 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002330 Method = Method->Next)
2331 if (Method->Method)
2332 ++NumFactoryMethods;
2333
2334 clang::io::Emit16(Out, NumInstanceMethods);
2335 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002336 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002337 Method = Method->Next)
2338 if (Method->Method)
2339 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002340 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002341 Method = Method->Next)
2342 if (Method->Method)
2343 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002344
2345 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002346 }
2347};
2348} // end anonymous namespace
2349
Sebastian Redla19a67f2010-08-03 21:58:15 +00002350/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002351///
2352/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002353/// in an on-disk hash table indexed by the selector. The hash table also
2354/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002355void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002356 using namespace llvm;
2357
Sebastian Redla19a67f2010-08-03 21:58:15 +00002358 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002359 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002360 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002361 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002362 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002363 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002364 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002365 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002366
Sebastian Redla19a67f2010-08-03 21:58:15 +00002367 // Create the on-disk hash table representation. We walk through every
2368 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002369 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002370 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002371 I = SelectorIDs.begin(), E = SelectorIDs.end();
2372 I != E; ++I) {
2373 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002374 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002375 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002376 I->second,
2377 ObjCMethodList(),
2378 ObjCMethodList()
2379 };
2380 if (F != SemaRef.MethodPool.end()) {
2381 Data.Instance = F->second.first;
2382 Data.Factory = F->second.second;
2383 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002384 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002385 // changed.
2386 if (Chain && I->second < FirstSelectorID) {
2387 // Selector already exists. Did it change?
2388 bool changed = false;
2389 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2390 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002391 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002392 changed = true;
2393 }
2394 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2395 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002396 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002397 changed = true;
2398 }
2399 if (!changed)
2400 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002401 } else if (Data.Instance.Method || Data.Factory.Method) {
2402 // A new method pool entry.
2403 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002404 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002405 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002406 }
2407
Douglas Gregorc78d3462009-04-24 21:10:55 +00002408 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002409 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002410 uint32_t BucketOffset;
2411 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002412 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002413 llvm::raw_svector_ostream Out(MethodPool);
2414 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002415 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002416 BucketOffset = Generator.Emit(Out, Trait);
2417 }
2418
2419 // Create a blob abbreviation
2420 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002422 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2425 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2426
Douglas Gregor95c13f52009-04-25 17:48:32 +00002427 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002428 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002429 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002430 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002431 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002432 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002433
2434 // Create a blob abbreviation for the selector table offsets.
2435 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002436 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2440 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2441
2442 // Write the selector offsets table.
2443 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002444 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002445 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002446 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002447 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002448 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002449 }
2450}
2451
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002452/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002453void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002454 using namespace llvm;
2455 if (SemaRef.ReferencedSelectors.empty())
2456 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002457
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002458 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002459
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002460 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002461 // very tricky to fix, and given that @selector shouldn't really appear in
2462 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002463 for (DenseMap<Selector, SourceLocation>::iterator S =
2464 SemaRef.ReferencedSelectors.begin(),
2465 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2466 Selector Sel = (*S).first;
2467 SourceLocation Loc = (*S).second;
2468 AddSelectorRef(Sel, Record);
2469 AddSourceLocation(Loc, Record);
2470 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002471 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002472}
2473
Douglas Gregorc5046832009-04-27 18:38:38 +00002474//===----------------------------------------------------------------------===//
2475// Identifier Table Serialization
2476//===----------------------------------------------------------------------===//
2477
Douglas Gregorc78d3462009-04-24 21:10:55 +00002478namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002479class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002480 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002481 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002482 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002483 bool IsModule;
2484
Douglas Gregor1d583f22009-04-28 21:18:29 +00002485 /// \brief Determines whether this is an "interesting" identifier
2486 /// that needs a full IdentifierInfo structure written into the hash
2487 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002488 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002489 if (II->isPoisoned() ||
2490 II->isExtensionToken() ||
2491 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002492 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002493 II->getFETokenInfo<void>())
2494 return true;
2495
Douglas Gregord7910e92011-09-14 22:14:14 +00002496 return hasMacroDefinition(II, Macro);
2497 }
2498
2499 bool hasMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002500 if (!II->hasMacroDefinition())
2501 return false;
2502
Douglas Gregord7910e92011-09-14 22:14:14 +00002503 if (Macro || (Macro = PP.getMacroInfo(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002504 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002505
Douglas Gregord7910e92011-09-14 22:14:14 +00002506 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002507 }
2508
Douglas Gregore84a9da2009-04-20 20:36:09 +00002509public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002510 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002511 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002512
Sebastian Redl539c5062010-08-18 23:57:32 +00002513 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002514 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002515
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002516 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2517 IdentifierResolver &IdResolver, bool IsModule)
2518 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002519
2520 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002521 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002522 }
Mike Stump11289f42009-09-09 15:08:12 +00002523
2524 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002525 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002526 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002527 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002528 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002529 if (isInterestingIdentifier(II, Macro)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002530 DataLen += 2; // 2 bytes for builtin ID, flags
Douglas Gregord7910e92011-09-14 22:14:14 +00002531 if (hasMacroDefinition(II, Macro))
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002532 DataLen += 8;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002533
2534 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2535 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002536 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002537 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002538 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002539 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002540 // We emit the key length after the data length so that every
2541 // string is preceded by a 16-bit length. This matches the PTH
2542 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002543 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002544 return std::make_pair(KeyLen, DataLen);
2545 }
Mike Stump11289f42009-09-09 15:08:12 +00002546
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002547 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002548 unsigned KeyLen) {
2549 // Record the location of the key data. This is used when generating
2550 // the mapping from persistent IDs to strings.
2551 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002552 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002553 }
Mike Stump11289f42009-09-09 15:08:12 +00002554
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002555 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002556 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002557 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002558 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002559 clang::io::Emit32(Out, ID << 1);
2560 return;
2561 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002562
Douglas Gregor1d583f22009-04-28 21:18:29 +00002563 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002564 uint32_t Bits = 0;
Douglas Gregord7910e92011-09-14 22:14:14 +00002565 bool HasMacroDefinition = hasMacroDefinition(II, Macro);
Douglas Gregorb9256522009-04-28 21:32:13 +00002566 Bits = (uint32_t)II->getObjCOrBuiltinID();
Craig Topperdec792e2011-12-19 05:04:33 +00002567 assert((Bits & 0x7ff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
Douglas Gregord7910e92011-09-14 22:14:14 +00002568 Bits = (Bits << 1) | unsigned(HasMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002569 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2570 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002571 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002572 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002573 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002574
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002575 if (HasMacroDefinition) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002576 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002577 clang::io::Emit32(Out,
2578 Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
2579 }
2580
Douglas Gregora868bbd2009-04-21 22:25:48 +00002581 // Emit the declaration IDs in reverse order, because the
2582 // IdentifierResolver provides the declarations as they would be
2583 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002584 // "stat"), but the ASTReader adds declarations to the end of the list
2585 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002586 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002587 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2588 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002589 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002590 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002591 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002592 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002593 }
2594};
2595} // end anonymous namespace
2596
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002597/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002598///
2599/// The identifier table consists of a blob containing string data
2600/// (the actual identifiers themselves) and a separate "offsets" index
2601/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002602void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2603 IdentifierResolver &IdResolver,
2604 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002605 using namespace llvm;
2606
2607 // Create and write out the blob that contains the identifier
2608 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002609 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002610 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002611 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002612
Douglas Gregore6648fb2009-04-28 20:33:11 +00002613 // Look for any identifiers that were named while processing the
2614 // headers, but are otherwise not needed. We add these to the hash
2615 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002616 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002617 // file.
2618 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2619 IDEnd = PP.getIdentifierTable().end();
2620 ID != IDEnd; ++ID)
2621 getIdentifierRef(ID->second);
2622
Sebastian Redlff4a2952010-07-23 23:49:55 +00002623 // Create the on-disk hash table representation. We only store offsets
2624 // for identifiers that appear here for the first time.
2625 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002626 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002627 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2628 ID != IDEnd; ++ID) {
2629 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002630 if (!Chain || !ID->first->isFromAST() ||
2631 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002632 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2633 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002634 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002635
Douglas Gregore84a9da2009-04-20 20:36:09 +00002636 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002637 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002638 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002639 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002640 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002641 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002642 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002643 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002644 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002645 }
2646
2647 // Create a blob abbreviation
2648 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002649 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002650 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002651 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002652 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002653
2654 // Write the identifier table
2655 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002656 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002657 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002658 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002659 }
2660
2661 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002662 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002663 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002664 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2667 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2668
2669 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002670 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002671 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002672 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002673 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002674 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002675}
2676
Douglas Gregorc5046832009-04-27 18:38:38 +00002677//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002678// DeclContext's Name Lookup Table Serialization
2679//===----------------------------------------------------------------------===//
2680
2681namespace {
2682// Trait used for the on-disk hash table used in the method pool.
2683class ASTDeclContextNameLookupTrait {
2684 ASTWriter &Writer;
2685
2686public:
2687 typedef DeclarationName key_type;
2688 typedef key_type key_type_ref;
2689
2690 typedef DeclContext::lookup_result data_type;
2691 typedef const data_type& data_type_ref;
2692
2693 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2694
2695 unsigned ComputeHash(DeclarationName Name) {
2696 llvm::FoldingSetNodeID ID;
2697 ID.AddInteger(Name.getNameKind());
2698
2699 switch (Name.getNameKind()) {
2700 case DeclarationName::Identifier:
2701 ID.AddString(Name.getAsIdentifierInfo()->getName());
2702 break;
2703 case DeclarationName::ObjCZeroArgSelector:
2704 case DeclarationName::ObjCOneArgSelector:
2705 case DeclarationName::ObjCMultiArgSelector:
2706 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2707 break;
2708 case DeclarationName::CXXConstructorName:
2709 case DeclarationName::CXXDestructorName:
2710 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002711 break;
2712 case DeclarationName::CXXOperatorName:
2713 ID.AddInteger(Name.getCXXOverloadedOperator());
2714 break;
2715 case DeclarationName::CXXLiteralOperatorName:
2716 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2717 case DeclarationName::CXXUsingDirective:
2718 break;
2719 }
2720
2721 return ID.ComputeHash();
2722 }
2723
2724 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002725 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002726 data_type_ref Lookup) {
2727 unsigned KeyLen = 1;
2728 switch (Name.getNameKind()) {
2729 case DeclarationName::Identifier:
2730 case DeclarationName::ObjCZeroArgSelector:
2731 case DeclarationName::ObjCOneArgSelector:
2732 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002733 case DeclarationName::CXXLiteralOperatorName:
2734 KeyLen += 4;
2735 break;
2736 case DeclarationName::CXXOperatorName:
2737 KeyLen += 1;
2738 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002739 case DeclarationName::CXXConstructorName:
2740 case DeclarationName::CXXDestructorName:
2741 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002742 case DeclarationName::CXXUsingDirective:
2743 break;
2744 }
2745 clang::io::Emit16(Out, KeyLen);
2746
2747 // 2 bytes for num of decls and 4 for each DeclID.
2748 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2749 clang::io::Emit16(Out, DataLen);
2750
2751 return std::make_pair(KeyLen, DataLen);
2752 }
2753
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002754 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002755 using namespace clang::io;
2756
2757 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2758 Emit8(Out, Name.getNameKind());
2759 switch (Name.getNameKind()) {
2760 case DeclarationName::Identifier:
2761 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2762 break;
2763 case DeclarationName::ObjCZeroArgSelector:
2764 case DeclarationName::ObjCOneArgSelector:
2765 case DeclarationName::ObjCMultiArgSelector:
2766 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2767 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002768 case DeclarationName::CXXOperatorName:
2769 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2770 Emit8(Out, Name.getCXXOverloadedOperator());
2771 break;
2772 case DeclarationName::CXXLiteralOperatorName:
2773 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2774 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002775 case DeclarationName::CXXConstructorName:
2776 case DeclarationName::CXXDestructorName:
2777 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002778 case DeclarationName::CXXUsingDirective:
2779 break;
2780 }
2781 }
2782
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002783 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002784 data_type Lookup, unsigned DataLen) {
2785 uint64_t Start = Out.tell(); (void)Start;
2786 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2787 for (; Lookup.first != Lookup.second; ++Lookup.first)
2788 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2789
2790 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2791 }
2792};
2793} // end anonymous namespace
2794
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002795/// \brief Write the block containing all of the declaration IDs
2796/// visible from the given DeclContext.
2797///
2798/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002799/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002800uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2801 DeclContext *DC) {
2802 if (DC->getPrimaryContext() != DC)
2803 return 0;
2804
2805 // Since there is no name lookup into functions or methods, don't bother to
2806 // build a visible-declarations table for these entities.
2807 if (DC->isFunctionOrMethod())
2808 return 0;
2809
2810 // If not in C++, we perform name lookup for the translation unit via the
2811 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2812 // FIXME: In C++ we need the visible declarations in order to "see" the
2813 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00002814 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002815 return 0;
2816
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002817 // Serialize the contents of the mapping used for lookup. Note that,
2818 // although we have two very different code paths, the serialized
2819 // representation is the same for both cases: a declaration name,
2820 // followed by a size, followed by references to the visible
2821 // declarations that have that name.
2822 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00002823 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002824 if (!Map || Map->empty())
2825 return 0;
2826
2827 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2828 ASTDeclContextNameLookupTrait Trait(*this);
2829
2830 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002831 DeclarationName ConversionName;
2832 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002833 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2834 D != DEnd; ++D) {
2835 DeclarationName Name = D->first;
2836 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002837 if (Result.first != Result.second) {
2838 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2839 // Hash all conversion function names to the same name. The actual
2840 // type information in conversion function name is not used in the
2841 // key (since such type information is not stable across different
2842 // modules), so the intended effect is to coalesce all of the conversion
2843 // functions under a single key.
2844 if (!ConversionName)
2845 ConversionName = Name;
2846 ConversionDecls.append(Result.first, Result.second);
2847 continue;
2848 }
2849
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002850 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002851 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002852 }
2853
Douglas Gregor05ef9312011-08-30 20:49:19 +00002854 // Add the conversion functions
2855 if (!ConversionDecls.empty()) {
2856 Generator.insert(ConversionName,
2857 DeclContext::lookup_result(ConversionDecls.begin(),
2858 ConversionDecls.end()),
2859 Trait);
2860 }
2861
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002862 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002863 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002864 uint32_t BucketOffset;
2865 {
2866 llvm::raw_svector_ostream Out(LookupTable);
2867 // Make sure that no bucket is at offset 0
2868 clang::io::Emit32(Out, 0);
2869 BucketOffset = Generator.Emit(Out, Trait);
2870 }
2871
2872 // Write the lookup table
2873 RecordData Record;
2874 Record.push_back(DECL_CONTEXT_VISIBLE);
2875 Record.push_back(BucketOffset);
2876 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2877 LookupTable.str());
2878
2879 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2880 ++NumVisibleDeclContexts;
2881 return Offset;
2882}
2883
Sebastian Redla4071b42010-08-24 00:50:09 +00002884/// \brief Write an UPDATE_VISIBLE block for the given context.
2885///
2886/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2887/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00002888/// (in C++), for namespaces, and for classes with forward-declared unscoped
2889/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00002890void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002891 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2892 if (!Map || Map->empty())
2893 return;
2894
2895 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2896 ASTDeclContextNameLookupTrait Trait(*this);
2897
2898 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002899 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2900 D != DEnd; ++D) {
2901 DeclarationName Name = D->first;
2902 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002903 // For any name that appears in this table, the results are complete, i.e.
2904 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002905 if (Result.first != Result.second)
2906 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002907 }
2908
2909 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002910 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00002911 uint32_t BucketOffset;
2912 {
2913 llvm::raw_svector_ostream Out(LookupTable);
2914 // Make sure that no bucket is at offset 0
2915 clang::io::Emit32(Out, 0);
2916 BucketOffset = Generator.Emit(Out, Trait);
2917 }
2918
2919 // Write the lookup table
2920 RecordData Record;
2921 Record.push_back(UPDATE_VISIBLE);
2922 Record.push_back(getDeclID(cast<Decl>(DC)));
2923 Record.push_back(BucketOffset);
2924 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2925}
2926
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002927/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2928void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2929 RecordData Record;
2930 Record.push_back(Opts.fp_contract);
2931 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2932}
2933
2934/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2935void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002936 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002937 return;
2938
2939 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2940 RecordData Record;
2941#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2942#include "clang/Basic/OpenCLExtensions.def"
2943 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2944}
2945
Douglas Gregor358cd442012-01-15 16:58:34 +00002946void ASTWriter::WriteRedeclarations() {
2947 RecordData LocalRedeclChains;
2948 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
2949
2950 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
2951 Decl *First = Redeclarations[I];
2952 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
2953
2954 Decl *MostRecent = First->getMostRecentDecl();
2955
2956 // If we only have a single declaration, there is no point in storing
2957 // a redeclaration chain.
2958 if (First == MostRecent)
2959 continue;
2960
2961 unsigned Offset = LocalRedeclChains.size();
2962 unsigned Size = 0;
2963 LocalRedeclChains.push_back(0); // Placeholder for the size.
2964
2965 // Collect the set of local redeclarations of this declaration.
2966 for (Decl *Prev = MostRecent; Prev != First;
2967 Prev = Prev->getPreviousDecl()) {
2968 if (!Prev->isFromASTFile()) {
2969 AddDeclRef(Prev, LocalRedeclChains);
2970 ++Size;
2971 }
2972 }
2973 LocalRedeclChains[Offset] = Size;
2974
2975 // Reverse the set of local redeclarations, so that we store them in
2976 // order (since we found them in reverse order).
2977 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
2978
2979 // Add the mapping from the first ID to the set of local declarations.
2980 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
2981 LocalRedeclsMap.push_back(Info);
2982
2983 assert(N == Redeclarations.size() &&
2984 "Deserialized a declaration we shouldn't have");
2985 }
2986
2987 if (LocalRedeclChains.empty())
2988 return;
2989
2990 // Sort the local redeclarations map by the first declaration ID,
2991 // since the reader will be performing binary searches on this information.
2992 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
2993
2994 // Emit the local redeclarations map.
2995 using namespace llvm;
2996 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2997 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
2998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
2999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3000 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3001
3002 RecordData Record;
3003 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3004 Record.push_back(LocalRedeclsMap.size());
3005 Stream.EmitRecordWithBlob(AbbrevID, Record,
3006 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3007 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3008
3009 // Emit the redeclaration chains.
3010 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3011}
3012
Douglas Gregor404cdde2012-01-27 01:47:08 +00003013void ASTWriter::WriteObjCCategories() {
3014 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3015 RecordData Categories;
3016
3017 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3018 unsigned Size = 0;
3019 unsigned StartIndex = Categories.size();
3020
3021 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3022
3023 // Allocate space for the size.
3024 Categories.push_back(0);
3025
3026 // Add the categories.
3027 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3028 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3029 assert(getDeclID(Cat) != 0 && "Bogus category");
3030 AddDeclRef(Cat, Categories);
3031 }
3032
3033 // Update the size.
3034 Categories[StartIndex] = Size;
3035
3036 // Record this interface -> category map.
3037 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3038 CategoriesMap.push_back(CatInfo);
3039 }
3040
3041 // Sort the categories map by the definition ID, since the reader will be
3042 // performing binary searches on this information.
3043 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3044
3045 // Emit the categories map.
3046 using namespace llvm;
3047 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3048 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3051 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3052
3053 RecordData Record;
3054 Record.push_back(OBJC_CATEGORIES_MAP);
3055 Record.push_back(CategoriesMap.size());
3056 Stream.EmitRecordWithBlob(AbbrevID, Record,
3057 reinterpret_cast<char*>(CategoriesMap.data()),
3058 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3059
3060 // Emit the category lists.
3061 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3062}
3063
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003064void ASTWriter::WriteMergedDecls() {
3065 if (!Chain || Chain->MergedDecls.empty())
3066 return;
3067
3068 RecordData Record;
3069 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3070 IEnd = Chain->MergedDecls.end();
3071 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003072 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003073 : getDeclID(I->first);
3074 assert(CanonID && "Merged declaration not known?");
3075
3076 Record.push_back(CanonID);
3077 Record.push_back(I->second.size());
3078 Record.append(I->second.begin(), I->second.end());
3079 }
3080 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3081}
3082
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003083//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003084// General Serialization Routines
3085//===----------------------------------------------------------------------===//
3086
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003087/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003088void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3089 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003090 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003091 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3092 e = Attrs.end(); i != e; ++i){
3093 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003094 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003095 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003096
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003097#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003098
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003099 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003100}
3101
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003102void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003103 Record.push_back(Str.size());
3104 Record.insert(Record.end(), Str.begin(), Str.end());
3105}
3106
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003107void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3108 RecordDataImpl &Record) {
3109 Record.push_back(Version.getMajor());
3110 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3111 Record.push_back(*Minor + 1);
3112 else
3113 Record.push_back(0);
3114 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3115 Record.push_back(*Subminor + 1);
3116 else
3117 Record.push_back(0);
3118}
3119
Douglas Gregore84a9da2009-04-20 20:36:09 +00003120/// \brief Note that the identifier II occurs at the given offset
3121/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003122void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003123 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003124 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003125 // up earlier in the chain and thus don't need an offset.
3126 if (ID >= FirstIdentID)
3127 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003128}
3129
Douglas Gregor95c13f52009-04-25 17:48:32 +00003130/// \brief Note that the selector Sel occurs at the given offset
3131/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003132void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003133 unsigned ID = SelectorIDs[Sel];
3134 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003135 // Don't record offsets for selectors that are also available in a different
3136 // file.
3137 if (ID < FirstSelectorID)
3138 return;
3139 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003140}
3141
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003142ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003143 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003144 WritingAST(false), DoneWritingDeclsAndTypes(false),
3145 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003146 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00003148 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003149 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3150 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003151 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003152 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003153 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003154 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003155 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003156 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003157 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3158 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3159 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003160 DeclTypedefAbbrev(0),
3161 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3162 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003163{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003164}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003165
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003166ASTWriter::~ASTWriter() {
3167 for (FileDeclIDsTy::iterator
3168 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3169 delete I->second;
3170}
3171
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003172void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003173 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003174 Module *WritingModule, StringRef isysroot,
3175 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003176 WritingAST = true;
3177
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003178 ASTHasCompilerErrors = hasErrors;
3179
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003180 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003181 Stream.Emit((unsigned)'C', 8);
3182 Stream.Emit((unsigned)'P', 8);
3183 Stream.Emit((unsigned)'C', 8);
3184 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003185
Chris Lattner28fa4e62009-04-26 22:26:21 +00003186 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003187
Douglas Gregoreda8e122011-08-09 15:13:55 +00003188 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003189 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003190 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003191 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003192 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003193 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003194 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003195
3196 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003197}
3198
Douglas Gregora94a1542011-07-27 21:45:57 +00003199template<typename Vector>
3200static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3201 ASTWriter::RecordData &Record) {
3202 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3203 I != E; ++I) {
3204 Writer.AddDeclRef(*I, Record);
3205 }
3206}
3207
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003208void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003209 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003210 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003211 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003212 using namespace llvm;
3213
Douglas Gregorcf68c582011-12-01 22:20:10 +00003214 // Make sure that the AST reader knows to finalize itself.
3215 if (Chain)
3216 Chain->finalizeForWriting();
3217
Sebastian Redl143413f2010-07-12 22:02:52 +00003218 ASTContext &Context = SemaRef.Context;
3219 Preprocessor &PP = SemaRef.PP;
3220
Douglas Gregordab42432011-08-12 00:15:20 +00003221 // Set up predefined declaration IDs.
3222 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003223 if (Context.ObjCIdDecl)
3224 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003225 if (Context.ObjCSelDecl)
3226 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003227 if (Context.ObjCClassDecl)
3228 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003229 if (Context.ObjCProtocolClassDecl)
3230 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003231 if (Context.Int128Decl)
3232 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3233 if (Context.UInt128Decl)
3234 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003235 if (Context.ObjCInstanceTypeDecl)
3236 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003237 if (Context.BuiltinVaListDecl)
3238 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3239
Douglas Gregor851443c2011-08-12 01:39:19 +00003240 if (!Chain) {
3241 // Make sure that we emit IdentifierInfos (and any attached
3242 // declarations) for builtins. We don't need to do this when we're
3243 // emitting chained PCH files, because all of the builtins will be
3244 // in the original PCH file.
3245 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003246 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003247 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003248 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003249 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003250 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3251 getIdentifierRef(&Table.get(BuiltinNames[I]));
3252 }
3253
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003254 // If there are any out-of-date identifiers, bring them up to date.
3255 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3256 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3257 IDEnd = PP.getIdentifierTable().end();
3258 ID != IDEnd; ++ID)
3259 if (ID->second->isOutOfDate())
3260 ExtSource->updateOutOfDateIdentifier(*ID->second);
3261 }
3262
Chris Lattner0c797362009-09-08 18:19:27 +00003263 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003264 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003265 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003266 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003267 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003268
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003269 // Build a record containing all of the file scoped decls in this file.
3270 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003271 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3272 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003273
Douglas Gregor851443c2011-08-12 01:39:19 +00003274 // Build a record containing all of the delegating constructors we still need
3275 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003276 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003277 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003278
Douglas Gregor851443c2011-08-12 01:39:19 +00003279 // Write the set of weak, undeclared identifiers. We always write the
3280 // entire table, since later PCH files in a PCH chain are only interested in
3281 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003282 RecordData WeakUndeclaredIdentifiers;
3283 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003284 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003285 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3286 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3287 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3288 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3289 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3290 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3291 }
3292 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003293
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003294 // Build a record containing all of the locally-scoped external
3295 // declarations in this header file. Generally, this record will be
3296 // empty.
3297 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003298 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003299 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003300 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003301 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3302 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003303 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003304 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003305 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3306 }
3307
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003308 // Build a record containing all of the ext_vector declarations.
3309 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003310 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003311
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003312 // Build a record containing all of the VTable uses information.
3313 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003314 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003315 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3316 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3317 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3318 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3319 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003320 }
3321
3322 // Build a record containing all of dynamic classes declarations.
3323 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003324 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003325
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003326 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003327 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003328 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003329 I = SemaRef.PendingInstantiations.begin(),
3330 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3331 AddDeclRef(I->first, PendingInstantiations);
3332 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003333 }
3334 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3335 "There are local ones at end of translation unit!");
3336
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003337 // Build a record containing some declaration references.
3338 RecordData SemaDeclRefs;
3339 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3340 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3341 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3342 }
3343
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003344 RecordData CUDASpecialDeclRefs;
3345 if (Context.getcudaConfigureCallDecl()) {
3346 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3347 }
3348
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003349 // Build a record containing all of the known namespaces.
3350 RecordData KnownNamespaces;
3351 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3352 I = SemaRef.KnownNamespaces.begin(),
3353 IEnd = SemaRef.KnownNamespaces.end();
3354 I != IEnd; ++I) {
3355 if (!I->second)
3356 AddDeclRef(I->first, KnownNamespaces);
3357 }
3358
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003359 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003360 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003361 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003362 WriteMetadata(Context, isysroot, OutputFile);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003363 WriteLanguageOptions(Context.getLangOpts());
Douglas Gregorc567ba22011-07-22 16:35:34 +00003364 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003365 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003366
3367 // Create a lexical update block containing all of the declarations in the
3368 // translation unit that do not come from other AST files.
3369 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3370 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3371 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3372 E = TU->noload_decls_end();
3373 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003374 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003375 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003376 }
3377
3378 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3379 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3380 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3381 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3382 Record.clear();
3383 Record.push_back(TU_UPDATE_LEXICAL);
3384 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3385 data(NewGlobalDecls));
3386
3387 // And a visible updates block for the translation unit.
3388 Abv = new llvm::BitCodeAbbrev();
3389 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3390 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3391 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3392 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3393 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3394 WriteDeclContextVisibleUpdate(TU);
3395
3396 // If the translation unit has an anonymous namespace, and we don't already
3397 // have an update block for it, write it as an update block.
3398 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3399 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3400 if (Record.empty()) {
3401 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003402 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003403 }
3404 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003405
3406 // Make sure visible decls, added to DeclContexts previously loaded from
3407 // an AST file, are registered for serialization.
3408 for (SmallVector<const Decl *, 16>::iterator
3409 I = UpdatingVisibleDecls.begin(),
3410 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3411 GetDeclRef(*I);
3412 }
3413
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003414 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003415 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003416
Douglas Gregor5204bde2011-08-02 16:26:37 +00003417 // Form the record of special types.
3418 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003419 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003420 AddTypeRef(Context.getFILEType(), SpecialTypes);
3421 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3422 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3423 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3424 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003425 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003426 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003427
Douglas Gregor1970d882009-04-26 03:49:13 +00003428 // Keep writing types and declarations until all types and
3429 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003430 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003431 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003432 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3433 E = DeclsToRewrite.end();
3434 I != E; ++I)
3435 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003436 while (!DeclTypesToEmit.empty()) {
3437 DeclOrType DOT = DeclTypesToEmit.front();
3438 DeclTypesToEmit.pop();
3439 if (DOT.isType())
3440 WriteType(DOT.getType());
3441 else
3442 WriteDecl(Context, DOT.getDecl());
3443 }
3444 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003445
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003446 DoneWritingDeclsAndTypes = true;
3447
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003448 WriteFileDeclIDsMap();
3449 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003450 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003451
3452 if (Chain) {
3453 // Write the mapping information describing our module dependencies and how
3454 // each of those modules were mapped into our own offset/ID space, so that
3455 // the reader can build the appropriate mapping to its own offset/ID space.
3456 // The map consists solely of a blob with the following format:
3457 // *(module-name-len:i16 module-name:len*i8
3458 // source-location-offset:i32
3459 // identifier-id:i32
3460 // preprocessed-entity-id:i32
3461 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003462 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003463 // selector-id:i32
3464 // declaration-id:i32
3465 // c++-base-specifiers-id:i32
3466 // type-id:i32)
3467 //
3468 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3469 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3470 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3471 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003472 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003473 {
3474 llvm::raw_svector_ostream Out(Buffer);
3475 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003476 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003477 M != MEnd; ++M) {
3478 StringRef FileName = (*M)->FileName;
3479 io::Emit16(Out, FileName.size());
3480 Out.write(FileName.data(), FileName.size());
3481 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3482 io::Emit32(Out, (*M)->BaseIdentifierID);
3483 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003484 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003485 io::Emit32(Out, (*M)->BaseSelectorID);
3486 io::Emit32(Out, (*M)->BaseDeclID);
3487 io::Emit32(Out, (*M)->BaseTypeIndex);
3488 }
3489 }
3490 Record.clear();
3491 Record.push_back(MODULE_OFFSET_MAP);
3492 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3493 Buffer.data(), Buffer.size());
3494 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003495 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003496 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003497 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003498 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003499 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003500 WriteFPPragmaOptions(SemaRef.getFPOptions());
3501 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003502
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003503 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003504 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003505
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003506 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003507
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003508 // If we're emitting a module, write out the submodule information.
3509 if (WritingModule)
3510 WriteSubmodules(WritingModule);
3511
Douglas Gregor5204bde2011-08-02 16:26:37 +00003512 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3513
Douglas Gregord4df8652009-04-22 22:02:47 +00003514 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003515 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003516 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003517
3518 // Write the record containing tentative definitions.
3519 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003520 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003521
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003522 // Write the record containing unused file scoped decls.
3523 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003524 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003525
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003526 // Write the record containing weak undeclared identifiers.
3527 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003528 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003529 WeakUndeclaredIdentifiers);
3530
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003531 // Write the record containing locally-scoped external definitions.
3532 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003533 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003534 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003535
3536 // Write the record containing ext_vector type names.
3537 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003538 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003539
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003540 // Write the record containing VTable uses information.
3541 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003542 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003543
3544 // Write the record containing dynamic classes declarations.
3545 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003546 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003547
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003548 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003549 if (!PendingInstantiations.empty())
3550 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003551
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003552 // Write the record containing declaration references of Sema.
3553 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003554 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003555
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003556 // Write the record containing CUDA-specific declaration references.
3557 if (!CUDASpecialDeclRefs.empty())
3558 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003559
3560 // Write the delegating constructors.
3561 if (!DelegatingCtorDecls.empty())
3562 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003563
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003564 // Write the known namespaces.
3565 if (!KnownNamespaces.empty())
3566 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3567
Douglas Gregor851443c2011-08-12 01:39:19 +00003568 // Write the visible updates to DeclContexts.
3569 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3570 I = UpdatedDeclContexts.begin(),
3571 E = UpdatedDeclContexts.end();
3572 I != E; ++I)
3573 WriteDeclContextVisibleUpdate(*I);
3574
Douglas Gregor959bb062011-12-03 01:15:29 +00003575 if (!WritingModule) {
3576 // Write the submodules that were imported, if any.
3577 RecordData ImportedModules;
3578 for (ASTContext::import_iterator I = Context.local_import_begin(),
3579 IEnd = Context.local_import_end();
3580 I != IEnd; ++I) {
3581 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3582 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3583 }
3584 if (!ImportedModules.empty()) {
3585 // Sort module IDs.
3586 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3587
3588 // Unique module IDs.
3589 ImportedModules.erase(std::unique(ImportedModules.begin(),
3590 ImportedModules.end()),
3591 ImportedModules.end());
3592
3593 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3594 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003595 }
3596
Douglas Gregordab42432011-08-12 00:15:20 +00003597 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003598 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003599 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003600 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003601 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003602
Douglas Gregor08f01292009-04-17 22:13:46 +00003603 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003604 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003605 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003606 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003607 Record.push_back(NumLexicalDeclContexts);
3608 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003609 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003610 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003611}
3612
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003613/// \brief Go through the declaration update blocks and resolve declaration
3614/// pointers into declaration IDs.
3615void ASTWriter::ResolveDeclUpdatesBlocks() {
3616 for (DeclUpdateMap::iterator
3617 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3618 const Decl *D = I->first;
3619 UpdateRecord &URec = I->second;
3620
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003621 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003622 continue; // The decl will be written completely
3623
3624 unsigned Idx = 0, N = URec.size();
3625 while (Idx < N) {
3626 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003627 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3628 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3629 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3630 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3631 ++Idx;
3632 break;
3633
3634 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3635 ++Idx;
3636 break;
3637 }
3638 }
3639 }
3640}
3641
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003642void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003643 if (DeclUpdates.empty())
3644 return;
3645
3646 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003647 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003648 for (DeclUpdateMap::iterator
3649 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3650 const Decl *D = I->first;
3651 UpdateRecord &URec = I->second;
3652
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003653 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003654 continue; // The decl will be written completely,no need to store updates.
3655
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003656 uint64_t Offset = Stream.GetCurrentBitNo();
3657 Stream.EmitRecord(DECL_UPDATES, URec);
3658
3659 OffsetsRecord.push_back(GetDeclRef(D));
3660 OffsetsRecord.push_back(Offset);
3661 }
3662 Stream.ExitBlock();
3663 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3664}
3665
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003666void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003667 if (ReplacedDecls.empty())
3668 return;
3669
3670 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003671 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003672 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003673 Record.push_back(I->ID);
3674 Record.push_back(I->Offset);
3675 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003676 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003677 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003678}
3679
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003680void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003681 Record.push_back(Loc.getRawEncoding());
3682}
3683
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003684void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003685 AddSourceLocation(Range.getBegin(), Record);
3686 AddSourceLocation(Range.getEnd(), Record);
3687}
3688
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003689void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003690 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003691 const uint64_t *Words = Value.getRawData();
3692 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003693}
3694
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003695void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003696 Record.push_back(Value.isUnsigned());
3697 AddAPInt(Value, Record);
3698}
3699
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003700void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003701 AddAPInt(Value.bitcastToAPInt(), Record);
3702}
3703
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003704void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003705 Record.push_back(getIdentifierRef(II));
3706}
3707
Sebastian Redl539c5062010-08-18 23:57:32 +00003708IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003709 if (II == 0)
3710 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003711
Sebastian Redl539c5062010-08-18 23:57:32 +00003712 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003713 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003714 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003715 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003716}
3717
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003718void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003719 Record.push_back(getSelectorRef(SelRef));
3720}
3721
Sebastian Redl539c5062010-08-18 23:57:32 +00003722SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003723 if (Sel.getAsOpaquePtr() == 0) {
3724 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003725 }
3726
Sebastian Redl539c5062010-08-18 23:57:32 +00003727 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003728 if (SID == 0 && Chain) {
3729 // This might trigger a ReadSelector callback, which will set the ID for
3730 // this selector.
3731 Chain->LoadSelector(Sel);
3732 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003733 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003734 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003735 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003736 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003737}
3738
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003739void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003740 AddDeclRef(Temp->getDestructor(), Record);
3741}
3742
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003743void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3744 CXXBaseSpecifier const *BasesEnd,
3745 RecordDataImpl &Record) {
3746 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3747 CXXBaseSpecifiersToWrite.push_back(
3748 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3749 Bases, BasesEnd));
3750 Record.push_back(NextCXXBaseSpecifiersID++);
3751}
3752
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003753void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003754 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003755 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003756 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003757 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003758 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003759 break;
3760 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003761 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003762 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003763 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003764 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003765 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003766 break;
3767 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003768 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003769 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003770 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003771 break;
John McCall0ad16662009-10-29 08:12:44 +00003772 case TemplateArgument::Null:
3773 case TemplateArgument::Integral:
3774 case TemplateArgument::Declaration:
3775 case TemplateArgument::Pack:
3776 break;
3777 }
3778}
3779
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003780void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003781 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003782 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003783
3784 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3785 bool InfoHasSameExpr
3786 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3787 Record.push_back(InfoHasSameExpr);
3788 if (InfoHasSameExpr)
3789 return; // Avoid storing the same expr twice.
3790 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003791 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3792 Record);
3793}
3794
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003795void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3796 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003797 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003798 AddTypeRef(QualType(), Record);
3799 return;
3800 }
3801
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003802 AddTypeLoc(TInfo->getTypeLoc(), Record);
3803}
3804
3805void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3806 AddTypeRef(TL.getType(), Record);
3807
John McCall8f115c62009-10-16 21:56:05 +00003808 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003809 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003810 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003811}
3812
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003813void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003814 Record.push_back(GetOrCreateTypeID(T));
3815}
3816
Douglas Gregoreda8e122011-08-09 15:13:55 +00003817TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3818 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003819 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3820}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003821
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003822TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003823 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003824 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003825}
3826
3827TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3828 if (T.isNull())
3829 return TypeIdx();
3830 assert(!T.getLocalFastQualifiers());
3831
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003832 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003833 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003834 if (DoneWritingDeclsAndTypes) {
3835 assert(0 && "New type seen after serializing all the types to emit!");
3836 return TypeIdx();
3837 }
3838
Douglas Gregor1970d882009-04-26 03:49:13 +00003839 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003840 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003841 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003842 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003843 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003844 return Idx;
3845}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003846
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003847TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003848 if (T.isNull())
3849 return TypeIdx();
3850 assert(!T.getLocalFastQualifiers());
3851
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003852 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3853 assert(I != TypeIdxs.end() && "Type not emitted!");
3854 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003855}
3856
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003857void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003858 Record.push_back(GetDeclRef(D));
3859}
3860
Sebastian Redl539c5062010-08-18 23:57:32 +00003861DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003862 assert(WritingAST && "Cannot request a declaration ID before AST writing");
3863
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003864 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003865 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003866 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00003867
3868 // If D comes from an AST file, its declaration ID is already known and
3869 // fixed.
3870 if (D->isFromASTFile())
3871 return D->getGlobalID();
3872
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003873 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003874 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003875 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003876 if (DoneWritingDeclsAndTypes) {
3877 assert(0 && "New decl seen after serializing all the decls to emit!");
3878 return 0;
3879 }
3880
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003881 // We haven't seen this declaration before. Give it a new ID and
3882 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003883 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003884 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003885 }
3886
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003887 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003888}
3889
Sebastian Redl539c5062010-08-18 23:57:32 +00003890DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003891 if (D == 0)
3892 return 0;
3893
Douglas Gregorb3163e52012-01-05 22:33:30 +00003894 // If D comes from an AST file, its declaration ID is already known and
3895 // fixed.
3896 if (D->isFromASTFile())
3897 return D->getGlobalID();
3898
Douglas Gregore84a9da2009-04-20 20:36:09 +00003899 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3900 return DeclIDs[D];
3901}
3902
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003903static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
3904 std::pair<unsigned, serialization::DeclID> R) {
3905 return L.first < R.first;
3906}
3907
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003908void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003909 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003910 assert(D);
3911
3912 SourceLocation Loc = D->getLocation();
3913 if (Loc.isInvalid())
3914 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003915
3916 // We only keep track of the file-level declarations of each file.
3917 if (!D->getLexicalDeclContext()->isFileContext())
3918 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00003919 // FIXME: ParmVarDecls that are part of a function type of a parameter of
3920 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00003921 if (isa<ParmVarDecl>(D))
3922 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003923
3924 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00003925 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003926 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003927 FileID FID;
3928 unsigned Offset;
3929 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003930 if (FID.isInvalid())
3931 return;
3932 const SrcMgr::SLocEntry *Entry = &SM.getSLocEntry(FID);
3933 assert(Entry->isFile());
3934
3935 DeclIDInFileInfo *&Info = FileDeclIDs[Entry];
3936 if (!Info)
3937 Info = new DeclIDInFileInfo();
3938
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003939 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003940 LocDeclIDsTy &Decls = Info->DeclIDs;
3941
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00003942 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003943 Decls.push_back(LocDecl);
3944 return;
3945 }
3946
3947 LocDeclIDsTy::iterator
3948 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
3949
3950 Decls.insert(I, LocDecl);
3951}
3952
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003953void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003954 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003955 Record.push_back(Name.getNameKind());
3956 switch (Name.getNameKind()) {
3957 case DeclarationName::Identifier:
3958 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3959 break;
3960
3961 case DeclarationName::ObjCZeroArgSelector:
3962 case DeclarationName::ObjCOneArgSelector:
3963 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003964 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003965 break;
3966
3967 case DeclarationName::CXXConstructorName:
3968 case DeclarationName::CXXDestructorName:
3969 case DeclarationName::CXXConversionFunctionName:
3970 AddTypeRef(Name.getCXXNameType(), Record);
3971 break;
3972
3973 case DeclarationName::CXXOperatorName:
3974 Record.push_back(Name.getCXXOverloadedOperator());
3975 break;
3976
Alexis Hunt3d221f22009-11-29 07:34:05 +00003977 case DeclarationName::CXXLiteralOperatorName:
3978 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3979 break;
3980
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003981 case DeclarationName::CXXUsingDirective:
3982 // No extra data to emit
3983 break;
3984 }
3985}
Chris Lattnerca025db2010-05-07 21:43:38 +00003986
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003987void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003988 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003989 switch (Name.getNameKind()) {
3990 case DeclarationName::CXXConstructorName:
3991 case DeclarationName::CXXDestructorName:
3992 case DeclarationName::CXXConversionFunctionName:
3993 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3994 break;
3995
3996 case DeclarationName::CXXOperatorName:
3997 AddSourceLocation(
3998 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3999 Record);
4000 AddSourceLocation(
4001 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4002 Record);
4003 break;
4004
4005 case DeclarationName::CXXLiteralOperatorName:
4006 AddSourceLocation(
4007 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4008 Record);
4009 break;
4010
4011 case DeclarationName::Identifier:
4012 case DeclarationName::ObjCZeroArgSelector:
4013 case DeclarationName::ObjCOneArgSelector:
4014 case DeclarationName::ObjCMultiArgSelector:
4015 case DeclarationName::CXXUsingDirective:
4016 break;
4017 }
4018}
4019
4020void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004021 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004022 AddDeclarationName(NameInfo.getName(), Record);
4023 AddSourceLocation(NameInfo.getLoc(), Record);
4024 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4025}
4026
4027void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004028 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004029 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004030 Record.push_back(Info.NumTemplParamLists);
4031 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4032 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4033}
4034
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004035void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004036 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004037 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004038 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004039 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004040
4041 // Push each of the NNS's onto a stack for serialization in reverse order.
4042 while (NNS) {
4043 NestedNames.push_back(NNS);
4044 NNS = NNS->getPrefix();
4045 }
4046
4047 Record.push_back(NestedNames.size());
4048 while(!NestedNames.empty()) {
4049 NNS = NestedNames.pop_back_val();
4050 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4051 Record.push_back(Kind);
4052 switch (Kind) {
4053 case NestedNameSpecifier::Identifier:
4054 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4055 break;
4056
4057 case NestedNameSpecifier::Namespace:
4058 AddDeclRef(NNS->getAsNamespace(), Record);
4059 break;
4060
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004061 case NestedNameSpecifier::NamespaceAlias:
4062 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4063 break;
4064
Chris Lattnerca025db2010-05-07 21:43:38 +00004065 case NestedNameSpecifier::TypeSpec:
4066 case NestedNameSpecifier::TypeSpecWithTemplate:
4067 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4068 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4069 break;
4070
4071 case NestedNameSpecifier::Global:
4072 // Don't need to write an associated value.
4073 break;
4074 }
4075 }
4076}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004077
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004078void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4079 RecordDataImpl &Record) {
4080 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004081 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004082 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004083
4084 // Push each of the nested-name-specifiers's onto a stack for
4085 // serialization in reverse order.
4086 while (NNS) {
4087 NestedNames.push_back(NNS);
4088 NNS = NNS.getPrefix();
4089 }
4090
4091 Record.push_back(NestedNames.size());
4092 while(!NestedNames.empty()) {
4093 NNS = NestedNames.pop_back_val();
4094 NestedNameSpecifier::SpecifierKind Kind
4095 = NNS.getNestedNameSpecifier()->getKind();
4096 Record.push_back(Kind);
4097 switch (Kind) {
4098 case NestedNameSpecifier::Identifier:
4099 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4100 AddSourceRange(NNS.getLocalSourceRange(), Record);
4101 break;
4102
4103 case NestedNameSpecifier::Namespace:
4104 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4105 AddSourceRange(NNS.getLocalSourceRange(), Record);
4106 break;
4107
4108 case NestedNameSpecifier::NamespaceAlias:
4109 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4110 AddSourceRange(NNS.getLocalSourceRange(), Record);
4111 break;
4112
4113 case NestedNameSpecifier::TypeSpec:
4114 case NestedNameSpecifier::TypeSpecWithTemplate:
4115 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4116 AddTypeLoc(NNS.getTypeLoc(), Record);
4117 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4118 break;
4119
4120 case NestedNameSpecifier::Global:
4121 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4122 break;
4123 }
4124 }
4125}
4126
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004127void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004128 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004129 Record.push_back(Kind);
4130 switch (Kind) {
4131 case TemplateName::Template:
4132 AddDeclRef(Name.getAsTemplateDecl(), Record);
4133 break;
4134
4135 case TemplateName::OverloadedTemplate: {
4136 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4137 Record.push_back(OvT->size());
4138 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4139 I != E; ++I)
4140 AddDeclRef(*I, Record);
4141 break;
4142 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004143
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004144 case TemplateName::QualifiedTemplate: {
4145 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4146 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4147 Record.push_back(QualT->hasTemplateKeyword());
4148 AddDeclRef(QualT->getTemplateDecl(), Record);
4149 break;
4150 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004151
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004152 case TemplateName::DependentTemplate: {
4153 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4154 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4155 Record.push_back(DepT->isIdentifier());
4156 if (DepT->isIdentifier())
4157 AddIdentifierRef(DepT->getIdentifier(), Record);
4158 else
4159 Record.push_back(DepT->getOperator());
4160 break;
4161 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004162
4163 case TemplateName::SubstTemplateTemplateParm: {
4164 SubstTemplateTemplateParmStorage *subst
4165 = Name.getAsSubstTemplateTemplateParm();
4166 AddDeclRef(subst->getParameter(), Record);
4167 AddTemplateName(subst->getReplacement(), Record);
4168 break;
4169 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004170
4171 case TemplateName::SubstTemplateTemplateParmPack: {
4172 SubstTemplateTemplateParmPackStorage *SubstPack
4173 = Name.getAsSubstTemplateTemplateParmPack();
4174 AddDeclRef(SubstPack->getParameterPack(), Record);
4175 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4176 break;
4177 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004178 }
4179}
4180
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004181void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004182 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004183 Record.push_back(Arg.getKind());
4184 switch (Arg.getKind()) {
4185 case TemplateArgument::Null:
4186 break;
4187 case TemplateArgument::Type:
4188 AddTypeRef(Arg.getAsType(), Record);
4189 break;
4190 case TemplateArgument::Declaration:
4191 AddDeclRef(Arg.getAsDecl(), Record);
4192 break;
4193 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004194 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004195 AddTypeRef(Arg.getIntegralType(), Record);
4196 break;
4197 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004198 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4199 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004200 case TemplateArgument::TemplateExpansion:
4201 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004202 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4203 Record.push_back(*NumExpansions + 1);
4204 else
4205 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004206 break;
4207 case TemplateArgument::Expression:
4208 AddStmt(Arg.getAsExpr());
4209 break;
4210 case TemplateArgument::Pack:
4211 Record.push_back(Arg.pack_size());
4212 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4213 I != E; ++I)
4214 AddTemplateArgument(*I, Record);
4215 break;
4216 }
4217}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004218
4219void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004220ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004221 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004222 assert(TemplateParams && "No TemplateParams!");
4223 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4224 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4225 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4226 Record.push_back(TemplateParams->size());
4227 for (TemplateParameterList::const_iterator
4228 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4229 P != PEnd; ++P)
4230 AddDeclRef(*P, Record);
4231}
4232
4233/// \brief Emit a template argument list.
4234void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004235ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004236 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004237 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004238 Record.push_back(TemplateArgs->size());
4239 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004240 AddTemplateArgument(TemplateArgs->get(i), Record);
4241}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004242
4243
4244void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004245ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004246 Record.push_back(Set.size());
4247 for (UnresolvedSetImpl::const_iterator
4248 I = Set.begin(), E = Set.end(); I != E; ++I) {
4249 AddDeclRef(I.getDecl(), Record);
4250 Record.push_back(I.getAccess());
4251 }
4252}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004253
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004254void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004255 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004256 Record.push_back(Base.isVirtual());
4257 Record.push_back(Base.isBaseOfClass());
4258 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004259 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004260 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004261 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004262 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4263 : SourceLocation(),
4264 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004265}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004266
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004267void ASTWriter::FlushCXXBaseSpecifiers() {
4268 RecordData Record;
4269 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4270 Record.clear();
4271
4272 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004273 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004274 if (Index == CXXBaseSpecifiersOffsets.size())
4275 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4276 else {
4277 if (Index > CXXBaseSpecifiersOffsets.size())
4278 CXXBaseSpecifiersOffsets.resize(Index + 1);
4279 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4280 }
4281
4282 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4283 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4284 Record.push_back(BEnd - B);
4285 for (; B != BEnd; ++B)
4286 AddCXXBaseSpecifier(*B, Record);
4287 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004288
4289 // Flush any expressions that were written as part of the base specifiers.
4290 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004291 }
4292
4293 CXXBaseSpecifiersToWrite.clear();
4294}
4295
Alexis Hunt1d792652011-01-08 20:30:50 +00004296void ASTWriter::AddCXXCtorInitializers(
4297 const CXXCtorInitializer * const *CtorInitializers,
4298 unsigned NumCtorInitializers,
4299 RecordDataImpl &Record) {
4300 Record.push_back(NumCtorInitializers);
4301 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4302 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004303
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004304 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004305 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004306 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004307 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004308 } else if (Init->isDelegatingInitializer()) {
4309 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004310 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004311 } else if (Init->isMemberInitializer()){
4312 Record.push_back(CTOR_INITIALIZER_MEMBER);
4313 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004314 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004315 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4316 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004317 }
Francois Pichetd583da02010-12-04 09:14:42 +00004318
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004319 AddSourceLocation(Init->getMemberLocation(), Record);
4320 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004321 AddSourceLocation(Init->getLParenLoc(), Record);
4322 AddSourceLocation(Init->getRParenLoc(), Record);
4323 Record.push_back(Init->isWritten());
4324 if (Init->isWritten()) {
4325 Record.push_back(Init->getSourceOrder());
4326 } else {
4327 Record.push_back(Init->getNumArrayIndices());
4328 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4329 AddDeclRef(Init->getArrayIndex(i), Record);
4330 }
4331 }
4332}
4333
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004334void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4335 assert(D->DefinitionData);
4336 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004337 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004338 Record.push_back(Data.UserDeclaredConstructor);
4339 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004340 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004341 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004342 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004343 Record.push_back(Data.UserDeclaredDestructor);
4344 Record.push_back(Data.Aggregate);
4345 Record.push_back(Data.PlainOldData);
4346 Record.push_back(Data.Empty);
4347 Record.push_back(Data.Polymorphic);
4348 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004349 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004350 Record.push_back(Data.HasNoNonEmptyBases);
4351 Record.push_back(Data.HasPrivateFields);
4352 Record.push_back(Data.HasProtectedFields);
4353 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004354 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004355 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004356 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004357 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004358 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004359 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004360 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004361 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004362 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004363 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004364 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004365 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004366 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004367 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004368 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004369 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004370 Record.push_back(Data.DeclaredDefaultConstructor);
4371 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004372 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004373 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004374 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004375 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004376 Record.push_back(Data.FailedImplicitMoveConstructor);
4377 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004378 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004379
4380 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004381 if (Data.NumBases > 0)
4382 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4383 Record);
4384
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004385 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4386 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004387 if (Data.NumVBases > 0)
4388 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4389 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004390
4391 AddUnresolvedSet(Data.Conversions, Record);
4392 AddUnresolvedSet(Data.VisibleConversions, Record);
4393 // Data.Definition is the owning decl, no need to write it.
4394 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004395
4396 // Add lambda-specific data.
4397 if (Data.IsLambda) {
4398 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004399 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004400 Record.push_back(Lambda.NumCaptures);
4401 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004402 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004403 AddDeclRef(Lambda.ContextDecl, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004404 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4405 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4406 AddSourceLocation(Capture.getLocation(), Record);
4407 Record.push_back(Capture.isImplicit());
4408 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4409 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4410 AddDeclRef(Var, Record);
4411 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4412 : SourceLocation(),
4413 Record);
4414 }
4415 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004416}
4417
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004418void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004419 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004420 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004421 assert(FirstDeclID == NextDeclID &&
4422 FirstTypeID == NextTypeID &&
4423 FirstIdentID == NextIdentID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004424 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004425 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004426 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004427
Sebastian Redl07a89a82010-07-30 00:29:29 +00004428 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004429
Douglas Gregordf0c1512011-08-18 04:12:04 +00004430 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4431 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4432 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004433 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004434 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004435 NextDeclID = FirstDeclID;
4436 NextTypeID = FirstTypeID;
4437 NextIdentID = FirstIdentID;
4438 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004439 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004440}
4441
Sebastian Redl539c5062010-08-18 23:57:32 +00004442void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004443 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00004444 if (II->hasMacroDefinition())
4445 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004446}
4447
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004448void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004449 // Always take the highest-numbered type index. This copes with an interesting
4450 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004451 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004452 // keep the higher-numbered entry so that we can properly write it out to
4453 // the AST file.
4454 TypeIdx &StoredIdx = TypeIdxs[T];
4455 if (Idx.getIndex() >= StoredIdx.getIndex())
4456 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004457}
4458
Sebastian Redl539c5062010-08-18 23:57:32 +00004459void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004460 SelectorIDs[S] = ID;
4461}
Douglas Gregor91096292010-10-02 19:29:26 +00004462
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004463void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004464 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004465 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004466 MacroDefinitions[MD] = ID;
4467}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004468
Douglas Gregor0abc2622011-12-20 22:06:13 +00004469void ASTWriter::MacroVisible(IdentifierInfo *II) {
4470 DeserializedMacroNames.push_back(II);
4471}
4472
Douglas Gregore37a85a2011-12-02 17:30:13 +00004473void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4474 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4475 SubmoduleIDs[Mod] = ID;
4476}
4477
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004478void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004479 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004480 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004481 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4482 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004483 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004484 // A forward reference was mutated into a definition. Rewrite it.
4485 // FIXME: This happens during template instantiation, should we
4486 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004487 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004488 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004489 }
4490}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004491void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004492 assert(!WritingAST && "Already writing the AST!");
4493
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004494 // TU and namespaces are handled elsewhere.
4495 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4496 return;
4497
Douglas Gregorb3722e22011-09-09 23:01:35 +00004498 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004499 return; // Not a source decl added to a DeclContext from PCH.
4500
4501 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004502 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004503}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004504
4505void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004506 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004507 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004508 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004509 return; // Not a source member added to a class from PCH.
4510 if (!isa<CXXMethodDecl>(D))
4511 return; // We are interested in lazily declared implicit methods.
4512
4513 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004514 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004515 UpdateRecord &Record = DeclUpdates[RD];
4516 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004517 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004518}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004519
4520void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4521 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004522 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004523 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004524 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004525 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004526 return; // Not a source specialization added to a template from PCH.
4527
4528 UpdateRecord &Record = DeclUpdates[TD];
4529 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004530 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004531}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004532
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004533void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4534 const FunctionDecl *D) {
4535 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004536 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004537 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004538 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004539 return; // Not a source specialization added to a template from PCH.
4540
4541 UpdateRecord &Record = DeclUpdates[TD];
4542 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004543 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004544}
4545
Sebastian Redlab238a72011-04-24 16:28:06 +00004546void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004547 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004548 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004549 return; // Declaration not imported from PCH.
4550
4551 // Implicit decl from a PCH was defined.
4552 // FIXME: Should implicit definition be a separate FunctionDecl?
4553 RewriteDecl(D);
4554}
4555
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004556void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004557 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004558 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004559 return;
4560
4561 // Since the actual instantiation is delayed, this really means that we need
4562 // to update the instantiation location.
4563 UpdateRecord &Record = DeclUpdates[D];
4564 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4565 AddSourceLocation(
4566 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4567}
4568
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004569void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4570 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004571 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004572 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004573 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004574
4575 assert(IFD->getDefinition() && "Category on a class without a definition?");
4576 ObjCClassesWithCategories.insert(
4577 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004578}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004579
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004580
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004581void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4582 const ObjCPropertyDecl *OrigProp,
4583 const ObjCCategoryDecl *ClassExt) {
4584 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4585 if (!D)
4586 return;
4587
4588 assert(!WritingAST && "Already writing the AST!");
4589 if (!D->isFromASTFile())
4590 return; // Declaration not imported from PCH.
4591
4592 RewriteDecl(D);
4593}