blob: adbca78d97c71e571e78c583d432674c0b124950 [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 Gregorcb177f12012-10-16 23:40:58 +000038#include "clang/Basic/TargetOptions.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregor925296b2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054
Sebastian Redl3df5a082010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000060}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000066}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000071
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
David Blaikie83d382b2011-09-23 05:06:16 +000095 llvm_unreachable("Built-in types are never serialized");
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096}
97
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106}
107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117}
118
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122}
123
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128}
129
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140}
141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153}
154
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176}
177
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181}
182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
Richard Smith5e580292012-02-10 09:58:53 +0000189 Record.push_back(T->hasTrailingReturn());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000190 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000191 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000192 Record.push_back(T->getExceptionSpecType());
193 if (T->getExceptionSpecType() == EST_Dynamic) {
194 Record.push_back(T->getNumExceptions());
195 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
196 Writer.AddTypeRef(T->getExceptionType(I), Record);
197 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
198 Writer.AddStmt(T->getNoexceptExpr());
Richard Smith8b987a92012-04-21 17:47:47 +0000199 } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
200 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
201 Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
Richard Smithd3b5c9082012-07-27 04:22:15 +0000202 } else if (T->getExceptionSpecType() == EST_Unevaluated) {
203 Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000204 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000205 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000206}
207
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000209 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000210 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000211}
John McCallb96ec562009-12-04 22:46:56 +0000212
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000215 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
216 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000217 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218}
219
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000221 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000222 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223}
224
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000226 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000227 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000228}
229
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000230void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Douglas Gregor81495f32012-02-12 18:42:33 +0000231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Anders Carlsson81df7b82009-06-24 19:06:50 +0000232 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000233 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000234}
235
Alexis Hunte852b102011-05-24 22:41:36 +0000236void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
237 Writer.AddTypeRef(T->getBaseType(), Record);
238 Writer.AddTypeRef(T->getUnderlyingType(), Record);
239 Record.push_back(T->getUTTKind());
240 Code = TYPE_UNARY_TRANSFORM;
241}
242
Richard Smith30482bc2011-02-20 03:19:35 +0000243void ASTTypeWriter::VisitAutoType(const AutoType *T) {
244 Writer.AddTypeRef(T->getDeducedType(), Record);
245 Code = TYPE_AUTO;
246}
247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000249 Record.push_back(T->isDependentType());
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000250 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000251 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000252 "Cannot serialize in the middle of a type definition");
253}
254
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000257 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000258}
259
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000260void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000261 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000262 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263}
264
John McCall81904512011-01-06 01:58:22 +0000265void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
266 Writer.AddTypeRef(T->getModifiedType(), Record);
267 Writer.AddTypeRef(T->getEquivalentType(), Record);
268 Record.push_back(T->getAttrKind());
269 Code = TYPE_ATTRIBUTED;
270}
271
Mike Stump11289f42009-09-09 15:08:12 +0000272void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000273ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000274 const SubstTemplateTypeParmType *T) {
275 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
276 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000277 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000278}
279
280void
Douglas Gregorada4b792011-01-14 02:55:32 +0000281ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
282 const SubstTemplateTypeParmPackType *T) {
283 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
284 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
285 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
286}
287
288void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000289ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000290 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000291 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292 Writer.AddTemplateName(T->getTemplateName(), Record);
293 Record.push_back(T->getNumArgs());
294 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
295 ArgI != ArgE; ++ArgI)
296 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000297 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
298 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000299 : T->getCanonicalTypeInternal(),
300 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000301 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000306 VisitArrayType(T);
307 Writer.AddStmt(T->getSizeExpr());
308 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000309 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000310}
311
312void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000314 const DependentSizedExtVectorType *T) {
315 // FIXME: Serialize this type (C++ only)
David Blaikie83d382b2011-09-23 05:06:16 +0000316 llvm_unreachable("Cannot serialize dependent sized extended vector types");
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000317}
318
319void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000320ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000321 Record.push_back(T->getDepth());
322 Record.push_back(T->getIndex());
323 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000324 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000325 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000326}
327
328void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000329ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000330 Record.push_back(T->getKeyword());
331 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
332 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000333 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
334 : T->getCanonicalTypeInternal(),
335 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000337}
338
339void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000340ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000341 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000342 Record.push_back(T->getKeyword());
343 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
344 Writer.AddIdentifierRef(T->getIdentifier(), Record);
345 Record.push_back(T->getNumArgs());
346 for (DependentTemplateSpecializationType::iterator
347 I = T->begin(), E = T->end(); I != E; ++I)
348 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000349 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000350}
351
Douglas Gregord2fa7662010-12-20 02:24:11 +0000352void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
353 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000354 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
355 Record.push_back(*NumExpansions + 1);
356 else
357 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000358 Code = TYPE_PACK_EXPANSION;
359}
360
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000361void ASTTypeWriter::VisitParenType(const ParenType *T) {
362 Writer.AddTypeRef(T->getInnerType(), Record);
363 Code = TYPE_PAREN;
364}
365
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000367 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000368 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
369 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000370 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000371}
372
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000373void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
Douglas Gregor9f218892012-03-26 15:52:37 +0000374 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000375 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000376 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000377}
378
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000379void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregorf3bccd72012-01-17 19:21:53 +0000380 Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000381 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000382}
383
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000384void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000385 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000386 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000387 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000388 E = T->qual_end(); I != E; ++I)
389 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000390 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000391}
392
Steve Narofffb4330f2009-06-17 22:40:22 +0000393void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000394ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000395 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000396 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000397}
398
Eli Friedman0dfb8892011-10-06 23:00:33 +0000399void
400ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
401 Writer.AddTypeRef(T->getValueType(), Record);
402 Code = TYPE_ATOMIC;
403}
404
John McCall8f115c62009-10-16 21:56:05 +0000405namespace {
406
407class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000408 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000409 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000410
411public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000412 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000413 : Writer(Writer), Record(Record) { }
414
John McCall17001972009-10-18 01:05:36 +0000415#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000416#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000417 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000418#include "clang/AST/TypeLocNodes.def"
419
John McCall17001972009-10-18 01:05:36 +0000420 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
421 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000422};
423
424}
425
John McCall17001972009-10-18 01:05:36 +0000426void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
427 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000430 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
431 if (TL.needsExtraLocalData()) {
432 Record.push_back(TL.getWrittenTypeSpec());
433 Record.push_back(TL.getWrittenSignSpec());
434 Record.push_back(TL.getWrittenWidthSpec());
435 Record.push_back(TL.hasModeAttr());
436 }
John McCall8f115c62009-10-16 21:56:05 +0000437}
John McCall17001972009-10-18 01:05:36 +0000438void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000443}
John McCall17001972009-10-18 01:05:36 +0000444void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000446}
John McCall17001972009-10-18 01:05:36 +0000447void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000449}
John McCall17001972009-10-18 01:05:36 +0000450void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000452}
John McCall17001972009-10-18 01:05:36 +0000453void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000455 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000456}
John McCall17001972009-10-18 01:05:36 +0000457void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
459 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
460 Record.push_back(TL.getSizeExpr() ? 1 : 0);
461 if (TL.getSizeExpr())
462 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000463}
John McCall17001972009-10-18 01:05:36 +0000464void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
465 VisitArrayTypeLoc(TL);
466}
467void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
468 VisitArrayTypeLoc(TL);
469}
470void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
471 VisitArrayTypeLoc(TL);
472}
473void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
474 DependentSizedArrayTypeLoc TL) {
475 VisitArrayTypeLoc(TL);
476}
477void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
478 DependentSizedExtVectorTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
484void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
487void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000488 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000489 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
490 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000491 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
John McCall17001972009-10-18 01:05:36 +0000492 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
493 Writer.AddDeclRef(TL.getArg(i), Record);
494}
495void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
496 VisitFunctionTypeLoc(TL);
497}
498void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
499 VisitFunctionTypeLoc(TL);
500}
John McCallb96ec562009-12-04 22:46:56 +0000501void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCall17001972009-10-18 01:05:36 +0000504void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
507void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000508 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000511}
512void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000513 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
514 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
515 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
516 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000517}
518void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
Alexis Hunte852b102011-05-24 22:41:36 +0000521void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getKWLoc(), Record);
523 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
524 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
525 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
526}
Richard Smith30482bc2011-02-20 03:19:35 +0000527void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
John McCall17001972009-10-18 01:05:36 +0000530void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getNameLoc(), Record);
532}
533void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
535}
John McCall81904512011-01-06 01:58:22 +0000536void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
538 if (TL.hasAttrOperand()) {
539 SourceRange range = TL.getAttrOperandParensRange();
540 Writer.AddSourceLocation(range.getBegin(), Record);
541 Writer.AddSourceLocation(range.getEnd(), Record);
542 }
543 if (TL.hasAttrExprOperand()) {
544 Expr *operand = TL.getAttrExprOperand();
545 Record.push_back(operand ? 1 : 0);
546 if (operand) Writer.AddStmt(operand);
547 } else if (TL.hasAttrEnumOperand()) {
548 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
549 }
550}
John McCall17001972009-10-18 01:05:36 +0000551void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
John McCallcebee162009-10-18 09:09:24 +0000554void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
555 SubstTemplateTypeParmTypeLoc TL) {
556 Writer.AddSourceLocation(TL.getNameLoc(), Record);
557}
Douglas Gregorada4b792011-01-14 02:55:32 +0000558void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
559 SubstTemplateTypeParmPackTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561}
John McCall17001972009-10-18 01:05:36 +0000562void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
563 TemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000564 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
John McCall0ad16662009-10-29 08:12:44 +0000565 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
566 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
567 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
568 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000569 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
570 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000571}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000572void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
573 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
574 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
575}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000576void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000577 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000578 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000579}
John McCalle78aac42010-03-10 03:28:59 +0000580void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000583void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnara9033e2b2012-02-06 19:09:27 +0000584 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000585 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000586 Writer.AddSourceLocation(TL.getNameLoc(), Record);
587}
John McCallc392f372010-06-11 00:33:02 +0000588void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
589 DependentTemplateSpecializationTypeLoc TL) {
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000590 Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000591 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
Abramo Bagnarae0a70b22012-02-06 22:45:07 +0000592 Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
Abramo Bagnara48c05be2012-02-06 14:41:24 +0000593 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000594 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
595 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
596 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000597 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
598 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000599}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000600void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
601 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
602}
John McCall17001972009-10-18 01:05:36 +0000603void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
604 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000605}
606void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
607 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000608 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
609 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
610 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
611 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000612}
John McCallfc93cf92009-10-22 22:37:11 +0000613void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
614 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000615}
Eli Friedman0dfb8892011-10-06 23:00:33 +0000616void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
617 Writer.AddSourceLocation(TL.getKWLoc(), Record);
618 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
619 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
620}
John McCall8f115c62009-10-16 21:56:05 +0000621
Chris Lattner19cea4e2009-04-22 05:57:30 +0000622//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000623// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000624//===----------------------------------------------------------------------===//
625
Chris Lattner28fa4e62009-04-26 22:26:21 +0000626static void EmitBlockID(unsigned ID, const char *Name,
627 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000628 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000629 Record.clear();
630 Record.push_back(ID);
631 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
632
633 // Emit the block name if present.
634 if (Name == 0 || Name[0] == 0) return;
635 Record.clear();
636 while (*Name)
637 Record.push_back(*Name++);
638 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
639}
640
641static void EmitRecordID(unsigned ID, const char *Name,
642 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000643 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000644 Record.clear();
645 Record.push_back(ID);
646 while (*Name)
647 Record.push_back(*Name++);
648 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000649}
650
651static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000652 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000653#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000654 RECORD(STMT_STOP);
655 RECORD(STMT_NULL_PTR);
656 RECORD(STMT_NULL);
657 RECORD(STMT_COMPOUND);
658 RECORD(STMT_CASE);
659 RECORD(STMT_DEFAULT);
660 RECORD(STMT_LABEL);
Richard Smithc202b282012-04-14 00:33:13 +0000661 RECORD(STMT_ATTRIBUTED);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000662 RECORD(STMT_IF);
663 RECORD(STMT_SWITCH);
664 RECORD(STMT_WHILE);
665 RECORD(STMT_DO);
666 RECORD(STMT_FOR);
667 RECORD(STMT_GOTO);
668 RECORD(STMT_INDIRECT_GOTO);
669 RECORD(STMT_CONTINUE);
670 RECORD(STMT_BREAK);
671 RECORD(STMT_RETURN);
672 RECORD(STMT_DECL);
Chad Rosierde70e0e2012-08-25 00:11:56 +0000673 RECORD(STMT_GCCASM);
Chad Rosiere30d4992012-08-24 23:51:02 +0000674 RECORD(STMT_MSASM);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000675 RECORD(EXPR_PREDEFINED);
676 RECORD(EXPR_DECL_REF);
677 RECORD(EXPR_INTEGER_LITERAL);
678 RECORD(EXPR_FLOATING_LITERAL);
679 RECORD(EXPR_IMAGINARY_LITERAL);
680 RECORD(EXPR_STRING_LITERAL);
681 RECORD(EXPR_CHARACTER_LITERAL);
682 RECORD(EXPR_PAREN);
683 RECORD(EXPR_UNARY_OPERATOR);
684 RECORD(EXPR_SIZEOF_ALIGN_OF);
685 RECORD(EXPR_ARRAY_SUBSCRIPT);
686 RECORD(EXPR_CALL);
687 RECORD(EXPR_MEMBER);
688 RECORD(EXPR_BINARY_OPERATOR);
689 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
690 RECORD(EXPR_CONDITIONAL_OPERATOR);
691 RECORD(EXPR_IMPLICIT_CAST);
692 RECORD(EXPR_CSTYLE_CAST);
693 RECORD(EXPR_COMPOUND_LITERAL);
694 RECORD(EXPR_EXT_VECTOR_ELEMENT);
695 RECORD(EXPR_INIT_LIST);
696 RECORD(EXPR_DESIGNATED_INIT);
697 RECORD(EXPR_IMPLICIT_VALUE_INIT);
698 RECORD(EXPR_VA_ARG);
699 RECORD(EXPR_ADDR_LABEL);
700 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000701 RECORD(EXPR_CHOOSE);
702 RECORD(EXPR_GNU_NULL);
703 RECORD(EXPR_SHUFFLE_VECTOR);
704 RECORD(EXPR_BLOCK);
Peter Collingbourne91147592011-04-15 00:35:48 +0000705 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000706 RECORD(EXPR_OBJC_STRING_LITERAL);
Patrick Beard0caa3942012-04-19 00:25:12 +0000707 RECORD(EXPR_OBJC_BOXED_EXPRESSION);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000708 RECORD(EXPR_OBJC_ARRAY_LITERAL);
709 RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000710 RECORD(EXPR_OBJC_ENCODE);
711 RECORD(EXPR_OBJC_SELECTOR_EXPR);
712 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
713 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
714 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
715 RECORD(EXPR_OBJC_KVC_REF_EXPR);
716 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000717 RECORD(STMT_OBJC_FOR_COLLECTION);
718 RECORD(STMT_OBJC_CATCH);
719 RECORD(STMT_OBJC_FINALLY);
720 RECORD(STMT_OBJC_AT_TRY);
721 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
722 RECORD(STMT_OBJC_AT_THROW);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000723 RECORD(EXPR_OBJC_BOOL_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000724 RECORD(EXPR_CXX_OPERATOR_CALL);
725 RECORD(EXPR_CXX_CONSTRUCT);
726 RECORD(EXPR_CXX_STATIC_CAST);
727 RECORD(EXPR_CXX_DYNAMIC_CAST);
728 RECORD(EXPR_CXX_REINTERPRET_CAST);
729 RECORD(EXPR_CXX_CONST_CAST);
730 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
Richard Smithc67fdd42012-03-07 08:35:16 +0000731 RECORD(EXPR_USER_DEFINED_LITERAL);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000732 RECORD(EXPR_CXX_BOOL_LITERAL);
733 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000734 RECORD(EXPR_CXX_TYPEID_EXPR);
735 RECORD(EXPR_CXX_TYPEID_TYPE);
736 RECORD(EXPR_CXX_UUIDOF_EXPR);
737 RECORD(EXPR_CXX_UUIDOF_TYPE);
738 RECORD(EXPR_CXX_THIS);
739 RECORD(EXPR_CXX_THROW);
740 RECORD(EXPR_CXX_DEFAULT_ARG);
741 RECORD(EXPR_CXX_BIND_TEMPORARY);
742 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
743 RECORD(EXPR_CXX_NEW);
744 RECORD(EXPR_CXX_DELETE);
745 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
746 RECORD(EXPR_EXPR_WITH_CLEANUPS);
747 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
748 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
749 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
750 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
751 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
752 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
753 RECORD(EXPR_CXX_NOEXCEPT);
754 RECORD(EXPR_OPAQUE_VALUE);
755 RECORD(EXPR_BINARY_TYPE_TRAIT);
756 RECORD(EXPR_PACK_EXPANSION);
757 RECORD(EXPR_SIZEOF_PACK);
758 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000759 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000760#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000761}
Mike Stump11289f42009-09-09 15:08:12 +0000762
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000763void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000764 RecordData Record;
765 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000766
Sebastian Redl539c5062010-08-18 23:57:32 +0000767#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
768#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000769
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000770 // Control Block.
771 BLOCK(CONTROL_BLOCK);
772 RECORD(METADATA);
773 RECORD(IMPORTS);
774 RECORD(LANGUAGE_OPTIONS);
775 RECORD(TARGET_OPTIONS);
Douglas Gregorfad10d82012-10-18 18:36:53 +0000776 RECORD(ORIGINAL_FILE);
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000777 RECORD(ORIGINAL_PCH_DIR);
778
779 // AST Top-Level Block.
780 BLOCK(AST_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000781 RECORD(TYPE_OFFSET);
782 RECORD(DECL_OFFSET);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000783 RECORD(IDENTIFIER_OFFSET);
784 RECORD(IDENTIFIER_TABLE);
785 RECORD(EXTERNAL_DEFINITIONS);
786 RECORD(SPECIAL_TYPES);
787 RECORD(STATISTICS);
788 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000789 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000790 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
791 RECORD(SELECTOR_OFFSETS);
792 RECORD(METHOD_POOL);
793 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000794 RECORD(SOURCE_LOCATION_OFFSETS);
795 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000796 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000797 RECORD(EXT_VECTOR_DECLS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +0000798 RECORD(PPD_ENTITIES_OFFSETS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000799 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000800 RECORD(TU_UPDATE_LEXICAL);
Douglas Gregor358cd442012-01-15 16:58:34 +0000801 RECORD(LOCAL_REDECLARATIONS_MAP);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000802 RECORD(SEMA_DECL_REFS);
803 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
804 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
805 RECORD(DECL_REPLACEMENTS);
806 RECORD(UPDATE_VISIBLE);
807 RECORD(DECL_UPDATE_OFFSETS);
808 RECORD(DECL_UPDATES);
809 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
810 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000811 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000812 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000813 RECORD(FP_PRAGMA_OPTIONS);
814 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000815 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000816 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000817 RECORD(MODULE_OFFSET_MAP);
818 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000819 RECORD(OBJC_CATEGORIES_MAP);
Douglas Gregor66e4add2011-12-19 21:09:25 +0000820 RECORD(FILE_SORTED_DECLS);
821 RECORD(IMPORTED_MODULES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000822 RECORD(MERGED_DECLARATIONS);
823 RECORD(LOCAL_REDECLARATIONS);
Douglas Gregor404cdde2012-01-27 01:47:08 +0000824 RECORD(OBJC_CATEGORIES);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000825 RECORD(MACRO_OFFSET);
826 RECORD(MACRO_UPDATES);
Douglas Gregor358cd442012-01-15 16:58:34 +0000827
Chris Lattner28fa4e62009-04-26 22:26:21 +0000828 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000829 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000830 RECORD(SM_SLOC_FILE_ENTRY);
831 RECORD(SM_SLOC_BUFFER_ENTRY);
832 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000833 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattner28fa4e62009-04-26 22:26:21 +0000835 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000836 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000837 RECORD(PP_MACRO_OBJECT_LIKE);
838 RECORD(PP_MACRO_FUNCTION_LIKE);
839 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000840
Douglas Gregor12bfa382009-10-17 00:13:19 +0000841 // Decls and Types block.
842 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000843 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000844 RECORD(TYPE_COMPLEX);
845 RECORD(TYPE_POINTER);
846 RECORD(TYPE_BLOCK_POINTER);
847 RECORD(TYPE_LVALUE_REFERENCE);
848 RECORD(TYPE_RVALUE_REFERENCE);
849 RECORD(TYPE_MEMBER_POINTER);
850 RECORD(TYPE_CONSTANT_ARRAY);
851 RECORD(TYPE_INCOMPLETE_ARRAY);
852 RECORD(TYPE_VARIABLE_ARRAY);
853 RECORD(TYPE_VECTOR);
854 RECORD(TYPE_EXT_VECTOR);
855 RECORD(TYPE_FUNCTION_PROTO);
856 RECORD(TYPE_FUNCTION_NO_PROTO);
857 RECORD(TYPE_TYPEDEF);
858 RECORD(TYPE_TYPEOF_EXPR);
859 RECORD(TYPE_TYPEOF);
860 RECORD(TYPE_RECORD);
861 RECORD(TYPE_ENUM);
862 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000863 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000864 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000865 RECORD(TYPE_DECLTYPE);
866 RECORD(TYPE_ELABORATED);
867 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
868 RECORD(TYPE_UNRESOLVED_USING);
869 RECORD(TYPE_INJECTED_CLASS_NAME);
870 RECORD(TYPE_OBJC_OBJECT);
871 RECORD(TYPE_TEMPLATE_TYPE_PARM);
872 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
873 RECORD(TYPE_DEPENDENT_NAME);
874 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
875 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
876 RECORD(TYPE_PAREN);
877 RECORD(TYPE_PACK_EXPANSION);
878 RECORD(TYPE_ATTRIBUTED);
879 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Eli Friedman0dfb8892011-10-06 23:00:33 +0000880 RECORD(TYPE_ATOMIC);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000881 RECORD(DECL_TYPEDEF);
882 RECORD(DECL_ENUM);
883 RECORD(DECL_RECORD);
884 RECORD(DECL_ENUM_CONSTANT);
885 RECORD(DECL_FUNCTION);
886 RECORD(DECL_OBJC_METHOD);
887 RECORD(DECL_OBJC_INTERFACE);
888 RECORD(DECL_OBJC_PROTOCOL);
889 RECORD(DECL_OBJC_IVAR);
890 RECORD(DECL_OBJC_AT_DEFS_FIELD);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000891 RECORD(DECL_OBJC_CATEGORY);
892 RECORD(DECL_OBJC_CATEGORY_IMPL);
893 RECORD(DECL_OBJC_IMPLEMENTATION);
894 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
895 RECORD(DECL_OBJC_PROPERTY);
896 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000897 RECORD(DECL_FIELD);
898 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000899 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000901 RECORD(DECL_FILE_SCOPE_ASM);
902 RECORD(DECL_BLOCK);
903 RECORD(DECL_CONTEXT_LEXICAL);
904 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000905 RECORD(DECL_NAMESPACE);
906 RECORD(DECL_NAMESPACE_ALIAS);
907 RECORD(DECL_USING);
908 RECORD(DECL_USING_SHADOW);
909 RECORD(DECL_USING_DIRECTIVE);
910 RECORD(DECL_UNRESOLVED_USING_VALUE);
911 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
912 RECORD(DECL_LINKAGE_SPEC);
913 RECORD(DECL_CXX_RECORD);
914 RECORD(DECL_CXX_METHOD);
915 RECORD(DECL_CXX_CONSTRUCTOR);
916 RECORD(DECL_CXX_DESTRUCTOR);
917 RECORD(DECL_CXX_CONVERSION);
918 RECORD(DECL_ACCESS_SPEC);
919 RECORD(DECL_FRIEND);
920 RECORD(DECL_FRIEND_TEMPLATE);
921 RECORD(DECL_CLASS_TEMPLATE);
922 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
923 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
924 RECORD(DECL_FUNCTION_TEMPLATE);
925 RECORD(DECL_TEMPLATE_TYPE_PARM);
926 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
927 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
928 RECORD(DECL_STATIC_ASSERT);
929 RECORD(DECL_CXX_BASE_SPECIFIERS);
930 RECORD(DECL_INDIRECTFIELD);
931 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
932
Douglas Gregor03412ba2011-06-03 02:27:19 +0000933 // Statements and Exprs can occur in the Decls and Types block.
934 AddStmtsExprs(Stream, Record);
935
Douglas Gregor92a96f52011-02-08 21:58:10 +0000936 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000937 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000938 RECORD(PPD_MACRO_DEFINITION);
939 RECORD(PPD_INCLUSION_DIRECTIVE);
940
Chris Lattner28fa4e62009-04-26 22:26:21 +0000941#undef RECORD
942#undef BLOCK
943 Stream.ExitBlock();
944}
945
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000946/// \brief Adjusts the given filename to only write out the portion of the
947/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000948///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000949/// \param Filename the file name to adjust.
950///
951/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
952/// the returned filename will be adjusted by this system root.
953///
954/// \returns either the original filename (if it needs no adjustment) or the
955/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000956static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000957adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000959
Douglas Gregorc567ba22011-07-22 16:35:34 +0000960 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000961 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000962
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 // Verify that the filename and the system root have the same prefix.
964 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000965 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000966 if (Filename[Pos] != isysroot[Pos])
967 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000968
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000969 // We hit the end of the filename before we hit the end of the system root.
970 if (!Filename[Pos])
971 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000972
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 // If the file name has a '/' at the current position, skip over the '/'.
974 // We distinguish sysroot-based includes from absolute includes by the
975 // absence of '/' at the beginning of sysroot-based includes.
976 if (Filename[Pos] == '/')
977 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000978
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000979 return Filename + Pos;
980}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000981
Douglas Gregor112b9072012-10-18 05:31:06 +0000982/// \brief Write the control block.
983void ASTWriter::WriteControlBlock(ASTContext &Context, StringRef isysroot,
984 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000985 using namespace llvm;
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000986 Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
987 RecordData Record;
Douglas Gregor112b9072012-10-18 05:31:06 +0000988
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000989 // Metadata
Douglas Gregor0aa21c92012-10-18 18:27:37 +0000990 BitCodeAbbrev *MetadataAbbrev = new BitCodeAbbrev();
991 MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
992 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
993 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
994 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
995 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
996 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
997 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
998 MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
999 unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1000 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +00001001 Record.push_back(VERSION_MAJOR);
1002 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001003 Record.push_back(CLANG_VERSION_MAJOR);
1004 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +00001005 Record.push_back(!isysroot.empty());
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00001006 Record.push_back(ASTHasCompilerErrors);
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001007 Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1008 getClangFullRepositoryVersion());
Douglas Gregor29cc6422011-08-17 21:07:30 +00001009
Douglas Gregor112b9072012-10-18 05:31:06 +00001010 // Imports
Douglas Gregor29cc6422011-08-17 21:07:30 +00001011 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +00001012 serialization::ModuleManager &Mgr = Chain->getModuleManager();
1013 llvm::SmallVector<char, 128> ModulePaths;
1014 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +00001015
1016 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
1017 M != MEnd; ++M) {
1018 // Skip modules that weren't directly imported.
1019 if (!(*M)->isDirectlyImported())
1020 continue;
1021
1022 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
1023 // FIXME: Write import location, once it matters.
1024 // FIXME: This writes the absolute path for AST files we depend on.
1025 const std::string &FileName = (*M)->FileName;
1026 Record.push_back(FileName.size());
1027 Record.append(FileName.begin(), FileName.end());
1028 }
Douglas Gregor29cc6422011-08-17 21:07:30 +00001029 Stream.EmitRecord(IMPORTS, Record);
1030 }
Mike Stump11289f42009-09-09 15:08:12 +00001031
Douglas Gregor112b9072012-10-18 05:31:06 +00001032 // Language options.
1033 Record.clear();
1034 const LangOptions &LangOpts = Context.getLangOpts();
1035#define LANGOPT(Name, Bits, Default, Description) \
1036 Record.push_back(LangOpts.Name);
1037#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1038 Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1039#include "clang/Basic/LangOptions.def"
1040
1041 Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1042 AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1043
1044 Record.push_back(LangOpts.CurrentModule.size());
1045 Record.append(LangOpts.CurrentModule.begin(), LangOpts.CurrentModule.end());
1046 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1047
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001048 // Target options.
1049 Record.clear();
Douglas Gregor0aa21c92012-10-18 18:27:37 +00001050 const TargetInfo &Target = Context.getTargetInfo();
1051 const TargetOptions &TargetOpts = Target.getTargetOpts();
Douglas Gregor4d3611c2012-10-18 17:58:09 +00001052 AddString(TargetOpts.Triple, Record);
1053 AddString(TargetOpts.CPU, Record);
1054 AddString(TargetOpts.ABI, Record);
1055 AddString(TargetOpts.CXXABI, Record);
1056 AddString(TargetOpts.LinkerVersion, Record);
1057 Record.push_back(TargetOpts.FeaturesAsWritten.size());
1058 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1059 AddString(TargetOpts.FeaturesAsWritten[I], Record);
1060 }
1061 Record.push_back(TargetOpts.Features.size());
1062 for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1063 AddString(TargetOpts.Features[I], Record);
1064 }
1065 Stream.EmitRecord(TARGET_OPTIONS, Record);
1066
Douglas Gregora3b20262011-05-06 21:43:30 +00001067 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001068 SourceManager &SM = Context.getSourceManager();
1069 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1070 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Douglas Gregorfad10d82012-10-18 18:36:53 +00001071 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1072 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
Douglas Gregor45fe0362009-05-12 01:31:05 +00001073 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1074 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1075
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001076 SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001077
Michael J. Spencer740857f2010-12-21 16:45:57 +00001078 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001079
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001080 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001081 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001082 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001083 RecordData Record;
Douglas Gregorfad10d82012-10-18 18:36:53 +00001084 Record.push_back(ORIGINAL_FILE);
Douglas Gregora3b20262011-05-06 21:43:30 +00001085 Record.push_back(SM.getMainFileID().getOpaqueValue());
Douglas Gregorfad10d82012-10-18 18:36:53 +00001086 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
1087 Record.clear();
Douglas Gregor45fe0362009-05-12 01:31:05 +00001088 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001089
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001090 // Original PCH directory
1091 if (!OutputFile.empty() && OutputFile != "-") {
1092 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1093 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1094 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1095 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1096
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001097 SmallString<128> OutputPath(OutputFile);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001098
1099 llvm::sys::fs::make_absolute(OutputPath);
1100 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1101
1102 RecordData Record;
1103 Record.push_back(ORIGINAL_PCH_DIR);
1104 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1105 }
1106
Douglas Gregor72be3902012-10-19 00:38:02 +00001107 WriteInputFiles(Context.SourceMgr, isysroot);
1108 Stream.ExitBlock();
1109}
1110
1111void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, StringRef isysroot) {
1112 using namespace llvm;
1113 Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1114 RecordData Record;
1115
1116 // Create input-file abbreviation.
1117 BitCodeAbbrev *IFAbbrev = new BitCodeAbbrev();
1118 IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1119 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1120 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1121 IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1122 unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1123
1124 // Write out all of the input files.
1125 std::vector<uint32_t> InputFileOffsets;
1126 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1127 // Get this source location entry.
1128 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1129 FileID FID = FileID::get(I);
1130 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
1131
1132 // We only care about file entries that were not overridden.
1133 if (!SLoc->isFile())
1134 continue;
1135 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1136 if (!Cache->OrigEntry || Cache->BufferOverridden)
1137 continue;
1138
1139 Record.clear();
1140 Record.push_back(INPUT_FILE);
1141
1142 // Emit size/modification time for this file.
1143 Record.push_back(Cache->OrigEntry->getSize());
1144 Record.push_back(Cache->OrigEntry->getModificationTime());
1145
1146 // Turn the file name into an absolute path, if it isn't already.
1147 const char *Filename = Cache->OrigEntry->getName();
1148 SmallString<128> FilePath(Filename);
1149
1150 // Ask the file manager to fixup the relative path for us. This will
1151 // honor the working directory.
1152 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1153
1154 // FIXME: This call to make_absolute shouldn't be necessary, the
1155 // call to FixupRelativePath should always return an absolute path.
1156 llvm::sys::fs::make_absolute(FilePath);
1157 Filename = FilePath.c_str();
1158
1159 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1160
1161 Stream.EmitRecordWithBlob(IFAbbrevCode, Record, Filename);
1162 }
1163
Douglas Gregor112b9072012-10-18 05:31:06 +00001164 Stream.ExitBlock();
Douglas Gregor55abb232009-04-10 20:39:37 +00001165}
1166
Douglas Gregora7f71a92009-04-10 03:52:48 +00001167//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001168// stat cache Serialization
1169//===----------------------------------------------------------------------===//
1170
1171namespace {
1172// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001173class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001174public:
1175 typedef const char * key_type;
1176 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattner2a6fa472010-11-23 19:28:12 +00001178 typedef struct stat data_type;
1179 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001180
1181 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001182 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001183 }
Mike Stump11289f42009-09-09 15:08:12 +00001184
1185 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001186 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001187 data_type_ref Data) {
1188 unsigned StrLen = strlen(path);
1189 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001190 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001191 clang::io::Emit8(Out, DataLen);
1192 return std::make_pair(StrLen + 1, DataLen);
1193 }
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001195 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001196 Out.write(path, KeyLen);
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001199 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001200 data_type_ref Data, unsigned DataLen) {
1201 using namespace clang::io;
1202 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner2a6fa472010-11-23 19:28:12 +00001204 Emit32(Out, (uint32_t) Data.st_ino);
1205 Emit32(Out, (uint32_t) Data.st_dev);
1206 Emit16(Out, (uint16_t) Data.st_mode);
1207 Emit64(Out, (uint64_t) Data.st_mtime);
1208 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001209
1210 assert(Out.tell() - Start == DataLen && "Wrong data length");
1211 }
1212};
1213} // end anonymous namespace
1214
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001215/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001216void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001217 // Build the on-disk hash table containing information about every
1218 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001219 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001220 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001221 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001222 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001223 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001224 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001225 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001226 }
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregorc5046832009-04-27 18:38:38 +00001228 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001229 SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001230 uint32_t BucketOffset;
1231 {
1232 llvm::raw_svector_ostream Out(StatCacheData);
1233 // Make sure that no bucket is at offset 0
1234 clang::io::Emit32(Out, 0);
1235 BucketOffset = Generator.Emit(Out);
1236 }
1237
1238 // Create a blob abbreviation
1239 using namespace llvm;
1240 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001241 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1245 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1246
1247 // Write the stat cache
1248 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001249 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001250 Record.push_back(BucketOffset);
1251 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001252 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001253}
1254
1255//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001256// Source Manager Serialization
1257//===----------------------------------------------------------------------===//
1258
1259/// \brief Create an abbreviation for the SLocEntry that refers to a
1260/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001261static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001262 using namespace llvm;
1263 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001264 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001269 // FileEntry fields.
1270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor9dc32122011-11-16 20:05:18 +00001272 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // BufferOverridden
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
Douglas Gregora7f71a92009-04-10 03:52:48 +00001276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001277 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001278}
1279
1280/// \brief Create an abbreviation for the SLocEntry that refers to a
1281/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001282static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001283 using namespace llvm;
1284 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001285 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1289 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001291 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001292}
1293
1294/// \brief Create an abbreviation for the SLocEntry that refers to a
1295/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001296static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001297 using namespace llvm;
1298 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001299 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001301 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001302}
1303
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001304/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1305/// expansion.
1306static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001307 using namespace llvm;
1308 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001309 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1312 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1313 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001314 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001315 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001316}
1317
Douglas Gregor09b69892011-02-10 17:09:37 +00001318namespace {
1319 // Trait used for the on-disk hash table of header search information.
1320 class HeaderFileInfoTrait {
1321 ASTWriter &Writer;
Douglas Gregor09b69892011-02-10 17:09:37 +00001322
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001323 // Keep track of the framework names we've used during serialization.
1324 SmallVector<char, 128> FrameworkStringData;
1325 llvm::StringMap<unsigned> FrameworkNameOffset;
1326
Douglas Gregor09b69892011-02-10 17:09:37 +00001327 public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001328 HeaderFileInfoTrait(ASTWriter &Writer)
1329 : Writer(Writer) { }
Douglas Gregor09b69892011-02-10 17:09:37 +00001330
1331 typedef const char *key_type;
1332 typedef key_type key_type_ref;
1333
1334 typedef HeaderFileInfo data_type;
1335 typedef const data_type &data_type_ref;
1336
1337 static unsigned ComputeHash(const char *path) {
1338 // The hash is based only on the filename portion of the key, so that the
1339 // reader can match based on filenames when symlinking or excess path
1340 // elements ("foo/../", "../") change the form of the name. However,
1341 // complete path is still the key.
1342 return llvm::HashString(llvm::sys::path::filename(path));
1343 }
1344
1345 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001346 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001347 data_type_ref Data) {
1348 unsigned StrLen = strlen(path);
1349 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001350 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001351 clang::io::Emit8(Out, DataLen);
1352 return std::make_pair(StrLen + 1, DataLen);
1353 }
1354
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001355 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001356 Out.write(path, KeyLen);
1357 }
1358
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001359 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001360 data_type_ref Data, unsigned DataLen) {
1361 using namespace clang::io;
1362 uint64_t Start = Out.tell(); (void)Start;
1363
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001364 unsigned char Flags = (Data.isImport << 5)
1365 | (Data.isPragmaOnce << 4)
1366 | (Data.DirInfo << 2)
1367 | (Data.Resolved << 1)
1368 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001369 Emit8(Out, (uint8_t)Flags);
1370 Emit16(Out, (uint16_t) Data.NumIncludes);
1371
1372 if (!Data.ControllingMacro)
1373 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1374 else
1375 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001376
1377 unsigned Offset = 0;
1378 if (!Data.Framework.empty()) {
1379 // If this header refers into a framework, save the framework name.
1380 llvm::StringMap<unsigned>::iterator Pos
1381 = FrameworkNameOffset.find(Data.Framework);
1382 if (Pos == FrameworkNameOffset.end()) {
1383 Offset = FrameworkStringData.size() + 1;
1384 FrameworkStringData.append(Data.Framework.begin(),
1385 Data.Framework.end());
1386 FrameworkStringData.push_back(0);
1387
1388 FrameworkNameOffset[Data.Framework] = Offset;
1389 } else
1390 Offset = Pos->second;
1391 }
1392 Emit32(Out, Offset);
1393
Douglas Gregor09b69892011-02-10 17:09:37 +00001394 assert(Out.tell() - Start == DataLen && "Wrong data length");
1395 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001396
1397 const char *strings_begin() const { return FrameworkStringData.begin(); }
1398 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001399 };
1400} // end anonymous namespace
1401
1402/// \brief Write the header search block for the list of files that
1403///
1404/// \param HS The header search structure to save.
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001405void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001406 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001407 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1408
1409 if (FilesByUID.size() > HS.header_file_size())
1410 FilesByUID.resize(HS.header_file_size());
1411
Benjamin Kramerd1d76b22012-06-06 17:32:50 +00001412 HeaderFileInfoTrait GeneratorTrait(*this);
Douglas Gregor09b69892011-02-10 17:09:37 +00001413 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001414 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001415 unsigned NumHeaderSearchEntries = 0;
1416 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1417 const FileEntry *File = FilesByUID[UID];
1418 if (!File)
1419 continue;
1420
Argyrios Kyrtzidisf5ab0342011-11-13 22:08:39 +00001421 // Use HeaderSearch's getFileInfo to make sure we get the HeaderFileInfo
1422 // from the external source if it was not provided already.
1423 const HeaderFileInfo &HFI = HS.getFileInfo(File);
Douglas Gregor09b69892011-02-10 17:09:37 +00001424 if (HFI.External && Chain)
1425 continue;
1426
1427 // Turn the file name into an absolute path, if it isn't already.
1428 const char *Filename = File->getName();
1429 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1430
1431 // If we performed any translation on the file name at all, we need to
1432 // save this string, since the generator will refer to it later.
1433 if (Filename != File->getName()) {
1434 Filename = strdup(Filename);
1435 SavedStrings.push_back(Filename);
1436 }
1437
1438 Generator.insert(Filename, HFI, GeneratorTrait);
1439 ++NumHeaderSearchEntries;
1440 }
1441
1442 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001443 SmallString<4096> TableData;
Douglas Gregor09b69892011-02-10 17:09:37 +00001444 uint32_t BucketOffset;
1445 {
1446 llvm::raw_svector_ostream Out(TableData);
1447 // Make sure that no bucket is at offset 0
1448 clang::io::Emit32(Out, 0);
1449 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1450 }
1451
1452 // Create a blob abbreviation
1453 using namespace llvm;
1454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1455 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1460 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1461
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001462 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001463 RecordData Record;
1464 Record.push_back(HEADER_SEARCH_TABLE);
1465 Record.push_back(BucketOffset);
1466 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001467 Record.push_back(TableData.size());
1468 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001469 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1470
1471 // Free all of the strings we had to duplicate.
1472 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1473 free((void*)SavedStrings[I]);
1474}
1475
Douglas Gregora7f71a92009-04-10 03:52:48 +00001476/// \brief Writes the block containing the serialized form of the
1477/// source manager.
1478///
1479/// TODO: We should probably use an on-disk hash table (stored in a
1480/// blob), indexed based on the file name, so that we only create
1481/// entries for files that we actually need. In the common case (no
1482/// errors), we probably won't have to create file entries for any of
1483/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001484void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001485 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001486 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001487 RecordData Record;
1488
Chris Lattner0910e3b2009-04-10 17:16:57 +00001489 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001490 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001491
1492 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001493 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1494 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1495 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001496 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001497
Douglas Gregor258ae542009-04-27 06:38:32 +00001498 // Write out the source location entry table. We skip the first
1499 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001500 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001501 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001502 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1503 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001504 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001505 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001506 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001507 FileID FID = FileID::get(I);
1508 assert(&SourceMgr.getSLocEntry(FID) == SLoc);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001509
Douglas Gregor258ae542009-04-27 06:38:32 +00001510 // Record the offset of this source-location entry.
1511 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1512
1513 // Figure out which record code to use.
1514 unsigned Code;
1515 if (SLoc->isFile()) {
Douglas Gregor9dc32122011-11-16 20:05:18 +00001516 const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1517 if (Cache->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001518 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001519 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001520 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001521 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001522 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001523 Record.clear();
1524 Record.push_back(Code);
1525
Douglas Gregor925296b2011-07-19 16:10:42 +00001526 // Starting offset of this entry within this module, so skip the dummy.
1527 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001528 if (SLoc->isFile()) {
1529 const SrcMgr::FileInfo &File = SLoc->getFile();
1530 Record.push_back(File.getIncludeLoc().getRawEncoding());
1531 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1532 Record.push_back(File.hasLineDirectives());
1533
1534 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001535 if (Content->OrigEntry) {
1536 assert(Content->OrigEntry == Content->ContentsEntry &&
Douglas Gregor9dc32122011-11-16 20:05:18 +00001537 "Writing to AST an overridden file is not supported");
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001538
Douglas Gregor258ae542009-04-27 06:38:32 +00001539 // The source location entry is a file. The blob associated
1540 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001541
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001542 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001543 Record.push_back(Content->OrigEntry->getSize());
1544 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor9dc32122011-11-16 20:05:18 +00001545 Record.push_back(Content->BufferOverridden);
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001546 Record.push_back(File.NumCreatedFIDs);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001547
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00001548 FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00001549 if (FDI != FileDeclIDs.end()) {
1550 Record.push_back(FDI->second->FirstDeclIndex);
1551 Record.push_back(FDI->second->DeclIDs.size());
1552 } else {
1553 Record.push_back(0);
1554 Record.push_back(0);
1555 }
Douglas Gregor9dc32122011-11-16 20:05:18 +00001556
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001557 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001558 const char *Filename = Content->OrigEntry->getName();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001559 SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001560
1561 // Ask the file manager to fixup the relative path for us. This will
1562 // honor the working directory.
1563 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1564
1565 // FIXME: This call to make_absolute shouldn't be necessary, the
1566 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001567 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001568 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001569
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001570 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001571 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor9dc32122011-11-16 20:05:18 +00001572
1573 if (Content->BufferOverridden) {
1574 Record.clear();
1575 Record.push_back(SM_SLOC_BUFFER_BLOB);
1576 const llvm::MemoryBuffer *Buffer
1577 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1578 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1579 StringRef(Buffer->getBufferStart(),
1580 Buffer->getBufferSize() + 1));
1581 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001582 } else {
1583 // The source location entry is a buffer. The blob associated
1584 // with this entry contains the contents of the buffer.
1585
1586 // We add one to the size so that we capture the trailing NULL
1587 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1588 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001589 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001590 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001591 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001592 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001593 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001594 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001595 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001596 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001597 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001598 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001599
Douglas Gregor925296b2011-07-19 16:10:42 +00001600 if (strcmp(Name, "<built-in>") == 0) {
1601 PreloadSLocs.push_back(SLocEntryOffsets.size());
1602 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001603 }
1604 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001605 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001606 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001607 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1608 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001609 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1610 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001611
1612 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001613 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001614 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001615 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001616 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001617 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001618 }
1619 }
1620
Douglas Gregor8f45df52009-04-16 22:23:12 +00001621 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001622
1623 if (SLocEntryOffsets.empty())
1624 return;
1625
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001626 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001627 // table is used for lazily loading source-location information.
1628 using namespace llvm;
1629 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001630 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001631 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001632 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001633 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1634 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001635
Douglas Gregor258ae542009-04-27 06:38:32 +00001636 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001637 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001638 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001639 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001640 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001641
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001642 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001643 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001644 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001645
1646 // Write the line table. It depends on remapping working, so it must come
1647 // after the source location offsets.
1648 if (SourceMgr.hasLineTable()) {
1649 LineTableInfo &LineTable = SourceMgr.getLineTable();
1650
1651 Record.clear();
1652 // Emit the file names
1653 Record.push_back(LineTable.getNumFilenames());
1654 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1655 // Emit the file name
1656 const char *Filename = LineTable.getFilename(I);
1657 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1658 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1659 Record.push_back(FilenameLen);
1660 if (FilenameLen)
1661 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1662 }
1663
1664 // Emit the line entries
1665 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1666 L != LEnd; ++L) {
1667 // Only emit entries for local files.
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001668 if (L->first.ID < 0)
Douglas Gregor925296b2011-07-19 16:10:42 +00001669 continue;
1670
1671 // Emit the file ID
Douglas Gregor02c2dbf2012-06-08 16:40:28 +00001672 Record.push_back(L->first.ID);
Douglas Gregor925296b2011-07-19 16:10:42 +00001673
1674 // Emit the line entries
1675 Record.push_back(L->second.size());
1676 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1677 LEEnd = L->second.end();
1678 LE != LEEnd; ++LE) {
1679 Record.push_back(LE->FileOffset);
1680 Record.push_back(LE->LineNo);
1681 Record.push_back(LE->FilenameID);
1682 Record.push_back((unsigned)LE->FileKind);
1683 Record.push_back(LE->IncludeOffset);
1684 }
1685 }
1686 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1687 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001688}
1689
Douglas Gregorc5046832009-04-27 18:38:38 +00001690//===----------------------------------------------------------------------===//
1691// Preprocessor Serialization
1692//===----------------------------------------------------------------------===//
1693
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001694static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1695 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1696 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1697 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1698 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1699 return X.first->getName().compare(Y.first->getName());
1700}
1701
Chris Lattnereeffaef2009-04-10 17:15:23 +00001702/// \brief Writes the block containing the serialized form of the
1703/// preprocessor.
1704///
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001705void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001706 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
1707 if (PPRec)
1708 WritePreprocessorDetail(*PPRec);
1709
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001710 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001711
Chris Lattner0af3ba12009-04-13 01:29:17 +00001712 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1713 if (PP.getCounterValue() != 0) {
1714 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001715 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001716 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001717 }
1718
1719 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001720 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001721
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001722 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001723 // FIXME: use diagnostics subsystem for localization etc.
1724 if (PP.SawDateOrTime())
1725 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001726
Douglas Gregor796d76a2010-10-20 22:00:55 +00001727
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001728 // Loop over all the macro definitions that are live at the end of the file,
1729 // emitting each to the PP section.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001730
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001731 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001732 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001733 MacrosToEmit;
1734 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001735 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
Douglas Gregor68051a72011-02-11 00:26:14 +00001736 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001737 I != E; ++I) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001738 if (!IsModule || I->second->isPublic()) {
1739 MacroDefinitionsSeen.insert(I->first);
1740 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00001741 }
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001742 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001743
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001744 // Sort the set of macro definitions that need to be serialized by the
1745 // name of the macro, to provide a stable ordering.
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001746 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001747 &compareMacroDefinitions);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001748
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001749 /// \brief Offsets of each of the macros into the bitstream, indexed by
1750 /// the local macro ID
1751 ///
1752 /// For each identifier that is associated with a macro, this map
1753 /// provides the offset into the bitstream where that macro is
1754 /// defined.
1755 std::vector<uint32_t> MacroOffsets;
1756
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001757 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1758 const IdentifierInfo *Name = MacrosToEmit[I].first;
Douglas Gregoreb114da2010-10-01 01:03:07 +00001759
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001760 for (MacroInfo *MI = MacrosToEmit[I].second; MI;
1761 MI = MI->getPreviousDefinition()) {
1762 MacroID ID = getMacroRef(MI);
1763 if (!ID)
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001764 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001765
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001766 // Skip macros from a AST file if we're chaining.
1767 if (Chain && MI->isFromAST() && !MI->hasChangedAfterLoad())
1768 continue;
1769
1770 if (ID < FirstMacroID) {
1771 // This will have been dealt with via an update record.
1772 assert(MacroUpdates.count(MI) > 0 && "Missing macro update");
1773 continue;
1774 }
1775
1776 // Record the local offset of this macro.
1777 unsigned Index = ID - FirstMacroID;
1778 if (Index == MacroOffsets.size())
1779 MacroOffsets.push_back(Stream.GetCurrentBitNo());
1780 else {
1781 if (Index > MacroOffsets.size())
1782 MacroOffsets.resize(Index + 1);
1783
1784 MacroOffsets[Index] = Stream.GetCurrentBitNo();
1785 }
1786
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001787 AddIdentifierRef(Name, Record);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001788 addMacroRef(MI, Record);
Douglas Gregor5a4649b2012-10-11 00:46:49 +00001789 Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001790 AddSourceLocation(MI->getDefinitionLoc(), Record);
1791 AddSourceLocation(MI->getUndefLoc(), Record);
1792 Record.push_back(MI->isUsed());
1793 Record.push_back(MI->isPublic());
1794 AddSourceLocation(MI->getVisibilityLocation(), Record);
1795 unsigned Code;
1796 if (MI->isObjectLike()) {
1797 Code = PP_MACRO_OBJECT_LIKE;
1798 } else {
1799 Code = PP_MACRO_FUNCTION_LIKE;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001800
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001801 Record.push_back(MI->isC99Varargs());
1802 Record.push_back(MI->isGNUVarargs());
1803 Record.push_back(MI->getNumArgs());
1804 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1805 I != E; ++I)
1806 AddIdentifierRef(*I, Record);
1807 }
Mike Stump11289f42009-09-09 15:08:12 +00001808
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001809 // If we have a detailed preprocessing record, record the macro definition
1810 // ID that corresponds to this macro.
1811 if (PPRec)
1812 Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
1813
1814 Stream.EmitRecord(Code, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001815 Record.clear();
Alexander Kornienko1d26c022012-09-25 17:18:14 +00001816
1817 // Emit the tokens array.
1818 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1819 // Note that we know that the preprocessor does not have any annotation
1820 // tokens in it because they are created by the parser, and thus can't
1821 // be in a macro definition.
1822 const Token &Tok = MI->getReplacementToken(TokNo);
1823
1824 Record.push_back(Tok.getLocation().getRawEncoding());
1825 Record.push_back(Tok.getLength());
1826
1827 // FIXME: When reading literal tokens, reconstruct the literal pointer
1828 // if it is needed.
1829 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
1830 // FIXME: Should translate token kind to a stable encoding.
1831 Record.push_back(Tok.getKind());
1832 // FIXME: Should translate token flags to a stable encoding.
1833 Record.push_back(Tok.getFlags());
1834
1835 Stream.EmitRecord(PP_TOKEN, Record);
1836 Record.clear();
1837 }
1838 ++NumMacros;
Chris Lattner2199f5b2009-04-10 18:08:30 +00001839 }
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001840 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001841 Stream.ExitBlock();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00001842
1843 // Write the offsets table for macro IDs.
1844 using namespace llvm;
1845 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1846 Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
1847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
1848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
1849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1850
1851 unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1852 Record.clear();
1853 Record.push_back(MACRO_OFFSET);
1854 Record.push_back(MacroOffsets.size());
1855 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
1856 Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record,
1857 data(MacroOffsets));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001858}
1859
1860void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001861 if (PPRec.local_begin() == PPRec.local_end())
Douglas Gregor92a96f52011-02-08 21:58:10 +00001862 return;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001863
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001864 SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001865
Douglas Gregor92a96f52011-02-08 21:58:10 +00001866 // Enter the preprocessor block.
1867 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001868
Douglas Gregoraae92242010-03-19 21:51:54 +00001869 // If the preprocessor has a preprocessing record, emit it.
1870 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001871 using namespace llvm;
1872
1873 // Set up the abbreviation for
1874 unsigned InclusionAbbrev = 0;
1875 {
1876 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1877 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
Douglas Gregor92a96f52011-02-08 21:58:10 +00001878 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
Douglas Gregor92a96f52011-02-08 21:58:10 +00001882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1883 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1884 }
1885
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001886 unsigned FirstPreprocessorEntityID
1887 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1888 + NUM_PREDEF_PP_ENTITY_IDS;
1889 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001890 RecordData Record;
Argyrios Kyrtzidis7f448362011-09-19 20:40:42 +00001891 for (PreprocessingRecord::iterator E = PPRec.local_begin(),
1892 EEnd = PPRec.local_end();
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001893 E != EEnd;
1894 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001895 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001896
Argyrios Kyrtzidis64f63812011-09-19 20:40:25 +00001897 PreprocessedEntityOffsets.push_back(PPEntityOffset((*E)->getSourceRange(),
1898 Stream.GetCurrentBitNo()));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001899
Douglas Gregor92a96f52011-02-08 21:58:10 +00001900 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001901 // Record this macro definition's ID.
1902 MacroDefinitions[MD] = NextPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001903
Douglas Gregor92a96f52011-02-08 21:58:10 +00001904 AddIdentifierRef(MD->getName(), Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001905 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1906 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001907 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001908
Chandler Carrutha88a22182011-07-14 08:20:46 +00001909 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Argyrios Kyrtzidis80f78b92011-09-08 17:18:41 +00001910 Record.push_back(ME->isBuiltinMacro());
1911 if (ME->isBuiltinMacro())
1912 AddIdentifierRef(ME->getName(), Record);
1913 else
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001914 Record.push_back(MacroDefinitions[ME->getDefinition()]);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001915 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001916 continue;
1917 }
1918
1919 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1920 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001921 Record.push_back(ID->getFileName().size());
1922 Record.push_back(ID->wasInQuotes());
1923 Record.push_back(static_cast<unsigned>(ID->getKind()));
Argyrios Kyrtzidisf590e092012-10-02 16:10:46 +00001924 Record.push_back(ID->importedModule());
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001925 SmallString<64> Buffer;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001926 Buffer += ID->getFileName();
Argyrios Kyrtzidis8dbcfc32012-03-08 01:08:28 +00001927 // Check that the FileEntry is not null because it was not resolved and
1928 // we create a PCH even with compiler errors.
1929 if (ID->getFile())
1930 Buffer += ID->getFile()->getName();
Douglas Gregor92a96f52011-02-08 21:58:10 +00001931 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1932 continue;
1933 }
1934
1935 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1936 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001937 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001938
Douglas Gregoraae92242010-03-19 21:51:54 +00001939 // Write the offsets table for the preprocessing record.
1940 if (NumPreprocessingRecords > 0) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001941 assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
1942
Douglas Gregoraae92242010-03-19 21:51:54 +00001943 // Write the offsets table for identifier IDs.
1944 using namespace llvm;
1945 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001946 Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001949 unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001950
Douglas Gregoraae92242010-03-19 21:51:54 +00001951 Record.clear();
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001952 Record.push_back(PPD_ENTITIES_OFFSETS);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001953 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00001954 Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
1955 data(PreprocessedEntityOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001956 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001957}
1958
Douglas Gregora89c5ac2011-12-06 01:10:29 +00001959unsigned ASTWriter::getSubmoduleID(Module *Mod) {
1960 llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
1961 if (Known != SubmoduleIDs.end())
1962 return Known->second;
1963
1964 return SubmoduleIDs[Mod] = NextSubmoduleID++;
1965}
1966
Douglas Gregor253eefe2011-12-01 00:59:36 +00001967/// \brief Compute the number of modules within the given tree (including the
1968/// given module).
1969static unsigned getNumberOfModules(Module *Mod) {
1970 unsigned ChildModules = 0;
Douglas Gregoreb90e832012-01-04 23:32:19 +00001971 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
1972 SubEnd = Mod->submodule_end();
Douglas Gregor253eefe2011-12-01 00:59:36 +00001973 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00001974 ChildModules += getNumberOfModules(*Sub);
Douglas Gregor253eefe2011-12-01 00:59:36 +00001975
1976 return ChildModules + 1;
1977}
1978
Douglas Gregorde3ef502011-11-30 23:21:26 +00001979void ASTWriter::WriteSubmodules(Module *WritingModule) {
Douglas Gregor60382512011-12-05 16:35:23 +00001980 // Determine the dependencies of our module and each of it's submodules.
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001981 // FIXME: This feels like it belongs somewhere else, but there are no
1982 // other consumers of this information.
1983 SourceManager &SrcMgr = PP->getSourceManager();
1984 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
1985 for (ASTContext::import_iterator I = Context->local_import_begin(),
1986 IEnd = Context->local_import_end();
1987 I != IEnd; ++I) {
Douglas Gregor0093b3c2011-12-05 16:33:54 +00001988 if (Module *ImportedFrom
1989 = ModMap.inferModuleFromLocation(FullSourceLoc(I->getLocation(),
1990 SrcMgr))) {
1991 ImportedFrom->Imports.push_back(I->getImportedModule());
1992 }
1993 }
1994
Douglas Gregor69021972011-11-30 17:33:56 +00001995 // Enter the submodule description block.
1996 Stream.EnterSubblock(SUBMODULE_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
1997
1998 // Write the abbreviations needed for the submodules block.
1999 using namespace llvm;
2000 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2001 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
Douglas Gregor69021972011-11-30 17:33:56 +00002003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
Douglas Gregora686e1b2012-01-27 19:52:33 +00002006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
Douglas Gregor73441092011-12-05 22:27:44 +00002008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
Douglas Gregor73441092011-12-05 22:27:44 +00002009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
Douglas Gregor69021972011-11-30 17:33:56 +00002010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2011 unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2012
2013 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002014 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
Douglas Gregor69021972011-11-30 17:33:56 +00002015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2016 unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2017
2018 Abbrev = new BitCodeAbbrev();
2019 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2021 unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor524e33e2011-12-08 19:11:24 +00002022
2023 Abbrev = new BitCodeAbbrev();
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002024 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2026 unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2027
2028 Abbrev = new BitCodeAbbrev();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002029 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2031 unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2032
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002033 Abbrev = new BitCodeAbbrev();
2034 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Feature
2036 unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2037
Douglas Gregor59527662012-10-15 06:28:11 +00002038 Abbrev = new BitCodeAbbrev();
2039 Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2041 unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2042
Douglas Gregor253eefe2011-12-01 00:59:36 +00002043 // Write the submodule metadata block.
2044 RecordData Record;
2045 Record.push_back(getNumberOfModules(WritingModule));
2046 Record.push_back(FirstSubmoduleID - NUM_PREDEF_SUBMODULE_IDS);
2047 Stream.EmitRecord(SUBMODULE_METADATA, Record);
2048
Douglas Gregor69021972011-11-30 17:33:56 +00002049 // Write all of the submodules.
Douglas Gregorde3ef502011-11-30 23:21:26 +00002050 std::queue<Module *> Q;
Douglas Gregor69021972011-11-30 17:33:56 +00002051 Q.push(WritingModule);
Douglas Gregor69021972011-11-30 17:33:56 +00002052 while (!Q.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00002053 Module *Mod = Q.front();
Douglas Gregor69021972011-11-30 17:33:56 +00002054 Q.pop();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002055 unsigned ID = getSubmoduleID(Mod);
Douglas Gregor69021972011-11-30 17:33:56 +00002056
2057 // Emit the definition of the block.
2058 Record.clear();
2059 Record.push_back(SUBMODULE_DEFINITION);
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002060 Record.push_back(ID);
Douglas Gregor69021972011-11-30 17:33:56 +00002061 if (Mod->Parent) {
2062 assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2063 Record.push_back(SubmoduleIDs[Mod->Parent]);
2064 } else {
2065 Record.push_back(0);
2066 }
2067 Record.push_back(Mod->IsFramework);
2068 Record.push_back(Mod->IsExplicit);
Douglas Gregora686e1b2012-01-27 19:52:33 +00002069 Record.push_back(Mod->IsSystem);
Douglas Gregor73441092011-12-05 22:27:44 +00002070 Record.push_back(Mod->InferSubmodules);
2071 Record.push_back(Mod->InferExplicitSubmodules);
2072 Record.push_back(Mod->InferExportWildcard);
Douglas Gregor69021972011-11-30 17:33:56 +00002073 Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2074
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +00002075 // Emit the requirements.
2076 for (unsigned I = 0, N = Mod->Requires.size(); I != N; ++I) {
2077 Record.clear();
2078 Record.push_back(SUBMODULE_REQUIRES);
2079 Stream.EmitRecordWithBlob(RequiresAbbrev, Record,
2080 Mod->Requires[I].data(),
2081 Mod->Requires[I].size());
2082 }
2083
Douglas Gregor69021972011-11-30 17:33:56 +00002084 // Emit the umbrella header, if there is one.
Douglas Gregor73141fa2011-12-08 17:39:04 +00002085 if (const FileEntry *UmbrellaHeader = Mod->getUmbrellaHeader()) {
Douglas Gregor69021972011-11-30 17:33:56 +00002086 Record.clear();
Douglas Gregor524e33e2011-12-08 19:11:24 +00002087 Record.push_back(SUBMODULE_UMBRELLA_HEADER);
Douglas Gregor69021972011-11-30 17:33:56 +00002088 Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
Douglas Gregor73141fa2011-12-08 17:39:04 +00002089 UmbrellaHeader->getName());
Douglas Gregor524e33e2011-12-08 19:11:24 +00002090 } else if (const DirectoryEntry *UmbrellaDir = Mod->getUmbrellaDir()) {
2091 Record.clear();
2092 Record.push_back(SUBMODULE_UMBRELLA_DIR);
2093 Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2094 UmbrellaDir->getName());
Douglas Gregor69021972011-11-30 17:33:56 +00002095 }
2096
2097 // Emit the headers.
2098 for (unsigned I = 0, N = Mod->Headers.size(); I != N; ++I) {
2099 Record.clear();
2100 Record.push_back(SUBMODULE_HEADER);
2101 Stream.EmitRecordWithBlob(HeaderAbbrev, Record,
2102 Mod->Headers[I]->getName());
2103 }
Douglas Gregor59527662012-10-15 06:28:11 +00002104 // Emit the excluded headers.
2105 for (unsigned I = 0, N = Mod->ExcludedHeaders.size(); I != N; ++I) {
2106 Record.clear();
2107 Record.push_back(SUBMODULE_EXCLUDED_HEADER);
2108 Stream.EmitRecordWithBlob(ExcludedHeaderAbbrev, Record,
2109 Mod->ExcludedHeaders[I]->getName());
2110 }
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +00002111 for (unsigned I = 0, N = Mod->TopHeaders.size(); I != N; ++I) {
2112 Record.clear();
2113 Record.push_back(SUBMODULE_TOPHEADER);
2114 Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record,
2115 Mod->TopHeaders[I]->getName());
2116 }
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002117
2118 // Emit the imports.
2119 if (!Mod->Imports.empty()) {
2120 Record.clear();
2121 for (unsigned I = 0, N = Mod->Imports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002122 unsigned ImportedID = getSubmoduleID(Mod->Imports[I]);
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002123 assert(ImportedID && "Unknown submodule!");
2124 Record.push_back(ImportedID);
2125 }
2126 Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2127 }
2128
Douglas Gregor24bb9232011-12-02 18:58:38 +00002129 // Emit the exports.
2130 if (!Mod->Exports.empty()) {
2131 Record.clear();
2132 for (unsigned I = 0, N = Mod->Exports.size(); I != N; ++I) {
Douglas Gregor18b58642011-12-12 23:17:57 +00002133 if (Module *Exported = Mod->Exports[I].getPointer()) {
2134 unsigned ExportedID = SubmoduleIDs[Exported];
2135 assert(ExportedID > 0 && "Unknown submodule ID?");
2136 Record.push_back(ExportedID);
2137 } else {
2138 Record.push_back(0);
2139 }
2140
Douglas Gregor24bb9232011-12-02 18:58:38 +00002141 Record.push_back(Mod->Exports[I].getInt());
2142 }
2143 Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2144 }
2145
Douglas Gregor69021972011-11-30 17:33:56 +00002146 // Queue up the submodules of this module.
Douglas Gregoreb90e832012-01-04 23:32:19 +00002147 for (Module::submodule_iterator Sub = Mod->submodule_begin(),
2148 SubEnd = Mod->submodule_end();
Douglas Gregor69021972011-11-30 17:33:56 +00002149 Sub != SubEnd; ++Sub)
Douglas Gregoreb90e832012-01-04 23:32:19 +00002150 Q.push(*Sub);
Douglas Gregor69021972011-11-30 17:33:56 +00002151 }
2152
2153 Stream.ExitBlock();
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002154
2155 assert((NextSubmoduleID - FirstSubmoduleID
2156 == getNumberOfModules(WritingModule)) && "Wrong # of submodules");
Douglas Gregor69021972011-11-30 17:33:56 +00002157}
2158
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002159serialization::SubmoduleID
2160ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002161 if (Loc.isInvalid() || !WritingModule)
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002162 return 0; // No submodule
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002163
2164 // Find the module that owns this location.
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002165 ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
Douglas Gregor0093b3c2011-12-05 16:33:54 +00002166 Module *OwningMod
2167 = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002168 if (!OwningMod)
2169 return 0;
2170
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002171 // Check whether this submodule is part of our own module.
2172 if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002173 return 0;
2174
Douglas Gregora89c5ac2011-12-06 01:10:29 +00002175 return getSubmoduleID(OwningMod);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00002176}
2177
David Blaikie9c902b52011-09-25 23:23:43 +00002178void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002179 RecordData Record;
David Blaikie9c902b52011-09-25 23:23:43 +00002180 for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002181 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2182 I != E; ++I) {
David Blaikie9c902b52011-09-25 23:23:43 +00002183 const DiagnosticsEngine::DiagStatePoint &point = *I;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002184 if (point.Loc.isInvalid())
2185 continue;
2186
2187 Record.push_back(point.Loc.getRawEncoding());
Daniel Dunbare8c12a22011-09-29 01:42:25 +00002188 for (DiagnosticsEngine::DiagState::const_iterator
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002189 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
Daniel Dunbara3637e62011-09-29 01:30:00 +00002190 if (I->second.isPragma()) {
2191 Record.push_back(I->first);
2192 Record.push_back(I->second.getMapping());
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002193 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002194 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002195 Record.push_back(-1); // mark the end of the diag/map pairs for this
2196 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002197 }
2198
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00002199 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002200 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002201}
2202
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002203void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2204 if (CXXBaseSpecifiersOffsets.empty())
2205 return;
2206
2207 RecordData Record;
2208
2209 // Create a blob abbreviation for the C++ base specifiers offsets.
2210 using namespace llvm;
2211
2212 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2213 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2216 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2217
Douglas Gregorc27b2872011-08-04 00:01:48 +00002218 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002219 Record.clear();
2220 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2221 Record.push_back(CXXBaseSpecifiersOffsets.size());
2222 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002223 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002224}
2225
Douglas Gregorc5046832009-04-27 18:38:38 +00002226//===----------------------------------------------------------------------===//
2227// Type Serialization
2228//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00002229
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002230/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002231void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002232 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002233 if (Idx.getIndex() == 0) // we haven't seen this type before.
2234 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00002235
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002236 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00002237
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002238 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002239 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002240 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00002241 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002242 else if (TypeOffsets.size() < Index) {
2243 TypeOffsets.resize(Index + 1);
2244 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002245 }
2246
2247 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00002248
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002249 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002250 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002251
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00002252 if (T.hasLocalNonFastQualifiers()) {
2253 Qualifiers Qs = T.getLocalQualifiers();
2254 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00002255 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002256 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002257 } else {
2258 switch (T->getTypeClass()) {
2259 // For all of the concrete, non-dependent types, call the
2260 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002261#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002262 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002263#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002264#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002265 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002266 }
2267
2268 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002269 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002270
2271 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002272 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002273}
2274
Douglas Gregorc5046832009-04-27 18:38:38 +00002275//===----------------------------------------------------------------------===//
2276// Declaration Serialization
2277//===----------------------------------------------------------------------===//
2278
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002279/// \brief Write the block containing all of the declaration IDs
2280/// lexically declared within the given DeclContext.
2281///
2282/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2283/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002284uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002285 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002286 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002287 return 0;
2288
Douglas Gregor8f45df52009-04-16 22:23:12 +00002289 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002290 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002291 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002292 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002293 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2294 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002295 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002296
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002297 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002298 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002299 return Offset;
2300}
2301
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002302void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002303 using namespace llvm;
2304 RecordData Record;
2305
2306 // Write the type offsets array
2307 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002308 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002309 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002310 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002311 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2312 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2313 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002314 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002315 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002316 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002317 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002318
2319 // Write the declaration offsets array
2320 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002321 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002324 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2325 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2326 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002327 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002328 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002329 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002330 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002331}
2332
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002333void ASTWriter::WriteFileDeclIDsMap() {
2334 using namespace llvm;
2335 RecordData Record;
2336
2337 // Join the vectors of DeclIDs from all files.
2338 SmallVector<DeclID, 256> FileSortedIDs;
2339 for (FileDeclIDsTy::iterator
2340 FI = FileDeclIDs.begin(), FE = FileDeclIDs.end(); FI != FE; ++FI) {
2341 DeclIDInFileInfo &Info = *FI->second;
2342 Info.FirstDeclIndex = FileSortedIDs.size();
2343 for (LocDeclIDsTy::iterator
2344 DI = Info.DeclIDs.begin(), DE = Info.DeclIDs.end(); DI != DE; ++DI)
2345 FileSortedIDs.push_back(DI->second);
2346 }
2347
2348 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2349 Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002350 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002351 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2352 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2353 Record.push_back(FILE_SORTED_DECLS);
Argyrios Kyrtzidis10e78462012-10-02 21:09:13 +00002354 Record.push_back(FileSortedIDs.size());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00002355 Stream.EmitRecordWithBlob(AbbrevCode, Record, data(FileSortedIDs));
2356}
2357
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002358void ASTWriter::WriteComments() {
2359 Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002360 ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002361 RecordData Record;
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002362 for (ArrayRef<RawComment *>::iterator I = RawComments.begin(),
2363 E = RawComments.end();
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002364 I != E; ++I) {
2365 Record.clear();
Dmitri Gribenko7dd29d42012-07-06 18:19:34 +00002366 AddSourceRange((*I)->getSourceRange(), Record);
2367 Record.push_back((*I)->getKind());
2368 Record.push_back((*I)->isTrailingComment());
2369 Record.push_back((*I)->isAlmostTrailingComment());
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00002370 Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2371 }
2372 Stream.ExitBlock();
2373}
2374
Douglas Gregorc5046832009-04-27 18:38:38 +00002375//===----------------------------------------------------------------------===//
2376// Global Method Pool and Selector Serialization
2377//===----------------------------------------------------------------------===//
2378
Douglas Gregore84a9da2009-04-20 20:36:09 +00002379namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002380// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002381class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002382 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002383
2384public:
2385 typedef Selector key_type;
2386 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002387
Sebastian Redl834bb972010-08-04 17:20:04 +00002388 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002389 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002390 ObjCMethodList Instance, Factory;
2391 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002392 typedef const data_type& data_type_ref;
2393
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002394 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002395
Douglas Gregorc78d3462009-04-24 21:10:55 +00002396 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002397 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002398 }
Mike Stump11289f42009-09-09 15:08:12 +00002399
2400 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002401 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002402 data_type_ref Methods) {
2403 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2404 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002405 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2406 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002407 Method = Method->Next)
2408 if (Method->Method)
2409 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002410 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002411 Method = Method->Next)
2412 if (Method->Method)
2413 DataLen += 4;
2414 clang::io::Emit16(Out, DataLen);
2415 return std::make_pair(KeyLen, DataLen);
2416 }
Mike Stump11289f42009-09-09 15:08:12 +00002417
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002418 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002419 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002420 assert((Start >> 32) == 0 && "Selector key offset too large");
2421 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002422 unsigned N = Sel.getNumArgs();
2423 clang::io::Emit16(Out, N);
2424 if (N == 0)
2425 N = 1;
2426 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002427 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002428 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2429 }
Mike Stump11289f42009-09-09 15:08:12 +00002430
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002431 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002432 data_type_ref Methods, unsigned DataLen) {
2433 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002434 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002435 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002436 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002437 Method = Method->Next)
2438 if (Method->Method)
2439 ++NumInstanceMethods;
2440
2441 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002442 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002443 Method = Method->Next)
2444 if (Method->Method)
2445 ++NumFactoryMethods;
2446
2447 clang::io::Emit16(Out, NumInstanceMethods);
2448 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002449 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002450 Method = Method->Next)
2451 if (Method->Method)
2452 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002453 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002454 Method = Method->Next)
2455 if (Method->Method)
2456 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002457
2458 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002459 }
2460};
2461} // end anonymous namespace
2462
Sebastian Redla19a67f2010-08-03 21:58:15 +00002463/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002464///
2465/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002466/// in an on-disk hash table indexed by the selector. The hash table also
2467/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002468void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002469 using namespace llvm;
2470
Sebastian Redla19a67f2010-08-03 21:58:15 +00002471 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002472 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002473 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002474 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002475 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002476 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002477 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002478 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002479
Sebastian Redla19a67f2010-08-03 21:58:15 +00002480 // Create the on-disk hash table representation. We walk through every
2481 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002482 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002483 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002484 I = SelectorIDs.begin(), E = SelectorIDs.end();
2485 I != E; ++I) {
2486 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002487 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002488 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002489 I->second,
2490 ObjCMethodList(),
2491 ObjCMethodList()
2492 };
2493 if (F != SemaRef.MethodPool.end()) {
2494 Data.Instance = F->second.first;
2495 Data.Factory = F->second.second;
2496 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002497 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002498 // changed.
2499 if (Chain && I->second < FirstSelectorID) {
2500 // Selector already exists. Did it change?
2501 bool changed = false;
2502 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2503 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002504 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002505 changed = true;
2506 }
2507 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2508 M = M->Next) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00002509 if (!M->Method->isFromASTFile())
Sebastian Redld95a56e2010-08-04 18:21:41 +00002510 changed = true;
2511 }
2512 if (!changed)
2513 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002514 } else if (Data.Instance.Method || Data.Factory.Method) {
2515 // A new method pool entry.
2516 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002517 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002518 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002519 }
2520
Douglas Gregorc78d3462009-04-24 21:10:55 +00002521 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002522 SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002523 uint32_t BucketOffset;
2524 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002525 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002526 llvm::raw_svector_ostream Out(MethodPool);
2527 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002528 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002529 BucketOffset = Generator.Emit(Out, Trait);
2530 }
2531
2532 // Create a blob abbreviation
2533 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002534 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002535 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2538 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2539
Douglas Gregor95c13f52009-04-25 17:48:32 +00002540 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002541 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002542 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002543 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002544 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002545 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002546
2547 // Create a blob abbreviation for the selector table offsets.
2548 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002549 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002552 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2553 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2554
2555 // Write the selector offsets table.
2556 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002557 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002558 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002559 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002560 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002561 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002562 }
2563}
2564
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002565/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002566void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002567 using namespace llvm;
2568 if (SemaRef.ReferencedSelectors.empty())
2569 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002570
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002571 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002572
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002573 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002574 // very tricky to fix, and given that @selector shouldn't really appear in
2575 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002576 for (DenseMap<Selector, SourceLocation>::iterator S =
2577 SemaRef.ReferencedSelectors.begin(),
2578 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2579 Selector Sel = (*S).first;
2580 SourceLocation Loc = (*S).second;
2581 AddSelectorRef(Sel, Record);
2582 AddSourceLocation(Loc, Record);
2583 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002584 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002585}
2586
Douglas Gregorc5046832009-04-27 18:38:38 +00002587//===----------------------------------------------------------------------===//
2588// Identifier Table Serialization
2589//===----------------------------------------------------------------------===//
2590
Douglas Gregorc78d3462009-04-24 21:10:55 +00002591namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002592class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002593 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002594 Preprocessor &PP;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002595 IdentifierResolver &IdResolver;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002596 bool IsModule;
2597
Douglas Gregor1d583f22009-04-28 21:18:29 +00002598 /// \brief Determines whether this is an "interesting" identifier
2599 /// that needs a full IdentifierInfo structure written into the hash
2600 /// table.
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002601 bool isInterestingIdentifier(IdentifierInfo *II, MacroInfo *&Macro) {
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002602 if (II->isPoisoned() ||
2603 II->isExtensionToken() ||
2604 II->getObjCOrBuiltinID() ||
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002605 II->hasRevertedTokenIDToIdentifier() ||
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002606 II->getFETokenInfo<void>())
2607 return true;
2608
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002609 return hadMacroDefinition(II, Macro);
Douglas Gregord7910e92011-09-14 22:14:14 +00002610 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002611
2612 bool hadMacroDefinition(IdentifierInfo *II, MacroInfo *&Macro) {
2613 if (!II->hadMacroDefinition())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002614 return false;
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002615
2616 if (Macro || (Macro = PP.getMacroInfoHistory(II)))
Douglas Gregorebf00492011-10-17 15:32:29 +00002617 return !Macro->isBuiltinMacro() && (!IsModule || Macro->isPublic());
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002618
2619 return false;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002620 }
2621
Douglas Gregore84a9da2009-04-20 20:36:09 +00002622public:
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002623 typedef IdentifierInfo* key_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002624 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002625
Sebastian Redl539c5062010-08-18 23:57:32 +00002626 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002627 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002628
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002629 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
2630 IdentifierResolver &IdResolver, bool IsModule)
2631 : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002632
2633 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002634 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002635 }
Mike Stump11289f42009-09-09 15:08:12 +00002636
2637 std::pair<unsigned,unsigned>
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002638 EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002639 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002640 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
Douglas Gregord7910e92011-09-14 22:14:14 +00002641 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002642 if (isInterestingIdentifier(II, Macro)) {
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002643 DataLen += 2; // 2 bytes for builtin ID
2644 DataLen += 2; // 2 bytes for flags
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002645 if (hadMacroDefinition(II, Macro)) {
2646 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2647 if (Writer.getMacroRef(M) != 0)
2648 DataLen += 4;
2649 }
2650
2651 DataLen += 4;
2652 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002653
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002654 for (IdentifierResolver::iterator D = IdResolver.begin(II),
2655 DEnd = IdResolver.end();
Douglas Gregor1d583f22009-04-28 21:18:29 +00002656 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002657 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002658 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002659 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002660 // We emit the key length after the data length so that every
2661 // string is preceded by a 16-bit length. This matches the PTH
2662 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002663 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002664 return std::make_pair(KeyLen, DataLen);
2665 }
Mike Stump11289f42009-09-09 15:08:12 +00002666
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002667 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002668 unsigned KeyLen) {
2669 // Record the location of the key data. This is used when generating
2670 // the mapping from persistent IDs to strings.
2671 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002672 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002673 }
Mike Stump11289f42009-09-09 15:08:12 +00002674
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002675 void EmitData(raw_ostream& Out, IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002676 IdentID ID, unsigned) {
Douglas Gregord7910e92011-09-14 22:14:14 +00002677 MacroInfo *Macro = 0;
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002678 if (!isInterestingIdentifier(II, Macro)) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002679 clang::io::Emit32(Out, ID << 1);
2680 return;
2681 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002682
Douglas Gregor1d583f22009-04-28 21:18:29 +00002683 clang::io::Emit32(Out, (ID << 1) | 0x01);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002684 uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
2685 assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
2686 clang::io::Emit16(Out, Bits);
2687 Bits = 0;
2688 bool HadMacroDefinition = hadMacroDefinition(II, Macro);
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002689 Bits = (Bits << 1) | unsigned(HadMacroDefinition);
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002690 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2691 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002692 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002693 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002694 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002695
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002696 if (HadMacroDefinition) {
Douglas Gregor5a4649b2012-10-11 00:46:49 +00002697 // Write all of the macro IDs associated with this identifier.
2698 for (MacroInfo *M = Macro; M; M = M->getPreviousDefinition()) {
2699 if (MacroID ID = Writer.getMacroRef(M))
2700 clang::io::Emit32(Out, ID);
2701 }
2702
2703 clang::io::Emit32(Out, 0);
Douglas Gregor7b8e4bc2011-12-02 15:45:10 +00002704 }
Alexander Kornienko1d26c022012-09-25 17:18:14 +00002705
Douglas Gregora868bbd2009-04-21 22:25:48 +00002706 // Emit the declaration IDs in reverse order, because the
2707 // IdentifierResolver provides the declarations as they would be
2708 // visible (e.g., the function "stat" would come before the struct
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002709 // "stat"), but the ASTReader adds declarations to the end of the list
2710 // (so we need to see the struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002711 // Only emit declarations that aren't from a chained PCH, though.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002712 SmallVector<Decl *, 16> Decls(IdResolver.begin(II),
2713 IdResolver.end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002714 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002715 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002716 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002717 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002718 }
2719};
2720} // end anonymous namespace
2721
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002722/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002723///
2724/// The identifier table consists of a blob containing string data
2725/// (the actual identifiers themselves) and a separate "offsets" index
2726/// that maps identifier IDs to locations within the blob.
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002727void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
2728 IdentifierResolver &IdResolver,
2729 bool IsModule) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002730 using namespace llvm;
2731
2732 // Create and write out the blob that contains the identifier
2733 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002734 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002735 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002736 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Mike Stump11289f42009-09-09 15:08:12 +00002737
Douglas Gregore6648fb2009-04-28 20:33:11 +00002738 // Look for any identifiers that were named while processing the
2739 // headers, but are otherwise not needed. We add these to the hash
2740 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002741 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002742 // file.
2743 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2744 IDEnd = PP.getIdentifierTable().end();
2745 ID != IDEnd; ++ID)
2746 getIdentifierRef(ID->second);
2747
Sebastian Redlff4a2952010-07-23 23:49:55 +00002748 // Create the on-disk hash table representation. We only store offsets
2749 // for identifiers that appear here for the first time.
2750 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002751 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002752 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2753 ID != IDEnd; ++ID) {
2754 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002755 if (!Chain || !ID->first->isFromAST() ||
2756 ID->first->hasChangedSinceDeserialization())
Douglas Gregor4a69c2e2011-09-01 17:04:32 +00002757 Generator.insert(const_cast<IdentifierInfo *>(ID->first), ID->second,
2758 Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002759 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002760
Douglas Gregore84a9da2009-04-20 20:36:09 +00002761 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002762 SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002763 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002764 {
Douglas Gregor935bc7a22011-10-27 09:33:13 +00002765 ASTIdentifierTableTrait Trait(*this, PP, IdResolver, IsModule);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002766 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002767 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002768 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002769 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002770 }
2771
2772 // Create a blob abbreviation
2773 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002775 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002777 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002778
2779 // Write the identifier table
2780 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002781 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002782 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002783 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002784 }
2785
2786 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002787 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002788 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2792 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2793
2794 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002795 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002796 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002797 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002798 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002799 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002800}
2801
Douglas Gregorc5046832009-04-27 18:38:38 +00002802//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002803// DeclContext's Name Lookup Table Serialization
2804//===----------------------------------------------------------------------===//
2805
2806namespace {
2807// Trait used for the on-disk hash table used in the method pool.
2808class ASTDeclContextNameLookupTrait {
2809 ASTWriter &Writer;
2810
2811public:
2812 typedef DeclarationName key_type;
2813 typedef key_type key_type_ref;
2814
2815 typedef DeclContext::lookup_result data_type;
2816 typedef const data_type& data_type_ref;
2817
2818 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2819
2820 unsigned ComputeHash(DeclarationName Name) {
2821 llvm::FoldingSetNodeID ID;
2822 ID.AddInteger(Name.getNameKind());
2823
2824 switch (Name.getNameKind()) {
2825 case DeclarationName::Identifier:
2826 ID.AddString(Name.getAsIdentifierInfo()->getName());
2827 break;
2828 case DeclarationName::ObjCZeroArgSelector:
2829 case DeclarationName::ObjCOneArgSelector:
2830 case DeclarationName::ObjCMultiArgSelector:
2831 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2832 break;
2833 case DeclarationName::CXXConstructorName:
2834 case DeclarationName::CXXDestructorName:
2835 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002836 break;
2837 case DeclarationName::CXXOperatorName:
2838 ID.AddInteger(Name.getCXXOverloadedOperator());
2839 break;
2840 case DeclarationName::CXXLiteralOperatorName:
2841 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2842 case DeclarationName::CXXUsingDirective:
2843 break;
2844 }
2845
2846 return ID.ComputeHash();
2847 }
2848
2849 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002850 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002851 data_type_ref Lookup) {
2852 unsigned KeyLen = 1;
2853 switch (Name.getNameKind()) {
2854 case DeclarationName::Identifier:
2855 case DeclarationName::ObjCZeroArgSelector:
2856 case DeclarationName::ObjCOneArgSelector:
2857 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002858 case DeclarationName::CXXLiteralOperatorName:
2859 KeyLen += 4;
2860 break;
2861 case DeclarationName::CXXOperatorName:
2862 KeyLen += 1;
2863 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002864 case DeclarationName::CXXConstructorName:
2865 case DeclarationName::CXXDestructorName:
2866 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002867 case DeclarationName::CXXUsingDirective:
2868 break;
2869 }
2870 clang::io::Emit16(Out, KeyLen);
2871
2872 // 2 bytes for num of decls and 4 for each DeclID.
2873 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2874 clang::io::Emit16(Out, DataLen);
2875
2876 return std::make_pair(KeyLen, DataLen);
2877 }
2878
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002879 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002880 using namespace clang::io;
2881
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002882 Emit8(Out, Name.getNameKind());
2883 switch (Name.getNameKind()) {
2884 case DeclarationName::Identifier:
2885 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002886 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002887 case DeclarationName::ObjCZeroArgSelector:
2888 case DeclarationName::ObjCOneArgSelector:
2889 case DeclarationName::ObjCMultiArgSelector:
2890 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002891 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002892 case DeclarationName::CXXOperatorName:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002893 assert(Name.getCXXOverloadedOperator() < NUM_OVERLOADED_OPERATORS &&
2894 "Invalid operator?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002895 Emit8(Out, Name.getCXXOverloadedOperator());
Benjamin Kramer53750b12012-09-19 13:40:40 +00002896 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002897 case DeclarationName::CXXLiteralOperatorName:
2898 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
Benjamin Kramer53750b12012-09-19 13:40:40 +00002899 return;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002900 case DeclarationName::CXXConstructorName:
2901 case DeclarationName::CXXDestructorName:
2902 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002903 case DeclarationName::CXXUsingDirective:
Benjamin Kramer53750b12012-09-19 13:40:40 +00002904 return;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002905 }
Benjamin Kramer53750b12012-09-19 13:40:40 +00002906
2907 llvm_unreachable("Invalid name kind?");
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002908 }
2909
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002910 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002911 data_type Lookup, unsigned DataLen) {
2912 uint64_t Start = Out.tell(); (void)Start;
2913 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2914 for (; Lookup.first != Lookup.second; ++Lookup.first)
2915 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2916
2917 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2918 }
2919};
2920} // end anonymous namespace
2921
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002922/// \brief Write the block containing all of the declaration IDs
2923/// visible from the given DeclContext.
2924///
2925/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002926/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002927uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2928 DeclContext *DC) {
2929 if (DC->getPrimaryContext() != DC)
2930 return 0;
2931
2932 // Since there is no name lookup into functions or methods, don't bother to
2933 // build a visible-declarations table for these entities.
2934 if (DC->isFunctionOrMethod())
2935 return 0;
2936
2937 // If not in C++, we perform name lookup for the translation unit via the
2938 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2939 // FIXME: In C++ we need the visible declarations in order to "see" the
2940 // friend declarations, is there a way to do this without writing the table ?
David Blaikiebbafb8a2012-03-11 07:00:24 +00002941 if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002942 return 0;
2943
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002944 // Serialize the contents of the mapping used for lookup. Note that,
2945 // although we have two very different code paths, the serialized
2946 // representation is the same for both cases: a declaration name,
2947 // followed by a size, followed by references to the visible
2948 // declarations that have that name.
2949 uint64_t Offset = Stream.GetCurrentBitNo();
Richard Smithf634c902012-03-16 06:12:59 +00002950 StoredDeclsMap *Map = DC->buildLookup();
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002951 if (!Map || Map->empty())
2952 return 0;
2953
2954 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2955 ASTDeclContextNameLookupTrait Trait(*this);
2956
2957 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002958 DeclarationName ConversionName;
2959 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002960 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2961 D != DEnd; ++D) {
2962 DeclarationName Name = D->first;
2963 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002964 if (Result.first != Result.second) {
2965 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2966 // Hash all conversion function names to the same name. The actual
2967 // type information in conversion function name is not used in the
2968 // key (since such type information is not stable across different
2969 // modules), so the intended effect is to coalesce all of the conversion
2970 // functions under a single key.
2971 if (!ConversionName)
2972 ConversionName = Name;
2973 ConversionDecls.append(Result.first, Result.second);
2974 continue;
2975 }
2976
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002977 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002978 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002979 }
2980
Douglas Gregor05ef9312011-08-30 20:49:19 +00002981 // Add the conversion functions
2982 if (!ConversionDecls.empty()) {
2983 Generator.insert(ConversionName,
2984 DeclContext::lookup_result(ConversionDecls.begin(),
2985 ConversionDecls.end()),
2986 Trait);
2987 }
2988
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002989 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002990 SmallString<4096> LookupTable;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002991 uint32_t BucketOffset;
2992 {
2993 llvm::raw_svector_ostream Out(LookupTable);
2994 // Make sure that no bucket is at offset 0
2995 clang::io::Emit32(Out, 0);
2996 BucketOffset = Generator.Emit(Out, Trait);
2997 }
2998
2999 // Write the lookup table
3000 RecordData Record;
3001 Record.push_back(DECL_CONTEXT_VISIBLE);
3002 Record.push_back(BucketOffset);
3003 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3004 LookupTable.str());
3005
3006 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
3007 ++NumVisibleDeclContexts;
3008 return Offset;
3009}
3010
Sebastian Redla4071b42010-08-24 00:50:09 +00003011/// \brief Write an UPDATE_VISIBLE block for the given context.
3012///
3013/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3014/// DeclContext in a dependent AST file. As such, they only exist for the TU
Richard Smithf634c902012-03-16 06:12:59 +00003015/// (in C++), for namespaces, and for classes with forward-declared unscoped
3016/// enumeration members (in C++11).
Sebastian Redla4071b42010-08-24 00:50:09 +00003017void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00003018 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
3019 if (!Map || Map->empty())
3020 return;
3021
3022 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
3023 ASTDeclContextNameLookupTrait Trait(*this);
3024
3025 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00003026 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
3027 D != DEnd; ++D) {
3028 DeclarationName Name = D->first;
3029 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00003030 // For any name that appears in this table, the results are complete, i.e.
3031 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00003032 if (Result.first != Result.second)
3033 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00003034 }
3035
3036 // Create the on-disk hash table in a buffer.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003037 SmallString<4096> LookupTable;
Sebastian Redla4071b42010-08-24 00:50:09 +00003038 uint32_t BucketOffset;
3039 {
3040 llvm::raw_svector_ostream Out(LookupTable);
3041 // Make sure that no bucket is at offset 0
3042 clang::io::Emit32(Out, 0);
3043 BucketOffset = Generator.Emit(Out, Trait);
3044 }
3045
3046 // Write the lookup table
3047 RecordData Record;
3048 Record.push_back(UPDATE_VISIBLE);
3049 Record.push_back(getDeclID(cast<Decl>(DC)));
3050 Record.push_back(BucketOffset);
3051 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
3052}
3053
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003054/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3055void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3056 RecordData Record;
3057 Record.push_back(Opts.fp_contract);
3058 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3059}
3060
3061/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3062void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00003063 if (!SemaRef.Context.getLangOpts().OpenCL)
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003064 return;
3065
3066 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3067 RecordData Record;
3068#define OPENCLEXT(nm) Record.push_back(Opts.nm);
3069#include "clang/Basic/OpenCLExtensions.def"
3070 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3071}
3072
Douglas Gregor358cd442012-01-15 16:58:34 +00003073void ASTWriter::WriteRedeclarations() {
3074 RecordData LocalRedeclChains;
3075 SmallVector<serialization::LocalRedeclarationsInfo, 2> LocalRedeclsMap;
3076
3077 for (unsigned I = 0, N = Redeclarations.size(); I != N; ++I) {
3078 Decl *First = Redeclarations[I];
3079 assert(First->getPreviousDecl() == 0 && "Not the first declaration?");
3080
3081 Decl *MostRecent = First->getMostRecentDecl();
3082
3083 // If we only have a single declaration, there is no point in storing
3084 // a redeclaration chain.
3085 if (First == MostRecent)
3086 continue;
3087
3088 unsigned Offset = LocalRedeclChains.size();
3089 unsigned Size = 0;
3090 LocalRedeclChains.push_back(0); // Placeholder for the size.
3091
3092 // Collect the set of local redeclarations of this declaration.
3093 for (Decl *Prev = MostRecent; Prev != First;
3094 Prev = Prev->getPreviousDecl()) {
3095 if (!Prev->isFromASTFile()) {
3096 AddDeclRef(Prev, LocalRedeclChains);
3097 ++Size;
3098 }
3099 }
3100 LocalRedeclChains[Offset] = Size;
3101
3102 // Reverse the set of local redeclarations, so that we store them in
3103 // order (since we found them in reverse order).
3104 std::reverse(LocalRedeclChains.end() - Size, LocalRedeclChains.end());
3105
3106 // Add the mapping from the first ID to the set of local declarations.
3107 LocalRedeclarationsInfo Info = { getDeclID(First), Offset };
3108 LocalRedeclsMap.push_back(Info);
3109
3110 assert(N == Redeclarations.size() &&
3111 "Deserialized a declaration we shouldn't have");
3112 }
3113
3114 if (LocalRedeclChains.empty())
3115 return;
3116
3117 // Sort the local redeclarations map by the first declaration ID,
3118 // since the reader will be performing binary searches on this information.
3119 llvm::array_pod_sort(LocalRedeclsMap.begin(), LocalRedeclsMap.end());
3120
3121 // Emit the local redeclarations map.
3122 using namespace llvm;
3123 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3124 Abbrev->Add(BitCodeAbbrevOp(LOCAL_REDECLARATIONS_MAP));
3125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3126 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3127 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3128
3129 RecordData Record;
3130 Record.push_back(LOCAL_REDECLARATIONS_MAP);
3131 Record.push_back(LocalRedeclsMap.size());
3132 Stream.EmitRecordWithBlob(AbbrevID, Record,
3133 reinterpret_cast<char*>(LocalRedeclsMap.data()),
3134 LocalRedeclsMap.size() * sizeof(LocalRedeclarationsInfo));
3135
3136 // Emit the redeclaration chains.
3137 Stream.EmitRecord(LOCAL_REDECLARATIONS, LocalRedeclChains);
3138}
3139
Douglas Gregor404cdde2012-01-27 01:47:08 +00003140void ASTWriter::WriteObjCCategories() {
3141 llvm::SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3142 RecordData Categories;
3143
3144 for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3145 unsigned Size = 0;
3146 unsigned StartIndex = Categories.size();
3147
3148 ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3149
3150 // Allocate space for the size.
3151 Categories.push_back(0);
3152
3153 // Add the categories.
3154 for (ObjCCategoryDecl *Cat = Class->getCategoryList();
3155 Cat; Cat = Cat->getNextClassCategory(), ++Size) {
3156 assert(getDeclID(Cat) != 0 && "Bogus category");
3157 AddDeclRef(Cat, Categories);
3158 }
3159
3160 // Update the size.
3161 Categories[StartIndex] = Size;
3162
3163 // Record this interface -> category map.
3164 ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3165 CategoriesMap.push_back(CatInfo);
3166 }
3167
3168 // Sort the categories map by the definition ID, since the reader will be
3169 // performing binary searches on this information.
3170 llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3171
3172 // Emit the categories map.
3173 using namespace llvm;
3174 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3175 Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3178 unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3179
3180 RecordData Record;
3181 Record.push_back(OBJC_CATEGORIES_MAP);
3182 Record.push_back(CategoriesMap.size());
3183 Stream.EmitRecordWithBlob(AbbrevID, Record,
3184 reinterpret_cast<char*>(CategoriesMap.data()),
3185 CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3186
3187 // Emit the category lists.
3188 Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3189}
3190
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003191void ASTWriter::WriteMergedDecls() {
3192 if (!Chain || Chain->MergedDecls.empty())
3193 return;
3194
3195 RecordData Record;
3196 for (ASTReader::MergedDeclsMap::iterator I = Chain->MergedDecls.begin(),
3197 IEnd = Chain->MergedDecls.end();
3198 I != IEnd; ++I) {
Douglas Gregor64af53c2012-01-05 22:27:05 +00003199 DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID()
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003200 : getDeclID(I->first);
3201 assert(CanonID && "Merged declaration not known?");
3202
3203 Record.push_back(CanonID);
3204 Record.push_back(I->second.size());
3205 Record.append(I->second.begin(), I->second.end());
3206 }
3207 Stream.EmitRecord(MERGED_DECLARATIONS, Record);
3208}
3209
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003210//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00003211// General Serialization Routines
3212//===----------------------------------------------------------------------===//
3213
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003214/// \brief Write a record containing the given attributes.
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003215void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3216 RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00003217 Record.push_back(Attrs.size());
Alexander Kornienko20f6fc62012-07-09 10:04:07 +00003218 for (ArrayRef<const Attr *>::iterator i = Attrs.begin(),
3219 e = Attrs.end(); i != e; ++i){
3220 const Attr *A = *i;
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003221 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
Argyrios Kyrtzidis309b4c42011-09-13 16:05:58 +00003222 AddSourceRange(A->getRange(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003223
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003224#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00003225
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003226 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003227}
3228
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003229void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00003230 Record.push_back(Str.size());
3231 Record.insert(Record.end(), Str.begin(), Str.end());
3232}
3233
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003234void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3235 RecordDataImpl &Record) {
3236 Record.push_back(Version.getMajor());
3237 if (llvm::Optional<unsigned> Minor = Version.getMinor())
3238 Record.push_back(*Minor + 1);
3239 else
3240 Record.push_back(0);
3241 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
3242 Record.push_back(*Subminor + 1);
3243 else
3244 Record.push_back(0);
3245}
3246
Douglas Gregore84a9da2009-04-20 20:36:09 +00003247/// \brief Note that the identifier II occurs at the given offset
3248/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003249void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00003250 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003251 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00003252 // up earlier in the chain and thus don't need an offset.
3253 if (ID >= FirstIdentID)
3254 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00003255}
3256
Douglas Gregor95c13f52009-04-25 17:48:32 +00003257/// \brief Note that the selector Sel occurs at the given offset
3258/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003259void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00003260 unsigned ID = SelectorIDs[Sel];
3261 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00003262 // Don't record offsets for selectors that are also available in a different
3263 // file.
3264 if (ID < FirstSelectorID)
3265 return;
3266 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00003267}
3268
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003269ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003270 : Stream(Stream), Context(0), PP(0), Chain(0), WritingModule(0),
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003271 WritingAST(false), DoneWritingDeclsAndTypes(false),
3272 ASTHasCompilerErrors(false),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00003273 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00003274 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003275 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
3276 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor253eefe2011-12-01 00:59:36 +00003277 FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
3278 NextSubmoduleID(FirstSubmoduleID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00003279 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregor91096292010-10-02 19:29:26 +00003280 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00003281 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003282 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00003283 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003284 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00003285 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
3286 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
3287 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00003288 DeclTypedefAbbrev(0),
3289 DeclVarAbbrev(0), DeclFieldAbbrev(0),
3290 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003291{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003292}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003293
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003294ASTWriter::~ASTWriter() {
3295 for (FileDeclIDsTy::iterator
3296 I = FileDeclIDs.begin(), E = FileDeclIDs.end(); I != E; ++I)
3297 delete I->second;
3298}
3299
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003300void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003301 const std::string &OutputFile,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003302 Module *WritingModule, StringRef isysroot,
3303 bool hasErrors) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003304 WritingAST = true;
3305
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +00003306 ASTHasCompilerErrors = hasErrors;
3307
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003308 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00003309 Stream.Emit((unsigned)'C', 8);
3310 Stream.Emit((unsigned)'P', 8);
3311 Stream.Emit((unsigned)'C', 8);
3312 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00003313
Chris Lattner28fa4e62009-04-26 22:26:21 +00003314 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003315
Douglas Gregoreda8e122011-08-09 15:13:55 +00003316 Context = &SemaRef.Context;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003317 PP = &SemaRef.PP;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003318 this->WritingModule = WritingModule;
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003319 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile, WritingModule);
Douglas Gregoreda8e122011-08-09 15:13:55 +00003320 Context = 0;
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003321 PP = 0;
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003322 this->WritingModule = 0;
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003323
3324 WritingAST = false;
Sebastian Redl143413f2010-07-12 22:02:52 +00003325}
3326
Douglas Gregora94a1542011-07-27 21:45:57 +00003327template<typename Vector>
3328static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
3329 ASTWriter::RecordData &Record) {
3330 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
3331 I != E; ++I) {
3332 Writer.AddDeclRef(*I, Record);
3333 }
3334}
3335
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003336void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003337 StringRef isysroot,
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003338 const std::string &OutputFile,
Douglas Gregorde3ef502011-11-30 23:21:26 +00003339 Module *WritingModule) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003340 using namespace llvm;
3341
Douglas Gregorcf68c582011-12-01 22:20:10 +00003342 // Make sure that the AST reader knows to finalize itself.
3343 if (Chain)
3344 Chain->finalizeForWriting();
3345
Sebastian Redl143413f2010-07-12 22:02:52 +00003346 ASTContext &Context = SemaRef.Context;
3347 Preprocessor &PP = SemaRef.PP;
3348
Douglas Gregordab42432011-08-12 00:15:20 +00003349 // Set up predefined declaration IDs.
3350 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00003351 if (Context.ObjCIdDecl)
3352 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00003353 if (Context.ObjCSelDecl)
3354 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00003355 if (Context.ObjCClassDecl)
3356 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregord53ae832012-01-17 18:09:05 +00003357 if (Context.ObjCProtocolClassDecl)
3358 DeclIDs[Context.ObjCProtocolClassDecl] = PREDEF_DECL_OBJC_PROTOCOL_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00003359 if (Context.Int128Decl)
3360 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
3361 if (Context.UInt128Decl)
3362 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregorbab8a962011-09-08 01:46:34 +00003363 if (Context.ObjCInstanceTypeDecl)
3364 DeclIDs[Context.ObjCInstanceTypeDecl] = PREDEF_DECL_OBJC_INSTANCETYPE_ID;
Meador Inge5d3fb222012-06-16 03:34:49 +00003365 if (Context.BuiltinVaListDecl)
3366 DeclIDs[Context.getBuiltinVaListDecl()] = PREDEF_DECL_BUILTIN_VA_LIST_ID;
3367
Douglas Gregor851443c2011-08-12 01:39:19 +00003368 if (!Chain) {
3369 // Make sure that we emit IdentifierInfos (and any attached
3370 // declarations) for builtins. We don't need to do this when we're
3371 // emitting chained PCH files, because all of the builtins will be
3372 // in the original PCH file.
3373 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003374 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003375 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003376 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
David Blaikiebbafb8a2012-03-11 07:00:24 +00003377 Context.getLangOpts().NoBuiltin);
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003378 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
3379 getIdentifierRef(&Table.get(BuiltinNames[I]));
3380 }
3381
Douglas Gregor935bc7a22011-10-27 09:33:13 +00003382 // If there are any out-of-date identifiers, bring them up to date.
3383 if (ExternalPreprocessorSource *ExtSource = PP.getExternalSource()) {
3384 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
3385 IDEnd = PP.getIdentifierTable().end();
3386 ID != IDEnd; ++ID)
3387 if (ID->second->isOutOfDate())
3388 ExtSource->updateOutOfDateIdentifier(*ID->second);
3389 }
3390
Chris Lattner0c797362009-09-08 18:19:27 +00003391 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00003392 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00003393 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00003394 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003395 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003396
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003397 // Build a record containing all of the file scoped decls in this file.
3398 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003399 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3400 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003401
Douglas Gregor851443c2011-08-12 01:39:19 +00003402 // Build a record containing all of the delegating constructors we still need
3403 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00003404 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003405 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003406
Douglas Gregor851443c2011-08-12 01:39:19 +00003407 // Write the set of weak, undeclared identifiers. We always write the
3408 // entire table, since later PCH files in a PCH chain are only interested in
3409 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003410 RecordData WeakUndeclaredIdentifiers;
3411 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003412 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003413 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3414 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3415 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3416 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3417 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3418 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3419 }
3420 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003421
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003422 // Build a record containing all of the locally-scoped external
3423 // declarations in this header file. Generally, this record will be
3424 // empty.
3425 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003426 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00003427 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00003428 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003429 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3430 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00003431 TD != TDEnd; ++TD) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003432 if (!TD->second->isFromASTFile())
Douglas Gregordc5c9582011-07-28 14:20:37 +00003433 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3434 }
3435
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003436 // Build a record containing all of the ext_vector declarations.
3437 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003438 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003439
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003440 // Build a record containing all of the VTable uses information.
3441 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003442 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00003443 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3444 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3445 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3446 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3447 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003448 }
3449
3450 // Build a record containing all of dynamic classes declarations.
3451 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003452 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003453
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003454 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003455 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003456 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003457 I = SemaRef.PendingInstantiations.begin(),
3458 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
3459 AddDeclRef(I->first, PendingInstantiations);
3460 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003461 }
3462 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3463 "There are local ones at end of translation unit!");
3464
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003465 // Build a record containing some declaration references.
3466 RecordData SemaDeclRefs;
3467 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3468 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3469 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3470 }
3471
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003472 RecordData CUDASpecialDeclRefs;
3473 if (Context.getcudaConfigureCallDecl()) {
3474 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
3475 }
3476
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003477 // Build a record containing all of the known namespaces.
3478 RecordData KnownNamespaces;
3479 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
3480 I = SemaRef.KnownNamespaces.begin(),
3481 IEnd = SemaRef.KnownNamespaces.end();
3482 I != IEnd; ++I) {
3483 if (!I->second)
3484 AddDeclRef(I->first, KnownNamespaces);
3485 }
Douglas Gregor112b9072012-10-18 05:31:06 +00003486
3487 // Write the control block
3488 WriteControlBlock(Context, isysroot, OutputFile);
3489
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003490 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00003491 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003492 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Douglas Gregorc567ba22011-07-22 16:35:34 +00003493 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00003494 WriteStatCache(*StatCalls);
Douglas Gregor851443c2011-08-12 01:39:19 +00003495
3496 // Create a lexical update block containing all of the declarations in the
3497 // translation unit that do not come from other AST files.
3498 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3499 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3500 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3501 E = TU->noload_decls_end();
3502 I != E; ++I) {
Douglas Gregorb3722e22011-09-09 23:01:35 +00003503 if (!(*I)->isFromASTFile())
Douglas Gregor851443c2011-08-12 01:39:19 +00003504 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Douglas Gregor851443c2011-08-12 01:39:19 +00003505 }
3506
3507 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3508 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3509 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3510 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3511 Record.clear();
3512 Record.push_back(TU_UPDATE_LEXICAL);
3513 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3514 data(NewGlobalDecls));
3515
3516 // And a visible updates block for the translation unit.
3517 Abv = new llvm::BitCodeAbbrev();
3518 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3519 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3520 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3521 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3522 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3523 WriteDeclContextVisibleUpdate(TU);
3524
3525 // If the translation unit has an anonymous namespace, and we don't already
3526 // have an update block for it, write it as an update block.
3527 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3528 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3529 if (Record.empty()) {
3530 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003531 Record.push_back(reinterpret_cast<uint64_t>(NS));
Douglas Gregor851443c2011-08-12 01:39:19 +00003532 }
3533 }
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003534
3535 // Make sure visible decls, added to DeclContexts previously loaded from
3536 // an AST file, are registered for serialization.
3537 for (SmallVector<const Decl *, 16>::iterator
3538 I = UpdatingVisibleDecls.begin(),
3539 E = UpdatingVisibleDecls.end(); I != E; ++I) {
3540 GetDeclRef(*I);
3541 }
3542
Argyrios Kyrtzidis09c1b3d2011-11-14 04:52:24 +00003543 // Resolve any declaration pointers within the declaration updates block.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003544 ResolveDeclUpdatesBlocks();
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003545
Douglas Gregor5204bde2011-08-02 16:26:37 +00003546 // Form the record of special types.
3547 RecordData SpecialTypes;
Douglas Gregor5204bde2011-08-02 16:26:37 +00003548 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003549 AddTypeRef(Context.getFILEType(), SpecialTypes);
3550 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3551 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3552 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3553 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003554 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Rafael Espindola6cfa82b2011-11-13 21:51:09 +00003555 AddTypeRef(Context.getucontext_tType(), SpecialTypes);
Douglas Gregora28bcdd2011-12-01 02:07:58 +00003556
Douglas Gregor1970d882009-04-26 03:49:13 +00003557 // Keep writing types and declarations until all types and
3558 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003559 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003560 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003561 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3562 E = DeclsToRewrite.end();
3563 I != E; ++I)
3564 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003565 while (!DeclTypesToEmit.empty()) {
3566 DeclOrType DOT = DeclTypesToEmit.front();
3567 DeclTypesToEmit.pop();
3568 if (DOT.isType())
3569 WriteType(DOT.getType());
3570 else
3571 WriteDecl(Context, DOT.getDecl());
3572 }
3573 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003574
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003575 DoneWritingDeclsAndTypes = true;
3576
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003577 WriteFileDeclIDsMap();
3578 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Dmitri Gribenkoaab83832012-06-20 00:34:58 +00003579 WriteComments();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003580
3581 if (Chain) {
3582 // Write the mapping information describing our module dependencies and how
3583 // each of those modules were mapped into our own offset/ID space, so that
3584 // the reader can build the appropriate mapping to its own offset/ID space.
3585 // The map consists solely of a blob with the following format:
3586 // *(module-name-len:i16 module-name:len*i8
3587 // source-location-offset:i32
3588 // identifier-id:i32
3589 // preprocessed-entity-id:i32
3590 // macro-definition-id:i32
Douglas Gregor253eefe2011-12-01 00:59:36 +00003591 // submodule-id:i32
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003592 // selector-id:i32
3593 // declaration-id:i32
3594 // c++-base-specifiers-id:i32
3595 // type-id:i32)
3596 //
3597 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3598 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3599 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3600 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00003601 SmallString<2048> Buffer;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003602 {
3603 llvm::raw_svector_ostream Out(Buffer);
3604 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
Douglas Gregor24bb9232011-12-02 18:58:38 +00003605 MEnd = Chain->ModuleMgr.end();
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003606 M != MEnd; ++M) {
3607 StringRef FileName = (*M)->FileName;
3608 io::Emit16(Out, FileName.size());
3609 Out.write(FileName.data(), FileName.size());
3610 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3611 io::Emit32(Out, (*M)->BaseIdentifierID);
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003612 io::Emit32(Out, (*M)->BaseMacroID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003613 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
Douglas Gregor253eefe2011-12-01 00:59:36 +00003614 io::Emit32(Out, (*M)->BaseSubmoduleID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00003615 io::Emit32(Out, (*M)->BaseSelectorID);
3616 io::Emit32(Out, (*M)->BaseDeclID);
3617 io::Emit32(Out, (*M)->BaseTypeIndex);
3618 }
3619 }
3620 Record.clear();
3621 Record.push_back(MODULE_OFFSET_MAP);
3622 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3623 Buffer.data(), Buffer.size());
3624 }
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003625 WritePreprocessor(PP, WritingModule != 0);
Douglas Gregor09b69892011-02-10 17:09:37 +00003626 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003627 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003628 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorf7a700fd2011-11-30 04:39:39 +00003629 WriteIdentifierTable(PP, SemaRef.IdResolver, WritingModule != 0);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003630 WriteFPPragmaOptions(SemaRef.getFPOptions());
3631 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003632
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003633 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003634 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003635
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003636 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003637
Douglas Gregora89c5ac2011-12-06 01:10:29 +00003638 // If we're emitting a module, write out the submodule information.
3639 if (WritingModule)
3640 WriteSubmodules(WritingModule);
3641
Douglas Gregor5204bde2011-08-02 16:26:37 +00003642 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3643
Douglas Gregord4df8652009-04-22 22:02:47 +00003644 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003645 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003646 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003647
3648 // Write the record containing tentative definitions.
3649 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003650 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003651
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003652 // Write the record containing unused file scoped decls.
3653 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003654 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003655
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003656 // Write the record containing weak undeclared identifiers.
3657 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003658 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003659 WeakUndeclaredIdentifiers);
3660
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003661 // Write the record containing locally-scoped external definitions.
3662 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003663 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003664 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003665
3666 // Write the record containing ext_vector type names.
3667 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003668 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003669
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003670 // Write the record containing VTable uses information.
3671 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003672 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003673
3674 // Write the record containing dynamic classes declarations.
3675 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003676 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003677
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003678 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003679 if (!PendingInstantiations.empty())
3680 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003681
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003682 // Write the record containing declaration references of Sema.
3683 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003684 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003685
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003686 // Write the record containing CUDA-specific declaration references.
3687 if (!CUDASpecialDeclRefs.empty())
3688 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003689
3690 // Write the delegating constructors.
3691 if (!DelegatingCtorDecls.empty())
3692 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003693
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003694 // Write the known namespaces.
3695 if (!KnownNamespaces.empty())
3696 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3697
Douglas Gregor851443c2011-08-12 01:39:19 +00003698 // Write the visible updates to DeclContexts.
3699 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3700 I = UpdatedDeclContexts.begin(),
3701 E = UpdatedDeclContexts.end();
3702 I != E; ++I)
3703 WriteDeclContextVisibleUpdate(*I);
3704
Douglas Gregor959bb062011-12-03 01:15:29 +00003705 if (!WritingModule) {
3706 // Write the submodules that were imported, if any.
3707 RecordData ImportedModules;
3708 for (ASTContext::import_iterator I = Context.local_import_begin(),
3709 IEnd = Context.local_import_end();
3710 I != IEnd; ++I) {
3711 assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
3712 ImportedModules.push_back(SubmoduleIDs[I->getImportedModule()]);
3713 }
3714 if (!ImportedModules.empty()) {
3715 // Sort module IDs.
3716 llvm::array_pod_sort(ImportedModules.begin(), ImportedModules.end());
3717
3718 // Unique module IDs.
3719 ImportedModules.erase(std::unique(ImportedModules.begin(),
3720 ImportedModules.end()),
3721 ImportedModules.end());
3722
3723 Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
3724 }
Douglas Gregor0a839132011-12-03 00:59:55 +00003725 }
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003726
3727 WriteMacroUpdates();
Douglas Gregordab42432011-08-12 00:15:20 +00003728 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003729 WriteDeclReplacementsBlock();
Douglas Gregor464b0ca2011-12-22 21:40:42 +00003730 WriteMergedDecls();
Douglas Gregor358cd442012-01-15 16:58:34 +00003731 WriteRedeclarations();
Douglas Gregor404cdde2012-01-27 01:47:08 +00003732 WriteObjCCategories();
Douglas Gregor05f10352011-12-17 23:38:30 +00003733
Douglas Gregor08f01292009-04-17 22:13:46 +00003734 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003735 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003736 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003737 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003738 Record.push_back(NumLexicalDeclContexts);
3739 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003740 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003741 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003742}
3743
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003744void ASTWriter::WriteMacroUpdates() {
3745 if (MacroUpdates.empty())
3746 return;
3747
3748 RecordData Record;
3749 for (MacroUpdatesMap::iterator I = MacroUpdates.begin(),
3750 E = MacroUpdates.end();
3751 I != E; ++I) {
3752 addMacroRef(I->first, Record);
3753 AddSourceLocation(I->second.UndefLoc, Record);
Douglas Gregorcfa46a82012-10-12 00:16:50 +00003754 Record.push_back(inferSubmoduleIDFromLocation(I->second.UndefLoc));
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003755 }
3756 Stream.EmitRecord(MACRO_UPDATES, Record);
3757}
3758
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003759/// \brief Go through the declaration update blocks and resolve declaration
3760/// pointers into declaration IDs.
3761void ASTWriter::ResolveDeclUpdatesBlocks() {
3762 for (DeclUpdateMap::iterator
3763 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3764 const Decl *D = I->first;
3765 UpdateRecord &URec = I->second;
3766
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003767 if (isRewritten(D))
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003768 continue; // The decl will be written completely
3769
3770 unsigned Idx = 0, N = URec.size();
3771 while (Idx < N) {
3772 switch ((DeclUpdateKind)URec[Idx++]) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00003773 case UPD_CXX_ADDED_IMPLICIT_MEMBER:
3774 case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
3775 case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
3776 URec[Idx] = GetDeclRef(reinterpret_cast<Decl *>(URec[Idx]));
3777 ++Idx;
3778 break;
3779
3780 case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
3781 ++Idx;
3782 break;
3783 }
3784 }
3785 }
3786}
3787
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003788void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003789 if (DeclUpdates.empty())
3790 return;
3791
3792 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003793 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003794 for (DeclUpdateMap::iterator
3795 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3796 const Decl *D = I->first;
3797 UpdateRecord &URec = I->second;
3798
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00003799 if (isRewritten(D))
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003800 continue; // The decl will be written completely,no need to store updates.
3801
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003802 uint64_t Offset = Stream.GetCurrentBitNo();
3803 Stream.EmitRecord(DECL_UPDATES, URec);
3804
3805 OffsetsRecord.push_back(GetDeclRef(D));
3806 OffsetsRecord.push_back(Offset);
3807 }
3808 Stream.ExitBlock();
3809 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3810}
3811
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003812void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003813 if (ReplacedDecls.empty())
3814 return;
3815
3816 RecordData Record;
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003817 for (SmallVector<ReplacedDeclInfo, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003818 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
Argyrios Kyrtzidis6fb60032011-10-31 07:20:15 +00003819 Record.push_back(I->ID);
3820 Record.push_back(I->Offset);
3821 Record.push_back(I->Loc);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003822 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003823 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003824}
3825
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003826void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003827 Record.push_back(Loc.getRawEncoding());
3828}
3829
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003830void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003831 AddSourceLocation(Range.getBegin(), Record);
3832 AddSourceLocation(Range.getEnd(), Record);
3833}
3834
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003835void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003836 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003837 const uint64_t *Words = Value.getRawData();
3838 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003839}
3840
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003841void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003842 Record.push_back(Value.isUnsigned());
3843 AddAPInt(Value, Record);
3844}
3845
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003846void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003847 AddAPInt(Value.bitcastToAPInt(), Record);
3848}
3849
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003850void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003851 Record.push_back(getIdentifierRef(II));
3852}
3853
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003854void ASTWriter::addMacroRef(MacroInfo *MI, RecordDataImpl &Record) {
3855 Record.push_back(getMacroRef(MI));
3856}
3857
Sebastian Redl539c5062010-08-18 23:57:32 +00003858IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003859 if (II == 0)
3860 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003861
Sebastian Redl539c5062010-08-18 23:57:32 +00003862 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003863 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003864 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003865 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003866}
3867
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00003868MacroID ASTWriter::getMacroRef(MacroInfo *MI) {
3869 // Don't emit builtin macros like __LINE__ to the AST file unless they
3870 // have been redefined by the header (in which case they are not
3871 // isBuiltinMacro).
3872 if (MI == 0 || MI->isBuiltinMacro())
3873 return 0;
3874
3875 MacroID &ID = MacroIDs[MI];
3876 if (ID == 0)
3877 ID = NextMacroID++;
3878 return ID;
3879}
3880
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003881void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003882 Record.push_back(getSelectorRef(SelRef));
3883}
3884
Sebastian Redl539c5062010-08-18 23:57:32 +00003885SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003886 if (Sel.getAsOpaquePtr() == 0) {
3887 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003888 }
3889
Sebastian Redl539c5062010-08-18 23:57:32 +00003890 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003891 if (SID == 0 && Chain) {
3892 // This might trigger a ReadSelector callback, which will set the ID for
3893 // this selector.
3894 Chain->LoadSelector(Sel);
3895 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003896 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003897 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003898 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003899 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003900}
3901
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003902void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003903 AddDeclRef(Temp->getDestructor(), Record);
3904}
3905
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003906void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3907 CXXBaseSpecifier const *BasesEnd,
3908 RecordDataImpl &Record) {
3909 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3910 CXXBaseSpecifiersToWrite.push_back(
3911 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3912 Bases, BasesEnd));
3913 Record.push_back(NextCXXBaseSpecifiersID++);
3914}
3915
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003916void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003917 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003918 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003919 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003920 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003921 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003922 break;
3923 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003924 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003925 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003926 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003927 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003928 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003929 break;
3930 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003931 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003932 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003933 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003934 break;
John McCall0ad16662009-10-29 08:12:44 +00003935 case TemplateArgument::Null:
3936 case TemplateArgument::Integral:
3937 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00003938 case TemplateArgument::NullPtr:
John McCall0ad16662009-10-29 08:12:44 +00003939 case TemplateArgument::Pack:
Eli Friedmanb826a002012-09-26 02:36:12 +00003940 // FIXME: Is this right?
John McCall0ad16662009-10-29 08:12:44 +00003941 break;
3942 }
3943}
3944
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003945void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003946 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003947 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003948
3949 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3950 bool InfoHasSameExpr
3951 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3952 Record.push_back(InfoHasSameExpr);
3953 if (InfoHasSameExpr)
3954 return; // Avoid storing the same expr twice.
3955 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003956 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3957 Record);
3958}
3959
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003960void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3961 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003962 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003963 AddTypeRef(QualType(), Record);
3964 return;
3965 }
3966
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003967 AddTypeLoc(TInfo->getTypeLoc(), Record);
3968}
3969
3970void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3971 AddTypeRef(TL.getType(), Record);
3972
John McCall8f115c62009-10-16 21:56:05 +00003973 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003974 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003975 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003976}
3977
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003978void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003979 Record.push_back(GetOrCreateTypeID(T));
3980}
3981
Douglas Gregoreda8e122011-08-09 15:13:55 +00003982TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3983 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003984 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3985}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003986
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003987TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003988 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003989 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003990}
3991
3992TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3993 if (T.isNull())
3994 return TypeIdx();
3995 assert(!T.getLocalFastQualifiers());
3996
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003997 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003998 if (Idx.getIndex() == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00003999 if (DoneWritingDeclsAndTypes) {
4000 assert(0 && "New type seen after serializing all the types to emit!");
4001 return TypeIdx();
4002 }
4003
Douglas Gregor1970d882009-04-26 03:49:13 +00004004 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00004005 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004006 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00004007 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00004008 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004009 return Idx;
4010}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004011
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004012TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00004013 if (T.isNull())
4014 return TypeIdx();
4015 assert(!T.getLocalFastQualifiers());
4016
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00004017 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4018 assert(I != TypeIdxs.end() && "Type not emitted!");
4019 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004020}
4021
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004022void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004023 Record.push_back(GetDeclRef(D));
4024}
4025
Sebastian Redl539c5062010-08-18 23:57:32 +00004026DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004027 assert(WritingAST && "Cannot request a declaration ID before AST writing");
4028
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004029 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004030 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004031 }
Douglas Gregorb3163e52012-01-05 22:33:30 +00004032
4033 // If D comes from an AST file, its declaration ID is already known and
4034 // fixed.
4035 if (D->isFromASTFile())
4036 return D->getGlobalID();
4037
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004038 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00004039 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00004040 if (ID == 0) {
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004041 if (DoneWritingDeclsAndTypes) {
4042 assert(0 && "New decl seen after serializing all the decls to emit!");
4043 return 0;
4044 }
4045
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004046 // We haven't seen this declaration before. Give it a new ID and
4047 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00004048 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00004049 DeclTypesToEmit.push(const_cast<Decl *>(D));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004050 }
4051
Sebastian Redl66c5eef2010-07-27 00:17:23 +00004052 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004053}
4054
Sebastian Redl539c5062010-08-18 23:57:32 +00004055DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00004056 if (D == 0)
4057 return 0;
4058
Douglas Gregorb3163e52012-01-05 22:33:30 +00004059 // If D comes from an AST file, its declaration ID is already known and
4060 // fixed.
4061 if (D->isFromASTFile())
4062 return D->getGlobalID();
4063
Douglas Gregore84a9da2009-04-20 20:36:09 +00004064 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4065 return DeclIDs[D];
4066}
4067
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004068static inline bool compLocDecl(std::pair<unsigned, serialization::DeclID> L,
4069 std::pair<unsigned, serialization::DeclID> R) {
4070 return L.first < R.first;
4071}
4072
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004073void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004074 assert(ID);
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004075 assert(D);
4076
4077 SourceLocation Loc = D->getLocation();
4078 if (Loc.isInvalid())
4079 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004080
4081 // We only keep track of the file-level declarations of each file.
4082 if (!D->getLexicalDeclContext()->isFileContext())
4083 return;
Argyrios Kyrtzidise1bc99e2012-02-24 19:45:46 +00004084 // FIXME: ParmVarDecls that are part of a function type of a parameter of
4085 // a function/objc method, should not have TU as lexical context.
Argyrios Kyrtzidisffe055a82012-02-24 01:12:38 +00004086 if (isa<ParmVarDecl>(D))
4087 return;
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004088
4089 SourceManager &SM = Context->getSourceManager();
Argyrios Kyrtzidisdf53da82011-10-28 23:57:43 +00004090 SourceLocation FileLoc = SM.getFileLoc(Loc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004091 assert(SM.isLocalSourceLocation(FileLoc));
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004092 FileID FID;
4093 unsigned Offset;
4094 llvm::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004095 if (FID.isInvalid())
4096 return;
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004097 assert(SM.getSLocEntry(FID).isFile());
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004098
Argyrios Kyrtzidis4db774a2012-10-02 21:09:17 +00004099 DeclIDInFileInfo *&Info = FileDeclIDs[FID];
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004100 if (!Info)
4101 Info = new DeclIDInFileInfo();
4102
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004103 std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004104 LocDeclIDsTy &Decls = Info->DeclIDs;
4105
Argyrios Kyrtzidis7362e9b2011-10-28 23:57:47 +00004106 if (Decls.empty() || Decls.back().first <= Offset) {
Argyrios Kyrtzidis5fc727a2011-10-28 22:54:21 +00004107 Decls.push_back(LocDecl);
4108 return;
4109 }
4110
4111 LocDeclIDsTy::iterator
4112 I = std::upper_bound(Decls.begin(), Decls.end(), LocDecl, compLocDecl);
4113
4114 Decls.insert(I, LocDecl);
4115}
4116
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004117void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00004118 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004119 Record.push_back(Name.getNameKind());
4120 switch (Name.getNameKind()) {
4121 case DeclarationName::Identifier:
4122 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
4123 break;
4124
4125 case DeclarationName::ObjCZeroArgSelector:
4126 case DeclarationName::ObjCOneArgSelector:
4127 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00004128 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004129 break;
4130
4131 case DeclarationName::CXXConstructorName:
4132 case DeclarationName::CXXDestructorName:
4133 case DeclarationName::CXXConversionFunctionName:
4134 AddTypeRef(Name.getCXXNameType(), Record);
4135 break;
4136
4137 case DeclarationName::CXXOperatorName:
4138 Record.push_back(Name.getCXXOverloadedOperator());
4139 break;
4140
Alexis Hunt3d221f22009-11-29 07:34:05 +00004141 case DeclarationName::CXXLiteralOperatorName:
4142 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
4143 break;
4144
Douglas Gregoref84c4b2009-04-09 22:27:44 +00004145 case DeclarationName::CXXUsingDirective:
4146 // No extra data to emit
4147 break;
4148 }
4149}
Chris Lattnerca025db2010-05-07 21:43:38 +00004150
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004151void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004152 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004153 switch (Name.getNameKind()) {
4154 case DeclarationName::CXXConstructorName:
4155 case DeclarationName::CXXDestructorName:
4156 case DeclarationName::CXXConversionFunctionName:
4157 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
4158 break;
4159
4160 case DeclarationName::CXXOperatorName:
4161 AddSourceLocation(
4162 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
4163 Record);
4164 AddSourceLocation(
4165 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
4166 Record);
4167 break;
4168
4169 case DeclarationName::CXXLiteralOperatorName:
4170 AddSourceLocation(
4171 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
4172 Record);
4173 break;
4174
4175 case DeclarationName::Identifier:
4176 case DeclarationName::ObjCZeroArgSelector:
4177 case DeclarationName::ObjCOneArgSelector:
4178 case DeclarationName::ObjCMultiArgSelector:
4179 case DeclarationName::CXXUsingDirective:
4180 break;
4181 }
4182}
4183
4184void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004185 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004186 AddDeclarationName(NameInfo.getName(), Record);
4187 AddSourceLocation(NameInfo.getLoc(), Record);
4188 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
4189}
4190
4191void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004192 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00004193 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00004194 Record.push_back(Info.NumTemplParamLists);
4195 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
4196 AddTemplateParameterList(Info.TemplParamLists[i], Record);
4197}
4198
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004199void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004200 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00004201 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004202 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004203 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00004204
4205 // Push each of the NNS's onto a stack for serialization in reverse order.
4206 while (NNS) {
4207 NestedNames.push_back(NNS);
4208 NNS = NNS->getPrefix();
4209 }
4210
4211 Record.push_back(NestedNames.size());
4212 while(!NestedNames.empty()) {
4213 NNS = NestedNames.pop_back_val();
4214 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
4215 Record.push_back(Kind);
4216 switch (Kind) {
4217 case NestedNameSpecifier::Identifier:
4218 AddIdentifierRef(NNS->getAsIdentifier(), Record);
4219 break;
4220
4221 case NestedNameSpecifier::Namespace:
4222 AddDeclRef(NNS->getAsNamespace(), Record);
4223 break;
4224
Douglas Gregor7b26ff92011-02-24 02:36:08 +00004225 case NestedNameSpecifier::NamespaceAlias:
4226 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
4227 break;
4228
Chris Lattnerca025db2010-05-07 21:43:38 +00004229 case NestedNameSpecifier::TypeSpec:
4230 case NestedNameSpecifier::TypeSpecWithTemplate:
4231 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
4232 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4233 break;
4234
4235 case NestedNameSpecifier::Global:
4236 // Don't need to write an associated value.
4237 break;
4238 }
4239 }
4240}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004241
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004242void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
4243 RecordDataImpl &Record) {
4244 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00004245 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004246 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00004247
4248 // Push each of the nested-name-specifiers's onto a stack for
4249 // serialization in reverse order.
4250 while (NNS) {
4251 NestedNames.push_back(NNS);
4252 NNS = NNS.getPrefix();
4253 }
4254
4255 Record.push_back(NestedNames.size());
4256 while(!NestedNames.empty()) {
4257 NNS = NestedNames.pop_back_val();
4258 NestedNameSpecifier::SpecifierKind Kind
4259 = NNS.getNestedNameSpecifier()->getKind();
4260 Record.push_back(Kind);
4261 switch (Kind) {
4262 case NestedNameSpecifier::Identifier:
4263 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
4264 AddSourceRange(NNS.getLocalSourceRange(), Record);
4265 break;
4266
4267 case NestedNameSpecifier::Namespace:
4268 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
4269 AddSourceRange(NNS.getLocalSourceRange(), Record);
4270 break;
4271
4272 case NestedNameSpecifier::NamespaceAlias:
4273 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
4274 AddSourceRange(NNS.getLocalSourceRange(), Record);
4275 break;
4276
4277 case NestedNameSpecifier::TypeSpec:
4278 case NestedNameSpecifier::TypeSpecWithTemplate:
4279 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
4280 AddTypeLoc(NNS.getTypeLoc(), Record);
4281 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4282 break;
4283
4284 case NestedNameSpecifier::Global:
4285 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
4286 break;
4287 }
4288 }
4289}
4290
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004291void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004292 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004293 Record.push_back(Kind);
4294 switch (Kind) {
4295 case TemplateName::Template:
4296 AddDeclRef(Name.getAsTemplateDecl(), Record);
4297 break;
4298
4299 case TemplateName::OverloadedTemplate: {
4300 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
4301 Record.push_back(OvT->size());
4302 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
4303 I != E; ++I)
4304 AddDeclRef(*I, Record);
4305 break;
4306 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004307
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004308 case TemplateName::QualifiedTemplate: {
4309 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
4310 AddNestedNameSpecifier(QualT->getQualifier(), Record);
4311 Record.push_back(QualT->hasTemplateKeyword());
4312 AddDeclRef(QualT->getTemplateDecl(), Record);
4313 break;
4314 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004315
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004316 case TemplateName::DependentTemplate: {
4317 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
4318 AddNestedNameSpecifier(DepT->getQualifier(), Record);
4319 Record.push_back(DepT->isIdentifier());
4320 if (DepT->isIdentifier())
4321 AddIdentifierRef(DepT->getIdentifier(), Record);
4322 else
4323 Record.push_back(DepT->getOperator());
4324 break;
4325 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004326
4327 case TemplateName::SubstTemplateTemplateParm: {
4328 SubstTemplateTemplateParmStorage *subst
4329 = Name.getAsSubstTemplateTemplateParm();
4330 AddDeclRef(subst->getParameter(), Record);
4331 AddTemplateName(subst->getReplacement(), Record);
4332 break;
4333 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004334
4335 case TemplateName::SubstTemplateTemplateParmPack: {
4336 SubstTemplateTemplateParmPackStorage *SubstPack
4337 = Name.getAsSubstTemplateTemplateParmPack();
4338 AddDeclRef(SubstPack->getParameterPack(), Record);
4339 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
4340 break;
4341 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004342 }
4343}
4344
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004345void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004346 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004347 Record.push_back(Arg.getKind());
4348 switch (Arg.getKind()) {
4349 case TemplateArgument::Null:
4350 break;
4351 case TemplateArgument::Type:
4352 AddTypeRef(Arg.getAsType(), Record);
4353 break;
4354 case TemplateArgument::Declaration:
4355 AddDeclRef(Arg.getAsDecl(), Record);
Eli Friedmanb826a002012-09-26 02:36:12 +00004356 Record.push_back(Arg.isDeclForReferenceParam());
4357 break;
4358 case TemplateArgument::NullPtr:
4359 AddTypeRef(Arg.getNullPtrType(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004360 break;
4361 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00004362 AddAPSInt(Arg.getAsIntegral(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004363 AddTypeRef(Arg.getIntegralType(), Record);
4364 break;
4365 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00004366 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
4367 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00004368 case TemplateArgument::TemplateExpansion:
4369 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00004370 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
4371 Record.push_back(*NumExpansions + 1);
4372 else
4373 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00004374 break;
4375 case TemplateArgument::Expression:
4376 AddStmt(Arg.getAsExpr());
4377 break;
4378 case TemplateArgument::Pack:
4379 Record.push_back(Arg.pack_size());
4380 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
4381 I != E; ++I)
4382 AddTemplateArgument(*I, Record);
4383 break;
4384 }
4385}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004386
4387void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004388ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004389 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004390 assert(TemplateParams && "No TemplateParams!");
4391 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
4392 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
4393 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
4394 Record.push_back(TemplateParams->size());
4395 for (TemplateParameterList::const_iterator
4396 P = TemplateParams->begin(), PEnd = TemplateParams->end();
4397 P != PEnd; ++P)
4398 AddDeclRef(*P, Record);
4399}
4400
4401/// \brief Emit a template argument list.
4402void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004403ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004404 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004405 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00004406 Record.push_back(TemplateArgs->size());
4407 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00004408 AddTemplateArgument(TemplateArgs->get(i), Record);
4409}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004410
4411
4412void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004413ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00004414 Record.push_back(Set.size());
4415 for (UnresolvedSetImpl::const_iterator
4416 I = Set.begin(), E = Set.end(); I != E; ++I) {
4417 AddDeclRef(I.getDecl(), Record);
4418 Record.push_back(I.getAccess());
4419 }
4420}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004421
Sebastian Redl55c0ad52010-08-18 23:56:21 +00004422void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004423 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004424 Record.push_back(Base.isVirtual());
4425 Record.push_back(Base.isBaseOfClass());
4426 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00004427 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00004428 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004429 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00004430 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
4431 : SourceLocation(),
4432 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00004433}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004434
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004435void ASTWriter::FlushCXXBaseSpecifiers() {
4436 RecordData Record;
4437 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
4438 Record.clear();
4439
4440 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00004441 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004442 if (Index == CXXBaseSpecifiersOffsets.size())
4443 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
4444 else {
4445 if (Index > CXXBaseSpecifiersOffsets.size())
4446 CXXBaseSpecifiersOffsets.resize(Index + 1);
4447 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
4448 }
4449
4450 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
4451 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
4452 Record.push_back(BEnd - B);
4453 for (; B != BEnd; ++B)
4454 AddCXXBaseSpecifier(*B, Record);
4455 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00004456
4457 // Flush any expressions that were written as part of the base specifiers.
4458 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004459 }
4460
4461 CXXBaseSpecifiersToWrite.clear();
4462}
4463
Alexis Hunt1d792652011-01-08 20:30:50 +00004464void ASTWriter::AddCXXCtorInitializers(
4465 const CXXCtorInitializer * const *CtorInitializers,
4466 unsigned NumCtorInitializers,
4467 RecordDataImpl &Record) {
4468 Record.push_back(NumCtorInitializers);
4469 for (unsigned i=0; i != NumCtorInitializers; ++i) {
4470 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004471
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004472 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004473 Record.push_back(CTOR_INITIALIZER_BASE);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004474 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004475 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00004476 } else if (Init->isDelegatingInitializer()) {
4477 Record.push_back(CTOR_INITIALIZER_DELEGATING);
Douglas Gregord73f3dd2011-11-01 01:16:03 +00004478 AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
Alexis Hunt37a477f2011-05-04 01:19:08 +00004479 } else if (Init->isMemberInitializer()){
4480 Record.push_back(CTOR_INITIALIZER_MEMBER);
4481 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004482 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00004483 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
4484 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004485 }
Francois Pichetd583da02010-12-04 09:14:42 +00004486
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004487 AddSourceLocation(Init->getMemberLocation(), Record);
4488 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00004489 AddSourceLocation(Init->getLParenLoc(), Record);
4490 AddSourceLocation(Init->getRParenLoc(), Record);
4491 Record.push_back(Init->isWritten());
4492 if (Init->isWritten()) {
4493 Record.push_back(Init->getSourceOrder());
4494 } else {
4495 Record.push_back(Init->getNumArrayIndices());
4496 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
4497 AddDeclRef(Init->getArrayIndex(i), Record);
4498 }
4499 }
4500}
4501
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004502void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
4503 assert(D->DefinitionData);
4504 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Douglas Gregor99ae8062012-02-14 17:54:36 +00004505 Record.push_back(Data.IsLambda);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004506 Record.push_back(Data.UserDeclaredConstructor);
4507 Record.push_back(Data.UserDeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004508 Record.push_back(Data.UserDeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004509 Record.push_back(Data.UserDeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004510 Record.push_back(Data.UserDeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004511 Record.push_back(Data.UserDeclaredDestructor);
4512 Record.push_back(Data.Aggregate);
4513 Record.push_back(Data.PlainOldData);
4514 Record.push_back(Data.Empty);
4515 Record.push_back(Data.Polymorphic);
4516 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00004517 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00004518 Record.push_back(Data.HasNoNonEmptyBases);
4519 Record.push_back(Data.HasPrivateFields);
4520 Record.push_back(Data.HasProtectedFields);
4521 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00004522 Record.push_back(Data.HasMutableFields);
Richard Smith561fb152012-02-25 07:33:38 +00004523 Record.push_back(Data.HasOnlyCMembers);
Richard Smithe2648ba2012-05-07 01:07:30 +00004524 Record.push_back(Data.HasInClassInitializer);
Alexis Huntf479f1b2011-05-09 18:22:59 +00004525 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00004526 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Richard Smith561fb152012-02-25 07:33:38 +00004527 Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
Richard Smith561fb152012-02-25 07:33:38 +00004528 Record.push_back(Data.HasConstexprDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004529 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004530 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004531 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00004532 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004533 Record.push_back(Data.HasTrivialDestructor);
Richard Smith561fb152012-02-25 07:33:38 +00004534 Record.push_back(Data.HasIrrelevantDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00004535 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004536 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00004537 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004538 Record.push_back(Data.DeclaredDefaultConstructor);
4539 Record.push_back(Data.DeclaredCopyConstructor);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004540 Record.push_back(Data.DeclaredMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004541 Record.push_back(Data.DeclaredCopyAssignment);
Douglas Gregorcd0d8262011-09-06 16:38:46 +00004542 Record.push_back(Data.DeclaredMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004543 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00004544 Record.push_back(Data.FailedImplicitMoveConstructor);
4545 Record.push_back(Data.FailedImplicitMoveAssignment);
Richard Smith561fb152012-02-25 07:33:38 +00004546 // IsLambda bit is already saved.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004547
4548 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004549 if (Data.NumBases > 0)
4550 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
4551 Record);
4552
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004553 // FIXME: Make VBases lazily computed when needed to avoid storing them.
4554 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00004555 if (Data.NumVBases > 0)
4556 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
4557 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004558
4559 AddUnresolvedSet(Data.Conversions, Record);
4560 AddUnresolvedSet(Data.VisibleConversions, Record);
4561 // Data.Definition is the owning decl, no need to write it.
4562 AddDeclRef(Data.FirstFriend, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004563
4564 // Add lambda-specific data.
4565 if (Data.IsLambda) {
4566 CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
Douglas Gregor680e9e02012-02-21 19:11:17 +00004567 Record.push_back(Lambda.Dependent);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004568 Record.push_back(Lambda.NumCaptures);
4569 Record.push_back(Lambda.NumExplicitCaptures);
Douglas Gregor63798542012-02-20 19:44:39 +00004570 Record.push_back(Lambda.ManglingNumber);
Douglas Gregor7fcbd902012-02-21 00:37:24 +00004571 AddDeclRef(Lambda.ContextDecl, Record);
Eli Friedmand564afb2012-09-19 01:18:11 +00004572 AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
Douglas Gregor99ae8062012-02-14 17:54:36 +00004573 for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
4574 LambdaExpr::Capture &Capture = Lambda.Captures[I];
4575 AddSourceLocation(Capture.getLocation(), Record);
4576 Record.push_back(Capture.isImplicit());
4577 Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
4578 VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
4579 AddDeclRef(Var, Record);
4580 AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
4581 : SourceLocation(),
4582 Record);
4583 }
4584 }
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00004585}
4586
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004587void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00004588 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00004589 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004590 assert(FirstDeclID == NextDeclID &&
4591 FirstTypeID == NextTypeID &&
4592 FirstIdentID == NextIdentID &&
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004593 FirstMacroID == NextMacroID &&
Douglas Gregor253eefe2011-12-01 00:59:36 +00004594 FirstSubmoduleID == NextSubmoduleID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004595 FirstSelectorID == NextSelectorID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004596 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004597
Sebastian Redl07a89a82010-07-30 00:29:29 +00004598 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004599
Douglas Gregordf0c1512011-08-18 04:12:04 +00004600 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
4601 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
4602 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004603 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
Douglas Gregor253eefe2011-12-01 00:59:36 +00004604 FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
Douglas Gregordf0c1512011-08-18 04:12:04 +00004605 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004606 NextDeclID = FirstDeclID;
4607 NextTypeID = FirstTypeID;
4608 NextIdentID = FirstIdentID;
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004609 NextMacroID = FirstMacroID;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004610 NextSelectorID = FirstSelectorID;
Douglas Gregor253eefe2011-12-01 00:59:36 +00004611 NextSubmoduleID = FirstSubmoduleID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004612}
4613
Sebastian Redl539c5062010-08-18 23:57:32 +00004614void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004615 IdentifierIDs[II] = ID;
4616}
4617
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004618void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
4619 MacroIDs[MI] = ID;
4620}
4621
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004622void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004623 // Always take the highest-numbered type index. This copes with an interesting
4624 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004625 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004626 // keep the higher-numbered entry so that we can properly write it out to
4627 // the AST file.
4628 TypeIdx &StoredIdx = TypeIdxs[T];
4629 if (Idx.getIndex() >= StoredIdx.getIndex())
4630 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004631}
4632
Sebastian Redl539c5062010-08-18 23:57:32 +00004633void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004634 SelectorIDs[S] = ID;
4635}
Douglas Gregor91096292010-10-02 19:29:26 +00004636
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004637void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004638 MacroDefinition *MD) {
Argyrios Kyrtzidis03c40c52011-09-15 18:02:56 +00004639 assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
Douglas Gregor91096292010-10-02 19:29:26 +00004640 MacroDefinitions[MD] = ID;
4641}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004642
Douglas Gregore37a85a2011-12-02 17:30:13 +00004643void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
4644 assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
4645 SubmoduleIDs[Mod] = ID;
4646}
4647
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004648void ASTWriter::UndefinedMacro(MacroInfo *MI) {
4649 MacroUpdates[MI].UndefLoc = MI->getUndefLoc();
4650}
4651
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004652void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
John McCallf937c022011-10-07 06:10:15 +00004653 assert(D->isCompleteDefinition());
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004654 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004655 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4656 // We are interested when a PCH decl is modified.
Douglas Gregorb3722e22011-09-09 23:01:35 +00004657 if (RD->isFromASTFile()) {
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004658 // A forward reference was mutated into a definition. Rewrite it.
4659 // FIXME: This happens during template instantiation, should we
4660 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004661 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004662 }
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004663 }
4664}
Douglas Gregorcb28f9d2012-10-09 23:05:51 +00004665
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004666void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004667 assert(!WritingAST && "Already writing the AST!");
4668
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004669 // TU and namespaces are handled elsewhere.
4670 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4671 return;
4672
Douglas Gregorb3722e22011-09-09 23:01:35 +00004673 if (!(!D->isFromASTFile() && cast<Decl>(DC)->isFromASTFile()))
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004674 return; // Not a source decl added to a DeclContext from PCH.
4675
4676 AddUpdatedDeclContext(DC);
Argyrios Kyrtzidis442dd802012-07-02 19:19:01 +00004677 UpdatingVisibleDecls.push_back(D);
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004678}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004679
4680void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004681 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004682 assert(D->isImplicit());
Douglas Gregorb3722e22011-09-09 23:01:35 +00004683 if (!(!D->isFromASTFile() && RD->isFromASTFile()))
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004684 return; // Not a source member added to a class from PCH.
4685 if (!isa<CXXMethodDecl>(D))
4686 return; // We are interested in lazily declared implicit methods.
4687
4688 // A decl coming from PCH was modified.
John McCallf937c022011-10-07 06:10:15 +00004689 assert(RD->isCompleteDefinition());
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004690 UpdateRecord &Record = DeclUpdates[RD];
4691 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004692 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004693}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004694
4695void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4696 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004697 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004698 assert(!WritingAST && "Already writing the AST!");
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004699 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004700 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004701 return; // Not a source specialization added to a template from PCH.
4702
4703 UpdateRecord &Record = DeclUpdates[TD];
4704 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004705 Record.push_back(reinterpret_cast<uint64_t>(D));
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004706}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004707
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004708void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4709 const FunctionDecl *D) {
4710 // The specializations set is kept in the canonical template.
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004711 assert(!WritingAST && "Already writing the AST!");
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004712 TD = TD->getCanonicalDecl();
Douglas Gregorb3722e22011-09-09 23:01:35 +00004713 if (!(!D->isFromASTFile() && TD->isFromASTFile()))
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004714 return; // Not a source specialization added to a template from PCH.
4715
4716 UpdateRecord &Record = DeclUpdates[TD];
4717 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004718 Record.push_back(reinterpret_cast<uint64_t>(D));
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004719}
4720
Sebastian Redlab238a72011-04-24 16:28:06 +00004721void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004722 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004723 if (!D->isFromASTFile())
Sebastian Redlab238a72011-04-24 16:28:06 +00004724 return; // Declaration not imported from PCH.
4725
4726 // Implicit decl from a PCH was defined.
4727 // FIXME: Should implicit definition be a separate FunctionDecl?
4728 RewriteDecl(D);
4729}
4730
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004731void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004732 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004733 if (!D->isFromASTFile())
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004734 return;
4735
4736 // Since the actual instantiation is delayed, this really means that we need
4737 // to update the instantiation location.
4738 UpdateRecord &Record = DeclUpdates[D];
4739 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4740 AddSourceLocation(
4741 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4742}
4743
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004744void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
4745 const ObjCInterfaceDecl *IFD) {
Douglas Gregor2fd3d402011-09-17 00:05:03 +00004746 assert(!WritingAST && "Already writing the AST!");
Douglas Gregorb3722e22011-09-09 23:01:35 +00004747 if (!IFD->isFromASTFile())
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004748 return; // Declaration not imported from PCH.
Douglas Gregor404cdde2012-01-27 01:47:08 +00004749
4750 assert(IFD->getDefinition() && "Category on a class without a definition?");
4751 ObjCClassesWithCategories.insert(
4752 const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
Argyrios Kyrtzidis7d847c92011-09-01 00:58:55 +00004753}
Argyrios Kyrtzidisb97a4022011-11-12 21:07:46 +00004754
Argyrios Kyrtzidis0ca3a8b2011-11-12 21:07:52 +00004755
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +00004756void ASTWriter::AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
4757 const ObjCPropertyDecl *OrigProp,
4758 const ObjCCategoryDecl *ClassExt) {
4759 const ObjCInterfaceDecl *D = ClassExt->getClassInterface();
4760 if (!D)
4761 return;
4762
4763 assert(!WritingAST && "Already writing the AST!");
4764 if (!D->isFromASTFile())
4765 return; // Declaration not imported from PCH.
4766
4767 RewriteDecl(D);
4768}